Skip to content

Commit f9551c6

Browse files
committed
test(NODE-7205): refactor tests
1 parent c70cf11 commit f9551c6

1 file changed

Lines changed: 98 additions & 205 deletions

File tree

test/integration/node-specific/db.test.ts

Lines changed: 98 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { expect } from 'chai';
22

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';
511

612
describe('Db', function () {
713
before(function () {
@@ -52,236 +58,123 @@ describe('Db', function () {
5258
expect(error).to.be.instanceOf(Error);
5359
});
5460

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();
7467
});
7568

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();
95101
});
96102

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 });
124107

125-
it('shouldCorrectlyUseCursorWithListCollectionsCommand', {
126-
metadata: {
127-
requires: { topology: ['single', 'replicaset', 'sharded'] }
128-
},
108+
const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommand');
129109

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 });
132113

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+
});
136119

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;
139122

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');
143125

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 });
147127

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 });
153129

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();
160134
});
161135

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;
166138

167-
test: function (done) {
168-
const configuration = this.configuration;
139+
const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
140+
const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThem');
169141

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 });
173144

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);
176147

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+
});
180155

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;
184158

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');
190161

191-
client.close(done);
192-
});
193-
});
194-
});
195-
});
196-
}
197-
});
162+
await db1.collection('test.collection1').insertOne({ a: 1 });
198163

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 });
242165

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();
281172
});
282173

283174
it('should throw if Db.collection is passed a deprecated callback argument', () => {
284175
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
285178
expect(() => client.db('test').collection('test', () => {})).to.throw(
286179
'The callback form of this helper has been removed.'
287180
);

0 commit comments

Comments
 (0)