public
Rubygem
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
Ensure GROUP BY only happens with Query#fields contains a Property

* Added specs for GROUP BY change, and moved the classes to the top
  of the file and namespaced them to avoid colissions.
dkubb (author)
Tue Jul 15 15:40:12 -0700 2008
commit  f8bf592636e0684869e9f748254b6fedf54ff3ed
tree    46c7e572f420465ee24505610067a5f4672c68e4
parent  df51cd229213be3cbbcb434b9ffdd6f0493a2ea5
...
228
229
230
231
 
232
233
234
...
228
229
230
 
231
232
233
234
0
@@ -228,7 +228,7 @@ module DataMapper
0
           statement << " FROM #{quote_table_name(query.model.storage_name(query.repository.name))}"
0
           statement << links_statement(query) if query.links.any?
0
           statement << " WHERE #{conditions_statement(query)}" if query.conditions.any?
0
- statement << " GROUP BY #{group_by_statement(query)}" if query.unique?
0
+ statement << " GROUP BY #{group_by_statement(query)}" if query.unique? && query.fields.any? { |p| p.kind_of?(Property) }
0
           statement << " ORDER BY #{order_statement(query)}" if query.order.any?
0
           statement << " LIMIT #{quote_column_value(query.limit)}" if query.limit
0
           statement << " OFFSET #{quote_column_value(query.offset)}" if query.offset && query.offset > 0
...
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
...
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
...
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
...
287
288
289
290
291
 
 
292
293
294
 
295
296
297
298
299
 
300
301
302
303
304
 
305
306
307
...
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
...
347
348
349
350
 
351
352
353
...
357
358
359
360
 
361
362
363
364
365
366
367
368
369
370
371
372
 
 
373
374
375
...
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
...
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
...
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
...
357
358
359
 
 
360
361
362
363
 
364
365
366
367
368
 
369
370
371
372
373
 
374
375
376
377
...
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
...
417
418
419
 
420
421
422
423
...
427
428
429
 
430
431
432
433
434
 
 
 
 
 
 
 
 
435
436
437
438
439
0
@@ -1,49 +1,227 @@
0
 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
0
 
0
 if ADAPTER
0
+ module QuerySpec
0
+ class SailBoat
0
+ include DataMapper::Resource
0
+
0
+ property :id, Serial
0
+ property :name, String
0
+ property :port, String
0
+ property :captain, String
0
+
0
+ def self.default_repository_name
0
+ ADAPTER
0
+ end
0
+ end
0
+
0
+ class Permission
0
+ include DataMapper::Resource
0
+
0
+ property :id, Serial
0
+ property :user_id, Integer
0
+ property :resource_id, Integer
0
+ property :resource_type, String
0
+ property :token, String
0
+
0
+ def self.default_repository_name
0
+ ADAPTER
0
+ end
0
+ end
0
+
0
+ class Region
0
+ include DataMapper::Resource
0
+
0
+ property :id, Serial
0
+ property :name, String
0
+
0
+ def self.default_repository_name
0
+ ADAPTER
0
+ end
0
+ end
0
+
0
+ class Factory
0
+ include DataMapper::Resource
0
+
0
+ property :id, Serial
0
+ property :region_id, Integer
0
+ property :name, String
0
+
0
+ repository(:mock) do
0
+ property :land, String
0
+ end
0
+
0
+ belongs_to :region
0
+
0
+ def self.default_repository_name
0
+ ADAPTER
0
+ end
0
+ end
0
+
0
+ class Vehicle
0
+ include DataMapper::Resource
0
+
0
+ property :id, Serial
0
+ property :factory_id, Integer
0
+ property :name, String
0
+
0
+ belongs_to :factory
0
+
0
+ def self.default_repository_name
0
+ ADAPTER
0
+ end
0
+ end
0
+
0
+ class Group
0
+ include DataMapper::Resource
0
+ property :id, Serial
0
+ property :name, String
0
+ end
0
+ end
0
+
0
+ module Namespace
0
+ class Region
0
+ include DataMapper::Resource
0
+
0
+ property :id, Serial
0
+ property :name, String
0
+
0
+ def self.default_repository_name
0
+ ADAPTER
0
+ end
0
+ end
0
+
0
+ class Factory
0
+ include DataMapper::Resource
0
+
0
+ property :id, Serial
0
+ property :region_id, Integer
0
+ property :name, String
0
+
0
+ repository(:mock) do
0
+ property :land, String
0
+ end
0
+
0
+ belongs_to :region
0
+
0
+ def self.default_repository_name
0
+ ADAPTER
0
+ end
0
+ end
0
+
0
+ class Vehicle
0
+ include DataMapper::Resource
0
+ property :id, Serial
0
+ property :factory_id, Integer
0
+ property :name, String
0
+
0
+ belongs_to :factory
0
+
0
+ def self.default_repository_name
0
+ ADAPTER
0
+ end
0
+ end
0
+ end
0
+
0
   describe DataMapper::Query, "with #{ADAPTER}" do
