aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-03-16 16:53:49 -0700
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-03-16 16:53:49 -0700
commitaa5c9a19826c84bbb9c9f75f8d1a4b04b874780c (patch)
treec5a6720db91fed575f9fbd0b8de08de651def91c /spec
parent07f7f752fecb56316456f144c121e471fd0d0847 (diff)
downloadrails-aa5c9a19826c84bbb9c9f75f8d1a4b04b874780c.tar.gz
rails-aa5c9a19826c84bbb9c9f75f8d1a4b04b874780c.tar.bz2
rails-aa5c9a19826c84bbb9c9f75f8d1a4b04b874780c.zip
added support for `attribute IN ...` and `attribute BETWEEN ...`
- IN and BETWEEN are chosen depending on the type of the second operand - ranges (1..2), arrays ([1,2,3]), and relations ("SELECT * ...") are all supported
Diffstat (limited to 'spec')
-rw-r--r--spec/active_relation/unit/predicates/binary_spec.rb44
-rw-r--r--spec/active_relation/unit/predicates/in_spec.rb58
-rw-r--r--spec/active_relation/unit/primitives/attribute_spec.rb6
-rw-r--r--spec/active_relation/unit/relations/relation_spec.rb20
4 files changed, 82 insertions, 46 deletions
diff --git a/spec/active_relation/unit/predicates/binary_spec.rb b/spec/active_relation/unit/predicates/binary_spec.rb
index b63472a836..04f8d4f305 100644
--- a/spec/active_relation/unit/predicates/binary_spec.rb
+++ b/spec/active_relation/unit/predicates/binary_spec.rb
@@ -6,7 +6,6 @@ module ActiveRelation
@relation = Table.new(:users)
@attribute1 = @relation[:id]
@attribute2 = @relation[:name]
- @value = "1-asdf".bind(@relation)
class ConcreteBinary < Binary
def predicate_sql
"<=>"
@@ -24,6 +23,10 @@ module ActiveRelation
end
describe 'when relating an attribute and a value' do
+ before do
+ @value = "1-asdf".bind(@relation)
+ end
+
describe 'when relating to an integer attribute' do
it 'formats values as integers' do
ConcreteBinary.new(@attribute1, @value).to_sql.should be_like("
@@ -43,49 +46,16 @@ module ActiveRelation
describe 'when relating two values' do
before do
+ @value = "1-asdf".bind(@relation)
@another_value = 2.bind(@relation)
end
- it 'quotes values appropriate to their type' do
+ it 'formats values appropos of 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
- describe 'when the array\'s elements are the same type as the attribute' do
- before do
- @array = [1, 2, 3]
- end
-
- it 'manufactures sql with a comma separated list' do
- ConcreteBinary.new(@attribute1, @array.bind(@relation)).to_sql.should be_like("
- `users`.`id` <=> (1, 2, 3)
- ")
- end
- end
-
- describe 'when the array\'s elements are not same type as the attribute' do
- before do
- @array = ['1-asdf', 2, 3]
- end
-
- it 'formats values in the array as the type of the attribute' do
- ConcreteBinary.new(@attribute1, @array.bind(@relation)).to_sql.should be_like("
- `users`.`id` <=> (1, 2, 3)
- ")
- end
- 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
@@ -94,7 +64,7 @@ module ActiveRelation
Binary.new(@attribute1, @attribute2).should_not == Binary.new(@attribute1, @attribute1)
end
- it "obtains if the concrete type of the Predicates::Binarys are identical" do
+ it "obtains if the concrete type of the predicates are identical" do
Binary.new(@attribute1, @attribute2).should == Binary.new(@attribute1, @attribute2)
Binary.new(@attribute1, @attribute2).should_not == ConcreteBinary.new(@attribute1, @attribute2)
end
diff --git a/spec/active_relation/unit/predicates/in_spec.rb b/spec/active_relation/unit/predicates/in_spec.rb
new file mode 100644
index 0000000000..9e60c29ce0
--- /dev/null
+++ b/spec/active_relation/unit/predicates/in_spec.rb
@@ -0,0 +1,58 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
+
+module ActiveRelation
+ describe In do
+ before do
+ @relation = Table.new(:users)
+ @attribute = @relation[:id]
+ end
+
+ describe '#to_sql' do
+ describe 'when relating to an array' do
+ describe 'when the array\'s elements are the same type as the attribute' do
+ before do
+ @array = [1, 2, 3].bind(@relation)
+ end
+
+ it 'manufactures sql with a comma separated list' do
+ In.new(@attribute, @array).to_sql.should be_like("
+ `users`.`id` IN (1, 2, 3)
+ ")
+ end
+ end
+
+ describe 'when the array\'s elements are not same type as the attribute' do
+ before do
+ @array = ['1-asdf', 2, 3].bind(@relation)
+ end
+
+ it 'formats values in the array as the type of the attribute' do
+ In.new(@attribute, @array).to_sql.should be_like("
+ `users`.`id` IN (1, 2, 3)
+ ")
+ end
+ end
+ end
+
+ describe 'when relating to a range' do
+ before do
+ @range = (1..2).bind(@relation)
+ end
+
+ it 'manufactures sql with a between' do
+ In.new(@attribute, @range).to_sql.should be_like("
+ `users`.`id` BETWEEN 1 AND 2
+ ")
+ end
+ end
+
+ describe 'when relating to a relation' do
+ it 'manufactures sql with a subselect' do
+ In.new(@attribute, @relation).to_sql.should be_like("
+ `users`.`id` IN (SELECT `users`.`id`, `users`.`name` FROM `users`)
+ ")
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/spec/active_relation/unit/primitives/attribute_spec.rb b/spec/active_relation/unit/primitives/attribute_spec.rb
index a0f6cde8f7..2806d26330 100644
--- a/spec/active_relation/unit/primitives/attribute_spec.rb
+++ b/spec/active_relation/unit/primitives/attribute_spec.rb
@@ -130,6 +130,12 @@ module ActiveRelation
@attribute.matches(/.*/).should == Match.new(@attribute, /.*/)
end
end
+
+ describe '#in' do
+ it "manufactures an in predicate" do
+ @attribute.in(1..30).should == In.new(@attribute, 1..30)
+ end
+ end
end
describe Attribute::Expressions do
diff --git a/spec/active_relation/unit/relations/relation_spec.rb b/spec/active_relation/unit/relations/relation_spec.rb
index b5e204abcf..d434a1e317 100644
--- a/spec/active_relation/unit/relations/relation_spec.rb
+++ b/spec/active_relation/unit/relations/relation_spec.rb
@@ -30,18 +30,20 @@ module ActiveRelation
end
end
- describe '#include?' do
- it "manufactures an inclusion predicate" do
- @relation.include?(@attribute1).should be_kind_of(RelationInclusion)
+ describe Relation::Externalizable do
+ describe '#aggregation?' do
+ it "returns false" do
+ @relation.should_not be_aggregation
+ end
end
- end
-
- describe '#aggregation?' do
- it "returns false" do
- @relation.should_not be_aggregation
+
+ describe '#alias?' do
+ it "returns false" do
+ @relation.should_not be_alias
+ end
end
end
-
+
describe Relation::Operations do
describe 'joins' do
before do