diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-21 18:47:22 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-21 18:47:22 -0800 |
commit | 559d9bbfe09034e807e5eae169bea26df780aa36 (patch) | |
tree | 472c35a20a5d331961e6ed1488373646c9574e60 /spec | |
parent | d62ace142ce873c72eb916f5a14aa33a67ecfb86 (diff) | |
download | rails-559d9bbfe09034e807e5eae169bea26df780aa36.tar.gz rails-559d9bbfe09034e807e5eae169bea26df780aa36.tar.bz2 rails-559d9bbfe09034e807e5eae169bea26df780aa36.zip |
merging "schmoin" experiment with joining aggregations into regular "join" functionality; half-way done...
Diffstat (limited to 'spec')
-rw-r--r-- | spec/active_relation/relations/group_spec.rb | 6 | ||||
-rw-r--r-- | spec/active_relation/relations/join_spec.rb | 56 | ||||
-rw-r--r-- | spec/active_relation/relations/relation_spec.rb | 6 |
3 files changed, 54 insertions, 14 deletions
diff --git a/spec/active_relation/relations/group_spec.rb b/spec/active_relation/relations/group_spec.rb index 256d7dde45..532d6faea6 100644 --- a/spec/active_relation/relations/group_spec.rb +++ b/spec/active_relation/relations/group_spec.rb @@ -15,6 +15,12 @@ module ActiveRelation 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 diff --git a/spec/active_relation/relations/join_spec.rb b/spec/active_relation/relations/join_spec.rb index 68d7b83a12..00a6218c03 100644 --- a/spec/active_relation/relations/join_spec.rb +++ b/spec/active_relation/relations/join_spec.rb @@ -27,25 +27,53 @@ module ActiveRelation end describe '#attributes' do - it 'combines the attributes of the two relations' do - Join.new("INNER JOIN", @relation1, @relation2, @predicate).attributes.should == - @relation1.attributes + @relation2.attributes + describe 'with simple relations' do + it 'combines the attributes of the two relations' do + Join.new("INNER JOIN", @relation1, @relation2, @predicate).attributes.should == + @relation1.attributes + @relation2.attributes + end + end + + describe 'with aggregated relations' do + it '' do + end end end describe '#to_sql' do - before do - @relation1 = @relation1.select(@relation1[:id].equals(1)) + describe 'with simple relations' do + before do + @relation1 = @relation1.select(@relation1[:id].equals(1)) + end + + it 'manufactures sql joining the two tables on the predicate, merging the selects' do + Join.new("INNER JOIN", @relation1, @relation2, @predicate).to_sql.should be_like(""" + SELECT `foo`.`name`, `foo`.`id`, `bar`.`name`, `bar`.`foo_id`, `bar`.`id` + FROM `foo` + INNER JOIN `bar` ON `foo`.`id` = `bar`.`id` + WHERE + `foo`.`id` = 1 + """) + end end - - it 'manufactures sql joining the two tables on the predicate, merging the selects' do - Join.new("INNER JOIN", @relation1, @relation2, @predicate).to_sql.should be_like(""" - SELECT `foo`.`name`, `foo`.`id`, `bar`.`name`, `bar`.`foo_id`, `bar`.`id` - FROM `foo` - INNER JOIN `bar` ON `foo`.`id` = `bar`.`id` - WHERE - `foo`.`id` = 1 - """) + + describe 'with 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) + @predicate = Equality.new(@aggregate_relation[:user_id], @relation[:id]) + end + + 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` + 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 diff --git a/spec/active_relation/relations/relation_spec.rb b/spec/active_relation/relations/relation_spec.rb index 1b4ab785b7..f78f1b31b7 100644 --- a/spec/active_relation/relations/relation_spec.rb +++ b/spec/active_relation/relations/relation_spec.rb @@ -24,6 +24,12 @@ module ActiveRelation @relation1.include?(@attribute1).should be_kind_of(RelationInclusion) end end + + describe '#aggregation?' do + it "returns false" do + @relation1.should_not be_aggregation + end + end describe 'read operations' do describe 'joins' do |