dm-core 0.10.1 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +29 -0
- data/.document +5 -0
- data/.gitignore +27 -0
- data/LICENSE +20 -0
- data/{README.txt → README.rdoc} +14 -3
- data/Rakefile +23 -22
- data/VERSION +1 -0
- data/dm-core.gemspec +201 -10
- data/lib/dm-core.rb +32 -23
- data/lib/dm-core/adapters.rb +0 -1
- data/lib/dm-core/adapters/data_objects_adapter.rb +230 -151
- data/lib/dm-core/adapters/mysql_adapter.rb +7 -8
- data/lib/dm-core/adapters/oracle_adapter.rb +39 -59
- data/lib/dm-core/adapters/postgres_adapter.rb +0 -1
- data/lib/dm-core/adapters/sqlite3_adapter.rb +5 -0
- data/lib/dm-core/adapters/sqlserver_adapter.rb +114 -0
- data/lib/dm-core/adapters/yaml_adapter.rb +0 -5
- data/lib/dm-core/associations/many_to_many.rb +118 -56
- data/lib/dm-core/associations/many_to_one.rb +48 -21
- data/lib/dm-core/associations/one_to_many.rb +8 -30
- data/lib/dm-core/associations/one_to_one.rb +1 -5
- data/lib/dm-core/associations/relationship.rb +89 -97
- data/lib/dm-core/collection.rb +299 -184
- data/lib/dm-core/core_ext/enumerable.rb +28 -0
- data/lib/dm-core/core_ext/kernel.rb +0 -2
- data/lib/dm-core/migrations.rb +314 -170
- data/lib/dm-core/model.rb +97 -66
- data/lib/dm-core/model/descendant_set.rb +1 -1
- data/lib/dm-core/model/hook.rb +0 -3
- data/lib/dm-core/model/property.rb +7 -10
- data/lib/dm-core/model/relationship.rb +79 -26
- data/lib/dm-core/model/scope.rb +3 -4
- data/lib/dm-core/property.rb +152 -90
- data/lib/dm-core/property_set.rb +18 -37
- data/lib/dm-core/query.rb +452 -153
- data/lib/dm-core/query/conditions/comparison.rb +266 -173
- data/lib/dm-core/query/conditions/operation.rb +499 -57
- data/lib/dm-core/query/direction.rb +0 -3
- data/lib/dm-core/query/operator.rb +0 -4
- data/lib/dm-core/query/path.rb +10 -12
- data/lib/dm-core/query/sort.rb +4 -10
- data/lib/dm-core/repository.rb +10 -6
- data/lib/dm-core/resource.rb +343 -148
- data/lib/dm-core/spec/adapter_shared_spec.rb +17 -1
- data/lib/dm-core/spec/data_objects_adapter_shared_spec.rb +277 -17
- data/lib/dm-core/support/chainable.rb +0 -2
- data/lib/dm-core/support/equalizer.rb +27 -3
- data/lib/dm-core/transaction.rb +75 -75
- data/lib/dm-core/type.rb +19 -5
- data/lib/dm-core/types/discriminator.rb +4 -4
- data/lib/dm-core/types/object.rb +2 -7
- data/lib/dm-core/types/paranoid_boolean.rb +8 -2
- data/lib/dm-core/types/paranoid_datetime.rb +8 -2
- data/lib/dm-core/version.rb +1 -1
- data/script/performance.rb +7 -7
- data/script/profile.rb +6 -6
- data/spec/lib/collection_helpers.rb +2 -2
- data/spec/lib/pending_helpers.rb +22 -3
- data/spec/lib/rspec_immediate_feedback_formatter.rb +1 -0
- data/spec/public/associations/many_to_many_spec.rb +6 -4
- data/spec/public/associations/many_to_one_spec.rb +10 -1
- data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +39 -0
- data/spec/public/associations/one_to_many_spec.rb +4 -3
- data/spec/public/associations/one_to_one_spec.rb +19 -1
- data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +45 -0
- data/spec/public/collection_spec.rb +4 -3
- data/spec/public/migrations_spec.rb +144 -0
- data/spec/public/model/relationship_spec.rb +115 -55
- data/spec/public/model_spec.rb +13 -13
- data/spec/public/property/object_spec.rb +106 -0
- data/spec/public/property_spec.rb +18 -14
- data/spec/public/resource_spec.rb +10 -1
- data/spec/public/sel_spec.rb +16 -49
- data/spec/public/setup_spec.rb +1 -1
- data/spec/public/shared/association_collection_shared_spec.rb +6 -14
- data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
- data/spec/public/shared/collection_shared_spec.rb +214 -217
- data/spec/public/shared/finder_shared_spec.rb +259 -365
- data/spec/public/shared/resource_shared_spec.rb +524 -248
- data/spec/public/transaction_spec.rb +27 -3
- data/spec/public/types/discriminator_spec.rb +1 -1
- data/spec/rcov.opts +6 -0
- data/spec/semipublic/adapters/sqlserver_adapter_spec.rb +17 -0
- data/spec/semipublic/associations/many_to_one_spec.rb +3 -20
- data/spec/semipublic/associations_spec.rb +2 -2
- data/spec/semipublic/collection_spec.rb +0 -32
- data/spec/semipublic/model_spec.rb +96 -0
- data/spec/semipublic/property_spec.rb +3 -3
- data/spec/semipublic/query/conditions/comparison_spec.rb +1719 -0
- data/spec/semipublic/query/conditions/operation_spec.rb +1292 -0
- data/spec/semipublic/query_spec.rb +1285 -144
- data/spec/semipublic/resource_spec.rb +0 -24
- data/spec/semipublic/shared/resource_shared_spec.rb +103 -38
- data/spec/spec.opts +1 -1
- data/spec/spec_helper.rb +15 -6
- data/tasks/ci.rake +1 -0
- data/tasks/metrics.rake +37 -0
- data/tasks/spec.rake +41 -0
- data/tasks/yard.rake +9 -0
- data/tasks/yardstick.rake +19 -0
- metadata +99 -29
- data/CONTRIBUTING +0 -51
- data/FAQ +0 -93
- data/History.txt +0 -27
- data/MIT-LICENSE +0 -22
- data/Manifest.txt +0 -121
- data/QUICKLINKS +0 -11
- data/SPECS +0 -35
- data/TODO +0 -1
- data/spec/semipublic/query/conditions_spec.rb +0 -528
- data/tasks/ci.rb +0 -24
- data/tasks/dm.rb +0 -58
- data/tasks/doc.rb +0 -17
- data/tasks/gemspec.rb +0 -23
- data/tasks/hoe.rb +0 -45
- data/tasks/install.rb +0 -18
@@ -21,7 +21,7 @@ describe DataMapper::Query do
|
|
21
21
|
property :password, Password
|
22
22
|
property :balance, BigDecimal
|
23
23
|
|
24
|
-
belongs_to :referrer, self, :
|
24
|
+
belongs_to :referrer, self, :required => false
|
25
25
|
has n, :referrals, self, :inverse => :referrer
|
26
26
|
end
|
27
27
|
|
@@ -175,14 +175,6 @@ describe DataMapper::Query do
|
|
175
175
|
end
|
176
176
|
end
|
177
177
|
|
178
|
-
describe 'that is an empty Array and the unique option is false' do
|
179
|
-
it 'should raise an exception' do
|
180
|
-
lambda {
|
181
|
-
DataMapper::Query.new(@repository, @model, @options.update(:fields => [], :unique => false))
|
182
|
-
}.should raise_error(ArgumentError, '+options[:fields]+ should not be empty if +options[:unique]+ is false')
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
178
|
describe 'that is an Array containing an unknown Symbol' do
|
187
179
|
it 'should raise an exception' do
|
188
180
|
lambda {
|
@@ -579,24 +571,26 @@ describe DataMapper::Query do
|
|
579
571
|
end
|
580
572
|
end
|
581
573
|
|
582
|
-
describe 'with
|
574
|
+
describe 'with an Array with 1 entry' do
|
583
575
|
before :all do
|
584
|
-
@options[:conditions] = { :name =>
|
576
|
+
@options[:conditions] = { :name => [ 'Dan Kubb' ] }
|
585
577
|
@return = DataMapper::Query.new(@repository, @model, @options.freeze)
|
586
578
|
end
|
587
579
|
|
588
580
|
it { @return.should be_kind_of(DataMapper::Query) }
|
589
581
|
|
590
582
|
it 'should set the conditions' do
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
583
|
+
pending do
|
584
|
+
@return.conditions.should ==
|
585
|
+
DataMapper::Query::Conditions::Operation.new(
|
586
|
+
:and,
|
587
|
+
DataMapper::Query::Conditions::Comparison.new(
|
588
|
+
:eql,
|
589
|
+
@model.properties[:name],
|
590
|
+
'Dan Kubb'
|
591
|
+
)
|
598
592
|
)
|
599
|
-
|
593
|
+
end
|
600
594
|
end
|
601
595
|
|
602
596
|
it 'should be valid' do
|
@@ -604,9 +598,9 @@ describe DataMapper::Query do
|
|
604
598
|
end
|
605
599
|
end
|
606
600
|
|
607
|
-
describe 'with an Array with
|
601
|
+
describe 'with an Array with no entries' do
|
608
602
|
before :all do
|
609
|
-
@options[:conditions] = { :name => [
|
603
|
+
@options[:conditions] = { :name => [] }
|
610
604
|
@return = DataMapper::Query.new(@repository, @model, @options.freeze)
|
611
605
|
end
|
612
606
|
|
@@ -626,8 +620,8 @@ describe DataMapper::Query do
|
|
626
620
|
end
|
627
621
|
end
|
628
622
|
|
629
|
-
it 'should be valid' do
|
630
|
-
@return.
|
623
|
+
it 'should not be valid' do
|
624
|
+
@return.should_not be_valid
|
631
625
|
end
|
632
626
|
end
|
633
627
|
|
@@ -814,14 +808,6 @@ describe DataMapper::Query do
|
|
814
808
|
end
|
815
809
|
end
|
816
810
|
|
817
|
-
describe 'that is a Hash with a not operator that has an empty Array' do
|
818
|
-
it 'should raise an exception' do
|
819
|
-
lambda {
|
820
|
-
DataMapper::Query.new(@repository, @model, @options.update(:conditions => { :name.not => [] }))
|
821
|
-
}.should raise_error(ArgumentError, 'Cannot use \'not\' operator with a bind value that is an empty Array for #<DataMapper::Query::Operator @target=:name @operator=:not>')
|
822
|
-
end
|
823
|
-
end
|
824
|
-
|
825
811
|
describe 'that is a Hash with a key of a type that is not permitted' do
|
826
812
|
it 'should raise an exception' do
|
827
813
|
lambda {
|
@@ -924,6 +910,44 @@ describe DataMapper::Query do
|
|
924
910
|
end
|
925
911
|
|
926
912
|
describe 'with an order option' do
|
913
|
+
describe 'that is a single Symbol' do
|
914
|
+
before :all do
|
915
|
+
@options[:order] = :name
|
916
|
+
@return = DataMapper::Query.new(@repository, @model, @options.freeze)
|
917
|
+
end
|
918
|
+
|
919
|
+
it { @return.should be_kind_of(DataMapper::Query) }
|
920
|
+
|
921
|
+
it 'should set the order' do
|
922
|
+
@return.order.should == [ DataMapper::Query::Direction.new(@model.properties[:name]) ]
|
923
|
+
end
|
924
|
+
end
|
925
|
+
|
926
|
+
describe 'that is a single String' do
|
927
|
+
before :all do
|
928
|
+
@options[:order] = 'name'
|
929
|
+
@return = DataMapper::Query.new(@repository, @model, @options.freeze)
|
930
|
+
end
|
931
|
+
|
932
|
+
it { @return.should be_kind_of(DataMapper::Query) }
|
933
|
+
|
934
|
+
it 'should set the order' do
|
935
|
+
@return.order.should == [ DataMapper::Query::Direction.new(@model.properties[:name]) ]
|
936
|
+
end
|
937
|
+
end
|
938
|
+
|
939
|
+
describe 'that is a single Property' do
|
940
|
+
before :all do
|
941
|
+
@options[:order] = @model.properties.values_at(:name)
|
942
|
+
@return = DataMapper::Query.new(@repository, @model, @options.freeze)
|
943
|
+
end
|
944
|
+
|
945
|
+
it { @return.should be_kind_of(DataMapper::Query) }
|
946
|
+
|
947
|
+
it 'should set the order' do
|
948
|
+
@return.order.should == [ DataMapper::Query::Direction.new(@model.properties[:name]) ]
|
949
|
+
end
|
950
|
+
end
|
927
951
|
describe 'that is an Array containing a Symbol' do
|
928
952
|
before :all do
|
929
953
|
@return = DataMapper::Query.new(@repository, @model, @options.freeze)
|
@@ -1039,8 +1063,8 @@ describe DataMapper::Query do
|
|
1039
1063
|
describe 'that is invalid' do
|
1040
1064
|
it 'should raise an exception' do
|
1041
1065
|
lambda {
|
1042
|
-
DataMapper::Query.new(@repository, @model, @options.update(:order =>
|
1043
|
-
}.should raise_error(ArgumentError,
|
1066
|
+
DataMapper::Query.new(@repository, @model, @options.update(:order => 'unknown'))
|
1067
|
+
}.should raise_error(ArgumentError, "+options[:order]+ entry \"unknown\" does not map to a property in #{@model}")
|
1044
1068
|
end
|
1045
1069
|
end
|
1046
1070
|
|
@@ -1141,7 +1165,7 @@ describe DataMapper::Query do
|
|
1141
1165
|
|
1142
1166
|
describe 'that is missing' do
|
1143
1167
|
before :all do
|
1144
|
-
@return = DataMapper::Query.new(@repository, @model, @options.except(:unique).freeze)
|
1168
|
+
@return = DataMapper::Query.new(@repository, @model, @options.except(:unique, :links).freeze)
|
1145
1169
|
end
|
1146
1170
|
|
1147
1171
|
it { @return.should be_kind_of(DataMapper::Query) }
|
@@ -1281,16 +1305,24 @@ describe DataMapper::Query do
|
|
1281
1305
|
class ::User
|
1282
1306
|
include DataMapper::Resource
|
1283
1307
|
|
1284
|
-
property :name,
|
1308
|
+
property :name, String, :key => true
|
1285
1309
|
property :citizenship, String
|
1286
1310
|
|
1287
|
-
belongs_to :referrer, self, :
|
1288
|
-
has n, :referrals,
|
1311
|
+
belongs_to :referrer, self, :required => false
|
1312
|
+
has n, :referrals, self, :inverse => :referrer
|
1313
|
+
has n, :grandparents, self, :through => :referrals, :via => :referrer
|
1314
|
+
end
|
1315
|
+
|
1316
|
+
class ::Other
|
1317
|
+
include DataMapper::Resource
|
1289
1318
|
|
1290
|
-
|
1291
|
-
assert_valid
|
1319
|
+
property :id, Serial
|
1292
1320
|
end
|
1293
1321
|
|
1322
|
+
# TODO: figure out how to remove these
|
1323
|
+
User.send(:assert_valid)
|
1324
|
+
Other.send(:assert_valid)
|
1325
|
+
|
1294
1326
|
@repository = DataMapper::Repository.new(:default)
|
1295
1327
|
@model = User
|
1296
1328
|
@options = { :limit => 3 }
|
@@ -1312,7 +1344,9 @@ describe DataMapper::Query do
|
|
1312
1344
|
}
|
1313
1345
|
end
|
1314
1346
|
|
1315
|
-
|
1347
|
+
subject { @query }
|
1348
|
+
|
1349
|
+
it { should respond_to(:==) }
|
1316
1350
|
|
1317
1351
|
describe '#==' do
|
1318
1352
|
describe 'when other is equal' do
|
@@ -1394,7 +1428,7 @@ describe DataMapper::Query do
|
|
1394
1428
|
end
|
1395
1429
|
end
|
1396
1430
|
|
1397
|
-
it {
|
1431
|
+
it { should respond_to(:conditions) }
|
1398
1432
|
|
1399
1433
|
describe '#conditions' do
|
1400
1434
|
before :all do
|
@@ -1418,13 +1452,388 @@ describe DataMapper::Query do
|
|
1418
1452
|
end
|
1419
1453
|
end
|
1420
1454
|
|
1421
|
-
|
1455
|
+
[ :difference, :- ].each do |method|
|
1456
|
+
it { should respond_to(method) }
|
1457
|
+
|
1458
|
+
describe "##{method}" do
|
1459
|
+
supported_by :all do
|
1460
|
+
before :all do
|
1461
|
+
@key = @model.key(@repository.name)
|
1462
|
+
|
1463
|
+
@self_relationship = DataMapper::Associations::OneToMany::Relationship.new(
|
1464
|
+
:self,
|
1465
|
+
@model,
|
1466
|
+
@model,
|
1467
|
+
{
|
1468
|
+
:child_key => @key.map { |p| p.name },
|
1469
|
+
:parent_key => @key.map { |p| p.name },
|
1470
|
+
:child_repository_name => @repository,
|
1471
|
+
:parent_repository_name => @repository,
|
1472
|
+
}
|
1473
|
+
)
|
1474
|
+
|
1475
|
+
10.times do |n|
|
1476
|
+
@model.create(:name => "#{@model} #{n}")
|
1477
|
+
end
|
1478
|
+
end
|
1479
|
+
|
1480
|
+
subject { @query.send(method, @other) }
|
1481
|
+
|
1482
|
+
describe 'with other matching everything' do
|
1483
|
+
before do
|
1484
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
1485
|
+
@other = DataMapper::Query.new(@repository, @model)
|
1486
|
+
|
1487
|
+
@expected = DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
1488
|
+
end
|
1489
|
+
|
1490
|
+
it { should be_kind_of(DataMapper::Query) }
|
1491
|
+
|
1492
|
+
it { should_not equal(@query) }
|
1493
|
+
|
1494
|
+
it { should_not equal(@other) }
|
1495
|
+
|
1496
|
+
it 'should factor out the operation matching everything' do
|
1497
|
+
subject.conditions.should == @expected
|
1498
|
+
end
|
1499
|
+
end
|
1500
|
+
|
1501
|
+
describe 'with self matching everything' do
|
1502
|
+
before do
|
1503
|
+
@query = DataMapper::Query.new(@repository, @model)
|
1504
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
1505
|
+
|
1506
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:not,
|
1507
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
1508
|
+
)
|
1509
|
+
end
|
1510
|
+
|
1511
|
+
it { should be_kind_of(DataMapper::Query) }
|
1512
|
+
|
1513
|
+
it { should_not equal(@query) }
|
1514
|
+
|
1515
|
+
it { should_not equal(@other) }
|
1516
|
+
|
1517
|
+
it 'should factor out the operation matching everything, and negate the other' do
|
1518
|
+
subject.conditions.should == @expected
|
1519
|
+
end
|
1520
|
+
end
|
1521
|
+
|
1522
|
+
describe 'with self having a limit' do
|
1523
|
+
before do
|
1524
|
+
@query = DataMapper::Query.new(@repository, @model, :limit => 5)
|
1525
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
1526
|
+
|
1527
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
1528
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@query.merge(:fields => @key))),
|
1529
|
+
DataMapper::Query::Conditions::Operation.new(:not,
|
1530
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
1531
|
+
)
|
1532
|
+
)
|
1533
|
+
end
|
1534
|
+
|
1535
|
+
it { should_not equal(@query) }
|
1536
|
+
|
1537
|
+
it { should_not equal(@other) }
|
1538
|
+
|
1539
|
+
it 'should put each query into a subquery and AND them together, and negate the other' do
|
1540
|
+
subject.conditions.should == @expected
|
1541
|
+
end
|
1542
|
+
end
|
1543
|
+
|
1544
|
+
describe 'with other having a limit' do
|
1545
|
+
before do
|
1546
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
1547
|
+
@other = DataMapper::Query.new(@repository, @model, :limit => 5)
|
1548
|
+
|
1549
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
1550
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb'),
|
1551
|
+
DataMapper::Query::Conditions::Operation.new(:not,
|
1552
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@other.merge(:fields => @key)))
|
1553
|
+
)
|
1554
|
+
)
|
1555
|
+
end
|
1556
|
+
|
1557
|
+
it { should_not equal(@query) }
|
1558
|
+
|
1559
|
+
it { should_not equal(@other) }
|
1560
|
+
|
1561
|
+
it 'should put each query into a subquery and AND them together, and negate the other' do
|
1562
|
+
subject.conditions.should == @expected
|
1563
|
+
end
|
1564
|
+
end
|
1565
|
+
|
1566
|
+
describe 'with self having an offset > 0' do
|
1567
|
+
before do
|
1568
|
+
@query = DataMapper::Query.new(@repository, @model, :offset => 5, :limit => 5)
|
1569
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
1570
|
+
|
1571
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
1572
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@query.merge(:fields => @key))),
|
1573
|
+
DataMapper::Query::Conditions::Operation.new(:not,
|
1574
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
1575
|
+
)
|
1576
|
+
)
|
1577
|
+
end
|
1578
|
+
|
1579
|
+
it { should_not equal(@query) }
|
1580
|
+
|
1581
|
+
it { should_not equal(@other) }
|
1582
|
+
|
1583
|
+
it 'should put each query into a subquery and AND them together, and negate the other' do
|
1584
|
+
subject.conditions.should == @expected
|
1585
|
+
end
|
1586
|
+
end
|
1587
|
+
|
1588
|
+
describe 'with other having an offset > 0' do
|
1589
|
+
before do
|
1590
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
1591
|
+
@other = DataMapper::Query.new(@repository, @model, :offset => 5, :limit => 5)
|
1592
|
+
|
1593
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
1594
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb'),
|
1595
|
+
DataMapper::Query::Conditions::Operation.new(:not,
|
1596
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@other.merge(:fields => @key)))
|
1597
|
+
)
|
1598
|
+
)
|
1599
|
+
end
|
1600
|
+
|
1601
|
+
it { should_not equal(@query) }
|
1602
|
+
|
1603
|
+
it { should_not equal(@other) }
|
1604
|
+
|
1605
|
+
it 'should put each query into a subquery and AND them together, and negate the other' do
|
1606
|
+
subject.conditions.should == @expected
|
1607
|
+
end
|
1608
|
+
end
|
1609
|
+
|
1610
|
+
describe 'with self having links' do
|
1611
|
+
before :all do
|
1612
|
+
@do_adapter = defined?(DataMapper::Adapters::DataObjectsAdapter) && @adapter.kind_of?(DataMapper::Adapters::DataObjectsAdapter)
|
1613
|
+
end
|
1614
|
+
|
1615
|
+
before do
|
1616
|
+
@query = DataMapper::Query.new(@repository, @model, :links => [ :referrer ])
|
1617
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
1618
|
+
|
1619
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
1620
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@query.merge(:fields => @key))),
|
1621
|
+
DataMapper::Query::Conditions::Operation.new(:not,
|
1622
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
1623
|
+
)
|
1624
|
+
)
|
1625
|
+
end
|
1626
|
+
|
1627
|
+
it { should_not equal(@query) }
|
1628
|
+
|
1629
|
+
it { should_not equal(@other) }
|
1630
|
+
|
1631
|
+
it 'should put each query into a subquery and AND them together, and negate the other query' do
|
1632
|
+
pending_if 'TODO: Fix once table aliasing works', @do_adapter do
|
1633
|
+
subject.conditions.should == @expected
|
1634
|
+
end
|
1635
|
+
end
|
1636
|
+
end
|
1637
|
+
|
1638
|
+
describe 'with other having links' do
|
1639
|
+
before :all do
|
1640
|
+
@do_adapter = defined?(DataMapper::Adapters::DataObjectsAdapter) && @adapter.kind_of?(DataMapper::Adapters::DataObjectsAdapter)
|
1641
|
+
end
|
1642
|
+
|
1643
|
+
before do
|
1644
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
1645
|
+
@other = DataMapper::Query.new(@repository, @model, :links => [ :referrer ])
|
1646
|
+
|
1647
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
1648
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb'),
|
1649
|
+
DataMapper::Query::Conditions::Operation.new(:not,
|
1650
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@other.merge(:fields => @key)))
|
1651
|
+
)
|
1652
|
+
)
|
1653
|
+
end
|
1654
|
+
|
1655
|
+
it { should_not equal(@query) }
|
1656
|
+
|
1657
|
+
it { should_not equal(@other) }
|
1658
|
+
|
1659
|
+
it 'should put each query into a subquery and AND them together, and negate the other query' do
|
1660
|
+
pending_if 'TODO: Fix once table aliasing works', @do_adapter do
|
1661
|
+
subject.conditions.should == @expected
|
1662
|
+
end
|
1663
|
+
end
|
1664
|
+
end
|
1665
|
+
|
1666
|
+
describe 'with different conditions, no links/offset/limit' do
|
1667
|
+
before do
|
1668
|
+
property = @model.properties[:name]
|
1669
|
+
|
1670
|
+
@query = DataMapper::Query.new(@repository, @model, property.name => 'Dan Kubb')
|
1671
|
+
@other = DataMapper::Query.new(@repository, @model, property.name => 'John Doe')
|
1672
|
+
|
1673
|
+
@query.conditions.should_not == @other.conditions
|
1674
|
+
|
1675
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
1676
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, property, 'Dan Kubb'),
|
1677
|
+
DataMapper::Query::Conditions::Operation.new(:not,
|
1678
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, property, 'John Doe')
|
1679
|
+
)
|
1680
|
+
)
|
1681
|
+
end
|
1682
|
+
|
1683
|
+
it { should be_kind_of(DataMapper::Query) }
|
1684
|
+
|
1685
|
+
it { should_not equal(@query) }
|
1686
|
+
|
1687
|
+
it { should_not equal(@other) }
|
1688
|
+
|
1689
|
+
it 'should AND the conditions together, and negate the other query' do
|
1690
|
+
subject.conditions.should == @expected
|
1691
|
+
end
|
1692
|
+
end
|
1693
|
+
|
1694
|
+
describe 'with different fields' do
|
1695
|
+
before do
|
1696
|
+
@property = @model.properties[:name]
|
1697
|
+
|
1698
|
+
@query = DataMapper::Query.new(@repository, @model)
|
1699
|
+
@other = DataMapper::Query.new(@repository, @model, :fields => [ @property ])
|
1700
|
+
|
1701
|
+
@query.fields.should_not == @other.fields
|
1702
|
+
end
|
1703
|
+
|
1704
|
+
it { should be_kind_of(DataMapper::Query) }
|
1705
|
+
|
1706
|
+
it { should_not equal(@query) }
|
1707
|
+
|
1708
|
+
it { should_not equal(@other) }
|
1709
|
+
|
1710
|
+
it { subject.conditions.should == DataMapper::Query::Conditions::Operation.new(:and) }
|
1711
|
+
|
1712
|
+
it 'should use the other fields' do
|
1713
|
+
subject.fields.should == [ @property ]
|
1714
|
+
end
|
1715
|
+
end
|
1716
|
+
|
1717
|
+
describe 'with different order' do
|
1718
|
+
before do
|
1719
|
+
@property = @model.properties[:name]
|
1720
|
+
|
1721
|
+
@query = DataMapper::Query.new(@repository, @model)
|
1722
|
+
@other = DataMapper::Query.new(@repository, @model, :order => [ DataMapper::Query::Direction.new(@property, :desc) ])
|
1723
|
+
|
1724
|
+
@query.order.should_not == @other.order
|
1725
|
+
end
|
1726
|
+
|
1727
|
+
it { should be_kind_of(DataMapper::Query) }
|
1728
|
+
|
1729
|
+
it { should_not equal(@query) }
|
1730
|
+
|
1731
|
+
it { should_not equal(@other) }
|
1732
|
+
|
1733
|
+
it { subject.conditions.should == DataMapper::Query::Conditions::Operation.new(:and) }
|
1734
|
+
|
1735
|
+
it 'should use the other order' do
|
1736
|
+
subject.order.should == [ DataMapper::Query::Direction.new(@property, :desc) ]
|
1737
|
+
end
|
1738
|
+
end
|
1739
|
+
|
1740
|
+
describe 'with different unique' do
|
1741
|
+
before do
|
1742
|
+
@query = DataMapper::Query.new(@repository, @model)
|
1743
|
+
@other = DataMapper::Query.new(@repository, @model, :unique => true)
|
1744
|
+
|
1745
|
+
@query.unique?.should_not == @other.unique?
|
1746
|
+
end
|
1747
|
+
|
1748
|
+
it { should be_kind_of(DataMapper::Query) }
|
1749
|
+
|
1750
|
+
it { should_not equal(@query) }
|
1751
|
+
|
1752
|
+
it { should_not equal(@other) }
|
1753
|
+
|
1754
|
+
it { subject.conditions.should == DataMapper::Query::Conditions::Operation.new(:and) }
|
1755
|
+
|
1756
|
+
it 'should use the other unique' do
|
1757
|
+
subject.unique?.should == true
|
1758
|
+
end
|
1759
|
+
end
|
1760
|
+
|
1761
|
+
describe 'with different add_reversed' do
|
1762
|
+
before do
|
1763
|
+
@query = DataMapper::Query.new(@repository, @model)
|
1764
|
+
@other = DataMapper::Query.new(@repository, @model, :add_reversed => true)
|
1765
|
+
|
1766
|
+
@query.add_reversed?.should_not == @other.add_reversed?
|
1767
|
+
end
|
1768
|
+
|
1769
|
+
it { should be_kind_of(DataMapper::Query) }
|
1770
|
+
|
1771
|
+
it { should_not equal(@query) }
|
1772
|
+
|
1773
|
+
it { should_not equal(@other) }
|
1774
|
+
|
1775
|
+
it { subject.conditions.should == DataMapper::Query::Conditions::Operation.new(:and) }
|
1776
|
+
|
1777
|
+
it 'should use the other add_reversed' do
|
1778
|
+
subject.add_reversed?.should == true
|
1779
|
+
end
|
1780
|
+
end
|
1781
|
+
|
1782
|
+
describe 'with different reload' do
|
1783
|
+
before do
|
1784
|
+
@query = DataMapper::Query.new(@repository, @model)
|
1785
|
+
@other = DataMapper::Query.new(@repository, @model, :reload => true)
|
1786
|
+
|
1787
|
+
@query.reload?.should_not == @other.reload?
|
1788
|
+
end
|
1789
|
+
|
1790
|
+
it { should be_kind_of(DataMapper::Query) }
|
1791
|
+
|
1792
|
+
it { should_not equal(@query) }
|
1793
|
+
|
1794
|
+
it { should_not equal(@other) }
|
1795
|
+
|
1796
|
+
it { subject.conditions.should == DataMapper::Query::Conditions::Operation.new(:and) }
|
1797
|
+
|
1798
|
+
it 'should use the other reload' do
|
1799
|
+
subject.reload?.should == true
|
1800
|
+
end
|
1801
|
+
end
|
1802
|
+
|
1803
|
+
describe 'with different models' do
|
1804
|
+
before { @other = DataMapper::Query.new(@repository, Other) }
|
1805
|
+
|
1806
|
+
it { method(:subject).should raise_error(ArgumentError) }
|
1807
|
+
end
|
1808
|
+
end
|
1809
|
+
end
|
1810
|
+
end
|
1811
|
+
|
1812
|
+
it { should respond_to(:dup) }
|
1422
1813
|
|
1423
1814
|
describe '#dup' do
|
1424
|
-
|
1815
|
+
before :all do
|
1816
|
+
@new = @query.dup
|
1817
|
+
end
|
1818
|
+
|
1819
|
+
it 'should return a Query' do
|
1820
|
+
@new.should be_kind_of(DataMapper::Query)
|
1821
|
+
end
|
1822
|
+
|
1823
|
+
it 'should not equal query' do
|
1824
|
+
@new.should_not equal(@query)
|
1825
|
+
end
|
1826
|
+
|
1827
|
+
it 'should eql query' do
|
1828
|
+
@new.should eql(@query)
|
1829
|
+
end
|
1830
|
+
|
1831
|
+
it 'should == query' do
|
1832
|
+
@new.should == @query
|
1833
|
+
end
|
1425
1834
|
end
|
1426
1835
|
|
1427
|
-
it {
|
1836
|
+
it { should respond_to(:eql?) }
|
1428
1837
|
|
1429
1838
|
describe '#eql?' do
|
1430
1839
|
describe 'when other is equal' do
|
@@ -1476,7 +1885,7 @@ describe DataMapper::Query do
|
|
1476
1885
|
end
|
1477
1886
|
end
|
1478
1887
|
|
1479
|
-
it {
|
1888
|
+
it { should respond_to(:fields) }
|
1480
1889
|
|
1481
1890
|
describe '#fields' do
|
1482
1891
|
before :all do
|
@@ -1490,10 +1899,10 @@ describe DataMapper::Query do
|
|
1490
1899
|
end
|
1491
1900
|
end
|
1492
1901
|
|
1493
|
-
it {
|
1902
|
+
it { should respond_to(:filter_records) }
|
1494
1903
|
|
1495
|
-
|
1496
|
-
|
1904
|
+
describe '#filter_records' do
|
1905
|
+
supported_by :all do
|
1497
1906
|
before :all do
|
1498
1907
|
@john = { 'name' => 'John Doe', 'referrer_name' => nil }
|
1499
1908
|
@sam = { 'name' => 'Sam Smoot', 'referrer_name' => nil }
|
@@ -1520,7 +1929,7 @@ describe DataMapper::Query do
|
|
1520
1929
|
end
|
1521
1930
|
end
|
1522
1931
|
|
1523
|
-
it {
|
1932
|
+
it { should respond_to(:inspect) }
|
1524
1933
|
|
1525
1934
|
describe '#inspect' do
|
1526
1935
|
before :all do
|
@@ -1544,12 +1953,363 @@ describe DataMapper::Query do
|
|
1544
1953
|
end
|
1545
1954
|
end
|
1546
1955
|
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1956
|
+
[ :intersection, :& ].each do |method|
|
1957
|
+
it { should respond_to(method) }
|
1958
|
+
|
1959
|
+
describe "##{method}" do
|
1960
|
+
supported_by :all do
|
1961
|
+
before :all do
|
1962
|
+
@key = @model.key(@repository.name)
|
1963
|
+
|
1964
|
+
@self_relationship = DataMapper::Associations::OneToMany::Relationship.new(
|
1965
|
+
:self,
|
1966
|
+
@model,
|
1967
|
+
@model,
|
1968
|
+
{
|
1969
|
+
:child_key => @key.map { |p| p.name },
|
1970
|
+
:parent_key => @key.map { |p| p.name },
|
1971
|
+
:child_repository_name => @repository,
|
1972
|
+
:parent_repository_name => @repository,
|
1973
|
+
}
|
1974
|
+
)
|
1975
|
+
|
1976
|
+
10.times do |n|
|
1977
|
+
@model.create(:name => "#{@model} #{n}")
|
1978
|
+
end
|
1979
|
+
end
|
1980
|
+
|
1981
|
+
subject { @query.send(method, @other) }
|
1982
|
+
|
1983
|
+
describe 'with equivalent query' do
|
1984
|
+
before { @other = @query.dup }
|
1985
|
+
|
1986
|
+
it { should be_kind_of(DataMapper::Query) }
|
1987
|
+
|
1988
|
+
it { should_not equal(@query) }
|
1989
|
+
|
1990
|
+
it { should_not equal(@other) }
|
1991
|
+
|
1992
|
+
it { should == @query }
|
1993
|
+
end
|
1994
|
+
|
1995
|
+
describe 'with other matching everything' do
|
1996
|
+
before do
|
1997
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
1998
|
+
@other = DataMapper::Query.new(@repository, @model)
|
1999
|
+
end
|
2000
|
+
|
2001
|
+
it { should be_kind_of(DataMapper::Query) }
|
2002
|
+
|
2003
|
+
it { should_not equal(@query) }
|
2004
|
+
|
2005
|
+
it { should_not equal(@other) }
|
2006
|
+
|
2007
|
+
it 'should factor out the operation matching everything' do
|
2008
|
+
pending 'TODO: compress Query#conditions for proper comparison' do
|
2009
|
+
should == DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
2010
|
+
end
|
2011
|
+
end
|
2012
|
+
end
|
2013
|
+
|
2014
|
+
describe 'with self matching everything' do
|
2015
|
+
before do
|
2016
|
+
@query = DataMapper::Query.new(@repository, @model)
|
2017
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
2018
|
+
end
|
2019
|
+
|
2020
|
+
it { should be_kind_of(DataMapper::Query) }
|
2021
|
+
|
2022
|
+
it { should_not equal(@query) }
|
2023
|
+
|
2024
|
+
it { should_not equal(@other) }
|
2025
|
+
|
2026
|
+
it 'should factor out the operation matching everything' do
|
2027
|
+
pending 'TODO: compress Query#conditions for proper comparison' do
|
2028
|
+
should == DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
2029
|
+
end
|
2030
|
+
end
|
2031
|
+
end
|
2032
|
+
|
2033
|
+
describe 'with self having a limit' do
|
2034
|
+
before do
|
2035
|
+
@query = DataMapper::Query.new(@repository, @model, :limit => 5)
|
2036
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
2037
|
+
|
2038
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
2039
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@query.merge(:fields => @key))),
|
2040
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
2041
|
+
)
|
2042
|
+
end
|
2043
|
+
|
2044
|
+
it { should_not equal(@query) }
|
2045
|
+
|
2046
|
+
it { should_not equal(@other) }
|
2047
|
+
|
2048
|
+
it 'should put each query into a subquery and AND them together' do
|
2049
|
+
subject.conditions.should == @expected
|
2050
|
+
end
|
2051
|
+
end
|
2052
|
+
|
2053
|
+
describe 'with other having a limit' do
|
2054
|
+
before do
|
2055
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
2056
|
+
@other = DataMapper::Query.new(@repository, @model, :limit => 5)
|
2057
|
+
|
2058
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
2059
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb'),
|
2060
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@other.merge(:fields => @key)))
|
2061
|
+
)
|
2062
|
+
end
|
2063
|
+
|
2064
|
+
it { should_not equal(@query) }
|
2065
|
+
|
2066
|
+
it { should_not equal(@other) }
|
2067
|
+
|
2068
|
+
it 'should put each query into a subquery and AND them together' do
|
2069
|
+
subject.conditions.should == @expected
|
2070
|
+
end
|
2071
|
+
end
|
2072
|
+
|
2073
|
+
describe 'with self having an offset > 0' do
|
2074
|
+
before do
|
2075
|
+
@query = DataMapper::Query.new(@repository, @model, :offset => 5, :limit => 5)
|
2076
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
2077
|
+
|
2078
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
2079
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@query.merge(:fields => @key))),
|
2080
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
2081
|
+
)
|
2082
|
+
end
|
2083
|
+
|
2084
|
+
it { should_not equal(@query) }
|
2085
|
+
|
2086
|
+
it { should_not equal(@other) }
|
2087
|
+
|
2088
|
+
it 'should put each query into a subquery and AND them together' do
|
2089
|
+
subject.conditions.should == @expected
|
2090
|
+
end
|
2091
|
+
end
|
2092
|
+
|
2093
|
+
describe 'with other having an offset > 0' do
|
2094
|
+
before do
|
2095
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
2096
|
+
@other = DataMapper::Query.new(@repository, @model, :offset => 5, :limit => 5)
|
2097
|
+
|
2098
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
2099
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb'),
|
2100
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@other.merge(:fields => @key)))
|
2101
|
+
)
|
2102
|
+
end
|
2103
|
+
|
2104
|
+
it { should_not equal(@query) }
|
2105
|
+
|
2106
|
+
it { should_not equal(@other) }
|
2107
|
+
|
2108
|
+
it 'should put each query into a subquery and AND them together' do
|
2109
|
+
subject.conditions.should == @expected
|
2110
|
+
end
|
2111
|
+
end
|
2112
|
+
|
2113
|
+
describe 'with self having links' do
|
2114
|
+
before :all do
|
2115
|
+
@do_adapter = defined?(DataMapper::Adapters::DataObjectsAdapter) && @adapter.kind_of?(DataMapper::Adapters::DataObjectsAdapter)
|
2116
|
+
end
|
2117
|
+
|
2118
|
+
before do
|
2119
|
+
@query = DataMapper::Query.new(@repository, @model, :links => [ :referrer ])
|
2120
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
2121
|
+
|
2122
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
2123
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@query.merge(:fields => @key))),
|
2124
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
2125
|
+
)
|
2126
|
+
end
|
2127
|
+
|
2128
|
+
it { should_not equal(@query) }
|
2129
|
+
|
2130
|
+
it { should_not equal(@other) }
|
2131
|
+
|
2132
|
+
it 'should put each query into a subquery and AND them together' do
|
2133
|
+
pending_if 'TODO: Fix once table aliasing works', @do_adapter do
|
2134
|
+
subject.conditions.should == @expected
|
2135
|
+
end
|
2136
|
+
end
|
2137
|
+
end
|
2138
|
+
|
2139
|
+
describe 'with other having links' do
|
2140
|
+
before :all do
|
2141
|
+
@do_adapter = defined?(DataMapper::Adapters::DataObjectsAdapter) && @adapter.kind_of?(DataMapper::Adapters::DataObjectsAdapter)
|
2142
|
+
end
|
2143
|
+
|
2144
|
+
before do
|
2145
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
2146
|
+
@other = DataMapper::Query.new(@repository, @model, :links => [ :referrer ])
|
2147
|
+
|
2148
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
2149
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb'),
|
2150
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@other.merge(:fields => @key)))
|
2151
|
+
)
|
2152
|
+
end
|
2153
|
+
|
2154
|
+
it { should_not equal(@query) }
|
2155
|
+
|
2156
|
+
it { should_not equal(@other) }
|
2157
|
+
|
2158
|
+
it 'should put each query into a subquery and AND them together' do
|
2159
|
+
pending_if 'TODO: Fix once table aliasing works', @do_adapter do
|
2160
|
+
subject.conditions.should == @expected
|
2161
|
+
end
|
2162
|
+
end
|
2163
|
+
end
|
2164
|
+
|
2165
|
+
describe 'with different conditions, no links/offset/limit' do
|
2166
|
+
before do
|
2167
|
+
property = @model.properties[:name]
|
2168
|
+
|
2169
|
+
@query = DataMapper::Query.new(@repository, @model, property.name => 'Dan Kubb')
|
2170
|
+
@other = DataMapper::Query.new(@repository, @model, property.name => 'John Doe')
|
2171
|
+
|
2172
|
+
@query.conditions.should_not == @other.conditions
|
2173
|
+
|
2174
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:and,
|
2175
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, property, 'Dan Kubb'),
|
2176
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, property, 'John Doe')
|
2177
|
+
)
|
2178
|
+
end
|
2179
|
+
|
2180
|
+
it { should be_kind_of(DataMapper::Query) }
|
2181
|
+
|
2182
|
+
it { should_not equal(@query) }
|
2183
|
+
|
2184
|
+
it { should_not equal(@other) }
|
2185
|
+
|
2186
|
+
it 'should AND the conditions together' do
|
2187
|
+
subject.conditions.should == @expected
|
2188
|
+
end
|
2189
|
+
end
|
2190
|
+
|
2191
|
+
describe 'with different fields' do
|
2192
|
+
before do
|
2193
|
+
@property = @model.properties[:name]
|
2194
|
+
|
2195
|
+
@query = DataMapper::Query.new(@repository, @model)
|
2196
|
+
@other = DataMapper::Query.new(@repository, @model, :fields => [ @property ])
|
2197
|
+
|
2198
|
+
@query.fields.should_not == @other.fields
|
2199
|
+
end
|
2200
|
+
|
2201
|
+
it { should be_kind_of(DataMapper::Query) }
|
2202
|
+
|
2203
|
+
it { should_not equal(@query) }
|
2204
|
+
|
2205
|
+
it { should_not equal(@other) }
|
2206
|
+
|
2207
|
+
it { subject.conditions.should be_nil }
|
2208
|
+
|
2209
|
+
it 'should use the other fields' do
|
2210
|
+
subject.fields.should == [ @property ]
|
2211
|
+
end
|
2212
|
+
end
|
2213
|
+
|
2214
|
+
describe 'with different order' do
|
2215
|
+
before do
|
2216
|
+
@property = @model.properties[:name]
|
2217
|
+
|
2218
|
+
@query = DataMapper::Query.new(@repository, @model)
|
2219
|
+
@other = DataMapper::Query.new(@repository, @model, :order => [ DataMapper::Query::Direction.new(@property, :desc) ])
|
2220
|
+
|
2221
|
+
@query.order.should_not == @other.order
|
2222
|
+
end
|
2223
|
+
|
2224
|
+
it { should be_kind_of(DataMapper::Query) }
|
2225
|
+
|
2226
|
+
it { should_not equal(@query) }
|
2227
|
+
|
2228
|
+
it { should_not equal(@other) }
|
2229
|
+
|
2230
|
+
it { subject.conditions.should be_nil }
|
2231
|
+
|
2232
|
+
it 'should use the other order' do
|
2233
|
+
subject.order.should == [ DataMapper::Query::Direction.new(@property, :desc) ]
|
2234
|
+
end
|
2235
|
+
end
|
2236
|
+
|
2237
|
+
describe 'with different unique' do
|
2238
|
+
before do
|
2239
|
+
@query = DataMapper::Query.new(@repository, @model)
|
2240
|
+
@other = DataMapper::Query.new(@repository, @model, :unique => true)
|
2241
|
+
|
2242
|
+
@query.unique?.should_not == @other.unique?
|
2243
|
+
end
|
2244
|
+
|
2245
|
+
it { should be_kind_of(DataMapper::Query) }
|
2246
|
+
|
2247
|
+
it { should_not equal(@query) }
|
2248
|
+
|
2249
|
+
it { should_not equal(@other) }
|
2250
|
+
|
2251
|
+
it { subject.conditions.should be_nil }
|
2252
|
+
|
2253
|
+
it 'should use the other unique' do
|
2254
|
+
subject.unique?.should == true
|
2255
|
+
end
|
2256
|
+
end
|
2257
|
+
|
2258
|
+
describe 'with different add_reversed' do
|
2259
|
+
before do
|
2260
|
+
@query = DataMapper::Query.new(@repository, @model)
|
2261
|
+
@other = DataMapper::Query.new(@repository, @model, :add_reversed => true)
|
2262
|
+
|
2263
|
+
@query.add_reversed?.should_not == @other.add_reversed?
|
2264
|
+
end
|
2265
|
+
|
2266
|
+
it { should be_kind_of(DataMapper::Query) }
|
2267
|
+
|
2268
|
+
it { should_not equal(@query) }
|
2269
|
+
|
2270
|
+
it { should_not equal(@other) }
|
2271
|
+
|
2272
|
+
it { subject.conditions.should be_nil }
|
2273
|
+
|
2274
|
+
it 'should use the other add_reversed' do
|
2275
|
+
subject.add_reversed?.should == true
|
2276
|
+
end
|
2277
|
+
end
|
2278
|
+
|
2279
|
+
describe 'with different reload' do
|
2280
|
+
before do
|
2281
|
+
@query = DataMapper::Query.new(@repository, @model)
|
2282
|
+
@other = DataMapper::Query.new(@repository, @model, :reload => true)
|
2283
|
+
|
2284
|
+
@query.reload?.should_not == @other.reload?
|
2285
|
+
end
|
2286
|
+
|
2287
|
+
it { should be_kind_of(DataMapper::Query) }
|
2288
|
+
|
2289
|
+
it { should_not equal(@query) }
|
2290
|
+
|
2291
|
+
it { should_not equal(@other) }
|
2292
|
+
|
2293
|
+
it 'should use the other reload' do
|
2294
|
+
subject.reload?.should == true
|
2295
|
+
end
|
2296
|
+
end
|
2297
|
+
|
2298
|
+
describe 'with different models' do
|
2299
|
+
before { @other = DataMapper::Query.new(@repository, Other) }
|
2300
|
+
|
2301
|
+
it { method(:subject).should raise_error(ArgumentError) }
|
2302
|
+
end
|
2303
|
+
end
|
2304
|
+
end
|
2305
|
+
end
|
2306
|
+
|
2307
|
+
it { should respond_to(:limit) }
|
2308
|
+
|
2309
|
+
describe '#limit' do
|
2310
|
+
before :all do
|
2311
|
+
@return = @query.limit
|
2312
|
+
end
|
1553
2313
|
|
1554
2314
|
it { @return.should be_kind_of(Integer) }
|
1555
2315
|
|
@@ -1558,10 +2318,10 @@ describe DataMapper::Query do
|
|
1558
2318
|
end
|
1559
2319
|
end
|
1560
2320
|
|
1561
|
-
it {
|
2321
|
+
it { should respond_to(:limit_records) }
|
1562
2322
|
|
1563
|
-
|
1564
|
-
|
2323
|
+
describe '#limit_records' do
|
2324
|
+
supported_by :all do
|
1565
2325
|
before :all do
|
1566
2326
|
@john = { 'name' => 'John Doe', 'referrer_name' => nil }
|
1567
2327
|
@sam = { 'name' => 'Sam Smoot', 'referrer_name' => nil }
|
@@ -1588,7 +2348,7 @@ describe DataMapper::Query do
|
|
1588
2348
|
end
|
1589
2349
|
end
|
1590
2350
|
|
1591
|
-
it {
|
2351
|
+
it { should respond_to(:links) }
|
1592
2352
|
|
1593
2353
|
describe '#links' do
|
1594
2354
|
before :all do
|
@@ -1600,10 +2360,10 @@ describe DataMapper::Query do
|
|
1600
2360
|
it { @return.should be_empty }
|
1601
2361
|
end
|
1602
2362
|
|
1603
|
-
it {
|
2363
|
+
it { should respond_to(:match_records) }
|
1604
2364
|
|
1605
|
-
|
1606
|
-
|
2365
|
+
describe '#match_records' do
|
2366
|
+
supported_by :all do
|
1607
2367
|
before :all do
|
1608
2368
|
@john = { 'name' => 'John Doe', 'referrer_name' => nil }
|
1609
2369
|
@sam = { 'name' => 'Sam Smoot', 'referrer_name' => nil }
|
@@ -1630,32 +2390,32 @@ describe DataMapper::Query do
|
|
1630
2390
|
end
|
1631
2391
|
end
|
1632
2392
|
|
1633
|
-
it {
|
2393
|
+
it { should respond_to(:merge) }
|
1634
2394
|
|
1635
2395
|
describe '#merge' do
|
1636
|
-
describe
|
1637
|
-
before
|
2396
|
+
describe 'with a Hash' do
|
2397
|
+
before do
|
1638
2398
|
@return = @query.merge({ :limit => 202 })
|
1639
2399
|
end
|
1640
2400
|
|
1641
|
-
it
|
2401
|
+
it 'does not affect the receiver' do
|
1642
2402
|
@query.options[:limit].should == 3
|
1643
2403
|
end
|
1644
2404
|
end
|
1645
2405
|
|
1646
|
-
describe
|
1647
|
-
before
|
2406
|
+
describe 'with a Query' do
|
2407
|
+
before do
|
1648
2408
|
@other = DataMapper::Query.new(@repository, @model, @options.update(@other_options))
|
1649
2409
|
@return = @query.merge(@other)
|
1650
2410
|
end
|
1651
2411
|
|
1652
|
-
it
|
2412
|
+
it 'does not affect the receiver' do
|
1653
2413
|
@query.options[:limit].should == 3
|
1654
2414
|
end
|
1655
2415
|
end
|
1656
2416
|
end
|
1657
2417
|
|
1658
|
-
it {
|
2418
|
+
it { should respond_to(:model) }
|
1659
2419
|
|
1660
2420
|
describe '#model' do
|
1661
2421
|
before :all do
|
@@ -1669,7 +2429,7 @@ describe DataMapper::Query do
|
|
1669
2429
|
end
|
1670
2430
|
end
|
1671
2431
|
|
1672
|
-
it {
|
2432
|
+
it { should respond_to(:offset) }
|
1673
2433
|
|
1674
2434
|
describe '#offset' do
|
1675
2435
|
before :all do
|
@@ -1683,7 +2443,7 @@ describe DataMapper::Query do
|
|
1683
2443
|
end
|
1684
2444
|
end
|
1685
2445
|
|
1686
|
-
it {
|
2446
|
+
it { should respond_to(:order) }
|
1687
2447
|
|
1688
2448
|
describe '#order' do
|
1689
2449
|
before :all do
|
@@ -1697,7 +2457,7 @@ describe DataMapper::Query do
|
|
1697
2457
|
end
|
1698
2458
|
end
|
1699
2459
|
|
1700
|
-
it {
|
2460
|
+
it { should respond_to(:raw?) }
|
1701
2461
|
|
1702
2462
|
describe '#raw?' do
|
1703
2463
|
describe 'when the query contains raw conditions' do
|
@@ -1705,15 +2465,15 @@ describe DataMapper::Query do
|
|
1705
2465
|
@query.update(:conditions => [ 'name = ?', 'Dan Kubb' ])
|
1706
2466
|
end
|
1707
2467
|
|
1708
|
-
it {
|
2468
|
+
it { should be_raw }
|
1709
2469
|
end
|
1710
2470
|
|
1711
2471
|
describe 'when the query does not contain raw conditions' do
|
1712
|
-
it {
|
2472
|
+
it { should_not be_raw }
|
1713
2473
|
end
|
1714
2474
|
end
|
1715
2475
|
|
1716
|
-
it {
|
2476
|
+
it { should respond_to(:relative) }
|
1717
2477
|
|
1718
2478
|
describe '#relative' do
|
1719
2479
|
describe 'with a Hash' do
|
@@ -1733,45 +2493,6 @@ describe DataMapper::Query do
|
|
1733
2493
|
end
|
1734
2494
|
end
|
1735
2495
|
|
1736
|
-
describe 'using a different repository as a Repository' do
|
1737
|
-
before :all do
|
1738
|
-
@repository = DataMapper::Repository.new(:other)
|
1739
|
-
@return = @query.relative(:repository => @repository)
|
1740
|
-
end
|
1741
|
-
|
1742
|
-
it { @return.should be_kind_of(DataMapper::Query) }
|
1743
|
-
|
1744
|
-
it 'should not return self' do
|
1745
|
-
@return.should_not equal(@original)
|
1746
|
-
end
|
1747
|
-
|
1748
|
-
it 'should set the repository' do
|
1749
|
-
@return.repository.should equal(@repository)
|
1750
|
-
end
|
1751
|
-
end
|
1752
|
-
|
1753
|
-
describe 'using a different repository as a Symbol' do
|
1754
|
-
before :all do
|
1755
|
-
@other_adapter = DataMapper.setup(:other, :adapter => :in_memory)
|
1756
|
-
|
1757
|
-
@return = @query.relative(:repository => :other)
|
1758
|
-
end
|
1759
|
-
|
1760
|
-
after :all do
|
1761
|
-
DataMapper::Repository.adapters.delete(@other_adapter.name)
|
1762
|
-
end
|
1763
|
-
|
1764
|
-
it { @return.should be_kind_of(DataMapper::Query) }
|
1765
|
-
|
1766
|
-
it 'should not return self' do
|
1767
|
-
@return.should_not equal(@original)
|
1768
|
-
end
|
1769
|
-
|
1770
|
-
it 'should set the repository' do
|
1771
|
-
@return.repository.should == DataMapper::Repository.new(:other)
|
1772
|
-
end
|
1773
|
-
end
|
1774
|
-
|
1775
2496
|
describe 'using different options' do
|
1776
2497
|
before :all do
|
1777
2498
|
@return = @query.relative(@other_options)
|
@@ -1884,7 +2605,7 @@ describe DataMapper::Query do
|
|
1884
2605
|
end
|
1885
2606
|
end
|
1886
2607
|
|
1887
|
-
it {
|
2608
|
+
it { should respond_to(:reload?) }
|
1888
2609
|
|
1889
2610
|
describe '#reload?' do
|
1890
2611
|
describe 'when the query should reload' do
|
@@ -1892,15 +2613,15 @@ describe DataMapper::Query do
|
|
1892
2613
|
@query.update(:reload => true)
|
1893
2614
|
end
|
1894
2615
|
|
1895
|
-
it {
|
2616
|
+
it { should be_reload }
|
1896
2617
|
end
|
1897
2618
|
|
1898
2619
|
describe 'when the query should not reload' do
|
1899
|
-
it {
|
2620
|
+
it { should_not be_reload }
|
1900
2621
|
end
|
1901
2622
|
end
|
1902
2623
|
|
1903
|
-
it {
|
2624
|
+
it { should respond_to(:repository) }
|
1904
2625
|
|
1905
2626
|
describe '#repository' do
|
1906
2627
|
before :all do
|
@@ -1914,7 +2635,7 @@ describe DataMapper::Query do
|
|
1914
2635
|
end
|
1915
2636
|
end
|
1916
2637
|
|
1917
|
-
it {
|
2638
|
+
it { should respond_to(:reverse) }
|
1918
2639
|
|
1919
2640
|
describe '#reverse' do
|
1920
2641
|
before :all do
|
@@ -1943,7 +2664,7 @@ describe DataMapper::Query do
|
|
1943
2664
|
end
|
1944
2665
|
end
|
1945
2666
|
|
1946
|
-
it {
|
2667
|
+
it { should respond_to(:reverse!) }
|
1947
2668
|
|
1948
2669
|
describe '#reverse!' do
|
1949
2670
|
before :all do
|
@@ -1960,7 +2681,7 @@ describe DataMapper::Query do
|
|
1960
2681
|
end
|
1961
2682
|
|
1962
2683
|
[ :slice, :[] ].each do |method|
|
1963
|
-
it {
|
2684
|
+
it { should respond_to(method) }
|
1964
2685
|
|
1965
2686
|
describe "##{method}" do
|
1966
2687
|
describe 'with a positive offset' do
|
@@ -2151,7 +2872,7 @@ describe DataMapper::Query do
|
|
2151
2872
|
end
|
2152
2873
|
end
|
2153
2874
|
|
2154
|
-
it {
|
2875
|
+
it { should respond_to(:slice!) }
|
2155
2876
|
|
2156
2877
|
describe '#slice!' do
|
2157
2878
|
describe 'with a positive offset' do
|
@@ -2341,10 +3062,10 @@ describe DataMapper::Query do
|
|
2341
3062
|
end
|
2342
3063
|
end
|
2343
3064
|
|
2344
|
-
it {
|
3065
|
+
it { should respond_to(:sort_records) }
|
2345
3066
|
|
2346
|
-
|
2347
|
-
|
3067
|
+
describe '#sort_records' do
|
3068
|
+
supported_by :all do
|
2348
3069
|
before :all do
|
2349
3070
|
@john = { 'name' => 'John Doe', 'referrer_name' => nil }
|
2350
3071
|
@sam = { 'name' => 'Sam Smoot', 'referrer_name' => nil }
|
@@ -2371,7 +3092,356 @@ describe DataMapper::Query do
|
|
2371
3092
|
end
|
2372
3093
|
end
|
2373
3094
|
|
2374
|
-
|
3095
|
+
[ :union, :|, :+ ].each do |method|
|
3096
|
+
it { should respond_to(method) }
|
3097
|
+
|
3098
|
+
describe "##{method}" do
|
3099
|
+
supported_by :all do
|
3100
|
+
before :all do
|
3101
|
+
@key = @model.key(@repository.name)
|
3102
|
+
|
3103
|
+
@self_relationship = DataMapper::Associations::OneToMany::Relationship.new(
|
3104
|
+
:self,
|
3105
|
+
@model,
|
3106
|
+
@model,
|
3107
|
+
{
|
3108
|
+
:child_key => @key.map { |p| p.name },
|
3109
|
+
:parent_key => @key.map { |p| p.name },
|
3110
|
+
:child_repository_name => @repository,
|
3111
|
+
:parent_repository_name => @repository,
|
3112
|
+
}
|
3113
|
+
)
|
3114
|
+
|
3115
|
+
10.times do |n|
|
3116
|
+
@model.create(:name => "#{@model} #{n}")
|
3117
|
+
end
|
3118
|
+
end
|
3119
|
+
|
3120
|
+
subject { @query.send(method, @other) }
|
3121
|
+
|
3122
|
+
describe 'with equivalent query' do
|
3123
|
+
before { @other = @query.dup }
|
3124
|
+
|
3125
|
+
it { should be_kind_of(DataMapper::Query) }
|
3126
|
+
|
3127
|
+
it { should_not equal(@query) }
|
3128
|
+
|
3129
|
+
it { should_not equal(@other) }
|
3130
|
+
|
3131
|
+
it { should == @query }
|
3132
|
+
end
|
3133
|
+
|
3134
|
+
describe 'with other matching everything' do
|
3135
|
+
before do
|
3136
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
3137
|
+
@other = DataMapper::Query.new(@repository, @model)
|
3138
|
+
end
|
3139
|
+
|
3140
|
+
it { should be_kind_of(DataMapper::Query) }
|
3141
|
+
|
3142
|
+
it { should_not equal(@query) }
|
3143
|
+
|
3144
|
+
it { should_not equal(@other) }
|
3145
|
+
|
3146
|
+
it 'should match everything' do
|
3147
|
+
should == DataMapper::Query.new(@repository, @model)
|
3148
|
+
end
|
3149
|
+
end
|
3150
|
+
|
3151
|
+
describe 'with self matching everything' do
|
3152
|
+
before do
|
3153
|
+
@query = DataMapper::Query.new(@repository, @model)
|
3154
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
3155
|
+
end
|
3156
|
+
|
3157
|
+
it { should be_kind_of(DataMapper::Query) }
|
3158
|
+
|
3159
|
+
it { should_not equal(@query) }
|
3160
|
+
|
3161
|
+
it { should_not equal(@other) }
|
3162
|
+
|
3163
|
+
it 'should match everything' do
|
3164
|
+
should == DataMapper::Query.new(@repository, @model)
|
3165
|
+
end
|
3166
|
+
end
|
3167
|
+
|
3168
|
+
describe 'with self having a limit' do
|
3169
|
+
before do
|
3170
|
+
@query = DataMapper::Query.new(@repository, @model, :limit => 5)
|
3171
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
3172
|
+
|
3173
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:or,
|
3174
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@query.merge(:fields => @key))),
|
3175
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
3176
|
+
)
|
3177
|
+
end
|
3178
|
+
|
3179
|
+
it { should_not equal(@query) }
|
3180
|
+
|
3181
|
+
it { should_not equal(@other) }
|
3182
|
+
|
3183
|
+
it 'should put each query into a subquery and OR them together' do
|
3184
|
+
subject.conditions.should == @expected
|
3185
|
+
end
|
3186
|
+
end
|
3187
|
+
|
3188
|
+
describe 'with other having a limit' do
|
3189
|
+
before do
|
3190
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
3191
|
+
@other = DataMapper::Query.new(@repository, @model, :limit => 5)
|
3192
|
+
|
3193
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:or,
|
3194
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb'),
|
3195
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@other.merge(:fields => @key)))
|
3196
|
+
)
|
3197
|
+
end
|
3198
|
+
|
3199
|
+
it { should_not equal(@query) }
|
3200
|
+
|
3201
|
+
it { should_not equal(@other) }
|
3202
|
+
|
3203
|
+
it 'should put each query into a subquery and OR them together' do
|
3204
|
+
subject.conditions.should == @expected
|
3205
|
+
end
|
3206
|
+
end
|
3207
|
+
|
3208
|
+
describe 'with self having an offset > 0' do
|
3209
|
+
before do
|
3210
|
+
@query = DataMapper::Query.new(@repository, @model, :offset => 5, :limit => 5)
|
3211
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
3212
|
+
|
3213
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:or,
|
3214
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@query.merge(:fields => @key))),
|
3215
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
3216
|
+
)
|
3217
|
+
end
|
3218
|
+
|
3219
|
+
it { should_not equal(@query) }
|
3220
|
+
|
3221
|
+
it { should_not equal(@other) }
|
3222
|
+
|
3223
|
+
it 'should put each query into a subquery and OR them together' do
|
3224
|
+
subject.conditions.should == @expected
|
3225
|
+
end
|
3226
|
+
end
|
3227
|
+
|
3228
|
+
describe 'with other having an offset > 0' do
|
3229
|
+
before do
|
3230
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
3231
|
+
@other = DataMapper::Query.new(@repository, @model, :offset => 5, :limit => 5)
|
3232
|
+
|
3233
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:or,
|
3234
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb'),
|
3235
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@other.merge(:fields => @key)))
|
3236
|
+
)
|
3237
|
+
end
|
3238
|
+
|
3239
|
+
it { should_not equal(@query) }
|
3240
|
+
|
3241
|
+
it { should_not equal(@other) }
|
3242
|
+
|
3243
|
+
it 'should put each query into a subquery and OR them together' do
|
3244
|
+
subject.conditions.should == @expected
|
3245
|
+
end
|
3246
|
+
end
|
3247
|
+
|
3248
|
+
describe 'with self having links' do
|
3249
|
+
before :all do
|
3250
|
+
@do_adapter = defined?(DataMapper::Adapters::DataObjectsAdapter) && @adapter.kind_of?(DataMapper::Adapters::DataObjectsAdapter)
|
3251
|
+
end
|
3252
|
+
|
3253
|
+
before do
|
3254
|
+
@query = DataMapper::Query.new(@repository, @model, :links => [ :referrer ])
|
3255
|
+
@other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
3256
|
+
|
3257
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:or,
|
3258
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@query.merge(:fields => @key))),
|
3259
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb')
|
3260
|
+
)
|
3261
|
+
end
|
3262
|
+
|
3263
|
+
it { should_not equal(@query) }
|
3264
|
+
|
3265
|
+
it { should_not equal(@other) }
|
3266
|
+
|
3267
|
+
it 'should put each query into a subquery and OR them together' do
|
3268
|
+
pending_if 'TODO: Fix once table aliasing works', @do_adapter do
|
3269
|
+
subject.conditions.should == @expected
|
3270
|
+
end
|
3271
|
+
end
|
3272
|
+
end
|
3273
|
+
|
3274
|
+
describe 'with other having links' do
|
3275
|
+
before :all do
|
3276
|
+
@do_adapter = defined?(DataMapper::Adapters::DataObjectsAdapter) && @adapter.kind_of?(DataMapper::Adapters::DataObjectsAdapter)
|
3277
|
+
end
|
3278
|
+
|
3279
|
+
before do
|
3280
|
+
@query = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb')
|
3281
|
+
@other = DataMapper::Query.new(@repository, @model, :links => [ :referrer ])
|
3282
|
+
|
3283
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:or,
|
3284
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, @model.properties[:name], 'Dan Kubb'),
|
3285
|
+
DataMapper::Query::Conditions::Comparison.new(:in, @self_relationship, @model.all(@other.merge(:fields => @key)))
|
3286
|
+
)
|
3287
|
+
end
|
3288
|
+
|
3289
|
+
it { should_not equal(@query) }
|
3290
|
+
|
3291
|
+
it { should_not equal(@other) }
|
3292
|
+
|
3293
|
+
it 'should put each query into a subquery and OR them together' do
|
3294
|
+
pending_if 'TODO: Fix once table aliasing works', @do_adapter do
|
3295
|
+
subject.conditions.should == @expected
|
3296
|
+
end
|
3297
|
+
end
|
3298
|
+
end
|
3299
|
+
|
3300
|
+
describe 'with different conditions, no links/offset/limit' do
|
3301
|
+
before do
|
3302
|
+
property = @model.properties[:name]
|
3303
|
+
|
3304
|
+
@query = DataMapper::Query.new(@repository, @model, property.name => 'Dan Kubb')
|
3305
|
+
@other = DataMapper::Query.new(@repository, @model, property.name => 'John Doe')
|
3306
|
+
|
3307
|
+
@query.conditions.should_not == @other.conditions
|
3308
|
+
|
3309
|
+
@expected = DataMapper::Query::Conditions::Operation.new(:or,
|
3310
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, property, 'Dan Kubb'),
|
3311
|
+
DataMapper::Query::Conditions::Comparison.new(:eql, property, 'John Doe')
|
3312
|
+
)
|
3313
|
+
end
|
3314
|
+
|
3315
|
+
it { should be_kind_of(DataMapper::Query) }
|
3316
|
+
|
3317
|
+
it { should_not equal(@query) }
|
3318
|
+
|
3319
|
+
it { should_not equal(@other) }
|
3320
|
+
|
3321
|
+
it 'should OR the conditions together' do
|
3322
|
+
subject.conditions.should == @expected
|
3323
|
+
end
|
3324
|
+
end
|
3325
|
+
|
3326
|
+
describe 'with different fields' do
|
3327
|
+
before do
|
3328
|
+
@property = @model.properties[:name]
|
3329
|
+
|
3330
|
+
@query = DataMapper::Query.new(@repository, @model)
|
3331
|
+
@other = DataMapper::Query.new(@repository, @model, :fields => [ @property ])
|
3332
|
+
|
3333
|
+
@query.fields.should_not == @other.fields
|
3334
|
+
end
|
3335
|
+
|
3336
|
+
it { should be_kind_of(DataMapper::Query) }
|
3337
|
+
|
3338
|
+
it { should_not equal(@query) }
|
3339
|
+
|
3340
|
+
it { should_not equal(@other) }
|
3341
|
+
|
3342
|
+
it { subject.conditions.should be_nil }
|
3343
|
+
|
3344
|
+
it 'should use the other fields' do
|
3345
|
+
subject.fields.should == [ @property ]
|
3346
|
+
end
|
3347
|
+
end
|
3348
|
+
|
3349
|
+
describe 'with different order' do
|
3350
|
+
before do
|
3351
|
+
@property = @model.properties[:name]
|
3352
|
+
|
3353
|
+
@query = DataMapper::Query.new(@repository, @model)
|
3354
|
+
@other = DataMapper::Query.new(@repository, @model, :order => [ DataMapper::Query::Direction.new(@property, :desc) ])
|
3355
|
+
|
3356
|
+
@query.order.should_not == @other.order
|
3357
|
+
end
|
3358
|
+
|
3359
|
+
it { should be_kind_of(DataMapper::Query) }
|
3360
|
+
|
3361
|
+
it { should_not equal(@query) }
|
3362
|
+
|
3363
|
+
it { should_not equal(@other) }
|
3364
|
+
|
3365
|
+
it { subject.conditions.should be_nil }
|
3366
|
+
|
3367
|
+
it 'should use the other order' do
|
3368
|
+
subject.order.should == [ DataMapper::Query::Direction.new(@property, :desc) ]
|
3369
|
+
end
|
3370
|
+
end
|
3371
|
+
|
3372
|
+
describe 'with different unique' do
|
3373
|
+
before do
|
3374
|
+
@query = DataMapper::Query.new(@repository, @model)
|
3375
|
+
@other = DataMapper::Query.new(@repository, @model, :unique => true)
|
3376
|
+
|
3377
|
+
@query.unique?.should_not == @other.unique?
|
3378
|
+
end
|
3379
|
+
|
3380
|
+
it { should be_kind_of(DataMapper::Query) }
|
3381
|
+
|
3382
|
+
it { should_not equal(@query) }
|
3383
|
+
|
3384
|
+
it { should_not equal(@other) }
|
3385
|
+
|
3386
|
+
it { subject.conditions.should be_nil }
|
3387
|
+
|
3388
|
+
it 'should use the other unique' do
|
3389
|
+
subject.unique?.should == true
|
3390
|
+
end
|
3391
|
+
end
|
3392
|
+
|
3393
|
+
describe 'with different add_reversed' do
|
3394
|
+
before do
|
3395
|
+
@query = DataMapper::Query.new(@repository, @model)
|
3396
|
+
@other = DataMapper::Query.new(@repository, @model, :add_reversed => true)
|
3397
|
+
|
3398
|
+
@query.add_reversed?.should_not == @other.add_reversed?
|
3399
|
+
end
|
3400
|
+
|
3401
|
+
it { should be_kind_of(DataMapper::Query) }
|
3402
|
+
|
3403
|
+
it { should_not equal(@query) }
|
3404
|
+
|
3405
|
+
it { should_not equal(@other) }
|
3406
|
+
|
3407
|
+
it { subject.conditions.should be_nil }
|
3408
|
+
|
3409
|
+
it 'should use the other add_reversed' do
|
3410
|
+
subject.add_reversed?.should == true
|
3411
|
+
end
|
3412
|
+
end
|
3413
|
+
|
3414
|
+
describe 'with different reload' do
|
3415
|
+
before do
|
3416
|
+
@query = DataMapper::Query.new(@repository, @model)
|
3417
|
+
@other = DataMapper::Query.new(@repository, @model, :reload => true)
|
3418
|
+
|
3419
|
+
@query.reload?.should_not == @other.reload?
|
3420
|
+
end
|
3421
|
+
|
3422
|
+
it { should be_kind_of(DataMapper::Query) }
|
3423
|
+
|
3424
|
+
it { should_not equal(@query) }
|
3425
|
+
|
3426
|
+
it { should_not equal(@other) }
|
3427
|
+
|
3428
|
+
it { subject.conditions.should be_nil }
|
3429
|
+
|
3430
|
+
it 'should use the other reload' do
|
3431
|
+
subject.reload?.should == true
|
3432
|
+
end
|
3433
|
+
end
|
3434
|
+
|
3435
|
+
describe 'with different models' do
|
3436
|
+
before { @other = DataMapper::Query.new(@repository, Other) }
|
3437
|
+
|
3438
|
+
it { method(:subject).should raise_error(ArgumentError) }
|
3439
|
+
end
|
3440
|
+
end
|
3441
|
+
end
|
3442
|
+
end
|
3443
|
+
|
3444
|
+
it { should respond_to(:unique?) }
|
2375
3445
|
|
2376
3446
|
describe '#unique?' do
|
2377
3447
|
describe 'when the query is unique' do
|
@@ -2379,15 +3449,33 @@ describe DataMapper::Query do
|
|
2379
3449
|
@query.update(:unique => true)
|
2380
3450
|
end
|
2381
3451
|
|
2382
|
-
it {
|
3452
|
+
it { should be_unique }
|
2383
3453
|
end
|
2384
3454
|
|
2385
3455
|
describe 'when the query is not unique' do
|
2386
|
-
it {
|
3456
|
+
it { should_not be_unique }
|
3457
|
+
end
|
3458
|
+
|
3459
|
+
describe 'when links are provided, but unique is not specified' do
|
3460
|
+
before :all do
|
3461
|
+
@query.should_not be_unique
|
3462
|
+
@query.update(:links => [ :referrer ])
|
3463
|
+
end
|
3464
|
+
|
3465
|
+
it { should be_unique }
|
3466
|
+
end
|
3467
|
+
|
3468
|
+
describe 'when links are provided, but unique is false' do
|
3469
|
+
before :all do
|
3470
|
+
@query.should_not be_unique
|
3471
|
+
@query.update(:links => [ :referrer ], :unique => false)
|
3472
|
+
end
|
3473
|
+
|
3474
|
+
it { should_not be_unique }
|
2387
3475
|
end
|
2388
3476
|
end
|
2389
3477
|
|
2390
|
-
it {
|
3478
|
+
it { should respond_to(:update) }
|
2391
3479
|
|
2392
3480
|
describe '#update' do
|
2393
3481
|
describe 'with a Query' do
|
@@ -2406,20 +3494,21 @@ describe DataMapper::Query do
|
|
2406
3494
|
describe 'that has conditions set' do
|
2407
3495
|
before :all do
|
2408
3496
|
@and_operation = DataMapper::Query::Conditions::Operation.new(:and)
|
2409
|
-
@or_operation
|
3497
|
+
@or_operation = DataMapper::Query::Conditions::Operation.new(:or)
|
2410
3498
|
|
2411
|
-
@and_operation << DataMapper::Query::Conditions::Comparison.new(:eql,User.name,
|
2412
|
-
@and_operation << DataMapper::Query::Conditions::Comparison.new(:eql,User.citizenship,
|
3499
|
+
@and_operation << DataMapper::Query::Conditions::Comparison.new(:eql, User.properties[:name], 'Dan Kubb')
|
3500
|
+
@and_operation << DataMapper::Query::Conditions::Comparison.new(:eql, User.properties[:citizenship],'Canada')
|
2413
3501
|
|
2414
|
-
@or_operation << DataMapper::Query::Conditions::Comparison.new(:eql,User.name,
|
2415
|
-
@or_operation << DataMapper::Query::Conditions::Comparison.new(:eql,User.citizenship,
|
2416
|
-
|
2417
|
-
@
|
3502
|
+
@or_operation << DataMapper::Query::Conditions::Comparison.new(:eql, User.properties[:name], 'Ted Han')
|
3503
|
+
@or_operation << DataMapper::Query::Conditions::Comparison.new(:eql, User.properties[:citizenship], 'USA')
|
3504
|
+
|
3505
|
+
@query_one = DataMapper::Query.new(@repository, @model, :conditions => @and_operation)
|
3506
|
+
@query_two = DataMapper::Query.new(@repository, @model, :conditions => @or_operation)
|
2418
3507
|
|
2419
3508
|
@conditions = @query_one.merge(@query_two).conditions
|
2420
3509
|
end
|
2421
3510
|
|
2422
|
-
it { @conditions.should == (@and_operation
|
3511
|
+
it { @conditions.should == DataMapper::Query::Conditions::Operation.new(:and, @and_operation, @or_operation) }
|
2423
3512
|
end
|
2424
3513
|
|
2425
3514
|
describe 'that is for an ancestor model' do
|
@@ -2612,13 +3701,65 @@ describe DataMapper::Query do
|
|
2612
3701
|
|
2613
3702
|
it 'should update the conditions' do
|
2614
3703
|
@return.conditions.should == DataMapper::Query::Conditions::Operation.new(
|
2615
|
-
|
2616
|
-
|
2617
|
-
|
2618
|
-
|
2619
|
-
|
3704
|
+
:and,
|
3705
|
+
DataMapper::Query::Conditions::Comparison.new(
|
3706
|
+
:eql,
|
3707
|
+
@model.properties[:name],
|
3708
|
+
@options[:name]
|
3709
|
+
)
|
3710
|
+
)
|
3711
|
+
end
|
3712
|
+
end
|
3713
|
+
|
3714
|
+
describe 'using raw conditions' do
|
3715
|
+
before :all do
|
3716
|
+
@query.update(:conditions => [ 'name IS NOT NULL' ])
|
3717
|
+
|
3718
|
+
@return = @query.update(:conditions => [ 'name = ?', 'Dan Kubb' ])
|
3719
|
+
end
|
3720
|
+
|
3721
|
+
it { @return.should be_kind_of(DataMapper::Query) }
|
3722
|
+
|
3723
|
+
it { @return.should equal(@original) }
|
3724
|
+
|
3725
|
+
it 'should update the conditions' do
|
3726
|
+
@return.conditions.should == DataMapper::Query::Conditions::Operation.new(
|
3727
|
+
:and,
|
3728
|
+
[ 'name IS NOT NULL' ],
|
3729
|
+
[ 'name = ?', [ 'Dan Kubb' ] ]
|
2620
3730
|
)
|
2621
|
-
|
3731
|
+
end
|
3732
|
+
end
|
3733
|
+
|
3734
|
+
describe 'with the String key mapping to a Query::Path' do
|
3735
|
+
before :all do
|
3736
|
+
@query.links.should be_empty
|
3737
|
+
|
3738
|
+
@options = { 'grandparents.name' => 'Dan Kubb' }
|
3739
|
+
|
3740
|
+
@return = @query.update(@options)
|
3741
|
+
end
|
3742
|
+
|
3743
|
+
it { @return.should be_kind_of(DataMapper::Query) }
|
3744
|
+
|
3745
|
+
it 'should set the conditions' do
|
3746
|
+
@return.conditions.should ==
|
3747
|
+
DataMapper::Query::Conditions::Operation.new(
|
3748
|
+
:and,
|
3749
|
+
DataMapper::Query::Conditions::Comparison.new(
|
3750
|
+
:eql,
|
3751
|
+
@model.grandparents.name,
|
3752
|
+
'Dan Kubb'
|
3753
|
+
)
|
3754
|
+
)
|
3755
|
+
end
|
3756
|
+
|
3757
|
+
it 'should set the links' do
|
3758
|
+
@return.links.should == [ @model.relationships[:referrals], @model.relationships[:referrer] ]
|
3759
|
+
end
|
3760
|
+
|
3761
|
+
it 'should be valid' do
|
3762
|
+
@return.should be_valid
|
2622
3763
|
end
|
2623
3764
|
end
|
2624
3765
|
end
|