diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-03 15:43:16 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-03 15:43:16 -0800 |
commit | 6c73e3dbc714a9752a66a6da51e7e41f372797b3 (patch) | |
tree | 55571262b97fc7a91114af78d04f231b1c3e3892 /spec/active_relation | |
parent | 9c4403f67a8f2ac40fc0c964d8c24ecc6c6d7f27 (diff) | |
download | rails-6c73e3dbc714a9752a66a6da51e7e41f372797b3.tar.gz rails-6c73e3dbc714a9752a66a6da51e7e41f372797b3.tar.bz2 rails-6c73e3dbc714a9752a66a6da51e7e41f372797b3.zip |
i know it doesn't work but need to anchor here...
Diffstat (limited to 'spec/active_relation')
-rw-r--r-- | spec/active_relation/predicates/equality_spec.rb | 3 | ||||
-rw-r--r-- | spec/active_relation/primitives/aggregation_spec.rb | 62 | ||||
-rw-r--r-- | spec/active_relation/primitives/attribute_spec.rb | 55 | ||||
-rw-r--r-- | spec/active_relation/relations/alias_spec.rb | 19 | ||||
-rw-r--r-- | spec/active_relation/relations/compound_spec.rb | 41 | ||||
-rw-r--r-- | spec/active_relation/relations/group_spec.rb | 35 | ||||
-rw-r--r-- | spec/active_relation/relations/join_spec.rb | 18 | ||||
-rw-r--r-- | spec/active_relation/relations/projection_spec.rb | 4 | ||||
-rw-r--r-- | spec/active_relation/relations/relation_spec.rb | 11 | ||||
-rw-r--r-- | spec/active_relation/relations/rename_spec.rb | 58 | ||||
-rw-r--r-- | spec/active_relation/relations/schmoin_spec.rb | 24 | ||||
-rw-r--r-- | spec/active_relation/relations/table_spec.rb | 30 |
12 files changed, 175 insertions, 185 deletions
diff --git a/spec/active_relation/predicates/equality_spec.rb b/spec/active_relation/predicates/equality_spec.rb index d23893e438..f947bc6fe7 100644 --- a/spec/active_relation/predicates/equality_spec.rb +++ b/spec/active_relation/predicates/equality_spec.rb @@ -23,8 +23,5 @@ module ActiveRelation Equality.new(@attribute1, @attribute2).should == Equality.new(@attribute2, @attribute1) end end - - describe '#to_sql' do - end end end
\ No newline at end of file diff --git a/spec/active_relation/primitives/aggregation_spec.rb b/spec/active_relation/primitives/aggregation_spec.rb deleted file mode 100644 index 5daf774e06..0000000000 --- a/spec/active_relation/primitives/aggregation_spec.rb +++ /dev/null @@ -1,62 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') - -module ActiveRelation - describe Aggregation do - before do - @relation1 = Table.new(:foo) - @relation2 = Table.new(:bar) - end - - describe '==' do - it 'obtains if the attribute and function sql are identical' do - Aggregation.new(@relation1[:id], "SUM").should == Aggregation.new(@relation1[:id], "SUM") - Aggregation.new(@relation1[:id], "SUM").should_not == Aggregation.new(@relation1[:name], "SUM") - Aggregation.new(@relation1[:id], "SUM").should_not == Aggregation.new(@relation1[:name], "SUM") - Aggregation.new(@relation1[:id], "SUM").should_not == Aggregation.new(@relation2[:id], "SUM") - end - end - - describe Aggregation::Transformations do - describe '#substitute' do - it "distributes over the attribute and alias" do - Aggregation.new(@relation1[:id], "SUM", "alias").substitute(@relation2). \ - should == Aggregation.new(@relation1[:id].substitute(@relation2), "SUM", "alias") - end - end - - describe '#as' do - it "manufactures an aliased aggregation" do - Aggregation.new(@relation1[:id], "SUM").as(:doof). \ - should == Aggregation.new(@relation1[:id], "SUM", :doof) - end - end - - describe '#to_attribute' do - it "manufactures an attribute the name of which corresponds to the aggregation's alias" do - Aggregation.new(@relation1[:id], "SUM", :schmaggregation).to_attribute. \ - should == Attribute.new(@relation1, :schmaggregation) - end - end - end - - describe '#relation' do - it "delegates to the attribute" do - Aggregation.new(@relation1[:id], "SUM").relation.should == @relation1 - end - end - - describe '#to_sql' do - it 'manufactures sql with an aggregation function' do - Aggregation.new(@relation1[:id], "MAX").to_sql.should be_like(""" - MAX(`foo`.`id`) - """) - end - - it 'manufactures sql with an aliased aggregation function' do - Aggregation.new(@relation1[:id], "MAX", "marx").to_sql.should be_like(""" - MAX(`foo`.`id`) AS `marx` - """) - end - end - end -end
\ No newline at end of file diff --git a/spec/active_relation/primitives/attribute_spec.rb b/spec/active_relation/primitives/attribute_spec.rb index 7f5d4922b8..f76018c493 100644 --- a/spec/active_relation/primitives/attribute_spec.rb +++ b/spec/active_relation/primitives/attribute_spec.rb @@ -41,10 +41,6 @@ module ActiveRelation it "manufactures an attribute name prefixed with the relation's name" do Attribute.new(@relation1, :id).qualified_name.should == 'foo.id' end - - it "manufactures an attribute name prefixed with the relation's aliased name" do - Attribute.new(@relation1.as(:bar), :id).qualified_name.should == 'bar.id' - end end describe '==' do @@ -52,11 +48,36 @@ module ActiveRelation 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 == Aggregation.new(Attribute.new(@relation1, :name), "SUM") + Attribute.new(@relation1, :name).should_not == Expression.new(Attribute.new(@relation1, :name), "SUM") + end + end + + describe '#to_sql' do + 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`") + 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'") + 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 'predications' do + describe Attribute::Predications do before do @attribute1 = Attribute.new(@relation1, :name) @attribute2 = Attribute.new(@relation2, :name) @@ -99,38 +120,38 @@ module ActiveRelation end end - describe 'aggregations' do + describe 'Expressions' do before do @attribute1 = Attribute.new(@relation1, :name) end describe '#count' do - it "manufactures a count aggregation" do - @attribute1.count.should == Aggregation.new(@attribute1, "COUNT") + it "manufactures a count Expression" do + @attribute1.count.should == Expression.new(@attribute1, "COUNT") end end describe '#sum' do - it "manufactures a sum aggregation" do - @attribute1.sum.should == Aggregation.new(@attribute1, "SUM") + it "manufactures a sum Expression" do + @attribute1.sum.should == Expression.new(@attribute1, "SUM") end end describe '#maximum' do - it "manufactures a maximum aggregation" do - @attribute1.maximum.should == Aggregation.new(@attribute1, "MAX") + it "manufactures a maximum Expression" do + @attribute1.maximum.should == Expression.new(@attribute1, "MAX") end end describe '#minimum' do - it "manufactures a minimum aggregation" do - @attribute1.minimum.should == Aggregation.new(@attribute1, "MIN") + it "manufactures a minimum Expression" do + @attribute1.minimum.should == Expression.new(@attribute1, "MIN") end end describe '#average' do - it "manufactures an average aggregation" do - @attribute1.average.should == Aggregation.new(@attribute1, "AVG") + it "manufactures an average Expression" do + @attribute1.average.should == Expression.new(@attribute1, "AVG") end end end diff --git a/spec/active_relation/relations/alias_spec.rb b/spec/active_relation/relations/alias_spec.rb index 6c203990eb..c02b0df453 100644 --- a/spec/active_relation/relations/alias_spec.rb +++ b/spec/active_relation/relations/alias_spec.rb @@ -6,24 +6,5 @@ module ActiveRelation @relation = Table.new(:users) @alias_relation = @relation.as(:foo) end - - describe '#name' do - it 'returns the alias' do - @alias_relation.name.should == :foo - end - end - - describe '#attributes' do - it 'manufactures sql deleting a table relation' do - @alias_relation.attributes.should == @relation.attributes.collect { |a| Attribute.new(@alias_relation, a.name) } - end - end - - describe '[]' do - it 'manufactures attributes associated with the aliased relation' do - @alias_relation[:id].relation.should == @alias_relation - @alias_relation[:does_not_exist].should be_nil - end - 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 index 6309547c7d..a03b0206a9 100644 --- a/spec/active_relation/relations/compound_spec.rb +++ b/spec/active_relation/relations/compound_spec.rb @@ -3,13 +3,50 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') module ActiveRelation describe Compound do before do - @relation = Table.new(:users) - class ConcreteCompound < Compound def initialize(relation) @relation = relation end end + @relation = Table.new(:users) + @compound_relation = ConcreteCompound.new(@relation) + end + + 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) } + 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[: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 + end + end + + describe 'when given an', Expression do + before do + @nested_expression = Expression.new(Attribute.new(@relation, :id), "COUNT") + @unprojected_expression = Expression.new(Attribute.new(@relation, :id), "SUM") + @compound_relation = ConcreteCompound.new(Aggregation.new(@relation, :expressions => [@nested_expression])) + 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[@compound_relation[@expression]].should == @compound_relation[@expression] + @compound_relation[@unprojected_expression].should be_nil + end + end end end end
\ No newline at end of file diff --git a/spec/active_relation/relations/group_spec.rb b/spec/active_relation/relations/group_spec.rb deleted file mode 100644 index 532d6faea6..0000000000 --- a/spec/active_relation/relations/group_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') - -module ActiveRelation - describe Group do - before do - @relation1 = Table.new(:foo) - @relation2 = Table.new(:bar) - @attribute1 = @relation1[:id] - @attribute2 = @relation2[:id] - end - - describe '#qualify' do - it "distributes over the relation and attributes" do - Group.new(@relation1, @attribute1).qualify. \ - should == Group.new(@relation1.qualify, @attribute1.qualify) - end - end - - describe '#aggregation?' do - it "is true" do - Group.new(@relation1, @attribute1).should be_aggregation - end - end - - describe '#to_sql' do - it "manufactures sql with an order clause" do - Group.new(@relation1, @attribute1).to_sql.should be_like(""" - SELECT `foo`.`name`, `foo`.`id` - FROM `foo` - GROUP BY `foo`.`id` - """) - end - end - end -end diff --git a/spec/active_relation/relations/join_spec.rb b/spec/active_relation/relations/join_spec.rb index 0f30d11f98..5235a5b528 100644 --- a/spec/active_relation/relations/join_spec.rb +++ b/spec/active_relation/relations/join_spec.rb @@ -18,6 +18,12 @@ module ActiveRelation Join.new("INNER JOIN", @relation1, @relation2, @predicate).should == Join.new("INNER JOIN", @relation2, @relation1, @predicate) end end + + describe '[]' do + it "" do + pending + end + end describe '#qualify' do it 'distributes over the relations and predicates' do @@ -63,17 +69,16 @@ module ActiveRelation end end - describe 'aggregated relations' do before do @relation = Table.new(:users) photos = Table.new(:photos) - @aggregate_relation = photos.project(photos[:user_id], photos[:id].count).rename(photos[:id].count, :cnt) \ - .group(photos[:user_id]).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 aggregation on the right' do + describe 'with the expression 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` @@ -84,9 +89,8 @@ module ActiveRelation end end - describe 'with the aggregation on the left' do + describe 'with the expression on the left' do it 'manufactures sql joining the right table to a derived table' do - pending 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` FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photo_count` @@ -96,7 +100,7 @@ module ActiveRelation end end - it "keeps selects on the aggregation within the derived table" do + it "keeps selects on the expression within the derived table" do pending 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` diff --git a/spec/active_relation/relations/projection_spec.rb b/spec/active_relation/relations/projection_spec.rb index 8d6a093af2..e792eec3b8 100644 --- a/spec/active_relation/relations/projection_spec.rb +++ b/spec/active_relation/relations/projection_spec.rb @@ -9,6 +9,10 @@ module ActiveRelation @attribute2 = @relation2[:id] end + it "needs to test that [] is limited" do + pending + end + describe '==' do it "obtains if the relations and attributes are identical" do Projection.new(@relation1, @attribute1, @attribute2).should == Projection.new(@relation1, @attribute1, @attribute2) diff --git a/spec/active_relation/relations/relation_spec.rb b/spec/active_relation/relations/relation_spec.rb index 05330206e0..e28487b373 100644 --- a/spec/active_relation/relations/relation_spec.rb +++ b/spec/active_relation/relations/relation_spec.rb @@ -10,10 +10,6 @@ module ActiveRelation end describe '[]' do - it "manufactures an attribute when given a symbol" do - @relation1[:id].should == Attribute.new(@relation1, :id) - end - it "manufactures a range relation when given a range" do @relation1[1..2].should == Range.new(@relation1, 1..2) end @@ -25,7 +21,7 @@ module ActiveRelation end end - describe '#aggregation?' do + describe '#Expression?' do it "returns false" do @relation1.should_not be_aggregation end @@ -88,9 +84,10 @@ module ActiveRelation end end - describe '#group' do + describe '#aggregate' do it 'manufactures a group relation' do - @relation1.group(@attribute1).should == Group.new(@relation1, @attribute1) + @relation1.aggregate(@expression1, @expression2).group(@attribute1, @attribute2). \ + should == Aggregation.new(@relation1, :expressions => [@expresion, @expression2], :groupings => [@attribute1, @attribute2]) end end end diff --git a/spec/active_relation/relations/rename_spec.rb b/spec/active_relation/relations/rename_spec.rb index 1616d5fdb7..65ad9c51bd 100644 --- a/spec/active_relation/relations/rename_spec.rb +++ b/spec/active_relation/relations/rename_spec.rb @@ -25,15 +25,61 @@ module ActiveRelation describe '#attributes' do it "manufactures a list of attributes with the renamed attribute renameed" do - Rename.new(@relation1, @relation1[:id] => :schmid).attributes.should == - (@relation1.attributes - [@relation1[:id]]) + [@relation1[:id].as(:schmid)] + @renamed_relation.attributes.should include(Attribute.new(@renamed_relation, :id, :schmid)) + @renamed_relation.should have(@relation1.attributes.size).attributes end end describe '[]' do - it 'indexes attributes by rename' do - @renamed_relation[:id].should be_nil - @renamed_relation[:schmid].should == @relation1[:id].as(:schmid) + 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[: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 + end + end + + describe 'when given an', Expression do + it "manufactures a substituted and renamed expression if the expression is within the relation" do + end + end + + describe 'when the rename is constructed with a derived attribute' do + before do + @renamed_renamed_relation = Rename.new(@renamed_relation, @relation1[:id] => :flid) + end + + describe 'when given a', Symbol do + 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) + 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) + end + end + + describe 'when given an', Expression do + 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)] + end + end end end @@ -45,7 +91,7 @@ module ActiveRelation end describe '#to_sql' do - it 'manufactures sql renameing the attribute' do + it 'manufactures sql renaming the attribute' do @renamed_relation.to_sql.should be_like(""" SELECT `foo`.`name`, `foo`.`id` AS 'schmid' FROM `foo` diff --git a/spec/active_relation/relations/schmoin_spec.rb b/spec/active_relation/relations/schmoin_spec.rb deleted file mode 100644 index e5ed9be393..0000000000 --- a/spec/active_relation/relations/schmoin_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') - -module ActiveRelation - describe Schmoin do - before do - @relation = Table.new(:users) - photos = Table.new(:photos) - @aggregate_relation = photos.project(photos[:user_id], photos[:id].count).rename(photos[:id].count, :cnt) \ - .group(photos[:user_id]).as(:photo_count) - @predicate = Equality.new(@aggregate_relation[:user_id], @relation[:id]) - end - - describe '#to_sql' do - it 'manufactures sql joining the two tables on the predicate, merging the selects' do - Schmoin.new("INNER JOIN", @relation, @aggregate_relation, @predicate).to_sql.should be_like(""" - SELECT `users`.`name`, `users`.`id`, `photo_count`.`user_id`, `photo_count`.`cnt` - FROM `users` - INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photo_count` - ON `photo_count`.`user_id` = `users`.`id` - """) - end - end - end -end
\ No newline at end of file diff --git a/spec/active_relation/relations/table_spec.rb b/spec/active_relation/relations/table_spec.rb index c8679707f5..140346e6f6 100644 --- a/spec/active_relation/relations/table_spec.rb +++ b/spec/active_relation/relations/table_spec.rb @@ -6,6 +6,32 @@ module ActiveRelation @relation = Table.new(:users) 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 + 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 + end + end + + describe 'when given an', Expression do + before do + @expression = Expression.new(Attribute.new(@relation, :id), "COUNT") + end + + it "returns the Expression if the Expression is within the relation" do + @relation[@expression].should be_nil + end + end + end + describe '#to_sql' do it "manufactures a simple select query" do @relation.to_sql.should be_like(""" @@ -26,9 +52,7 @@ module ActiveRelation describe '#qualify' do it 'manufactures a rename relation with all attribute names qualified' do - @relation.qualify.should == Rename.new( - Rename.new(@relation, @relation[:id] => 'users.id'), @relation[:name] => 'users.name' - ) + @relation.qualify.should == Rename.new(@relation, @relation[:id] => 'users.id', @relation[:name] => 'users.name') end end end |