File: lazy_attributes_spec.rb

package info (click to toggle)
ruby-sequel 5.41.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,548 kB
  • sloc: ruby: 104,241; makefile: 3
file content (273 lines) | stat: -rw-r--r-- 10,251 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
require_relative "spec_helper"
require 'yaml'

describe "Sequel::Plugins::LazyAttributes" do
  before do
    @db = Sequel.mock
    def @db.supports_schema_parsing?() true end
    def @db.schema(*a) [[:id, {:type=>:integer}], [:name,{:type=>:string}]] end
    class ::LazyAttributesModel < Sequel::Model(@db[:la])
      plugin :lazy_attributes
      set_columns([:id, :name])
      def self.columns; [:id, :name] end
      singleton_class.send(:alias_method, :columns, :columns)
      lazy_attributes :name
      def self.columns; [:id] end
      set_dataset dataset.with_fetch(proc do |sql|
        if sql !~ /WHERE/
          if sql =~ /name/
            [{:id=>1, :name=>'1'}, {:id=>2, :name=>'2'}]
          else
            [{:id=>1}, {:id=>2}]
          end
        else
          if sql =~ /id IN \(([\d, ]+)\)/
            $1.split(', ')
          elsif sql =~ /id = (\d)/
            [$1]
          end.map do |x|
            if sql =~ /SELECT (la.)?name FROM/
              {:name=>x.to_s}
            else
              {:id=>x.to_i, :name=>x.to_s}
            end
          end
        end
      end)
    end
    @c = ::LazyAttributesModel
    @ds = LazyAttributesModel.dataset
    @db.sqls
  end
  after do
    Object.send(:remove_const, :LazyAttributesModel)
  end
  
  it "should allowing adding additional lazy attributes via plugin :lazy_attributes" do
    @c.set_dataset(@ds.select(:id, :blah))
    @c.dataset.sql.must_equal 'SELECT id, blah FROM la'
    @c.plugin :lazy_attributes, :blah
    @c.dataset.sql.must_equal 'SELECT id FROM la'
  end
  
  it "should allowing adding additional lazy attributes via lazy_attributes" do
    @c.set_dataset(@ds.select(:id, :blah))
    @c.dataset.sql.must_equal 'SELECT id, blah FROM la'
    @c.lazy_attributes :blah
    @c.dataset.sql.must_equal 'SELECT id FROM la'
  end

  it "should handle lazy attributes that are qualified in the selection" do
    @c.set_dataset(@ds.select(Sequel[:la][:id], Sequel[:la][:blah]))
    @c.dataset.sql.must_equal 'SELECT la.id, la.blah FROM la'
    @c.plugin :lazy_attributes, :blah
    @c.dataset.sql.must_equal 'SELECT la.id FROM la'
  end
  
  with_symbol_splitting "should handle lazy attributes that are qualified in the selection using symbol splitting" do
    @c.set_dataset(@ds.select(:la__id, :la__blah))
    @c.dataset.sql.must_equal 'SELECT la.id, la.blah FROM la'
    @c.plugin :lazy_attributes, :blah
    @c.dataset.sql.must_equal 'SELECT la.id FROM la'
  end
  
  it "should remove the attributes given from the SELECT columns of the model's dataset" do
    @ds.sql.must_equal 'SELECT la.id FROM la'
  end

  it "should still typecast correctly in lazy loaded column setters" do
    m = @c.new
    m.name = 1
    m.name.must_equal '1'
  end

  it "should raise error if the model has no primary key" do
    m = @c.first
    @c.no_primary_key
    proc{m.name}.must_raise(Sequel::Error)
  end

  it "should lazily load the attribute for a single model object" do
    m = @c.first
    m.values.must_equal(:id=>1)
    m.name.must_equal '1'
    m.values.must_equal(:id=>1, :name=>'1')
    @db.sqls.must_equal ['SELECT la.id FROM la LIMIT 1', 'SELECT la.name FROM la WHERE (id = 1) LIMIT 1']
  end

  it "should lazily load the attribute for a frozen model object" do
    m = @c.first
    m.freeze
    m.name.must_equal '1'
    @db.sqls.must_equal ['SELECT la.id FROM la LIMIT 1', 'SELECT la.name FROM la WHERE (id = 1) LIMIT 1']
    m.name.must_equal '1'
    @db.sqls.must_equal ['SELECT la.name FROM la WHERE (id = 1) LIMIT 1']
  end

  it "should not lazily load the attribute for a single model object if the value already exists" do
    m = @c.first
    m.values.must_equal(:id=>1)
    m[:name] = '1'
    m.name.must_equal '1'
    m.values.must_equal(:id=>1, :name=>'1')
    @db.sqls.must_equal ['SELECT la.id FROM la LIMIT 1']
  end

  it "should not lazily load the attribute for a single model object if it is a new record" do
    m = @c.new
    m.values.must_equal({})
    m.name.must_be_nil
    @db.sqls.must_equal []
  end

  it "should eagerly load the attribute for all model objects reteived with it" do
    ms = @c.all
    ms.map{|m| m.values}.must_equal [{:id=>1}, {:id=>2}]
    ms.map{|m| m.name}.must_equal %w'1 2'
    ms.map{|m| m.values}.must_equal [{:id=>1, :name=>'1'}, {:id=>2, :name=>'2'}]
    @db.sqls.must_equal ['SELECT la.id FROM la', 'SELECT la.id, la.name FROM la WHERE (la.id IN (1, 2))']
  end

  it "should not have eager loading modify values of rows if it returns unexpected values" do
    @c.dataset = @c.dataset.with_fetch([{:id=>1}, {:id=>2}, {:id=>3}])
    @db.sqls
    ms = @c.all
    @db.sqls.must_equal ['SELECT la.id FROM la']
    ms.map{|m| m.values}.must_equal [{:id=>1}, {:id=>2}, {:id=>3}]
    ms[2].name = 'foo'

    @c.dataset = @c.dataset.with_fetch([{:id=>1, :name=>'b'}, {:id=>2, :name=>'ba'}, {:id=>3, :name=>'bar'}, {:id=>4, :name=>'bar2'}])
    @db.sqls
    ms.map{|m| m.name}.must_equal %w'b ba foo'
    ms.map{|m| m.values}.must_equal [{:id=>1, :name=>'b'}, {:id=>2, :name=>'ba'}, {:id=>3, :name=>'foo'}]
    @db.sqls.must_equal ['SELECT la.id, la.name FROM la WHERE (la.id IN (1, 2))']
  end

  it "should raise Error if trying to load a lazy attribute for a model without a primary key" do
    @c.no_primary_key
    m = @c.first
    @db.sqls.must_equal ["SELECT la.id FROM la LIMIT 1"]
    m.values.must_equal(:id=>1)
    proc{m.name}.must_raise Sequel::Error

    ms = @c.all
    @db.sqls.must_equal [ "SELECT la.id FROM la"]
    proc{ms[0].name}.must_raise Sequel::Error
    @db.sqls.must_equal []
  end

  it "should not eagerly load the attribute if model instance is frozen, and deal with other frozen instances if not frozen" do
    ms = @c.all
    ms.first.freeze
    ms.map{|m| m.name}.must_equal %w'1 2'
    @db.sqls.must_equal ['SELECT la.id FROM la', 'SELECT la.name FROM la WHERE (id = 1) LIMIT 1', 'SELECT la.id, la.name FROM la WHERE (la.id IN (2))']
  end

  it "should add the accessors to a module included in the class, so they can be easily overridden" do
    @c.class_eval do
      def name
        "#{super}-blah"
      end
    end
    ms = @c.all
    ms.map{|m| m.values}.must_equal [{:id=>1}, {:id=>2}]
    ms.map{|m| m.name}.must_equal %w'1-blah 2-blah'
    ms.map{|m| m.values}.must_equal [{:id=>1, :name=>'1'}, {:id=>2, :name=>'2'}]
    sqls = @db.sqls
    ['SELECT la.id, la.name FROM la WHERE (la.id IN (1, 2))',
     'SELECT la.id, la.name FROM la WHERE (la.id IN (2, 1))'].must_include(sqls.pop)
    sqls.must_equal ['SELECT la.id FROM la']
  end

  it "should work with the serialization plugin" do
    @c.plugin :serialization, :yaml, :name
    @ds = @c.dataset = @ds.with_fetch([[{:id=>1}, {:id=>2}], [{:id=>1, :name=>"--- 3\n"}, {:id=>2, :name=>"--- 6\n"}], [{:id=>1}], [{:name=>"--- 3\n"}]])
    ms = @ds.all
    ms.map{|m| m.values}.must_equal [{:id=>1}, {:id=>2}]
    ms.map{|m| m.name}.must_equal [3,6]
    ms.map{|m| m.values}.must_equal [{:id=>1, :name=>"--- 3\n"}, {:id=>2, :name=>"--- 6\n"}]
    ms.map{|m| m.deserialized_values}.must_equal [{:name=>3}, {:name=>6}]
    ms.map{|m| m.name}.must_equal [3,6]
    sqls = @db.sqls
    ['SELECT la.id, la.name FROM la WHERE (la.id IN (1, 2))',
     'SELECT la.id, la.name FROM la WHERE (la.id IN (2, 1))'].must_include(sqls.pop)
    sqls.must_equal ['SELECT la.id FROM la']
    m = @ds.first
    m.values.must_equal(:id=>1)
    m.name.must_equal 3
    m.values.must_equal(:id=>1, :name=>"--- 3\n")
    m.deserialized_values.must_equal(:name=>3)
    m.name.must_equal 3
    @db.sqls.must_equal ["SELECT la.id FROM la LIMIT 1", "SELECT la.name FROM la WHERE (id = 1) LIMIT 1"]
  end

  it "should not allow additional lazy attributes after freezing" do
    @c.plugin :lazy_attributes, :blah
    @c.freeze
    proc{@c.lazy_attributes :name}.must_raise RuntimeError, TypeError
  end
