forked from mikhail-angelov/mongo-unit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
150 lines (131 loc) · 4.4 KB
/
test.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict'
const expect = require('chai').expect
const MongoClient = require('mongodb').MongoClient
const co = require('co')
const DB_NAME = 'test'
describe('mongo-unit', function() {
this.timeout(10000000)
const mongoUnit = require('./index')
const testData = {
col1: [{ doc: 1 }, { doc: 2 }],
col2: [{ rec: 1 }, { rec: 2 }],
}
before(() => mongoUnit.start({ dbName: DB_NAME }))
after(() => mongoUnit.stop())
afterEach(() => mongoUnit.drop())
it('should safely start mongo several time', () => {
return mongoUnit.start().then(url => {
expect(url).to.equal(mongoUnit.getUrl())
})
})
it('should connect to db and CRUD docs', () =>
co(function*() {
const client = yield MongoClient.connect(mongoUnit.getUrl(), {
useUnifiedTopology: true,
})
const db = client.db(DB_NAME)
const collection = db.collection('test')
yield collection.insertOne({ doc: 1 })
let results = yield collection.find().toArray()
expect(results.length).to.equal(1)
expect(results[0].doc).to.equal(1)
yield collection.remove({ doc: 1 })
results = yield collection.find().toArray()
expect(results.length).to.equal(0)
yield client.close()
}))
it('should load collection data', () =>
co(function*() {
yield mongoUnit.load(testData)
const client = yield MongoClient.connect(mongoUnit.getUrl(), {
useUnifiedTopology: true,
})
const db = client.db(DB_NAME)
const collection1 = db.collection('col1')
const collection2 = db.collection('col2')
let results = yield collection1.find().toArray()
expect(results.length).to.equal(2)
expect(results[0].doc).to.equal(1)
results = yield collection2.find().toArray()
expect(results.length).to.equal(2)
expect(results[1].rec).to.equal(2)
yield client.close()
}))
it('should clean collection data', () =>
co(function*() {
yield mongoUnit.load(testData)
yield mongoUnit.clean(testData)
const client = yield MongoClient.connect(mongoUnit.getUrl(), {
useUnifiedTopology: true,
})
const db = client.db(DB_NAME)
const collection1 = db.collection('col1')
const collection2 = db.collection('col2')
let results = yield collection1.find().toArray()
expect(results.length).to.equal(0)
results = yield collection2.find().toArray()
expect(results.length).to.equal(0)
yield client.close()
}))
it('should init DB data for given URL', () =>
co(function*() {
const url = mongoUnit.getUrl()
yield mongoUnit.initDb(testData)
const client = yield MongoClient.connect(mongoUnit.getUrl(), {
useUnifiedTopology: true,
})
const db = client.db(DB_NAME)
const collection1 = db.collection('col1')
const collection2 = db.collection('col2')
let results = yield collection1.find().toArray()
expect(results.length).to.equal(2)
results = yield collection2.find().toArray()
expect(results.length).to.equal(2)
yield client.close()
}))
it('should dropDb DB data for given URL', () =>
co(function*() {
yield mongoUnit.initDb(testData)
yield mongoUnit.dropDb()
const client = yield MongoClient.connect(mongoUnit.getUrl(), {
useUnifiedTopology: true,
})
const db = client.db(DB_NAME)
const collections = yield db.listCollections().toArray()
expect(collections.length).to.equal(0)
yield client.close()
}))
// it('should list mongo',(done)=>{
// var ps = require('ps-node');
// // A simple pid lookup
// ps.lookup({
// psargs:['-A'],
// command: 'mongod',
// arguments: '.mongo-unit'
// }, function(err, resultList ) {
// console.log('ps', err, resultList)
// if (err) {
// throw new Error( err );
// }
// resultList.forEach(function( process ){
// if( process ){
// console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
// }
// });
// done()
// });
// })
it('should stop mongo and start again', () => {
return mongoUnit
.stop()
.then(() => {
expect(mongoUnit.getUrl).to.throw(Error)
return mongoUnit.start()
})
.then(url => {
expect(true).to.equal(!!mongoUnit.getUrl(), {
useUnifiedTopology: true,
})
})
})
})