aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-04-28 14:01:33 -0700
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-04-28 14:01:33 -0700
commit09b2406e829cee71e4543a75fc4fbe226157d35e (patch)
treeee8a230c3b9c1d4f31cef38defe269f6b8868859 /spec
parent3ae7a601e962468468eac1399ebdfd7113730310 (diff)
downloadrails-09b2406e829cee71e4543a75fc4fbe226157d35e.tar.gz
rails-09b2406e829cee71e4543a75fc4fbe226157d35e.tar.bz2
rails-09b2406e829cee71e4543a75fc4fbe226157d35e.zip
automatically aliasing tables
Diffstat (limited to 'spec')
-rw-r--r--spec/arel/unit/primitives/attribute_spec.rb17
-rw-r--r--spec/arel/unit/relations/join_spec.rb89
2 files changed, 42 insertions, 64 deletions
diff --git a/spec/arel/unit/primitives/attribute_spec.rb b/spec/arel/unit/primitives/attribute_spec.rb
index ac9afa85c7..561c47da16 100644
--- a/spec/arel/unit/primitives/attribute_spec.rb
+++ b/spec/arel/unit/primitives/attribute_spec.rb
@@ -38,12 +38,6 @@ module Arel
end
end
- describe '#qualified_name' do
- it "manufactures an attribute name prefixed with the relation's name" do
- @attribute.qualified_name.should == "#{@relation.prefix_for(@attribute)}.id"
- end
- end
-
describe '#engine' do
it "delegates to its relation" do
Attribute.new(@relation, :id).engine.should == @relation.engine
@@ -77,17 +71,6 @@ module Arel
@attribute.to_sql.should be_like("`users`.`id`")
end
end
-
- describe 'for an attribute in a join relation where the source relation is aliased' do
- before do
- another_relation = Table.new(:photos)
- @join_with_alias = @relation.as(:alias).join(another_relation).on(@relation[:id].eq(another_relation[:user_id]))
- end
-
- it "manufactures sql with an alias" do
- @join_with_alias[@attribute].to_sql.should be_like("`alias`.`id`")
- end
- end
end
describe Attribute::Predications do
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]