|
1 | 1 | import { expect } from 'chai';
|
| 2 | +import { on } from 'events'; |
2 | 3 |
|
3 | 4 | import { MongoClient, MongoError, ObjectId, ReturnDocument } from '../../mongodb';
|
4 | 5 | import { assert as test } from '../shared';
|
@@ -60,130 +61,92 @@ describe('CRUD API', function () {
|
60 | 61 | await client.close();
|
61 | 62 | });
|
62 | 63 |
|
63 |
| - it('should correctly execute find method using crud api', function (done) { |
64 |
| - const db = client.db(); |
65 |
| - |
66 |
| - db.collection('t').insertMany([{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }], function (err) { |
67 |
| - expect(err).to.not.exist; |
68 |
| - |
69 |
| - // |
70 |
| - // Cursor |
71 |
| - // -------------------------------------------------- |
72 |
| - const makeCursor = () => { |
73 |
| - // Possible methods on the the cursor instance |
74 |
| - return db |
75 |
| - .collection('t') |
76 |
| - .find({}) |
77 |
| - .filter({ a: 1 }) |
78 |
| - .addCursorFlag('noCursorTimeout', true) |
79 |
| - .addQueryModifier('$comment', 'some comment') |
80 |
| - .batchSize(2) |
81 |
| - .comment('some comment 2') |
82 |
| - .limit(2) |
83 |
| - .maxTimeMS(50) |
84 |
| - .project({ a: 1 }) |
85 |
| - .skip(0) |
86 |
| - .sort({ a: 1 }); |
87 |
| - }; |
| 64 | + context('when creating a cursor with find', () => { |
| 65 | + let collection; |
88 | 66 |
|
89 |
| - // |
90 |
| - // Exercise count method |
91 |
| - // ------------------------------------------------- |
92 |
| - const countMethod = function () { |
93 |
| - // Execute the different methods supported by the cursor |
94 |
| - const cursor = makeCursor(); |
95 |
| - cursor.count(function (err, count) { |
96 |
| - expect(err).to.not.exist; |
97 |
| - test.equal(2, count); |
98 |
| - eachMethod(); |
99 |
| - }); |
100 |
| - }; |
| 67 | + beforeEach(async () => { |
| 68 | + collection = client.db().collection('t'); |
| 69 | + await collection.drop().catch(() => null); |
| 70 | + await collection.insertMany([{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }]); |
| 71 | + }); |
101 | 72 |
|
102 |
| - // |
103 |
| - // Exercise legacy method each |
104 |
| - // ------------------------------------------------- |
105 |
| - const eachMethod = function () { |
106 |
| - let count = 0; |
| 73 | + afterEach(async () => { |
| 74 | + await collection?.drop().catch(() => null); |
| 75 | + }); |
107 | 76 |
|
| 77 | + const makeCursor = () => { |
| 78 | + // Possible methods on the the cursor instance |
| 79 | + return collection |
| 80 | + .find({}) |
| 81 | + .filter({ a: 1 }) |
| 82 | + .addCursorFlag('noCursorTimeout', true) |
| 83 | + .addQueryModifier('$comment', 'some comment') |
| 84 | + .batchSize(1) |
| 85 | + .comment('some comment 2') |
| 86 | + .limit(2) |
| 87 | + .maxTimeMS(50) |
| 88 | + .project({ a: 1 }) |
| 89 | + .skip(0) |
| 90 | + .sort({ a: 1 }); |
| 91 | + }; |
| 92 | + |
| 93 | + describe('#count()', () => { |
| 94 | + it('returns the number of documents', async () => { |
108 | 95 | const cursor = makeCursor();
|
109 |
| - cursor.forEach( |
110 |
| - () => { |
111 |
| - count = count + 1; |
112 |
| - }, |
113 |
| - err => { |
114 |
| - expect(err).to.not.exist; |
115 |
| - test.equal(2, count); |
116 |
| - toArrayMethod(); |
117 |
| - } |
118 |
| - ); |
119 |
| - }; |
| 96 | + const res = await cursor.count(); |
| 97 | + expect(res).to.equal(2); |
| 98 | + }); |
| 99 | + }); |
120 | 100 |
|
121 |
| - // |
122 |
| - // Exercise toArray |
123 |
| - // ------------------------------------------------- |
124 |
| - const toArrayMethod = function () { |
| 101 | + describe('#forEach()', () => { |
| 102 | + it('iterates all the documents', async () => { |
125 | 103 | const cursor = makeCursor();
|
126 |
| - cursor.toArray(function (err, docs) { |
127 |
| - expect(err).to.not.exist; |
128 |
| - test.equal(2, docs.length); |
129 |
| - nextMethod(); |
| 104 | + let count = 0; |
| 105 | + await cursor.forEach(() => { |
| 106 | + count += 1; |
130 | 107 | });
|
131 |
| - }; |
| 108 | + expect(count).to.equal(2); |
| 109 | + }); |
| 110 | + }); |
132 | 111 |
|
133 |
| - // |
134 |
| - // Exercise next method |
135 |
| - // ------------------------------------------------- |
136 |
| - const nextMethod = function () { |
| 112 | + describe('#toArray()', () => { |
| 113 | + it('returns an array with all documents', async () => { |
137 | 114 | const cursor = makeCursor();
|
138 |
| - cursor.next(function (err, doc) { |
139 |
| - expect(err).to.not.exist; |
140 |
| - test.ok(doc != null); |
141 |
| - |
142 |
| - cursor.next(function (err, doc) { |
143 |
| - expect(err).to.not.exist; |
144 |
| - test.ok(doc != null); |
| 115 | + const res = await cursor.toArray(); |
| 116 | + expect(res).to.have.lengthOf(2); |
| 117 | + }); |
| 118 | + }); |
145 | 119 |
|
146 |
| - cursor.next(function (err, doc) { |
147 |
| - expect(err).to.not.exist; |
148 |
| - expect(doc).to.not.exist; |
149 |
| - streamMethod(); |
150 |
| - }); |
151 |
| - }); |
152 |
| - }); |
153 |
| - }; |
| 120 | + describe('#next()', () => { |
| 121 | + it('is callable without blocking', async () => { |
| 122 | + const cursor = makeCursor(); |
| 123 | + const doc0 = await cursor.next(); |
| 124 | + expect(doc0).to.exist; |
| 125 | + const doc1 = await cursor.next(); |
| 126 | + expect(doc1).to.exist; |
| 127 | + const doc2 = await cursor.next(); |
| 128 | + expect(doc2).to.not.exist; |
| 129 | + }); |
| 130 | + }); |
154 | 131 |
|
155 |
| - // |
156 |
| - // Exercise stream |
157 |
| - // ------------------------------------------------- |
158 |
| - const streamMethod = function () { |
159 |
| - let count = 0; |
| 132 | + describe('#stream()', () => { |
| 133 | + it('creates a node stream that emits data events', async () => { |
| 134 | + const count = 0; |
160 | 135 | const cursor = makeCursor();
|
161 | 136 | const stream = cursor.stream();
|
162 |
| - stream.on('data', function () { |
163 |
| - count = count + 1; |
164 |
| - }); |
165 |
| - |
| 137 | + on(stream, 'data'); |
166 | 138 | cursor.once('close', function () {
|
167 |
| - test.equal(2, count); |
168 |
| - explainMethod(); |
| 139 | + expect(count).to.equal(2); |
169 | 140 | });
|
170 |
| - }; |
| 141 | + }); |
| 142 | + }); |
171 | 143 |
|
172 |
| - // |
173 |
| - // Explain method |
174 |
| - // ------------------------------------------------- |
175 |
| - const explainMethod = function () { |
| 144 | + describe('#explain()', () => { |
| 145 | + it('returns an explain document', async () => { |
176 | 146 | const cursor = makeCursor();
|
177 |
| - cursor.explain(function (err, result) { |
178 |
| - expect(err).to.not.exist; |
179 |
| - test.ok(result != null); |
180 |
| - |
181 |
| - client.close(done); |
182 |
| - }); |
183 |
| - }; |
184 |
| - |
185 |
| - // Execute all the methods |
186 |
| - countMethod(); |
| 147 | + const result = await cursor.explain(); |
| 148 | + expect(result).to.exist; |
| 149 | + }); |
187 | 150 | });
|
188 | 151 | });
|
189 | 152 |
|
|
0 commit comments