0
- describe 'when ordering' do
0
- before :all do
0
- class SailBoat
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :name, String
0
- property :port, String
0
+ describe '#unique' do
0
+ include LoggingHelper
0
+
0
+ before do
0
+ QuerySpec::SailBoat.auto_migrate!
0
+
0
+ QuerySpec::SailBoat.create!(:name => 'A', :port => 'C')
0
+ QuerySpec::SailBoat.create!(:name => 'B', :port => 'B')
0
+ QuerySpec::SailBoat.create!(:name => 'C', :port => 'A')
0
+ end
0
+
0
+ def parse_statement(log)
0
+ log.readlines.join.chomp.split(' ~ ').last
0
+ end
0
+
0
+ describe 'when true' do
0
+ it 'should add a GROUP BY to the SQL query' do
0
+ logger do |log|
0
+ QuerySpec::SailBoat.all(:unique => true, :fields => [ :id ]).to_a
0
+ if ADAPTER == :mysql
0
+ parse_statement(log).should == 'SELECT `id` FROM `query_spec_sail_boats` GROUP BY `id` ORDER BY `id`'
0
+ else
0
+ parse_statement(log).should == 'SELECT "id" FROM "query_spec_sail_boats" GROUP BY "id" ORDER BY "id"'
0
+ end
0
+ end
0
+ end
0
+
0
+ it 'should not add a GROUP BY to the SQL query if no field is a Property' do
0
+ operator = DataMapper::Query::Operator.new(:thing, :test)
0
+
0
+ # make the operator act like a Property
0
+ class << operator
0
+ property = QuerySpec::SailBoat.properties[:id]
0
+ (property.methods - (public_instance_methods - %w[ type ])).each do |method|
0
+ define_method(method) do |*args|
0
+ property.send(method, *args)
0
+ end
0
+ end
0
+ end
0
+
0
+ operator.should_not be_kind_of(DataMapper::Property)
0
+
0
+ logger do |log|
0
+ QuerySpec::SailBoat.all(:unique => true, :fields => [ operator ]).to_a
0
+ if ADAPTER == :mysql
0
+ parse_statement(log).should == 'SELECT `id` FROM `query_spec_sail_boats` ORDER BY `id`'
0
+ else
0
+ parse_statement(log).should == 'SELECT "id" FROM "query_spec_sail_boats" ORDER BY "id"'
0
+ end
0
+ end
0
         end
0
       end
0
 
0
+ describe 'when false' do
0
+ it 'should not add a GROUP BY to the SQL query' do
0
+ logger do |log|
0
+ QuerySpec::SailBoat.all(:unique => false, :fields => [ :id ]).to_a
0
+ if ADAPTER == :mysql
0
+ parse_statement(log).should == 'SELECT `id` FROM `query_spec_sail_boats` ORDER BY `id`'
0
+ else
0
+ parse_statement(log).should == 'SELECT "id" FROM "query_spec_sail_boats" ORDER BY "id"'
0
+ end
0
+ end
0
+ end
0
+ end
0
+ end
0
+
0
+ describe 'when ordering' do
0
       before do