end

describe "Sequel::Plugins::LazyAttributes with composite keys" do
  before do
    @db = Sequel.mock
    def @db.supports_schema_parsing?() true end
    def @db.schema(*a) [[:id, {:type=>:integer}], [:id2, {:type=>:integer}], [:name,{:type=>:string}]] end
    class ::LazyAttributesModel < Sequel::Model(@db[:la])
      plugin :lazy_attributes
      set_columns([:id, :id2, :name])
      def self.columns; [:id, :id2, :name] end
      lazy_attributes :name
      singleton_class.send(:alias_method, :columns, :columns)
      def self.columns; [:id, :id2] end
      set_primary_key [:id, :id2]
      set_dataset dataset.with_fetch(proc do |sql|
        if sql !~ /WHERE/
          if sql =~ /name/
            [{:id=>1, :id2=>2, :name=>'1'}, {:id=>1, :id2=>3, :name=>'2'}]
          else
            [{:id=>1, :id2=>2}, {:id=>1, :id2=>3}]
          end
        else
          case sql
          when /\((?:la.)?id, (?:la.)?id2\) IN \(((?:\(\d, \d\)(?:, )?)+)/
            $1.gsub(/\D/, '|').split('|').delete_if(&:empty?).each_slice(2)
          when /id = (\d)\) AND \(id2 = (\d)/
            [[$1, $2]]
          when /id = (\d), id2 = (\d)/
            [[$1, $2]]
          end.map do |x, y|
            if sql =~ /SELECT (la.)?name FROM/
              {:name=>"#{x}-#{y}"}
            else
              {:id=>x.to_i, :id2=>y.to_i, :name=>"#{x}-#{y}"}
            end
          end
        end
      end)
    end
    @c = ::LazyAttributesModel
    @ds = LazyAttributesModel.dataset
    @db.sqls
  end
  after do
    Object.send(:remove_const, :LazyAttributesModel)
  end

  it "should lazily load the attribute for a single model object" do
    m = @c.first
    m.values.must_equal(:id=>1, :id2=>2)
    m.name.must_equal '1-2'
    m.values.must_equal(:id=>1, :id2=>2, :name=>'1-2')
    @db.sqls.must_equal ["SELECT la.id, la.id2 FROM la LIMIT 1", "SELECT la.name FROM la WHERE ((id = 1) AND (id2 = 2)) LIMIT 1"]
  end

  it "should eagerly load the attribute for all model objects reteived with it" do
    ms = @c.all
    ms.map{|m| m.values}.must_equal [{:id=>1, :id2=>2}, {:id=>1, :id2=>3}]
    @db.sqls.must_equal ["SELECT la.id, la.id2 FROM la"]
    ms.map{|m| m.name}.must_equal %w'1-2 1-3'
    ms.map{|m| m.values}.must_equal [{:id=>1, :id2=>2, :name=>'1-2'}, {:id=>1, :id2=>3, :name=>'1-3'}]
    @db.sqls.must_equal ["SELECT la.id, la.id2, la.name FROM la WHERE ((la.id, la.id2) IN ((1, 2), (1, 3)))"]
  end
end