dm-ambition 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/rcov.opts ADDED
@@ -0,0 +1,6 @@
1
+ --exclude-only "spec\/,^\/"
2
+ --sort coverage
3
+ --callsites
4
+ --xrefs
5
+ --profile
6
+ --text-summary
@@ -0,0 +1,718 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ describe DataMapper::Ambition::Query do
5
+ before :all do
6
+ class ::User
7
+ include DataMapper::Resource
8
+
9
+ property :id, Serial
10
+ property :name, String
11
+ property :admin, Boolean
12
+ end
13
+
14
+ if DataMapper.respond_to?(:auto_migrate!)
15
+ DataMapper.auto_migrate!
16
+ end
17
+ end
18
+
19
+ before :all do
20
+ @repository = DataMapper.repository(:default)
21
+ @model = User
22
+ @query = DataMapper::Query.new(@repository, @model)
23
+
24
+ @subject = @query
25
+ end
26
+
27
+ it { @subject.should respond_to(:filter) }
28
+
29
+ describe '#filter' do
30
+ describe 'with operator' do
31
+ describe '==' do
32
+ before :all do
33
+ @return = @subject.filter { |u| u.name == 'Dan Kubb' }
34
+ end
35
+
36
+ it 'should return a Query' do
37
+ @return.should be_kind_of(DataMapper::Query)
38
+ end
39
+
40
+ it 'should not return self' do
41
+ @return.should_not equal(@subject)
42
+ end
43
+
44
+ it 'should set conditions' do
45
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
46
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
47
+ )
48
+ end
49
+ end
50
+
51
+ describe '=~' do
52
+ before :all do
53
+ @return = @subject.filter { |u| u.name =~ /Dan Kubb/ }
54
+ end
55
+
56
+ it 'should return a Query' do
57
+ @return.should be_kind_of(DataMapper::Query)
58
+ end
59
+
60
+ it 'should not return self' do
61
+ @return.should_not equal(@subject)
62
+ end
63
+
64
+ it 'should set conditions' do
65
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
66
+ DataMapper::Query::Conditions::Comparison.new(:regexp, @model.properties[:name], /Dan Kubb/)
67
+ )
68
+ end
69
+ end
70
+
71
+ describe '>' do
72
+ before :all do
73
+ @return = @subject.filter { |u| u.id > 1 }
74
+ end
75
+
76
+ it 'should return a Query' do
77
+ @return.should be_kind_of(DataMapper::Query)
78
+ end
79
+
80
+ it 'should not return self' do
81
+ @return.should_not equal(@subject)
82
+ end
83
+
84
+ it 'should set conditions' do
85
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
86
+ DataMapper::Query::Conditions::Comparison.new(:gt, @model.properties[:id], 1)
87
+ )
88
+ end
89
+ end
90
+
91
+ describe '>=' do
92
+ before :all do
93
+ @return = @subject.filter { |u| u.id >= 1 }
94
+ end
95
+
96
+ it 'should return a Query' do
97
+ @return.should be_kind_of(DataMapper::Query)
98
+ end
99
+
100
+ it 'should not return self' do
101
+ @return.should_not equal(@subject)
102
+ end
103
+
104
+ it 'should set conditions' do
105
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
106
+ DataMapper::Query::Conditions::Comparison.new(:gte, @model.properties[:id], 1)
107
+ )
108
+ end
109
+ end
110
+
111
+ describe '<' do
112
+ before :all do
113
+ @return = @subject.filter { |u| u.id < 1 }
114
+ end
115
+
116
+ it 'should return a Query' do
117
+ @return.should be_kind_of(DataMapper::Query)
118
+ end
119
+
120
+ it 'should not return self' do
121
+ @return.should_not equal(@subject)
122
+ end
123
+
124
+ it 'should set conditions' do
125
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
126
+ DataMapper::Query::Conditions::Comparison.new(:lt, @model.properties[:id], 1)
127
+ )
128
+ end
129
+ end
130
+
131
+ describe '<=' do
132
+ before :all do
133
+ @return = @subject.filter { |u| u.id <= 1 }
134
+ end
135
+
136
+ it 'should return a Query' do
137
+ @return.should be_kind_of(DataMapper::Query)
138
+ end
139
+
140
+ it 'should not return self' do
141
+ @return.should_not equal(@subject)
142
+ end
143
+
144
+ it 'should set conditions' do
145
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
146
+ DataMapper::Query::Conditions::Comparison.new(:lte, @model.properties[:id], 1)
147
+ )
148
+ end
149
+ end
150
+
151
+ [ :include?, :member? ].each do |method|
152
+ describe "Array##{method}" do
153
+ before :all do
154
+ @return = @subject.filter { |u| [ 1, 2 ].send(method, u.id) }
155
+ end
156
+
157
+ it 'should return a Query' do
158
+ @return.should be_kind_of(DataMapper::Query)
159
+ end
160
+
161
+ it 'should not return self' do
162
+ @return.should_not equal(@subject)
163
+ end
164
+
165
+ it 'should set conditions' do
166
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
167
+ DataMapper::Query::Conditions::Comparison.new(:in, @model.properties[:id], [ 1, 2 ])
168
+ )
169
+ end
170
+ end
171
+ end
172
+
173
+ [ :include?, :member?, :=== ].each do |method|
174
+ describe "Range##{method} (inclusive)" do
175
+ before :all do
176
+ @return = @subject.filter { |u| (1..2).send(method, u.id) }
177
+ end
178
+
179
+ it 'should return a Query' do
180
+ @return.should be_kind_of(DataMapper::Query)
181
+ end
182
+
183
+ it 'should not return self' do
184
+ @return.should_not equal(@subject)
185
+ end
186
+
187
+ it 'should set conditions' do
188
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
189
+ DataMapper::Query::Conditions::Comparison.new(:in, @model.properties[:id], 1..2)
190
+ )
191
+ end
192
+ end
193
+
194
+ describe "Range##{method} (exclusive)" do
195
+ before :all do
196
+ @return = @subject.filter { |u| (1...3).send(method, u.id) }
197
+ end
198
+
199
+ it 'should return a Query' do
200
+ @return.should be_kind_of(DataMapper::Query)
201
+ end
202
+
203
+ it 'should not return self' do
204
+ @return.should_not equal(@subject)
205
+ end
206
+
207
+ it 'should set conditions' do
208
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
209
+ DataMapper::Query::Conditions::Comparison.new(:in, @model.properties[:id], 1...3)
210
+ )
211
+ end
212
+ end
213
+ end
214
+
215
+ [ :key?, :has_key?, :include?, :member? ].each do |method|
216
+ describe "Hash##{method}" do
217
+ before :all do
218
+ @return = @subject.filter { |u| { 1 => '1', 2 => '2' }.send(method, u.id) }
219
+ end
220
+
221
+ it 'should return a Query' do
222
+ @return.should be_kind_of(DataMapper::Query)
223
+ end
224
+
225
+ it 'should not return self' do
226
+ @return.should_not equal(@subject)
227
+ end
228
+
229
+ it 'should set conditions' do
230
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
231
+ DataMapper::Query::Conditions::Comparison.new(:in, @model.properties[:id], [ 1, 2 ])
232
+ )
233
+ end
234
+ end
235
+ end
236
+
237
+ [ :value?, :has_value? ].each do |method|
238
+ describe "Hash##{method}" do
239
+ before :all do
240
+ @return = @subject.filter { |u| { '1' => 1, '2' => 2 }.value?(u.id) }
241
+ end
242
+
243
+ it 'should return a Query' do
244
+ @return.should be_kind_of(DataMapper::Query)
245
+ end
246
+
247
+ it 'should not return self' do
248
+ @return.should_not equal(@subject)
249
+ end
250
+
251
+ it 'should set conditions' do
252
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
253
+ DataMapper::Query::Conditions::Comparison.new(:in, @model.properties[:id], [ 1, 2 ])
254
+ )
255
+ end
256
+ end
257
+ end
258
+
259
+ describe 'receiver.method.nil?' do
260
+ before :all do
261
+ @return = @subject.filter { |u| u.id.nil? }
262
+ end
263
+
264
+ it 'should return a Query' do
265
+ @return.should be_kind_of(DataMapper::Query)
266
+ end
267
+
268
+ it 'should not return self' do
269
+ @return.should_not equal(@subject)
270
+ end
271
+
272
+ it 'should set conditions' do
273
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
274
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:id], nil)
275
+ )
276
+ end
277
+ end
278
+ end
279
+
280
+ describe 'with bind value' do
281
+ describe 'nil' do
282
+ before :all do
283
+ @return = @subject.filter { |u| u.name == nil }
284
+ end
285
+
286
+ it 'should return a Query' do
287
+ @return.should be_kind_of(DataMapper::Query)
288
+ end
289
+
290
+ it 'should not return self' do
291
+ @return.should_not equal(@subject)
292
+ end
293
+
294
+ it 'should set conditions' do
295
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
296
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], nil)
297
+ )
298
+ end
299
+ end
300
+
301
+ describe 'true' do
302
+ before :all do
303
+ @return = @subject.filter { |u| u.admin == true }
304
+ end
305
+
306
+ it 'should return a Query' do
307
+ @return.should be_kind_of(DataMapper::Query)
308
+ end
309
+
310
+ it 'should not return self' do
311
+ @return.should_not equal(@subject)
312
+ end
313
+
314
+ it 'should set conditions' do
315
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
316
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:admin], true)
317
+ )
318
+ end
319
+ end
320
+
321
+ describe 'false' do
322
+ before :all do
323
+ @return = @subject.filter { |u| u.admin == false }
324
+ end
325
+
326
+ it 'should return a Query' do
327
+ @return.should be_kind_of(DataMapper::Query)
328
+ end
329
+
330
+ it 'should not return self' do
331
+ @return.should_not equal(@subject)
332
+ end
333
+
334
+ it 'should set conditions' do
335
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
336
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:admin], false)
337
+ )
338
+ end
339
+ end
340
+ end
341
+
342
+ describe 'with conditions' do
343
+ describe 'ANDed' do
344
+ before :all do
345
+ @return = @subject.filter { |u| u.id == 1 && u.name == 'Dan Kubb' }
346
+ end
347
+
348
+ it 'should return a Query' do
349
+ @return.should be_kind_of(DataMapper::Query)
350
+ end
351
+
352
+ it 'should not return self' do
353
+ @return.should_not equal(@subject)
354
+ end
355
+
356
+ it 'should set conditions' do
357
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
358
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:id], 1),
359
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
360
+ )
361
+ end
362
+ end
363
+
364
+ describe 'ORed' do
365
+ before :all do
366
+ @return = @subject.filter { |u| u.id == 1 || u.name == 'Dan Kubb' }
367
+ end
368
+
369
+ it 'should return a Query' do
370
+ @return.should be_kind_of(DataMapper::Query)
371
+ end
372
+
373
+ it 'should not return self' do
374
+ @return.should_not equal(@subject)
375
+ end
376
+
377
+ it 'should set conditions' do
378
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
379
+ DataMapper::Query::Conditions::Operation.new(:or,
380
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:id], 1),
381
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
382
+ )
383
+ )
384
+ end
385
+ end
386
+
387
+ describe 'negated' do
388
+ before :all do
389
+ @return = @subject.filter { |u| !(u.id == 1) }
390
+ end
391
+
392
+ it 'should return a Query' do
393
+ @return.should be_kind_of(DataMapper::Query)
394
+ end
395
+
396
+ it 'should not return self' do
397
+ @return.should_not equal(@subject)
398
+ end
399
+
400
+ it 'should set conditions' do
401
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
402
+ DataMapper::Query::Conditions::Operation.new(:not,
403
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:id], 1)
404
+ )
405
+ )
406
+ end
407
+ end
408
+
409
+ describe 'double-negated' do
410
+ before :all do
411
+ @return = @subject.filter { |u| !(!(u.id == 1)) }
412
+ end
413
+
414
+ it 'should return a Query' do
415
+ @return.should be_kind_of(DataMapper::Query)
416
+ end
417
+
418
+ it 'should not return self' do
419
+ @return.should_not equal(@subject)
420
+ end
421
+
422
+ it 'should set conditions' do
423
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
424
+ DataMapper::Query::Conditions::Operation.new(:not,
425
+ DataMapper::Query::Conditions::Operation.new(:not,
426
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:id], 1)
427
+ )
428
+ )
429
+ )
430
+ end
431
+ end
432
+
433
+ describe 'receiver matching a resource' do
434
+ before :all do
435
+ resource = User.create(:id => 1)
436
+
437
+ @return = @subject.filter { |u| u == resource }
438
+ end
439
+
440
+ it 'should return a Query' do
441
+ @return.should be_kind_of(DataMapper::Query)
442
+ end
443
+
444
+ it 'should not return self' do
445
+ @return.should_not equal(@subject)
446
+ end
447
+
448
+ it 'should set conditions' do
449
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
450
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:id], 1)
451
+ )
452
+ end
453
+ end
454
+
455
+ [ :include?, :member? ].each do |method|
456
+ describe "receiver matching a resource using Array##{method}" do
457
+ before :all do
458
+ resource = User.new(:id => 1)
459
+
460
+ @return = @subject.filter { |u| [ resource ].send(method, u) }
461
+ end
462
+
463
+ it 'should return a Query' do
464
+ @return.should be_kind_of(DataMapper::Query)
465
+ end
466
+
467
+ it 'should not return self' do
468
+ @return.should_not equal(@subject)
469
+ end
470
+
471
+ it 'should set conditions' do
472
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
473
+ DataMapper::Query::Conditions::Comparison.new(:in, @model.properties[:id], [ 1 ])
474
+ )
475
+ end
476
+ end
477
+ end
478
+
479
+ [ :key?, :has_key?, :include?, :member? ].each do |method|
480
+ describe "receiver matching a resource using Hash##{method}" do
481
+ before :all do
482
+ one = User.new(:id => 1)
483
+ two = User.new(:id => 2)
484
+
485
+ @return = @subject.filter { |u| { one => '1', two => '2' }.send(method, u) }
486
+ end
487
+
488
+ it 'should return a Query' do
489
+ @return.should be_kind_of(DataMapper::Query)
490
+ end
491
+
492
+ it 'should not return self' do
493
+ @return.should_not equal(@subject)
494
+ end
495
+
496
+ it 'should set conditions' do
497
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
498
+ DataMapper::Query::Conditions::Comparison.new(:in, @model.properties[:id], [ 1, 2 ])
499
+ )
500
+ end
501
+ end
502
+ end
503
+
504
+ [ :value?, :has_value? ].each do |method|
505
+ describe "receiver matching a resource using Hash##{method}" do
506
+ before :all do
507
+ one = User.new(:id => 1)
508
+ two = User.new(:id => 2)
509
+
510
+ @return = @subject.filter { |u| { '1' => one, '2' => two }.send(method, u) }
511
+ end
512
+
513
+ it 'should return a Query' do
514
+ @return.should be_kind_of(DataMapper::Query)
515
+ end
516
+
517
+ it 'should not return self' do
518
+ @return.should_not equal(@subject)
519
+ end
520
+
521
+ it 'should set conditions' do
522
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
523
+ DataMapper::Query::Conditions::Comparison.new(:in, @model.properties[:id], [ 1, 2 ])
524
+ )
525
+ end
526
+ end
527
+ end
528
+
529
+ describe 'using send on receiver' do
530
+ before :all do
531
+ @return = @subject.filter { |u| u.send(:name) == 'Dan Kubb' }
532
+ end
533
+
534
+ it 'should return a Query' do
535
+ @return.should be_kind_of(DataMapper::Query)
536
+ end
537
+
538
+ it 'should not return self' do
539
+ @return.should_not equal(@subject)
540
+ end
541
+
542
+ it 'should set conditions' do
543
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
544
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
545
+ )
546
+ end
547
+ end
548
+ end
549
+
550
+ describe 'with external value' do
551
+ describe 'local variable' do
552
+ before :all do
553
+ name = 'Dan Kubb'
554
+
555
+ @return = @subject.filter { |u| u.name == name }
556
+ end
557
+
558
+ it 'should return a Query' do
559
+ @return.should be_kind_of(DataMapper::Query)
560
+ end
561
+
562
+ it 'should not return self' do
563
+ @return.should_not equal(@subject)
564
+ end
565
+
566
+ it 'should set conditions' do
567
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
568
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
569
+ )
570
+ end
571
+ end
572
+
573
+ describe 'instance variable' do
574
+ before :all do
575
+ @name = 'Dan Kubb'
576
+
577
+ @return = @subject.filter { |u| u.name == @name }
578
+ end
579
+
580
+ it 'should return a Query' do
581
+ @return.should be_kind_of(DataMapper::Query)
582
+ end
583
+
584
+ it 'should not return self' do
585
+ @return.should_not equal(@subject)
586
+ end
587
+
588
+ it 'should set conditions' do
589
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
590
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], @name)
591
+ )
592
+ end
593
+ end
594
+
595
+ describe 'global variable' do
596
+ before :all do
597
+ $name = 'Dan Kubb'
598
+
599
+ @return = @subject.filter { |u| u.name == $name }
600
+ end
601
+
602
+ it 'should return a Query' do
603
+ @return.should be_kind_of(DataMapper::Query)
604
+ end
605
+
606
+ it 'should not return self' do
607
+ @return.should_not equal(@subject)
608
+ end
609
+
610
+ it 'should set conditions' do
611
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
612
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], $name)
613
+ )
614
+ end
615
+ end
616
+
617
+ describe 'method' do
618
+ def name
619
+ 'Dan Kubb'
620
+ end
621
+
622
+ before :all do
623
+ @return = @subject.filter { |u| u.name == name }
624
+ end
625
+
626
+ it 'should return a Query' do
627
+ @return.should be_kind_of(DataMapper::Query)
628
+ end
629
+
630
+ it 'should not return self' do
631
+ @return.should_not equal(@subject)
632
+ end
633
+
634
+ it 'should set conditions' do
635
+ pending 'TODO: make methods work inside block' do
636
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
637
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], name)
638
+ )
639
+ end
640
+ end
641
+ end
642
+
643
+ describe 'constant' do
644
+ NAME = 'Dan Kubb'
645
+
646
+ before :all do
647
+ @return = @subject.filter { |u| u.name == NAME }
648
+ end
649
+
650
+ it 'should return a Query' do
651
+ @return.should be_kind_of(DataMapper::Query)
652
+ end
653
+
654
+ it 'should not return self' do
655
+ @return.should_not equal(@subject)
656
+ end
657
+
658
+ it 'should set conditions' do
659
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
660
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], NAME)
661
+ )
662
+ end
663
+ end
664
+
665
+ describe 'namespaced constant' do
666
+ before :all do
667
+ Object.send(:remove_const, :Condition) if defined?(::Condition)
668
+ module ::Condition
669
+ Name = 'Dan Kubb'
670
+ end
671
+
672
+ @return = @subject.filter { |u| u.name == Condition::Name }
673
+ end
674
+
675
+ it 'should return a Query' do
676
+ @return.should be_kind_of(DataMapper::Query)
677
+ end
678
+
679
+ it 'should not return self' do
680
+ @return.should_not equal(@subject)
681
+ end
682
+
683
+ it 'should set conditions' do
684
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
685
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
686
+ )
687
+ end
688
+ end
689
+
690
+ describe 'namespaced method' do
691
+ before :all do
692
+ Object.send(:remove_const, :Condition) if defined?(::Condition)
693
+ module ::Condition
694
+ def self.name
695
+ 'Dan Kubb'
696
+ end
697
+ end
698
+
699
+ @return = @subject.filter { |u| u.name == Condition::name }
700
+ end
701
+
702
+ it 'should return a Query' do
703
+ @return.should be_kind_of(DataMapper::Query)
704
+ end
705
+
706
+ it 'should not return self' do
707
+ @return.should_not equal(@subject)
708
+ end
709
+
710
+ it 'should set conditions' do
711
+ @return.conditions.should == DataMapper::Query::Conditions::Operation.new(:and,
712
+ DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
713
+ )
714
+ end
715
+ end
716
+ end
717
+ end
718
+ end