diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-21 20:02:48 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-21 20:02:48 -0800 |
commit | 7f35d492372fd91cd35e7f9a2408efd64d28ccf5 (patch) | |
tree | 569b2217f861f99fa32104c7c9cc00ec09e50830 /spec | |
parent | 559d9bbfe09034e807e5eae169bea26df780aa36 (diff) | |
download | rails-7f35d492372fd91cd35e7f9a2408efd64d28ccf5.tar.gz rails-7f35d492372fd91cd35e7f9a2408efd64d28ccf5.tar.bz2 rails-7f35d492372fd91cd35e7f9a2408efd64d28ccf5.zip |
joining on aggregations; this time where the aggregation is on the right.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/active_relation/relations/join_spec.rb | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/spec/active_relation/relations/join_spec.rb b/spec/active_relation/relations/join_spec.rb index 00a6218c03..422a93772d 100644 --- a/spec/active_relation/relations/join_spec.rb +++ b/spec/active_relation/relations/join_spec.rb @@ -42,22 +42,27 @@ module ActiveRelation describe '#to_sql' do 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 + it 'manufactures sql joining the two tables on the predicate' 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 + + it 'manufactures sql joining the two tables, merging any selects' do + Join.new("INNER JOIN", @relation1.select(@relation1[:id].equals(1)), + @relation2.select(@relation2[:id].equals(2)), @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 + AND `bar`.`id` = 2 + """) + end end - describe 'with aggregated relations' do + describe 'aggregated relations' do before do @relation = Table.new(:users) photos = Table.new(:photos) @@ -66,11 +71,34 @@ module ActiveRelation @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(""" + 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` + 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 + + 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` + FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photo_count` + INNER JOIN `users` + ON `photo_count`.`user_id` = `users`.`id` + """) + end + end + + it "keeps selects on the aggregation 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` FROM `users` - INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photo_count` + INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` WHERE `photos`.`user_id` = 1 GROUP BY `photos`.`user_id`) AS `photo_count` ON `photo_count`.`user_id` = `users`.`id` """) end |