aboutsummaryrefslogtreecommitdiffstats
path: root/spec/attributes/attribute_spec.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-09-29 13:39:37 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-09-29 13:39:37 -0700
commitc8f83d61a113ae3c01005d2ac02e4c17891d918c (patch)
tree1cdd7fef7be6274731dc89569df52a600c4841e0 /spec/attributes/attribute_spec.rb
parent5ec2fac114b1c70fe7ac87f4767b6f1cbc1e78fc (diff)
parent5ca0c9a45788a14b9f454c93cd24fb0ae12a896b (diff)
downloadrails-c8f83d61a113ae3c01005d2ac02e4c17891d918c.tar.gz
rails-c8f83d61a113ae3c01005d2ac02e4c17891d918c.tar.bz2
rails-c8f83d61a113ae3c01005d2ac02e4c17891d918c.zip
Merge remote branch 'ernie/master'
* ernie/master: Make PostgreSQL play nice with its friends. (matches -> ILIKE instead of LIKE) Add support for remaining *_any/*_all attribute methods, and add matches/does_not_match/not_in Add eq_any.
Diffstat (limited to 'spec/attributes/attribute_spec.rb')
-rw-r--r--spec/attributes/attribute_spec.rb448
1 files changed, 448 insertions, 0 deletions
diff --git a/spec/attributes/attribute_spec.rb b/spec/attributes/attribute_spec.rb
index faef096792..c6d4fcebd4 100644
--- a/spec/attributes/attribute_spec.rb
+++ b/spec/attributes/attribute_spec.rb
@@ -28,6 +28,38 @@ module Arel
end
end
+ describe '#not_eq_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].not_eq_any([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].not_eq_any([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" != 1 OR "users"."id" != 2)
+ }
+ end
+ end
+
+ describe '#not_eq_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].not_eq_all([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].not_eq_all([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" != 1 AND "users"."id" != 2)
+ }
+ end
+ end
+
describe '#gt' do
it 'should create a GreaterThan node' do
relation = Table.new(:users)
@@ -44,6 +76,38 @@ module Arel
end
end
+ describe '#gt_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].gt_any([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].gt_any([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" > 1 OR "users"."id" > 2)
+ }
+ end
+ end
+
+ describe '#gt_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].gt_all([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].gt_all([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" > 1 AND "users"."id" > 2)
+ }
+ end
+ end
+
describe '#gteq' do
it 'should create a GreaterThanOrEqual node' do
relation = Table.new(:users)
@@ -60,6 +124,134 @@ module Arel
end
end
+ describe '#gteq_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].gteq_any([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].gteq_any([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" >= 1 OR "users"."id" >= 2)
+ }
+ end
+ end
+
+ describe '#gteq_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].gteq_all([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].gteq_all([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" >= 1 AND "users"."id" >= 2)
+ }
+ end
+ end
+
+ describe '#lt' do
+ it 'should create a LessThan node' do
+ relation = Table.new(:users)
+ relation[:id].lt(10).should be_kind_of Nodes::LessThan
+ end
+
+ it 'should generate < in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].lt(10)
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE "users"."id" < 10
+ }
+ end
+ end
+
+ describe '#lt_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].lt_any([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].lt_any([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" < 1 OR "users"."id" < 2)
+ }
+ end
+ end
+
+ describe '#lt_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].lt_all([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].lt_all([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" < 1 AND "users"."id" < 2)
+ }
+ end
+ end
+
+ describe '#lteq' do
+ it 'should create a LessThanOrEqual node' do
+ relation = Table.new(:users)
+ relation[:id].lteq(10).should be_kind_of Nodes::LessThanOrEqual
+ end
+
+ it 'should generate <= in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].lteq(10)
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE "users"."id" <= 10
+ }
+ end
+ end
+
+ describe '#lteq_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].lteq_any([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].lteq_any([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" <= 1 OR "users"."id" <= 2)
+ }
+ end
+ end
+
+ describe '#lteq_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].lteq_all([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].lteq_all([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" <= 1 AND "users"."id" <= 2)
+ }
+ end
+ end
+
describe '#average' do
it 'should create a AVG node' do
relation = Table.new(:users)
@@ -140,6 +332,152 @@ module Arel
check equality.right.should == 1
equality.should be_kind_of Nodes::Equality
end
+
+ it 'should generate = in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].eq(10)
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE "users"."id" = 10
+ }
+ end
+
+ it 'should handle nil' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].eq(nil)
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE "users"."id" IS NULL
+ }
+ end
+ end
+
+ describe '#eq_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].eq_any([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].eq_any([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" = 1 OR "users"."id" = 2)
+ }
+ end
+ end
+
+ describe '#eq_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].eq_all([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].eq_all([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" = 1 AND "users"."id" = 2)
+ }
+ end
+ end
+
+ describe '#matches' do
+ it 'should create a Matches node' do
+ relation = Table.new(:users)
+ relation[:name].matches('%bacon%').should be_kind_of Nodes::Matches
+ end
+
+ it 'should generate LIKE in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:name].matches('%bacon%')
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE "users"."name" LIKE '%bacon%'
+ }
+ end
+ end
+
+ describe '#matches_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:name].matches_any(['%chunky%','%bacon%']).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:name].matches_any(['%chunky%','%bacon%'])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."name" LIKE '%chunky%' OR "users"."name" LIKE '%bacon%')
+ }
+ end
+ end
+
+ describe '#matches_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:name].matches_all(['%chunky%','%bacon%']).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:name].matches_all(['%chunky%','%bacon%'])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."name" LIKE '%chunky%' AND "users"."name" LIKE '%bacon%')
+ }
+ end
+ end
+
+ describe '#does_not_match' do
+ it 'should create a DoesNotMatch node' do
+ relation = Table.new(:users)
+ relation[:name].does_not_match('%bacon%').should be_kind_of Nodes::DoesNotMatch
+ end
+
+ it 'should generate NOT LIKE in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:name].does_not_match('%bacon%')
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE "users"."name" NOT LIKE '%bacon%'
+ }
+ end
+ end
+
+ describe '#does_not_match_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:name].does_not_match_any(['%chunky%','%bacon%']).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:name].does_not_match_any(['%chunky%','%bacon%'])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."name" NOT LIKE '%chunky%' OR "users"."name" NOT LIKE '%bacon%')
+ }
+ end
+ end
+
+ describe '#does_not_match_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:name].does_not_match_all(['%chunky%','%bacon%']).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:name].does_not_match_all(['%chunky%','%bacon%'])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."name" NOT LIKE '%chunky%' AND "users"."name" NOT LIKE '%bacon%')
+ }
+ end
end
describe '#in' do
@@ -152,6 +490,116 @@ module Arel
check node.left.should == attribute
check node.right.should == [1, 2, 3]
end
+
+ it 'should generate IN in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].in([1,2,3])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE "users"."id" IN (1, 2, 3)
+ }
+ end
+ end
+
+ describe '#in_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].in_any([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].in_any([[1,2], [3,4]])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" IN (1, 2) OR "users"."id" IN (3, 4))
+ }
+ end
+ end
+
+ describe '#in_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].in_all([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].in_all([[1,2], [3,4]])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" IN (1, 2) AND "users"."id" IN (3, 4))
+ }
+ end
+ end
+
+ describe '#not_in' do
+ it 'can be constructed with a list' do
+ end
+
+ it 'should return a NotIn node' do
+ attribute = Attribute.new nil, nil, nil
+ node = Nodes::NotIn.new attribute, [1,2,3]
+ check node.left.should == attribute
+ check node.right.should == [1, 2, 3]
+ end
+
+ it 'should generate NOT IN in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].not_in([1,2,3])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE "users"."id" NOT IN (1, 2, 3)
+ }
+ end
+ end
+
+ describe '#not_in_any' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].not_in_any([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ORs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].not_in_any([[1,2], [3,4]])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" NOT IN (1, 2) OR "users"."id" NOT IN (3, 4))
+ }
+ end
+ end
+
+ describe '#not_in_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].not_in_all([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].not_in_all([[1,2], [3,4]])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" NOT IN (1, 2) AND "users"."id" NOT IN (3, 4))
+ }
+ end
+ end
+
+ describe '#eq_all' do
+ it 'should create a Grouping node' do
+ relation = Table.new(:users)
+ relation[:id].eq_all([1,2]).should be_kind_of Nodes::Grouping
+ end
+
+ it 'should generate ANDs in sql' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ mgr.where relation[:id].eq_all([1,2])
+ mgr.to_sql.should be_like %{
+ SELECT "users"."id" FROM "users" WHERE ("users"."id" = 1 AND "users"."id" = 2)
+ }
+ end
end
end