Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit 02615bc

Browse files
authored
DOP-3634: add test util to make new db for each test suite (#810)
* add test util to make new db for each test suite * convert metadata test too
1 parent 8cd8171 commit 02615bc

File tree

4 files changed

+74
-54
lines changed

4 files changed

+74
-54
lines changed

modules/persistence/tests/metadata/ToC.test.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { MongoClient } from 'mongodb';
2-
import { AssociatedProduct, Metadata } from '../../src/services/metadata/associated_products';
1+
import { Db, MongoClient } from 'mongodb';
2+
import { AssociatedProduct } from '../../src/services/metadata/associated_products';
3+
import { Metadata } from '../../src/services/metadata/';
34
import {
45
ToC,
56
ToCInsertions,
@@ -10,10 +11,10 @@ import {
1011
} from '../../src/services/metadata/ToC';
1112

1213
import metadata from '../data/metadata.json';
13-
import repoBranches from '../data/repos_branches.json';
14+
import { setMockDB, closeDb } from '../utils';
1415

15-
let connection;
16-
let mockDb;
16+
let connection: MongoClient;
17+
let mockDb: Db;
1718
jest.mock('../../src/services/connector', () => {
1819
return {
1920
pool: jest.fn(() => {
@@ -27,29 +28,17 @@ jest.mock('../../src/services/connector', () => {
2728

2829
describe('ToC module', () => {
2930
beforeAll(async () => {
30-
// process.env.MONGO_URL defaults to localhost
31-
// https://github.com/shelfio/jest-mongodb#3-configure-mongodb-client
32-
// or update jest-mongodb-config.js
33-
try {
34-
connection = await MongoClient.connect(process.env.MONGO_URL || 'test');
35-
mockDb = await connection.db();
36-
await mockDb.collection('repos_branches').insertMany(repoBranches);
37-
await mockDb.collection('metadata').insertMany(metadata);
38-
} catch (e) {
39-
console.error(e);
40-
}
31+
[mockDb, connection] = await setMockDB();
4132
});
4233

4334
afterAll(async () => {
44-
await mockDb.collection('repos_branches').deleteMany({});
45-
await mockDb.collection('metadata').deleteMany({});
46-
await connection.close();
35+
await closeDb(mockDb, connection);
4736
});
4837

4938
describe('copyToCTree', () => {
5039
it('creates a deep copy of a ToC when passed only the ToC', () => {
5140
const toctree = metadata[0].toctree;
52-
const copiedToCTree = copyToCTree(toctree);
41+
const copiedToCTree = copyToCTree(toctree as unknown as ToC);
5342
toctree.slug = '/';
5443
copiedToCTree.slug = 'test value';
5544
expect(toctree.slug).toBe('/');

modules/persistence/tests/metadata/associated_products.test.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MongoClient } from 'mongodb';
1+
import { Db, MongoClient } from 'mongodb';
22
import {
33
_getRepoBranchesEntry,
44
_getAllAssociatedRepoBranchesEntries,
@@ -9,10 +9,10 @@ import {
99
} from '../../src/services/metadata/associated_products';
1010

1111
import metadata from '../data/metadata.json';
12-
import repoBranches from '../data/repos_branches.json';
12+
import { setMockDB, closeDb } from '../utils';
1313

14-
let connection;
15-
let mockDb;
14+
let connection: MongoClient;
15+
let mockDb: Db;
1616
jest.mock('../../src/services/connector', () => {
1717
return {
1818
pool: jest.fn(() => {
@@ -29,19 +29,11 @@ describe('associated_products module', () => {
2929
const project = 'atlas-cli';
3030

3131
beforeAll(async () => {
32-
// process.env.MONGO_URL defaults to localhost
33-
// https://github.com/shelfio/jest-mongodb#3-configure-mongodb-client
34-
// or update jest-mongodb-config.js
35-
connection = await MongoClient.connect(process.env.MONGO_URL || 'test');
36-
mockDb = await connection.db();
37-
await mockDb.collection('repos_branches').insertMany(repoBranches);
38-
await mockDb.collection('metadata').insertMany(metadata);
32+
[mockDb, connection] = await setMockDB();
3933
});
4034

4135
afterAll(async () => {
42-
await mockDb.collection('repos_branches').deleteMany({});
43-
await mockDb.collection('metadata').deleteMany({});
44-
await connection.close();
36+
await closeDb(mockDb, connection);
4537
});
4638

4739
describe('getRepoBranchesEntry', () => {

modules/persistence/tests/metadata/metadata.test.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import AdmZip from 'adm-zip';
22
import { serialize } from 'bson';
33
import { Db, Document, MongoClient, ObjectId, WithId } from 'mongodb';
44
import { _metadataFromZip, insertMetadata, deleteStaleMetadata } from '../../src/services/metadata';
5-
import metadata from '../data/metadata.json';
6-
import repoBranches from '../data/repos_branches.json';
5+
import { setMockDB, closeDb } from '../utils';
76

87
let connection: MongoClient;
98
let mockDb: Db;
@@ -28,14 +27,6 @@ jest.mock('../../src/services/connector', () => {
2827
};
2928
});
3029

31-
const convertToBuildId = (docs: any[]) => {
32-
// convert _id field into a ObjectId
33-
return docs.map((d) => {
34-
d._id = new ObjectId(d._id);
35-
return d;
36-
});
37-
};
38-
3930
describe('metadata module', () => {
4031
const branch = 'master';
4132
const project = 'atlas-cli';
@@ -48,19 +39,11 @@ describe('metadata module', () => {
4839
zip.addFile('site.bson', Buffer.from(serialize(meta)));
4940

5041
beforeAll(async () => {
51-
// process.env.MONGO_URL defaults to localhost
52-
// https://github.com/shelfio/jest-mongodb#3-configure-mongodb-client
53-
// or update jest-mongodb-config.js
54-
connection = await MongoClient.connect(process.env.MONGO_URL || 'test');
55-
mockDb = connection.db();
56-
await mockDb.collection('repos_branches').insertMany(convertToBuildId(repoBranches));
57-
await mockDb.collection('metadata').insertMany(convertToBuildId(metadata));
42+
[mockDb, connection] = await setMockDB();
5843
});
5944

6045
afterAll(async () => {
61-
await mockDb.collection('repos_branches').deleteMany({});
62-
await mockDb.collection('metadata').deleteMany({});
63-
await connection.close();
46+
await closeDb(mockDb, connection);
6447
});
6548

6649
describe('metadataFromZip', () => {

modules/persistence/tests/utils.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Test utilities
3+
*
4+
* Export functions to set/mock environments (ie. jest mocks)
5+
*/
6+
7+
import { Db, MongoClient, ObjectId } from 'mongodb';
8+
import metadata from './data/metadata.json';
9+
import repoBranches from './data/repos_branches.json';
10+
11+
/**
12+
* mocks a db with test data in ./data collection
13+
* designed to set up test modules with fresh db
14+
*
15+
* @param dbName designated db name for target test module
16+
* @returns [Db, MongoClient]
17+
*/
18+
export const setMockDB = async (dbName: string = new ObjectId().toString()): Promise<[Db, MongoClient]> => {
19+
try {
20+
// process.env.MONGO_URL defaults to mongodb://127.0.0.1:58144/
21+
// https://github.com/shelfio/jest-mongodb#3-configure-mongodb-client
22+
// or update jest-mongodb-config.js
23+
const connection = await MongoClient.connect(process.env.MONGO_URL || 'test');
24+
const mockDb = connection.db(dbName);
25+
await mockDb.collection('repos_branches').insertMany(repoBranches as unknown[] as Document[]);
26+
await mockDb.collection('metadata').insertMany(metadata as unknown[] as Document[]);
27+
return [mockDb, connection];
28+
} catch (e) {
29+
console.error(e);
30+
throw e;
31+
}
32+
};
33+
34+
/**
35+
* Deletes all test data in all collections in documents and closes db connection
36+
*
37+
* @param db
38+
* @param connection
39+
* @returns
40+
*/
41+
export const closeDb = async (db: Db, connection: MongoClient) => {
42+
try {
43+
const collections = await db.listCollections().toArray();
44+
45+
await Promise.all(
46+
collections.map(async (collection) => {
47+
return db.collection(collection.name).deleteMany({});
48+
})
49+
);
50+
51+
return connection.close();
52+
} catch (e) {
53+
console.error(e);
54+
throw e;
55+
}
56+
};

0 commit comments

Comments
 (0)