thermos 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/thermos_test.rb CHANGED
@@ -1,126 +1,113 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class ThermosTest < ActiveSupport::TestCase
4
- ActiveSupport.test_order = :random
5
4
  self.use_transactional_tests = true
5
+ teardown :clear_cache
6
6
 
7
- def teardown
8
- Thermos::BeverageStorage.instance.empty
9
- Rails.cache.clear
10
- end
11
-
12
- test "keeps the cache warm using fill / drink" do
7
+ test 'keeps the cache warm using fill / drink' do
13
8
  mock = Minitest::Mock.new
14
9
 
15
- Thermos.fill(key: "key", model: Category) do |id|
16
- mock.call(id)
17
- end
10
+ Thermos.fill(key: 'key', model: Category) { |id| mock.call(id) }
18
11
 
19
12
  mock.expect(:call, 1, [1])
20
- assert_equal 1, Thermos.drink(key: "key", id: 1)
13
+ assert_equal 1, Thermos.drink(key: 'key', id: 1)
21
14
  mock.verify
22
15
 
23
16
  mock.expect(:call, 2, [1])
24
- assert_equal 1, Thermos.drink(key: "key", id: 1)
17
+ assert_equal 1, Thermos.drink(key: 'key', id: 1)
25
18
  assert_raises(MockExpectationError) { mock.verify }
26
19
  end
27
20
 
28
- test "keeps the cache warm using keep_warm" do
21
+ test 'keeps the cache warm using keep_warm' do
29
22
  mock = Minitest::Mock.new
30
23
 
31
24
  mock.expect(:call, 1, [1])
32
- response = Thermos.keep_warm(key: "key", model: Category, id: 1) do |id|
33
- mock.call(id)
34
- end
25
+ response =
26
+ Thermos.keep_warm(key: 'key', model: Category, id: 1) do |id|
27
+ mock.call(id)
28
+ end
35
29
  assert_equal 1, response
36
30
  mock.verify
37
31
 
38
32
  mock.expect(:call, 2, [1])
39
- response = Thermos.keep_warm(key: "key", model: Category, id: 1) do |id|
40
- mock.call(id)
41
- end
33
+ response =
34
+ Thermos.keep_warm(key: 'key', model: Category, id: 1) do |id|
35
+ mock.call(id)
36
+ end
42
37
  assert_equal 1, response
43
38
  assert_raises(MockExpectationError) { mock.verify }
44
39
  end
45
40
 
46
- # primary model changes
47
- test "rebuilds the cache on primary model change" do
41
+ # primary model changes
42
+ test 'rebuilds the cache on primary model change' do
48
43
  mock = Minitest::Mock.new
49
44
  category = categories(:baseball)
50
45
 
51
- Thermos.fill(key: "key", model: Category) do |id|
52
- mock.call(id)
53
- end
46
+ Thermos.fill(key: 'key', model: Category) { |id| mock.call(id) }
54
47
 
55
48
  mock.expect(:call, 1, [category.id])
56
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
49
+ assert_equal 1, Thermos.drink(key: 'key', id: category.id)
57
50
  mock.verify
58
51
 
59
52
  mock.expect(:call, 2, [category.id])
60
- category.update!(name: "foo")
53
+ category.update!(name: 'foo')
61
54
  mock.verify
62
55
 
63
56
  mock.expect(:call, 3, [category.id])
64
- assert_equal 2, Thermos.drink(key: "key", id: category.id)
57
+ assert_equal 2, Thermos.drink(key: 'key', id: category.id)
65
58
  assert_raises(MockExpectationError) { mock.verify }
66
59
  end
67
60
 
68
- test "does not rebuild the cache on rolled back primary model change" do
61
+ test 'does not rebuild the cache on rolled back primary model change' do
69
62
  mock = Minitest::Mock.new
70
63
  category = categories(:baseball)
71
64
 
72
- Thermos.fill(key: "key", model: Category) do |id|
73
- mock.call(id)
74
- end
65
+ Thermos.fill(key: 'key', model: Category) { |id| mock.call(id) }
75
66
 
76
67
  mock.expect(:call, 1, [category.id])
77
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
68
+ assert_equal 1, Thermos.drink(key: 'key', id: category.id)
78
69
  mock.verify
79
70
 
80
71
  mock.expect(:call, 2, [category.id])
