From 49d119ae84bbb7cd180ca855cf48997dc731554c Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 16 May 2009 21:13:32 -0400 Subject: [PATCH] Adding spec:mysql and spec:sqlite3 tasks --- .gitignore | 1 + Rakefile | 24 ++- .../integration/joins/with_adjacency_spec.rb | 150 +++++++++++++----- .../joins/with_aggregations_spec.rb | 150 +++++++++++++----- .../integration/joins/with_compounds_spec.rb | 90 ++++++++--- spec/arel/unit/predicates/binary_spec.rb | 76 ++++++--- spec/arel/unit/predicates/equality_spec.rb | 38 +++-- spec/arel/unit/predicates/in_spec.rb | 68 +++++--- spec/arel/unit/predicates/predicates_spec.rb | 36 ++++- spec/arel/unit/primitives/attribute_spec.rb | 62 ++++---- spec/arel/unit/primitives/expression_spec.rb | 10 +- spec/arel/unit/relations/alias_spec.rb | 30 +++- spec/arel/unit/relations/delete_spec.rb | 57 +++++-- spec/arel/unit/relations/group_spec.rb | 48 ++++-- spec/arel/unit/relations/insert_spec.rb | 75 ++++++--- spec/arel/unit/relations/join_spec.rb | 52 ++++-- spec/arel/unit/relations/order_spec.rb | 101 ++++++++---- spec/arel/unit/relations/project_spec.rb | 89 ++++++++--- spec/arel/unit/relations/skip_spec.rb | 22 ++- spec/arel/unit/relations/table_spec.rb | 41 +++-- spec/arel/unit/relations/take_spec.rb | 22 ++- spec/arel/unit/relations/update_spec.rb | 95 ++++++++--- spec/arel/unit/relations/where_spec.rb | 44 +++-- spec/connections/mysql_connection.rb | 13 ++ spec/connections/sqlite3_connection.rb | 22 +++ spec/doubles/database.rb | 51 ------ spec/schemas/mysql_schema.rb | 18 +++ spec/schemas/sqlite3_schema.rb | 18 +++ spec/spec_helper.rb | 19 ++- 29 files changed, 1101 insertions(+), 421 deletions(-) create mode 100644 spec/connections/mysql_connection.rb create mode 100644 spec/connections/sqlite3_connection.rb delete mode 100644 spec/doubles/database.rb create mode 100644 spec/schemas/mysql_schema.rb create mode 100644 spec/schemas/sqlite3_schema.rb diff --git a/.gitignore b/.gitignore index 2bab52ab..da2f440b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ coverage/* config/database.yml +spec/fixtures/*database* *.DS_Store diff --git a/Rakefile b/Rakefile index b1fc74a1..9292ac25 100644 --- a/Rakefile +++ b/Rakefile @@ -1,15 +1,31 @@ require 'rubygems' require 'spec/rake/spectask' -Spec::Rake::SpecTask.new do |t| - t.spec_files = FileList['spec/**/*_spec.rb'] -end +spec_file_list = FileList['spec/**/*_spec.rb'] +desc "Run specs using RCov (uses mysql database adapter)" Spec::Rake::SpecTask.new(:coverage) do |t| - t.spec_files = FileList['spec/**/*_spec.rb'] + t.spec_files = + ["spec/connections/mysql_connection.rb"] + + spec_file_list t.rcov = true t.rcov_opts = ['-x', 'spec,gems'] end +namespace :spec do + for adapter in %w[mysql sqlite3] + desc "Run specs with the #{adapter} database adapter" + Spec::Rake::SpecTask.new(adapter) do |t| + t.spec_files = + ["spec/connections/#{adapter}_connection.rb"] + + ["spec/schemas/#{adapter}_schema.rb"] + + spec_file_list + end + end +end + +desc "Run specs with mysql and sqlite3 database adapters (default)" +task :spec => ["spec:sqlite3", "spec:mysql"] + desc "Default task is to run specs" task :default => :spec \ No newline at end of file diff --git a/spec/arel/integration/joins/with_adjacency_spec.rb b/spec/arel/integration/joins/with_adjacency_spec.rb index 559b5bbe..fbac723e 100644 --- a/spec/arel/integration/joins/with_adjacency_spec.rb +++ b/spec/arel/integration/joins/with_adjacency_spec.rb @@ -11,39 +11,79 @@ module Arel 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 - @relation1.join(@relation2).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` - ") - end + sql = @relation1.join(@relation2).on(@predicate).to_sql - describe 'when joining with a where on the same relation' do - it 'manufactures sql aliasing the tables properly' do - @relation1 \ - .join(@relation2.where(@relation2[:id].eq(1))) \ - .on(@predicate) \ - .to_sql.should be_like(" + 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 - ") + 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 - @relation1 \ + + sql = @relation1 \ .join(relation2) \ .on(relation2[:id].eq(@relation1[: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_2`.`id` = `users`.`id` AND `users_2`.`id` = 1 - ") + .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 @@ -55,35 +95,65 @@ module Arel describe 'when joining left-associatively' do it 'manufactures sql aliasing the tables properly' do - @relation1 \ + sql = @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` - ") + .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 - @relation1 \ + sql = @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` - ") + .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 diff --git a/spec/arel/integration/joins/with_aggregations_spec.rb b/spec/arel/integration/joins/with_aggregations_spec.rb index 2b21dcaa..41978c0a 100644 --- a/spec/arel/integration/joins/with_aggregations_spec.rb +++ b/spec/arel/integration/joins/with_aggregations_spec.rb @@ -18,68 +18,146 @@ module Arel describe '#to_sql' do # CLEANUP it '' do - @relation1.join(@relation2.take(3)).on(@predicate).to_sql.should be_like(" - 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` - ") + 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 - @relation1.join(@aggregation).on(@predicate).to_sql.should be_like(" - 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` - ") + 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 - @aggregation.join(@relation1).on(@predicate).to_sql.should be_like(" - 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` - ") + 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 - @aggregation.join(aggregation2).on(aggregation2[:user_id].eq(@aggregation[:user_id])).to_sql.should be_like(" - 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` - ") + 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 - @relation1.join(@aggregation.where(@aggregation[:user_id].eq(1))).on(@predicate).to_sql.should be_like(" - 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` - ") + 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 - @aggregation.where(@aggregation[:user_id].eq(1)).join(@relation1).on(@predicate).to_sql.should be_like(" - 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` - ") + 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 diff --git a/spec/arel/integration/joins/with_compounds_spec.rb b/spec/arel/integration/joins/with_compounds_spec.rb index 95fadc23..7582c5fc 100644 --- a/spec/arel/integration/joins/with_compounds_spec.rb +++ b/spec/arel/integration/joins/with_compounds_spec.rb @@ -12,15 +12,28 @@ module Arel describe 'when the join contains a where' do describe 'and the where is given a string' do it 'does not escape the string' do - @relation1 \ + sql = @relation1 \ .join(@relation2.where("asdf")) \ .on(@predicate) \ - .to_sql.should be_like(" - 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 - ") + .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 @@ -28,35 +41,64 @@ module Arel describe 'when a compound contains a join' do describe 'and the compound is a where' do it 'manufactures sql disambiguating the tables' do - @relation1 \ + sql = @relation1 \ .where(@relation1[:id].eq(1)) \ .join(@relation2) \ .on(@predicate) \ .where(@relation1[:id].eq(1)) \ - .to_sql.should be_like(" - 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 - ") + .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 - @relation1 \ + sql = @relation1 \ .join(@relation2) \ .on(@predicate) \ .group(@relation1[:id]) \ - .to_sql.should be_like(" - 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` - ") + .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 diff --git a/spec/arel/unit/predicates/binary_spec.rb b/spec/arel/unit/predicates/binary_spec.rb index 56fcf2d8..2d0c67e0 100644 --- a/spec/arel/unit/predicates/binary_spec.rb +++ b/spec/arel/unit/predicates/binary_spec.rb @@ -18,13 +18,19 @@ def predicate_sql @operand1 = ConcreteBinary.new(@attribute1, 1) @operand2 = ConcreteBinary.new(@attribute2, "name") end - + describe Or do describe "#to_sql" do it "manufactures sql with an OR operation" do - Or.new(@operand1, @operand2).to_sql.should be_like(" - (`users`.`id` <=> 1 OR `users`.`name` <=> 'name') - ") + sql = Or.new(@operand1, @operand2).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{(`users`.`id` <=> 1 OR `users`.`name` <=> 'name')}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> 'name')}) + end end end end @@ -32,58 +38,82 @@ def predicate_sql describe And do describe "#to_sql" do it "manufactures sql with an AND operation" do - And.new(@operand1, @operand2).to_sql.should be_like(" - (`users`.`id` <=> 1 AND `users`.`name` <=> 'name') - ") + sql = And.new(@operand1, @operand2).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{(`users`.`id` <=> 1 AND `users`.`name` <=> 'name')}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> 'name')}) + end end end end end - + describe '#to_sql' do describe 'when relating two attributes' do it 'manufactures sql with a binary operation' do - ConcreteBinary.new(@attribute1, @attribute2).to_sql.should be_like(" - `users`.`id` <=> `users`.`name` - ") + sql = ConcreteBinary.new(@attribute1, @attribute2).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` <=> `users`.`name`}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{"users"."id" <=> "users"."name"}) + end end end - + describe 'when relating an attribute and a value' do before do @value = "1-asdf" end - + describe 'when relating to an integer attribute' do it 'formats values as integers' do - ConcreteBinary.new(@attribute1, @value).to_sql.should be_like(" - `users`.`id` <=> 1 - ") + sql = ConcreteBinary.new(@attribute1, @value).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` <=> 1}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{"users"."id" <=> 1}) + end end end - + describe 'when relating to a string attribute' do it 'formats values as strings' do - ConcreteBinary.new(@attribute2, @value).to_sql.should be_like(" - `users`.`name` <=> '1-asdf' - ") + sql = ConcreteBinary.new(@attribute2, @value).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`name` <=> '1-asdf'}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{"users"."name" <=> '1-asdf'}) + end end end end end - + describe '#bind' do before do @another_relation = @relation.alias end - + describe 'when both operands are attributes' do it "manufactures an expression with the attributes bound to the relation" do ConcreteBinary.new(@attribute1, @attribute2).bind(@another_relation). \ should == ConcreteBinary.new(@another_relation[@attribute1], @another_relation[@attribute2]) end end - + describe 'when an operand is a value' do it "manufactures an expression with unmodified values" do ConcreteBinary.new(@attribute1, "asdf").bind(@another_relation). \ diff --git a/spec/arel/unit/predicates/equality_spec.rb b/spec/arel/unit/predicates/equality_spec.rb index 8a58ba30..b595cdd2 100644 --- a/spec/arel/unit/predicates/equality_spec.rb +++ b/spec/arel/unit/predicates/equality_spec.rb @@ -8,40 +8,52 @@ module Arel @attribute1 = @relation1[:id] @attribute2 = @relation2[:user_id] end - - describe '==' do + + describe '==' do it "obtains if attribute1 and attribute2 are identical" do Equality.new(@attribute1, @attribute2).should == Equality.new(@attribute1, @attribute2) Equality.new(@attribute1, @attribute2).should_not == Equality.new(@attribute1, @attribute1) end - + it "obtains if the concrete type of the predicates are identical" do Equality.new(@attribute1, @attribute2).should_not == Binary.new(@attribute1, @attribute2) end - + it "is commutative on the attributes" do Equality.new(@attribute1, @attribute2).should == Equality.new(@attribute2, @attribute1) end end - + describe '#to_sql' do describe 'when relating to a non-nil value' do it "manufactures an equality predicate" do - Equality.new(@attribute1, @attribute2).to_sql.should be_like(" - `users`.`id` = `photos`.`user_id` - ") + sql = Equality.new(@attribute1, @attribute2).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` = `photos`.`user_id`}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{"users"."id" = "photos"."user_id"}) + end end end - + describe 'when relation to a nil value' do before do @nil = nil end - + it "manufactures an is null predicate" do - Equality.new(@attribute1, @nil).to_sql.should be_like(" - `users`.`id` IS NULL - ") + sql = Equality.new(@attribute1, @nil).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` IS NULL}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{"users"."id" IS NULL}) + end end end end diff --git a/spec/arel/unit/predicates/in_spec.rb b/spec/arel/unit/predicates/in_spec.rb index 797798a7..9107da9d 100644 --- a/spec/arel/unit/predicates/in_spec.rb +++ b/spec/arel/unit/predicates/in_spec.rb @@ -6,51 +6,79 @@ module Arel @relation = Table.new(:users) @attribute = @relation[:id] end - - describe '#to_sql' do + + describe '#to_sql' do describe 'when relating to an array' do describe 'when the array\'s elements are the same type as the attribute' do before do @array = [1, 2, 3] end - + it 'manufactures sql with a comma separated list' do - In.new(@attribute, @array).to_sql.should be_like(" - `users`.`id` IN (1, 2, 3) - ") + sql = In.new(@attribute, @array).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` IN (1, 2, 3)}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{"users"."id" IN (1, 2, 3)}) + end end end - + describe 'when the array\'s elements are not same type as the attribute' do before do @array = ['1-asdf', 2, 3] end - + it 'formats values in the array as the type of the attribute' do - In.new(@attribute, @array).to_sql.should be_like(" - `users`.`id` IN (1, 2, 3) - ") + sql = In.new(@attribute, @array).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` IN (1, 2, 3)}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{"users"."id" IN (1, 2, 3)}) + end end end end - + describe 'when relating to a range' do before do @range = 1..2 end - + it 'manufactures sql with a between' do - In.new(@attribute, @range).to_sql.should be_like(" - `users`.`id` BETWEEN 1 AND 2 - ") + sql = In.new(@attribute, @range).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` BETWEEN 1 AND 2}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{"users"."id" BETWEEN 1 AND 2}) + end end end - + describe 'when relating to a relation' do it 'manufactures sql with a subselect' do - In.new(@attribute, @relation).to_sql.should be_like(" - `users`.`id` IN (SELECT `users`.`id`, `users`.`name` FROM `users`) - ") + sql = In.new(@attribute, @relation).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + `users`.`id` IN (SELECT `users`.`id`, `users`.`name` FROM `users`) + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + "users"."id" IN (SELECT "users"."id", "users"."name" FROM "users") + }) + end end end end diff --git a/spec/arel/unit/predicates/predicates_spec.rb b/spec/arel/unit/predicates/predicates_spec.rb index d11637ca..8f9cec53 100644 --- a/spec/arel/unit/predicates/predicates_spec.rb +++ b/spec/arel/unit/predicates/predicates_spec.rb @@ -9,23 +9,43 @@ module Arel @operand1 = Equality.new(@attribute1, 1) @operand2 = Equality.new(@attribute2, "name") end - + describe "when being combined with another predicate with AND logic" do describe "#to_sql" do it "manufactures sql with an AND operation" do - @operand1.and(@operand2).to_sql.should be_like(" - (`users`.`id` = 1 AND `users`.`name` = 'name') - ") + sql = @operand1.and(@operand2).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + (`users`.`id` = 1 AND `users`.`name` = 'name') + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + ("users"."id" = 1 AND "users"."name" = 'name') + }) + end end end end - + describe "when being combined with another predicate with OR logic" do describe "#to_sql" do it "manufactures sql with an OR operation" do - @operand1.or(@operand2).to_sql.should be_like(" - (`users`.`id` = 1 OR `users`.`name` = 'name') - ") + sql = @operand1.or(@operand2).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + (`users`.`id` = 1 OR `users`.`name` = 'name') + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + ("users"."id" = 1 OR "users"."name" = 'name') + }) + end end end end diff --git a/spec/arel/unit/primitives/attribute_spec.rb b/spec/arel/unit/primitives/attribute_spec.rb index b341c6f8..6d0f146a 100644 --- a/spec/arel/unit/primitives/attribute_spec.rb +++ b/spec/arel/unit/primitives/attribute_spec.rb @@ -6,57 +6,57 @@ module Arel @relation = Table.new(:users) @attribute = @relation[:id] end - + describe Attribute::Transformations do describe '#as' do it "manufactures an aliased attributed" do @attribute.as(:alias).should == Attribute.new(@relation, @attribute.name, :alias => :alias, :ancestor => @attribute) end end - + describe '#bind' do it "manufactures an attribute with the relation bound and self as an ancestor" do derived_relation = @relation.where(@relation[:id].eq(1)) @attribute.bind(derived_relation).should == Attribute.new(derived_relation, @attribute.name, :ancestor => @attribute) end - + it "returns self if the substituting to the same relation" do @attribute.bind(@relation).should == @attribute end end - + describe '#to_attribute' do it "returns self" do @attribute.to_attribute.should == @attribute end end end - + describe '#column' do it "returns the corresponding column in the relation" do @attribute.column.should == @relation.column_for(@attribute) end end - + describe '#engine' do it "delegates to its relation" do Attribute.new(@relation, :id).engine.should == @relation.engine end end - + describe Attribute::Congruence do describe '/' do before do @aliased_relation = @relation.alias @doubly_aliased_relation = @aliased_relation.alias end - + describe 'when dividing two unrelated attributes' do it "returns 0.0" do (@relation[:id] / @relation[:name]).should == 0.0 end end - + describe 'when dividing two matching attributes' do it 'returns a the highest score for the most similar attributes' do (@aliased_relation[:id] / @relation[:id]) \ @@ -67,97 +67,105 @@ module Arel end end end - + describe '#to_sql' do describe 'for a simple attribute' do it "manufactures sql with an alias" do - @attribute.to_sql.should be_like("`users`.`id`") + sql = @attribute.to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id`}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{"users"."id"}) + end end end end - + describe Attribute::Predications do before do @attribute = Attribute.new(@relation, :name) end - + describe '#eq' do it "manufactures an equality predicate" do @attribute.eq('name').should == Equality.new(@attribute, 'name') end end - + describe '#lt' do it "manufactures a less-than predicate" do @attribute.lt(10).should == LessThan.new(@attribute, 10) end end - + describe '#lteq' do it "manufactures a less-than or equal-to predicate" do @attribute.lteq(10).should == LessThanOrEqualTo.new(@attribute, 10) end end - + describe '#gt' do it "manufactures a greater-than predicate" do @attribute.gt(10).should == GreaterThan.new(@attribute, 10) end end - + describe '#gteq' do it "manufactures a greater-than or equal-to predicate" do @attribute.gteq(10).should == GreaterThanOrEqualTo.new(@attribute, 10) end end - + describe '#matches' do it "manufactures a match predicate" do @attribute.matches(/.*/).should == Match.new(@attribute, /.*/) end end - + describe '#in' do it "manufactures an in predicate" do @attribute.in(1..30).should == In.new(@attribute, (1..30)) end end end - + describe Attribute::Expressions do before do - @attribute = Attribute.new(@relation, :name) + @attribute = Attribute.new(@relation, :name) end - + describe '#count' do it "manufactures a count Expression" do @attribute.count.should == Expression.new(@attribute, "COUNT") end end - + describe '#sum' do it "manufactures a sum Expression" do @attribute.sum.should == Expression.new(@attribute, "SUM") end end - + describe '#maximum' do it "manufactures a maximum Expression" do @attribute.maximum.should == Expression.new(@attribute, "MAX") end end - + describe '#minimum' do it "manufactures a minimum Expression" do @attribute.minimum.should == Expression.new(@attribute, "MIN") end end - + describe '#average' do it "manufactures an average Expression" do @attribute.average.should == Expression.new(@attribute, "AVG") end - end + end end end end \ No newline at end of file diff --git a/spec/arel/unit/primitives/expression_spec.rb b/spec/arel/unit/primitives/expression_spec.rb index 4943f4ef..ebde55ff 100644 --- a/spec/arel/unit/primitives/expression_spec.rb +++ b/spec/arel/unit/primitives/expression_spec.rb @@ -38,7 +38,15 @@ module Arel describe '#to_sql' do it "manufactures sql with the expression and alias" do - Expression.new(@attribute, "COUNT", :alias).to_sql.should == "COUNT(`users`.`id`) AS `alias`" + sql = Expression.new(@attribute, "COUNT", :alias).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{COUNT(`users`.`id`) AS `alias`}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{COUNT("users"."id") AS "alias"}) + end end end end diff --git a/spec/arel/unit/relations/alias_spec.rb b/spec/arel/unit/relations/alias_spec.rb index 460a0ed0..570f3158 100644 --- a/spec/arel/unit/relations/alias_spec.rb +++ b/spec/arel/unit/relations/alias_spec.rb @@ -16,19 +16,33 @@ module Arel describe '#to_sql' do describe 'when there is no ambiguity' do it 'does not alias table names anywhere a table name can appear' do - @relation \ + sql = @relation \ .where(@relation[:id].eq(1)) \ .order(@relation[:id]) \ .project(@relation[:id]) \ .group(@relation[:id]) \ .alias \ - .to_sql.should be_like(" - SELECT `users`.`id` - FROM `users` - WHERE `users`.`id` = 1 - GROUP BY `users`.`id` - ORDER BY `users`.`id` - ") + .to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id` + FROM `users` + WHERE `users`.`id` = 1 + GROUP BY `users`.`id` + ORDER BY `users`.`id` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id" + FROM "users" + WHERE "users"."id" = 1 + GROUP BY "users"."id" + ORDER BY "users"."id" + }) + end end end end diff --git a/spec/arel/unit/relations/delete_spec.rb b/spec/arel/unit/relations/delete_spec.rb index fa887d20..3798178c 100644 --- a/spec/arel/unit/relations/delete_spec.rb +++ b/spec/arel/unit/relations/delete_spec.rb @@ -8,26 +8,55 @@ module Arel describe '#to_sql' do it 'manufactures sql deleting a table relation' do - Deletion.new(@relation).to_sql.should be_like(" - DELETE - FROM `users` - ") + sql = Deletion.new(@relation).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{DELETE FROM `users`}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{DELETE FROM "users"}) + end end it 'manufactures sql deleting a where relation' do - Deletion.new(@relation.where(@relation[:id].eq(1))).to_sql.should be_like(" - DELETE - FROM `users` - WHERE `users`.`id` = 1 - ") + sql = Deletion.new(@relation.where(@relation[:id].eq(1))).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + DELETE + FROM `users` + WHERE `users`.`id` = 1 + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + DELETE + FROM "users" + WHERE "users"."id" = 1 + }) + end end it "manufactures sql deleting a ranged relation" do - Deletion.new(@relation.take(1)).to_sql.should be_like(" - DELETE - FROM `users` - LIMIT 1 - ") + sql = Deletion.new(@relation.take(1)).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + DELETE + FROM `users` + LIMIT 1 + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + DELETE + FROM "users" + LIMIT 1 + }) + end end end diff --git a/spec/arel/unit/relations/group_spec.rb b/spec/arel/unit/relations/group_spec.rb index a0147b94..658c0ad4 100644 --- a/spec/arel/unit/relations/group_spec.rb +++ b/spec/arel/unit/relations/group_spec.rb @@ -6,25 +6,49 @@ module Arel @relation = Table.new(:users) @attribute = @relation[:id] end - + describe '#to_sql' do describe 'when given a predicate' do it "manufactures sql with where clause conditions" do - Group.new(@relation, @attribute).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - GROUP BY `users`.`id` - ") + sql = Group.new(@relation, @attribute).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + GROUP BY `users`.`id` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + GROUP BY "users"."id" + }) + end end end - + describe 'when given a string' do it "passes the string through to the where clause" do - Group.new(@relation, 'asdf').to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - GROUP BY asdf - ") + sql = Group.new(@relation, 'asdf').to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + GROUP BY asdf + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + GROUP BY asdf + }) + end end end end diff --git a/spec/arel/unit/relations/insert_spec.rb b/spec/arel/unit/relations/insert_spec.rb index 441c97b2..2fd07dd9 100644 --- a/spec/arel/unit/relations/insert_spec.rb +++ b/spec/arel/unit/relations/insert_spec.rb @@ -8,24 +8,35 @@ module Arel describe '#to_sql' do it 'manufactures sql inserting data when given multiple rows' do - pending 'it should insert multiple rows' - @insertion = Insert.new(@relation, [@relation[:name] => "nick", @relation[:name] => "bryan"]) + pending 'it should insert multiple rows' do + @insertion = Insert.new(@relation, [@relation[:name] => "nick", @relation[:name] => "bryan"]) - @insertion.to_sql.should be_like(" - INSERT - INTO `users` - (`name`) VALUES ('nick'), ('bryan') - ") + @insertion.to_sql.should be_like(" + INSERT + INTO `users` + (`name`) VALUES ('nick'), ('bryan') + ") + end end it 'manufactures sql inserting data when given multiple values' do @insertion = Insert.new(@relation, @relation[:id] => "1", @relation[:name] => "nick") - @insertion.to_sql.should be_like(" - INSERT - INTO `users` - (`id`, `name`) VALUES (1, 'nick') - ") + adapter_is :mysql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO `users` + (`id`, `name`) VALUES (1, 'nick') + }) + end + + adapter_is_not :mysql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO "users" + ("id", "name") VALUES (1, 'nick') + }) + end end describe 'when given values whose types correspond to the types of the attributes' do @@ -34,11 +45,21 @@ module Arel end it 'manufactures sql inserting data' do - @insertion.to_sql.should be_like(" - INSERT - INTO `users` - (`name`) VALUES ('nick') - ") + adapter_is :mysql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO `users` + (`name`) VALUES ('nick') + }) + end + + adapter_is_not :mysql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO "users" + ("name") VALUES ('nick') + }) + end end end @@ -48,11 +69,21 @@ module Arel end it 'manufactures sql inserting data' do - @insertion.to_sql.should be_like(" - INSERT - INTO `users` - (`id`) VALUES (1) - ") + adapter_is :mysql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO `users` + (`id`) VALUES (1) + }) + end + + adapter_is_not :mysql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO "users" + ("id") VALUES (1) + }) + end end end end diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb index 1698bf96..fa6bbbe2 100644 --- a/spec/arel/unit/relations/join_spec.rb +++ b/spec/arel/unit/relations/join_spec.rb @@ -7,20 +7,20 @@ module Arel @relation2 = Table.new(:photos) @predicate = @relation1[:id].eq(@relation2[:user_id]) end - + describe 'hashing' do it 'implements hash equality' do Join.new("INNER JOIN", @relation1, @relation2, @predicate) \ .should hash_the_same_as(Join.new("INNER JOIN", @relation1, @relation2, @predicate)) end end - + describe '#engine' do it "delegates to a relation's engine" do Join.new("INNER JOIN", @relation1, @relation2, @predicate).engine.should == @relation1.engine end end - + describe '#attributes' do it 'combines the attributes of the two relations' do join = Join.new("INNER JOIN", @relation1, @relation2, @predicate) @@ -32,21 +32,45 @@ module Arel describe '#to_sql' do describe 'when joining with another relation' do it 'manufactures sql joining the two tables on the predicate' do - Join.new("INNER JOIN", @relation1, @relation2, @predicate).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id` - FROM `users` - INNER JOIN `photos` ON `users`.`id` = `photos`.`user_id` - ") + sql = Join.new("INNER JOIN", @relation1, @relation2, @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` + }) + 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" + }) + end end end - + describe 'when joining with a string' do it "passes the string through to the where clause" do - Join.new("INNER JOIN asdf ON fdsa", @relation1).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - INNER JOIN asdf ON fdsa - ") + sql = Join.new("INNER JOIN asdf ON fdsa", @relation1).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + INNER JOIN asdf ON fdsa + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + INNER JOIN asdf ON fdsa + }) + end end end end diff --git a/spec/arel/unit/relations/order_spec.rb b/spec/arel/unit/relations/order_spec.rb index d373a8ba..31014ddd 100644 --- a/spec/arel/unit/relations/order_spec.rb +++ b/spec/arel/unit/relations/order_spec.rb @@ -10,57 +10,104 @@ module Arel describe '#to_sql' do describe "when given an attribute" do it "manufactures sql with an order clause populated by the attribute" do - Order.new(@relation, @attribute).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - ORDER BY `users`.`id` - ") + sql = Order.new(@relation, @attribute).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + ORDER BY `users`.`id` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + ORDER BY "users"."id" + }) + end end end - + describe "when given multiple attributes" do before do @another_attribute = @relation[:name] end - + it "manufactures sql with an order clause populated by comma-separated attributes" do - Order.new(@relation, @attribute, @another_attribute).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - ORDER BY `users`.`id`, `users`.`name` - ") + sql = Order.new(@relation, @attribute, @another_attribute).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + ORDER BY `users`.`id`, `users`.`name` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + ORDER BY "users"."id", "users"."name" + }) + end end end - + describe "when given a string" do before do @string = "asdf" end - + it "passes the string through to the order clause" do - Order.new(@relation, @string).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - ORDER BY asdf - ") + sql = Order.new(@relation, @string).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + ORDER BY asdf + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + ORDER BY asdf + }) + end end end - + describe "when ordering an ordered relation" do before do @ordered_relation = Order.new(@relation, @attribute) @another_attribute = @relation[:name] end - + it "manufactures sql with the order clause of the last ordering preceding the first ordering" do - Order.new(@ordered_relation, @another_attribute).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - ORDER BY `users`.`name`, `users`.`id` - ") + sql = Order.new(@ordered_relation, @another_attribute).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + ORDER BY `users`.`name`, `users`.`id` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + ORDER BY "users"."name", "users"."id" + }) + end end end end end end - \ No newline at end of file diff --git a/spec/arel/unit/relations/project_spec.rb b/spec/arel/unit/relations/project_spec.rb index f389b18c..d2d1fb38 100644 --- a/spec/arel/unit/relations/project_spec.rb +++ b/spec/arel/unit/relations/project_spec.rb @@ -20,10 +20,21 @@ module Arel describe '#to_sql' do describe 'when given an attribute' do it "manufactures sql with a limited select clause" do - Project.new(@relation, @attribute).to_sql.should be_like(" - SELECT `users`.`id` - FROM `users` - ") + sql = Project.new(@relation, @attribute).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id` + FROM `users` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id" + FROM "users" + }) + end end end @@ -33,33 +44,75 @@ module Arel end it "manufactures sql with scalar selects" do - Project.new(@relation, @scalar_relation).to_sql.should be_like(" - SELECT (SELECT `users`.`name` FROM `users`) AS `users` FROM `users` - ") + sql = Project.new(@relation, @scalar_relation).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT (SELECT `users`.`name` FROM `users`) AS `users` FROM `users` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT (SELECT "users"."name" FROM "users") AS "users" FROM "users" + }) + end end end describe 'when given a string' do it "passes the string through to the select clause" do - Project.new(@relation, 'asdf').to_sql.should be_like(" - SELECT asdf FROM `users` - ") + sql = Project.new(@relation, 'asdf').to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT asdf FROM `users` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT asdf FROM "users" + }) + end end end describe 'when given an expression' do it 'manufactures sql with expressions' do - @relation.project(@attribute.count).to_sql.should be_like(" - SELECT COUNT(`users`.`id`) AS count_id - FROM `users` - ") + sql = @relation.project(@attribute.count).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT COUNT(`users`.`id`) AS count_id + FROM `users` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT COUNT("users"."id") AS count_id + FROM "users" + }) + end end it 'manufactures sql with distinct expressions' do - @relation.project(@attribute.count(true)).to_sql.should be_like(" - SELECT COUNT(DISTINCT `users`.`id`) AS count_id - FROM `users` - ") + sql = @relation.project(@attribute.count(true)).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT COUNT(DISTINCT `users`.`id`) AS count_id + FROM `users` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT COUNT(DISTINCT "users"."id") AS count_id + FROM "users" + }) + end end end end diff --git a/spec/arel/unit/relations/skip_spec.rb b/spec/arel/unit/relations/skip_spec.rb index d83c969a..0653d795 100644 --- a/spec/arel/unit/relations/skip_spec.rb +++ b/spec/arel/unit/relations/skip_spec.rb @@ -9,11 +9,23 @@ module Arel describe '#to_sql' do it "manufactures sql with limit and offset" do - Skip.new(@relation, @skipped).to_s.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - OFFSET #{@skipped} - ") + sql = Skip.new(@relation, @skipped).to_s + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + OFFSET 4 + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + OFFSET 4 + }) + end end end end diff --git a/spec/arel/unit/relations/table_spec.rb b/spec/arel/unit/relations/table_spec.rb index 54520bf3..08486c7b 100644 --- a/spec/arel/unit/relations/table_spec.rb +++ b/spec/arel/unit/relations/table_spec.rb @@ -5,7 +5,7 @@ module Arel before do @relation = Table.new(:users) end - + describe '[]' do describe 'when given a', Symbol do it "manufactures an attribute if the symbol names an attribute within the relation" do @@ -18,39 +18,50 @@ module Arel it "returns the attribute if the attribute is within the relation" do @relation[@relation[:id]].should == @relation[:id] end - + it "returns nil if the attribtue is not within the relation" do another_relation = Table.new(:photos) @relation[another_relation[:id]].should be_nil end end - + describe 'when given an', Expression do before do @expression = @relation[:id].count end - + it "returns the Expression if the Expression is within the relation" do @relation[@expression].should be_nil end end end - + describe '#to_sql' do it "manufactures a simple select query" do - @relation.to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - ") + sql = @relation.to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + }) + end end end - + describe '#column_for' do it "returns the column corresponding to the attribute" do @relation.column_for(@relation[:id]).should == @relation.columns.detect { |c| c.name == 'id' } end end - + describe '#attributes' do it 'manufactures attributes corresponding to columns in the table' do @relation.attributes.should == [ @@ -58,7 +69,7 @@ module Arel Attribute.new(@relation, :name) ] end - + describe '#reset' do it "reloads columns from the database" do lambda { stub(@relation.engine).columns { [] } }.should_not change { @relation.attributes } @@ -66,20 +77,20 @@ module Arel end end end - + describe 'hashing' do it "implements hash equality" do Table.new(:users).should hash_the_same_as(Table.new(:users)) Table.new(:users).should_not hash_the_same_as(Table.new(:photos)) end end - + describe '#engine' do it "defaults to global engine" do Table.engine = engine = Engine.new Table.new(:users).engine.should == engine end - + it "can be specified" do Table.new(:users, engine = Engine.new).engine.should == engine end diff --git a/spec/arel/unit/relations/take_spec.rb b/spec/arel/unit/relations/take_spec.rb index dca78060..911b84e0 100644 --- a/spec/arel/unit/relations/take_spec.rb +++ b/spec/arel/unit/relations/take_spec.rb @@ -9,11 +9,23 @@ module Arel describe '#to_sql' do it "manufactures sql with limit and offset" do - Take.new(@relation, @taken).to_s.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - LIMIT #{@taken} - ") + sql = Take.new(@relation, @taken).to_s + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + LIMIT 4 + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + LIMIT 4 + }) + end end end end diff --git a/spec/arel/unit/relations/update_spec.rb b/spec/arel/unit/relations/update_spec.rb index b6736925..0bbc9113 100644 --- a/spec/arel/unit/relations/update_spec.rb +++ b/spec/arel/unit/relations/update_spec.rb @@ -8,18 +8,41 @@ module Arel describe '#to_sql' do it "manufactures sql updating attributes when given multiple attributes" do - Update.new(@relation, @relation[:id] => 1, @relation[:name] => "nick").to_sql.should be_like(" - UPDATE `users` - SET `id` = 1, `name` = 'nick' - ") + sql = Update.new(@relation, @relation[:id] => 1, @relation[:name] => "nick").to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + UPDATE `users` + SET `id` = 1, `name` = 'nick' + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + UPDATE "users" + SET "id" = 1, "name" = 'nick' + }) + end end it "manufactures sql updating attributes when given a ranged relation" do - Update.new(@relation.take(1), @relation[:name] => "nick").to_sql.should be_like(" - UPDATE `users` - SET `name` = 'nick' - LIMIT 1 - ") + sql = Update.new(@relation.take(1), @relation[:name] => "nick").to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + UPDATE `users` + SET `name` = 'nick' + LIMIT 1 + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + UPDATE "users" + SET "name" = 'nick' + LIMIT 1 + }) + end end describe 'when given values whose types correspond to the types of the attributes' do @@ -28,10 +51,19 @@ module Arel end it 'manufactures sql updating attributes' do - @update.to_sql.should be_like(" - UPDATE `users` - SET `name` = 'nick' - ") + adapter_is :mysql do + @update.to_sql.should be_like(%Q{ + UPDATE `users` + SET `name` = 'nick' + }) + end + + adapter_is_not :mysql do + @update.to_sql.should be_like(%Q{ + UPDATE "users" + SET "name" = 'nick' + }) + end end end @@ -41,10 +73,19 @@ module Arel end it 'manufactures sql updating attributes' do - @update.to_sql.should be_like(" - UPDATE `users` - SET `id` = 1 - ") + adapter_is :mysql do + @update.to_sql.should be_like(%Q{ + UPDATE `users` + SET `id` = 1 + }) + end + + adapter_is_not :mysql do + @update.to_sql.should be_like(%Q{ + UPDATE "users" + SET "id" = 1 + }) + end end end @@ -57,11 +98,21 @@ module Arel end it 'manufactures sql updating a where relation' do - @update.to_sql.should be_like(" - UPDATE `users` - SET `name` = 'nick' - WHERE `users`.`id` = 1 - ") + adapter_is :mysql do + @update.to_sql.should be_like(%Q{ + UPDATE `users` + SET `name` = 'nick' + WHERE `users`.`id` = 1 + }) + end + + adapter_is_not :mysql do + @update.to_sql.should be_like(%Q{ + UPDATE "users" + SET "name" = 'nick' + WHERE "users"."id" = 1 + }) + end end end end diff --git a/spec/arel/unit/relations/where_spec.rb b/spec/arel/unit/relations/where_spec.rb index 8ef4d54b..64f97c81 100644 --- a/spec/arel/unit/relations/where_spec.rb +++ b/spec/arel/unit/relations/where_spec.rb @@ -18,21 +18,45 @@ module Arel describe '#to_sql' do describe 'when given a predicate' do it "manufactures sql with where clause conditions" do - Where.new(@relation, @predicate).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - WHERE `users`.`id` = 1 - ") + sql = Where.new(@relation, @predicate).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + WHERE `users`.`id` = 1 + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + WHERE "users"."id" = 1 + }) + end end end describe 'when given a string' do it "passes the string through to the where clause" do - Where.new(@relation, 'asdf').to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - WHERE asdf - ") + sql = Where.new(@relation, 'asdf').to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + WHERE asdf + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + WHERE asdf + }) + end end end end diff --git a/spec/connections/mysql_connection.rb b/spec/connections/mysql_connection.rb new file mode 100644 index 00000000..789628b9 --- /dev/null +++ b/spec/connections/mysql_connection.rb @@ -0,0 +1,13 @@ +require "activerecord" +puts "Using native MySQL" + +ActiveRecord::Base.configurations = { + 'unit' => { + :adapter => 'mysql', + :username => 'rails', + :encoding => 'utf8', + :database => 'arel_unit', + } +} + +ActiveRecord::Base.establish_connection 'unit' \ No newline at end of file diff --git a/spec/connections/sqlite3_connection.rb b/spec/connections/sqlite3_connection.rb new file mode 100644 index 00000000..bae07771 --- /dev/null +++ b/spec/connections/sqlite3_connection.rb @@ -0,0 +1,22 @@ +require "activerecord" +puts "Using native SQLite3" + +db_file = "spec/fixtures/fixture_database.sqlite3" + +ActiveRecord::Base.configurations = { + "unit" => { + :adapter => 'sqlite3', + :database => db_file, + :timeout => 5000 + } +} + +unless File.exist?(db_file) + puts "SQLite3 database not found at #{db_file}. Rebuilding it." + FileUtils.mkdir_p(File.dirname(db_file)) + sqlite_command = %Q{sqlite3 "#{db_file}" "create table a (a integer); drop table a;"} + puts "Executing '#{sqlite_command}'" + raise "Seems that there is no sqlite3 executable available" unless system(sqlite_command) +end + +ActiveRecord::Base.establish_connection("unit") diff --git a/spec/doubles/database.rb b/spec/doubles/database.rb deleted file mode 100644 index f8a4b38e..00000000 --- a/spec/doubles/database.rb +++ /dev/null @@ -1,51 +0,0 @@ -module Fake - class Engine - def connection - @conn ||= Connection.new - end - end - - class Connection - include ActiveRecord::ConnectionAdapters::Quoting - - def columns(table_name, comment) - { "users" => - [ - Column.new("id", :integer), - Column.new("name", :string) - ], - "photos" => - [ - Column.new("id", :integer), - Column.new("user_id", :integer), - Column.new("camera_id", :integer) - ] - }[table_name] - end - - def execute(*args) - [] - end - - def quote_column_name(column_name) - "`#{column_name}`" - end - - def quote_table_name(table_name) - "`#{table_name}`" - end - - def supports_count_distinct? - true - end - end - - class Column - attr_reader :name, :type - - def initialize(name, type) - @name = name - @type = type - end - end -end diff --git a/spec/schemas/mysql_schema.rb b/spec/schemas/mysql_schema.rb new file mode 100644 index 00000000..1123f458 --- /dev/null +++ b/spec/schemas/mysql_schema.rb @@ -0,0 +1,18 @@ +sql = <<-SQL + DROP TABLE IF EXISTS users; + CREATE TABLE users ( + id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL + ); + + DROP TABLE IF EXISTS photos; + CREATE TABLE photos ( + id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, + user_id INTEGER NOT NULL, + camera_id INTEGER NOT NULL + ); +SQL + +sql.split(/;/).select(&:present?).each do |sql_statement| + ActiveRecord::Base.connection.execute sql_statement +end \ No newline at end of file diff --git a/spec/schemas/sqlite3_schema.rb b/spec/schemas/sqlite3_schema.rb new file mode 100644 index 00000000..6c98a4f9 --- /dev/null +++ b/spec/schemas/sqlite3_schema.rb @@ -0,0 +1,18 @@ +sql = <<-SQL + DROP TABLE IF EXISTS users; + CREATE TABLE users ( + id INTEGER NOT NULL PRIMARY KEY, + name VARCHAR(255) NOT NULL + ); + + DROP TABLE IF EXISTS photos; + CREATE TABLE photos ( + id INTEGER NOT NULL PRIMARY KEY, + user_id INTEGER NOT NULL, + camera_id INTEGER NOT NULL + ); +SQL + +sql.split(/;/).select(&:present?).each do |sql_statement| + ActiveRecord::Base.connection.execute sql_statement +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ce539b6f..8d515d66 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,10 +11,25 @@ Dir["#{dir}/#{helper}/*"].each { |m| require "#{dir}/#{helper}/#{File.basename(m)}" } end +module AdapterGuards + def adapter_is(name) + yield if name.to_s == adapter_name + end + + def adapter_is_not(name) + yield if name.to_s != adapter_name + end + + def adapter_name + Arel::Table.engine.connection.class.name.underscore.split("/").last.gsub(/_adapter/, '') + end +end + Spec::Runner.configure do |config| - config.include(BeLikeMatcher, HashTheSameAsMatcher, DisambiguateAttributesMatcher) + config.include BeLikeMatcher, HashTheSameAsMatcher, DisambiguateAttributesMatcher + config.include AdapterGuards config.mock_with :rr config.before do - Arel::Table.engine = Arel::Engine.new(Fake::Engine.new) + Arel::Table.engine = Arel::Engine.new(ActiveRecord::Base) end end