aboutsummaryrefslogtreecommitdiffstats
path: root/spec/active_relation/unit/predicates/binary_spec.rb
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-03-12 23:31:18 -0700
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-03-12 23:31:18 -0700
commitb1acebaaf0823c093853ade5700bbf5117b4f31a (patch)
treeafb68e6d23f032802854e396e5f988aa1642c4d3 /spec/active_relation/unit/predicates/binary_spec.rb
parent2654c29bfdb2ccddfed8cceaaaba06e46892bdb9 (diff)
downloadrails-b1acebaaf0823c093853ade5700bbf5117b4f31a.tar.gz
rails-b1acebaaf0823c093853ade5700bbf5117b4f31a.tar.bz2
rails-b1acebaaf0823c093853ade5700bbf5117b4f31a.zip
- renamed scalar to value
- added better test coverage and documentation of binary spec #to_sql
Diffstat (limited to 'spec/active_relation/unit/predicates/binary_spec.rb')
-rw-r--r--spec/active_relation/unit/predicates/binary_spec.rb96
1 files changed, 76 insertions, 20 deletions
diff --git a/spec/active_relation/unit/predicates/binary_spec.rb b/spec/active_relation/unit/predicates/binary_spec.rb
index d552d8b501..13b3f10a8c 100644
--- a/spec/active_relation/unit/predicates/binary_spec.rb
+++ b/spec/active_relation/unit/predicates/binary_spec.rb
@@ -3,17 +3,83 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
module ActiveRelation
describe Binary do
before do
- @relation1 = Table.new(:users)
- @relation2 = Table.new(:photos)
- @attribute1 = @relation1[:id]
- @attribute2 = @relation2[:id]
+ @relation = Table.new(:users)
+ @attribute1 = @relation[:id]
+ @attribute2 = @relation[:name]
+ @value = "1-asdf".bind(@relation)
class ConcreteBinary < Binary
def predicate_sql
"<=>"
end
end
end
-
+
+ describe '#to_sql' do
+ describe 'when relating two attributes' do
+ it 'manufactures sql with a binary operation' do
+ ConcreteBinary.new(@attribute1, @attribute2).to_sql.should be_like("
+ `users`.`id` <=> `users`.`name`
+ ")
+ end
+ end
+
+ describe 'when relating an attribute and a value' do
+ describe 'when relating to an integer attribute' do
+ it 'formats values as integers' do
+ ConcreteBinary.new(@attribute1, @value).to_sql.should be_like("
+ `users`.`id` <=> 1
+ ")
+ end
+ end
+
+ describe 'when relating to a string attribute' do
+ it 'formats values as strings' do
+ ConcreteBinary.new(@attribute2, @value).to_sql.should be_like("
+ `users`.`name` <=> '1-asdf'
+ ")
+ end
+ end
+ end
+
+ describe 'when relating two values' do
+ before do
+ @another_value = 2.bind(@relation)
+ end
+
+ it 'quotes values appropriate to their type' do
+ ConcreteBinary.new(string = @value, integer = @another_value).to_sql.should be_like("
+ '1-asdf' <=> 2
+ ")
+ end
+ end
+
+ describe 'when relating to an array' do
+ it 'manufactures sql with a list' do
+ pending
+ array = [1, 2, 3]
+ ConcreteBinary.new(@attribute1, array).to_sql.should be_like("
+ `users`.`id` <=> (1,2,3)
+ ")
+ end
+
+ it 'formats values in the array in the type of the attribute' do
+ pending
+ array = ['1-asdf', 2, 3]
+ ConcreteBinary.new(@attribute1, array).to_sql.should be_like("
+ `users`.`id` <=> (1,2,3)
+ ")
+ end
+ end
+
+ describe 'when relating to a relation' do
+ it 'manufactures sql with a subselect' do
+ ConcreteBinary.new(@attribute1, @relation).to_sql.should be_like("
+ `users`.`id` <=> (SELECT `users`.`id`, `users`.`name` FROM `users`)
+ ")
+ end
+ end
+ end
+
describe '==' do
it "obtains if attribute1 and attribute2 are identical" do
Binary.new(@attribute1, @attribute2).should == Binary.new(@attribute1, @attribute2)
@@ -41,23 +107,13 @@ module ActiveRelation
end
describe '#bind' do
- it "distributes over the predicates and attributes" do
- ConcreteBinary.new(@attribute1, @attribute2).bind(@relation2). \
- should == ConcreteBinary.new(@attribute1.bind(@relation2), @attribute2.bind(@relation2))
- end
- end
-
- describe '#to_sql' do
- it 'manufactures sql with a binary operation' do
- ConcreteBinary.new(@attribute1, @attribute2).to_sql.should be_like("
- `users`.`id` <=> `photos`.`id`
- ")
+ before do
+ @another_relation = Table.new(:photos)
end
- it 'appropriately quotes scalars' do
- ConcreteBinary.new(@attribute1, "1-asdf".bind(@relation1)).to_sql.should be_like("
- `users`.`id` <=> 1
- ")
+ it "descends" do
+ ConcreteBinary.new(@attribute1, @attribute2).bind(@another_relation). \
+ should == ConcreteBinary.new(@attribute1.bind(@another_relation), @attribute2.bind(@another_relation))
end
end
end