81
72
  ActiveRecord::Base.transaction do
82
- category.update!(name: "foo")
73
+ category.update!(name: 'foo')
83
74
  raise ActiveRecord::Rollback
84
75
  end
85
76
  assert_raises(MockExpectationError) { mock.verify }
86
77
 
87
78
  mock.expect(:call, 3, [category.id])
88
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
79
+ assert_equal 1, Thermos.drink(key: 'key', id: category.id)
89
80
  assert_raises(MockExpectationError) { mock.verify }
90
81
  end
91
82
 
92
- test "does not rebuild the cache for an unrelated primary model change" do
83
+ test 'does not rebuild the cache for an unrelated primary model change' do
93
84
  mock = Minitest::Mock.new
94
85
  category = categories(:baseball)
95
- other_category = Category.create!(name: "bar")
86
+ other_category = Category.create!(name: 'bar')
96
87
 
97
- Thermos.fill(key: "key", model: Category) do |id|
98
- mock.call(id)
99
- end
88
+ Thermos.fill(key: 'key', model: Category) { |id| mock.call(id) }
100
89
 
101
90
  mock.expect(:call, 2, [other_category.id])
102
- assert_equal 2, Thermos.drink(key: "key", id: other_category.id)
91
+ assert_equal 2, Thermos.drink(key: 'key', id: other_category.id)
103
92
  mock.verify
104
93
 
105
94
  mock.expect(:call, 1, [category.id])
106
- category.update!(name: "foo")
95
+ category.update!(name: 'foo')
107
96
  mock.verify
108
97
 
109
98
  mock.expect(:call, 3, [other_category.id])
110
- assert_equal 2, Thermos.drink(key: "key", id: other_category.id)
99
+ assert_equal 2, Thermos.drink(key: 'key', id: other_category.id)
111
100
  assert_raises(MockExpectationError) { mock.verify }
112
101
  end
113
102
 
114
- test "does not rebuild the cache on primary model destroy" do
103
+ test 'does not rebuild the cache on primary model destroy' do
115
104
  mock = Minitest::Mock.new
116
105
  category = categories(:baseball)
117
106
 
118
- Thermos.fill(key: "key", model: Category) do |id|
119
- mock.call(id)
120
- end
107
+ Thermos.fill(key: 'key', model: Category) { |id| mock.call(id) }
121
108
 
122
109
  mock.expect(:call, 1, [category.id])
123
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
110
+ assert_equal 1, Thermos.drink(key: 'key', id: category.id)
124
111
  mock.verify
125
112
 
126
113
  mock.expect(:call, 2, [category.id])
@@ -128,377 +115,40 @@ class ThermosTest < ActiveSupport::TestCase
128
115
  assert_raises(MockExpectationError) { mock.verify }
129
116
  end
130
117
 
131
- test "pre-builds cache for new primary model records" do
132
- mock = Minitest::Mock.new
133
-
134
- Thermos.fill(key: "key", model: Category, lookup_key: "name") do |name|
135
- mock.call(name)
136
- end
137
-
138
- mock.expect(:call, 1, ["foo"])
139
- Category.create!(name: "foo")
140
- mock.verify
141
-
142
- mock.expect(:call, 2, ["foo"])
143
- assert_equal 1, Thermos.drink(key: "key", id: "foo")
144
- assert_raises(MockExpectationError) { mock.verify }
145
- end
146
-
147
- test "allows filtering for which records should be rebuilt" do
148
- mock = Minitest::Mock.new
149
- category = categories(:baseball)
150
- filter = ->(model) { model.name.match("ball") }
151
- Thermos.fill(key: "key", model: Category, lookup_key: "name", filter: filter) do |name|
152
- mock.call(name)
153
- end
154
-
155
- mock.expect(:call, 1, ["basketball"])
156
- category.update!(name: "basketball")
157
- mock.verify
158
-
159
- mock.expect(:call, 1, ["hockey"])
160
- category.update!(name: "hockey")
161
- assert_raises(MockExpectationError) { mock.verify }
162
- end
163
-
164
- test "allows filtering based on the beverage when multiple beverages are configured and only one of them has a filter" do
118
+ test 'pre-builds cache for new primary model records' do
165
119
  mock = Minitest::Mock.new
