This repository was archived by the owner on Nov 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcategorized_counter_cache_spec.rb
More file actions
407 lines (309 loc) · 13.1 KB
/
categorized_counter_cache_spec.rb
File metadata and controls
407 lines (309 loc) · 13.1 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
require 'spec_helper'
describe Mongoid::CategorizedCounterCache do
let(:company) { Company.create }
context 'when creating record' do
it 'should set count for the matched category' do
staff = Staff.create company: company, gender: :male
company.reload
expect(company.staffs_count).to eq(1)
expect(company.staffs_male_count).to eq(1)
expect(company.staffs_female_count).to eq(0)
end
it 'should increase count for the matched category' do
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
staff = Staff.create company: company, gender: :male
company.reload
expect(company.staffs_count).to eq(6)
expect(company.staffs_male_count).to eq(4)
expect(company.staffs_female_count).to eq(2)
end
end
context 'when updating record' do
let(:staff) { staff = Staff.create name: 'anna', company: company, gender: :male }
before do
staff
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
end
context 'company has not been changed' do
it 'does nothing if category not changed' do
staff.update_attributes name: 'elsa'
company.reload
expect(company.staffs_count).to eq(5)
expect(company.staffs_male_count).to eq(3)
expect(company.staffs_female_count).to eq(2)
end
it 'increase the current category counter and decrease the original counter if category has been changed' do
staff.update_attributes gender: :female
company.reload
expect(company.staffs_count).to eq(5)
expect(company.staffs_male_count).to eq(2)
expect(company.staffs_female_count).to eq(3)
end
it 'handles if category changed to empty/nil' do
staff.update_attributes gender: nil
company.reload
expect(company.staffs_count).to eq(5)
expect(company.staffs_male_count).to eq(2)
expect(company.staffs_female_count).to eq(2)
end
it 'handles if category changed from empty/nil' do
a_staff = Staff.create name: 'anna', company: company, gender: ''
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
a_staff.update_attributes gender: :male
company.reload
expect(company.staffs_count).to eq(5)
expect(company.staffs_male_count).to eq(4)
expect(company.staffs_female_count).to eq(2)
end
end
context 'company has been changed' do
let(:another_company) { Company.create }
context 'category has not been changed' do
it 'should decrease count from original record and increase count on new record' do
staff = Staff.create company: company, gender: :male
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
another_company.set staffs_count: 50
another_company.set staffs_male_count: 30
another_company.set staffs_female_count: 20
staff.update_attributes company: another_company
company.reload
another_company.reload
expect(company.staffs_count).to eq(4)
expect(company.staffs_male_count).to eq(2)
expect(company.staffs_female_count).to eq(2)
expect(another_company.staffs_count).to eq(51)
expect(another_company.staffs_male_count).to eq(31)
expect(another_company.staffs_female_count).to eq(20)
end
it 'should decrease count of original record if new record is empty' do
staff = Staff.create company: company, gender: :male
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
staff.update_attributes company: nil
company.reload
expect(company.staffs_count).to eq(5) # mongoid `counter_cache` issue: it wont decrease if you update record to nil
expect(company.staffs_male_count).to eq(2)
expect(company.staffs_female_count).to eq(2)
end
it 'should increase count of current record if original record is empty' do
staff = Staff.create company: nil, gender: :male
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
staff.update_attributes company: company
company.reload
expect(company.staffs_count).to eq(6)
expect(company.staffs_male_count).to eq(4)
expect(company.staffs_female_count).to eq(2)
end
end
context 'category has also been changed' do
it 'should decrease original category count of original record and increase current category count of new record' do
staff = Staff.create company: company, gender: :male
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
another_company.set staffs_count: 50
another_company.set staffs_male_count: 30
another_company.set staffs_female_count: 20
staff.update_attributes company: another_company, gender: :female
company.reload
another_company.reload
expect(company.staffs_count).to eq(4)
expect(company.staffs_male_count).to eq(2)
expect(company.staffs_female_count).to eq(2)
expect(another_company.staffs_count).to eq(51)
expect(another_company.staffs_male_count).to eq(30)
expect(another_company.staffs_female_count).to eq(21)
end
it 'handles original category as empty/nil' do
staff = Staff.create company: company, gender: nil
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
another_company.set staffs_count: 50
another_company.set staffs_male_count: 30
another_company.set staffs_female_count: 20
staff.update_attributes company: another_company, gender: :female
company.reload
another_company.reload
expect(company.staffs_count).to eq(4)
expect(company.staffs_male_count).to eq(3)
expect(company.staffs_female_count).to eq(2)
expect(another_company.staffs_count).to eq(51)
expect(another_company.staffs_male_count).to eq(30)
expect(another_company.staffs_female_count).to eq(21)
end
it 'handles current category as empty/nil' do
staff = Staff.create company: company, gender: :male
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
another_company.set staffs_count: 50
another_company.set staffs_male_count: 30
another_company.set staffs_female_count: 20
staff.update_attributes company: another_company, gender: nil
company.reload
another_company.reload
expect(company.staffs_count).to eq(4)
expect(company.staffs_male_count).to eq(2)
expect(company.staffs_female_count).to eq(2)
expect(another_company.staffs_count).to eq(51)
expect(another_company.staffs_male_count).to eq(30)
expect(another_company.staffs_female_count).to eq(20)
end
end
end
end
context 'when destroying record' do
it 'should decrease count from the destroyed record' do
staff = Staff.create company: company, gender: :male
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
staff.destroy
company.reload
expect(company.staffs_count).to eq(4)
expect(company.staffs_male_count).to eq(2)
expect(company.staffs_female_count).to eq(2)
end
it 'handles original category as empty/nil' do
staff = Staff.create company: company, gender: nil
company.set staffs_count: 5
company.set staffs_male_count: 3
company.set staffs_female_count: 2
staff.destroy
company.reload
expect(company.staffs_count).to eq(4)
expect(company.staffs_male_count).to eq(3)
expect(company.staffs_female_count).to eq(2)
end
end
context 'category as prefix' do
let(:company) { CategoryAsPrefix::Company.create }
let(:user) { CategoryAsPrefix::User.create company: company, status: :active }
it 'create user' do
user
company.reload
expect(company.users_count).to eq(1)
expect(company.active_users_count).to eq(1)
expect(company.inactive_users_count).to eq(0)
end
end
context 'using relation as category' do
let(:company) { CategoryAsRelation::Company.create }
let(:west_city) { CategoryAsRelation::City.create region: :west }
let(:east_city) { CategoryAsRelation::City.create region: :east }
let(:user) { CategoryAsRelation::User.create company: company, city: west_city }
before do
user
end
context 'creation' do
it 'increase counter when create resource with resource' do
company.set users_count: 5
company.set users_west_count: 3
company.set users_east_count: 2
CategoryAsRelation::User.create company: company, city: west_city
company.reload
expect(company.users_count).to eq(6)
expect(company.users_west_count).to eq(4)
expect(company.users_east_count).to eq(2)
end
it 'handles empty/nil value for relation' do
company.set users_count: 5
company.set users_west_count: 3
company.set users_east_count: 2
CategoryAsRelation::User.create company: company, city: nil
company.reload
expect(company.users_count).to eq(6)
expect(company.users_west_count).to eq(3)
expect(company.users_east_count).to eq(2)
end
it 'handles empty/nil value for cateogry form relation' do
company.set users_count: 5
company.set users_west_count: 3
company.set users_east_count: 2
no_region_city = CategoryAsRelation::City.create
CategoryAsRelation::User.create company: company, city: no_region_city
company.reload
expect(company.users_count).to eq(6)
expect(company.users_west_count).to eq(3)
expect(company.users_east_count).to eq(2)
end
end
context 'relation updated' do
let(:another_company) { CategoryAsRelation::Company.create }
context 'category has not been changed' do
it 'decrease the counter of original relation and increase the counter of current relation' do
company.set users_count: 5
company.set users_west_count: 3
company.set users_east_count: 2
another_company.set users_count: 50
another_company.set users_west_count: 30
another_company.set users_east_count: 20
user.update_attributes company: another_company
company.reload
another_company.reload
expect(company.users_count).to eq(4)
expect(company.users_west_count).to eq(2)
expect(company.users_east_count).to eq(2)
expect(another_company.users_count).to eq(51)
expect(another_company.users_west_count).to eq(31)
expect(another_company.users_east_count).to eq(20)
end
end
context 'category relation has been changed' do
it 'decrease the counter of original relation and increase the counter of current relation' do
company.set users_count: 5
company.set users_west_count: 3
company.set users_east_count: 2
another_company.set users_count: 50
another_company.set users_west_count: 30
another_company.set users_east_count: 20
user.update_attributes company: another_company, city: east_city
company.reload
another_company.reload
expect(company.users_count).to eq(4)
expect(company.users_west_count).to eq(2)
expect(company.users_east_count).to eq(2)
expect(another_company.users_count).to eq(51)
expect(another_company.users_west_count).to eq(30)
expect(another_company.users_east_count).to eq(21)
end
end
end
context 'category has been updated' do
it 'decrease the counter of original category and increase the counter of current category' do
company.set users_count: 5
company.set users_west_count: 3
company.set users_east_count: 2
user.update_attributes city: east_city
company.reload
expect(company.users_count).to eq(5)
expect(company.users_west_count).to eq(2)
expect(company.users_east_count).to eq(3)
end
end
context 'destroy' do
it 'decrease the counter of original category' do
company.set users_count: 5
company.set users_west_count: 3
company.set users_east_count: 2
user.destroy
company.reload
expect(company.users_count).to eq(4)
expect(company.users_west_count).to eq(2)
expect(company.users_east_count).to eq(2)
end
end
end
end