-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.coffee
More file actions
247 lines (216 loc) · 10.2 KB
/
test.coffee
File metadata and controls
247 lines (216 loc) · 10.2 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Mocha test using livedb's snapshot tests
rethinkdbdash = require 'rethinkdbdash'
liveDBRethinkDB = require './rethinkdb'
assert = require 'assert'
# Clear mongo
clear = (callback) ->
r = rethinkdbdash({ host: 'localhost', port: '28015', db: 'sharejs' })
r
# Original tests converted form dropping table to deleting all records
# This is due to a difference in functionality between RethinkDB and Mongo
.table 'testcollection'
.delete()
.run()
.catch(() ->)
.then () ->
r
.table 'testcollection_ops'
.delete()
.run()
.catch(() ->)
.then () ->
callback()
create = (callback) ->
clear () ->
callback liveDBRethinkDB { host: 'localhost', port: '28015', db: 'sharejs' }
describe 'rethinkdb', ->
# afterEach () ->
# console.log('After Each');
# r = rethinkdbdash({ host: 'localhost', port: '28015', db: 'sharejs' })
# r
# .tableList()
# .run()
# .then (tableList) ->
# console.log('tableList');
# console.log(tableList);
describe 'raw', ->
beforeEach (done) ->
@r = rethinkdbdash { host: 'localhost', port: '28015', db: 'sharejs' }
create (@db) => done()
it 'adds an index for ops', (done) -> create (db) =>
db.writeOp 'testcollection', 'foo', {v:0, create:{type:'json0'}}, (err) =>
# The problem here is that the index might not have been created yet if
# the database is busy, which makes this test flakey. I'll put a
# setTimeout for now, but if there's more problems, it might have to be
# rewritten.
@r.table('testcollection_ops').indexList().run().then (indexes) ->
assert.deepEqual(indexes, ['name', 'v']);
# We should find an index with [[ 'name', 1 ], [ 'v', 1 ]]
# for name, idx of indexes
# # if JSON.stringify(idx) is '[["name",1],["v",1]]'
done()
# throw Error "Could not find index in ops db - #{JSON.stringify(indexes)}"
it 'does not allow editing the system collection', (done) ->
@db.writeSnapshot 'system', 'test', {type:'json0', v:5, m:{}, data:{x:5}}, (err) =>
assert.ok err
@db.getSnapshot 'system', 'test', (err, data) ->
assert.ok err
assert.equal data, null
done()
it 'defaults to the version of the document if there are no ops', (done) ->
@db.writeSnapshot 'testcollection', 'versiontest', {type: 'json0', v: 3, data:{x:5}}, (err) =>
@db.getVersion 'testcollection', 'versiontest', (err, v) =>
throw Error err if err
assert.equal v, 3
done()
describe 'query', ->
it 'returns data in the collection', (done) ->
snapshot = {type:'json0', v:5, m:{}, data:{x:5, y:6}}
@db.writeSnapshot 'testcollection', 'test', snapshot, (err) =>
@db.query 'unused', 'testcollection', {x:5}, {}, (err, results) ->
throw Error err if err
delete results[0].docName
assert.deepEqual results, [snapshot]
done()
it 'returns nothing when there is no data', (done) ->
@db.query 'unused', 'testcollection', {x:5}, {}, (err, results) ->
throw Error err if err
assert.deepEqual results, []
done()
it 'does not allow $where queries', (done) ->
@db.query 'unused', 'testcollection', {$where:"true"}, {}, (err, results) ->
assert.ok err
assert.equal results, null
done()
it '$distinct should perform distinct operation', (done) ->
snapshots = [
{type:'json0', v:5, m:{}, data:{x:1, y:1}},
{type:'json0', v:5, m:{}, data:{x:2, y:2}},
{type:'json0', v:5, m:{}, data:{x:3, y:2}}
]
@db.writeSnapshot 'testcollection', 'test1', snapshots[0], (err) =>
@db.writeSnapshot 'testcollection', 'test2', snapshots[1], (err) =>
@db.writeSnapshot 'testcollection', 'test3', snapshots[2], (err) =>
@db.query 'unused', 'testcollection', {$distinct: true, $field: 'y', $query: {}}, {}, (err, results) ->
throw Error err if err
assert.deepEqual results.extra, [1,2]
done()
it '$aggregate should perform aggregate command', (done) ->
snapshots = [
{type:'json0', v:5, m:{}, data:{x:1, y:1}},
{type:'json0', v:5, m:{}, data:{x:2, y:2}},
{type:'json0', v:5, m:{}, data:{x:3, y:2}}
]
@db.allowAggregateQueries = true
@db.writeSnapshot 'testcollection', 'test1', snapshots[0], (err) =>
@db.writeSnapshot 'testcollection', 'test2', snapshots[1], (err) =>
@db.writeSnapshot 'testcollection', 'test3', snapshots[2], (err) =>
@db.query 'unused', 'testcollection', {
$aggregate: [{$group: {_id: '$y', count: {$sum: 1}}}, {$sort: {count: 1}}]
}, {}, (err, results) ->
throw Error err if err
assert.deepEqual results.extra, [{_id: 1, count: 1}, {_id: 2, count: 2}]
done()
it 'does not let you run $aggregate queries without options.allowAggregateQueries', (done) ->
@db.query 'unused', 'testcollection', {$aggregate: [{$group: {_id: '$y', count: {$sum: 1}}}, {$sort: {count: 1}}]}, {}, (err, results) ->
assert.ok err
done()
it 'does not allow $mapReduce queries by default', (done) ->
snapshots = [
{type:'json0', v:5, m:{}, data:{player: 'a', round: 1, score: 5}},
{type:'json0', v:5, m:{}, data:{player: 'a', round: 2, score: 7}},
{type:'json0', v:5, m:{}, data:{player: 'b', round: 1, score: 15}}
]
@db.writeSnapshot 'testcollection', 'test1', snapshots[0], (err) =>
@db.writeSnapshot 'testcollection', 'test2', snapshots[1], (err) =>
@db.writeSnapshot 'testcollection', 'test3', snapshots[2], (err) =>
@db.query 'unused', 'testcollection',
$mapReduce: true,
$map: ->
emit @.player, @score
$reduce: (key, values) ->
values.reduce (t, s) -> t + s
$query: {}
, {}, (err, results) ->
assert.ok err
assert.equal results, null
done()
it '$mapReduce queries should work when allowJavaScriptQuery == true', (done) ->
snapshots = [
{type:'json0', v:5, m:{}, data:{player: 'a', round: 1, score: 5}},
{type:'json0', v:5, m:{}, data:{player: 'a', round: 2, score: 7}},
{type:'json0', v:5, m:{}, data:{player: 'b', round: 1, score: 15}}
]
@db.allowJSQueries = true
@db.writeSnapshot 'testcollection', 'test1', snapshots[0], (err) =>
@db.writeSnapshot 'testcollection', 'test2', snapshots[1], (err) =>
@db.writeSnapshot 'testcollection', 'test3', snapshots[2], (err) =>
@db.query 'unused', 'testcollection',
$mapReduce: true,
$map: ->
emit @.player, @score
$reduce: (key, values) ->
values.reduce (t, s) -> t + s
$query: {}
, {}, (err, results) ->
throw Error err if err
assert.deepEqual results.extra, [{_id: 'a', value: 12}, {_id: 'b', value: 15}]
done()
describe 'queryProjected', ->
it 'returns only projected fields', (done) ->
@db.writeSnapshot 'testcollection', 'test', {type:'json0', v:5, m:{}, data:{x:5, y:6}}, (err) =>
@db.queryProjected 'unused', 'testcollection', {y:true}, {x:5}, {}, (err, results) ->
throw Error err if err
assert.deepEqual results, [{type:'json0', v:5, m:{}, data:{y:6}, docName:'test'}]
done()
it 'returns no data for matching documents if fields is empty', (done) ->
@db.writeSnapshot 'testcollection', 'test', {type:'json0', v:5, m:{}, data:{x:5, y:6}}, (err) =>
@db.queryProjected 'unused', 'testcollection', {}, {x:5}, {}, (err, results) ->
throw Error err if err
assert.deepEqual results, [{type:'json0', v:5, m:{}, data:{}, docName:'test'}]
done()
describe 'queryDoc', ->
it 'returns null when the document does not exist', (done) ->
@db.queryDoc 'unused', 'unused', 'testcollection', 'doesnotexist', {}, (err, result) ->
throw Error err if err
assert.equal result, null
done()
it 'returns the doc when the document does exist', (done) ->
snapshot = {type:'json0', v:5, m:{}, data:{x:5, y:6}}
@db.writeSnapshot 'testcollection', 'test', snapshot, (err) =>
@db.queryDoc 'unused', 'unused', 'testcollection', 'test', {}, (err, result) ->
throw Error err if err
snapshot.docName = 'test'
assert.deepEqual result, snapshot
done()
it 'does not allow $where queries', (done) ->
@db.queryDoc 'unused', 'unused', 'testcollection', 'somedoc', {$where:"true"}, (err, result) ->
assert.ok err
assert.equal result, null
done()
describe 'queryDocProjected', ->
beforeEach (done) ->
@snapshot = {type:'json0', v:5, m:{}, data:{x:5, y:6}}
@db.writeSnapshot 'testcollection', 'test', @snapshot, (err) =>
@snapshot.docName = 'test'
throw Error err if err
done()
it 'returns null when the document does not exist', (done) ->
@db.queryDocProjected 'unused', 'unused', 'testcollection', 'doesnotexist', {x:true}, {}, (err, result) ->
throw Error err if err
assert.equal result, null
done()
it 'returns the requested fields of the doc', (done) ->
@db.queryDocProjected 'unused', 'unused', 'testcollection', 'test', {x:true}, {}, (err, result) =>
throw Error err if err
@snapshot.data = {x:5}
assert.deepEqual result, @snapshot
done()
it 'returns empty data if no fields are requested', (done) ->
@db.queryDocProjected 'unused', 'unused', 'testcollection', 'test', {}, {}, (err, result) =>
throw Error err if err
@snapshot.data = {}
assert.deepEqual result, @snapshot
done()
# require('livedb/test/snapshotdb') create
# require('livedb/test/oplog') create