166
- store = stores(:supermarket)
167
- category = categories(:baseball)
168
- # filter method specific to one model
169
- # store.ball? doesn't exist
170
- filter = ->(model) { model.ball? }
171
120
 
172
- Thermos.fill(key: "key", model: Category, lookup_key: "name", filter: filter) do |name|
121
+ Thermos.fill(key: 'key', model: Category, lookup_key: 'name') do |name|
173
122
  mock.call(name)
174
123
  end
175
124
 
176
- Thermos.fill(key: "key_2", model: Store, lookup_key: "name") do |name|
177
- mock.call(name)
178
- end
179
-
180
- mock.expect(:call, 1, ["groceries"])
181
- store.update!(name: "groceries")
182
- assert_equal 1, Thermos.drink(key: "key_2", id: "groceries")
183
- mock.verify
184
- end
185
-
186
- # has_many model changes
187
- test "rebuilds the cache on has_many model change" do
188
- mock = Minitest::Mock.new
189
- category = categories(:baseball)
190
- category_item = category_items(:baseball_glove)
191
-
192
- Thermos.fill(key: "key", model: Category, deps: [:category_items]) do |id|
193
- mock.call(id)
194
- end
195
-
196
- mock.expect(:call, 1, [category.id])
197
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
198
- mock.verify
199
-
200
- mock.expect(:call, 2, [category.id])
201
- category_item.update!(name: "foo")
202
- mock.verify
203
-
204
- mock.expect(:call, 3, [category.id])
205
- assert_equal 2, Thermos.drink(key: "key", id: category.id)
206
- assert_raises(MockExpectationError) { mock.verify }
207
- end
208
-
209
- test "does not rebuild the cache for an unrelated has_many model change" do
210
- mock = Minitest::Mock.new
211
- category = categories(:baseball)
212
- category_item = CategoryItem.create(category: nil)
213
-
214
- Thermos.fill(key: "key", model: Category, deps: [:category_items]) do |id|
215
- mock.call(id)
216
- end
217
-
218
- mock.expect(:call, 1, [category.id])
219
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
220
- mock.verify
221
-
222
- mock.expect(:call, 2, [category.id])
223
- category_item.update!(name: "foo")
224
- assert_raises(MockExpectationError) { mock.verify }
225
-
226
- mock.expect(:call, 3, [category.id])
227
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
228
- assert_raises(MockExpectationError) { mock.verify }
229
- end
230
-
231
- test "re-builds the cache for new has_many records" do
232
- mock = Minitest::Mock.new
233
- category = categories(:baseball)
234
-
235
- Thermos.fill(key: "key", model: Category, deps: [:category_items]) do |id|
236
- mock.call(id)
237
- end
238
-
239
- mock.expect(:call, 1, [category.id])
240
- CategoryItem.create!(category: category)
241
- mock.verify
242
-
243
- mock.expect(:call, 2, [category.id])
244
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
245
- assert_raises(MockExpectationError) { mock.verify }
246
- end
247
-
248
- test "re-builds the cache for has_many record changes when filter condition is met" do
249
- mock = Minitest::Mock.new
250
- category = categories(:baseball)
251
- filter = ->(model) { model.ball? }
252
-
253
- Thermos.fill(key: "key", model: Category, deps: [:category_items], filter: filter) do |id|
254
- mock.call(id)
255
- end
256
-
257
- mock.expect(:call, 1, [category.id])
258
- CategoryItem.create!(category: category)
259
- mock.verify
260
-
261
- category.update!(name: "hockey")
262
-
263
- mock.expect(:call, 1, [category.id])
264
- CategoryItem.create!(category: category)
265
- assert_raises(MockExpectationError) { mock.verify }
266
- end
267
-
268
- # belongs_to model changes
269
- test "rebuilds the cache on belongs_to model change" do
270
- mock = Minitest::Mock.new
271
- category = categories(:baseball)
272
- store = stores(:sports)
273
-
274
- Thermos.fill(key: "key", model: Category, deps: [:store]) do |id|
275
- mock.call(id)
276
- end
277
-
278
- mock.expect(:call, 1, [category.id])
279
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
280
- mock.verify
281
-
282
- mock.expect(:call, 2, [category.id])
283
- store.update!(name: "foo")
284
- mock.verify
285
-
286
- mock.expect(:call, 3, [category.id])
287
- assert_equal 2, Thermos.drink(key: "key", id: category.id)
288
- assert_raises(MockExpectationError) { mock.verify }
289
- end
290
-
291
- test "does not rebuild the cache for an unrelated belongs_to model change" do
292
- mock = Minitest::Mock.new
293
- category = categories(:baseball)
294
- store = Store.create!
295
-
296
- Thermos.fill(key: "key", model: Category, deps: [:store]) do |id|
297
- mock.call(id)
298
- end
299
-
300
- mock.expect(:call, 1, [category.id])
301
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
302
- mock.verify
303
-
304
- mock.expect(:call, 2, [category.id])
305
- store.update!(name: "foo")
306
- assert_raises(MockExpectationError) { mock.verify }
307
-
308
- mock.expect(:call, 3, [category.id])
309
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
310
- assert_raises(MockExpectationError) { mock.verify }
311
- end
312
-
313
- test "re-builds the cache for new belongs_to records" do
314
- mock = Minitest::Mock.new
315
- category = categories(:baseball)
316
-
317
- Thermos.fill(key: "key", model: Category, deps: [:store]) do |id|
318
- mock.call(id)
319
- end
320
-
321
- mock.expect(:call, 1, [category.id])
322
- mock.expect(:call, 1, [category.id])
323
- Store.create!(name: "foo", categories: [category])
324
- mock.verify
325
-
326
- mock.expect(:call, 2, [category.id])
327
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
328
- assert_raises(MockExpectationError) { mock.verify }
329
- end
330
-
331
- test "re-builds the cache for belongs_to record changes when filter condition is met" do
332
- mock = Minitest::Mock.new
333
- category = categories(:baseball)
334
- filter = ->(model) { model.ball? }
335
-
336
- Thermos.fill(key: "key", model: Category, deps: [:store], filter: filter) do |id|
337
- mock.call(id)
338
- end
339
-
340
- mock.expect(:call, 1, [category.id])
341
- mock.expect(:call, 1, [category.id])
342
- Store.create!(name: "foo", categories: [category])
343
- mock.verify
344
-
345
- category.update!(name: "hockey")
346
-
347
- mock.expect(:call, 2, [category.id])
348
- Store.create!(name: "bar", categories: [category])
349
- assert_raises(MockExpectationError) { mock.verify }
350
- end
351
-
352
- # has_many through model changes
353
- test "rebuilds the cache on has_many through model change" do
354
- mock = Minitest::Mock.new
355
- category = categories(:baseball)
356
- product = products(:glove)
357
-
358
- Thermos.fill(key: "key", model: Category, deps: [:products]) do |id|
359
- mock.call(id)
360
- end
361
-
362
- mock.expect(:call, 1, [category.id])
363
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
364
- mock.verify
365
-
366
- mock.expect(:call, 2, [category.id])
367
- product.update!(name: "foo")
125
+ mock.expect(:call, 1, ['foo'])
126
+ Category.create!(name: 'foo')
368
127
  mock.verify
