-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.js
154 lines (127 loc) · 3.54 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
151
152
153
154
'use strict'
const assert = require('assert')
const co = require('co')
const datastore = require('./index')
const path = require('path')
const _ = require('lodash')
describe('datastore', () => {
const DB = datastore({ autoload: true })
//
// before each: clear and then stage data
//
beforeEach(co.wrap(function * () {
yield DB.remove({ }, { multi: true })
yield DB.insert([ {
num: 1,
alpha: 'a'
}, {
num: 2,
alpha: 'b'
}, {
num: 3,
alpha: 'c'
} ])
}))
describe('#find()', () => {
it('should return them all', co.wrap(function * () {
assert.equal((yield DB.find({})).length, 3)
}))
it('should only return 1', co.wrap(function * () {
assert.equal((yield DB.find({ num: 3 })).length, 1)
}))
it ('should project', co.wrap(function * () {
let doc = yield DB.cfind({ num: 3 })
.projection({ num: 1, _id: 0 })
.exec()
doc = doc[0]
assert.equal(doc.num, 3)
assert.equal(doc.alpha, undefined)
}))
it('should sort', co.wrap(function * () {
let docs = yield DB.cfind({})
.sort({ num: -1 })
.exec()
docs = _.chain(docs)
.map(d => d.num)
.value()
let assertion = docs[0] == 3 && docs[1] == 2 && docs[2] == 1
assert(assertion)
}))
})
describe('#findOne()', () => {
it('should only return one', co.wrap(function * () {
let doc = yield DB.findOne({ num: 2 })
assert.equal(doc.num, 2)
}))
it('should project', co.wrap(function * () {
let doc = yield DB.cfindOne({ num: 2 }, true)
.projection({ num: 1, _id: false })
.exec()
assert.equal(doc.num, 2)
assert.equal(doc.alpha, undefined)
}))
})
describe('#count()', () => {
it('should return the number of documents in the database', co.wrap(function * () {
let count = yield DB.count({})
assert.equal(count, 3)
}))
it('should work with cursors', co.wrap(function * () {
const count = yield DB.ccount({}).limit(2).exec()
assert.equal(count, 2)
}))
})
describe('#insert()', () => {
it('should insert two documents', co.wrap(function * () {
let beforeCount = yield DB.count({})
let docs = yield DB.insert([{
num: 4,
alpha: 'd'
}, {
num: 5,
alpha: 'e'
}])
let afterCount = yield DB.count({})
assert.equal(afterCount - beforeCount, 2)
}))
})
describe('#update()', () => {
it('should update a document', co.wrap(function * () {
yield DB.update({ num: 3 }, { $set: { updated: true } })
let updated = yield DB.findOne({ num: 3 })
assert(updated.updated)
}))
it('should insert a new document', co.wrap(function * () {
let beforeCount = yield DB.count({})
yield DB.update({ num: 4 }, {
num: 4,
alpha: 'f'
}, { upsert: true })
let afterCount = yield DB.count({})
assert.equal(afterCount - beforeCount, 1)
}))
})
describe('#remove()', () => {
it('should remove a document', co.wrap(function * () {
let beforeCount = yield DB.count({})
yield DB.remove({})
let afterCount = yield DB.count({})
assert.equal(beforeCount - afterCount, 1)
}))
it('should remove all documents', co.wrap(function * () {
yield DB.remove({}, { multi: true })
let afterCount = yield DB.count({})
assert.equal(afterCount, 0)
}))
})
describe('Cursors', () => {
it('should work with no suffix', co.wrap(function * () {
const db = datastore('')
yield db.insert([ { id: 2, name: 'Tim' }, { id: 1, name: 'Tom' } ])
const res = yield db.cfind().sort({ name: -1 }).projection({ name: 1 }).exec()
assert.equal(res[0].name, 'Tom')
assert.equal(res[1].name, 'Tim')
assert.equal(typeof res[0].id, 'undefined')
}))
})
})