0
- SailBoat.auto_migrate!(ADAPTER)
0
+ QuerySpec::SailBoat.auto_migrate!
0
 
0
- repository(ADAPTER) do
0
- SailBoat.create!(:name => 'A', :port => 'C')
0
- SailBoat.create!(:name => 'B', :port => 'B')
0
- SailBoat.create!(:name => 'C', :port => 'A')
0
- end
0
+ QuerySpec::SailBoat.create!(:name => 'A', :port => 'C')
0
+ QuerySpec::SailBoat.create!(:name => 'B', :port => 'B')
0
+ QuerySpec::SailBoat.create!(:name => 'C', :port => 'A')
0
       end
0
 
0
       it "should find by conditions" do
0
         lambda do
0
           repository(ADAPTER) do
0
- SailBoat.first(:conditions => ['name = ?', 'B'])
0
+ QuerySpec::SailBoat.first(:conditions => [ 'name = ?', 'B' ])
0
           end
0
         end.should_not raise_error
0
 
0
         lambda do
0
           repository(ADAPTER) do
0
- SailBoat.first(:conditions => ['name = ?', 'A'])
0
+ QuerySpec::SailBoat.first(:conditions => [ 'name = ?', 'A' ])
0
           end
0
         end.should_not raise_error
0
       end
0
 
0
       it "should find by conditions passed in as hash" do
0
         repository(ADAPTER) do
0
- SailBoat.create!(:name => "couldbe@email.com", :port => 'wee')
0
+ QuerySpec::SailBoat.create!(:name => "couldbe@email.com", :port => 'wee')
0
 
0
- find = SailBoat.first(:name => 'couldbe@email.com')
0
+ find = QuerySpec::SailBoat.first(:name => 'couldbe@email.com')
0
           find.name.should == 'couldbe@email.com'
0
 
0
- find = SailBoat.first(:name => 'couldbe@email.com', :port.not => nil)
0
+ find = QuerySpec::SailBoat.first(:name => 'couldbe@email.com', :port.not => nil)
0
           find.should_not be_nil
0
           find.port.should_not be_nil
0
           find.name.should == 'couldbe@email.com'
0
@@ -52,100 +230,78 @@ if ADAPTER
0
 
0
       it "should find by conditions passed in a range" do
0
         repository(ADAPTER) do
0
- find = SailBoat.all(:id => 0..2)
0
+ find = QuerySpec::SailBoat.all(:id => 0..2)
0
           find.should_not be_nil
0
           find.should have(2).entries
0
 
0
- find = SailBoat.all(:id.not => 0..2)
0
+ find = QuerySpec::SailBoat.all(:id.not => 0..2)
0
           find.should have(1).entries
0
         end
0
       end
0
 
0
       it "should order results" do
0
         repository(ADAPTER) do
0
- result = SailBoat.all(:order => [
0
- DataMapper::Query::Direction.new(SailBoat.properties[:name], :asc)
0
+ result = QuerySpec::SailBoat.all(:order => [
0
+ DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:name], :asc)
0
           ])
0
           result[0].id.should == 1
0
 
0
- result = SailBoat.all(:order => [
0
- DataMapper::Query::Direction.new(SailBoat.properties[:port], :asc)
0
+ result = QuerySpec::SailBoat.all(:order => [
0
+ DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:port], :asc)
0
           ])
0
           result[0].id.should == 3
0
 
0
- result = SailBoat.all(:order => [
0
- DataMapper::Query::Direction.new(SailBoat.properties[:name], :asc),
0
- DataMapper::Query::Direction.new(SailBoat.properties[:port], :asc)
0
+ result = QuerySpec::SailBoat.all(:order => [
0
+ DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:name], :asc),
0
+ DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:port], :asc)
0
           ])
