-
Notifications
You must be signed in to change notification settings - Fork 29
/
query_spec.rb
506 lines (412 loc) · 17.3 KB
/
query_spec.rb
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
if ADAPTER
module QuerySpec
class SailBoat
include DataMapper::Resource
property :id, Serial
property :name, String
property :port, String
property :captain, String
def self.default_repository_name
ADAPTER
end
end
class Permission
include DataMapper::Resource
property :id, Serial
property :user_id, Integer
property :resource_id, Integer
property :resource_type, String
property :token, String
def self.default_repository_name
ADAPTER
end
end
class Region
include DataMapper::Resource
property :id, Serial
property :name, String
property :type, String
def self.default_repository_name
ADAPTER
end
end
class Factory
include DataMapper::Resource
property :id, Serial
property :region_id, Integer
property :name, String
repository(:mock) do
property :land, String
end
belongs_to :region
def self.default_repository_name
ADAPTER
end
end
class Vehicle
include DataMapper::Resource
property :id, Serial
property :factory_id, Integer
property :name, String
belongs_to :factory
def self.default_repository_name
ADAPTER
end
end
class Group
include DataMapper::Resource
property :id, Serial
property :name, String
end
end
module Namespace
class Region
include DataMapper::Resource
property :id, Serial
property :name, String
def self.default_repository_name
ADAPTER
end
end
class Factory
include DataMapper::Resource
property :id, Serial
property :region_id, Integer
property :name, String
repository(:mock) do
property :land, String
end
belongs_to :region
def self.default_repository_name
ADAPTER
end
end
class Vehicle
include DataMapper::Resource
property :id, Serial
property :factory_id, Integer
property :name, String
belongs_to :factory
def self.default_repository_name
ADAPTER
end
end
end
describe DataMapper::Query, "with #{ADAPTER}" do
describe '#unique' do
include LoggingHelper
before(:each) do
QuerySpec::SailBoat.auto_migrate!
QuerySpec::SailBoat.create(:name => 'A', :port => 'C')
QuerySpec::SailBoat.create(:name => 'B', :port => 'B')
QuerySpec::SailBoat.create(:name => 'C', :port => 'A')
end
def parse_statement(log)
log.readlines.join.chomp.split(' ~ ').last
end
describe 'when true' do
if [ :postgres, :sqlite3, :mysql ].include?(ADAPTER)
it 'should add a GROUP BY to the SQL query' do
logger do |log|
QuerySpec::SailBoat.all(:unique => true, :fields => [ :id ]).to_a
case ADAPTER
when :postgres, :sqlite3
parse_statement(log).should == 'SELECT "id" FROM "query_spec_sail_boats" GROUP BY "id" ORDER BY "id"'
when :mysql
parse_statement(log).should == 'SELECT `id` FROM `query_spec_sail_boats` GROUP BY `id` ORDER BY `id`'
end
end
end
it 'should not add a GROUP BY to the SQL query if no field is a Property' do
operator = DataMapper::Query::Operator.new(:thing, :test)
# make the operator act like a Property
class << operator
property = QuerySpec::SailBoat.properties[:id]
(property.methods - (public_instance_methods - %w[ type ])).each do |method|
define_method(method) do |*args|
property.send(method, *args)
end
end
end
operator.should_not be_kind_of(DataMapper::Property)
logger do |log|
QuerySpec::SailBoat.all(:unique => true, :fields => [ operator ]).to_a
case ADAPTER
when :postgres, :sqlite3
parse_statement(log).should == 'SELECT "id" FROM "query_spec_sail_boats" ORDER BY "id"'
when :mysql
parse_statement(log).should == 'SELECT `id` FROM `query_spec_sail_boats` ORDER BY `id`'
end
end
end
end
end
describe 'when false' do
if [ :postgres, :sqlite3, :mysql ].include?(ADAPTER)
it 'should not add a GROUP BY to the SQL query' do
logger do |log|
QuerySpec::SailBoat.all(:unique => false, :fields => [ :id ]).to_a
case ADAPTER
when :postgres, :sqlite3
parse_statement(log).should == 'SELECT "id" FROM "query_spec_sail_boats" ORDER BY "id"'
when :mysql
parse_statement(log).should == 'SELECT `id` FROM `query_spec_sail_boats` ORDER BY `id`'
end
end
end
end
end
end
describe 'when ordering' do
before(:each) do
QuerySpec::SailBoat.auto_migrate!
QuerySpec::SailBoat.create(:name => 'A', :port => 'C')
QuerySpec::SailBoat.create(:name => 'B', :port => 'B')
QuerySpec::SailBoat.create(:name => 'C', :port => 'A')
end
it "should find by conditions" do
lambda do
repository(ADAPTER) do
QuerySpec::SailBoat.first(:conditions => [ 'name = ?', 'B' ])
end
end.should_not raise_error
lambda do
repository(ADAPTER) do
QuerySpec::SailBoat.first(:conditions => [ 'name = ?', 'A' ])
end
end.should_not raise_error
end
it "should find by conditions passed in as hash" do
repository(ADAPTER) do
QuerySpec::SailBoat.create(:name => "couldbe@email.com", :port => 'wee')
find = QuerySpec::SailBoat.first(:name => 'couldbe@email.com')
find.name.should == 'couldbe@email.com'
find = QuerySpec::SailBoat.first(:name => 'couldbe@email.com', :port.not => nil)
find.should_not be_nil
find.port.should_not be_nil
find.name.should == 'couldbe@email.com'
end
end
it "should find by conditions passed in a range" do
repository(ADAPTER) do
find = QuerySpec::SailBoat.all(:id => 0..2)
find.should_not be_nil
find.should have(2).entries
find = QuerySpec::SailBoat.all(:id.not => 0..2)
find.should have(1).entries
end
end
it "should find by conditions passed in as an array" do
repository(ADAPTER) do
find = QuerySpec::SailBoat.all(:id => [1,2])
find.should_not be_nil
find.should have(2).entries
find = QuerySpec::SailBoat.all(:id.not => [1,2])
find.should have(1).entries
end
end
describe "conditions passed in as an empty array" do
it "should work when id is an empty Array" do
repository(ADAPTER) do
find = QuerySpec::SailBoat.all(:id => [])
find.should have(0).entries
end
end
it "should work when id is NOT an empty Array" do
repository(ADAPTER) do
find = QuerySpec::SailBoat.all(:id.not => [])
find.should have(3).entries
end
end
it "should work when id is an empty Array and other conditions are specified" do
repository(ADAPTER) do
find = QuerySpec::SailBoat.all(:id => [], :name => "A")
find.should have(0).entries
end
end
it "should work when id is NOT an empty Array and other conditions are specified" do
repository(ADAPTER) do
find = QuerySpec::SailBoat.all(:id.not => [], :name => "A")
find.should have(1).entries
end
end
it "should work when id is NOT an empty Array and other Array conditions are specified" do
repository(ADAPTER) do
find = QuerySpec::SailBoat.all(:id.not => [], :name => ["A", "B"])
find.should have(2).entries
end
end
end
it "should order results" do
repository(ADAPTER) do
result = QuerySpec::SailBoat.all(:order => [
DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:name], :asc)
])
result[0].id.should == 1
result = QuerySpec::SailBoat.all(:order => [
DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:port], :asc)
])
result[0].id.should == 3
result = QuerySpec::SailBoat.all(:order => [
DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:name], :asc),
DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:port], :asc)
])
result[0].id.should == 1
result = QuerySpec::SailBoat.all(:order => [
QuerySpec::SailBoat.properties[:name],
DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:port], :asc)
])
result[0].id.should == 1
result = QuerySpec::SailBoat.all(:order => [ :name ])
result[0].id.should == 1
result = QuerySpec::SailBoat.all(:order => [ :name.desc ])
result[0].id.should == 3
end
end
end
describe 'when sub-selecting' do
before(:each) do
[ QuerySpec::SailBoat, QuerySpec::Permission ].each { |m| m.auto_migrate! }
QuerySpec::SailBoat.create(:id => 1, :name => "Fantasy I", :port => "Cape Town", :captain => 'Joe')
QuerySpec::SailBoat.create(:id => 2, :name => "Royal Flush II", :port => "Cape Town", :captain => 'James')
QuerySpec::SailBoat.create(:id => 3, :name => "Infringer III", :port => "Cape Town", :captain => 'Jason')
#User 1 permission -- read boat 1 & 2
QuerySpec::Permission.create(:id => 1, :user_id => 1, :resource_id => 1, :resource_type => 'SailBoat', :token => 'READ')
QuerySpec::Permission.create(:id => 2, :user_id => 1, :resource_id => 2, :resource_type => 'SailBoat', :token => 'READ')
#User 2 permission -- read boat 2 & 3
QuerySpec::Permission.create(:id => 3, :user_id => 2, :resource_id => 2, :resource_type => 'SailBoat', :token => 'READ')
QuerySpec::Permission.create(:id => 4, :user_id => 2, :resource_id => 3, :resource_type => 'SailBoat', :token => 'READ')
end
it 'should accept a DM::Query as a value of a condition' do
# User 1
acl = DataMapper::Query.new(repository(ADAPTER), QuerySpec::Permission, :user_id => 1, :resource_type => 'SailBoat', :token => 'READ', :fields => [ :resource_id ])
query = { :port => 'Cape Town', :id => acl, :captain.like => 'J%', :order => [ :id ] }
boats = repository(ADAPTER) { QuerySpec::SailBoat.all(query) }
boats.should have(2).entries
boats.entries[0].id.should == 1
boats.entries[1].id.should == 2
# User 2
acl = DataMapper::Query.new(repository(ADAPTER), QuerySpec::Permission, :user_id => 2, :resource_type => 'SailBoat', :token => 'READ', :fields => [ :resource_id ])
query = { :port => 'Cape Town', :id => acl, :captain.like => 'J%', :order => [ :id ] }
boats = repository(ADAPTER) { QuerySpec::SailBoat.all(query) }
boats.should have(2).entries
boats.entries[0].id.should == 2
boats.entries[1].id.should == 3
end
it 'when value is NOT IN another query' do
# Boats that User 1 Cannot see
acl = DataMapper::Query.new(repository(ADAPTER), QuerySpec::Permission, :user_id => 1, :resource_type => 'SailBoat', :token => 'READ', :fields => [ :resource_id ])
query = { :port => 'Cape Town', :id.not => acl, :captain.like => 'J%' }
boats = repository(ADAPTER) { QuerySpec::SailBoat.all(query) }
boats.should have(1).entries
boats.entries[0].id.should == 3
end
end # describe sub-selecting
describe 'when linking associated objects' do
before(:each) do
[ QuerySpec::Region, QuerySpec::Factory, QuerySpec::Vehicle ].each { |m| m.auto_migrate! }
QuerySpec::Region.create(:id => 1, :name => 'North West', :type => 'commercial')
QuerySpec::Factory.create(:id => 2000, :region_id => 1, :name => 'North West Plant')
QuerySpec::Vehicle.create(:id => 1, :factory_id => 2000, :name => '10 ton delivery truck')
Namespace::Region.auto_migrate!
Namespace::Factory.auto_migrate!
Namespace::Vehicle.auto_migrate!
Namespace::Region.create(:id => 1, :name => 'North West')
Namespace::Factory.create(:id => 2000, :region_id => 1, :name => 'North West Plant')
Namespace::Vehicle.create(:id => 1, :factory_id => 2000, :name => '10 ton delivery truck')
end
it 'should require that all properties in :fields and all :links come from the same repository' #do
# land = QuerySpec::Factory.properties(:mock)[:land]
# fields = []
# QuerySpec::Vehicle.properties(ADAPTER).map do |property|
# fields << property
# end
# fields << land
#
# lambda{
# begin
# results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ :factory ], :fields => fields) }
# rescue RuntimeError
# $!.message.should == "Property QuerySpec::Factory.land not available in repository #{ADAPTER}"
# raise $!
# end
# }.should raise_error(RuntimeError)
#end
it 'should accept a DM::Assoc::Relationship as a link' do
factory = DataMapper::Associations::Relationship.new(
:factory,
ADAPTER,
'QuerySpec::Vehicle',
'QuerySpec::Factory',
{ :child_key => [ :factory_id ], :parent_key => [ :id ] }
)
results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ factory ]) }
results.should have(1).entries
end
it 'should accept a symbol of an association name as a link' do
results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ :factory ]) }
results.should have(1).entries
end
it 'should accept a string of an association name as a link' do
results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ 'factory' ]) }
results.should have(1).entries
end
it 'should accept a mixture of items as a set of links' do
region = DataMapper::Associations::Relationship.new(
:region,
ADAPTER,
'QuerySpec::Factory',
'QuerySpec::Region',
{ :child_key => [ :region_id ], :parent_key => [ :id ] }
)
results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ 'factory', region ]) }
results.should have(1).entries
end
it 'should only accept a DM::Assoc::Relationship, String & Symbol as a link' do
lambda{
DataMapper::Query.new(repository(ADAPTER), QuerySpec::Vehicle, :links => [1])
}.should raise_error(ArgumentError)
end
it 'should have a association by the name of the Symbol or String' do
lambda{
DataMapper::Query.new(repository(ADAPTER), QuerySpec::Vehicle, :links => [ 'Sailing' ])
}.should raise_error(ArgumentError)
lambda{
DataMapper::Query.new(repository(ADAPTER), QuerySpec::Vehicle, :links => [ :sailing ])
}.should raise_error(ArgumentError)
end
it 'should create an n-level query path' do
QuerySpec::Vehicle.factory.region.model.should == QuerySpec::Region
QuerySpec::Vehicle.factory.region.name.property.should == QuerySpec::Region.properties(QuerySpec::Region.repository.name)[ :name ]
end
it 'should accept a DM::QueryPath as the key to a condition' do
vehicle = QuerySpec::Vehicle.first(QuerySpec::Vehicle.factory.region.name => 'North West')
vehicle.name.should == '10 ton delivery truck'
vehicle = Namespace::Vehicle.first(Namespace::Vehicle.factory.region.name => 'North West')
vehicle.name.should == '10 ton delivery truck'
end
it "should accept a string representing a DM::QueryPath as they key to a condition" do
vehicle = QuerySpec::Vehicle.first("factory.region.name" => 'North West')
vehicle.name.should == '10 ton delivery truck'
end
it "should accept 'id' and 'type' as endpoints on ah DM::QueryPath" do
vehicle = QuerySpec::Vehicle.first( QuerySpec::Vehicle.factory.region.type => 'commercial' )
vehicle.name.should == '10 ton delivery truck'
vehicle = QuerySpec::Vehicle.first( QuerySpec::Vehicle.factory.region.id => 1 )
vehicle.name.should == '10 ton delivery truck'
end
it 'should auto generate the link if a DM::Property from a different resource is in the :fields option'
it 'should create links with composite keys'
it 'should eager load associations' do
repository(ADAPTER) do
vehicle = QuerySpec::Vehicle.first(:includes => [ QuerySpec::Vehicle.factory ])
end
end
it "should behave when using mocks" do
QuerySpec::Group.should_receive(:all).with(:order => [ :id.asc ])
QuerySpec::Group.all(:order => [ :id.asc ])
end
end # describe links
end # DM::Query
end