diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/active_relation/predicates/binary_spec.rb | 7 | ||||
-rw-r--r-- | spec/active_relation/primitives/attribute_spec.rb | 71 | ||||
-rw-r--r-- | spec/active_relation/primitives/expression_spec.rb | 56 | ||||
-rw-r--r-- | spec/active_relation/relations/join_spec.rb | 10 | ||||
-rw-r--r-- | spec/active_relation/relations/table_spec.rb | 38 |
5 files changed, 127 insertions, 55 deletions
diff --git a/spec/active_relation/predicates/binary_spec.rb b/spec/active_relation/predicates/binary_spec.rb index a6878e4898..5d1e7d1223 100644 --- a/spec/active_relation/predicates/binary_spec.rb +++ b/spec/active_relation/predicates/binary_spec.rb @@ -32,6 +32,13 @@ module ActiveRelation should == ConcreteBinary.new(@attribute1.qualify, @attribute2.qualify) end end + + describe '#substitute' do + it "distributes over the predicates and attributes" do + ConcreteBinary.new(@attribute1, @attribute2).substitute(@relation2). \ + should == ConcreteBinary.new(@attribute1.substitute(@relation2), @attribute2.substitute(@relation2)) + end + end describe '#to_sql' do it 'manufactures correct sql' do diff --git a/spec/active_relation/primitives/attribute_spec.rb b/spec/active_relation/primitives/attribute_spec.rb index 0c6c44f1f4..e79f0c994d 100644 --- a/spec/active_relation/primitives/attribute_spec.rb +++ b/spec/active_relation/primitives/attribute_spec.rb @@ -3,24 +3,28 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') module ActiveRelation describe Attribute do before do - @relation1 = Table.new(:foo) - @relation2 = Table.new(:bar) + @relation = Table.new(:users) end describe Attribute::Transformations do before do - @attribute = Attribute.new(@relation1, :id) + @attribute = Attribute.new(@relation, :id) end describe '#as' do it "manufactures an aliased attributed" do - @attribute.as(:alias).should == Attribute.new(@relation1, @attribute.name, :alias, @attribute) + @attribute.as(:alias).should == Attribute.new(@relation, @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, nil, @attribute) + it "manufactures an attribute with the relation substituted and self as an ancestor" do + derived_relation = @relation.select(@relation[:id].equals(1)) + @attribute.substitute(derived_relation).should == Attribute.new(derived_relation, @attribute.name, nil, @attribute) + end + + it "returns self if the substituting to the same relation" do + @attribute.substitute(@relation).should == @attribute end end @@ -39,110 +43,107 @@ module ActiveRelation describe '#qualified_name' do it "manufactures an attribute name prefixed with the relation's name" do - Attribute.new(@relation1, :id).qualified_name.should == 'foo.id' + Attribute.new(@relation, :id).qualified_name.should == 'users.id' end end - - describe '==' do - it "obtains if the relation and attribute name are identical" do - Attribute.new(@relation1, :name).should == Attribute.new(@relation1, :name) - Attribute.new(@relation1, :name).should_not == Attribute.new(@relation1, :another_name) - Attribute.new(@relation1, :name).should_not == Attribute.new(@relation2, :name) - Attribute.new(@relation1, :name).should_not == Expression.new(Attribute.new(@relation1, :name), "SUM") + + describe '=~' do + it "obtains if the attributes are identical" do + Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name) + end + + it "obtains if the attributes have an overlapping history" do + Attribute.new(@relation, :name, nil, Attribute.new(@relation, :name)).should =~ Attribute.new(@relation, :name) + Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name, nil, Attribute.new(@relation, :name)) end 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`") + Attribute.new(@relation, :name, :alias).to_sql(Sql::Predicate.new).should be_like("`users`.`name`") end it "manufactures sql with an alias if the strategy is Projection" do - Attribute.new(@relation1, :name, :alias).to_sql(Sql::Projection.new).should be_like("`foo`.`name` AS 'alias'") + Attribute.new(@relation, :name, :alias).to_sql(Sql::Projection.new).should be_like("`users`.`name` AS 'alias'") end end end describe Attribute::Predications do before do - @attribute1 = Attribute.new(@relation1, :name) - @attribute2 = Attribute.new(@relation2, :name) + @attribute = Attribute.new(@relation, :name) end describe '#equals' do it "manufactures an equality predicate" do - @attribute1.equals(@attribute2).should == Equality.new(@attribute1, @attribute2) + @attribute.equals('name').should == Equality.new(@attribute, 'name') end end describe '#less_than' do it "manufactures a less-than predicate" do - @attribute1.less_than(@attribute2).should == LessThan.new(@attribute1, @attribute2) + @attribute.less_than(10).should == LessThan.new(@attribute, 10) 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 == LessThanOrEqualTo.new(@attribute1, @attribute2) + @attribute.less_than_or_equal_to(10).should == LessThanOrEqualTo.new(@attribute, 10) end end describe '#greater_than' do it "manufactures a greater-than predicate" do - @attribute1.greater_than(@attribute2).should == GreaterThan.new(@attribute1, @attribute2) + @attribute.greater_than(10).should == GreaterThan.new(@attribute, 10) 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 == GreaterThanOrEqualTo.new(@attribute1, @attribute2) + it "manufactures a greater-than or equal-to predicate" do + @attribute.greater_than_or_equal_to(10).should == GreaterThanOrEqualTo.new(@attribute, 10) end end describe '#matches' do it "manufactures a match predicate" do - @attribute1.matches(/.*/).should == Match.new(@attribute1, @attribute2) + @attribute.matches(/.*/).should == Match.new(@attribute, /.*/) end end end describe Attribute::Expressions do before do - @attribute1 = Attribute.new(@relation1, :name) + @attribute = Attribute.new(@relation, :name) end describe '#count' do it "manufactures a count Expression" do - @attribute1.count.should == Expression.new(@attribute1, "COUNT") + @attribute.count.should == Expression.new(@attribute, "COUNT") end end describe '#sum' do it "manufactures a sum Expression" do - @attribute1.sum.should == Expression.new(@attribute1, "SUM") + @attribute.sum.should == Expression.new(@attribute, "SUM") end end describe '#maximum' do it "manufactures a maximum Expression" do - @attribute1.maximum.should == Expression.new(@attribute1, "MAX") + @attribute.maximum.should == Expression.new(@attribute, "MAX") end end describe '#minimum' do it "manufactures a minimum Expression" do - @attribute1.minimum.should == Expression.new(@attribute1, "MIN") + @attribute.minimum.should == Expression.new(@attribute, "MIN") end end describe '#average' do it "manufactures an average Expression" do - @attribute1.average.should == Expression.new(@attribute1, "AVG") + @attribute.average.should == Expression.new(@attribute, "AVG") end end end diff --git a/spec/active_relation/primitives/expression_spec.rb b/spec/active_relation/primitives/expression_spec.rb new file mode 100644 index 0000000000..d699dfded0 --- /dev/null +++ b/spec/active_relation/primitives/expression_spec.rb @@ -0,0 +1,56 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') + +module ActiveRelation + describe Expression do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + + describe Expression::Transformations do + before do + @expression = Expression.new(@attribute, "COUNT") + end + + describe '#substitute' do + it "manufactures an attribute with a substituted relation and self as the ancestor" do + derived_relation = @relation.select(@relation[:id] == 1) + @expression.substitute(derived_relation).should == Expression.new(@attribute.substitute(derived_relation), "COUNT", nil, @expression) + end + + it "returns self if the substituting to the same relation" do + @expression.substitute(@relation).should == @expression + end + end + + describe '#as' do + it "manufactures an aliased expression" do + @expression.as(:foo).should == Expression.new(@attribute, "COUNT", :foo, @expression) + end + end + + describe '#to_attribute' do + it "manufactures an attribute with the expression as an ancestor" do + @expression.to_attribute.should == Attribute.new(@expression.relation, @expression.alias, nil, @expression) + end + end + end + + describe '=~' do + it "obtains if the expressions are identical" do + Expression.new(@attribute, "COUNT").should =~ Expression.new(@attribute, "COUNT") + end + + it "obtains if the expressions have an overlapping history" do + Expression.new(@attribute, "COUNT", nil, Expression.new(@attribute, "COUNT")).should =~ Expression.new(@attribute, "COUNT") + Expression.new(@attribute, "COUNT").should =~ Expression.new(@attribute, "COUNT", nil, Expression.new(@attribute, "COUNT")) + end + end + + describe '#to_sql' do + it "manufactures sql with the expression and alias" do + Expression.new(@attribute, "COUNT", :alias).to_sql.should == "COUNT(`users`.`id`) AS `alias`" + end + end + end +end
\ No newline at end of file diff --git a/spec/active_relation/relations/join_spec.rb b/spec/active_relation/relations/join_spec.rb index 52915b62c7..2df349dcd3 100644 --- a/spec/active_relation/relations/join_spec.rb +++ b/spec/active_relation/relations/join_spec.rb @@ -77,12 +77,11 @@ 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]) - @aggregate_relation = aggregate_relation.rename(photos[:id].count, :cnt).as(:photo_count) + @aggregate_relation = photos.aggregate(photos[:user_id], photos[:id].count).group(photos[:user_id]).rename(photos[:id].count, :cnt).as(:photo_count) @predicate = Equality.new(@aggregate_relation[:user_id], @relation[:id]) end - describe 'with the expression on the right' do + describe 'with the aggregation on the right' do it 'manufactures sql joining the left table to a derived table' do Join.new("INNER JOIN", @relation, @aggregate_relation, @predicate).to_sql.should be_like(""" SELECT `users`.`name`, `users`.`id`, `photo_count`.`user_id`, `photo_count`.`cnt` @@ -93,7 +92,7 @@ module ActiveRelation end end - describe 'with the expression on the left' do + describe 'with the aggregation on the left' do it 'manufactures sql joining the right table to a derived table' do Join.new("INNER JOIN", @aggregate_relation, @relation, @predicate).to_sql.should be_like(""" SELECT `photo_count`.`user_id`, `photo_count`.`cnt`, `users`.`name`, `users`.`id` @@ -104,8 +103,7 @@ module ActiveRelation end end - it "keeps selects on the expression within the derived table" do - pending + it "keeps selects on the aggregation within the derived table" do Join.new("INNER JOIN", @relation, @aggregate_relation.select(@aggregate_relation[:user_id].equals(1)), @predicate).to_sql.should be_like(""" SELECT `users`.`name`, `users`.`id`, `photo_count`.`user_id`, `photo_count`.`cnt` FROM `users` diff --git a/spec/active_relation/relations/table_spec.rb b/spec/active_relation/relations/table_spec.rb index 02e669a08b..e48d259fb8 100644 --- a/spec/active_relation/relations/table_spec.rb +++ b/spec/active_relation/relations/table_spec.rb @@ -3,39 +3,43 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') module ActiveRelation describe Table do before do - @relation1 = Table.new(:users) + @table = 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 - @relation1[:id].should == Attribute.new(@relation1, :id) - @relation1[:does_not_exist].should be_nil + @table[:id].should == Attribute.new(@table, :id) + @table[: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 - @relation1[@relation1[:id]].should == @relation1[:id] - @relation1[@relation2[:id]].should be_nil + @table[@table[:id]].should == @table[:id] + end + + it "returns nil if the attribtue is not within the relation" do + another_relation = Table.new(:photos) + @table[another_relation[:id]].should be_nil end end describe 'when given an', Expression do before do - @expression = Expression.new(Attribute.new(@relation1, :id), "COUNT") + @expression = Expression.new(Attribute.new(@table, :id), "COUNT") end it "returns the Expression if the Expression is within the relation" do - @relation1[@expression].should be_nil + @table[@expression].should be_nil end end end describe '#to_sql' do it "manufactures a simple select query" do - @relation1.to_sql.should be_like(""" + @table.to_sql.should be_like(""" SELECT `users`.`name`, `users`.`id` FROM `users` """) @@ -43,23 +47,29 @@ module ActiveRelation end describe '#prefix_for' do - it "always returns the table name" do - @relation1.prefix_for(Attribute.new(@relation1, :id)).should == :users + it "returns the table name" do + @table.prefix_for(Attribute.new(@table, :id)).should == :users + end + end + + describe '#aliased_prefix_for' do + it "returns the table name" do + @table.aliased_prefix_for(Attribute.new(@table, :id)).should == :users end end describe '#attributes' do it 'manufactures attributes corresponding to columns in the table' do - @relation1.attributes.should == [ - Attribute.new(@relation1, :name), - Attribute.new(@relation1, :id) + @table.attributes.should == [ + Attribute.new(@table, :name), + Attribute.new(@table, :id) ] end end describe '#qualify' do it 'manufactures a rename relation with all attribute names qualified' do - @relation1.qualify.should == Rename.new(@relation1, @relation1[:id] => 'users.id', @relation1[:name] => 'users.name') + @table.qualify.should == Rename.new(@table, @table[:name] => 'users.name', @table[:id] => 'users.id') end end end |