aboutsummaryrefslogtreecommitdiffstats
path: root/spec/active_relation/relations/join_spec.rb
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-21 18:47:22 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-21 18:47:22 -0800
commit559d9bbfe09034e807e5eae169bea26df780aa36 (patch)
tree472c35a20a5d331961e6ed1488373646c9574e60 /spec/active_relation/relations/join_spec.rb
parentd62ace142ce873c72eb916f5a14aa33a67ecfb86 (diff)
downloadrails-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/active_relation/relations/join_spec.rb')
-rw-r--r--spec/active_relation/relations/join_spec.rb56
1 files changed, 42 insertions, 14 deletions
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