-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcounter-cache_tests.js
305 lines (249 loc) · 9.4 KB
/
counter-cache_tests.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
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
var setup = function(test, options) {
var collections = {
authors: new Mongo.Collection('authors' + test.id),
books: new Mongo.Collection('books' + test.id)
}
options = options || {target: {}, source: {}};
_.defaults(options.target, {
collection: collections.authors,
counter: 'booksCount'
});
_.defaults(options.source, {
collection: collections.books,
foreignKey: 'authorId'
});
var bookCounter = new CounterCache(options);
collections.books.mutate = {
insert: function(doc) {
bookCounter.insert(doc);
return collections.books.insert(doc);
},
update: function(id, update) {
bookCounter.update(id, update);
return collections.books.update(id, update);
},
remove: function(id) {
bookCounter.remove(id);
return collections.books.remove(id)
}
};
return collections;
}
Tinytest.add('Counter cache - foreignKey works', function(test) {
var collections = setup(test);
var authorId = collections.authors.insert({
name: 'Charles Darwin'
});
var bookId = collections.books.mutate.insert({
name: 'On the Origin of Species',
authorId: authorId
});
var author = collections.authors.findOne(authorId);
// insert
test.equal(author.booksCount, 1);
collections.books.mutate.remove(bookId);
author = collections.authors.findOne(authorId);
// remove
test.equal(author.booksCount, 0);
// insert book again
bookId = collections.books.mutate.insert({
name: 'On the Origin of Species',
authorId: authorId
});
author = collections.authors.findOne(authorId);
// we should be 1 again
test.equal(author.booksCount, 1);
author2Id = collections.authors.insert({
name: 'Charles Darwin 2'
});
// insert a book for a different author
book2Id = collections.books.mutate.insert({
name: 'A test book',
authorId: author2Id
});
// this should not affect the first author count
author = collections.authors.findOne(authorId);
test.equal(author.booksCount, 1);
collections.books.mutate.remove(book2Id);
// change this book to another author
collections.books.mutate.update(bookId, { $set: { authorId: author2Id }});
author = collections.authors.findOne(authorId);
// book changed author
test.equal(author.booksCount, 0);
var author2 = collections.authors.findOne(author2Id);
test.equal(author2.booksCount, 1);
// set back to original author again
collections.books.mutate.update(bookId, { $set: { authorId: authorId }});
author2 = collections.authors.findOne(author2Id);
test.equal(author2.booksCount, 0);
author = collections.authors.findOne(authorId);
test.equal(author.booksCount, 1);
// remove authorId
collections.books.mutate.update(bookId, { $unset: { authorId: '' }});
author = collections.authors.findOne(authorId);
test.equal(author.booksCount, 0);
// random update that doesn't include the authorId
collections.books.mutate.update(bookId, { $set: { nothing: true }});
test.equal(author.booksCount, 0);
// update with the same id
collections.books.mutate.update(bookId, { $set: { authorId: authorId }});
collections.books.mutate.update(bookId, { $set: { authorId: authorId }});
collections.books.mutate.update(bookId, { $set: { authorId: authorId }});
author = collections.authors.findOne(authorId);
test.equal(author.booksCount, 1);
});
Tinytest.add('Counter cache - foreignKey lookup function', function(test) {
// this one's a bit different
var collections = {
publishers: new Mongo.Collection('publishers' + test.id),
authors: new Mongo.Collection('authors' + test.id),
books: new Mongo.Collection('books' + test.id)
}
var bookCounter = new CounterCache({
target: {
collection: collections.publishers,
counter: 'fictionBooksCount'
},
source: {
collection: collections.books,
foreignKey: function(doc) {
return collections.authors.findOne(doc.authorId).publisherId;
},
filter: function(doc) { return doc.isFiction; }
}
});
collections.books.mutate = {
insert: function(doc) {
bookCounter.insert(doc);
return collections.books.insert(doc);
},
update: function(id, update) {
bookCounter.update(id, update);
return collections.books.update(id, update);
},
remove: function(id) {
bookCounter.remove(id);
return collections.books.remove(id)
}
};
var publisherId = collections.publishers.insert({
name: 'Good collections.books'
});
var publisher2Id = collections.publishers.insert({
name: 'Gooder collections.books'
});
var authorId = collections.authors.insert({
name: 'Charles Darwin',
publisherId: publisherId
});
var author2Id = collections.authors.insert({
name: 'Charles Darwin 2',
publisherId: publisher2Id
});
var bookId = collections.books.mutate.insert({
name: 'On the Origin of Species',
authorId: authorId,
isFiction: true
});
var publisher = collections.publishers.findOne(publisherId);
// insert
test.equal(publisher.fictionBooksCount, 1);
// remove
collections.books.mutate.remove(bookId);
publisher = collections.publishers.findOne(publisherId);
test.equal(publisher.fictionBooksCount, 0);
// TODO: Add tests like above
// Note:
// If we remove an author, the publishers book count will not change to reflect this,
// in this case a before-remove collection-hook on collections.authors should be setup to remove
// all collections.books related to this author.
});
Tinytest.add('Counter cache - filter and foreignKey - add and remove', function(test) {
var collections = setup(test, {
target: {
counter: 'fictionBooksCount'
},
source: {
filter: function(doc) { return doc.isFiction; }
}
});
var authorId = collections.authors.insert({
name: 'Test Author'
});
// Filter increments on insert
var book1Id = collections.books.mutate.insert({
name: 'How to test your book counters',
authorId: authorId,
isFiction: true
});
test.equal(collections.authors.findOne(authorId).fictionBooksCount, 1);
// Filter shouldn't have incremented with this book
var book2Id = collections.books.mutate.insert({
name: 'How to test your book counters again',
authorId: authorId,
isFiction: false
});
test.equal(collections.authors.findOne(authorId).fictionBooksCount, 1);
// filter decrements on delete
collections.books.mutate.remove(book1Id);
test.equal(collections.authors.findOne(authorId).fictionBooksCount, 0);
// but not if it's false
collections.books.mutate.remove(book2Id);
test.equal(collections.authors.findOne(authorId).fictionBooksCount, 0);
});
Tinytest.add('Counter cache - filter and foreignKey - changes', function(test) {
var collections = setup(test, {
target: {
counter: 'fictionBooksCount'
},
source: {
filter: function(doc) { return doc.isFiction; }
}
});
var author1Id = collections.authors.insert({
name: 'Test Author',
fictionBooksCount: 0
});
var author2Id = collections.authors.insert({
name: 'Test Author 2',
fictionBooksCount: 0
});
var bookId = collections.books.mutate.insert({
name: 'How to test your book counters',
authorId: author1Id,
isFiction: true
});
test.equal(collections.authors.findOne(author1Id).fictionBooksCount, 1);
// 1. an irrelevant change
collections.books.mutate.update(bookId, {$set: {name: 'A new way to test'}});
test.equal(collections.authors.findOne(author1Id).fictionBooksCount, 1);
test.equal(collections.authors.findOne(author2Id).fictionBooksCount, 0);
// 2. change it to no longer match
collections.books.mutate.update(bookId, {$set: {isFiction: false}});
test.equal(collections.authors.findOne(author1Id).fictionBooksCount, 0);
test.equal(collections.authors.findOne(author2Id).fictionBooksCount, 0);
// 3. an irrelevant change
collections.books.mutate.update(bookId, {$set: {name: 'A new way to test again'}});
test.equal(collections.authors.findOne(author1Id).fictionBooksCount, 0);
test.equal(collections.authors.findOne(author2Id).fictionBooksCount, 0);
// 4. change author without changing matching
collections.books.mutate.update(bookId, {$set: {authorId: author2Id}});
test.equal(collections.authors.findOne(author1Id).fictionBooksCount, 0);
test.equal(collections.authors.findOne(author2Id).fictionBooksCount, 0);
// 5. change to now match
collections.books.mutate.update(bookId, {$set: {isFiction: true}});
test.equal(collections.authors.findOne(author1Id).fictionBooksCount, 0);
test.equal(collections.authors.findOne(author2Id).fictionBooksCount, 1);
// 6. change author without changing matching
collections.books.mutate.update(bookId, {$set: {authorId: author1Id}});
test.equal(collections.authors.findOne(author1Id).fictionBooksCount, 1);
test.equal(collections.authors.findOne(author2Id).fictionBooksCount, 0);
// 7. change author while changing matching
collections.books.mutate.update(bookId, {$set: {authorId: author2Id, isFiction: false}});
test.equal(collections.authors.findOne(author1Id).fictionBooksCount, 0);
test.equal(collections.authors.findOne(author2Id).fictionBooksCount, 0);
// 8. change author while changing matching
collections.books.mutate.update(bookId, {$set: {authorId: author1Id, isFiction: true}});
test.equal(collections.authors.findOne(author1Id).fictionBooksCount, 1);
test.equal(collections.authors.findOne(author2Id).fictionBooksCount, 0);
});