diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/active_relation/unit/primitives/attribute_spec.rb | 14 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/order_spec.rb | 42 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/selection_spec.rb | 45 |
3 files changed, 71 insertions, 30 deletions
diff --git a/spec/active_relation/unit/primitives/attribute_spec.rb b/spec/active_relation/unit/primitives/attribute_spec.rb index 2806d26330..0f6e5e289d 100644 --- a/spec/active_relation/unit/primitives/attribute_spec.rb +++ b/spec/active_relation/unit/primitives/attribute_spec.rb @@ -97,43 +97,43 @@ module ActiveRelation describe '#eq' do it "manufactures an equality predicate" do - @attribute.eq('name').should == Equality.new(@attribute, 'name') + @attribute.eq('name').should == Equality.new(@attribute, 'name'.bind(@relation)) end end describe '#lt' do it "manufactures a less-than predicate" do - @attribute.lt(10).should == LessThan.new(@attribute, 10) + @attribute.lt(10).should == LessThan.new(@attribute, 10.bind(@relation)) end end describe '#lteq' do it "manufactures a less-than or equal-to predicate" do - @attribute.lteq(10).should == LessThanOrEqualTo.new(@attribute, 10) + @attribute.lteq(10).should == LessThanOrEqualTo.new(@attribute, 10.bind(@relation)) end end describe '#gt' do it "manufactures a greater-than predicate" do - @attribute.gt(10).should == GreaterThan.new(@attribute, 10) + @attribute.gt(10).should == GreaterThan.new(@attribute, 10.bind(@relation)) end end describe '#gteq' do it "manufactures a greater-than or equal-to predicate" do - @attribute.gteq(10).should == GreaterThanOrEqualTo.new(@attribute, 10) + @attribute.gteq(10).should == GreaterThanOrEqualTo.new(@attribute, 10.bind(@relation)) end end describe '#matches' do it "manufactures a match predicate" do - @attribute.matches(/.*/).should == Match.new(@attribute, /.*/) + @attribute.matches(/.*/).should == Match.new(@attribute, /.*/.bind(@relation)) end end describe '#in' do it "manufactures an in predicate" do - @attribute.in(1..30).should == In.new(@attribute, 1..30) + @attribute.in(1..30).should == In.new(@attribute, (1..30).bind(@relation)) end end end diff --git a/spec/active_relation/unit/relations/order_spec.rb b/spec/active_relation/unit/relations/order_spec.rb index 6a9ce07024..f37a9f26e9 100644 --- a/spec/active_relation/unit/relations/order_spec.rb +++ b/spec/active_relation/unit/relations/order_spec.rb @@ -22,12 +22,42 @@ module ActiveRelation end describe '#to_sql' do - it "manufactures sql with an order clause" do - Order.new(@relation, @attribute).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - ORDER BY `users`.`id` - ") + describe "when given an attribute" do + it "manufactures sql with an order clause populated by the attribute" do + Order.new(@relation, @attribute).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + ORDER BY `users`.`id` + ") + end + end + + describe "when given multiple attributes" do + before do + @another_attribute = @relation[:name] + end + + it "manufactures sql with an order clause populated by comma-separated attributes" do + Order.new(@relation, @attribute, @another_attribute).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + ORDER BY `users`.`id`, `users`.`name` + ") + end + end + + describe "when given a string" do + before do + @string = "asdf".bind(@relation) + end + + it "passes the string through to the order clause" do + Order.new(@relation, @string).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + ORDER BY asdf + ") + end end end end diff --git a/spec/active_relation/unit/relations/selection_spec.rb b/spec/active_relation/unit/relations/selection_spec.rb index 4bb3817bf5..001c38c370 100644 --- a/spec/active_relation/unit/relations/selection_spec.rb +++ b/spec/active_relation/unit/relations/selection_spec.rb @@ -4,14 +4,17 @@ module ActiveRelation describe Selection do before do @relation = Table.new(:users) - @predicate = Equality.new(@relation[:id], 1.bind(@relation)) + @predicate = @relation[:id].eq(1) end describe '#initialize' do + before do + @another_predicate = @relation[:name].lt(2) + end + it "manufactures nested selection relations if multiple predicates are provided" do - @predicate2 = LessThan.new(@relation[:age], 2.bind(@relation)) - Selection.new(@relation, @predicate, @predicate2). \ - should == Selection.new(Selection.new(@relation, @predicate2), @predicate) + Selection.new(@relation, @predicate, @another_predicate). \ + should == Selection.new(Selection.new(@relation, @another_predicate), @predicate) end end @@ -30,20 +33,28 @@ module ActiveRelation end describe '#to_sql' do - it "manufactures sql with where clause conditions" do - Selection.new(@relation, @predicate).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - WHERE `users`.`id` = 1 - ") + describe 'when given a predicate' do + it "manufactures sql with where clause conditions" do + Selection.new(@relation, @predicate).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + WHERE `users`.`id` = 1 + ") + end end - - it "allows arbitrary sql" do - Selection.new(@relation, "asdf".bind(@relation)).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - WHERE asdf - ") + + describe 'when given a string' do + before do + @string = "asdf".bind(@relation) + end + + it "passes the string through to the where clause" do + Selection.new(@relation, @string).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + WHERE asdf + ") + end end end end |