369
128
 
370
- mock.expect(:call, 3, [category.id])
371
- assert_equal 2, Thermos.drink(key: "key", id: category.id)
129
+ mock.expect(:call, 2, ['foo'])
130
+ assert_equal 1, Thermos.drink(key: 'key', id: 'foo')
372
131
  assert_raises(MockExpectationError) { mock.verify }
373
132
  end
374
133
 
375
- test "does not rebuild the cache for an unrelated has_many through model change" do
376
- mock = Minitest::Mock.new
377
- category = categories(:baseball)
378
- product = Product.create!
379
-
380
- Thermos.fill(key: "key", model: Category, deps: [:products]) do |id|
381
- mock.call(id)
382
- end
383
-
384
- mock.expect(:call, 1, [category.id])
385
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
386
- mock.verify
387
-
388
- mock.expect(:call, 2, [category.id])
389
- product.update!(name: "foo")
390
- assert_raises(MockExpectationError) { mock.verify }
391
-
392
- mock.expect(:call, 3, [category.id])
393
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
394
- assert_raises(MockExpectationError) { mock.verify }
395
- end
396
-
397
- test "re-builds the cache for new has_many through records" do
398
- mock = Minitest::Mock.new
399
- category = categories(:baseball)
400
-
401
- Thermos.fill(key: "key", model: Category, deps: [:products]) do |id|
402
- mock.call(id)
403
- end
404
-
405
- mock.expect(:call, 1, [category.id])
406
- Product.create!(categories: [category])
407
- mock.verify
408
-
409
- mock.expect(:call, 2, [category.id])
410
- assert_equal 1, Thermos.drink(key: "key", id: category.id)
411
- assert_raises(MockExpectationError) { mock.verify }
412
- end
413
-
414
- test "re-builds the cache for has_many through record changes when filter condition is met" do
415
- mock = Minitest::Mock.new
416
- category = categories(:baseball)
417
- filter = ->(model) { model.ball? }
418
-
419
- Thermos.fill(key: "key", model: Category, deps: [:products], filter: filter) do |id|
420
- mock.call(id)
421
- end
422
-
423
- mock.expect(:call, 1, [category.id])
424
- Product.create!(categories: [category])
425
- mock.verify
426
-
427
- category.update!(name: "hockey")
428
-
429
- mock.expect(:call, 2, [category.id])
430
- Product.create!(categories: [category])
431
- assert_raises(MockExpectationError) { mock.verify }
432
- end
433
-
434
- test "handles indirect associations" do
435
- mock = Minitest::Mock.new
436
- category = categories(:baseball)
437
- store = category.store
438
-
439
- Thermos.fill(key: "key", model: Store, deps: [categories: [:products]]) do |id|
440
- mock.call(id)
441
- end
442
-
443
- mock.expect(:call, 1, [store.id])
444
- category.update!(name: "foo")
445
- mock.verify
446
-
447
- mock.expect(:call, 2, [store.id])
448
- assert_equal 1, Thermos.drink(key: "key", id: store.id)
449
- assert_raises(MockExpectationError) { mock.verify }
450
- Product.create!(categories: [category])
451
- mock.verify
452
-
453
- mock.expect(:call, 3, [store.id])
454
- assert_equal 2, Thermos.drink(key: "key", id: store.id)
455
- assert_raises(MockExpectationError) { mock.verify }
456
- end
457
-
458
- test "only rebuilds cache for stated dependencies, even if another cache has an associated model of the primary" do
459
- category_mock = Minitest::Mock.new
460
- product_mock = Minitest::Mock.new
461
- category = categories(:baseball)
462
- product = products(:glove)
463
-
464
- Thermos.fill(key: "category_key", model: Category) do |id|
465
- category_mock.call(id)
466
- end
467
-
468
- Thermos.fill(key: "product_key", model: Product) do |id|
469
- product_mock.call(id)
470
- end
471
-
472
- category_mock.expect(:call, 2, [category.id])
473
- product_mock.expect(:call, 2, [product.id])
474
- product.update!(name: "foo")
475
- assert_raises(MockExpectationError) { category_mock.verify }
476
- product_mock.verify
477
- end
478
-
479
134
  test "accepts and can rebuild off of an id other than the 'id'" do
