diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/active_relation/primitives/attribute_spec.rb | 25 | ||||
-rw-r--r-- | spec/active_relation/relations/compound_spec.rb | 15 | ||||
-rw-r--r-- | spec/active_relation/relations/join_spec.rb | 12 | ||||
-rw-r--r-- | spec/active_relation/relations/rename_spec.rb | 28 | ||||
-rw-r--r-- | spec/active_relation/relations/table_spec.rb | 31 |
5 files changed, 59 insertions, 52 deletions
diff --git a/spec/active_relation/primitives/attribute_spec.rb b/spec/active_relation/primitives/attribute_spec.rb index f76018c493..0c6c44f1f4 100644 --- a/spec/active_relation/primitives/attribute_spec.rb +++ b/spec/active_relation/primitives/attribute_spec.rb @@ -14,19 +14,19 @@ module ActiveRelation describe '#as' do it "manufactures an aliased attributed" do - @attribute.as(:alias).should == Attribute.new(@relation1, @attribute.name, :alias) + @attribute.as(:alias).should == Attribute.new(@relation1, @attribute.name, :alias, @attribute) end end describe '#substitute' do it "manufactures an attribute with the relation substituted" do - @attribute.substitute(@relation2).should == Attribute.new(@relation2, @attribute.name) + @attribute.substitute(@relation2).should == Attribute.new(@relation2, @attribute.name, nil, @attribute) end end describe '#qualify' do it "manufactures an attribute aliased with that attributes qualified name" do - @attribute.qualify.should == Attribute.new(@attribute.relation, @attribute.name, @attribute.qualified_name) + @attribute.qualify.should == Attribute.new(@attribute.relation, @attribute.name, @attribute.qualified_name, @attribute) end end @@ -53,6 +53,10 @@ module ActiveRelation end describe '#to_sql' do + it "needs to test prefix_for" do + pending + end + describe Sql::Strategy do it "manufactures sql without an alias if the strategy is Predicate" do Attribute.new(@relation1, :name, :alias).to_sql(Sql::Predicate.new).should be_like("`foo`.`name`") @@ -62,19 +66,6 @@ module ActiveRelation Attribute.new(@relation1, :name, :alias).to_sql(Sql::Projection.new).should be_like("`foo`.`name` AS 'alias'") end end - - describe 'binding' do - before do - @attribute = Attribute.new(@relation1, :name, :alias) - @aliased_relation = @relation1.as(:schmoo) - end - - it "is fancy pants" do - pending - @attribute.to_sql.should be_like("`foo`.`name`") - @attribute.substitute(@aliased_relation).to_sql.should be_like("`schmoo`.`alias`") - end - end end describe Attribute::Predications do @@ -120,7 +111,7 @@ module ActiveRelation end end - describe 'Expressions' do + describe Attribute::Expressions do before do @attribute1 = Attribute.new(@relation1, :name) end diff --git a/spec/active_relation/relations/compound_spec.rb b/spec/active_relation/relations/compound_spec.rb index a03b0206a9..79ac965e63 100644 --- a/spec/active_relation/relations/compound_spec.rb +++ b/spec/active_relation/relations/compound_spec.rb @@ -14,35 +14,36 @@ module ActiveRelation describe '#attributes' do it 'manufactures attributes associated with the compound relation' do - @compound_relation.attributes.should == @relation.attributes.collect { |a| Attribute.new(@compound_relation, a.name) } + @compound_relation.attributes.should == @relation.attributes.collect { |a| a.substitute(@compound_relation) } end end describe '[]' do describe 'when given a', Symbol do it 'manufactures attributes associated with the compound relation if the symbol names an attribute within the relation' do - @compound_relation[:id].relation.should == @compound_relation + @compound_relation[:id].should == @relation[:id].substitute(@compound_relation) @compound_relation[:does_not_exist].should be_nil end end describe 'when given an', Attribute do it "manufactures a substituted attribute when given an attribute within the relation" do - @compound_relation[Attribute.new(@relation, :id)].should == Attribute.new(@compound_relation, :id) - @compound_relation[Attribute.new(@compound_relation, :id)].should == Attribute.new(@compound_relation, :id) - @compound_relation[Attribute.new(another_relation = Table.new(:photos), :id)].should be_nil + @compound_relation[@relation[:id]].should == @relation[:id].substitute(@compound_relation) + @compound_relation[@compound_relation[:id]].should == @compound_relation[:id] + pending "test nil" end end describe 'when given an', Expression do before do @nested_expression = Expression.new(Attribute.new(@relation, :id), "COUNT") + @nested_relation = Aggregation.new(@relation, :expressions => [@nested_expression]) @unprojected_expression = Expression.new(Attribute.new(@relation, :id), "SUM") - @compound_relation = ConcreteCompound.new(Aggregation.new(@relation, :expressions => [@nested_expression])) + @compound_relation = ConcreteCompound.new(@nested_relation) end it "manufactures a substituted Expression when given an Expression within the relation" do - @compound_relation[@nested_expression].should == @nested_expression.substitute(@compound_relation) + @compound_relation[@nested_expression].should == @nested_relation[@nested_expression].substitute(@compound_relation) @compound_relation[@compound_relation[@expression]].should == @compound_relation[@expression] @compound_relation[@unprojected_expression].should be_nil end diff --git a/spec/active_relation/relations/join_spec.rb b/spec/active_relation/relations/join_spec.rb index 5235a5b528..52915b62c7 100644 --- a/spec/active_relation/relations/join_spec.rb +++ b/spec/active_relation/relations/join_spec.rb @@ -34,9 +34,13 @@ module ActiveRelation describe '#attributes' do describe 'with simple relations' do + before do + @join = Join.new("INNER JOIN", @relation1, @relation2, @predicate) + end + it 'combines the attributes of the two relations' do - Join.new("INNER JOIN", @relation1, @relation2, @predicate).attributes.should == - @relation1.attributes + @relation2.attributes + @join.attributes.should == + (@relation1.attributes + @relation2.attributes).collect { |a| a.substitute(@join) } end end @@ -73,8 +77,8 @@ module ActiveRelation before do @relation = Table.new(:users) photos = Table.new(:photos) - @aggregate_relation = photos.aggregate(photos[:user_id], photos[:id].count).group(photos[:user_id]).rename(photos[:id].count, :cnt) \ - .as(:photo_count) + aggregate_relation = photos.aggregate(photos[:user_id], photos[:id].count).group(photos[:user_id]) + @aggregate_relation = aggregate_relation.rename(photos[:id].count, :cnt).as(:photo_count) @predicate = Equality.new(@aggregate_relation[:user_id], @relation[:id]) end diff --git a/spec/active_relation/relations/rename_spec.rb b/spec/active_relation/relations/rename_spec.rb index 65ad9c51bd..ebe95f13b9 100644 --- a/spec/active_relation/relations/rename_spec.rb +++ b/spec/active_relation/relations/rename_spec.rb @@ -25,7 +25,7 @@ module ActiveRelation describe '#attributes' do it "manufactures a list of attributes with the renamed attribute renameed" do - @renamed_relation.attributes.should include(Attribute.new(@renamed_relation, :id, :schmid)) + @renamed_relation.attributes.should include(@renamed_relation[:schmid]) @renamed_relation.should have(@relation1.attributes.size).attributes end end @@ -34,17 +34,17 @@ module ActiveRelation describe 'when given a', Symbol do it 'indexes attributes by rename if the symbol names an attribute within the relation' do @renamed_relation[:id].should be_nil - @renamed_relation[:schmid].should == Attribute.new(@renamed_relation, :id, :schmid) + @renamed_relation[:schmid].should == @relation1[:id].as(:schmid).substitute(@renamed_relation) @renamed_relation[:does_not_exist].should be_nil end end describe 'when given an', Attribute do it 'manufactures a substituted and renamed attribute if the attribute is within the relation' do - @renamed_relation[Attribute.new(@relation1, :id)].should == Attribute.new(@renamed_relation, :id, :schmid) - @renamed_relation[Attribute.new(@relation1, :name)].should == Attribute.new(@renamed_relation, :name) - @renamed_relation[Attribute.new(@renamed_relation, :name)].should == Attribute.new(@renamed_relation, :name) - @renamed_relation[Attribute.new(@relation2, :id)].should be_nil + @renamed_relation[@relation1[:id]].should == @relation1[:id].as(:schmid).substitute(@renamed_relation) + @renamed_relation[@relation1[:name]].should == @relation1[:name].substitute(@renamed_relation) + @renamed_relation[@renamed_relation[:name]].should == @renamed_relation[:name] + @renamed_relation[@relation2[:id]].should be_nil end end @@ -62,22 +62,26 @@ module ActiveRelation it 'manufactures a substituted and renamed attribute if the attribute is within the relation' do @renamed_renamed_relation[:id].should be_nil @renamed_renamed_relation[:schmid].should be_nil - @renamed_renamed_relation[:flid].should == Attribute.new(@renamed_renamed_relation, :id, :flid) + @renamed_renamed_relation[:flid].should == @renamed_relation[:schmid].as(:flid).substitute(@renamed_renamed_relation) end end describe 'when given an', Attribute do it "manufactures a substituted and renamed attribute if the attribute is within the relation -- even if the provided attribute derived" do - @renamed_renamed_relation[Attribute.new(@renamed_relation, :id, :schmid)].should == Attribute.new(@renamed_renamed_relation, :id, :flid) - @renamed_renamed_relation[Attribute.new(@relation1, :id)].should == Attribute.new(@renamed_renamed_relation, :id, :flid) + @renamed_renamed_relation[@renamed_relation[:schmid]].should == @renamed_relation[:schmid].as(:flid).substitute(@renamed_renamed_relation) + @renamed_renamed_relation[@relation1[:id]].should == @renamed_relation[:schmid].as(:flid).substitute(@renamed_renamed_relation) end end describe 'when given an', Expression do + before do + @expression = @relation1[:id].count + @aggregation = Aggregation.new(@relation1, :expressions => [@expression]) + @renamed_relation = Rename.new(@aggregation, @expression => :cnt) + end + it "manufactures a substituted and renamed expression if the expression is within the relation" do - renamed_relation = Rename.new(Aggregation.new(@relation1, :expressions => [@relation1[:id].count]), @relation1[:id].count => :cnt) - renamed_relation[@relation1[:id].count].should == @relation1[:id].count.as(:cnt).substitute(renamed_relation) - renamed_relation.attributes.should == [@relation1[:id].count.as(:cnt).substitute(renamed_relation)] + @renamed_relation[@expression].should == @aggregation[@expression].as(:cnt).substitute(@renamed_relation) end end end diff --git a/spec/active_relation/relations/table_spec.rb b/spec/active_relation/relations/table_spec.rb index 140346e6f6..02e669a08b 100644 --- a/spec/active_relation/relations/table_spec.rb +++ b/spec/active_relation/relations/table_spec.rb @@ -3,56 +3,63 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') module ActiveRelation describe Table do before do - @relation = Table.new(:users) + @relation1 = Table.new(:users) + @relation2 = Table.new(:photos) end describe '[]' do describe 'when given a', Symbol do it "manufactures an attribute if the symbol names an attribute within the relation" do - @relation[:id].should == Attribute.new(@relation, :id) - @relation[:does_not_exist].should be_nil + @relation1[:id].should == Attribute.new(@relation1, :id) + @relation1[:does_not_exist].should be_nil end end describe 'when given an', Attribute do it "returns the attribute if the attribute is within the relation" do - @relation[Attribute.new(@relation, :id)].should == Attribute.new(@relation, :id) - @relation[Attribute.new(another_relation = Table.new(:photos), :id)].should be_nil + @relation1[@relation1[:id]].should == @relation1[:id] + @relation1[@relation2[:id]].should be_nil end end describe 'when given an', Expression do before do - @expression = Expression.new(Attribute.new(@relation, :id), "COUNT") + @expression = Expression.new(Attribute.new(@relation1, :id), "COUNT") end it "returns the Expression if the Expression is within the relation" do - @relation[@expression].should be_nil + @relation1[@expression].should be_nil end end end describe '#to_sql' do it "manufactures a simple select query" do - @relation.to_sql.should be_like(""" + @relation1.to_sql.should be_like(""" SELECT `users`.`name`, `users`.`id` FROM `users` """) end end + + describe '#prefix_for' do + it "always returns the table name" do + @relation1.prefix_for(Attribute.new(@relation1, :id)).should == :users + end + end describe '#attributes' do it 'manufactures attributes corresponding to columns in the table' do - @relation.attributes.should == [ - Attribute.new(@relation, :name), - Attribute.new(@relation, :id) + @relation1.attributes.should == [ + Attribute.new(@relation1, :name), + Attribute.new(@relation1, :id) ] end end describe '#qualify' do it 'manufactures a rename relation with all attribute names qualified' do - @relation.qualify.should == Rename.new(@relation, @relation[:id] => 'users.id', @relation[:name] => 'users.name') + @relation1.qualify.should == Rename.new(@relation1, @relation1[:id] => 'users.id', @relation1[:name] => 'users.name') end end end |