0
           result[0].id.should == 1
0
 
0
- result = SailBoat.all(:order => [
0
- SailBoat.properties[:name],
0
- DataMapper::Query::Direction.new(SailBoat.properties[:port], :asc)
0
+ result = QuerySpec::SailBoat.all(:order => [
0
+ QuerySpec::SailBoat.properties[:name],
0
+ DataMapper::Query::Direction.new(QuerySpec::SailBoat.properties[:port], :asc)
0
           ])
0
           result[0].id.should == 1
0
 
0
- result = SailBoat.all(:order => [:name])
0
+ result = QuerySpec::SailBoat.all(:order => [ :name ])
0
           result[0].id.should == 1
0
 
0
- result = SailBoat.all(:order => [:name.desc])
0
+ result = QuerySpec::SailBoat.all(:order => [ :name.desc ])
0
           result[0].id.should == 3
0
         end
0
       end
0
     end
0
 
0
     describe 'when sub-selecting' do
0
- before :all do
0
- class Permission
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :user_id, Integer
0
- property :resource_id, Integer
0
- property :resource_type, String
0
- property :token, String
0
- end
0
-
0
- class SailBoat
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :name, String
0
- property :port, String
0
- property :captain, String
0
- end
0
- end
0
-
0
       before do
0
- Permission.auto_migrate!(ADAPTER)
0
- SailBoat.auto_migrate!(ADAPTER)
0
+ [ QuerySpec::SailBoat, QuerySpec::Permission ].each { |m| m.auto_migrate! }
0
 
0
- repository(ADAPTER) do
0
- SailBoat.create!(:id => 1, :name => "Fantasy I", :port => "Cape Town", :captain => 'Joe')
0
- SailBoat.create!(:id => 2, :name => "Royal Flush II", :port => "Cape Town", :captain => 'James')
0
- SailBoat.create!(:id => 3, :name => "Infringer III", :port => "Cape Town", :captain => 'Jason')
0
+ QuerySpec::SailBoat.create!(:id => 1, :name => "Fantasy I", :port => "Cape Town", :captain => 'Joe')
0
+ QuerySpec::SailBoat.create!(:id => 2, :name => "Royal Flush II", :port => "Cape Town", :captain => 'James')
0
+ QuerySpec::SailBoat.create!(:id => 3, :name => "Infringer III", :port => "Cape Town", :captain => 'Jason')
0
 
0
- #User 1 permission -- read boat 1 & 2
0
- Permission.create!(:id => 1, :user_id => 1, :resource_id => 1, :resource_type => 'SailBoat', :token => 'READ')
0
- Permission.create!(:id => 2, :user_id => 1, :resource_id => 2, :resource_type => 'SailBoat', :token => 'READ')
0
+ #User 1 permission -- read boat 1 & 2
0
+ QuerySpec::Permission.create!(:id => 1, :user_id => 1, :resource_id => 1, :resource_type => 'SailBoat', :token => 'READ')
0
+ QuerySpec::Permission.create!(:id => 2, :user_id => 1, :resource_id => 2, :resource_type => 'SailBoat', :token => 'READ')
0
 
0
- #User 2 permission -- read boat 2 & 3
0
- Permission.create!(:id => 3, :user_id => 2, :resource_id => 2, :resource_type => 'SailBoat', :token => 'READ')
0
- Permission.create!(:id => 4, :user_id => 2, :resource_id => 3, :resource_type => 'SailBoat', :token => 'READ')
0
- end
0
+ #User 2 permission -- read boat 2 & 3
0
+ QuerySpec::Permission.create!(:id => 3, :user_id => 2, :resource_id => 2, :resource_type => 'SailBoat', :token => 'READ')
0
+ QuerySpec::Permission.create!(:id => 4, :user_id => 2, :resource_id => 3, :resource_type => 'SailBoat', :token => 'READ')
0
       end
0
 
0
       it 'should accept a DM::Query as a value of a condition' do
0
         # User 1
0
- acl = DataMapper::Query.new(repository(ADAPTER), Permission, :user_id => 1, :resource_type => 'SailBoat', :token => 'READ', :fields => [ :resource_id ])
0
+ acl = DataMapper::Query.new(repository(ADAPTER), QuerySpec::Permission, :user_id => 1, :resource_type => 'SailBoat', :token => 'READ', :fields => [ :resource_id ])
0
         query = { :port => 'Cape Town', :id => acl, :captain.like => 'J%', :order => [ :id ] }
0
- boats = repository(ADAPTER) { SailBoat.all(query) }
0
+ boats = repository(ADAPTER) { QuerySpec::SailBoat.all(query) }
0
         boats.should have(2).entries
0
         boats.entries[0].id.should == 1
0
         boats.entries[1].id.should == 2
0
 
0
         # User 2
0
- acl = DataMapper::Query.new(repository(ADAPTER), Permission, :user_id => 2, :resource_type => 'SailBoat', :token => 'READ', :fields => [ :resource_id ])
0
+ acl = DataMapper::Query.new(repository(ADAPTER), QuerySpec::Permission, :user_id => 2, :resource_type => 'SailBoat', :token => 'READ', :fields => [ :resource_id ])
0
         query = { :port => 'Cape Town', :id => acl, :captain.like => 'J%', :order => [ :id ] }
0
- boats = repository(ADAPTER) { SailBoat.all(query) }
0
+ boats = repository(ADAPTER) { QuerySpec::SailBoat.all(query) }
0
 
0
         boats.should have(2).entries
0
         boats.entries[0].id.should == 2
0
@@ -154,130 +310,44 @@ if ADAPTER
0
 
0
       it 'when value is NOT IN another query' do
0
         # Boats that User 1 Cannot see
0
- acl = DataMapper::Query.new(repository(ADAPTER), Permission, :user_id => 1, :resource_type => 'SailBoat', :token => 'READ', :fields => [:resource_id])
0
+ acl = DataMapper::Query.new(repository(ADAPTER), QuerySpec::Permission, :user_id => 1, :resource_type => 'SailBoat', :token => 'READ', :fields => [ :resource_id ])
0
         query = { :port => 'Cape Town', :id.not => acl, :captain.like => 'J%' }
0
- boats = repository(ADAPTER) { SailBoat.all(query) }
0
+ boats = repository(ADAPTER) { QuerySpec::SailBoat.all(query) }
0
         boats.should have(1).entries
0
         boats.entries[0].id.should == 3
0
       end
0
     end # describe sub-selecting
0
 
0
     describe 'when linking associated objects' do
0
- before :all do
0
- class Region
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :name, String
0
-
0
- def self.default_repository_name
0
- ADAPTER
0
- end
0
- end
0
-
0
- class Factory
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :region_id, Integer
0
- property :name, String
0
-
0
- repository(:mock) do
0
- property :land, String
0
- end
0
-
0
- belongs_to :region
0
-
0
- def self.default_repository_name
0
- ADAPTER
0
- end
0
- end
0
-
0
- class Vehicle
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :factory_id, Integer
0
- property :name, String
0
-
0
- belongs_to :factory
0
-
0
- def self.default_repository_name
0
- ADAPTER
0
- end
0
- end
0
-
0
- module Namespace
0
- class Region
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :name, String
0
-
0
- def self.default_repository_name
0
- ADAPTER
0
- end
0
- end
0
-
0
- class Factory
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :region_id, Integer
0
- property :name, String
0
-
0
- repository(:mock) do
0
- property :land, String
0
- end
0
-
0
- belongs_to :region
0
-
0
- def self.default_repository_name
0
- ADAPTER
0
- end
0
- end
0
-
0
- class Vehicle
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :factory_id, Integer
0
- property :name, String
0
-
0
- belongs_to :factory
0
-
0
- def self.default_repository_name
0
- ADAPTER
0
- end
0
- end
0
- end
0
- end
0
-
0
       before do
0
- Region.auto_migrate!
0
- Factory.auto_migrate!
0
- Vehicle.auto_migrate!
0
+ [ QuerySpec::Region, QuerySpec::Factory, QuerySpec::Vehicle ].each { |m| m.auto_migrate! }
0
 
0
- Region.new(:id=>1, :name=>'North West').save
0
- Factory.new(:id=>2000, :region_id=>1, :name=>'North West Plant').save
0
- Vehicle.new(:id=>1, :factory_id=>2000, :name=>'10 ton delivery truck').save
0
+ QuerySpec::Region.create(:id => 1, :name => 'North West')
0
+ QuerySpec::Factory.create(:id => 2000, :region_id => 1, :name => 'North West Plant')
0
+ QuerySpec::Vehicle.create(:id => 1, :factory_id => 2000, :name => '10 ton delivery truck')
0
 
0
         Namespace::Region.auto_migrate!
0
         Namespace::Factory.auto_migrate!
0
         Namespace::Vehicle.auto_migrate!
0
 
0
- Namespace::Region.new(:id=>1, :name=>'North West').save
0
- Namespace::Factory.new(:id=>2000, :region_id=>1, :name=>'North West Plant').save
0
- Namespace::Vehicle.new(:id=>1, :factory_id=>2000, :name=>'10 ton delivery truck').save
0
+ Namespace::Region.create(:id => 1, :name => 'North West')
0
+ Namespace::Factory.create(:id => 2000, :region_id => 1, :name => 'North West Plant')
0
+ Namespace::Vehicle.create(:id => 1, :factory_id => 2000, :name => '10 ton delivery truck')
0
       end
0
 
0
       it 'should require that all properties in :fields and all :links come from the same repository' #do
0
- # land = Factory.properties(:mock)[:land]
0
+ # land = QuerySpec::Factory.properties(:mock)[:land]
0
       # fields = []
0
- # Vehicle.properties(ADAPTER).map do |property|
0
+ # QuerySpec::Vehicle.properties(ADAPTER).map do |property|
0
       # fields << property
0
       # end
0
       # fields << land
0
       #
0
       # lambda{
0
       # begin
0
- # results = repository(ADAPTER) { Vehicle.all(:links => [:factory], :fields => fields) }
0
+ # results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ :factory ], :fields => fields) }
0
       # rescue RuntimeError
0
- # $!.message.should == "Property Factory.land not available in repository #{ADAPTER}"
0
+ # $!.message.should == "Property QuerySpec::Factory.land not available in repository #{ADAPTER}"
0
       # raise $!
0
       # end
0
       # }.should raise_error(RuntimeError)
0
@@ -287,21 +357,21 @@ if ADAPTER
0
         factory = DataMapper::Associations::Relationship.new(
0
           :factory,
0
           ADAPTER,
0
- 'Vehicle',
0
- 'Factory',
0
+ 'QuerySpec::Vehicle',
0
+ 'QuerySpec::Factory',
0
           { :child_key => [ :factory_id ], :parent_key => [ :id ] }
0
         )
0
- results = repository(ADAPTER) { Vehicle.all(:links => [factory]) }
0
+ results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ factory ]) }
0
         results.should have(1).entries
