diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-12 23:30:12 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-12 23:30:12 -0800 |
commit | 7c7044085617a66abb3f9bd37dc7c072701f03c7 (patch) | |
tree | 9b220bc7e359d593da61cdccf979e3668c9836cf /spec/active_relation | |
parent | 0cba6cde9958969933da79626d00464f7d07b22f (diff) | |
download | rails-7c7044085617a66abb3f9bd37dc7c072701f03c7.tar.gz rails-7c7044085617a66abb3f9bd37dc7c072701f03c7.tar.bz2 rails-7c7044085617a66abb3f9bd37dc7c072701f03c7.zip |
aliasing of relations
Diffstat (limited to 'spec/active_relation')
-rw-r--r-- | spec/active_relation/predicates/binary_spec.rb | 10 | ||||
-rw-r--r-- | spec/active_relation/primitives/aggregation_spec.rb | 25 | ||||
-rw-r--r-- | spec/active_relation/primitives/attribute_spec.rb | 115 | ||||
-rw-r--r-- | spec/active_relation/relations/base_spec.rb | 22 | ||||
-rw-r--r-- | spec/active_relation/relations/compound_spec.rb | 13 | ||||
-rw-r--r-- | spec/active_relation/relations/projection_spec.rb | 2 | ||||
-rw-r--r-- | spec/active_relation/relations/rename_spec.rb | 4 |
7 files changed, 179 insertions, 12 deletions
diff --git a/spec/active_relation/predicates/binary_spec.rb b/spec/active_relation/predicates/binary_spec.rb index 02c72ef96d..be607efd95 100644 --- a/spec/active_relation/predicates/binary_spec.rb +++ b/spec/active_relation/predicates/binary_spec.rb @@ -6,7 +6,7 @@ describe ActiveRelation::Predicates::Binary do @relation2 = ActiveRelation::Relations::Table.new(:bar) @attribute1 = ActiveRelation::Primitives::Attribute.new(@relation1, :name1) @attribute2 = ActiveRelation::Primitives::Attribute.new(@relation2, :name2) - class ActiveRelation::Predicates::ConcreteBinary < ActiveRelation::Predicates::Binary + class ConcreteBinary < ActiveRelation::Predicates::Binary def predicate_sql "<=>" end @@ -21,20 +21,20 @@ describe ActiveRelation::Predicates::Binary do it "obtains if the concrete type of the ActiveRelation::Predicates::Binarys are identical" do ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2).should == ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2) - ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2).should_not == ActiveRelation::Predicates::ConcreteBinary.new(@attribute1, @attribute2) + ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2).should_not == ConcreteBinary.new(@attribute1, @attribute2) end end describe '#qualify' do it "distributes over the predicates and attributes" do - ActiveRelation::Predicates::ConcreteBinary.new(@attribute1, @attribute2).qualify. \ - should == ActiveRelation::Predicates::ConcreteBinary.new(@attribute1.qualify, @attribute2.qualify) + ConcreteBinary.new(@attribute1, @attribute2).qualify. \ + should == ConcreteBinary.new(@attribute1.qualify, @attribute2.qualify) end end describe '#to_sql' do it 'manufactures correct sql' do - ActiveRelation::Predicates::ConcreteBinary.new(@attribute1, @attribute2).to_sql.should be_like(""" + ConcreteBinary.new(@attribute1, @attribute2).to_sql.should be_like(""" `foo`.`name1` <=> `bar`.`name2` """) end diff --git a/spec/active_relation/primitives/aggregation_spec.rb b/spec/active_relation/primitives/aggregation_spec.rb new file mode 100644 index 0000000000..60bb4a6aa8 --- /dev/null +++ b/spec/active_relation/primitives/aggregation_spec.rb @@ -0,0 +1,25 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') + +describe ActiveRelation::Primitives::Aggregation do + before do + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) + end + + describe '==' do + it 'obtains if the attribute and function sql are identical' do + @relation1[:id].sum.should == @relation1[:id].sum + @relation1[:id].sum.should_not == @relation1[:name].sum + @relation1[:id].sum.should_not == @relation1[:name].average + @relation1[:id].sum.should_not == @relation2[:id].sum + end + end + + describe '#to_sql' do + it 'manufactures sql with an aggregation function' do + @relation1[:id].maximum.to_sql.should be_like(""" + MAX(`foo`.`id`) + """) + end + end +end diff --git a/spec/active_relation/primitives/attribute_spec.rb b/spec/active_relation/primitives/attribute_spec.rb new file mode 100644 index 0000000000..2aad844659 --- /dev/null +++ b/spec/active_relation/primitives/attribute_spec.rb @@ -0,0 +1,115 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') + +describe ActiveRelation::Primitives::Attribute do + before do + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) + end + + describe '#as' do + it "manufactures an aliased attributed when provided a parameter" do + @relation1[:id].as(:alias).should == ActiveRelation::Primitives::Attribute.new(@relation1, :id, :alias) + end + end + + describe '#qualified_name' do + it "manufactures an attribute name prefixed with the relation's name" do + @relation1[:id].qualified_name.should == 'foo.id' + end + end + + describe '#qualify' do + it "manufactures an attribute aliased with that attributes qualified name" do + @relation1[:id].qualify.should == @relation1[:id].qualify + end + end + + describe '==' do + it "obtains if the relation and attribute name are identical" do + ActiveRelation::Primitives::Attribute.new(@relation1, :name).should == ActiveRelation::Primitives::Attribute.new(@relation1, :name) + ActiveRelation::Primitives::Attribute.new(@relation1, :name).should_not == ActiveRelation::Primitives::Attribute.new(@relation1, :another_name) + ActiveRelation::Primitives::Attribute.new(@relation1, :name).should_not == ActiveRelation::Primitives::Attribute.new(@relation2, :name) + end + end + + describe 'predications' do + before do + @attribute1 = ActiveRelation::Primitives::Attribute.new(@relation1, :name) + @attribute2 = ActiveRelation::Primitives::Attribute.new(@relation2, :name) + end + + describe '#equals' do + it "manufactures an equality predicate" do + @attribute1.equals(@attribute2).should == ActiveRelation::Predicates::Equality.new(@attribute1, @attribute2) + end + end + + describe '#less_than' do + it "manufactures a less-than predicate" do + @attribute1.less_than(@attribute2).should == ActiveRelation::Predicates::LessThan.new(@attribute1, @attribute2) + end + end + + describe '#less_than_or_equal_to' do + it "manufactures a less-than or equal-to predicate" do + @attribute1.less_than_or_equal_to(@attribute2).should == ActiveRelation::Predicates::LessThanOrEqualTo.new(@attribute1, @attribute2) + end + end + + describe '#greater_than' do + it "manufactures a greater-than predicate" do + @attribute1.greater_than(@attribute2).should == ActiveRelation::Predicates::GreaterThan.new(@attribute1, @attribute2) + end + end + + describe '#greater_than_or_equal_to' do + it "manufactures a greater-than or equal to predicate" do + @attribute1.greater_than_or_equal_to(@attribute2).should == ActiveRelation::Predicates::GreaterThanOrEqualTo.new(@attribute1, @attribute2) + end + end + + describe '#matches' do + it "manufactures a match predicate" do + @attribute1.matches(/.*/).should == ActiveRelation::Predicates::Match.new(@attribute1, @attribute2) + end + end + end + + describe 'aggregations' do + before do + @attribute1 = ActiveRelation::Primitives::Attribute.new(@relation1, :name) + end + + describe '#count' do + it "manufactures a count aggregation" do + @attribute1.count.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "COUNT") + end + end + + describe '#sum' do + it "manufactures a sum aggregation" do + @attribute1.sum.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "SUM") + end + end + + describe '#maximum' do + it "manufactures a maximum aggregation" do + @attribute1.maximum.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "MAX") + end + end + + describe '#minimum' do + it "manufactures a minimum aggregation" do + @attribute1.minimum.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "MIN") + end + end + + describe '#average' do + it "manufactures an average aggregation" do + @attribute1.average.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "AVG") + end + end + + + end +end diff --git a/spec/active_relation/relations/base_spec.rb b/spec/active_relation/relations/base_spec.rb index 689e3ecadb..db112e679a 100644 --- a/spec/active_relation/relations/base_spec.rb +++ b/spec/active_relation/relations/base_spec.rb @@ -49,13 +49,19 @@ describe ActiveRelation::Relations::Base do end it "manufactures a projection relation" do - @relation1.project(@attribute1, @attribute2).should be_kind_of(ActiveRelation::Relations::Projection) + @relation1.project(@attribute1, @attribute2).should == ActiveRelation::Relations::Projection.new(@relation1, @attribute1, @attribute2) + end + end + + describe '#as' do + it "manufactures an alias relation" do + @relation1.as(:thucydides).should == ActiveRelation::Relations::Alias.new(@relation1, :thucydides) end end describe '#rename' do it "manufactures a rename relation" do - @relation1.rename(@attribute1, :foo).should be_kind_of(ActiveRelation::Relations::Rename) + @relation1.rename(@attribute1, :foo).should == ActiveRelation::Relations::Rename.new(@relation1, @attribute1 => :foo) end end @@ -65,11 +71,11 @@ describe ActiveRelation::Relations::Base do end it "manufactures a selection relation" do - @relation1.select(@predicate).should be_kind_of(ActiveRelation::Relations::Selection) + @relation1.select(@predicate).should == ActiveRelation::Relations::Selection.new(@relation1, @predicate) end it "accepts arbitrary strings" do - @relation1.select("arbitrary").should be_kind_of(ActiveRelation::Relations::Selection) + @relation1.select("arbitrary").should == ActiveRelation::Relations::Selection.new(@relation1, "arbitrary") end end @@ -93,4 +99,12 @@ describe ActiveRelation::Relations::Base do end end end + + describe '#to_sql' do + it "manufactures sql with scalar selects" do + @relation1.as(:tobias).to_sql(:use_parens => true).should be_like(""" + (SELECT `foo`.`name`, `foo`.`id` FROM `foo`) AS tobias + """) + end + end end
\ No newline at end of file diff --git a/spec/active_relation/relations/compound_spec.rb b/spec/active_relation/relations/compound_spec.rb new file mode 100644 index 0000000000..f383ff4dfd --- /dev/null +++ b/spec/active_relation/relations/compound_spec.rb @@ -0,0 +1,13 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') + +describe ActiveRelation::Relations::Compound do + before do + @relation = ActiveRelation::Relations::Table.new(:users) + + class ConcreteCompound < ActiveRelation::Relations::Compound + def initialize(relation) + @relation = relation + end + end + end +end
\ No newline at end of file diff --git a/spec/active_relation/relations/projection_spec.rb b/spec/active_relation/relations/projection_spec.rb index 8ba571e06c..6ffdeb8e09 100644 --- a/spec/active_relation/relations/projection_spec.rb +++ b/spec/active_relation/relations/projection_spec.rb @@ -17,7 +17,7 @@ describe ActiveRelation::Relations::Projection do end describe '#qualify' do - it "distributes over teh relation and attributes" do + it "distributes over the relation and attributes" do ActiveRelation::Relations::Projection.new(@relation1, @attribute1).qualify. \ should == ActiveRelation::Relations::Projection.new(@relation1.qualify, @attribute1.qualify) end diff --git a/spec/active_relation/relations/rename_spec.rb b/spec/active_relation/relations/rename_spec.rb index e52abab3a1..76b7069190 100644 --- a/spec/active_relation/relations/rename_spec.rb +++ b/spec/active_relation/relations/rename_spec.rb @@ -26,14 +26,14 @@ describe ActiveRelation::Relations::Rename do describe '#attributes' do it "manufactures a list of attributes with the renamed attribute aliased" do ActiveRelation::Relations::Rename.new(@relation, @relation[:id] => :schmid).attributes.should == - (@relation.attributes - [@relation[:id]]) + [@relation[:id].alias(:schmid)] + (@relation.attributes - [@relation[:id]]) + [@relation[:id].as(:schmid)] end end describe '[]' do it 'indexes attributes by alias' do @renamed_relation[:id].should be_nil - @renamed_relation[:schmid].should == @relation[:id].alias(:schmid) + @renamed_relation[:schmid].should == @relation[:id].as(:schmid) end end |