-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRedisDocumentIndexingSpec.coffee
378 lines (263 loc) · 10.3 KB
/
RedisDocumentIndexingSpec.coffee
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Test dependencies
cwd = process.cwd()
path = require 'path'
Faker = require 'faker'
chai = require 'chai'
sinon = require 'sinon'
sinonChai = require 'sinon-chai'
mockMulti = require './lib/multi'
expect = chai.expect
# Configure Chai and Sinon
chai.use sinonChai
chai.should()
# Code under test
Modinha = require 'modinha'
RedisDocument = require path.join(cwd, 'lib/RedisDocument')
# Redis lib for spying and stubbing
Redis = require 'ioredis'
client = new Redis({ port: 12345 })
rclient = Redis.prototype
multi = mockMulti(rclient)
describe 'Indexing', ->
{Document,data,documents,jsonDocuments} = {}
{err,instance,instances,update,deleted,original,ids} = {}
before ->
schema =
unique: { type: 'string' }
deleted: { type: 'string', unique: true }
reference: { type: 'string' }
secondary: { type: 'string' }
nested: { type: 'object' }
flag: { type: 'string' }
Document = Modinha.define 'documents', schema
Document.extend RedisDocument
# lookup object by some 1:1 attribute
Document.defineIndex
type: 'hash'
key: 'documents:unique'
field: 'unique'
value: '_id'
# has many/belongs to/has many through
Document.defineIndex
type: 'sorted'
key: ['references:$:documents', 'reference']
score: 'created'
member: '_id'
# find objects by 1:* attributes
Document.defineIndex
type: 'sorted'
key: ['documents:#:$', 'secondary', 'secondary']
score: 'modified'
member: '_id'
# compound field name
Document.defineIndex
type: 'hash'
key: 'a:b:c'
field: ['$:$', 'reference', 'secondary']
value: '_id'
# nested property
Document.defineIndex
type: 'hash'
key: ['documents:$', 'nested.key']
field: ['$', 'nested.prop']
value: '_id'
# dynamic nested property
Document.defineIndex
type: 'hash'
key: 'dynamic:field'
field: ['$', ['nested.$', 'flag']]
value: '_id'
Document.__redis = Redis
Document.__client = client
# Mock data
data = []
for i in [0..9]
data.push
unique: Faker.random.number(1000).toString()
reference: Faker.random.number(1000).toString()
secondary: Faker.random.number(1000).toString()
nested:
key: 'value'
prop: Faker.random.number(1000).toString()
foo: 'bar'
flag: 'foo'
documents = Document.initialize(data, { private: true })
jsonDocuments = documents.map (d) ->
Document.serialize(d)
ids = documents.map (d) ->
d._id
describe 'key interpolation', ->
it 'should replace # with literal parameters', ->
Document.indexKey(['a:#:b:#:c:#:d:#', '1', '2', '3', '4'])
.should.equal 'a:1:b:2:c:3:d:4'
it 'should replace $ with object values', ->
data = { a: 1, b: 2, c: '3', d: '4' }
Document.indexKey(['a:$:b:$:c:$:d:$', 'a', 'b', 'c', 'd'], data)
.should.equal 'a:1:b:2:c:3:d:4'
it 'should replace # and $ with the correct params', ->
data = { alpha: 1, bravo: 2, charlie: '3', delta: '4' }
Document.indexKey([
'a:#:b:$:c:#:d:$'
'alpha'
'bravo'
'charlie'
'delta'
], data).should.equal 'a:alpha:b:2:c:charlie:d:4'
it 'should evaluate array params', ->
data = { a: 1, b: { c: 'd', d: { e: 5 } } }
Document.indexKey([
'answer:$'
['b.$.e', 'b.c']
], data).should.equal 'answer:5'
describe 'index', ->
before ->
m = client.multi()
instance = documents[0]
sinon.stub multi, 'hset'
sinon.stub multi, 'zadd'
sinon.stub(multi, 'exec').callsArgWith(0, null, [])
Document.index m, instance
after ->
multi.hset.restore()
multi.zadd.restore()
multi.exec.restore()
it 'should add a field to a hash', ->
multi.hset.should.have.been.calledWith 'documents:unique', instance.unique, instance._id
it 'should add a dynamically named field to a hash', ->
multi.hset.should.have.been.calledWith 'a:b:c', "#{instance.reference}:#{instance.secondary}", instance._id
it 'should add a "nested property" field to a hash', ->
multi.hset.should.have.been.calledWith 'documents:value', instance.nested.prop, instance._id
it 'should add a "dynamic property" field to a hash', ->
multi.hset.should.have.been.calledWith 'dynamic:field', 'bar', instance._id
it 'should add a member to a sorted set', ->
multi.zadd.should.have.been.calledWith "documents:secondary:#{instance.secondary}", sinon.match.number, instance._id
describe 'deindex', ->
before ->
m = client.multi()
instance = documents[0]
sinon.stub multi, 'hdel'
sinon.stub multi, 'zrem'
sinon.stub(multi, 'exec').callsArgWith(0, null, [])
Document.deindex m, instance
after ->
multi.hdel.restore()
multi.zrem.restore()
multi.exec.restore()
it 'should remove a field from a hash', ->
multi.hdel.should.have.been.calledWith 'documents:unique', instance.unique
it 'should remove a dynamically named field from a hash', ->
multi.hdel.should.have.been.calledWith 'a:b:c', "#{instance.reference}:#{instance.secondary}"
it 'should remove a "nested property" field from a hash', ->
multi.hdel.should.have.been.calledWith 'documents:value', instance.nested.prop
it 'should remove a "dynamic property" field from a hash', ->
multi.hdel.should.have.been.calledWith 'dynamic:field', 'bar'
it 'should remove a member from a sorted set', ->
multi.zrem.should.have.been.calledWith "documents:secondary:#{instance.secondary}", instance._id
describe 'reindex', ->
beforeEach ->
sinon.stub multi, 'hset'
sinon.stub multi, 'zadd'
sinon.stub multi, 'hdel'
sinon.stub multi, 'zrem'
sinon.stub(multi, 'exec').callsArgWith(0, null, [])
afterEach ->
multi.hset.restore()
multi.zadd.restore()
multi.hdel.restore()
multi.zrem.restore()
multi.exec.restore()
describe 'with changed value indexed by hash', ->
beforeEach ->
m = client.multi()
Document.reindex m, { _id: 'id', unique: 'updated' }, { _id: 'id', unique: 'original', deleted: 'not deleted' }
it 'should index the object by new value', ->
multi.hset.should.have.been.calledWith 'documents:unique', 'updated', 'id'
it 'should deindex the object by old value', ->
multi.hdel.should.have.been.calledWith 'documents:unique', 'original'
it 'should deindex the object by a deleted property', ->
multi.hdel.should.have.been.calledWith 'documents:deleted', 'not deleted'
multi.hset.should.not.have.been.calledWith 'documents:deleted'
describe 'with change to hash index with dynamically named field', ->
beforeEach ->
m = client.multi()
Document.reindex m, { _id: 'id', reference: '345', secondary: '678' }, { _id: 'id', reference: '123', secondary: '456' }
it 'should index the modified version of the instance', ->
multi.hset.should.have.been.calledWith 'a:b:c', '345:678', 'id'
it 'should deindex the original version of the instance', ->
multi.hdel.should.have.been.calledWith 'a:b:c', '123:456'
describe 'with unchanged value indexed by hash', ->
beforeEach ->
m = client.multi()
Document.reindex m, { _id: 'id', unique: 'original' }, { _id: 'id', unique: 'original' }
it 'should not reindex', ->
multi.hset.should.not.have.been.called
multi.hdel.should.not.have.been.called
describe 'with changed value indexed by sorted set', ->
beforeEach ->
m = client.multi()
instance =
_id: 'id'
secondary: 'updated'
modified: '1235'
original =
_id: 'id'
secondary: 'original'
modified: '1234'
Document.reindex m, instance, original
it 'should index the object by new value', ->
multi.zadd.should.have.been
.calledWith 'documents:secondary:updated', instance.modified, instance._id
it 'should deindex the object by old value', ->
multi.zrem.should.have.been
.calledWith 'documents:secondary:original', instance._id
describe 'with unchanged value indexed by sorted set', ->
beforeEach ->
m = client.multi()
instance =
_id: 'id'
secondary: 'updated'
modified: '1234'
Document.reindex m, instance, instance
it 'should not reindex the value', ->
multi.zadd.should.not.have.been.called
multi.zrem.should.not.have.been.called
describe 'explicit index definition', ->
it 'should register an index', ->
config = {}
Document.defineIndex config
Document.__indices.should.contain config
describe 'unique index definition', ->
it 'should register a unique index', ->
Document.indexUnique('unique')
index = Document.__indices[..].pop()
index.type.should.equal 'hash'
index.key.should.equal 'documents:unique'
index.field.should.equal 'unique'
index.value.should.equal '_id'
describe 'secondary index definition', ->
it 'should register a secondary index', ->
Document.indexSecondary('secondary')
index = Document.__indices[..].pop()
index.type.should.equal 'sorted'
index.key[0].should.equal 'documents:#:$'
index.key[1].should.equal 'secondary'
index.key[2].should.equal 'secondary'
index.score.should.equal 'modified'
index.member.should.equal '_id'
describe 'reference index definition', ->
it 'should register a reference index', ->
Document.indexReference('reference', { collection: 'references' })
index = Document.__indices[..].pop()
index.type.should.equal 'sorted'
index.key[0].should.equal 'references:$:documents'
index.key[1].should.equal 'reference'
index.score.should.equal 'created'
index.member.should.equal '_id'
describe 'order index definition', ->
it 'should register an order index', ->
Document.indexOrder('created')
index = Document.__indices[..].pop()
index.type.should.equal 'sorted'
index.key[0].should.equal 'documents:created'
index.score.should.equal 'created'
index.member.should.equal '_id'