0
       end
0
 
0
       it 'should accept a symbol of an association name as a link' do
0
- results = repository(ADAPTER) { Vehicle.all(:links => [:factory]) }
0
+ results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ :factory ]) }
0
         results.should have(1).entries
0
       end
0
 
0
       it 'should accept a string of an association name as a link' do
0
- results = repository(ADAPTER) { Vehicle.all(:links => ['factory']) }
0
+ results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ 'factory' ]) }
0
         results.should have(1).entries
0
       end
0
 
0
@@ -309,37 +379,37 @@ if ADAPTER
0
         region = DataMapper::Associations::Relationship.new(
0
           :region,
0
           ADAPTER,
0
- 'Factory',
0
- 'Region',
0
+ 'QuerySpec::Factory',
0
+ 'QuerySpec::Region',
0
           { :child_key => [ :region_id ], :parent_key => [ :id ] }
0
         )
0
- results = repository(ADAPTER) { Vehicle.all(:links => ['factory',region]) }
0
+ results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ 'factory', region ]) }
0
         results.should have(1).entries
0
       end
0
 
0
       it 'should only accept a DM::Assoc::Relationship, String & Symbol as a link' do
0
         lambda{
0
- DataMapper::Query.new(repository(ADAPTER), Vehicle, :links => [1])
0
+ DataMapper::Query.new(repository(ADAPTER), QuerySpec::Vehicle, :links => [1])
0
         }.should raise_error(ArgumentError)
