public
Rubygem
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
commit  24c46588a0b88b6688bf871c086b9602d19f516c
tree    56a2241df3f8e6df61962e72c0e18aee8de68fdd
parent  5aa6314b6c588b25d19445002655e2cc41538a35
dm-core / spec / unit / query_spec.rb
100644 418 lines (345 sloc) 17.341 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
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
 
DataMapper.setup(:mock, "mock:///mock.db")
 
describe DataMapper::Query do
  GOOD_OPTIONS = [
    [ :reload, false ],
    [ :reload, true ],
    [ :offset, 0 ],
    [ :offset, 1 ],
    [ :limit, 1 ],
    [ :limit, 2 ],
    [ :order, [ DataMapper::Query::Direction.new(Article.properties[:created_at], :desc) ] ],
    [ :fields, Article.properties(:default).defaults.to_a ], # TODO: fill in allowed default value
    #[ :links, [ :stub ] ], # TODO: fill in allowed default value
    [ :includes, [ :stub ] ], # TODO: fill in allowed default value
  ]
 
  BAD_OPTIONS = {
    :reload => 'true',
    :offset => -1,
    :limit => 0,
    :order => [],
    :fields => [],
    :links => [],
    :includes => [],
    :conditions => [],
  }
 
  # flatten GOOD_OPTIONS into a Hash to remove default values, since
  # default value, when defined, is always listed first in GOOD_OPTIONS
  UPDATED_OPTIONS = GOOD_OPTIONS.inject({}) do |options,(attribute,value)|
    options.update attribute => value
  end
 
  UPDATED_OPTIONS.merge!({ :fields => [ :id, :author ]})
 
  describe '.new' do
    describe 'should set the attribute' do
      it '#model with model' do
        query = DataMapper::Query.new(repository(:mock), Article)
        query.model.should == Article
      end
 
      GOOD_OPTIONS.each do |(attribute,value)|
        it "##{attribute} with options[:#{attribute}] if it is #{value.inspect}" do
          query = DataMapper::Query.new(repository(:mock), Article, attribute => value)
          query.send(attribute).should == value
        end
      end
 
      describe ' #conditions with options[:conditions]' do
        it 'when they have a one element Array' do
          query = DataMapper::Query.new(repository(:mock), Article, :conditions => [ 'name = "dkubb"' ])
          query.conditions.should == [ [ 'name = "dkubb"' ] ]
        end
 
        it 'when they have a two or more element Array' do
          query = DataMapper::Query.new(repository(:mock), Article, :conditions => [ 'name = ?', 'dkubb' ])
          query.conditions.should == [ [ 'name = ?', [ 'dkubb' ] ] ]
          query.parameters.should == [ 'dkubb' ]
          
          query = DataMapper::Query.new(repository(:mock), Article, :conditions => [ 'name = ? OR age = ?', 'dkubb', 30 ], :limit => 1)
          query.conditions.should == [ [ 'name = ? OR age = ?', [ 'dkubb', 30 ] ] ]
          query.parameters.should == [ 'dkubb', 30 ]
        end
 
        it 'when they have another DM:Query as the value of sub-select' do
          class Acl
            include DataMapper::Resource
            property :id, Fixnum
            property :resource_id, Fixnum
          end
 
          acl_query = DataMapper::Query.new(repository(:mock), Acl, :fields=>[:resource_id]) #this would normally have conditions
          query = DataMapper::Query.new(repository(:mock), Article, :id.in => acl_query)
          query.conditions.each do |operator, property, value|
            operator.should == :in
            property.name.should == :id
            value.should == acl_query
          end
        end
      end
 
      describe ' #conditions with unknown options' do
        it 'when a Symbol object is a key' do
          query = DataMapper::Query.new(repository(:mock), Article, :author => 'dkubb')
          query.conditions.should == [ [ :eql, Article.properties[:author], 'dkubb' ] ]
        end
 
        it 'when a Symbol::Operator object is a key' do
          query = DataMapper::Query.new(repository(:mock), Article, :author.like => /\Ad(?:an\.)kubb\z/)
          query.conditions.should == [ [ :like, Article.properties[:author], /\Ad(?:an\.)kubb\z/ ] ]
        end
      end
    end
 
    describe 'should raise a TypeError' do
      it 'when repository is nil' do
        lambda {
          DataMapper::Query.new(nil, NormalClass)
        }.should raise_error(TypeError)
      end
 
      it 'when model is nil' do
        lambda {
          DataMapper::Query.new(repository(:mock), nil)
        }.should raise_error(TypeError)
      end
    end
 
    describe 'should raise an ArgumentError' do
      it 'when model is a Class that does not include DataMapper::Resource' do
        lambda {
          DataMapper::Query.new(repository(:mock), NormalClass)
        }.should raise_error(ArgumentError)
      end
 
      it 'when options is not a Hash' do
        lambda {
          DataMapper::Query.new(repository(:mock), Article, nil)
        }.should raise_error(ArgumentError)
      end
 
      BAD_OPTIONS.each do |attribute,value|
        it "when options[:#{attribute}] is nil" do
          lambda {
            DataMapper::Query.new(repository(:mock), Article, attribute => nil)
          }.should raise_error(ArgumentError)
        end
 
        it "when options[:#{attribute}] is #{value.kind_of?(Array) && value.empty? ? 'an empty Array' : value.inspect}" do
          lambda {
            DataMapper::Query.new(repository(:mock), Article, attribute => value)
          }.should raise_error(ArgumentError)
        end
      end
 
      it 'when unknown options use something that is not a Symbol::Operator, Symbol or String is a key' do
        lambda {
          DataMapper::Query.new(repository(:mock), Article, nil => nil)
        }.should raise_error(ArgumentError)
      end
    end
 
    describe 'should normalize' do
      it '#fields' do
        DataMapper::Query.new(repository(:mock), Article, :fields => [:id]).fields.should == Article.properties(:default).slice(:id).to_a
      end
    end
  end
 
  describe '#update' do
    before do
      @repository = repository(:mock)
      @query = DataMapper::Query.new(@repository, Article, UPDATED_OPTIONS)
    end
 
    it 'should instantiate a DataMapper::Query object from other when it is a Hash' do
      other = { :reload => :true }
 
      mock_query_class = mock('DataMapper::Query class')
      @query.should_receive(:class).with(no_args).ordered.and_return(mock_query_class)
      mock_query_class.should_receive(:new).with(@repository, @query.model, other).ordered.and_return(@query)
 
      @query.update(other)
    end
 
    it 'should return self' do
      other = DataMapper::Query.new(repository(:mock), Article)
      @query.update(other).should == @query
    end
 
    describe 'should overwrite the attribute' do
      it '#model with other model' do
        other = DataMapper::Query.new(repository(:mock), Comment)
        @query.update(other).model.should == Comment
      end
 
      it '#reload with other reload' do
        other = DataMapper::Query.new(repository(:mock), Article, :reload => true)
        @query.update(other).reload.should == true
      end
 
      it '#offset with other offset when it is not equal to 0' do
        other = DataMapper::Query.new(repository(:mock), Article, :offset => 1)
        @query.update(other).offset.should == 1
      end
 
      it '#limit with other limit when it is not nil' do
        other = DataMapper::Query.new(repository(:mock), Article, :limit => 1)
        @query.update(other).limit.should == 1
      end
 
      [ :eql, :like ].each do |operator|
        it "#conditions with other conditions when updating the '#{operator}' clause to a different value than in self" do
          # set the initial conditions
          @query.update(:author.send(operator) => 'ssmoot')
 
          # update the conditions, and overwrite with the new value
          other = DataMapper::Query.new(repository(:mock), Article, :author.send(operator) => 'dkubb')
          @query.update(other).conditions.should == [ [ operator, Article.properties[:author], 'dkubb' ] ]
        end
      end
 
      [ :gt, :gte ].each do |operator|
        it "#conditions with other conditions when updating the '#{operator}' clause to a value less than in self" do
          # set the initial conditions
          @query.update(:created_at.send(operator) => Time.at(1))
 
          # update the conditions, and overwrite with the new value is less
          other = DataMapper::Query.new(repository(:mock), Article, :created_at.send(operator) => Time.at(0))
          @query.update(other).conditions.should == [ [ operator, Article.properties[:created_at], Time.at(0) ] ]
        end
      end
 
      [ :lt, :lte ].each do |operator|
        it "#conditions with other conditions when updating the '#{operator}' clause to a value greater than in self" do
          # set the initial conditions
          @query.update(:created_at.send(operator) => Time.at(0))
 
          # update the conditions, and overwrite with the new value is more
          other = DataMapper::Query.new(repository(:mock), Article, :created_at.send(operator) => Time.at(1))
          @query.update(other).conditions.should == [ [ operator, Article.properties[:created_at], Time.at(1) ] ]
        end
      end
    end
 
    describe 'should append the attribute' do
      it "#order with other order unique values" do
        order = [
          DataMapper::Query::Direction.new(Article.properties[:created_at], :desc),
          DataMapper::Query::Direction.new(Article.properties[:author], :desc),
          DataMapper::Query::Direction.new(Article.properties[:title], :desc),
        ]
 
        other = DataMapper::Query.new(repository(:mock), Article, :order => order)
        @query.update(other).order.should == order
      end
      
      it "#order with a property that uses :field => something" do
        class Article
          property :plank, String, :field => 'real_plank'
        end
 
        query = DataMapper::Query.new(repository(:mock), Article, :order => [:plank.desc])
        
        query.order.first.property.should == Article.properties[:plank]
        query.order.first.property.field.should == 'real_plank'
        query.order.first.direction.should == :desc
        
        # this is the real test here, I can't find any other way to make it parse the order into sql than to run the whole query through
        repository(:mock).adapter.query_read_statement(query).should == %Q{SELECT "id", "blog_id", "created_at", "author", "title" FROM "articles" ORDER BY real_plank desc}
      end
 
      # dkubb: I am not sure i understand the intent here. link now needs to be
      # a DM::Assoc::Relationship or the name (Symbol or String) of an
      # association on the Resource -- thx guyvdb
      #
      # NOTE: I have commented out :links in the GOOD_OPTIONS above
      #
      [ :links, :includes ].each do |attribute|
        it "##{attribute} with other #{attribute} unique values" do
          pending 'DataMapper::Query::Path not ready'
          other = DataMapper::Query.new(repository(:mock), Article, attribute => [ :stub, :other, :new ])
          @query.update(other).send(attribute).should == [ :stub, :other, :new ]
        end
      end
 
      it "#fields with other fields unique values" do
        other = DataMapper::Query.new(repository(:mock), Article, :fields => [ :blog_id ])
        @query.update(other).fields.should == Article.properties(:default).slice(:id, :author, :blog_id).to_a
      end
 
      it '#conditions with other conditions when they are unique' do
        # set the initial conditions
        @query.update(:title => 'On DataMapper')
 
        # update the conditions, but merge the conditions together
        other = DataMapper::Query.new(repository(:mock), Article, :author => 'dkubb')
        @query.update(other).conditions.should == [ [ :eql, Article.properties[:title], 'On DataMapper' ], [ :eql, Article.properties[:author], 'dkubb' ] ]
      end
 
      [ :not, :in ].each do |operator|
        it "#conditions with other conditions when updating the '#{operator}' clause" do
          # set the initial conditions
          @query.update(:created_at.send(operator) => [ Time.at(0) ])
 
          # update the conditions, and overwrite with the new value is more
          other = DataMapper::Query.new(repository(:mock), Article, :created_at.send(operator) => [ Time.at(1) ])
          @query.update(other).conditions.should == [ [ operator, Article.properties[:created_at], [ Time.at(0), Time.at(1) ] ] ]
        end
      end
 
      it '#conditions with other conditions when they have a one element condition' do
        # set the initial conditions
        @query.update(:title => 'On DataMapper')
 
        # update the conditions, but merge the conditions together
        other = DataMapper::Query.new(repository(:mock), Article, :conditions => [ 'author = "dkubb"' ])
        @query.update(other).conditions.should == [ [ :eql, Article.properties[:title], 'On DataMapper' ], [ 'author = "dkubb"' ] ]
      end
 
      it '#conditions with other conditions when they have a two or more element condition' do
        # set the initial conditions
        @query.update(:title => 'On DataMapper')
 
        # update the conditions, but merge the conditions together
        other = DataMapper::Query.new(repository(:mock), Article, :conditions => [ 'author = ?', 'dkubb' ])
        @query.update(other).conditions.should == [ [ :eql, Article.properties[:title], 'On DataMapper' ], [ 'author = ?', [ 'dkubb' ] ] ]
      end
    end
 
    describe 'should not update the attribute' do
      it '#offset when other offset is equal to 0' do
        other = DataMapper::Query.new(repository(:mock), Article, :offset => 0)
        other.offset.should == 0
        @query.update(other).offset.should == 1
      end
 
      it '#limit when other limit is nil' do
        other = DataMapper::Query.new(repository(:mock), Article)
        other.limit.should be_nil
        @query.update(other).offset.should == 1
      end
 
      [ :gt, :gte ].each do |operator|
        it "#conditions with other conditions when they have a '#{operator}' clause with a value greater than in self" do
          # set the initial conditions
          @query.update(:created_at.send(operator) => Time.at(0))
 
          # do not overwrite with the new value if it is more
          other = DataMapper::Query.new(repository(:mock), Article, :created_at.send(operator) => Time.at(1))
          @query.update(other).conditions.should == [ [ operator, Article.properties[:created_at], Time.at(0) ] ]
        end
      end
 
      [ :lt, :lte ].each do |operator|
        it "#conditions with other conditions when they have a '#{operator}' clause with a value less than in self" do
          # set the initial conditions
          @query.update(:created_at.send(operator) => Time.at(1))
 
          # do not overwrite with the new value if it is less
          other = DataMapper::Query.new(repository(:mock), Article, :created_at.send(operator) => Time.at(0))
          @query.update(other).conditions.should == [ [ operator, Article.properties[:created_at], Time.at(1) ] ]
        end
      end
    end
  end
 
  describe '#merge' do
    before do
      @query = DataMapper::Query.new(repository(:mock), Article)
    end
 
    it 'should pass arguments as-is to duplicate object\'s #update method' do
      dupe_query = @query.dup
      @query.should_receive(:dup).with(no_args).ordered.and_return(dupe_query)
      dupe_query.should_receive(:update).with(:author => 'dkubb').ordered
      @query.merge(:author => 'dkubb')
    end
 
    it 'should return the duplicate object' do
      dupe_query = @query.merge(:author => 'dkubb')
      @query.object_id.should_not == dupe_query.object_id
      @query.merge(:author => 'dkubb').should == dupe_query
    end
  end
 
  describe '#==' do
    before do
      @query = DataMapper::Query.new(repository(:mock), Article)
    end
 
    describe 'should be equal' do
      it 'when other is same object' do
        @query.update(:author => 'dkubb').should == @query
      end
 
      it 'when other has the same attributes' do
        other = DataMapper::Query.new(repository(:mock), Article)
        @query.object_id.should_not == other.object_id
        @query.should == other
      end
 
      it 'when other has the same conditions sorted differently' do
        @query.update(:author => 'dkubb')
        @query.update(:title => 'On DataMapper')
 
        other = DataMapper::Query.new(repository(:mock), Article, :title => 'On DataMapper')
        other.update(:author => 'dkubb')
 
        # query conditions are in different order
        @query.conditions.should == [ [ :eql, Article.properties[:author], 'dkubb' ], [ :eql, Article.properties[:title], 'On DataMapper' ] ]
        other.conditions.should == [ [ :eql, Article.properties[:title], 'On DataMapper' ], [ :eql, Article.properties[:author], 'dkubb' ] ]
 
        @query.should == other
      end
    end