aboutsummaryrefslogtreecommitdiffstats
path: root/spec/arel/engines/sql/integration
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-05-26 12:41:52 -0300
committerEmilio Tagua <miloops@gmail.com>2009-05-26 12:41:52 -0300
commitc9bbea6115be520dbd47bd30108c5622289deb26 (patch)
treeec418e01954c1bd2dcfebc7fbc8220fb04b50baf /spec/arel/engines/sql/integration
parentae1e0ac5e98a7e5a2894d0a431f8c34af6575cae (diff)
parent86364591af807ed3fa4a7304f53e6f3458cb4961 (diff)
downloadrails-c9bbea6115be520dbd47bd30108c5622289deb26.tar.gz
rails-c9bbea6115be520dbd47bd30108c5622289deb26.tar.bz2
rails-c9bbea6115be520dbd47bd30108c5622289deb26.zip
Merge commit 'brynary/master'
Conflicts: lib/arel.rb lib/arel/session.rb
Diffstat (limited to 'spec/arel/engines/sql/integration')
-rw-r--r--spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb209
-rw-r--r--spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb167
-rw-r--r--spec/arel/engines/sql/integration/joins/with_compounds_spec.rb107
3 files changed, 483 insertions, 0 deletions
diff --git a/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb b/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb
new file mode 100644
index 0000000000..50b0908441
--- /dev/null
+++ b/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb
@@ -0,0 +1,209 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Join do
+ before do
+ @relation1 = Table(:users)
+ @relation2 = @relation1.alias
+ @predicate = @relation1[:id].eq(@relation2[:id])
+ end
+
+ describe 'when joining a relation to itself' do
+ describe '#to_sql' do
+ it 'manufactures sql aliasing the table and attributes properly in the join predicate and the where clause' do
+ sql = @relation1.join(@relation2).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ 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
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ 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 'when joining with a where on the same relation' do
+ it 'manufactures sql aliasing the tables properly' do
+ sql = @relation1 \
+ .join(@relation2.where(@relation2[:id].eq(1))) \
+ .on(@predicate) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ 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` AND `users_2`.`id` = 1
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ 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" AND "users_2"."id" = 1
+ })
+ end
+ end
+
+ describe 'when the where occurs before the alias' do
+ it 'manufactures sql aliasing the predicates properly' do
+ relation2 = @relation1.where(@relation1[:id].eq(1)).alias
+
+ sql = @relation1 \
+ .join(relation2) \
+ .on(relation2[:id].eq(@relation1[:id])) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
+ FROM `users`
+ INNER JOIN `users` AS `users_2`
+ ON `users_2`.`id` = `users`.`id` AND `users_2`.`id` = 1
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
+ FROM "users"
+ INNER JOIN "users" AS "users_2"
+ ON "users_2"."id" = "users"."id" AND "users_2"."id" = 1
+ })
+ end
+ end
+ end
+ end
+
+ describe 'when joining the relation to itself multiple times' do
+ before do
+ @relation3 = @relation1.alias
+ end
+
+ describe 'when joining left-associatively' do
+ it 'manufactures sql aliasing the tables properly' do
+ sql = @relation1 \
+ .join(@relation2 \
+ .join(@relation3) \
+ .on(@relation2[:id].eq(@relation3[:id]))) \
+ .on(@relation1[:id].eq(@relation2[:id])) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ 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
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ 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
+
+ describe 'when joining right-associatively' do
+ it 'manufactures sql aliasing the tables properly' do
+ sql = @relation1 \
+ .join(@relation2).on(@relation1[:id].eq(@relation2[:id])) \
+ .join(@relation3).on(@relation2[:id].eq(@relation3[:id])) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ 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
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ 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
+ 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
+ @relation1 \
+ .join(@relation2) \
+ .on(@predicate) \
+ .should disambiguate_attributes(@relation1[:id], @relation2[:id])
+ end
+
+ describe 'when both relations are compound and only one is an alias' do
+ it 'disambiguates the relation that serves as the ancestor to the attribute' do
+ compound1 = @relation1.where(@predicate)
+ compound2 = compound1.alias
+ compound1 \
+ .join(compound2) \
+ .on(@predicate) \
+ .should disambiguate_attributes(compound1[:id], compound2[:id])
+ end
+ end
+
+ describe 'when the left relation is extremely compound' do
+ it 'disambiguates the relation that serves as the ancestor to the attribute' do
+ @relation1 \
+ .where(@predicate) \
+ .where(@predicate) \
+ .join(@relation2) \
+ .on(@predicate) \
+ .should disambiguate_attributes(@relation1[:id], @relation2[:id])
+ end
+ end
+
+ describe 'when the right relation is extremely compound' do
+ it 'disambiguates the relation that serves as the ancestor to the attribute' do
+ @relation1 \
+ .join( \
+ @relation2 \
+ .where(@predicate) \
+ .where(@predicate) \
+ .where(@predicate)) \
+ .on(@predicate) \
+ .should disambiguate_attributes(@relation1[:id], @relation2[:id])
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb b/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb
new file mode 100644
index 0000000000..709ae9f8d1
--- /dev/null
+++ b/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb
@@ -0,0 +1,167 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Join do
+ before do
+ @relation1 = Table(:users)
+ @relation2 = Table(:photos)
+ @predicate = @relation1[:id].eq(@relation2[:user_id])
+ end
+
+ describe 'when joining aggregated relations' do
+ before do
+ @aggregation = @relation2 \
+ .group(@relation2[:user_id]) \
+ .project(@relation2[:user_id], @relation2[:id].count.as(:cnt)) \
+ end
+
+ describe '#to_sql' do
+ # CLEANUP
+ it '' do
+ sql = @relation1.join(@relation2.take(3)).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos_external`.`id`, `photos_external`.`user_id`, `photos_external`.`camera_id`
+ FROM `users`
+ INNER JOIN (SELECT `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id` FROM `photos` LIMIT 3) AS `photos_external`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos_external"."id", "photos_external"."user_id", "photos_external"."camera_id"
+ FROM "users"
+ INNER JOIN (SELECT "photos"."id", "photos"."user_id", "photos"."camera_id" FROM "photos" LIMIT 3) AS "photos_external"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+
+ describe 'with the aggregation on the right' do
+ it 'manufactures sql joining the left table to a derived table' do
+ sql = @relation1.join(@aggregation).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos_external`.`user_id`, `photos_external`.`cnt`
+ FROM `users`
+ INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos_external"."user_id", "photos_external"."cnt"
+ FROM "users"
+ INNER JOIN (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+
+ describe 'with the aggregation on the left' do
+ it 'manufactures sql joining the right table to a derived table' do
+ sql = @aggregation.join(@relation1).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `photos_external`.`user_id`, `photos_external`.`cnt`, `users`.`id`, `users`.`name`
+ FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external`
+ INNER JOIN `users`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "photos_external"."user_id", "photos_external"."cnt", "users"."id", "users"."name"
+ FROM (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external"
+ INNER JOIN "users"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+
+ describe 'with the aggregation on both sides' do
+ it 'it properly aliases the aggregations' do
+ aggregation2 = @aggregation.alias
+ sql = @aggregation.join(aggregation2).on(aggregation2[:user_id].eq(@aggregation[:user_id])).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `photos_external`.`user_id`, `photos_external`.`cnt`, `photos_external_2`.`user_id`, `photos_external_2`.`cnt`
+ FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external`
+ INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external_2`
+ ON `photos_external_2`.`user_id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "photos_external"."user_id", "photos_external"."cnt", "photos_external_2"."user_id", "photos_external_2"."cnt"
+ FROM (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external"
+ INNER JOIN (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external_2"
+ ON "photos_external_2"."user_id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+
+ describe 'when the aggration has a where' do
+ describe 'with the aggregation on the left' do
+ it "manufactures sql keeping wheres on the aggregation within the derived table" do
+ sql = @relation1.join(@aggregation.where(@aggregation[:user_id].eq(1))).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos_external`.`user_id`, `photos_external`.`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_external`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos_external"."user_id", "photos_external"."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_external"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+
+ describe 'with the aggregation on the right' do
+ it "manufactures sql keeping wheres on the aggregation within the derived table" do
+ sql = @aggregation.where(@aggregation[:user_id].eq(1)).join(@relation1).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `photos_external`.`user_id`, `photos_external`.`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_external`
+ INNER JOIN `users`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "photos_external"."user_id", "photos_external"."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_external"
+ INNER JOIN "users"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb b/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb
new file mode 100644
index 0000000000..4bceef4975
--- /dev/null
+++ b/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb
@@ -0,0 +1,107 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Join do
+ before do
+ @relation1 = Table(:users)
+ @relation2 = Table(:photos)
+ @predicate = @relation1[:id].eq(@relation2[:user_id])
+ end
+
+ describe '#to_sql' do
+ describe 'when the join contains a where' do
+ describe 'and the where is given a string' do
+ it 'does not escape the string' do
+ sql = @relation1 \
+ .join(@relation2.where("asdf")) \
+ .on(@predicate) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
+ FROM `users`
+ INNER JOIN `photos`
+ ON `users`.`id` = `photos`.`user_id` AND asdf
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos"."id", "photos"."user_id", "photos"."camera_id"
+ FROM "users"
+ INNER JOIN "photos"
+ ON "users"."id" = "photos"."user_id" AND asdf
+ })
+ end
+ end
+ end
+ end
+
+ describe 'when a compound contains a join' do
+ describe 'and the compound is a where' do
+ it 'manufactures sql disambiguating the tables' do
+ sql = @relation1 \
+ .where(@relation1[:id].eq(1)) \
+ .join(@relation2) \
+ .on(@predicate) \
+ .where(@relation1[:id].eq(1)) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
+ FROM `users`
+ INNER JOIN `photos`
+ ON `users`.`id` = `photos`.`user_id`
+ WHERE `users`.`id` = 1
+ AND `users`.`id` = 1
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos"."id", "photos"."user_id", "photos"."camera_id"
+ FROM "users"
+ INNER JOIN "photos"
+ ON "users"."id" = "photos"."user_id"
+ WHERE "users"."id" = 1
+ AND "users"."id" = 1
+ })
+ end
+ end
+ end
+
+ describe 'and the compound is a group' do
+ it 'manufactures sql disambiguating the tables' do
+ sql = @relation1 \
+ .join(@relation2) \
+ .on(@predicate) \
+ .group(@relation1[:id]) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
+ FROM `users`
+ INNER JOIN `photos`
+ ON `users`.`id` = `photos`.`user_id`
+ GROUP BY `users`.`id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos"."id", "photos"."user_id", "photos"."camera_id"
+ FROM "users"
+ INNER JOIN "photos"
+ ON "users"."id" = "photos"."user_id"
+ GROUP BY "users"."id"
+ })
+ end
+ end
+ end
+ end
+ end
+ end
+end