0
       end
0
 
0
       it 'should have a association by the name of the Symbol or String' do
0
         lambda{
0
- DataMapper::Query.new(repository(ADAPTER), Vehicle, :links=>['Sailing'])
0
+ DataMapper::Query.new(repository(ADAPTER), QuerySpec::Vehicle, :links => [ 'Sailing' ])
0
         }.should raise_error(ArgumentError)
0
 
0
         lambda{
0
- DataMapper::Query.new(repository(ADAPTER), Vehicle, :links=>[:sailing])
0
+ DataMapper::Query.new(repository(ADAPTER), QuerySpec::Vehicle, :links => [ :sailing ])
0
         }.should raise_error(ArgumentError)
0
       end
0
 
0
       it 'should create an n-level query path' do
0
- Vehicle.factory.region.model.should == Region
0
- Vehicle.factory.region.name.property.should == Region.properties(Region.repository.name)[:name]
0
+ QuerySpec::Vehicle.factory.region.model.should == QuerySpec::Region
0
+ QuerySpec::Vehicle.factory.region.name.property.should == QuerySpec::Region.properties(QuerySpec::Region.repository.name)[ :name ]
0
       end
0
 
0
       it 'should accept a DM::QueryPath as the key to a condition' do
0
- vehicle = Vehicle.first(Vehicle.factory.region.name => 'North West')
0
+ vehicle = QuerySpec::Vehicle.first(QuerySpec::Vehicle.factory.region.name => 'North West')
0
         vehicle.name.should == '10 ton delivery truck'
