forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenumerate_databases.prose.test.ts
More file actions
71 lines (57 loc) · 2.51 KB
/
enumerate_databases.prose.test.ts
File metadata and controls
71 lines (57 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { expect } from 'chai';
import { type MongoClient } from '../../src/mongo_client';
const REQUIRED_DBS = ['admin', 'local', 'config'];
const DB_NAME = 'listDatabasesTest';
describe('listDatabases() spec prose', function () {
/**
* Execute the method to enumerate full database information (e.g. listDatabases())
* - Verify that the method returns an Iterable of Document types
* - Verify that all databases on the server are present in the result set
* - Verify that the result set does not contain duplicates
*/
let client: MongoClient;
let ENTIRE_DB_LIST: string[];
beforeEach(async function () {
client = this.configuration.newClient();
const dbInfoFromCommand = await client.db('admin').command({ listDatabases: 1 });
const databasesToDrop = dbInfoFromCommand.databases
.map(({ name }) => name)
.filter(name => !REQUIRED_DBS.includes(name));
for (const dbToDrop of databasesToDrop) {
await client.db(dbToDrop).dropDatabase();
}
await client.db(DB_NAME).createCollection(DB_NAME);
ENTIRE_DB_LIST = (await client.db('admin').command({ listDatabases: 1 })).databases.map(
({ name }) => name
);
ENTIRE_DB_LIST.sort();
expect(ENTIRE_DB_LIST).to.have.lengthOf.at.least(1);
});
afterEach(async function () {
await client.db(DB_NAME).dropDatabase();
await client?.close();
});
it('Verify that the method returns an Iterable of Document types', async () => {
const dbInfo = await client.db().admin().listDatabases();
expect(dbInfo).to.have.property('databases');
expect(dbInfo.databases).to.be.an('array');
expect(dbInfo.databases).to.have.lengthOf(ENTIRE_DB_LIST.length);
for (const db of dbInfo.databases) {
expect(db).to.be.a('object');
}
});
it('Verify that all databases on the server are present in the result set', async () => {
const dbInfo = await client.db().admin().listDatabases();
const namesFromHelper = dbInfo.databases.map(({ name }) => name);
namesFromHelper.sort();
expect(namesFromHelper).to.have.lengthOf(ENTIRE_DB_LIST.length);
expect(namesFromHelper).to.deep.equal(ENTIRE_DB_LIST);
expect(namesFromHelper).to.include(DB_NAME);
});
it('Verify that the result set does not contain duplicates', async () => {
const dbInfo = await client.db().admin().listDatabases();
const databaseNames = dbInfo.databases.map(({ name }) => name);
const databaseNamesSet = new Set(databaseNames);
expect(databaseNames).to.have.lengthOf(databaseNamesSet.size);
});
});