-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud-mongo-jest.spec.js
59 lines (49 loc) · 1.7 KB
/
crud-mongo-jest.spec.js
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
test('read', async() => {
// set & setting
// input
const data = {"title":"Knock knock neo"};
const lastId = await crudmongo.create(dbs,"rnotes",data);
// output
const dbObj = await crudmongo.read(dbs,"rnotes",lastId);
expect(dbObj[0].title).toEqual("Knock knock neo");
});
test('update', async() => {
// set & setting
const data = {"title":"Knock knock neo"};
const lastId = await crudmongo.create(dbs,"unotes",data);
// input
await crudmongo.update(dbs,"unotes",{"_id":lastId},{"title":"Follow the white rabbit"});
// output
const obj = await crudmongo.read(dbs,"unotes",lastId);
expect(obj[0].title).toEqual("Follow the white rabbit");
});
test('delete', async() => {
// set & setting
const data = {"title":"Morpheus?"};
const lastId = await crudmongo.create(dbs,"dnotes",data);
// input
await crudmongo.delete(dbs,"dnotes",{"_id":lastId});
// output
const dbObj = await crudmongo.read(dbs,"dnotes",lastId);
expect(dbObj).toStrictEqual([]);
});
//
let dbs = '';
let client = '';
beforeAll(async() => {
const dbname = 'test-mq3';
await exec_cmd("mongo " + dbname + " --quiet --eval \"db.dropDatabase()\"")
try {
let obj = await crudmongo.connectWithClient(dbname);
dbs = obj.dbs;
client = obj.client;
} catch(err) {
console.error(err);
process.exit(1);
}
});
afterAll(() => {
return client.close();
});
const crudmongo = require('./crud-mongo');
const { exec } = require('child_process'); function exec_cmd(command) { return new Promise(function(resolve, reject) { exec(command, (err, stdout, stderr) => { if (err) { return reject(err); } else { return resolve(stdout); } }); });}