0
 
0
         vehicle = Namespace::Vehicle.first(Namespace::Vehicle.factory.region.name => 'North West')
0
@@ -347,7 +417,7 @@ if ADAPTER
0
       end
0
 
0
       it "should accept a string representing a DM::QueryPath as they key to a condition" do
0
- vehicle = Vehicle.first("factory.region.name" => 'North West')
0
+ vehicle = QuerySpec::Vehicle.first("factory.region.name" => 'North West')
0
         vehicle.name.should == '10 ton delivery truck'
0
       end
0
 
0
@@ -357,19 +427,13 @@ if ADAPTER
0
 
0
       it 'should eager load associations' do
0
         repository(ADAPTER) do
0
- vehicle = Vehicle.first(:includes => [Vehicle.factory])
0
+ vehicle = QuerySpec::Vehicle.first(:includes => [ QuerySpec::Vehicle.factory ])
0
         end
0
       end
0
 
0
       it "should behave when using mocks" do
0
- class Group
0
- include DataMapper::Resource
0
- property :id, Serial
0
- property :name, String
0
- end
0
-
0
- Group.should_receive(:all).with(:order => [:id.asc])
0
- Group.all(:order => [:id.asc])
0
+ QuerySpec::Group.should_receive(:all).with(:order => [ :id.asc ])
0
+ QuerySpec::Group.all(:order => [ :id.asc ])
0
       end
0
     end # describe links
0
   end # DM::Query

Comments

    No one has commented yet.