|
1 | 1 | import { expect } from 'chai'; |
2 | 2 |
|
3 | | -import { type Db, MongoClient, MongoInvalidArgumentError, MongoServerError } from '../../mongodb'; |
4 | | -import { assert as test, setupDatabase } from '../shared'; |
| 3 | +import { |
| 4 | + Collection, |
| 5 | + type Db, |
| 6 | + MongoClient, |
| 7 | + MongoInvalidArgumentError, |
| 8 | + MongoServerError |
| 9 | +} from '../../../src'; |
| 10 | +import { setupDatabase } from '../shared'; |
5 | 11 |
|
6 | 12 | describe('Db', function () { |
7 | 13 | before(function () { |
@@ -52,236 +58,123 @@ describe('Db', function () { |
52 | 58 | expect(error).to.be.instanceOf(Error); |
53 | 59 | }); |
54 | 60 |
|
55 | | - it('shouldCorrectlyGetErrorDroppingNonExistingDb', { |
56 | | - metadata: { |
57 | | - requires: { topology: ['single', 'replicaset', 'sharded'] } |
58 | | - }, |
59 | | - |
60 | | - test: function (done) { |
61 | | - const configuration = this.configuration; |
62 | | - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
63 | | - client.connect(function (err, client) { |
64 | | - const _db = client.db('nonexistingdb'); |
65 | | - |
66 | | - _db.dropDatabase(function (err, result) { |
67 | | - expect(err).to.not.exist; |
68 | | - test.equal(true, result); |
69 | | - |
70 | | - client.close(done); |
71 | | - }); |
72 | | - }); |
73 | | - } |
| 61 | + it('should not throw error dropping non existing Db', async function () { |
| 62 | + const configuration = this.configuration; |
| 63 | + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
| 64 | + const _db = client.db('nonexistingdb'); |
| 65 | + await _db.dropDatabase(); |
| 66 | + await client.close(); |
74 | 67 | }); |
75 | 68 |
|
76 | | - it.skip('shouldCorrectlyThrowWhenTryingToReOpenConnection', { |
77 | | - metadata: { |
78 | | - requires: { topology: ['single', 'replicaset', 'sharded'] } |
79 | | - }, |
80 | | - |
81 | | - test: function (done) { |
82 | | - const configuration = this.configuration; |
83 | | - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
84 | | - client.connect(err => { |
85 | | - expect(err).to.not.exist; |
86 | | - |
87 | | - try { |
88 | | - client.connect(function () {}); |
89 | | - test.ok(false); |
90 | | - } catch { |
91 | | - client.close(done); |
92 | | - } |
93 | | - }); |
94 | | - } |
| 69 | + // TODO(NODE-7192): remove test as it doesn't test anything |
| 70 | + // it.skip('shouldCorrectlyThrowWhenTryingToReOpenConnection', { |
| 71 | + // metadata: { |
| 72 | + // requires: { topology: ['single', 'replicaset', 'sharded'] } |
| 73 | + // }, |
| 74 | + // |
| 75 | + // test: function (done) { |
| 76 | + // var configuration = this.configuration; |
| 77 | + // var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
| 78 | + // client.connect(err => { |
| 79 | + // expect(err).to.not.exist; |
| 80 | + // |
| 81 | + // try { |
| 82 | + // client.connect(function () {}); |
| 83 | + // test.ok(false); |
| 84 | + // } catch { |
| 85 | + // client.close(done); |
| 86 | + // } |
| 87 | + // }); |
| 88 | + // } |
| 89 | + // }); |
| 90 | + |
| 91 | + it('should not cut collection name when it is the same as the database', async function () { |
| 92 | + const configuration = this.configuration; |
| 93 | + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
| 94 | + const db1 = client.db('node972'); |
| 95 | + await db1.collection('node972.test').insertOne({ a: 1 }); |
| 96 | + |
| 97 | + const collections = await db1.collections(); |
| 98 | + const collection = collections.find(c => c.collectionName === 'node972.test'); |
| 99 | + expect(collection).to.be.instanceOf(Collection); |
| 100 | + await client.close(); |
95 | 101 | }); |
96 | 102 |
|
97 | | - it('should not cut collection name when it is the same as the database', { |
98 | | - metadata: { |
99 | | - requires: { topology: ['single', 'replicaset', 'sharded'] } |
100 | | - }, |
101 | | - |
102 | | - test: function (done) { |
103 | | - const configuration = this.configuration; |
104 | | - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
105 | | - client.connect(function (err, client) { |
106 | | - expect(err).to.not.exist; |
107 | | - |
108 | | - const db1 = client.db('node972'); |
109 | | - db1.collection('node972.test').insertOne({ a: 1 }, function (err) { |
110 | | - expect(err).to.not.exist; |
111 | | - |
112 | | - db1.collections(function (err, collections) { |
113 | | - expect(err).to.not.exist; |
114 | | - collections = collections.map(function (c) { |
115 | | - return c.collectionName; |
116 | | - }); |
117 | | - test.notEqual(-1, collections.indexOf('node972.test')); |
118 | | - client.close(done); |
119 | | - }); |
120 | | - }); |
121 | | - }); |
122 | | - } |
123 | | - }); |
| 103 | + it('should correctly use cursor with list collections command', async function () { |
| 104 | + const configuration = this.configuration; |
| 105 | + |
| 106 | + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
124 | 107 |
|
125 | | - it('shouldCorrectlyUseCursorWithListCollectionsCommand', { |
126 | | - metadata: { |
127 | | - requires: { topology: ['single', 'replicaset', 'sharded'] } |
128 | | - }, |
| 108 | + const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommand'); |
129 | 109 |
|
130 | | - test: function (done) { |
131 | | - const configuration = this.configuration; |
| 110 | + // create 2 collections by inserting documents in them |
| 111 | + await db1.collection('test').insertOne({ a: 1 }); |
| 112 | + await db1.collection('test1').insertOne({ a: 1 }); |
132 | 113 |
|
133 | | - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
134 | | - client.connect(function (err, client) { |
135 | | - expect(err).to.not.exist; |
| 114 | + // Get listCollections filtering out the name |
| 115 | + const collections = await db1.listCollections({ name: 'test1' }).toArray(); |
| 116 | + expect(collections.length).to.equal(1); |
| 117 | + await client.close(); |
| 118 | + }); |
136 | 119 |
|
137 | | - // Get a db we that does not have any collections |
138 | | - const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommand'); |
| 120 | + it('should correctly use cursor with listCollections command and batchSize', async function () { |
| 121 | + const configuration = this.configuration; |
139 | 122 |
|
140 | | - // Create a collection |
141 | | - db1.collection('test').insertOne({ a: 1 }, function (err) { |
142 | | - expect(err).to.not.exist; |
| 123 | + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
| 124 | + const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommandAndBatchSize'); |
143 | 125 |
|
144 | | - // Create a collection |
145 | | - db1.collection('test1').insertOne({ a: 1 }, function () { |
146 | | - expect(err).to.not.exist; |
| 126 | + await db1.collection('test').insertOne({ a: 1 }); |
147 | 127 |
|
148 | | - // Get listCollections filtering out the name |
149 | | - const cursor = db1.listCollections({ name: 'test1' }); |
150 | | - cursor.toArray(function (err, names) { |
151 | | - expect(err).to.not.exist; |
152 | | - test.equal(1, names.length); |
| 128 | + await db1.collection('test1').insertOne({ a: 1 }); |
153 | 129 |
|
154 | | - client.close(done); |
155 | | - }); |
156 | | - }); |
157 | | - }); |
158 | | - }); |
159 | | - } |
| 130 | + // Get listCollections filtering out the name |
| 131 | + const collections = await db1.listCollections({ name: 'test' }, { batchSize: 1 }).toArray(); |
| 132 | + expect(collections.length).to.equal(1); |
| 133 | + await client.close(); |
160 | 134 | }); |
161 | 135 |
|
162 | | - it('shouldCorrectlyUseCursorWithListCollectionsCommandAndBatchSize', { |
163 | | - metadata: { |
164 | | - requires: { topology: ['single', 'replicaset', 'sharded'] } |
165 | | - }, |
| 136 | + it('should correctly list collection names with . in the middle', async function () { |
| 137 | + const configuration = this.configuration; |
166 | 138 |
|
167 | | - test: function (done) { |
168 | | - const configuration = this.configuration; |
| 139 | + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
| 140 | + const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThem'); |
169 | 141 |
|
170 | | - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
171 | | - client.connect(function (err, client) { |
172 | | - expect(err).to.not.exist; |
| 142 | + await db1.collection('test.collection1').insertOne({ a: 1 }); |
| 143 | + await db1.collection('test.collection2').insertOne({ a: 1 }); |
173 | 144 |
|
174 | | - // Get a db we that does not have any collections |
175 | | - const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommandAndBatchSize'); |
| 145 | + const collections = await db1.listCollections({ name: /test.collection/ }).toArray(); |
| 146 | + expect(collections.length).to.equal(2); |
176 | 147 |
|
177 | | - // Create a collection |
178 | | - db1.collection('test').insertOne({ a: 1 }, function (err) { |
179 | | - expect(err).to.not.exist; |
| 148 | + // Get listCollections filtering out the name |
| 149 | + const filteredCollections = await db1 |
| 150 | + .listCollections({ name: 'test.collection1' }, {}) |
| 151 | + .toArray(); |
| 152 | + expect(filteredCollections.length).to.equal(1); |
| 153 | + await client.close(); |
| 154 | + }); |
180 | 155 |
|
181 | | - // Create a collection |
182 | | - db1.collection('test1').insertOne({ a: 1 }, function () { |
183 | | - expect(err).to.not.exist; |
| 156 | + it('should correctly list collection names with batchSize 1', async function () { |
| 157 | + const configuration = this.configuration; |
184 | 158 |
|
185 | | - // Get listCollections filtering out the name |
186 | | - const cursor = db1.listCollections({ name: 'test' }, { batchSize: 1 }); |
187 | | - cursor.toArray(function (err, names) { |
188 | | - expect(err).to.not.exist; |
189 | | - test.equal(1, names.length); |
| 159 | + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
| 160 | + const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThemFor28'); |
190 | 161 |
|
191 | | - client.close(done); |
192 | | - }); |
193 | | - }); |
194 | | - }); |
195 | | - }); |
196 | | - } |
197 | | - }); |
| 162 | + await db1.collection('test.collection1').insertOne({ a: 1 }); |
198 | 163 |
|
199 | | - it('should correctly list collection names with . in the middle', { |
200 | | - metadata: { |
201 | | - requires: { topology: ['single', 'replicaset', 'sharded'] } |
202 | | - }, |
203 | | - |
204 | | - test: function (done) { |
205 | | - const configuration = this.configuration; |
206 | | - |
207 | | - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
208 | | - client.connect(function (err, client) { |
209 | | - expect(err).to.not.exist; |
210 | | - |
211 | | - // Get a db we that does not have any collections |
212 | | - const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThem'); |
213 | | - |
214 | | - // Create a collection |
215 | | - db1.collection('test.collection1').insertOne({ a: 1 }, function (err) { |
216 | | - expect(err).to.not.exist; |
217 | | - |
218 | | - // Create a collection |
219 | | - db1.collection('test.collection2').insertOne({ a: 1 }, function () { |
220 | | - expect(err).to.not.exist; |
221 | | - |
222 | | - // Get listCollections filtering out the name |
223 | | - const cursor = db1.listCollections({ name: /test.collection/ }); |
224 | | - cursor.toArray(function (err, names) { |
225 | | - expect(err).to.not.exist; |
226 | | - test.equal(2, names.length); |
227 | | - |
228 | | - // Get listCollections filtering out the name |
229 | | - const cursor = db1.listCollections({ name: 'test.collection1' }, {}); |
230 | | - cursor.toArray(function (err, names) { |
231 | | - expect(err).to.not.exist; |
232 | | - test.equal(1, names.length); |
233 | | - |
234 | | - client.close(done); |
235 | | - }); |
236 | | - }); |
237 | | - }); |
238 | | - }); |
239 | | - }); |
240 | | - } |
241 | | - }); |
| 164 | + await db1.collection('test.collection2').insertOne({ a: 1 }); |
242 | 165 |
|
243 | | - it('should correctly list collection names with batchSize 1 for 2.8 or higher', { |
244 | | - metadata: { |
245 | | - requires: { |
246 | | - topology: ['single', 'replicaset', 'sharded'], |
247 | | - mongodb: '>= 2.8.0' |
248 | | - } |
249 | | - }, |
250 | | - |
251 | | - test: function (done) { |
252 | | - const configuration = this.configuration; |
253 | | - |
254 | | - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
255 | | - client.connect(function (err, client) { |
256 | | - expect(err).to.not.exist; |
257 | | - |
258 | | - // Get a db we that does not have any collections |
259 | | - const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThemFor28'); |
260 | | - |
261 | | - // Create a collection |
262 | | - db1.collection('test.collection1').insertOne({ a: 1 }, function (err) { |
263 | | - expect(err).to.not.exist; |
264 | | - |
265 | | - // Create a collection |
266 | | - db1.collection('test.collection2').insertOne({ a: 1 }, function () { |
267 | | - expect(err).to.not.exist; |
268 | | - |
269 | | - // Get listCollections filtering out the name |
270 | | - const cursor = db1.listCollections({ name: /test.collection/ }, { batchSize: 1 }); |
271 | | - cursor.toArray(function (err, names) { |
272 | | - expect(err).to.not.exist; |
273 | | - test.equal(2, names.length); |
274 | | - |
275 | | - client.close(done); |
276 | | - }); |
277 | | - }); |
278 | | - }); |
279 | | - }); |
280 | | - } |
| 166 | + // Get listCollections filtering out the name |
| 167 | + const collections = await db1 |
| 168 | + .listCollections({ name: /test.collection/ }, { batchSize: 1 }) |
| 169 | + .toArray(); |
| 170 | + expect(collections.length).to.equal(2); |
| 171 | + await client.close(); |
281 | 172 | }); |
282 | 173 |
|
283 | 174 | it('should throw if Db.collection is passed a deprecated callback argument', () => { |
284 | 175 | const client = new MongoClient('mongodb://iLoveJavascript'); |
| 176 | + // @ts-expect-error Not allowed in TS, but can be used in JS |
| 177 | + // eslint-disable-next-line @typescript-eslint/no-empty-function |
285 | 178 | expect(() => client.db('test').collection('test', () => {})).to.throw( |
286 | 179 | 'The callback form of this helper has been removed.' |
287 | 180 | ); |
|
0 commit comments