480
135
  mock = Minitest::Mock.new
481
136
  category = categories(:baseball)
482
- product = products(:glove)
483
137
 
484
- Thermos.fill(key: "key", model: Category, deps: [:products], lookup_key: :name) do |id|
138
+ Thermos.fill(key: 'key', model: Category, lookup_key: :name) do |id|
485
139
  mock.call(id)
486
140
  end
487
141
 
488
142
  mock.expect(:call, 1, [category.name])
489
- assert_equal 1, Thermos.drink(key: "key", id: category.name)
143
+ assert_equal 1, Thermos.drink(key: 'key', id: category.name)
490
144
  mock.verify
491
145
 
492
- mock.expect(:call, 2, ["foo"])
493
- category.update!(name: "foo")
146
+ mock.expect(:call, 2, ['foo'])
147
+ category.update!(name: 'foo')
494
148
  mock.verify
495
149
 
496
150
  mock.expect(:call, 3, [category.name])
497
- product.update!(name: "foo")
498
- mock.verify
499
-
500
- mock.expect(:call, 4, [category.name])
501
- assert_equal 3, Thermos.drink(key: "key", id: category.name)
151
+ assert_equal 2, Thermos.drink(key: 'key', id: category.name)
502
152
  assert_raises(MockExpectationError) { mock.verify }
503
153
  end
504
154
  end