File: forbid_lazy_load_spec.rb

package info (click to toggle)
ruby-sequel 5.97.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,188 kB
  • sloc: ruby: 123,115; makefile: 3
file content (379 lines) | stat: -rw-r--r-- 15,466 bytes parent folder | download
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
require_relative "spec_helper"

describe "forbid_lazy_load plugin" do
  # opts are forwarded to `plugin :forbid_lazy_load`
  def setup_model(opts={})
    @c = Class.new(Sequel::Model)
    @c.set_dataset Sequel::Model.db[:ts].with_fetch({:id=>2, :t_id=>3})
    @c.plugin :forbid_lazy_load, opts
    @c.columns :id, :t_id
    @c.many_to_one :t, :class=>@c, :key=>:t_id
    @c.one_to_many :ts, :class=>@c, :key=>:t_id
    @c.many_to_many :mtm_ts, :class=>@c, :join_table=>:ts, :left_key=>:id, :right_key=>:t_id
    @c.one_to_one :otoo_t, :class=>@c, :key=>:t_id
    @c.one_through_one :oto_t, :class=>@c, :join_table=>:ts, :left_key=>:id, :right_key=>:t_id
    @o1 = @c.load(:id=>1, :t_id=>2)
    @o2 = @c.load(:id=>2, :t_id=>3)
  end

  model_instance_specs = Module.new do
    extend Minitest::Spec::DSL

    it "should not forbid lazy load if not set at instance level" do
      @o1.t.must_equal @o2
      @o1.ts.must_equal [@o2]
      @o1.mtm_ts.must_equal [@o2]
      @o1.otoo_t.must_equal @o2
      @o1.oto_t.must_equal @o2
    end

    it "should forbid lazy load when using :forbid_lazy_load true association method option" do
      proc{@o1.t(:forbid_lazy_load=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.ts(:forbid_lazy_load=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.mtm_ts(:forbid_lazy_load=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.otoo_t(:forbid_lazy_load=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.oto_t(:forbid_lazy_load=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
    end

    it "should forbid lazy load if set at instance level" do
      @o1.forbid_lazy_load.must_be_same_as @o1
      proc{@o1.t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.mtm_ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.oto_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
    end

    it "should allow lazy load for instance if set at instance level" do
      o = @c.all.first
      o.allow_lazy_load.must_be_same_as o
      o.t.must_equal @o2
      o.ts.must_equal [@o2]
      o.mtm_ts.must_equal [@o2]
      o.otoo_t.must_equal @o2
      o.oto_t.must_equal @o2
    end
  end

  describe "default options" do
    include model_instance_specs

    before do
      setup_model
    end

    it "should forbid lazy load if retrieved by dataset via Dataset#all" do
      o = @c.all.first
      proc{o.t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{o.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{o.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{o.mtm_ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{o.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{o.oto_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
    end

    it "should forbid lazy load if retrieved by dataset via Dataset#each" do
      o = @c.each{|x| break x}
      proc{o.t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{o.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{o.mtm_ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{o.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{o.oto_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
    end

    it "should forbid lazy load if retrieved by dataset via Dataset#where_each" do
      5.times do
        o = @c.where_each(:id=>1){|x| break x}
        proc{o.t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.mtm_ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.oto_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      end
    end

    it "should forbid lazy load if retrieved by dataset via Dataset#first with integer argument" do
      5.times do
        o = @c.first(2)[0]
        proc{o.t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.mtm_ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.oto_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error

        o = @c.first(2){id > 0}[0]
        proc{o.t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.mtm_ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.oto_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      end
    end

    it "should not forbid lazy load if retrieved by dataset via Dataset#first with no arguments" do
      5.times do
        o = @c.first
        o.t.must_equal @o2
        o.ts.must_equal [@o2]
        o.mtm_ts.must_equal [@o2]
        o.otoo_t.must_equal @o2
        o.oto_t.must_equal @o2
      end
    end

    it "should not forbid lazy load if retrieved by dataset via Dataset#first with hash argument" do
      5.times do
        o = @c.first(id: 2)
        o.t.must_equal @o2
        o.ts.must_equal [@o2]
        o.mtm_ts.must_equal [@o2]
        o.otoo_t.must_equal @o2
        o.oto_t.must_equal @o2
      end
    end

    it "should not forbid lazy load if retrieved by dataset via Dataset#first with block" do
      5.times do
        o = @c.first{id > 1}
        o.t.must_equal @o2
        o.ts.must_equal [@o2]
        o.mtm_ts.must_equal [@o2]
        o.otoo_t.must_equal @o2
        o.oto_t.must_equal @o2
      end
    end

    it "should not forbid lazy load if retrieved by dataset via Dataset#with_pk" do
      5.times do
        o = @c.dataset.with_pk(1)
        o.t.must_equal @o2
        o.ts.must_equal [@o2]
        o.mtm_ts.must_equal [@o2]
        o.otoo_t.must_equal @o2
        o.oto_t.must_equal @o2
      end
    end

    it "should not forbid lazy load for associated objects returned by singular associations" do
      [@o1.t, @o1.otoo_t, @o1.oto_t].each do |o|
        o.associations.clear
        o.t.must_equal @o2
        o.ts.must_equal [@o2]
        o.mtm_ts.must_equal [@o2]
        o.otoo_t.must_equal @o2
        o.oto_t.must_equal @o2
      end
    end

    it "should forbid lazy load for associated objects returned by plural associations" do
      [@o1.ts, @o1.mtm_ts].each do |os|
        o = os.first
        o.associations.clear
        proc{o.t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.mtm_ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
        proc{o.oto_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      end
    end

    it "should allow association access if cached even if forbidding lazy loading" do
      @o1.t.must_equal @o2
      @o1.ts.must_equal [@o2]
      @o1.mtm_ts.must_equal [@o2]
      @o1.otoo_t.must_equal @o2
      @o1.oto_t.must_equal @o2

      @o1.forbid_lazy_load

      @o1.t.must_equal @o2
      @o1.ts.must_equal [@o2]
      @o1.mtm_ts.must_equal [@o2]
      @o1.otoo_t.must_equal @o2
      @o1.oto_t.must_equal @o2
    end

    it "should forbid lazy load for associations if forbid_lazy_load true association option is used" do
      @c.many_to_one :t, :class=>@c, :key=>:t_id, :forbid_lazy_load=>true
      @c.one_to_many :ts, :class=>@c, :key=>:t_id, :forbid_lazy_load=>true
      @c.many_to_many :mtm_ts, :class=>@c, :join_table=>:ts, :left_key=>:id, :right_key=>:t_id, :forbid_lazy_load=>true
      @c.one_to_one :otoo_t, :class=>@c, :key=>:t_id, :forbid_lazy_load=>true
      @c.one_through_one :oto_t, :class=>@c, :join_table=>:ts, :left_key=>:id, :right_key=>:t_id, :forbid_lazy_load=>true

      proc{@o1.t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.mtm_ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.oto_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
    end

    it "should allow lazy load for associations even if instances have it forbidden if forbid_lazy_load false association option is used" do
      @c.many_to_one :t, :class=>@c, :key=>:t_id, :forbid_lazy_load=>false
      @c.one_to_many :ts, :class=>@c, :key=>:t_id, :forbid_lazy_load=>false
      @c.many_to_many :mtm_ts, :class=>@c, :join_table=>:ts, :left_key=>:id, :right_key=>:t_id, :forbid_lazy_load=>false
      @c.one_to_one :otoo_t, :class=>@c, :key=>:t_id, :forbid_lazy_load=>false
      @c.one_through_one :oto_t, :class=>@c, :join_table=>:ts, :left_key=>:id, :right_key=>:t_id, :forbid_lazy_load=>false

      o = @c.all.first
      o.t.must_equal @o2
      o.ts.must_equal [@o2]
      o.mtm_ts.must_equal [@o2]
      o.otoo_t.must_equal @o2
      o.oto_t.must_equal @o2
    end

    it "should forbid lazy load after finalizing associations if not using static_cache in associated class" do
      @c.finalize_associations
      @o1.forbid_lazy_load

      proc{@o1.t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.mtm_ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.otoo_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.oto_t}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
    end

    it "should set forbid_lazy_load false association option if using static_cache in associated class and using allow_lazy_load_for_static_cache_associations" do
      @c.plugin :static_cache
      @c.allow_lazy_load_for_static_cache_associations
      @o1.forbid_lazy_load

      @o1.t.must_equal @o2
      @o1.ts.must_equal [@o2]
      @o1.mtm_ts.must_equal [@o2]
      @o1.otoo_t.must_equal @o2
      @o1.oto_t.must_equal @o2
    end

    it "should automatically set forbid_lazy_load false association option when finalizing associations if using static_cache in associated class" do
      @c.plugin :static_cache
      @c.finalize_associations
      @o1.forbid_lazy_load

      @o1.t.must_equal @o2
      @o1.ts.must_equal [@o2]
      @o1.mtm_ts.must_equal [@o2]
      @o1.otoo_t.must_equal @o2
      @o1.oto_t.must_equal @o2
    end

    it "should allow lazy load when forbidden when using :reload association method option" do
      @o1.forbid_lazy_load
      @o1.t(:reload=>true).must_equal @o2
      @o1.ts(:reload=>true).must_equal [@o2]
      @o1.mtm_ts(:reload=>true).must_equal [@o2]
      @o1.otoo_t(:reload=>true).must_equal @o2
      @o1.oto_t(:reload=>true).must_equal @o2
    end

    it "should work correctly if loading an associated object for a class that does not use the forbid_lazy_load plugin" do
      c = Class.new(Sequel::Model)
      c.set_dataset Sequel::Model.db[:ts].with_fetch({:id=>2, :t_id=>3})
      @c.one_to_one :otoo_t, :class=>c, :key=>:t_id
      @o1.otoo_t.must_equal c.load(@o2.values)
    end

    it "should not allow lazy load for associations to static cache models not using forbid_lazy_load plugin" do
      c = Class.new(Sequel::Model)
      c.set_dataset Sequel::Model.db[:ts].with_fetch({:id=>2, :t_id=>3})
      @c.one_to_many :ts, :class=>c, :key=>:t_id
      @c.plugin :static_cache
      @c.finalize_associations
      @o1.forbid_lazy_load

      @o1.t.must_equal @o2
      proc{@o1.ts}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      @o1.mtm_ts.must_equal [@o2]
      @o1.otoo_t.must_equal @o2
      @o1.oto_t.must_equal @o2
    end

    it "should allow lazy load when forbidden when using :forbid_lazy_load false association option" do
      @o1.forbid_lazy_load
      @o1.t(:forbid_lazy_load=>false).must_equal @o2
      @o1.ts(:forbid_lazy_load=>false).must_equal [@o2]
      @o1.mtm_ts(:forbid_lazy_load=>false).must_equal [@o2]
      @o1.otoo_t(:forbid_lazy_load=>false).must_equal @o2
      @o1.oto_t(:forbid_lazy_load=>false).must_equal @o2
    end

    it "should forbid lazy load when using :forbid_lazy_load true association method option even when using :reload association method option" do
      proc{@o1.t(:forbid_lazy_load=>true, :reload=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.ts(:forbid_lazy_load=>true, :reload=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.mtm_ts(:forbid_lazy_load=>true, :reload=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.otoo_t(:forbid_lazy_load=>true, :reload=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
      proc{@o1.oto_t(:forbid_lazy_load=>true, :reload=>true)}.must_raise Sequel::Plugins::ForbidLazyLoad::Error
    end

    it "should not effect naked datasets" do
      @c.naked.all.must_equal [{:id=>2, :t_id=>3}]
      @c.naked.where_each(:id=>1){|x| break x}.must_equal(:id=>2, :t_id=>3)
    end

    it "should handle datasets without row_procs" do
      ds = @c.naked
      ds.all.first.must_equal(:id=>2, :t_id=>3)
      ds.each{|x| break x}.must_equal(:id=>2, :t_id=>3)
      ds.where_each(:id=>1){|x| break x}.must_equal(:id=>2, :t_id=>3)
      ds.first(2)[0].must_equal(:id=>2, :t_id=>3)
      ds.first(2){id > 0}[0].must_equal(:id=>2, :t_id=>3)
      ds.first.must_equal(:id=>2, :t_id=>3)
      ds.first{id > 1}.must_equal(:id=>2, :t_id=>3)
      ds.first(:id=>2).must_equal(:id=>2, :t_id=>3)
      ds.with_pk(1).must_equal(:id=>2, :t_id=>3)
    end

    it "should handle datasets with row_procs different from the model" do
      ds = @c.dataset.with_row_proc(proc{|x| x})
      ds.all.first.must_equal(:id=>2, :t_id=>3)
      ds.each{|x| break x}.must_equal(:id=>2, :t_id=>3)
      ds.where_each(:id=>1){|x| break x}.must_equal(:id=>2, :t_id=>3)
      ds.first(2)[0].must_equal(:id=>2, :t_id=>3)
      ds.first(2){id > 0}[0].must_equal(:id=>2, :t_id=>3)
      ds.first.must_equal(:id=>2, :t_id=>3)
      ds.first{id > 1}.must_equal(:id=>2, :t_id=>3)
      ds.first(:id=>2).must_equal(:id=>2, :t_id=>3)
      ds.with_pk(1).must_equal(:id=>2, :t_id=>3)
    end
  end

  describe "allow_by_default option" do
    before do
      setup_model(allow_by_default: true)
    end

    include model_instance_specs

    it "should allow lazy load if retrieved by dataset via Dataset#all" do
      o = @c.all.first
      o.allow_lazy_load.must_be_same_as o
      o.t.must_equal @o2
      o.ts.must_equal [@o2]
      o.mtm_ts.must_equal [@o2]
      o.otoo_t.must_equal @o2
      o.oto_t.must_equal @o2
    end

    it "should allow lazy load if retrieved by dataset via Dataset#each" do
      o = @c.each{|x| break x}
      o.allow_lazy_load.must_be_same_as o
      o.t.must_equal @o2
      o.ts.must_equal [@o2]
      o.mtm_ts.must_equal [@o2]
      o.otoo_t.must_equal @o2
      o.oto_t.must_equal @o2
    end

    it "should allow lazy load if retrieved by dataset via Dataset#where_each" do
      5.times do
        o = @c.where_each(:id=>1){|x| break x}
        o.allow_lazy_load.must_be_same_as o
        o.t.must_equal @o2
        o.ts.must_equal [@o2]
        o.mtm_ts.must_equal [@o2]
        o.otoo_t.must_equal @o2
        o.oto_t.must_equal @o2
      end
    end
  end
end