diff options
Diffstat (limited to 'spec/arel/unit/relations/join_spec.rb')
-rw-r--r-- | spec/arel/unit/relations/join_spec.rb | 89 |
1 files changed, 42 insertions, 47 deletions
diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb index 12b7d7a593..15c00a4f24 100644 --- a/spec/arel/unit/relations/join_spec.rb +++ b/spec/arel/unit/relations/join_spec.rb @@ -31,31 +31,6 @@ module Arel .should hash_the_same_as(Join.new("INNER JOIN", @relation1, @relation2, @predicate)) end end - - describe '#prefix_for' do - describe 'when the joined relations are simple' do - it "returns the name of the relation containing the attribute" do - Join.new("INNER JOIN", @relation1, @relation2, @predicate).prefix_for(@relation1[:id]) \ - .should == @relation1.prefix_for(@relation1[:id]) - Join.new("INNER JOIN", @relation1, @relation2, @predicate).prefix_for(@relation2[:id]) \ - .should == @relation2.prefix_for(@relation2[:id]) - - end - end - - describe 'when one of the joined relations is an alias' do - before do - @aliased_relation = @relation1.as(:alias) - end - - it "returns the alias of the relation containing the attribute" do - Join.new("INNER JOIN", @aliased_relation, @relation2, @predicate).prefix_for(@aliased_relation[:id]) \ - .should == @aliased_relation.alias - Join.new("INNER JOIN", @aliased_relation, @relation2, @predicate).prefix_for(@relation2[:id]) \ - .should == @relation2.prefix_for(@relation2[:id]) - end - end - end describe '#engine' do it "delegates to a relation's engine" do @@ -71,6 +46,15 @@ module Arel (@relation1.attributes + @relation2.attributes).collect { |a| a.bind(join) } end end + + describe '#prefix_for' do + it "returns the name of the relation containing the attribute" do + Join.new("INNER JOIN", @relation1, @relation2, @predicate).prefix_for(@relation1[:id]) \ + .should == @relation1.prefix_for(@relation1[:id]) + Join.new("INNER JOIN", @relation1, @relation2, @predicate).prefix_for(@relation2[:id]) \ + .should == @relation2.prefix_for(@relation2[:id]) + end + end describe '#to_sql' do it 'manufactures sql joining the two tables on the predicate' do @@ -89,7 +73,7 @@ module Arel INNER JOIN `photos` ON `users`.`id` = `photos`.`user_id` WHERE `users`.`id` = 1 AND `photos`.`id` = 2 - ") + ") end end end @@ -99,7 +83,6 @@ module Arel @aggregation = @relation2 \ .group(@relation2[:user_id]) \ .project(@relation2[:user_id], @relation2[:id].count.as(:cnt)) \ - .as('photo_count') end describe '#attributes' do @@ -114,10 +97,10 @@ module Arel describe 'with the aggregation on the right' do it 'manufactures sql joining the left table to a derived table' do Join.new("INNER JOIN", @relation1, @aggregation, @predicate).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name`, `photo_count`.`user_id`, `photo_count`.`cnt` + SELECT `users`.`id`, `users`.`name`, `photos_aggregation`.`user_id`, `photos_aggregation`.`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 `users`.`id` = `photo_count`.`user_id` + INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_aggregation` + ON `users`.`id` = `photos_aggregation`.`user_id` ") end end @@ -125,45 +108,57 @@ module Arel describe 'with the aggregation on the left' do it 'manufactures sql joining the right table to a derived table' do Join.new("INNER JOIN", @aggregation, @relation1, @predicate).to_sql.should be_like(" - SELECT `photo_count`.`user_id`, `photo_count`.`cnt`, `users`.`id`, `users`.`name` - FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photo_count` + SELECT `photos_aggregation`.`user_id`, `photos_aggregation`.`cnt`, `users`.`id`, `users`.`name` + FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_aggregation` INNER JOIN `users` - ON `users`.`id` = `photo_count`.`user_id` + ON `users`.`id` = `photos_aggregation`.`user_id` ") end end it "keeps selects on the aggregation within the derived table" do Join.new("INNER JOIN", @relation1, @aggregation.select(@aggregation[:user_id].eq(1)), @predicate).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name`, `photo_count`.`user_id`, `photo_count`.`cnt` + SELECT `users`.`id`, `users`.`name`, `photos_aggregation`.`user_id`, `photos_aggregation`.`cnt` FROM `users` - 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 `users`.`id` = `photo_count`.`user_id` + INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` WHERE `photos`.`user_id` = 1 GROUP BY `photos`.`user_id`) AS `photos_aggregation` + ON `users`.`id` = `photos_aggregation`.`user_id` ") end end end - describe 'when joining aliased relations' do + describe 'when joining a relation to itself' do before do @aliased_relation = @relation1.as(:alias) - end + @predicate = @relation1[:id].eq(@aliased_relation[:id]) + end - describe '#to_sql' do - it 'aliases the table and attributes properly' do - @relation1.join(@aliased_relation).on(@relation1[:id].eq(@aliased_relation[:id])).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name`, `alias`.`id`, `alias`.`name` - FROM `users` - INNER JOIN `users` AS `alias` - ON `users`.`id` = `alias`.`id` - ") + describe '#prefix_for' do + it "returns the alias of the relation containing the attribute" do + relation = Join.new("INNER JOIN", @relation1, @aliased_relation, @predicate) + relation.prefix_for(@aliased_relation[:id]) \ + .should == @relation1.name + relation.prefix_for(@relation1[:id]) \ + .should == @relation1.name + '_2' end end describe 'when joining the same relation to itself' do + describe '#to_sql' do + it 'aliases the table and attributes properly in selects' + it 'aliases the table and attributes properly in the join predicate' do + @relation1.join(@aliased_relation).on(@relation1[:id].eq(@aliased_relation[:id])).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name` + FROM `users` + INNER JOIN `users` AS `users_2` + ON `users`.`id` = `users_2`.`id` + ") + end + end + describe '[]' do describe 'when given an attribute belonging to both sub-relations' do - it '' do + it 'disambiguates the ...' do relation = @relation1.join(@aliased_relation).on(@relation1[:id].eq(@aliased_relation[:id])) relation[@relation1[:id]].ancestor.should == @relation1[:id] relation[@aliased_relation[:id]].ancestor.should == @aliased_relation[:id] |