|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { expect } = require('chai'); |
| 4 | +const { MongoError } = require('../../../index'); |
| 5 | + |
| 6 | +describe('Cursor Async Iterator Tests', function() { |
| 7 | + let client, collection; |
| 8 | + before(async function() { |
| 9 | + client = this.configuration.newClient(); |
| 10 | + |
| 11 | + await client.connect(); |
| 12 | + const docs = Array.from({ length: 1000 }).map((_, index) => ({ foo: index, bar: 1 })); |
| 13 | + |
| 14 | + collection = client.db(this.configuration.db).collection('async_cursor_tests'); |
| 15 | + |
| 16 | + await collection.deleteMany({}); |
| 17 | + await collection.insertMany(docs); |
| 18 | + await client.close(); |
| 19 | + }); |
| 20 | + |
| 21 | + beforeEach(async function() { |
| 22 | + client = this.configuration.newClient(); |
| 23 | + await client.connect(); |
| 24 | + collection = client.db(this.configuration.db).collection('async_cursor_tests'); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => client.close()); |
| 28 | + |
| 29 | + it('should be able to use a for-await loop on a find command cursor', { |
| 30 | + metadata: { requires: { node: '>=10.5.0' } }, |
| 31 | + test: async function() { |
| 32 | + const cursor = collection.find({ bar: 1 }); |
| 33 | + |
| 34 | + let counter = 0; |
| 35 | + for await (const doc of cursor) { |
| 36 | + expect(doc).to.have.property('bar', 1); |
| 37 | + counter += 1; |
| 38 | + } |
| 39 | + |
| 40 | + expect(counter).to.equal(1000); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + it('should be able to use a for-await loop on an aggregation cursor', { |
| 45 | + metadata: { requires: { node: '>=10.5.0' } }, |
| 46 | + test: async function() { |
| 47 | + const cursor = collection.aggregate([{ $match: { bar: 1 } }]); |
| 48 | + |
| 49 | + let counter = 0; |
| 50 | + for await (const doc of cursor) { |
| 51 | + expect(doc).to.have.property('bar', 1); |
| 52 | + counter += 1; |
| 53 | + } |
| 54 | + |
| 55 | + expect(counter).to.equal(1000); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + it('should be able to use a for-await loop on a command cursor', { |
| 60 | + metadata: { requires: { node: '>=10.5.0', mongodb: '>=3.0.0' } }, |
| 61 | + test: async function() { |
| 62 | + const cursor1 = collection.listIndexes(); |
| 63 | + const cursor2 = collection.listIndexes(); |
| 64 | + |
| 65 | + const indexes = await cursor1.toArray(); |
| 66 | + let counter = 0; |
| 67 | + for await (const doc of cursor2) { |
| 68 | + expect(doc).to.exist; |
| 69 | + counter += 1; |
| 70 | + } |
| 71 | + |
| 72 | + expect(counter).to.equal(indexes.length); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it('should properly error when cursor is closed', { |
| 77 | + metadata: { requires: { node: '>=10.5.0' } }, |
| 78 | + test: async function() { |
| 79 | + const cursor = collection.find(); |
| 80 | + |
| 81 | + try { |
| 82 | + for await (const doc of cursor) { |
| 83 | + expect(doc).to.exist; |
| 84 | + cursor.close(); |
| 85 | + } |
| 86 | + throw new Error('expected closing the cursor to break iteration'); |
| 87 | + } catch (e) { |
| 88 | + expect(e).to.be.an.instanceOf(MongoError); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | +}); |
0 commit comments