diff options
author | Ernie Miller <ernie@metautonomo.us> | 2010-05-05 14:12:22 -0400 |
---|---|---|
committer | Ernie Miller <ernie@metautonomo.us> | 2010-05-07 13:09:00 -0400 |
commit | 6249e5822a105719a09b7ccebc14336f37a8917c (patch) | |
tree | 07340df46650329bb96326ca45cbedbc8a6522dd /spec | |
parent | 0f68c734b1642c4960778f8924d2c752717e2790 (diff) | |
download | rails-6249e5822a105719a09b7ccebc14336f37a8917c.tar.gz rails-6249e5822a105719a09b7ccebc14336f37a8917c.tar.bz2 rails-6249e5822a105719a09b7ccebc14336f37a8917c.zip |
Tests for ranges with excluded ends and complements
Diffstat (limited to 'spec')
-rw-r--r-- | spec/engines/sql/unit/predicates/in_spec.rb | 22 | ||||
-rw-r--r-- | spec/shared/relation_spec.rb | 65 |
2 files changed, 82 insertions, 5 deletions
diff --git a/spec/engines/sql/unit/predicates/in_spec.rb b/spec/engines/sql/unit/predicates/in_spec.rb index 5d9b2cdcaa..f62ee6e829 100644 --- a/spec/engines/sql/unit/predicates/in_spec.rb +++ b/spec/engines/sql/unit/predicates/in_spec.rb @@ -99,6 +99,28 @@ module Arel end end end + + describe 'when relating to a range with an excluded end' do + before do + @range = 1...3 + end + + it 'manufactures sql with a >= and <' do + sql = In.new(@attribute, @range).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{(`users`.`id` >= 1 AND `users`.`id` < 3)}) + end + + adapter_is :oracle do + sql.should be_like(%Q{("USERS"."ID" >= 1 AND "USERS"."ID" < 3)}) + end + + adapter_is_not :mysql, :oracle do + sql.should be_like(%Q{("users"."id" >= 1 AND "users"."id" < 3)}) + end + end + end describe 'when relating to a time range' do before do diff --git a/spec/shared/relation_spec.rb b/spec/shared/relation_spec.rb index 0759700d8a..e0c74fc7ee 100644 --- a/spec/shared/relation_spec.rb +++ b/spec/shared/relation_spec.rb @@ -35,65 +35,120 @@ share_examples_for 'A Relation' do @relation.where(@relation[:age].eq(@pivot[@relation[:age]])).should have_rows(expected) end + it "finds rows with an equal to complement predicate" do + expected = @expected.select { |r| r[@relation[:age]] != @pivot[@relation[:age]] } + @relation.where(@relation[:age].eq(@pivot[@relation[:age]]).complement).should have_rows(expected) + end + it "finds rows with a not eq predicate" do expected = @expected.select { |r| r[@relation[:age]] != @pivot[@relation[:age]] } @relation.where(@relation[:age].not_eq(@pivot[@relation[:age]])).should have_rows(expected) end + + it "finds rows with an not eq complement predicate" do + expected = @expected.select { |r| r[@relation[:age]] == @pivot[@relation[:age]] } + @relation.where(@relation[:age].not_eq(@pivot[@relation[:age]]).complement).should have_rows(expected) + end it "finds rows with a less than predicate" do expected = @expected.select { |r| r[@relation[:age]] < @pivot[@relation[:age]] } @relation.where(@relation[:age].lt(@pivot[@relation[:age]])).should have_rows(expected) end + + it "finds rows with a less than complement predicate" do + expected = @expected.select { |r| r[@relation[:age]] >= @pivot[@relation[:age]] } + @relation.where(@relation[:age].lt(@pivot[@relation[:age]]).complement).should have_rows(expected) + end it "finds rows with a less than or equal to predicate" do expected = @expected.select { |r| r[@relation[:age]] <= @pivot[@relation[:age]] } @relation.where(@relation[:age].lteq(@pivot[@relation[:age]])).should have_rows(expected) end + + it "finds rows with a less than or equal to complement predicate" do + expected = @expected.select { |r| r[@relation[:age]] > @pivot[@relation[:age]] } + @relation.where(@relation[:age].lteq(@pivot[@relation[:age]]).complement).should have_rows(expected) + end it "finds rows with a greater than predicate" do expected = @expected.select { |r| r[@relation[:age]] > @pivot[@relation[:age]] } @relation.where(@relation[:age].gt(@pivot[@relation[:age]])).should have_rows(expected) end + + it "finds rows with a greater than complement predicate" do + expected = @expected.select { |r| r[@relation[:age]] <= @pivot[@relation[:age]] } + @relation.where(@relation[:age].gt(@pivot[@relation[:age]]).complement).should have_rows(expected) + end it "finds rows with a greater than or equal to predicate" do expected = @expected.select { |r| r[@relation[:age]] >= @pivot[@relation[:age]] } @relation.where(@relation[:age].gteq(@pivot[@relation[:age]])).should have_rows(expected) end + + it "finds rows with a greater than or equal to complement predicate" do + expected = @expected.select { |r| r[@relation[:age]] < @pivot[@relation[:age]] } + @relation.where(@relation[:age].gteq(@pivot[@relation[:age]]).complement).should have_rows(expected) + end it "finds rows with a matches predicate" do expected = @expected.select { |r| r[@relation[:name]] =~ /#{@pivot[@relation[:name]]}/ } @relation.where(@relation[:name].matches(/#{@pivot[@relation[:name]]}/)).should have_rows(expected) end + it "finds rows with a matches complement predicate" do + expected = @expected.select { |r| r[@relation[:name]] !~ /#{@pivot[@relation[:name]]}/ } + @relation.where(@relation[:name].matches(/#{@pivot[@relation[:name]]}/).complement).should have_rows(expected) + end + it "finds rows with a not matches predicate" do expected = @expected.select { |r| r[@relation[:name]] !~ /#{@pivot[@relation[:name]]}/ } @relation.where(@relation[:name].not_matches(/#{@pivot[@relation[:name]]}/)).should have_rows(expected) end + + it "finds rows with a not matches complement predicate" do + expected = @expected.select { |r| r[@relation[:name]] =~ /#{@pivot[@relation[:name]]}/ } + @relation.where(@relation[:name].not_matches(/#{@pivot[@relation[:name]]}/).complement).should have_rows(expected) + end it "finds rows with an in predicate" do expected = @expected.select {|r| r[@relation[:age]] >=3 && r[@relation[:age]] <= 20} @relation.where(@relation[:age].in(3..20)).should have_rows(expected) end + it "finds rows with an in complement predicate" do + expected = @expected.select {|r| !(r[@relation[:age]] >=3 && r[@relation[:age]] <= 20)} + @relation.where(@relation[:age].in(3..20).complement).should have_rows(expected) + end + it "finds rows with a not in predicate" do expected = @expected.select {|r| !(r[@relation[:age]] >=3 && r[@relation[:age]] <= 20)} @relation.where(@relation[:age].not_in(3..20)).should have_rows(expected) end - it "finds rows with a not predicate" do - expected = @expected.select {|r| !(r[@relation[:age]] >= 3 && r[@relation[:age]] <= 20)} - @relation.where(@relation[:age].in(3..20).not).should have_rows(expected) + it "finds rows with a not in complement predicate" do + expected = @expected.select {|r| r[@relation[:age]] >=3 && r[@relation[:age]] <= 20} + @relation.where(@relation[:age].not_in(3..20).complement).should have_rows(expected) end - it "finds rows with a grouped predicate of class Any" do + it "finds rows with a polyadic predicate of class Any" do expected = @expected.select {|r| [2,4,8,16].include?(r[@relation[:age]])} @relation.where(@relation[:age].in_any([2,4], [8, 16])).should have_rows(expected) end - it "finds rows with a grouped predicate of class All" do + it "finds rows with a polyadic predicate of class Any complement" do + expected = @expected.select {|r| ![2,4,8,16].include?(r[@relation[:age]])} + @relation.where(@relation[:age].in_any([2,4], [8, 16]).complement).should have_rows(expected) + end + + it "finds rows with a polyadic predicate of class All" do expected = @expected.select {|r| r[@relation[:name]] =~ /Name/ && r[@relation[:name]] =~ /1/} @relation.where(@relation[:name].matches_all(/Name/, /1/)).should have_rows(expected) end + + it "finds rows with a polyadic predicate of class All complement" do + expected = @expected.select {|r| !(r[@relation[:name]] =~ /Name/ && r[@relation[:name]] =~ /1/)} + @relation.where(@relation[:name].matches_all(/Name/, /1/).complement).should have_rows(expected) + end end describe "#order" do |