From 5e52a24e67dfedaf9b2a3bb6601f79f1f8e00c72 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sun, 4 May 2008 17:23:22 -0700 Subject: additional testing --- lib/arel/relations/relation.rb | 9 +-- spec/arel/unit/relations/join_spec.rb | 106 +++++++++++++++++++++++++--------- 2 files changed, 80 insertions(+), 35 deletions(-) diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 62656773dd..846594e7cd 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -115,8 +115,8 @@ module Arel "SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ')}", "FROM #{table_sql(Sql::TableReference.new(self))}", (joins(Sql::TableReference.new(self)) unless joins.blank? ), - ("WHERE #{selects.collect { |s| s.to_sql(Sql::WhereClause.new(self)) }.join("\n\tAND ")}" unless selects.blank? ), - ("ORDER BY #{orders.collect { |o| o.to_sql(Sql::OrderClause.new(self)) }.join(', ')}" unless orders.blank? ), + ("WHERE #{selects.collect { |s| s.to_sql(Sql::WhereClause.new(self)) }.join("\n\tAND ")}" unless selects.blank? ), + ("ORDER BY #{orders.collect { |o| o.to_sql(Sql::OrderClause.new(self)) }.join(', ')}" unless orders.blank? ), ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), ("LIMIT #{taken}" unless taken.blank? ), ("OFFSET #{skipped}" unless skipped.blank? ) @@ -162,11 +162,6 @@ module Arel self end - # INVESTIGATE - def format(object) - object.to_sql(Sql::WhereCondition.new(self)) - end - def christener self end diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb index 52e1a93b8e..9cd7a13ed7 100644 --- a/spec/arel/unit/relations/join_spec.rb +++ b/spec/arel/unit/relations/join_spec.rb @@ -107,14 +107,28 @@ module Arel end end - # TESTME try other direction too! - 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`, `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 `photos_aggregation` - ON `users`.`id` = `photos_aggregation`.`user_id` - ") + describe 'when the aggration has a selection' do + describe 'with the aggregation on the left' do + it "manufactures sql keeping 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`, `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 `photos_aggregation` + ON `users`.`id` = `photos_aggregation`.`user_id` + ") + end + end + + describe 'with the aggregation on the right' do + it "manufactures sql keeping selects on the aggregation within the derived table" do + Join.new("INNER JOIN", @aggregation.select(@aggregation[:user_id].eq(1)), @relation1, @predicate).to_sql.should be_like(" + SELECT `photos_aggregation`.`user_id`, `photos_aggregation`.`cnt`, `users`.`id`, `users`.`name` + FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` WHERE `photos`.`user_id` = 1 GROUP BY `photos`.`user_id`) AS `photos_aggregation` + INNER JOIN `users` + ON `users`.`id` = `photos_aggregation`.`user_id` + ") + end + end end end end @@ -127,38 +141,74 @@ module Arel describe 'when joining the same relation to itself' do describe '#to_sql' do - it '' do - relation2 = @relation1.alias - relation3 = @relation1.alias - @relation1 \ - .join(relation2.join(relation3).on(relation2[:id].eq(relation3[:id]))) \ - .on(@relation1[:id].eq(relation2[:id])) \ - .select(@relation1[:id].eq(1)) \ - .to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name` + it 'manufactures sql aliasing the table and attributes properly in the join predicate and the where clause' do + @relation1.join(@aliased_relation).on(@predicate).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` - INNER JOIN `users` AS `users_3` - ON `users_2`.`id` = `users_3`.`id` - WHERE `users`.`id` = 1 ") end - it 'aliases the table and attributes properly in the join predicate and the where clause' 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` - ") + describe 'when joining with a selection on the same relation' do + it 'manufactures sql aliasing the tables properly' do + @relation1 \ + .join(@aliased_relation.select(@aliased_relation[:id].eq(1))) \ + .on(@predicate) \ + .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` + WHERE `users_2`.`id` = 1 + ") + end + end + + describe 'when joining the same relation to itself multiple times' do + before do + @relation2 = @relation1.alias + @relation3 = @relation1.alias + end + + describe 'when joining left-associatively' do + it 'manufactures sql aliasing the tables properly' do + @relation1 \ + .join(@relation2.join(@relation3).on(@relation2[:id].eq(@relation3[:id]))) \ + .on(@relation1[:id].eq(@relation2[:id])) \ + .to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name` + FROM `users` + INNER JOIN `users` AS `users_2` + ON `users`.`id` = `users_2`.`id` + INNER JOIN `users` AS `users_3` + ON `users_2`.`id` = `users_3`.`id` + ") + end + end + + describe 'when joining right-associatively' do + it 'manufactures sql aliasing the tables properly' do + @relation1 \ + .join(@relation2).on(@relation1[:id].eq(@relation2[:id])) \ + .join(@relation3).on(@relation2[:id].eq(@relation3[:id])) \ + .to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name` + FROM `users` + INNER JOIN `users` AS `users_2` + ON `users`.`id` = `users_2`.`id` + INNER JOIN `users` AS `users_3` + ON `users_2`.`id` = `users_3`.`id` + ") + end + end end end describe '[]' do describe 'when given an attribute belonging to both sub-relations' do it 'disambiguates the relation that serves as the ancestor to the attribute' do - relation = @relation1.join(@aliased_relation).on(@relation1[:id].eq(@aliased_relation[:id])) + relation = @relation1.join(@aliased_relation).on(@predicate) relation[@relation1[:id]].ancestor.should == @relation1[:id] relation[@aliased_relation[:id]].ancestor.should == @aliased_relation[:id] end -- cgit v1.2.3