-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathn1_loader_spec.rb
More file actions
440 lines (350 loc) · 13.6 KB
/
Copy pathn1_loader_spec.rb
File metadata and controls
440 lines (350 loc) · 13.6 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# frozen_string_literal: true
RSpec.describe N1Loader do
let(:loader) do
Class.new(N1Loader::Loader) do
def perform(elements)
elements.each { |element| fulfill(element, [element]) }
end
end
end
let(:klass) do
custom_loader = loader
Class.new do
include N1Loader::Loadable
class << self
def perform!
@count = count + 1
end
def count
@count || 0
end
end
n1_optimized :inline do |elements|
elements.first.class.perform!
elements.each { |element| fulfill(element, [element]) }
end
n1_optimized :sleepy do |elements|
sleep(0.5)
elements.first.class.perform!
elements.each { |element| fulfill(element, [element]) }
end
n1_optimized :custom, custom_loader
n1_optimized :single_optimized do
def single(element)
[element]
end
def perform(_elements)
raise "unknown"
end
end
n1_optimized :missing_fulfill do
def perform(elements)
elements.group_by(&:itself)
end
end
n1_optimized :with_arguments do
argument :something
argument :anything
def perform(elements)
elements.first.class.perform!
elements.each do |element|
fulfill(element, [element, something, anything])
end
end
end
n1_optimized :with_optional_argument do
argument :something, optional: true
argument :anything
def perform(elements)
elements.first.class.perform!
elements.each do |element|
fulfill(element, [element, something, anything])
end
end
end
n1_optimized :with_default_argument do
argument :something, default: -> { [] }
argument :anything
def perform(elements)
elements.first.class.perform!
elements.each do |element|
fulfill(element, [element, something, anything])
end
end
end
n1_optimized :with_custom_arguments_key do
argument :something
argument :anything
cache_key { something + anything }
def perform(elements)
elements.first.class.perform!
elements.each do |element|
fulfill(element, [element, something, anything])
end
end
end
n1_optimized :with_question_mark? do
argument :something
def perform(elements)
elements.first.class.perform!
elements.each do |element|
fulfill(element, [element, something])
end
end
end
end
end
let(:child_klass) do
Class.new(klass) do
n1_optimized :child_something do |elements|
elements.first.class.perform!
elements.each do |element|
fulfill(element, [element])
end
end
end
end
let(:object) { klass.new }
let(:objects) { [klass.new, klass.new] }
it "works with unsupported objects" do
expect do
N1Loader::Preloader.new([object, 123]).preload(:with_arguments)
end.not_to raise_error(NoMethodError)
end
describe "thread-safety" do
it "is thread-safe" do
N1Loader::Preloader.new(objects).preload(:sleepy)
threads = []
10.times do
threads << Thread.new do
objects.each do |obj|
expect(obj.sleepy).to eq([obj])
end
end
end
threads.each(&:join)
expect(klass.count).to eq(1)
end
end
describe "error handling" do
it "raises the same error on the subsequent calls" do
faulty_klass = Class.new do
include N1Loader::Loadable
n1_optimized :faulty do |_|
raise StandardError, "Something went wrong"
end
end
faulty_object = faulty_klass.new
expect { faulty_object.faulty }.to raise_error(StandardError, "Something went wrong")
expect { faulty_object.faulty }.to raise_error(StandardError, "Something went wrong")
end
end
describe "loaded comparison" do
it "compares by value" do
instance = loader.new(objects)
expect(instance.for(objects.first)).to eq([objects.first])
expect { instance.for(object) }.to raise_error(N1Loader::NotLoaded)
end
end
context "when fulfill was not used" do
it "throws error" do
expect { object.missing_fulfill }
.to raise_error(N1Loader::NotFilled, "Nothing was preloaded, perhaps you forgot to use fulfill method")
end
end
describe "question mark support" do
it "works" do
expect { object.with_question_mark?(something: "something") }.not_to raise_error
expect(object.with_question_mark?(something: "something")).to eq([object, "something"])
end
end
describe "clear cache" do
it "works" do
expect { object.inline }.to change(klass, :count).by(1)
expect { object.inline }.not_to change(klass, :count)
object.n1_clear_cache
expect { object.inline }.to change(klass, :count).by(1)
expect { object.inline }.not_to change(klass, :count)
end
context "with parent loader" do
let(:object) { child_klass.new }
it "works" do
expect { object.inline }.to change(child_klass, :count).by(1)
expect { object.child_something }.to change(child_klass, :count).by(1)
expect { object.inline }.not_to change(child_klass, :count)
expect { object.child_something }.not_to change(child_klass, :count)
object.n1_clear_cache
expect { object.inline }.to change(child_klass, :count).by(1)
expect { object.child_something }.to change(child_klass, :count).by(1)
expect { object.inline }.not_to change(child_klass, :count)
expect { object.child_something }.not_to change(child_klass, :count)
end
end
end
describe "arguments support" do
it "has to receive all arguments" do
expect { object.with_arguments }.to raise_error(N1Loader::MissingArgument)
expect { object.with_arguments(something: "something") }.to raise_error(N1Loader::MissingArgument)
expect { object.with_arguments("something") }.to raise_error(ArgumentError)
expect(object.with_arguments(something: "something",
anything: "anything")).to eq([object,
"something", "anything"])
end
it "supports optional arguments" do
expect { object.with_optional_argument }
.to raise_error(N1Loader::MissingArgument, "Loader requires [:anything] arguments but they are missing")
expect(object.with_optional_argument(anything: 2)).to eq([object, nil, 2])
expect(object.with_optional_argument(something: 1, anything: 2)).to eq([object, 1, 2])
expect { object.with_optional_argument(tmp: 1, anything: 2) }
.to raise_error(N1Loader::InvalidArgument, "Loader doesn't define tmp argument")
end
it "supports default arguments" do
expect { object.with_default_argument }
.to raise_error(N1Loader::MissingArgument, "Loader requires [:anything] arguments but they are missing")
expect(object.with_default_argument(anything: 2)).to eq([object, [], 2])
expect(object.with_default_argument(something: 1, anything: 2)).to eq([object, 1, 2])
expect { object.with_default_argument(tmp: 1, anything: 2) }
.to raise_error(N1Loader::InvalidArgument, "Loader doesn't define tmp argument")
end
it "can have custom arguments key" do
# The following two has the same result for custom key, so we only do one perform
expect { object.with_custom_arguments_key(something: 1, anything: 2) }.to change(klass, :count).by(1)
expect { object.with_custom_arguments_key(something: 2, anything: 1) }.not_to change(klass, :count)
expect { object.with_custom_arguments_key(something: 2, anything: 3) }.to change(klass, :count).by(1)
end
it "supports named arguments" do
expect do
object.with_custom_arguments_key
end.to raise_error(N1Loader::MissingArgument,
"Loader requires [:something, :anything] arguments but they are missing")
expect do
object.with_custom_arguments_key(something: "something")
end.to raise_error(N1Loader::MissingArgument,
"Loader requires [:anything] arguments but they are missing")
expect(object.with_custom_arguments_key(something: "something",
anything: "anything")).to eq([
object, "something", "anything"
])
end
it "supports falsey argument values" do
expect(object.with_default_argument(anything: 2)).to eq([object, [], 2]) # default value
expect(object.with_default_argument(something: false, anything: 2)).to eq([object, false, 2]) # false
expect(object.with_default_argument(something: nil, anything: 2)).to eq([object, nil, 2]) # nil
end
it "works with preloading" do
N1Loader::Preloader.new(objects).preload(:with_arguments)
expect do
objects.each do |object|
expect(object.with_arguments(something: "something",
anything: "anything")).to eq([object,
"something", "anything"])
end
end.to change(klass, :count).by(1)
end
it "caches based on arguments" do
N1Loader::Preloader.new(objects).preload(:with_arguments, :with_default_argument)
expect do
objects.each { |object| object.with_arguments(something: "something", anything: "anything") }
end.to change(klass, :count).by(1)
expect do
objects.each { |object| object.with_arguments(something: "something2", anything: "anything") }
end.to change(klass, :count).by(1)
expect do
objects.each { |object| object.with_arguments(something: "something", anything: "anything2") }
end.to change(klass, :count).by(1)
expect do
objects.each { |object| object.with_arguments(something: "something", anything: "anything") }
end.not_to change(klass, :count)
expect do
objects.each { |object| object.with_default_argument(something: false, anything: nil) }
end.to change(klass, :count).by(1)
expect do
objects.each { |object| object.with_default_argument(something: false, anything: nil) }
end.not_to change(klass, :count)
end
it "supports reloading" do
expect do
object.with_arguments(something: "something", anything: "anything")
end.to change(klass, :count).by(1)
expect do
object.with_arguments(something: "something", anything: "anything", reload: true)
end.to change(klass, :count).by(1)
expect do
object.with_arguments(something: "something", anything: "anything")
end.not_to change(klass, :count)
end
end
describe "optimization for single object" do
it "uses optimization" do
expect(object.single_optimized).to eq([object])
N1Loader::Preloader.new(objects).preload(:single_optimized)
expect { objects.map(&:single_optimized) }.to raise_error(StandardError, "unknown")
end
end
describe "isolated loaders" do
it "does not need injection" do
instance = loader.new(objects)
objects.each do |object|
expect(instance.for(object)).to eq([object])
end
end
it "checks that element was provided" do
instance = loader.new(objects)
objects.each do |object|
expect(instance.for(object)).to eq([object])
end
expect do
instance.for(object)
end.to raise_error(N1Loader::NotLoaded, "The data was not preloaded for the given element")
end
end
describe "reloading" do
context "with preloading" do
it "reloads cached data" do
N1Loader::Preloader.new(objects).preload(:inline)
expect { objects.map(&:inline) }.to change(klass, :count).by(1)
N1Loader::Preloader.new(objects).preload(:inline)
expect { objects.map(&:inline) }.to change(klass, :count).by(1)
expect { objects.map(&:inline) }.not_to change(klass, :count)
end
end
context "without preloading" do
it "reloads cached data" do
expect { object.inline }.to change(klass, :count).by(1)
expect { object.inline(reload: true) }.to change(klass, :count).by(1)
expect { object.inline(reload: false) }.not_to change(klass, :count)
expect { object.inline }.not_to change(klass, :count)
end
end
end
context "with custom loader" do
it "works" do
expect(object.custom).to eq([object])
end
end
context "without preloading" do
it "returns right data" do
expect(object.inline).to eq([object])
end
it "caches data" do
expect { object.inline }.to change(klass, :count).by(1)
expect { object.inline }.not_to change(klass, :count)
end
end
context "with preloading" do
it "returns right data" do
N1Loader::Preloader.new(objects).preload(:inline)
expect(objects.first.inline).to eq([objects.first])
expect(objects.last.inline).to eq([objects.last])
end
it "lazy loads data" do
expect { N1Loader::Preloader.new(objects).preload(:inline) }.not_to change(klass, :count)
expect { objects.map(&:inline) }.to change(klass, :count).by(1)
end
it "uses preloaded data" do
N1Loader::Preloader.new(objects).preload(:inline)
expect { objects.map(&:inline) }.to change(klass, :count).by(1)
expect { objects.map(&:inline) }.not_to change(klass, :count)
end
end
end