From 49d119ae84bbb7cd180ca855cf48997dc731554c Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 16 May 2009 21:13:32 -0400 Subject: Adding spec:mysql and spec:sqlite3 tasks --- spec/arel/integration/joins/with_adjacency_spec.rb | 150 +++++++++++++++------ .../integration/joins/with_aggregations_spec.rb | 150 ++++++++++++++++----- spec/arel/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 ++- 27 files changed, 1080 insertions(+), 417 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 (limited to 'spec') diff --git a/spec/arel/integration/joins/with_adjacency_spec.rb b/spec/arel/integration/joins/with_adjacency_spec.rb index 559b5bbe1a..fbac723e10 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 2b21dcaa1e..41978c0a5a 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 95fadc23d6..7582c5fc83 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 56fcf2d8ad..2d0c67e006 100644 --- a/spec/arel/unit/predicates/binary_spec.rb +++ b/spec/arel/unit/predicates/binary_spec.rb @@ -18,13 +18,19 @@ module Arel @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 @@ module Arel 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 8a58ba3096..b595cdd247 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 797798a77f..9107da9d4b 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 d11637cabe..8f9cec5376 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 b341c6f88e..6d0f146a39 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 4943f4ef33..ebde55ff90 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 460a0ed0df..570f315892 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 fa887d20fd..3798178ccc 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 a0147b9416..658c0ad406 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 441c97b290..2fd07dd96c 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 1698bf9647..fa6bbbe216 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 d373a8ba12..31014ddd39 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 f389b18c54..d2d1fb3873 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 d83c969aa8..0653d795b1 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 54520bf3b6..08486c7b6c 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 dca7806057..911b84e01e 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 b67369251f..0bbc9113c6 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 8ef4d54b63..64f97c8135 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 0000000000..789628b95d --- /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 0000000000..bae077711d --- /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 f8a4b38e17..0000000000 --- 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 0000000000..1123f4582e --- /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 0000000000..6c98a4f934 --- /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 ce539b6ffa..8d515d66f1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,10 +11,25 @@ require 'arel' 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 -- cgit v1.2.3 From 2bbf8ca9d2af3ea959a21c3729b4894bc31f088b Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 13:58:29 -0400 Subject: reorganized call Conflicts: doc/TODO lib/arel/relations/relation.rb lib/arel/relations/writes/delete.rb lib/arel/relations/writes/insert.rb lib/arel/relations/writes/update.rb lib/arel/session.rb spec/arel/unit/relations/delete_spec.rb spec/arel/unit/relations/insert_spec.rb spec/arel/unit/relations/relation_spec.rb spec/arel/unit/relations/update_spec.rb spec/arel/unit/session/session_spec.rb --- spec/arel/unit/relations/array_spec.rb | 27 +++++++++++++++++++++++++++ spec/arel/unit/relations/delete_spec.rb | 8 -------- spec/arel/unit/relations/insert_spec.rb | 11 ----------- spec/arel/unit/relations/relation_spec.rb | 7 ------- spec/arel/unit/relations/update_spec.rb | 11 ----------- spec/arel/unit/session/session_spec.rb | 10 +++++----- 6 files changed, 32 insertions(+), 42 deletions(-) create mode 100644 spec/arel/unit/relations/array_spec.rb (limited to 'spec') diff --git a/spec/arel/unit/relations/array_spec.rb b/spec/arel/unit/relations/array_spec.rb new file mode 100644 index 0000000000..1330f0f205 --- /dev/null +++ b/spec/arel/unit/relations/array_spec.rb @@ -0,0 +1,27 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') + +module Arel + describe Array do + before do + @relation = Array.new([[1], [2], [3]], [:id]) + end + + describe '#attributes' do + it 'manufactures attributes corresponding to the names given on construction' do + @relation.attributes.should == [ + Attribute.new(@relation, :id) + ] + end + end + + describe '#call' do + it "manufactures an array of hashes of attributes to values" do + @relation.call.should == [ + { @relation[:id] => 1 }, + { @relation[:id] => 2 }, + { @relation[:id] => 3 } + ] + end + end + end +end \ No newline at end of file diff --git a/spec/arel/unit/relations/delete_spec.rb b/spec/arel/unit/relations/delete_spec.rb index 3798178ccc..23aca563f7 100644 --- a/spec/arel/unit/relations/delete_spec.rb +++ b/spec/arel/unit/relations/delete_spec.rb @@ -59,13 +59,5 @@ module Arel end end end - - describe '#call' do - it 'executes a delete on the connection' do - deletion = Deletion.new(@relation) - mock(connection = Object.new).delete(deletion.to_sql) - deletion.call(connection) - end - end end end diff --git a/spec/arel/unit/relations/insert_spec.rb b/spec/arel/unit/relations/insert_spec.rb index 2fd07dd96c..5ab3ef1299 100644 --- a/spec/arel/unit/relations/insert_spec.rb +++ b/spec/arel/unit/relations/insert_spec.rb @@ -87,16 +87,5 @@ module Arel end end end - - describe '#call' do - before do - @insertion = Insert.new(@relation, @relation[:name] => "nick") - end - - it 'executes an insert on the connection' do - mock(connection = Object.new).insert(@insertion.to_sql) - @insertion.call(connection) - end - end end end diff --git a/spec/arel/unit/relations/relation_spec.rb b/spec/arel/unit/relations/relation_spec.rb index a3bfa67353..7df10be59c 100644 --- a/spec/arel/unit/relations/relation_spec.rb +++ b/spec/arel/unit/relations/relation_spec.rb @@ -165,12 +165,5 @@ module Arel @relation.first.should == @relation.session.read(@relation).first end end - - describe '#call' do - it 'executes a select_all on the connection' do - mock(connection = Object.new).execute(@relation.to_sql) { [] } - @relation.call(connection) - end - end end end diff --git a/spec/arel/unit/relations/update_spec.rb b/spec/arel/unit/relations/update_spec.rb index 0bbc9113c6..e0d7ddd295 100644 --- a/spec/arel/unit/relations/update_spec.rb +++ b/spec/arel/unit/relations/update_spec.rb @@ -117,16 +117,5 @@ module Arel end end - describe '#call' do - before do - @update = Update.new(@relation, @relation[:name] => "nick") - end - - it 'executes an update on the connection' do - mock(connection = Object.new).update(@update.to_sql) - @update.call(connection) - end - end - end end diff --git a/spec/arel/unit/session/session_spec.rb b/spec/arel/unit/session/session_spec.rb index 6e73d74f2d..c30ba6195f 100644 --- a/spec/arel/unit/session/session_spec.rb +++ b/spec/arel/unit/session/session_spec.rb @@ -40,19 +40,19 @@ module Arel describe '#create' do it "executes an insertion on the connection" do - mock(@insert).call(@insert.engine) + mock(@insert).call @session.create(@insert) end end describe '#read' do it "executes an selection on the connection" do - mock(@read).call(@read.engine) + mock(@read).call @session.read(@read) end it "is memoized" do - mock(@read).call(@read.engine).once + mock(@read).call.once @session.read(@read) @session.read(@read) end @@ -60,14 +60,14 @@ module Arel describe '#update' do it "executes an update on the connection" do - mock(@update).call(@update.engine) + mock(@update).call @session.update(@update) end end describe '#delete' do it "executes a delete on the connection" do - mock(@delete).call(@delete.engine) + mock(@delete).call @session.delete(@delete) end end -- cgit v1.2.3 From bdca9ed42ffea10aa6989ea3ecebedb424fa01ed Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 14:20:29 -0400 Subject: moved sql related code to its own engine area Conflicts: lib/arel/engine.rb lib/arel/extensions/object.rb lib/arel/predicates.rb lib/arel/primitives/attribute.rb lib/arel/primitives/expression.rb lib/arel/primitives/value.rb lib/arel/relations/operations/join.rb lib/arel/relations/relation.rb lib/arel/relations/utilities/externalization.rb lib/arel/relations/utilities/nil.rb lib/arel/relations/writes/delete.rb lib/arel/relations/writes/insert.rb lib/arel/relations/writes/update.rb spec/arel/unit/relations/skip_spec.rb spec/arel/unit/relations/take_spec.rb spec/spec_helper.rb --- spec/arel/unit/relations/skip_spec.rb | 2 +- spec/arel/unit/relations/table_spec.rb | 4 ++-- spec/arel/unit/relations/take_spec.rb | 2 +- spec/spec_helper.rb | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/arel/unit/relations/skip_spec.rb b/spec/arel/unit/relations/skip_spec.rb index 0653d795b1..2c8f6ccadb 100644 --- a/spec/arel/unit/relations/skip_spec.rb +++ b/spec/arel/unit/relations/skip_spec.rb @@ -9,7 +9,7 @@ module Arel describe '#to_sql' do it "manufactures sql with limit and offset" do - sql = Skip.new(@relation, @skipped).to_s + sql = Skip.new(@relation, @skipped).to_sql adapter_is :mysql do sql.should be_like(%Q{ diff --git a/spec/arel/unit/relations/table_spec.rb b/spec/arel/unit/relations/table_spec.rb index 08486c7b6c..211e6921f8 100644 --- a/spec/arel/unit/relations/table_spec.rb +++ b/spec/arel/unit/relations/table_spec.rb @@ -87,12 +87,12 @@ module Arel describe '#engine' do it "defaults to global engine" do - Table.engine = engine = Engine.new + Table.engine = engine = Sql::Engine.new Table.new(:users).engine.should == engine end it "can be specified" do - Table.new(:users, engine = Engine.new).engine.should == engine + Table.new(:users, engine = Sql::Engine.new).engine.should == engine end end end diff --git a/spec/arel/unit/relations/take_spec.rb b/spec/arel/unit/relations/take_spec.rb index 911b84e01e..d6442fc9d1 100644 --- a/spec/arel/unit/relations/take_spec.rb +++ b/spec/arel/unit/relations/take_spec.rb @@ -9,7 +9,7 @@ module Arel describe '#to_sql' do it "manufactures sql with limit and offset" do - sql = Take.new(@relation, @taken).to_s + sql = Take.new(@relation, @taken).to_sql adapter_is :mysql do sql.should be_like(%Q{ diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8d515d66f1..6a9a2ef23c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -30,6 +30,6 @@ Spec::Runner.configure do |config| config.include AdapterGuards config.mock_with :rr config.before do - Arel::Table.engine = Arel::Engine.new(ActiveRecord::Base) + Arel::Table.engine = Arel::Sql::Engine.new(ActiveRecord::Base) end end -- cgit v1.2.3 From 7032a50297fce4d7724d1735e81e5df5fd919e71 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 14:31:04 -0400 Subject: reorganized file structures Conflicts: lib/arel.rb lib/arel/arel.rb lib/arel/engines/memory/predicates.rb lib/arel/engines/memory/relations/array.rb lib/arel/engines/sql/relations/table.rb --- spec/arel/integration/joins/with_adjacency_spec.rb | 2 +- spec/arel/integration/joins/with_aggregations_spec.rb | 4 ++-- spec/arel/integration/joins/with_compounds_spec.rb | 4 ++-- spec/arel/unit/relations/array_spec.rb | 7 +++++++ 4 files changed, 12 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/arel/integration/joins/with_adjacency_spec.rb b/spec/arel/integration/joins/with_adjacency_spec.rb index fbac723e10..ffd6498749 100644 --- a/spec/arel/integration/joins/with_adjacency_spec.rb +++ b/spec/arel/integration/joins/with_adjacency_spec.rb @@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') module Arel describe Join do before do - @relation1 = Arel(:users) + @relation1 = Table(:users) @relation2 = @relation1.alias @predicate = @relation1[:id].eq(@relation2[:id]) end diff --git a/spec/arel/integration/joins/with_aggregations_spec.rb b/spec/arel/integration/joins/with_aggregations_spec.rb index 41978c0a5a..4aba005d51 100644 --- a/spec/arel/integration/joins/with_aggregations_spec.rb +++ b/spec/arel/integration/joins/with_aggregations_spec.rb @@ -3,8 +3,8 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') module Arel describe Join do before do - @relation1 = Arel(:users) - @relation2 = Arel(:photos) + @relation1 = Table(:users) + @relation2 = Table(:photos) @predicate = @relation1[:id].eq(@relation2[:user_id]) end diff --git a/spec/arel/integration/joins/with_compounds_spec.rb b/spec/arel/integration/joins/with_compounds_spec.rb index 7582c5fc83..41f04349b8 100644 --- a/spec/arel/integration/joins/with_compounds_spec.rb +++ b/spec/arel/integration/joins/with_compounds_spec.rb @@ -3,8 +3,8 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') module Arel describe Join do before do - @relation1 = Arel(:users) - @relation2 = Arel(:photos) + @relation1 = Table(:users) + @relation2 = Table(:photos) @predicate = @relation1[:id].eq(@relation2[:user_id]) end diff --git a/spec/arel/unit/relations/array_spec.rb b/spec/arel/unit/relations/array_spec.rb index 1330f0f205..c90843cd7d 100644 --- a/spec/arel/unit/relations/array_spec.rb +++ b/spec/arel/unit/relations/array_spec.rb @@ -22,6 +22,13 @@ module Arel { @relation[:id] => 3 } ] end + + it '' do + @relation.where(@relation[:id].lt(3)).call.should == [ + { @relation[:id] => 1 }, + { @relation[:id] => 2 } + ] + end end end end \ No newline at end of file -- cgit v1.2.3 From b0a45d52fdb7d8ce564f4dc2013bc790f98f1da3 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 14:38:09 -0400 Subject: consolidated files Conflicts: lib/arel/algebra/predicates.rb lib/arel/algebra/relations/writes/delete.rb lib/arel/algebra/relations/writes/insert.rb lib/arel/algebra/relations/writes/update.rb lib/arel/engines/memory/predicates.rb lib/arel/engines/memory/relations.rb lib/arel/engines/sql/primitives/attribute.rb lib/arel/engines/sql/relations/writes/insert.rb lib/arel/engines/sql/relations/writes/update.rb --- spec/arel/unit/relations/table_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/spec/arel/unit/relations/table_spec.rb b/spec/arel/unit/relations/table_spec.rb index 211e6921f8..2779c0fe5d 100644 --- a/spec/arel/unit/relations/table_spec.rb +++ b/spec/arel/unit/relations/table_spec.rb @@ -73,14 +73,14 @@ module Arel describe '#reset' do it "reloads columns from the database" do lambda { stub(@relation.engine).columns { [] } }.should_not change { @relation.attributes } - lambda { @relation.reset }.should change { @relation.attributes } + lambda { @relation.reset }.should change { @relation.attributes } 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 hash_the_same_as(Table.new(:users)) Table.new(:users).should_not hash_the_same_as(Table.new(:photos)) end end -- cgit v1.2.3 From 892337509b2bd269920dc567bc48c6a28c7222d2 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 14:46:08 -0400 Subject: removed function_sql in favor of polymorphism Conflicts: lib/arel/algebra/primitives/attribute.rb lib/arel/algebra/primitives/expression.rb spec/arel/unit/primitives/expression_spec.rb --- spec/arel/unit/primitives/attribute_spec.rb | 10 +++++----- spec/arel/unit/primitives/expression_spec.rb | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'spec') diff --git a/spec/arel/unit/primitives/attribute_spec.rb b/spec/arel/unit/primitives/attribute_spec.rb index 6d0f146a39..e512b40ebf 100644 --- a/spec/arel/unit/primitives/attribute_spec.rb +++ b/spec/arel/unit/primitives/attribute_spec.rb @@ -139,31 +139,31 @@ module Arel describe '#count' do it "manufactures a count Expression" do - @attribute.count.should == Expression.new(@attribute, "COUNT") + @attribute.count.should == Count.new(@attribute) end end describe '#sum' do it "manufactures a sum Expression" do - @attribute.sum.should == Expression.new(@attribute, "SUM") + @attribute.sum.should == Sum.new(@attribute) end end describe '#maximum' do it "manufactures a maximum Expression" do - @attribute.maximum.should == Expression.new(@attribute, "MAX") + @attribute.maximum.should == Maximum.new(@attribute) end end describe '#minimum' do it "manufactures a minimum Expression" do - @attribute.minimum.should == Expression.new(@attribute, "MIN") + @attribute.minimum.should == Minimum.new(@attribute) end end describe '#average' do it "manufactures an average Expression" do - @attribute.average.should == Expression.new(@attribute, "AVG") + @attribute.average.should == Average.new(@attribute) end end end diff --git a/spec/arel/unit/primitives/expression_spec.rb b/spec/arel/unit/primitives/expression_spec.rb index ebde55ff90..92f300c4ee 100644 --- a/spec/arel/unit/primitives/expression_spec.rb +++ b/spec/arel/unit/primitives/expression_spec.rb @@ -9,13 +9,13 @@ module Arel describe Expression::Transformations do before do - @expression = Expression.new(@attribute, "COUNT") + @expression = Count.new(@attribute) end describe '#bind' do it "manufactures an attribute with a rebound relation and self as the ancestor" do derived_relation = @relation.where(@relation[:id].eq(1)) - @expression.bind(derived_relation).should == Expression.new(@attribute.bind(derived_relation), "COUNT", nil, @expression) + @expression.bind(derived_relation).should == Count.new(@attribute.bind(derived_relation), nil, @expression) end it "returns self if the substituting to the same relation" do @@ -25,7 +25,7 @@ module Arel describe '#as' do it "manufactures an aliased expression" do - @expression.as(:alias).should == Expression.new(@attribute, "COUNT", :alias, @expression) + @expression.as(:alias).should == Expression.new(@attribute, :alias, @expression) end end @@ -38,7 +38,7 @@ module Arel describe '#to_sql' do it "manufactures sql with the expression and alias" do - sql = Expression.new(@attribute, "COUNT", :alias).to_sql + sql = Count.new(@attribute, :alias).to_sql adapter_is :mysql do sql.should be_like(%Q{COUNT(`users`.`id`) AS `alias`}) -- cgit v1.2.3 From 3a6e8e5a3f99841691b70b89b0a10f836e6ec071 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 14:49:56 -0400 Subject: join sql stuff moved into sql adapter Conflicts: lib/arel/algebra/primitives/value.rb lib/arel/algebra/relations/operations/join.rb lib/arel/algebra/relations/relation.rb spec/arel/unit/relations/join_spec.rb --- spec/arel/unit/relations/join_spec.rb | 12 ++++++------ spec/arel/unit/relations/relation_spec.rb | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'spec') diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb index fa6bbbe216..0e3e6ef16b 100644 --- a/spec/arel/unit/relations/join_spec.rb +++ b/spec/arel/unit/relations/join_spec.rb @@ -10,20 +10,20 @@ module Arel 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)) + InnerJoin.new(@relation1, @relation2, @predicate) \ + .should hash_the_same_as(InnerJoin.new(@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 + InnerJoin.new(@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) + join = InnerJoin.new(@relation1, @relation2, @predicate) join.attributes.should == (@relation1.attributes + @relation2.attributes).collect { |a| a.bind(join) } end @@ -32,7 +32,7 @@ module Arel describe '#to_sql' do describe 'when joining with another relation' do it 'manufactures sql joining the two tables on the predicate' do - sql = Join.new("INNER JOIN", @relation1, @relation2, @predicate).to_sql + sql = InnerJoin.new(@relation1, @relation2, @predicate).to_sql adapter_is :mysql do sql.should be_like(%Q{ @@ -54,7 +54,7 @@ module Arel describe 'when joining with a string' do it "passes the string through to the where clause" do - sql = Join.new("INNER JOIN asdf ON fdsa", @relation1).to_sql + sql = StringJoin.new(@relation1, "INNER JOIN asdf ON fdsa").to_sql adapter_is :mysql do sql.should be_like(%Q{ diff --git a/spec/arel/unit/relations/relation_spec.rb b/spec/arel/unit/relations/relation_spec.rb index 7df10be59c..6a61f39966 100644 --- a/spec/arel/unit/relations/relation_spec.rb +++ b/spec/arel/unit/relations/relation_spec.rb @@ -34,7 +34,7 @@ module Arel describe 'when given a relation' do it "manufactures an inner join operation between those two relations" do @relation.join(@relation).on(@predicate). \ - should == Join.new("INNER JOIN", @relation, @relation, @predicate) + should == InnerJoin.new(@relation, @relation, @predicate) end end @@ -54,7 +54,7 @@ module Arel describe '#outer_join' do it "manufactures a left outer join operation between those two relations" do @relation.outer_join(@relation).on(@predicate). \ - should == Join.new("LEFT OUTER JOIN", @relation, @relation, @predicate) + should == OuterJoin.new(@relation, @relation, @predicate) end end end -- cgit v1.2.3 From 4e3c9a01307339916f6b947d24f19b0f442afd78 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 14:58:46 -0400 Subject: most in memory operations save join and group Conflicts: lib/arel/algebra/extensions/object.rb lib/arel/algebra/primitives/value.rb lib/arel/engines/memory/relations.rb lib/arel/engines/sql/formatters.rb lib/arel/engines/sql/primitives.rb spec/arel/unit/relations/alias_spec.rb spec/arel/unit/relations/array_spec.rb spec/arel/unit/relations/order_spec.rb --- spec/arel/unit/relations/alias_spec.rb | 4 +- spec/arel/unit/relations/array_spec.rb | 75 +++++++++++++++++++++++++++++----- spec/arel/unit/relations/order_spec.rb | 12 +++--- 3 files changed, 72 insertions(+), 19 deletions(-) (limited to 'spec') diff --git a/spec/arel/unit/relations/alias_spec.rb b/spec/arel/unit/relations/alias_spec.rb index 570f315892..63c15cfeff 100644 --- a/spec/arel/unit/relations/alias_spec.rb +++ b/spec/arel/unit/relations/alias_spec.rb @@ -30,7 +30,7 @@ module Arel FROM `users` WHERE `users`.`id` = 1 GROUP BY `users`.`id` - ORDER BY `users`.`id` + ORDER BY `users`.`id` ASC }) end @@ -40,7 +40,7 @@ module Arel FROM "users" WHERE "users"."id" = 1 GROUP BY "users"."id" - ORDER BY "users"."id" + ORDER BY "users"."id" ASC }) end end diff --git a/spec/arel/unit/relations/array_spec.rb b/spec/arel/unit/relations/array_spec.rb index c90843cd7d..d1c65c60a9 100644 --- a/spec/arel/unit/relations/array_spec.rb +++ b/spec/arel/unit/relations/array_spec.rb @@ -3,13 +3,18 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') module Arel describe Array do before do - @relation = Array.new([[1], [2], [3]], [:id]) + @relation = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) end describe '#attributes' do it 'manufactures attributes corresponding to the names given on construction' do @relation.attributes.should == [ - Attribute.new(@relation, :id) + Attribute.new(@relation, :id), + Attribute.new(@relation, :name) ] end end @@ -17,17 +22,65 @@ module Arel describe '#call' do it "manufactures an array of hashes of attributes to values" do @relation.call.should == [ - { @relation[:id] => 1 }, - { @relation[:id] => 2 }, - { @relation[:id] => 3 } + { @relation[:id] => 1, @relation[:name] => 'duck' }, + { @relation[:id] => 2, @relation[:name] => 'duck' }, + { @relation[:id] => 3, @relation[:name] => 'goose' } ] end - - it '' do - @relation.where(@relation[:id].lt(3)).call.should == [ - { @relation[:id] => 1 }, - { @relation[:id] => 2 } - ] + + describe 'where' do + it 'filters the relation with the provided predicate' do + @relation.where(@relation[:id].lt(3)).call.should == [ + { @relation[:id] => 1, @relation[:name] => 'duck' }, + { @relation[:id] => 2, @relation[:name] => 'duck' } + ] + end + end + + describe 'group' do + it 'sorts the relation with the provided ordering' do + end + end + + describe 'order' do + it 'sorts the relation with the provided ordering' do + @relation.order(@relation[:id].desc).call.should == [ + { @relation[:id] => 3, @relation[:name] => 'goose' }, + { @relation[:id] => 2, @relation[:name] => 'duck' }, + { @relation[:id] => 1, @relation[:name] => 'duck' } + ] + end + end + + describe 'project' do + it 'projects' do + @relation.project(@relation[:id]).call.should == [ + { @relation[:id] => 1 }, + { @relation[:id] => 2 }, + { @relation[:id] => 3 } + ] + end + end + + describe 'skip' do + it 'slices' do + @relation.skip(1).call.should == [ + { @relation[:id] => 2, @relation[:name] => 'duck' }, + { @relation[:id] => 3, @relation[:name] => 'goose' } + ] + end + end + + describe 'take' do + it 'dices' do + @relation.take(2).call.should == [ + { @relation[:id] => 1, @relation[:name] => 'duck' }, + { @relation[:id] => 2, @relation[:name] => 'duck' } + ] + end + end + + describe 'join' do end end end diff --git a/spec/arel/unit/relations/order_spec.rb b/spec/arel/unit/relations/order_spec.rb index 31014ddd39..cb0f1de84c 100644 --- a/spec/arel/unit/relations/order_spec.rb +++ b/spec/arel/unit/relations/order_spec.rb @@ -16,7 +16,7 @@ module Arel sql.should be_like(%Q{ SELECT `users`.`id`, `users`.`name` FROM `users` - ORDER BY `users`.`id` + ORDER BY `users`.`id` ASC }) end @@ -24,7 +24,7 @@ module Arel sql.should be_like(%Q{ SELECT "users"."id", "users"."name" FROM "users" - ORDER BY "users"."id" + ORDER BY "users"."id" ASC }) end end @@ -42,7 +42,7 @@ module Arel sql.should be_like(%Q{ SELECT `users`.`id`, `users`.`name` FROM `users` - ORDER BY `users`.`id`, `users`.`name` + ORDER BY `users`.`id` ASC, `users`.`name` ASC }) end @@ -50,7 +50,7 @@ module Arel sql.should be_like(%Q{ SELECT "users"."id", "users"."name" FROM "users" - ORDER BY "users"."id", "users"."name" + ORDER BY "users"."id" ASC, "users"."name" ASC }) end end @@ -95,7 +95,7 @@ module Arel sql.should be_like(%Q{ SELECT `users`.`id`, `users`.`name` FROM `users` - ORDER BY `users`.`name`, `users`.`id` + ORDER BY `users`.`name` ASC, `users`.`id` ASC }) end @@ -103,7 +103,7 @@ module Arel sql.should be_like(%Q{ SELECT "users"."id", "users"."name" FROM "users" - ORDER BY "users"."name", "users"."id" + ORDER BY "users"."name" ASC, "users"."id" ASC }) end end -- cgit v1.2.3 From a7aacd29460aa137c3b06ee214ff8ffdff8ee365 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Mon, 26 May 2008 21:02:23 -0700 Subject: reorganizing tests --- spec/arel/algebra/unit/predicates/binary_spec.rb | 33 ++++ spec/arel/algebra/unit/predicates/equality_spec.rb | 27 +++ spec/arel/algebra/unit/predicates/in_spec.rb | 10 + .../arel/algebra/unit/primitives/attribute_spec.rb | 169 +++++++++++++++++ .../algebra/unit/primitives/expression_spec.rb | 39 ++++ spec/arel/algebra/unit/primitives/value_spec.rb | 15 ++ spec/arel/algebra/unit/relations/alias_spec.rb | 16 ++ spec/arel/algebra/unit/relations/delete_spec.rb | 9 + spec/arel/algebra/unit/relations/group_spec.rb | 10 + spec/arel/algebra/unit/relations/insert_spec.rb | 9 + spec/arel/algebra/unit/relations/join_spec.rb | 26 +++ spec/arel/algebra/unit/relations/order_spec.rb | 11 ++ spec/arel/algebra/unit/relations/project_spec.rb | 34 ++++ spec/arel/algebra/unit/relations/relation_spec.rb | 188 ++++++++++++++++++ spec/arel/algebra/unit/relations/skip_spec.rb | 10 + spec/arel/algebra/unit/relations/table_spec.rb | 39 ++++ spec/arel/algebra/unit/relations/take_spec.rb | 10 + spec/arel/algebra/unit/relations/update_spec.rb | 9 + spec/arel/algebra/unit/relations/where_spec.rb | 18 ++ spec/arel/algebra/unit/session/session_spec.rb | 84 +++++++++ .../sql/integration/joins/with_adjacency_spec.rb | 209 +++++++++++++++++++++ .../integration/joins/with_aggregations_spec.rb | 167 ++++++++++++++++ .../sql/integration/joins/with_compounds_spec.rb | 107 +++++++++++ .../engines/sql/unit/predicates/binary_spec.rb | 125 ++++++++++++ .../engines/sql/unit/predicates/equality_spec.rb | 61 ++++++ spec/arel/engines/sql/unit/predicates/in_spec.rb | 86 +++++++++ .../engines/sql/unit/primitives/attribute_spec.rb | 171 +++++++++++++++++ .../engines/sql/unit/primitives/expression_spec.rb | 53 ++++++ .../arel/engines/sql/unit/primitives/value_spec.rb | 28 +++ spec/arel/engines/sql/unit/relations/alias_spec.rb | 50 +++++ spec/arel/engines/sql/unit/relations/array_spec.rb | 87 +++++++++ .../arel/engines/sql/unit/relations/delete_spec.rb | 63 +++++++ spec/arel/engines/sql/unit/relations/group_spec.rb | 56 ++++++ .../arel/engines/sql/unit/relations/insert_spec.rb | 91 +++++++++ spec/arel/engines/sql/unit/relations/join_spec.rb | 78 ++++++++ spec/arel/engines/sql/unit/relations/order_spec.rb | 113 +++++++++++ .../engines/sql/unit/relations/project_spec.rb | 134 +++++++++++++ .../engines/sql/unit/relations/relation_spec.rb | 169 +++++++++++++++++ spec/arel/engines/sql/unit/relations/skip_spec.rb | 32 ++++ spec/arel/engines/sql/unit/relations/table_spec.rb | 99 ++++++++++ spec/arel/engines/sql/unit/relations/take_spec.rb | 32 ++++ .../arel/engines/sql/unit/relations/update_spec.rb | 121 ++++++++++++ spec/arel/engines/sql/unit/relations/where_spec.rb | 64 +++++++ spec/arel/engines/sql/unit/session/session_spec.rb | 84 +++++++++ spec/arel/integration/joins/with_adjacency_spec.rb | 209 --------------------- .../integration/joins/with_aggregations_spec.rb | 167 ---------------- spec/arel/integration/joins/with_compounds_spec.rb | 107 ----------- spec/arel/unit/predicates/binary_spec.rb | 125 ------------ spec/arel/unit/predicates/equality_spec.rb | 61 ------ spec/arel/unit/predicates/in_spec.rb | 86 --------- spec/arel/unit/primitives/attribute_spec.rb | 171 ----------------- spec/arel/unit/primitives/expression_spec.rb | 53 ------ spec/arel/unit/primitives/value_spec.rb | 28 --- spec/arel/unit/relations/alias_spec.rb | 50 ----- spec/arel/unit/relations/array_spec.rb | 87 --------- spec/arel/unit/relations/delete_spec.rb | 63 ------- spec/arel/unit/relations/group_spec.rb | 56 ------ spec/arel/unit/relations/insert_spec.rb | 91 --------- spec/arel/unit/relations/join_spec.rb | 78 -------- spec/arel/unit/relations/order_spec.rb | 113 ----------- spec/arel/unit/relations/project_spec.rb | 134 ------------- spec/arel/unit/relations/relation_spec.rb | 169 ----------------- spec/arel/unit/relations/skip_spec.rb | 32 ---- spec/arel/unit/relations/table_spec.rb | 99 ---------- spec/arel/unit/relations/take_spec.rb | 32 ---- spec/arel/unit/relations/update_spec.rb | 121 ------------ spec/arel/unit/relations/where_spec.rb | 64 ------- spec/arel/unit/session/session_spec.rb | 84 --------- 68 files changed, 3046 insertions(+), 2280 deletions(-) create mode 100644 spec/arel/algebra/unit/predicates/binary_spec.rb create mode 100644 spec/arel/algebra/unit/predicates/equality_spec.rb create mode 100644 spec/arel/algebra/unit/predicates/in_spec.rb create mode 100644 spec/arel/algebra/unit/primitives/attribute_spec.rb create mode 100644 spec/arel/algebra/unit/primitives/expression_spec.rb create mode 100644 spec/arel/algebra/unit/primitives/value_spec.rb create mode 100644 spec/arel/algebra/unit/relations/alias_spec.rb create mode 100644 spec/arel/algebra/unit/relations/delete_spec.rb create mode 100644 spec/arel/algebra/unit/relations/group_spec.rb create mode 100644 spec/arel/algebra/unit/relations/insert_spec.rb create mode 100644 spec/arel/algebra/unit/relations/join_spec.rb create mode 100644 spec/arel/algebra/unit/relations/order_spec.rb create mode 100644 spec/arel/algebra/unit/relations/project_spec.rb create mode 100644 spec/arel/algebra/unit/relations/relation_spec.rb create mode 100644 spec/arel/algebra/unit/relations/skip_spec.rb create mode 100644 spec/arel/algebra/unit/relations/table_spec.rb create mode 100644 spec/arel/algebra/unit/relations/take_spec.rb create mode 100644 spec/arel/algebra/unit/relations/update_spec.rb create mode 100644 spec/arel/algebra/unit/relations/where_spec.rb create mode 100644 spec/arel/algebra/unit/session/session_spec.rb create mode 100644 spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb create mode 100644 spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb create mode 100644 spec/arel/engines/sql/integration/joins/with_compounds_spec.rb create mode 100644 spec/arel/engines/sql/unit/predicates/binary_spec.rb create mode 100644 spec/arel/engines/sql/unit/predicates/equality_spec.rb create mode 100644 spec/arel/engines/sql/unit/predicates/in_spec.rb create mode 100644 spec/arel/engines/sql/unit/primitives/attribute_spec.rb create mode 100644 spec/arel/engines/sql/unit/primitives/expression_spec.rb create mode 100644 spec/arel/engines/sql/unit/primitives/value_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/alias_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/array_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/delete_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/group_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/insert_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/join_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/order_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/project_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/relation_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/skip_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/table_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/take_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/update_spec.rb create mode 100644 spec/arel/engines/sql/unit/relations/where_spec.rb create mode 100644 spec/arel/engines/sql/unit/session/session_spec.rb delete mode 100644 spec/arel/integration/joins/with_adjacency_spec.rb delete mode 100644 spec/arel/integration/joins/with_aggregations_spec.rb delete mode 100644 spec/arel/integration/joins/with_compounds_spec.rb delete mode 100644 spec/arel/unit/predicates/binary_spec.rb delete mode 100644 spec/arel/unit/predicates/equality_spec.rb delete mode 100644 spec/arel/unit/predicates/in_spec.rb delete mode 100644 spec/arel/unit/primitives/attribute_spec.rb delete mode 100644 spec/arel/unit/primitives/expression_spec.rb delete mode 100644 spec/arel/unit/primitives/value_spec.rb delete mode 100644 spec/arel/unit/relations/alias_spec.rb delete mode 100644 spec/arel/unit/relations/array_spec.rb delete mode 100644 spec/arel/unit/relations/delete_spec.rb delete mode 100644 spec/arel/unit/relations/group_spec.rb delete mode 100644 spec/arel/unit/relations/insert_spec.rb delete mode 100644 spec/arel/unit/relations/join_spec.rb delete mode 100644 spec/arel/unit/relations/order_spec.rb delete mode 100644 spec/arel/unit/relations/project_spec.rb delete mode 100644 spec/arel/unit/relations/relation_spec.rb delete mode 100644 spec/arel/unit/relations/skip_spec.rb delete mode 100644 spec/arel/unit/relations/table_spec.rb delete mode 100644 spec/arel/unit/relations/take_spec.rb delete mode 100644 spec/arel/unit/relations/update_spec.rb delete mode 100644 spec/arel/unit/relations/where_spec.rb delete mode 100644 spec/arel/unit/session/session_spec.rb (limited to 'spec') diff --git a/spec/arel/algebra/unit/predicates/binary_spec.rb b/spec/arel/algebra/unit/predicates/binary_spec.rb new file mode 100644 index 0000000000..9022a543d1 --- /dev/null +++ b/spec/arel/algebra/unit/predicates/binary_spec.rb @@ -0,0 +1,33 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Binary do + before do + @relation = Table.new(:users) + @attribute1 = @relation[:id] + @attribute2 = @relation[:name] + class ConcreteBinary < Binary + 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). \ + should == ConcreteBinary.new(@attribute1.find_correlate_in(@another_relation), "asdf".find_correlate_in(@another_relation)) + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/predicates/equality_spec.rb b/spec/arel/algebra/unit/predicates/equality_spec.rb new file mode 100644 index 0000000000..9a56ed5eaf --- /dev/null +++ b/spec/arel/algebra/unit/predicates/equality_spec.rb @@ -0,0 +1,27 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Equality do + before do + @relation1 = Table.new(:users) + @relation2 = Table.new(:photos) + @attribute1 = @relation1[:id] + @attribute2 = @relation2[:user_id] + end + + 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 + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/predicates/in_spec.rb b/spec/arel/algebra/unit/predicates/in_spec.rb new file mode 100644 index 0000000000..91c154763c --- /dev/null +++ b/spec/arel/algebra/unit/predicates/in_spec.rb @@ -0,0 +1,10 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe In do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/primitives/attribute_spec.rb b/spec/arel/algebra/unit/primitives/attribute_spec.rb new file mode 100644 index 0000000000..bab9fad3d5 --- /dev/null +++ b/spec/arel/algebra/unit/primitives/attribute_spec.rb @@ -0,0 +1,169 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Attribute do + before do + @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]) \ + .should == (@aliased_relation[:id] / @relation[:id]) + (@aliased_relation[:id] / @relation[:id]) \ + .should < (@aliased_relation[:id] / @aliased_relation[: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) + end + + describe '#count' do + it "manufactures a count Expression" do + @attribute.count.should == Count.new(@attribute) + end + end + + describe '#sum' do + it "manufactures a sum Expression" do + @attribute.sum.should == Sum.new(@attribute) + end + end + + describe '#maximum' do + it "manufactures a maximum Expression" do + @attribute.maximum.should == Maximum.new(@attribute) + end + end + + describe '#minimum' do + it "manufactures a minimum Expression" do + @attribute.minimum.should == Minimum.new(@attribute) + end + end + + describe '#average' do + it "manufactures an average Expression" do + @attribute.average.should == Average.new(@attribute) + end + end + end + + describe Attribute::Orderings do + describe '#asc' do + it 'manufactures an ascending ordering' do + pending + end + end + + describe '#desc' do + it 'manufactures a descending ordering' do + pending + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/primitives/expression_spec.rb b/spec/arel/algebra/unit/primitives/expression_spec.rb new file mode 100644 index 0000000000..10bdb56302 --- /dev/null +++ b/spec/arel/algebra/unit/primitives/expression_spec.rb @@ -0,0 +1,39 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Expression do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + + describe Expression::Transformations do + before do + @expression = Count.new(@attribute) + end + + describe '#bind' do + it "manufactures an attribute with a rebound relation and self as the ancestor" do + derived_relation = @relation.where(@relation[:id].eq(1)) + @expression.bind(derived_relation).should == Count.new(@attribute.bind(derived_relation), nil, @expression) + end + + it "returns self if the substituting to the same relation" do + @expression.bind(@relation).should == @expression + end + end + + describe '#as' do + it "manufactures an aliased expression" do + @expression.as(:alias).should == Expression.new(@attribute, :alias, @expression) + end + end + + describe '#to_attribute' do + it "manufactures an attribute with the expression as an ancestor" do + @expression.to_attribute.should == Attribute.new(@expression.relation, @expression.alias, :ancestor => @expression) + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/primitives/value_spec.rb b/spec/arel/algebra/unit/primitives/value_spec.rb new file mode 100644 index 0000000000..8774ca78c5 --- /dev/null +++ b/spec/arel/algebra/unit/primitives/value_spec.rb @@ -0,0 +1,15 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Value do + before do + @relation = Table.new(:users) + end + + describe '#bind' do + it "manufactures a new value whose relation is the provided relation" do + Value.new(1, @relation).bind(another_relation = Table.new(:photos)).should == Value.new(1, another_relation) + end + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/alias_spec.rb b/spec/arel/algebra/unit/relations/alias_spec.rb new file mode 100644 index 0000000000..c87a0ca2dd --- /dev/null +++ b/spec/arel/algebra/unit/relations/alias_spec.rb @@ -0,0 +1,16 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Alias do + before do + @relation = Table.new(:users) + end + + describe '==' do + it "obtains if the objects are the same" do + Alias.new(@relation).should_not == Alias.new(@relation) + (aliaz = Alias.new(@relation)).should == aliaz + end + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/delete_spec.rb b/spec/arel/algebra/unit/relations/delete_spec.rb new file mode 100644 index 0000000000..075e59e724 --- /dev/null +++ b/spec/arel/algebra/unit/relations/delete_spec.rb @@ -0,0 +1,9 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Deletion do + before do + @relation = Table.new(:users) + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/group_spec.rb b/spec/arel/algebra/unit/relations/group_spec.rb new file mode 100644 index 0000000000..050de2993d --- /dev/null +++ b/spec/arel/algebra/unit/relations/group_spec.rb @@ -0,0 +1,10 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Group do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/insert_spec.rb b/spec/arel/algebra/unit/relations/insert_spec.rb new file mode 100644 index 0000000000..184cd2a926 --- /dev/null +++ b/spec/arel/algebra/unit/relations/insert_spec.rb @@ -0,0 +1,9 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Insert do + before do + @relation = Table.new(:users) + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/join_spec.rb b/spec/arel/algebra/unit/relations/join_spec.rb new file mode 100644 index 0000000000..5b512cc7f6 --- /dev/null +++ b/spec/arel/algebra/unit/relations/join_spec.rb @@ -0,0 +1,26 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Join do + before do + @relation1 = Table.new(:users) + @relation2 = Table.new(:photos) + @predicate = @relation1[:id].eq(@relation2[:user_id]) + end + + describe 'hashing' do + it 'implements hash equality' do + InnerJoin.new(@relation1, @relation2, @predicate) \ + .should hash_the_same_as(InnerJoin.new(@relation1, @relation2, @predicate)) + end + end + + describe '#attributes' do + it 'combines the attributes of the two relations' do + join = InnerJoin.new(@relation1, @relation2, @predicate) + join.attributes.should == + (@relation1.attributes + @relation2.attributes).collect { |a| a.bind(join) } + end + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/order_spec.rb b/spec/arel/algebra/unit/relations/order_spec.rb new file mode 100644 index 0000000000..0e1b1a0e54 --- /dev/null +++ b/spec/arel/algebra/unit/relations/order_spec.rb @@ -0,0 +1,11 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Order do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + end +end + \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/project_spec.rb b/spec/arel/algebra/unit/relations/project_spec.rb new file mode 100644 index 0000000000..b71acf5e91 --- /dev/null +++ b/spec/arel/algebra/unit/relations/project_spec.rb @@ -0,0 +1,34 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Project do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + + describe '#attributes' do + before do + @projection = Project.new(@relation, @attribute) + end + + it "manufactures attributes associated with the projection relation" do + @projection.attributes.should == [@attribute].collect { |a| a.bind(@projection) } + end + end + + describe '#externalizable?' do + describe 'when the projections are attributes' do + it 'returns false' do + Project.new(@relation, @attribute).should_not be_externalizable + end + end + + describe 'when the projections include an aggregation' do + it "obtains" do + Project.new(@relation, @attribute.sum).should be_externalizable + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/relation_spec.rb b/spec/arel/algebra/unit/relations/relation_spec.rb new file mode 100644 index 0000000000..3286f373f5 --- /dev/null +++ b/spec/arel/algebra/unit/relations/relation_spec.rb @@ -0,0 +1,188 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Relation do + before do + @relation = Table.new(:users) + @attribute1 = @relation[:id] + @attribute2 = @relation[:name] + end + + describe '[]' do + describe 'when given an', Attribute do + it "return the attribute congruent to the provided attribute" do + @relation[@attribute1].should == @attribute1 + end + end + + describe 'when given a', Symbol, String do + it "returns the attribute with the same name, if it exists" do + @relation[:id].should == @attribute1 + @relation['id'].should == @attribute1 + @relation[:does_not_exist].should be_nil + end + end + end + + describe Relation::Operable do + describe 'joins' do + before do + @predicate = @relation[:id].eq(@relation[:id]) + end + + describe '#join' do + describe 'when given a relation' do + it "manufactures an inner join operation between those two relations" do + @relation.join(@relation).on(@predicate). \ + should == InnerJoin.new(@relation, @relation, @predicate) + end + end + + describe "when given a string" do + it "manufactures a join operation with the string passed through" do + @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) + end + end + + describe "when given something blank" do + it "returns self" do + @relation.join.should == @relation + end + end + end + + describe '#outer_join' do + it "manufactures a left outer join operation between those two relations" do + @relation.outer_join(@relation).on(@predicate). \ + should == OuterJoin.new(@relation, @relation, @predicate) + end + end + end + + describe '#project' do + it "manufactures a projection relation" do + @relation.project(@attribute1, @attribute2). \ + should == Project.new(@relation, @attribute1, @attribute2) + end + + describe "when given blank attributes" do + it "returns self" do + @relation.project.should == @relation + end + end + end + + describe '#alias' do + it "manufactures an alias relation" do + @relation.alias.relation.should == Alias.new(@relation).relation + end + end + + describe '#where' do + before do + @predicate = Equality.new(@attribute1, @attribute2) + end + + it "manufactures a where relation" do + @relation.where(@predicate).should == Where.new(@relation, @predicate) + end + + it "accepts arbitrary strings" do + @relation.where("arbitrary").should == Where.new(@relation, "arbitrary") + end + + describe 'when given a blank predicate' do + it 'returns self' do + @relation.where.should == @relation + end + end + end + + describe '#order' do + it "manufactures an order relation" do + @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2) + end + + describe 'when given a blank ordering' do + it 'returns self' do + @relation.order.should == @relation + end + end + end + + describe '#take' do + it "manufactures a take relation" do + @relation.take(5).should == Take.new(@relation, 5) + end + + describe 'when given a blank number of items' do + it 'returns self' do + @relation.take.should == @relation + end + end + end + + describe '#skip' do + it "manufactures a skip relation" do + @relation.skip(4).should == Skip.new(@relation, 4) + end + + describe 'when given a blank number of items' do + it 'returns self' do + @relation.skip.should == @relation + end + end + end + + describe '#group' do + it 'manufactures a group relation' do + @relation.group(@attribute1, @attribute2).should == Group.new(@relation, @attribute1, @attribute2) + end + + describe 'when given blank groupings' do + it 'returns self' do + @relation.group.should == @relation + end + end + end + + describe Relation::Operable::Writable do + describe '#delete' do + it 'manufactures a deletion relation' do + Session.start do + mock(Session.new).delete(Deletion.new(@relation)) + @relation.delete + end + end + end + + describe '#insert' do + it 'manufactures an insertion relation' do + Session.start do + record = {@relation[:name] => 'carl'} + mock(Session.new).create(Insert.new(@relation, record)) + @relation.insert(record) + end + end + end + + describe '#update' do + it 'manufactures an update relation' do + Session.start do + assignments = {@relation[:name] => Value.new('bob', @relation)} + mock(Session.new).update(Update.new(@relation, assignments)) + @relation.update(assignments) + end + end + end + end + end + + describe Relation::Enumerable do + it "implements enumerable" do + @relation.collect.should == @relation.session.read(@relation) + @relation.first.should == @relation.session.read(@relation).first + end + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/skip_spec.rb b/spec/arel/algebra/unit/relations/skip_spec.rb new file mode 100644 index 0000000000..ff57e03d1c --- /dev/null +++ b/spec/arel/algebra/unit/relations/skip_spec.rb @@ -0,0 +1,10 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Skip do + before do + @relation = Table.new(:users) + @skipped = 4 + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/table_spec.rb b/spec/arel/algebra/unit/relations/table_spec.rb new file mode 100644 index 0000000000..4821d92299 --- /dev/null +++ b/spec/arel/algebra/unit/relations/table_spec.rb @@ -0,0 +1,39 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Table do + 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 + @relation[:id].should == Attribute.new(@relation, :id) + @relation[:does_not_exist].should be_nil + end + end + + describe 'when given an', Attribute do + 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 + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/take_spec.rb b/spec/arel/algebra/unit/relations/take_spec.rb new file mode 100644 index 0000000000..6f8b4fd36e --- /dev/null +++ b/spec/arel/algebra/unit/relations/take_spec.rb @@ -0,0 +1,10 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Take do + before do + @relation = Table.new(:users) + @taken = 4 + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/update_spec.rb b/spec/arel/algebra/unit/relations/update_spec.rb new file mode 100644 index 0000000000..c27afb48b2 --- /dev/null +++ b/spec/arel/algebra/unit/relations/update_spec.rb @@ -0,0 +1,9 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Update do + before do + @relation = Table.new(:users) + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/relations/where_spec.rb b/spec/arel/algebra/unit/relations/where_spec.rb new file mode 100644 index 0000000000..3f37b53138 --- /dev/null +++ b/spec/arel/algebra/unit/relations/where_spec.rb @@ -0,0 +1,18 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Where do + before do + @relation = Table.new(:users) + @predicate = @relation[:id].eq(1) + end + + describe '#initialize' do + it "manufactures nested where relations if multiple predicates are provided" do + another_predicate = @relation[:name].lt(2) + Where.new(@relation, @predicate, another_predicate). \ + should == Where.new(Where.new(@relation, another_predicate), @predicate) + end + end + end +end \ No newline at end of file diff --git a/spec/arel/algebra/unit/session/session_spec.rb b/spec/arel/algebra/unit/session/session_spec.rb new file mode 100644 index 0000000000..e17b5d638a --- /dev/null +++ b/spec/arel/algebra/unit/session/session_spec.rb @@ -0,0 +1,84 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Session do + before do + @relation = Table.new(:users) + @session = Session.new + end + + describe '::start' do + describe '::instance' do + it "it is a singleton within the started session" do + Session.start do + Session.new.should == Session.new + end + end + + it "is a singleton across nested sessions" do + Session.start do + outside = Session.new + Session.start do + Session.new.should == outside + end + end + end + + it "manufactures new sessions outside of the started session" do + Session.new.should_not == Session.new + end + end + end + + describe Session::CRUD do + before do + @insert = Insert.new(@relation, @relation[:name] => 'nick') + @update = Update.new(@relation, @relation[:name] => 'nick') + @delete = Deletion.new(@relation) + @read = @relation + end + + describe '#create' do + it "executes an insertion on the connection" do + mock(@insert).call + @session.create(@insert) + end + end + + describe '#read' do + it "executes an selection on the connection" do + mock(@read).call + @session.read(@read) + end + + it "is memoized" do + mock(@read).call.once + @session.read(@read) + @session.read(@read) + end + end + + describe '#update' do + it "executes an update on the connection" do + mock(@update).call + @session.update(@update) + end + end + + describe '#delete' do + it "executes a delete on the connection" do + mock(@delete).call + @session.delete(@delete) + end + end + end + + describe 'Transactions' do + describe '#begin' do + end + + describe '#end' do + end + end + end +end \ No newline at end of file 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 diff --git a/spec/arel/engines/sql/unit/predicates/binary_spec.rb b/spec/arel/engines/sql/unit/predicates/binary_spec.rb new file mode 100644 index 0000000000..03574225c2 --- /dev/null +++ b/spec/arel/engines/sql/unit/predicates/binary_spec.rb @@ -0,0 +1,125 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Binary do + before do + @relation = Table.new(:users) + @attribute1 = @relation[:id] + @attribute2 = @relation[:name] + class ConcreteBinary < Binary + def predicate_sql + "<=>" + end + end + end + + describe "with compound predicates" do + before do + @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 + 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 + + describe And do + describe "#to_sql" do + it "manufactures sql with an AND operation" do + 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 + 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 + 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 + 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). \ + should == ConcreteBinary.new(@attribute1.find_correlate_in(@another_relation), "asdf".find_correlate_in(@another_relation)) + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/predicates/equality_spec.rb b/spec/arel/engines/sql/unit/predicates/equality_spec.rb new file mode 100644 index 0000000000..2ab79d7028 --- /dev/null +++ b/spec/arel/engines/sql/unit/predicates/equality_spec.rb @@ -0,0 +1,61 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Equality do + before do + @relation1 = Table.new(:users) + @relation2 = Table.new(:photos) + @attribute1 = @relation1[:id] + @attribute2 = @relation2[:user_id] + end + + 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 + 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 + 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 + end +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/predicates/in_spec.rb b/spec/arel/engines/sql/unit/predicates/in_spec.rb new file mode 100644 index 0000000000..d977937e4e --- /dev/null +++ b/spec/arel/engines/sql/unit/predicates/in_spec.rb @@ -0,0 +1,86 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe In do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + + 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 + 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 + 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 + 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 + 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 + end +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/primitives/attribute_spec.rb b/spec/arel/engines/sql/unit/primitives/attribute_spec.rb new file mode 100644 index 0000000000..f37cd14370 --- /dev/null +++ b/spec/arel/engines/sql/unit/primitives/attribute_spec.rb @@ -0,0 +1,171 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Attribute do + before do + @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]) \ + .should == (@aliased_relation[:id] / @relation[:id]) + (@aliased_relation[:id] / @relation[:id]) \ + .should < (@aliased_relation[:id] / @aliased_relation[:id]) + end + end + end + end + + describe '#to_sql' do + describe 'for a simple attribute' do + it "manufactures sql with an alias" do + 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) + end + + describe '#count' do + it "manufactures a count Expression" do + @attribute.count.should == Count.new(@attribute) + end + end + + describe '#sum' do + it "manufactures a sum Expression" do + @attribute.sum.should == Sum.new(@attribute) + end + end + + describe '#maximum' do + it "manufactures a maximum Expression" do + @attribute.maximum.should == Maximum.new(@attribute) + end + end + + describe '#minimum' do + it "manufactures a minimum Expression" do + @attribute.minimum.should == Minimum.new(@attribute) + end + end + + describe '#average' do + it "manufactures an average Expression" do + @attribute.average.should == Average.new(@attribute) + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/primitives/expression_spec.rb b/spec/arel/engines/sql/unit/primitives/expression_spec.rb new file mode 100644 index 0000000000..0869d9e403 --- /dev/null +++ b/spec/arel/engines/sql/unit/primitives/expression_spec.rb @@ -0,0 +1,53 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Expression do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + + describe Expression::Transformations do + before do + @expression = Count.new(@attribute) + end + + describe '#bind' do + it "manufactures an attribute with a rebound relation and self as the ancestor" do + derived_relation = @relation.where(@relation[:id].eq(1)) + @expression.bind(derived_relation).should == Count.new(@attribute.bind(derived_relation), nil, @expression) + end + + it "returns self if the substituting to the same relation" do + @expression.bind(@relation).should == @expression + end + end + + describe '#as' do + it "manufactures an aliased expression" do + @expression.as(:alias).should == Expression.new(@attribute, :alias, @expression) + end + end + + describe '#to_attribute' do + it "manufactures an attribute with the expression as an ancestor" do + @expression.to_attribute.should == Attribute.new(@expression.relation, @expression.alias, :ancestor => @expression) + end + end + end + + describe '#to_sql' do + it "manufactures sql with the expression and alias" do + sql = Count.new(@attribute, :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 +end diff --git a/spec/arel/engines/sql/unit/primitives/value_spec.rb b/spec/arel/engines/sql/unit/primitives/value_spec.rb new file mode 100644 index 0000000000..f76323f32b --- /dev/null +++ b/spec/arel/engines/sql/unit/primitives/value_spec.rb @@ -0,0 +1,28 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Value do + before do + @relation = Table.new(:users) + end + + describe '#to_sql' do + it "appropriately quotes the value" do + Value.new(1, @relation).to_sql.should be_like('1') + Value.new('asdf', @relation).to_sql.should be_like("'asdf'") + end + end + + describe '#format' do + it "returns the sql of the provided object" do + Value.new(1, @relation).format(@relation[:id]).should == @relation[:id].to_sql + end + end + + describe '#bind' do + it "manufactures a new value whose relation is the provided relation" do + Value.new(1, @relation).bind(another_relation = Table.new(:photos)).should == Value.new(1, another_relation) + end + end + end +end diff --git a/spec/arel/engines/sql/unit/relations/alias_spec.rb b/spec/arel/engines/sql/unit/relations/alias_spec.rb new file mode 100644 index 0000000000..83b9113f6d --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/alias_spec.rb @@ -0,0 +1,50 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Alias do + before do + @relation = Table.new(:users) + end + + describe '==' do + it "obtains if the objects are the same" do + Alias.new(@relation).should_not == Alias.new(@relation) + (aliaz = Alias.new(@relation)).should == aliaz + end + end + + describe '#to_sql' do + describe 'when there is no ambiguity' do + it 'does not alias table names anywhere a table name can appear' do + sql = @relation \ + .where(@relation[:id].eq(1)) \ + .order(@relation[:id]) \ + .project(@relation[:id]) \ + .group(@relation[:id]) \ + .alias \ + .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` ASC + }) + 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" ASC + }) + end + end + end + end + end +end diff --git a/spec/arel/engines/sql/unit/relations/array_spec.rb b/spec/arel/engines/sql/unit/relations/array_spec.rb new file mode 100644 index 0000000000..8d40858c5f --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/array_spec.rb @@ -0,0 +1,87 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Array do + before do + @relation = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + end + + describe '#attributes' do + it 'manufactures attributes corresponding to the names given on construction' do + @relation.attributes.should == [ + Attribute.new(@relation, :id), + Attribute.new(@relation, :name) + ] + end + end + + describe '#call' do + it "manufactures an array of hashes of attributes to values" do + @relation.call.should == [ + { @relation[:id] => 1, @relation[:name] => 'duck' }, + { @relation[:id] => 2, @relation[:name] => 'duck' }, + { @relation[:id] => 3, @relation[:name] => 'goose' } + ] + end + + describe 'where' do + it 'filters the relation with the provided predicate' do + @relation.where(@relation[:id].lt(3)).call.should == [ + { @relation[:id] => 1, @relation[:name] => 'duck' }, + { @relation[:id] => 2, @relation[:name] => 'duck' } + ] + end + end + + describe 'group' do + it 'sorts the relation with the provided ordering' do + end + end + + describe 'order' do + it 'sorts the relation with the provided ordering' do + @relation.order(@relation[:id].desc).call.should == [ + { @relation[:id] => 3, @relation[:name] => 'goose' }, + { @relation[:id] => 2, @relation[:name] => 'duck' }, + { @relation[:id] => 1, @relation[:name] => 'duck' } + ] + end + end + + describe 'project' do + it 'projects' do + @relation.project(@relation[:id]).call.should == [ + { @relation[:id] => 1 }, + { @relation[:id] => 2 }, + { @relation[:id] => 3 } + ] + end + end + + describe 'skip' do + it 'slices' do + @relation.skip(1).call.should == [ + { @relation[:id] => 2, @relation[:name] => 'duck' }, + { @relation[:id] => 3, @relation[:name] => 'goose' } + ] + end + end + + describe 'take' do + it 'dices' do + @relation.take(2).call.should == [ + { @relation[:id] => 1, @relation[:name] => 'duck' }, + { @relation[:id] => 2, @relation[:name] => 'duck' } + ] + end + end + + describe 'join' do + end + end + end +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/relations/delete_spec.rb b/spec/arel/engines/sql/unit/relations/delete_spec.rb new file mode 100644 index 0000000000..7a5e2b0088 --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/delete_spec.rb @@ -0,0 +1,63 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Deletion do + before do + @relation = Table.new(:users) + end + + describe '#to_sql' do + it 'manufactures sql deleting a table relation' do + 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 + 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 + 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 + end +end diff --git a/spec/arel/engines/sql/unit/relations/group_spec.rb b/spec/arel/engines/sql/unit/relations/group_spec.rb new file mode 100644 index 0000000000..b7279a23d9 --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/group_spec.rb @@ -0,0 +1,56 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Group do + before do + @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 + 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 + 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 + end +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/relations/insert_spec.rb b/spec/arel/engines/sql/unit/relations/insert_spec.rb new file mode 100644 index 0000000000..dd1995cced --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/insert_spec.rb @@ -0,0 +1,91 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Insert do + before do + @relation = Table.new(:users) + end + + describe '#to_sql' do + it 'manufactures sql inserting data when given multiple rows' do + 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') + ") + end + end + + it 'manufactures sql inserting data when given multiple values' do + @insertion = Insert.new(@relation, @relation[:id] => "1", @relation[:name] => "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 + before do + @insertion = Insert.new(@relation, @relation[:name] => "nick") + end + + it 'manufactures sql inserting data' do + 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 + + describe 'when given values whose types differ from from the types of the attributes' do + before do + @insertion = Insert.new(@relation, @relation[:id] => '1-asdf') + end + + it 'manufactures sql inserting data' do + 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 + end +end diff --git a/spec/arel/engines/sql/unit/relations/join_spec.rb b/spec/arel/engines/sql/unit/relations/join_spec.rb new file mode 100644 index 0000000000..ea17f8106f --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/join_spec.rb @@ -0,0 +1,78 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Join do + before do + @relation1 = Table.new(:users) + @relation2 = Table.new(:photos) + @predicate = @relation1[:id].eq(@relation2[:user_id]) + end + + describe 'hashing' do + it 'implements hash equality' do + InnerJoin.new(@relation1, @relation2, @predicate) \ + .should hash_the_same_as(InnerJoin.new(@relation1, @relation2, @predicate)) + end + end + + describe '#engine' do + it "delegates to a relation's engine" do + InnerJoin.new(@relation1, @relation2, @predicate).engine.should == @relation1.engine + end + end + + describe '#attributes' do + it 'combines the attributes of the two relations' do + join = InnerJoin.new(@relation1, @relation2, @predicate) + join.attributes.should == + (@relation1.attributes + @relation2.attributes).collect { |a| a.bind(join) } + end + end + + describe '#to_sql' do + describe 'when joining with another relation' do + it 'manufactures sql joining the two tables on the predicate' do + sql = InnerJoin.new(@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 + sql = StringJoin.new(@relation1, "INNER JOIN asdf ON fdsa").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 + end +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/relations/order_spec.rb b/spec/arel/engines/sql/unit/relations/order_spec.rb new file mode 100644 index 0000000000..ce97a4dd5e --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/order_spec.rb @@ -0,0 +1,113 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Order do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + + describe '#to_sql' do + describe "when given an attribute" do + it "manufactures sql with an order clause populated by the attribute" do + 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` ASC + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + ORDER BY "users"."id" ASC + }) + 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 + 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` ASC, `users`.`name` ASC + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + ORDER BY "users"."id" ASC, "users"."name" ASC + }) + end + end + end + + describe "when given a string" do + before do + @string = "asdf" + end + + it "passes the string through to the order clause" do + 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 + 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` ASC, `users`.`id` ASC + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + ORDER BY "users"."name" ASC, "users"."id" ASC + }) + end + end + end + end + end +end diff --git a/spec/arel/engines/sql/unit/relations/project_spec.rb b/spec/arel/engines/sql/unit/relations/project_spec.rb new file mode 100644 index 0000000000..9d6b9fab06 --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/project_spec.rb @@ -0,0 +1,134 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Project do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + + describe '#attributes' do + before do + @projection = Project.new(@relation, @attribute) + end + + it "manufactures attributes associated with the projection relation" do + @projection.attributes.should == [@attribute].collect { |a| a.bind(@projection) } + end + end + + describe '#to_sql' do + describe 'when given an attribute' do + it "manufactures sql with a limited select clause" do + 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 + + describe 'when given a relation' do + before do + @scalar_relation = Project.new(@relation, @relation[:name]) + end + + it "manufactures sql with scalar selects" do + 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 + 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 + 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 + 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 + + describe '#externalizable?' do + describe 'when the projections are attributes' do + it 'returns false' do + Project.new(@relation, @attribute).should_not be_externalizable + end + end + + describe 'when the projections include an aggregation' do + it "obtains" do + Project.new(@relation, @attribute.sum).should be_externalizable + end + end + end + end +end diff --git a/spec/arel/engines/sql/unit/relations/relation_spec.rb b/spec/arel/engines/sql/unit/relations/relation_spec.rb new file mode 100644 index 0000000000..9a5cfbff41 --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/relation_spec.rb @@ -0,0 +1,169 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Relation do + before do + @relation = Table.new(:users) + @attribute1 = @relation[:id] + @attribute2 = @relation[:name] + end + + describe '[]' do + describe 'when given an', Attribute do + it "return the attribute congruent to the provided attribute" do + @relation[@attribute1].should == @attribute1 + end + end + + describe 'when given a', Symbol, String do + it "returns the attribute with the same name, if it exists" do + @relation[:id].should == @attribute1 + @relation['id'].should == @attribute1 + @relation[:does_not_exist].should be_nil + end + end + end + + describe Relation::Operable do + describe 'joins' do + before do + @predicate = @relation[:id].eq(@relation[:id]) + end + + describe '#join' do + describe 'when given a relation' do + it "manufactures an inner join operation between those two relations" do + @relation.join(@relation).on(@predicate). \ + should == InnerJoin.new(@relation, @relation, @predicate) + end + end + + describe "when given a string" do + it "manufactures a join operation with the string passed through" do + @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) + end + end + + describe "when given something blank" do + it "returns self" do + @relation.join.should == @relation + end + end + end + + describe '#outer_join' do + it "manufactures a left outer join operation between those two relations" do + @relation.outer_join(@relation).on(@predicate). \ + should == OuterJoin.new(@relation, @relation, @predicate) + end + end + end + + describe '#project' do + it "manufactures a projection relation" do + @relation.project(@attribute1, @attribute2). \ + should == Project.new(@relation, @attribute1, @attribute2) + end + + describe "when given blank attributes" do + it "returns self" do + @relation.project.should == @relation + end + end + end + + describe '#alias' do + it "manufactures an alias relation" do + @relation.alias.relation.should == Alias.new(@relation).relation + end + end + + describe '#where' do + before do + @predicate = Equality.new(@attribute1, @attribute2) + end + + it "manufactures a where relation" do + @relation.where(@predicate).should == Where.new(@relation, @predicate) + end + + it "accepts arbitrary strings" do + @relation.where("arbitrary").should == Where.new(@relation, "arbitrary") + end + + describe 'when given a blank predicate' do + it 'returns self' do + @relation.where.should == @relation + end + end + end + + describe '#order' do + it "manufactures an order relation" do + @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2) + end + + describe 'when given a blank ordering' do + it 'returns self' do + @relation.order.should == @relation + end + end + end + + describe '#take' do + it "manufactures a take relation" do + @relation.take(5).should == Take.new(@relation, 5) + end + + describe 'when given a blank number of items' do + it 'returns self' do + @relation.take.should == @relation + end + end + end + + describe '#skip' do + it "manufactures a skip relation" do + @relation.skip(4).should == Skip.new(@relation, 4) + end + + describe 'when given a blank number of items' do + it 'returns self' do + @relation.skip.should == @relation + end + end + end + + describe '#group' do + it 'manufactures a group relation' do + @relation.group(@attribute1, @attribute2).should == Group.new(@relation, @attribute1, @attribute2) + end + + describe 'when given blank groupings' do + it 'returns self' do + @relation.group.should == @relation + end + end + end + + describe Relation::Operable::Writable do + describe '#insert' do + it 'manufactures an insertion relation' do + Session.start do + record = {@relation[:name] => 'carl'} + mock(Session.new).create(Insert.new(@relation, record)) + @relation.insert(record).should == @relation + end + end + end + end + end + + describe Relation::Enumerable do + it "implements enumerable" do + @relation.collect.should == @relation.session.read(@relation) + @relation.first.should == @relation.session.read(@relation).first + end + end + end +end diff --git a/spec/arel/engines/sql/unit/relations/skip_spec.rb b/spec/arel/engines/sql/unit/relations/skip_spec.rb new file mode 100644 index 0000000000..f8e5ceaad3 --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/skip_spec.rb @@ -0,0 +1,32 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Skip do + before do + @relation = Table.new(:users) + @skipped = 4 + end + + describe '#to_sql' do + it "manufactures sql with limit and offset" do + sql = Skip.new(@relation, @skipped).to_sql + + 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 +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/relations/table_spec.rb b/spec/arel/engines/sql/unit/relations/table_spec.rb new file mode 100644 index 0000000000..6de49a9157 --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/table_spec.rb @@ -0,0 +1,99 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Table do + 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 + @relation[:id].should == Attribute.new(@relation, :id) + @relation[:does_not_exist].should be_nil + end + end + + describe 'when given an', Attribute do + 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 + 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 == [ + Attribute.new(@relation, :id), + 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 } + lambda { @relation.reset }.should change { @relation.attributes } + 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 = Sql::Engine.new + Table.new(:users).engine.should == engine + end + + it "can be specified" do + Table.new(:users, engine = Sql::Engine.new).engine.should == engine + end + end + end +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/relations/take_spec.rb b/spec/arel/engines/sql/unit/relations/take_spec.rb new file mode 100644 index 0000000000..1263ed4795 --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/take_spec.rb @@ -0,0 +1,32 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Take do + before do + @relation = Table.new(:users) + @taken = 4 + end + + describe '#to_sql' do + it "manufactures sql with limit and offset" do + sql = Take.new(@relation, @taken).to_sql + + 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 +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/relations/update_spec.rb b/spec/arel/engines/sql/unit/relations/update_spec.rb new file mode 100644 index 0000000000..f553490ef5 --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/update_spec.rb @@ -0,0 +1,121 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Update do + before do + @relation = Table.new(:users) + end + + describe '#to_sql' do + it "manufactures sql updating attributes when given multiple attributes" do + 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 + 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 + before do + @update = Update.new(@relation, @relation[:name] => "nick") + end + + it 'manufactures sql updating attributes' do + 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 + + describe 'when given values whose types differ from from the types of the attributes' do + before do + @update = Update.new(@relation, @relation[:id] => '1-asdf') + end + + it 'manufactures sql updating attributes' do + 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 + + describe 'when the relation is a where' do + before do + @update = Update.new( + @relation.where(@relation[:id].eq(1)), + @relation[:name] => "nick" + ) + end + + it 'manufactures sql updating a where relation' do + 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 + + end +end diff --git a/spec/arel/engines/sql/unit/relations/where_spec.rb b/spec/arel/engines/sql/unit/relations/where_spec.rb new file mode 100644 index 0000000000..5870b6b793 --- /dev/null +++ b/spec/arel/engines/sql/unit/relations/where_spec.rb @@ -0,0 +1,64 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Where do + before do + @relation = Table.new(:users) + @predicate = @relation[:id].eq(1) + end + + describe '#initialize' do + it "manufactures nested where relations if multiple predicates are provided" do + another_predicate = @relation[:name].lt(2) + Where.new(@relation, @predicate, another_predicate). \ + should == Where.new(Where.new(@relation, another_predicate), @predicate) + end + end + + describe '#to_sql' do + describe 'when given a predicate' do + it "manufactures sql with where clause conditions" do + 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 + 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 + end +end diff --git a/spec/arel/engines/sql/unit/session/session_spec.rb b/spec/arel/engines/sql/unit/session/session_spec.rb new file mode 100644 index 0000000000..c489984a61 --- /dev/null +++ b/spec/arel/engines/sql/unit/session/session_spec.rb @@ -0,0 +1,84 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Session do + before do + @relation = Table.new(:users) + @session = Session.new + end + + describe '::start' do + describe '::instance' do + it "it is a singleton within the started session" do + Session.start do + Session.new.should == Session.new + end + end + + it "is a singleton across nested sessions" do + Session.start do + outside = Session.new + Session.start do + Session.new.should == outside + end + end + end + + it "manufactures new sessions outside of the started session" do + Session.new.should_not == Session.new + end + end + end + + describe Session::CRUD do + before do + @insert = Insert.new(@relation, @relation[:name] => 'nick') + @update = Update.new(@relation, @relation[:name] => 'nick') + @delete = Deletion.new(@relation) + @read = @relation + end + + describe '#create' do + it "executes an insertion on the connection" do + mock(@insert).call + @session.create(@insert) + end + end + + describe '#read' do + it "executes an selection on the connection" do + mock(@read).call + @session.read(@read) + end + + it "is memoized" do + mock(@read).call.once + @session.read(@read) + @session.read(@read) + end + end + + describe '#update' do + it "executes an update on the connection" do + mock(@update).call + @session.update(@update) + end + end + + describe '#delete' do + it "executes a delete on the connection" do + mock(@delete).call + @session.delete(@delete) + end + end + end + + describe 'Transactions' do + describe '#begin' do + end + + describe '#end' do + end + end + end +end diff --git a/spec/arel/integration/joins/with_adjacency_spec.rb b/spec/arel/integration/joins/with_adjacency_spec.rb deleted file mode 100644 index ffd6498749..0000000000 --- a/spec/arel/integration/joins/with_adjacency_spec.rb +++ /dev/null @@ -1,209 +0,0 @@ -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/integration/joins/with_aggregations_spec.rb b/spec/arel/integration/joins/with_aggregations_spec.rb deleted file mode 100644 index 4aba005d51..0000000000 --- a/spec/arel/integration/joins/with_aggregations_spec.rb +++ /dev/null @@ -1,167 +0,0 @@ -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/integration/joins/with_compounds_spec.rb b/spec/arel/integration/joins/with_compounds_spec.rb deleted file mode 100644 index 41f04349b8..0000000000 --- a/spec/arel/integration/joins/with_compounds_spec.rb +++ /dev/null @@ -1,107 +0,0 @@ -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 diff --git a/spec/arel/unit/predicates/binary_spec.rb b/spec/arel/unit/predicates/binary_spec.rb deleted file mode 100644 index 2d0c67e006..0000000000 --- a/spec/arel/unit/predicates/binary_spec.rb +++ /dev/null @@ -1,125 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Binary do - before do - @relation = Table.new(:users) - @attribute1 = @relation[:id] - @attribute2 = @relation[:name] - class ConcreteBinary < Binary - def predicate_sql - "<=>" - end - end - end - - describe "with compound predicates" do - before do - @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 - 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 - - describe And do - describe "#to_sql" do - it "manufactures sql with an AND operation" do - 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 - 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 - 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 - 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). \ - should == ConcreteBinary.new(@attribute1.find_correlate_in(@another_relation), "asdf".find_correlate_in(@another_relation)) - end - end - end - end -end \ No newline at end of file diff --git a/spec/arel/unit/predicates/equality_spec.rb b/spec/arel/unit/predicates/equality_spec.rb deleted file mode 100644 index b595cdd247..0000000000 --- a/spec/arel/unit/predicates/equality_spec.rb +++ /dev/null @@ -1,61 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Equality do - before do - @relation1 = Table.new(:users) - @relation2 = Table.new(:photos) - @attribute1 = @relation1[:id] - @attribute2 = @relation2[:user_id] - end - - 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 - 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 - 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 - end -end \ No newline at end of file diff --git a/spec/arel/unit/predicates/in_spec.rb b/spec/arel/unit/predicates/in_spec.rb deleted file mode 100644 index 9107da9d4b..0000000000 --- a/spec/arel/unit/predicates/in_spec.rb +++ /dev/null @@ -1,86 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe In do - before do - @relation = Table.new(:users) - @attribute = @relation[:id] - end - - 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 - 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 - 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 - 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 - 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 - end -end \ No newline at end of file diff --git a/spec/arel/unit/primitives/attribute_spec.rb b/spec/arel/unit/primitives/attribute_spec.rb deleted file mode 100644 index e512b40ebf..0000000000 --- a/spec/arel/unit/primitives/attribute_spec.rb +++ /dev/null @@ -1,171 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Attribute do - before do - @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]) \ - .should == (@aliased_relation[:id] / @relation[:id]) - (@aliased_relation[:id] / @relation[:id]) \ - .should < (@aliased_relation[:id] / @aliased_relation[:id]) - end - end - end - end - - describe '#to_sql' do - describe 'for a simple attribute' do - it "manufactures sql with an alias" do - 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) - end - - describe '#count' do - it "manufactures a count Expression" do - @attribute.count.should == Count.new(@attribute) - end - end - - describe '#sum' do - it "manufactures a sum Expression" do - @attribute.sum.should == Sum.new(@attribute) - end - end - - describe '#maximum' do - it "manufactures a maximum Expression" do - @attribute.maximum.should == Maximum.new(@attribute) - end - end - - describe '#minimum' do - it "manufactures a minimum Expression" do - @attribute.minimum.should == Minimum.new(@attribute) - end - end - - describe '#average' do - it "manufactures an average Expression" do - @attribute.average.should == Average.new(@attribute) - 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 deleted file mode 100644 index 92f300c4ee..0000000000 --- a/spec/arel/unit/primitives/expression_spec.rb +++ /dev/null @@ -1,53 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Expression do - before do - @relation = Table.new(:users) - @attribute = @relation[:id] - end - - describe Expression::Transformations do - before do - @expression = Count.new(@attribute) - end - - describe '#bind' do - it "manufactures an attribute with a rebound relation and self as the ancestor" do - derived_relation = @relation.where(@relation[:id].eq(1)) - @expression.bind(derived_relation).should == Count.new(@attribute.bind(derived_relation), nil, @expression) - end - - it "returns self if the substituting to the same relation" do - @expression.bind(@relation).should == @expression - end - end - - describe '#as' do - it "manufactures an aliased expression" do - @expression.as(:alias).should == Expression.new(@attribute, :alias, @expression) - end - end - - describe '#to_attribute' do - it "manufactures an attribute with the expression as an ancestor" do - @expression.to_attribute.should == Attribute.new(@expression.relation, @expression.alias, :ancestor => @expression) - end - end - end - - describe '#to_sql' do - it "manufactures sql with the expression and alias" do - sql = Count.new(@attribute, :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 -end diff --git a/spec/arel/unit/primitives/value_spec.rb b/spec/arel/unit/primitives/value_spec.rb deleted file mode 100644 index ba9a80bb49..0000000000 --- a/spec/arel/unit/primitives/value_spec.rb +++ /dev/null @@ -1,28 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Value do - before do - @relation = Table.new(:users) - end - - describe '#to_sql' do - it "appropriately quotes the value" do - Value.new(1, @relation).to_sql.should be_like('1') - Value.new('asdf', @relation).to_sql.should be_like("'asdf'") - end - end - - describe '#format' do - it "returns the sql of the provided object" do - Value.new(1, @relation).format(@relation[:id]).should == @relation[:id].to_sql - end - end - - describe '#bind' do - it "manufactures a new value whose relation is the provided relation" do - Value.new(1, @relation).bind(another_relation = Table.new(:photos)).should == Value.new(1, another_relation) - end - end - end -end diff --git a/spec/arel/unit/relations/alias_spec.rb b/spec/arel/unit/relations/alias_spec.rb deleted file mode 100644 index 63c15cfeff..0000000000 --- a/spec/arel/unit/relations/alias_spec.rb +++ /dev/null @@ -1,50 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Alias do - before do - @relation = Table.new(:users) - end - - describe '==' do - it "obtains if the objects are the same" do - Alias.new(@relation).should_not == Alias.new(@relation) - (aliaz = Alias.new(@relation)).should == aliaz - end - end - - describe '#to_sql' do - describe 'when there is no ambiguity' do - it 'does not alias table names anywhere a table name can appear' do - sql = @relation \ - .where(@relation[:id].eq(1)) \ - .order(@relation[:id]) \ - .project(@relation[:id]) \ - .group(@relation[:id]) \ - .alias \ - .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` ASC - }) - 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" ASC - }) - end - end - end - end - end -end diff --git a/spec/arel/unit/relations/array_spec.rb b/spec/arel/unit/relations/array_spec.rb deleted file mode 100644 index d1c65c60a9..0000000000 --- a/spec/arel/unit/relations/array_spec.rb +++ /dev/null @@ -1,87 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Array do - before do - @relation = Array.new([ - [1, 'duck' ], - [2, 'duck' ], - [3, 'goose'] - ], [:id, :name]) - end - - describe '#attributes' do - it 'manufactures attributes corresponding to the names given on construction' do - @relation.attributes.should == [ - Attribute.new(@relation, :id), - Attribute.new(@relation, :name) - ] - end - end - - describe '#call' do - it "manufactures an array of hashes of attributes to values" do - @relation.call.should == [ - { @relation[:id] => 1, @relation[:name] => 'duck' }, - { @relation[:id] => 2, @relation[:name] => 'duck' }, - { @relation[:id] => 3, @relation[:name] => 'goose' } - ] - end - - describe 'where' do - it 'filters the relation with the provided predicate' do - @relation.where(@relation[:id].lt(3)).call.should == [ - { @relation[:id] => 1, @relation[:name] => 'duck' }, - { @relation[:id] => 2, @relation[:name] => 'duck' } - ] - end - end - - describe 'group' do - it 'sorts the relation with the provided ordering' do - end - end - - describe 'order' do - it 'sorts the relation with the provided ordering' do - @relation.order(@relation[:id].desc).call.should == [ - { @relation[:id] => 3, @relation[:name] => 'goose' }, - { @relation[:id] => 2, @relation[:name] => 'duck' }, - { @relation[:id] => 1, @relation[:name] => 'duck' } - ] - end - end - - describe 'project' do - it 'projects' do - @relation.project(@relation[:id]).call.should == [ - { @relation[:id] => 1 }, - { @relation[:id] => 2 }, - { @relation[:id] => 3 } - ] - end - end - - describe 'skip' do - it 'slices' do - @relation.skip(1).call.should == [ - { @relation[:id] => 2, @relation[:name] => 'duck' }, - { @relation[:id] => 3, @relation[:name] => 'goose' } - ] - end - end - - describe 'take' do - it 'dices' do - @relation.take(2).call.should == [ - { @relation[:id] => 1, @relation[:name] => 'duck' }, - { @relation[:id] => 2, @relation[:name] => 'duck' } - ] - end - end - - describe 'join' do - end - end - end -end \ No newline at end of file diff --git a/spec/arel/unit/relations/delete_spec.rb b/spec/arel/unit/relations/delete_spec.rb deleted file mode 100644 index 23aca563f7..0000000000 --- a/spec/arel/unit/relations/delete_spec.rb +++ /dev/null @@ -1,63 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Deletion do - before do - @relation = Table.new(:users) - end - - describe '#to_sql' do - it 'manufactures sql deleting a table relation' do - 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 - 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 - 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 - end -end diff --git a/spec/arel/unit/relations/group_spec.rb b/spec/arel/unit/relations/group_spec.rb deleted file mode 100644 index 658c0ad406..0000000000 --- a/spec/arel/unit/relations/group_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Group do - before do - @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 - 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 - 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 - end -end \ No newline at end of file diff --git a/spec/arel/unit/relations/insert_spec.rb b/spec/arel/unit/relations/insert_spec.rb deleted file mode 100644 index 5ab3ef1299..0000000000 --- a/spec/arel/unit/relations/insert_spec.rb +++ /dev/null @@ -1,91 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Insert do - before do - @relation = Table.new(:users) - end - - describe '#to_sql' do - it 'manufactures sql inserting data when given multiple rows' do - 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') - ") - end - end - - it 'manufactures sql inserting data when given multiple values' do - @insertion = Insert.new(@relation, @relation[:id] => "1", @relation[:name] => "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 - before do - @insertion = Insert.new(@relation, @relation[:name] => "nick") - end - - it 'manufactures sql inserting data' do - 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 - - describe 'when given values whose types differ from from the types of the attributes' do - before do - @insertion = Insert.new(@relation, @relation[:id] => '1-asdf') - end - - it 'manufactures sql inserting data' do - 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 - end -end diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb deleted file mode 100644 index 0e3e6ef16b..0000000000 --- a/spec/arel/unit/relations/join_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Join do - before do - @relation1 = Table.new(:users) - @relation2 = Table.new(:photos) - @predicate = @relation1[:id].eq(@relation2[:user_id]) - end - - describe 'hashing' do - it 'implements hash equality' do - InnerJoin.new(@relation1, @relation2, @predicate) \ - .should hash_the_same_as(InnerJoin.new(@relation1, @relation2, @predicate)) - end - end - - describe '#engine' do - it "delegates to a relation's engine" do - InnerJoin.new(@relation1, @relation2, @predicate).engine.should == @relation1.engine - end - end - - describe '#attributes' do - it 'combines the attributes of the two relations' do - join = InnerJoin.new(@relation1, @relation2, @predicate) - join.attributes.should == - (@relation1.attributes + @relation2.attributes).collect { |a| a.bind(join) } - end - end - - describe '#to_sql' do - describe 'when joining with another relation' do - it 'manufactures sql joining the two tables on the predicate' do - sql = InnerJoin.new(@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 - sql = StringJoin.new(@relation1, "INNER JOIN asdf ON fdsa").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 - end -end \ No newline at end of file diff --git a/spec/arel/unit/relations/order_spec.rb b/spec/arel/unit/relations/order_spec.rb deleted file mode 100644 index cb0f1de84c..0000000000 --- a/spec/arel/unit/relations/order_spec.rb +++ /dev/null @@ -1,113 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Order do - before do - @relation = Table.new(:users) - @attribute = @relation[:id] - end - - describe '#to_sql' do - describe "when given an attribute" do - it "manufactures sql with an order clause populated by the attribute" do - 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` ASC - }) - end - - adapter_is_not :mysql do - sql.should be_like(%Q{ - SELECT "users"."id", "users"."name" - FROM "users" - ORDER BY "users"."id" ASC - }) - 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 - 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` ASC, `users`.`name` ASC - }) - end - - adapter_is_not :mysql do - sql.should be_like(%Q{ - SELECT "users"."id", "users"."name" - FROM "users" - ORDER BY "users"."id" ASC, "users"."name" ASC - }) - end - end - end - - describe "when given a string" do - before do - @string = "asdf" - end - - it "passes the string through to the order clause" do - 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 - 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` ASC, `users`.`id` ASC - }) - end - - adapter_is_not :mysql do - sql.should be_like(%Q{ - SELECT "users"."id", "users"."name" - FROM "users" - ORDER BY "users"."name" ASC, "users"."id" ASC - }) - end - end - end - end - end -end diff --git a/spec/arel/unit/relations/project_spec.rb b/spec/arel/unit/relations/project_spec.rb deleted file mode 100644 index d2d1fb3873..0000000000 --- a/spec/arel/unit/relations/project_spec.rb +++ /dev/null @@ -1,134 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Project do - before do - @relation = Table.new(:users) - @attribute = @relation[:id] - end - - describe '#attributes' do - before do - @projection = Project.new(@relation, @attribute) - end - - it "manufactures attributes associated with the projection relation" do - @projection.attributes.should == [@attribute].collect { |a| a.bind(@projection) } - end - end - - describe '#to_sql' do - describe 'when given an attribute' do - it "manufactures sql with a limited select clause" do - 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 - - describe 'when given a relation' do - before do - @scalar_relation = Project.new(@relation, @relation[:name]) - end - - it "manufactures sql with scalar selects" do - 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 - 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 - 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 - 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 - - describe '#externalizable?' do - describe 'when the projections are attributes' do - it 'returns false' do - Project.new(@relation, @attribute).should_not be_externalizable - end - end - - describe 'when the projections include an aggregation' do - it "obtains" do - Project.new(@relation, @attribute.sum).should be_externalizable - end - end - end - end -end diff --git a/spec/arel/unit/relations/relation_spec.rb b/spec/arel/unit/relations/relation_spec.rb deleted file mode 100644 index 6a61f39966..0000000000 --- a/spec/arel/unit/relations/relation_spec.rb +++ /dev/null @@ -1,169 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Relation do - before do - @relation = Table.new(:users) - @attribute1 = @relation[:id] - @attribute2 = @relation[:name] - end - - describe '[]' do - describe 'when given an', Attribute do - it "return the attribute congruent to the provided attribute" do - @relation[@attribute1].should == @attribute1 - end - end - - describe 'when given a', Symbol, String do - it "returns the attribute with the same name, if it exists" do - @relation[:id].should == @attribute1 - @relation['id'].should == @attribute1 - @relation[:does_not_exist].should be_nil - end - end - end - - describe Relation::Operable do - describe 'joins' do - before do - @predicate = @relation[:id].eq(@relation[:id]) - end - - describe '#join' do - describe 'when given a relation' do - it "manufactures an inner join operation between those two relations" do - @relation.join(@relation).on(@predicate). \ - should == InnerJoin.new(@relation, @relation, @predicate) - end - end - - describe "when given a string" do - it "manufactures a join operation with the string passed through" do - @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) - end - end - - describe "when given something blank" do - it "returns self" do - @relation.join.should == @relation - end - end - end - - describe '#outer_join' do - it "manufactures a left outer join operation between those two relations" do - @relation.outer_join(@relation).on(@predicate). \ - should == OuterJoin.new(@relation, @relation, @predicate) - end - end - end - - describe '#project' do - it "manufactures a projection relation" do - @relation.project(@attribute1, @attribute2). \ - should == Project.new(@relation, @attribute1, @attribute2) - end - - describe "when given blank attributes" do - it "returns self" do - @relation.project.should == @relation - end - end - end - - describe '#alias' do - it "manufactures an alias relation" do - @relation.alias.relation.should == Alias.new(@relation).relation - end - end - - describe '#where' do - before do - @predicate = Equality.new(@attribute1, @attribute2) - end - - it "manufactures a where relation" do - @relation.where(@predicate).should == Where.new(@relation, @predicate) - end - - it "accepts arbitrary strings" do - @relation.where("arbitrary").should == Where.new(@relation, "arbitrary") - end - - describe 'when given a blank predicate' do - it 'returns self' do - @relation.where.should == @relation - end - end - end - - describe '#order' do - it "manufactures an order relation" do - @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2) - end - - describe 'when given a blank ordering' do - it 'returns self' do - @relation.order.should == @relation - end - end - end - - describe '#take' do - it "manufactures a take relation" do - @relation.take(5).should == Take.new(@relation, 5) - end - - describe 'when given a blank number of items' do - it 'returns self' do - @relation.take.should == @relation - end - end - end - - describe '#skip' do - it "manufactures a skip relation" do - @relation.skip(4).should == Skip.new(@relation, 4) - end - - describe 'when given a blank number of items' do - it 'returns self' do - @relation.skip.should == @relation - end - end - end - - describe '#group' do - it 'manufactures a group relation' do - @relation.group(@attribute1, @attribute2).should == Group.new(@relation, @attribute1, @attribute2) - end - - describe 'when given blank groupings' do - it 'returns self' do - @relation.group.should == @relation - end - end - end - - describe Relation::Operable::Writable do - describe '#insert' do - it 'manufactures an insertion relation' do - Session.start do - record = {@relation[:name] => 'carl'} - mock(Session.new).create(Insert.new(@relation, record)) - @relation.insert(record).should == @relation - end - end - end - end - end - - describe Relation::Enumerable do - it "implements enumerable" do - @relation.collect.should == @relation.session.read(@relation) - @relation.first.should == @relation.session.read(@relation).first - end - end - end -end diff --git a/spec/arel/unit/relations/skip_spec.rb b/spec/arel/unit/relations/skip_spec.rb deleted file mode 100644 index 2c8f6ccadb..0000000000 --- a/spec/arel/unit/relations/skip_spec.rb +++ /dev/null @@ -1,32 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Skip do - before do - @relation = Table.new(:users) - @skipped = 4 - end - - describe '#to_sql' do - it "manufactures sql with limit and offset" do - sql = Skip.new(@relation, @skipped).to_sql - - 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 -end \ No newline at end of file diff --git a/spec/arel/unit/relations/table_spec.rb b/spec/arel/unit/relations/table_spec.rb deleted file mode 100644 index 2779c0fe5d..0000000000 --- a/spec/arel/unit/relations/table_spec.rb +++ /dev/null @@ -1,99 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Table do - 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 - @relation[:id].should == Attribute.new(@relation, :id) - @relation[:does_not_exist].should be_nil - end - end - - describe 'when given an', Attribute do - 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 - 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 == [ - Attribute.new(@relation, :id), - 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 } - lambda { @relation.reset }.should change { @relation.attributes } - 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 = Sql::Engine.new - Table.new(:users).engine.should == engine - end - - it "can be specified" do - Table.new(:users, engine = Sql::Engine.new).engine.should == engine - end - end - end -end \ No newline at end of file diff --git a/spec/arel/unit/relations/take_spec.rb b/spec/arel/unit/relations/take_spec.rb deleted file mode 100644 index d6442fc9d1..0000000000 --- a/spec/arel/unit/relations/take_spec.rb +++ /dev/null @@ -1,32 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Take do - before do - @relation = Table.new(:users) - @taken = 4 - end - - describe '#to_sql' do - it "manufactures sql with limit and offset" do - sql = Take.new(@relation, @taken).to_sql - - 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 -end \ No newline at end of file diff --git a/spec/arel/unit/relations/update_spec.rb b/spec/arel/unit/relations/update_spec.rb deleted file mode 100644 index e0d7ddd295..0000000000 --- a/spec/arel/unit/relations/update_spec.rb +++ /dev/null @@ -1,121 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Update do - before do - @relation = Table.new(:users) - end - - describe '#to_sql' do - it "manufactures sql updating attributes when given multiple attributes" do - 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 - 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 - before do - @update = Update.new(@relation, @relation[:name] => "nick") - end - - it 'manufactures sql updating attributes' do - 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 - - describe 'when given values whose types differ from from the types of the attributes' do - before do - @update = Update.new(@relation, @relation[:id] => '1-asdf') - end - - it 'manufactures sql updating attributes' do - 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 - - describe 'when the relation is a where' do - before do - @update = Update.new( - @relation.where(@relation[:id].eq(1)), - @relation[:name] => "nick" - ) - end - - it 'manufactures sql updating a where relation' do - 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 - - end -end diff --git a/spec/arel/unit/relations/where_spec.rb b/spec/arel/unit/relations/where_spec.rb deleted file mode 100644 index 64f97c8135..0000000000 --- a/spec/arel/unit/relations/where_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Where do - before do - @relation = Table.new(:users) - @predicate = @relation[:id].eq(1) - end - - describe '#initialize' do - it "manufactures nested where relations if multiple predicates are provided" do - another_predicate = @relation[:name].lt(2) - Where.new(@relation, @predicate, another_predicate). \ - should == Where.new(Where.new(@relation, another_predicate), @predicate) - end - end - - describe '#to_sql' do - describe 'when given a predicate' do - it "manufactures sql with where clause conditions" do - 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 - 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 - end -end diff --git a/spec/arel/unit/session/session_spec.rb b/spec/arel/unit/session/session_spec.rb deleted file mode 100644 index c30ba6195f..0000000000 --- a/spec/arel/unit/session/session_spec.rb +++ /dev/null @@ -1,84 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Session do - before do - @relation = Table.new(:users) - @session = Session.new - end - - describe '::start' do - describe '::instance' do - it "it is a singleton within the started session" do - Session.start do - Session.new.should == Session.new - end - end - - it "is a singleton across nested sessions" do - Session.start do - outside = Session.new - Session.start do - Session.new.should == outside - end - end - end - - it "manufactures new sessions outside of the started session" do - Session.new.should_not == Session.new - end - end - end - - describe Session::CRUD do - before do - @insert = Insert.new(@relation, @relation[:name] => 'nick') - @update = Update.new(@relation, @relation[:name] => 'nick') - @delete = Deletion.new(@relation) - @read = @relation - end - - describe '#create' do - it "executes an insertion on the connection" do - mock(@insert).call - @session.create(@insert) - end - end - - describe '#read' do - it "executes an selection on the connection" do - mock(@read).call - @session.read(@read) - end - - it "is memoized" do - mock(@read).call.once - @session.read(@read) - @session.read(@read) - end - end - - describe '#update' do - it "executes an update on the connection" do - mock(@update).call - @session.update(@update) - end - end - - describe '#delete' do - it "executes a delete on the connection" do - mock(@delete).call - @session.delete(@delete) - end - end - end - - describe 'Transactions' do - describe '#begin' do - end - - describe '#end' do - end - end - end -end -- cgit v1.2.3 From 4c71e3b2ea4b8da574954cbd8a26d12f2cc640d0 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 15:41:49 -0400 Subject: removed duplicates from sql tests Conflicts: spec/arel/engines/sql/unit/predicates/binary_spec.rb spec/arel/engines/sql/unit/predicates/equality_spec.rb spec/arel/engines/sql/unit/primitives/attribute_spec.rb spec/arel/engines/sql/unit/primitives/expression_spec.rb spec/arel/engines/sql/unit/relations/alias_spec.rb spec/arel/engines/sql/unit/relations/join_spec.rb spec/arel/engines/sql/unit/relations/project_spec.rb spec/arel/engines/sql/unit/relations/relation_spec.rb spec/arel/engines/sql/unit/relations/table_spec.rb spec/arel/engines/sql/unit/relations/where_spec.rb spec/arel/engines/sql/unit/session/session_spec.rb --- .../engines/memory/unit/relations/array_spec.rb | 87 +++++++++++ .../engines/sql/unit/predicates/binary_spec.rb | 20 --- .../engines/sql/unit/predicates/equality_spec.rb | 15 -- .../engines/sql/unit/primitives/attribute_spec.rb | 141 +---------------- .../engines/sql/unit/primitives/expression_spec.rb | 29 ---- .../arel/engines/sql/unit/primitives/value_spec.rb | 6 - spec/arel/engines/sql/unit/relations/alias_spec.rb | 7 - spec/arel/engines/sql/unit/relations/array_spec.rb | 87 ----------- spec/arel/engines/sql/unit/relations/join_spec.rb | 21 --- .../engines/sql/unit/relations/project_spec.rb | 24 --- .../engines/sql/unit/relations/relation_spec.rb | 169 --------------------- spec/arel/engines/sql/unit/relations/table_spec.rb | 30 ---- spec/arel/engines/sql/unit/relations/where_spec.rb | 8 - spec/arel/engines/sql/unit/session/session_spec.rb | 84 ---------- 14 files changed, 88 insertions(+), 640 deletions(-) create mode 100644 spec/arel/engines/memory/unit/relations/array_spec.rb delete mode 100644 spec/arel/engines/sql/unit/relations/array_spec.rb delete mode 100644 spec/arel/engines/sql/unit/relations/relation_spec.rb delete mode 100644 spec/arel/engines/sql/unit/session/session_spec.rb (limited to 'spec') diff --git a/spec/arel/engines/memory/unit/relations/array_spec.rb b/spec/arel/engines/memory/unit/relations/array_spec.rb new file mode 100644 index 0000000000..8d40858c5f --- /dev/null +++ b/spec/arel/engines/memory/unit/relations/array_spec.rb @@ -0,0 +1,87 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Array do + before do + @relation = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + end + + describe '#attributes' do + it 'manufactures attributes corresponding to the names given on construction' do + @relation.attributes.should == [ + Attribute.new(@relation, :id), + Attribute.new(@relation, :name) + ] + end + end + + describe '#call' do + it "manufactures an array of hashes of attributes to values" do + @relation.call.should == [ + { @relation[:id] => 1, @relation[:name] => 'duck' }, + { @relation[:id] => 2, @relation[:name] => 'duck' }, + { @relation[:id] => 3, @relation[:name] => 'goose' } + ] + end + + describe 'where' do + it 'filters the relation with the provided predicate' do + @relation.where(@relation[:id].lt(3)).call.should == [ + { @relation[:id] => 1, @relation[:name] => 'duck' }, + { @relation[:id] => 2, @relation[:name] => 'duck' } + ] + end + end + + describe 'group' do + it 'sorts the relation with the provided ordering' do + end + end + + describe 'order' do + it 'sorts the relation with the provided ordering' do + @relation.order(@relation[:id].desc).call.should == [ + { @relation[:id] => 3, @relation[:name] => 'goose' }, + { @relation[:id] => 2, @relation[:name] => 'duck' }, + { @relation[:id] => 1, @relation[:name] => 'duck' } + ] + end + end + + describe 'project' do + it 'projects' do + @relation.project(@relation[:id]).call.should == [ + { @relation[:id] => 1 }, + { @relation[:id] => 2 }, + { @relation[:id] => 3 } + ] + end + end + + describe 'skip' do + it 'slices' do + @relation.skip(1).call.should == [ + { @relation[:id] => 2, @relation[:name] => 'duck' }, + { @relation[:id] => 3, @relation[:name] => 'goose' } + ] + end + end + + describe 'take' do + it 'dices' do + @relation.take(2).call.should == [ + { @relation[:id] => 1, @relation[:name] => 'duck' }, + { @relation[:id] => 2, @relation[:name] => 'duck' } + ] + end + end + + describe 'join' do + end + end + end +end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/predicates/binary_spec.rb b/spec/arel/engines/sql/unit/predicates/binary_spec.rb index 03574225c2..679147067e 100644 --- a/spec/arel/engines/sql/unit/predicates/binary_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/binary_spec.rb @@ -101,25 +101,5 @@ module Arel 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). \ - should == ConcreteBinary.new(@attribute1.find_correlate_in(@another_relation), "asdf".find_correlate_in(@another_relation)) - end - end - end end end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/predicates/equality_spec.rb b/spec/arel/engines/sql/unit/predicates/equality_spec.rb index 2ab79d7028..e8c8c42675 100644 --- a/spec/arel/engines/sql/unit/predicates/equality_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/equality_spec.rb @@ -9,21 +9,6 @@ module Arel @attribute2 = @relation2[:user_id] end - 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 diff --git a/spec/arel/engines/sql/unit/primitives/attribute_spec.rb b/spec/arel/engines/sql/unit/primitives/attribute_spec.rb index f37cd14370..e71ab949f1 100644 --- a/spec/arel/engines/sql/unit/primitives/attribute_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/attribute_spec.rb @@ -7,67 +7,12 @@ module Arel @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]) \ - .should == (@aliased_relation[:id] / @relation[:id]) - (@aliased_relation[:id] / @relation[:id]) \ - .should < (@aliased_relation[:id] / @aliased_relation[:id]) - end - end - end - end - + describe '#to_sql' do describe 'for a simple attribute' do it "manufactures sql with an alias" do @@ -83,89 +28,5 @@ module Arel 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) - end - - describe '#count' do - it "manufactures a count Expression" do - @attribute.count.should == Count.new(@attribute) - end - end - - describe '#sum' do - it "manufactures a sum Expression" do - @attribute.sum.should == Sum.new(@attribute) - end - end - - describe '#maximum' do - it "manufactures a maximum Expression" do - @attribute.maximum.should == Maximum.new(@attribute) - end - end - - describe '#minimum' do - it "manufactures a minimum Expression" do - @attribute.minimum.should == Minimum.new(@attribute) - end - end - - describe '#average' do - it "manufactures an average Expression" do - @attribute.average.should == Average.new(@attribute) - end - end - end end end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/primitives/expression_spec.rb b/spec/arel/engines/sql/unit/primitives/expression_spec.rb index 0869d9e403..ee7f2c1461 100644 --- a/spec/arel/engines/sql/unit/primitives/expression_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/expression_spec.rb @@ -7,35 +7,6 @@ module Arel @attribute = @relation[:id] end - describe Expression::Transformations do - before do - @expression = Count.new(@attribute) - end - - describe '#bind' do - it "manufactures an attribute with a rebound relation and self as the ancestor" do - derived_relation = @relation.where(@relation[:id].eq(1)) - @expression.bind(derived_relation).should == Count.new(@attribute.bind(derived_relation), nil, @expression) - end - - it "returns self if the substituting to the same relation" do - @expression.bind(@relation).should == @expression - end - end - - describe '#as' do - it "manufactures an aliased expression" do - @expression.as(:alias).should == Expression.new(@attribute, :alias, @expression) - end - end - - describe '#to_attribute' do - it "manufactures an attribute with the expression as an ancestor" do - @expression.to_attribute.should == Attribute.new(@expression.relation, @expression.alias, :ancestor => @expression) - end - end - end - describe '#to_sql' do it "manufactures sql with the expression and alias" do sql = Count.new(@attribute, :alias).to_sql diff --git a/spec/arel/engines/sql/unit/primitives/value_spec.rb b/spec/arel/engines/sql/unit/primitives/value_spec.rb index f76323f32b..da5a163d3b 100644 --- a/spec/arel/engines/sql/unit/primitives/value_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/value_spec.rb @@ -18,11 +18,5 @@ module Arel Value.new(1, @relation).format(@relation[:id]).should == @relation[:id].to_sql end end - - describe '#bind' do - it "manufactures a new value whose relation is the provided relation" do - Value.new(1, @relation).bind(another_relation = Table.new(:photos)).should == Value.new(1, another_relation) - end - end end end diff --git a/spec/arel/engines/sql/unit/relations/alias_spec.rb b/spec/arel/engines/sql/unit/relations/alias_spec.rb index 83b9113f6d..b67a0bbc89 100644 --- a/spec/arel/engines/sql/unit/relations/alias_spec.rb +++ b/spec/arel/engines/sql/unit/relations/alias_spec.rb @@ -6,13 +6,6 @@ module Arel @relation = Table.new(:users) end - describe '==' do - it "obtains if the objects are the same" do - Alias.new(@relation).should_not == Alias.new(@relation) - (aliaz = Alias.new(@relation)).should == aliaz - end - end - describe '#to_sql' do describe 'when there is no ambiguity' do it 'does not alias table names anywhere a table name can appear' do diff --git a/spec/arel/engines/sql/unit/relations/array_spec.rb b/spec/arel/engines/sql/unit/relations/array_spec.rb deleted file mode 100644 index 8d40858c5f..0000000000 --- a/spec/arel/engines/sql/unit/relations/array_spec.rb +++ /dev/null @@ -1,87 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') - -module Arel - describe Array do - before do - @relation = Array.new([ - [1, 'duck' ], - [2, 'duck' ], - [3, 'goose'] - ], [:id, :name]) - end - - describe '#attributes' do - it 'manufactures attributes corresponding to the names given on construction' do - @relation.attributes.should == [ - Attribute.new(@relation, :id), - Attribute.new(@relation, :name) - ] - end - end - - describe '#call' do - it "manufactures an array of hashes of attributes to values" do - @relation.call.should == [ - { @relation[:id] => 1, @relation[:name] => 'duck' }, - { @relation[:id] => 2, @relation[:name] => 'duck' }, - { @relation[:id] => 3, @relation[:name] => 'goose' } - ] - end - - describe 'where' do - it 'filters the relation with the provided predicate' do - @relation.where(@relation[:id].lt(3)).call.should == [ - { @relation[:id] => 1, @relation[:name] => 'duck' }, - { @relation[:id] => 2, @relation[:name] => 'duck' } - ] - end - end - - describe 'group' do - it 'sorts the relation with the provided ordering' do - end - end - - describe 'order' do - it 'sorts the relation with the provided ordering' do - @relation.order(@relation[:id].desc).call.should == [ - { @relation[:id] => 3, @relation[:name] => 'goose' }, - { @relation[:id] => 2, @relation[:name] => 'duck' }, - { @relation[:id] => 1, @relation[:name] => 'duck' } - ] - end - end - - describe 'project' do - it 'projects' do - @relation.project(@relation[:id]).call.should == [ - { @relation[:id] => 1 }, - { @relation[:id] => 2 }, - { @relation[:id] => 3 } - ] - end - end - - describe 'skip' do - it 'slices' do - @relation.skip(1).call.should == [ - { @relation[:id] => 2, @relation[:name] => 'duck' }, - { @relation[:id] => 3, @relation[:name] => 'goose' } - ] - end - end - - describe 'take' do - it 'dices' do - @relation.take(2).call.should == [ - { @relation[:id] => 1, @relation[:name] => 'duck' }, - { @relation[:id] => 2, @relation[:name] => 'duck' } - ] - end - end - - describe 'join' do - end - end - end -end \ No newline at end of file diff --git a/spec/arel/engines/sql/unit/relations/join_spec.rb b/spec/arel/engines/sql/unit/relations/join_spec.rb index ea17f8106f..1f43101133 100644 --- a/spec/arel/engines/sql/unit/relations/join_spec.rb +++ b/spec/arel/engines/sql/unit/relations/join_spec.rb @@ -8,27 +8,6 @@ module Arel @predicate = @relation1[:id].eq(@relation2[:user_id]) end - describe 'hashing' do - it 'implements hash equality' do - InnerJoin.new(@relation1, @relation2, @predicate) \ - .should hash_the_same_as(InnerJoin.new(@relation1, @relation2, @predicate)) - end - end - - describe '#engine' do - it "delegates to a relation's engine" do - InnerJoin.new(@relation1, @relation2, @predicate).engine.should == @relation1.engine - end - end - - describe '#attributes' do - it 'combines the attributes of the two relations' do - join = InnerJoin.new(@relation1, @relation2, @predicate) - join.attributes.should == - (@relation1.attributes + @relation2.attributes).collect { |a| a.bind(join) } - end - end - describe '#to_sql' do describe 'when joining with another relation' do it 'manufactures sql joining the two tables on the predicate' do diff --git a/spec/arel/engines/sql/unit/relations/project_spec.rb b/spec/arel/engines/sql/unit/relations/project_spec.rb index 9d6b9fab06..5e29124cfa 100644 --- a/spec/arel/engines/sql/unit/relations/project_spec.rb +++ b/spec/arel/engines/sql/unit/relations/project_spec.rb @@ -7,16 +7,6 @@ module Arel @attribute = @relation[:id] end - describe '#attributes' do - before do - @projection = Project.new(@relation, @attribute) - end - - it "manufactures attributes associated with the projection relation" do - @projection.attributes.should == [@attribute].collect { |a| a.bind(@projection) } - end - end - describe '#to_sql' do describe 'when given an attribute' do it "manufactures sql with a limited select clause" do @@ -116,19 +106,5 @@ module Arel end end end - - describe '#externalizable?' do - describe 'when the projections are attributes' do - it 'returns false' do - Project.new(@relation, @attribute).should_not be_externalizable - end - end - - describe 'when the projections include an aggregation' do - it "obtains" do - Project.new(@relation, @attribute.sum).should be_externalizable - end - end - end end end diff --git a/spec/arel/engines/sql/unit/relations/relation_spec.rb b/spec/arel/engines/sql/unit/relations/relation_spec.rb deleted file mode 100644 index 9a5cfbff41..0000000000 --- a/spec/arel/engines/sql/unit/relations/relation_spec.rb +++ /dev/null @@ -1,169 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') - -module Arel - describe Relation do - before do - @relation = Table.new(:users) - @attribute1 = @relation[:id] - @attribute2 = @relation[:name] - end - - describe '[]' do - describe 'when given an', Attribute do - it "return the attribute congruent to the provided attribute" do - @relation[@attribute1].should == @attribute1 - end - end - - describe 'when given a', Symbol, String do - it "returns the attribute with the same name, if it exists" do - @relation[:id].should == @attribute1 - @relation['id'].should == @attribute1 - @relation[:does_not_exist].should be_nil - end - end - end - - describe Relation::Operable do - describe 'joins' do - before do - @predicate = @relation[:id].eq(@relation[:id]) - end - - describe '#join' do - describe 'when given a relation' do - it "manufactures an inner join operation between those two relations" do - @relation.join(@relation).on(@predicate). \ - should == InnerJoin.new(@relation, @relation, @predicate) - end - end - - describe "when given a string" do - it "manufactures a join operation with the string passed through" do - @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) - end - end - - describe "when given something blank" do - it "returns self" do - @relation.join.should == @relation - end - end - end - - describe '#outer_join' do - it "manufactures a left outer join operation between those two relations" do - @relation.outer_join(@relation).on(@predicate). \ - should == OuterJoin.new(@relation, @relation, @predicate) - end - end - end - - describe '#project' do - it "manufactures a projection relation" do - @relation.project(@attribute1, @attribute2). \ - should == Project.new(@relation, @attribute1, @attribute2) - end - - describe "when given blank attributes" do - it "returns self" do - @relation.project.should == @relation - end - end - end - - describe '#alias' do - it "manufactures an alias relation" do - @relation.alias.relation.should == Alias.new(@relation).relation - end - end - - describe '#where' do - before do - @predicate = Equality.new(@attribute1, @attribute2) - end - - it "manufactures a where relation" do - @relation.where(@predicate).should == Where.new(@relation, @predicate) - end - - it "accepts arbitrary strings" do - @relation.where("arbitrary").should == Where.new(@relation, "arbitrary") - end - - describe 'when given a blank predicate' do - it 'returns self' do - @relation.where.should == @relation - end - end - end - - describe '#order' do - it "manufactures an order relation" do - @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2) - end - - describe 'when given a blank ordering' do - it 'returns self' do - @relation.order.should == @relation - end - end - end - - describe '#take' do - it "manufactures a take relation" do - @relation.take(5).should == Take.new(@relation, 5) - end - - describe 'when given a blank number of items' do - it 'returns self' do - @relation.take.should == @relation - end - end - end - - describe '#skip' do - it "manufactures a skip relation" do - @relation.skip(4).should == Skip.new(@relation, 4) - end - - describe 'when given a blank number of items' do - it 'returns self' do - @relation.skip.should == @relation - end - end - end - - describe '#group' do - it 'manufactures a group relation' do - @relation.group(@attribute1, @attribute2).should == Group.new(@relation, @attribute1, @attribute2) - end - - describe 'when given blank groupings' do - it 'returns self' do - @relation.group.should == @relation - end - end - end - - describe Relation::Operable::Writable do - describe '#insert' do - it 'manufactures an insertion relation' do - Session.start do - record = {@relation[:name] => 'carl'} - mock(Session.new).create(Insert.new(@relation, record)) - @relation.insert(record).should == @relation - end - end - end - end - end - - describe Relation::Enumerable do - it "implements enumerable" do - @relation.collect.should == @relation.session.read(@relation) - @relation.first.should == @relation.session.read(@relation).first - end - end - end -end diff --git a/spec/arel/engines/sql/unit/relations/table_spec.rb b/spec/arel/engines/sql/unit/relations/table_spec.rb index 6de49a9157..3f5a5ac248 100644 --- a/spec/arel/engines/sql/unit/relations/table_spec.rb +++ b/spec/arel/engines/sql/unit/relations/table_spec.rb @@ -6,36 +6,6 @@ module Arel @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 - @relation[:id].should == Attribute.new(@relation, :id) - @relation[:does_not_exist].should be_nil - end - end - - describe 'when given an', Attribute do - 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 sql = @relation.to_sql diff --git a/spec/arel/engines/sql/unit/relations/where_spec.rb b/spec/arel/engines/sql/unit/relations/where_spec.rb index 5870b6b793..4f0cce1e01 100644 --- a/spec/arel/engines/sql/unit/relations/where_spec.rb +++ b/spec/arel/engines/sql/unit/relations/where_spec.rb @@ -7,14 +7,6 @@ module Arel @predicate = @relation[:id].eq(1) end - describe '#initialize' do - it "manufactures nested where relations if multiple predicates are provided" do - another_predicate = @relation[:name].lt(2) - Where.new(@relation, @predicate, another_predicate). \ - should == Where.new(Where.new(@relation, another_predicate), @predicate) - end - end - describe '#to_sql' do describe 'when given a predicate' do it "manufactures sql with where clause conditions" do diff --git a/spec/arel/engines/sql/unit/session/session_spec.rb b/spec/arel/engines/sql/unit/session/session_spec.rb deleted file mode 100644 index c489984a61..0000000000 --- a/spec/arel/engines/sql/unit/session/session_spec.rb +++ /dev/null @@ -1,84 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') - -module Arel - describe Session do - before do - @relation = Table.new(:users) - @session = Session.new - end - - describe '::start' do - describe '::instance' do - it "it is a singleton within the started session" do - Session.start do - Session.new.should == Session.new - end - end - - it "is a singleton across nested sessions" do - Session.start do - outside = Session.new - Session.start do - Session.new.should == outside - end - end - end - - it "manufactures new sessions outside of the started session" do - Session.new.should_not == Session.new - end - end - end - - describe Session::CRUD do - before do - @insert = Insert.new(@relation, @relation[:name] => 'nick') - @update = Update.new(@relation, @relation[:name] => 'nick') - @delete = Deletion.new(@relation) - @read = @relation - end - - describe '#create' do - it "executes an insertion on the connection" do - mock(@insert).call - @session.create(@insert) - end - end - - describe '#read' do - it "executes an selection on the connection" do - mock(@read).call - @session.read(@read) - end - - it "is memoized" do - mock(@read).call.once - @session.read(@read) - @session.read(@read) - end - end - - describe '#update' do - it "executes an update on the connection" do - mock(@update).call - @session.update(@update) - end - end - - describe '#delete' do - it "executes a delete on the connection" do - mock(@delete).call - @session.delete(@delete) - end - end - end - - describe 'Transactions' do - describe '#begin' do - end - - describe '#end' do - end - end - end -end -- cgit v1.2.3 From 8339f024c7663133a78c4d0a8824b5b6fafaf239 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 15:42:42 -0400 Subject: recursive memory operations now possible Conflicts: lib/arel/algebra/relations/relation.rb --- .../engines/memory/unit/relations/array_spec.rb | 83 +++++++++++++++------- 1 file changed, 57 insertions(+), 26 deletions(-) (limited to 'spec') diff --git a/spec/arel/engines/memory/unit/relations/array_spec.rb b/spec/arel/engines/memory/unit/relations/array_spec.rb index 8d40858c5f..22cddf7156 100644 --- a/spec/arel/engines/memory/unit/relations/array_spec.rb +++ b/spec/arel/engines/memory/unit/relations/array_spec.rb @@ -22,61 +22,92 @@ module Arel describe '#call' do it "manufactures an array of hashes of attributes to values" do @relation.call.should == [ - { @relation[:id] => 1, @relation[:name] => 'duck' }, - { @relation[:id] => 2, @relation[:name] => 'duck' }, - { @relation[:id] => 3, @relation[:name] => 'goose' } + Row.new(@relation, [1, 'duck']), + Row.new(@relation, [2, 'duck']), + Row.new(@relation, [3, 'goose']) ] end describe 'where' do + xit 'filters the relation with the provided predicate' do + @relation \ + .where(@relation[:id].lt(3)) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [1, 'duck']), + Row.new(relation, [2, 'duck']), + ] + end + end + it 'filters the relation with the provided predicate' do - @relation.where(@relation[:id].lt(3)).call.should == [ - { @relation[:id] => 1, @relation[:name] => 'duck' }, - { @relation[:id] => 2, @relation[:name] => 'duck' } - ] + @relation \ + .where(@relation[:id].gt(1)) \ + .where(@relation[:id].lt(3)) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [2, 'duck']) + ] + end end end describe 'group' do - it 'sorts the relation with the provided ordering' do + xit 'sorts the relation with the provided ordering' do end end describe 'order' do it 'sorts the relation with the provided ordering' do - @relation.order(@relation[:id].desc).call.should == [ - { @relation[:id] => 3, @relation[:name] => 'goose' }, - { @relation[:id] => 2, @relation[:name] => 'duck' }, - { @relation[:id] => 1, @relation[:name] => 'duck' } - ] + @relation \ + .order(@relation[:id].desc) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [3, 'goose']), + Row.new(relation, [2, 'duck']), + Row.new(relation, [1, 'duck']) + ] + end end end describe 'project' do it 'projects' do - @relation.project(@relation[:id]).call.should == [ - { @relation[:id] => 1 }, - { @relation[:id] => 2 }, - { @relation[:id] => 3 } - ] + @relation \ + .project(@relation[:id]) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [1]), + Row.new(relation, [2]), + Row.new(relation, [3]) + ] + end end end describe 'skip' do it 'slices' do - @relation.skip(1).call.should == [ - { @relation[:id] => 2, @relation[:name] => 'duck' }, - { @relation[:id] => 3, @relation[:name] => 'goose' } - ] + @relation \ + .skip(1) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [2, 'duck']), + Row.new(relation, [3, 'goose']), + ] + end end end describe 'take' do it 'dices' do - @relation.take(2).call.should == [ - { @relation[:id] => 1, @relation[:name] => 'duck' }, - { @relation[:id] => 2, @relation[:name] => 'duck' } - ] + @relation \ + .take(2) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [1, 'duck']), + Row.new(relation, [2, 'duck']), + ] + end end end -- cgit v1.2.3 From b7f58db57a535806e0cfc3057fbab80ca43b1a53 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 15:44:03 -0400 Subject: better test ordering Conflicts: doc/TODO --- .../engines/memory/unit/relations/array_spec.rb | 86 ---------------------- .../engines/memory/unit/relations/order_spec.rb | 27 +++++++ .../engines/memory/unit/relations/project_spec.rb | 27 +++++++ .../engines/memory/unit/relations/skip_spec.rb | 26 +++++++ .../engines/memory/unit/relations/take_spec.rb | 26 +++++++ .../engines/memory/unit/relations/where_spec.rb | 39 ++++++++++ 6 files changed, 145 insertions(+), 86 deletions(-) create mode 100644 spec/arel/engines/memory/unit/relations/order_spec.rb create mode 100644 spec/arel/engines/memory/unit/relations/project_spec.rb create mode 100644 spec/arel/engines/memory/unit/relations/skip_spec.rb create mode 100644 spec/arel/engines/memory/unit/relations/take_spec.rb create mode 100644 spec/arel/engines/memory/unit/relations/where_spec.rb (limited to 'spec') diff --git a/spec/arel/engines/memory/unit/relations/array_spec.rb b/spec/arel/engines/memory/unit/relations/array_spec.rb index 22cddf7156..4fe24c77fa 100644 --- a/spec/arel/engines/memory/unit/relations/array_spec.rb +++ b/spec/arel/engines/memory/unit/relations/array_spec.rb @@ -27,92 +27,6 @@ module Arel Row.new(@relation, [3, 'goose']) ] end - - describe 'where' do - xit 'filters the relation with the provided predicate' do - @relation \ - .where(@relation[:id].lt(3)) \ - .let do |relation| - relation.call.should == [ - Row.new(relation, [1, 'duck']), - Row.new(relation, [2, 'duck']), - ] - end - end - - it 'filters the relation with the provided predicate' do - @relation \ - .where(@relation[:id].gt(1)) \ - .where(@relation[:id].lt(3)) \ - .let do |relation| - relation.call.should == [ - Row.new(relation, [2, 'duck']) - ] - end - end - end - - describe 'group' do - xit 'sorts the relation with the provided ordering' do - end - end - - describe 'order' do - it 'sorts the relation with the provided ordering' do - @relation \ - .order(@relation[:id].desc) \ - .let do |relation| - relation.call.should == [ - Row.new(relation, [3, 'goose']), - Row.new(relation, [2, 'duck']), - Row.new(relation, [1, 'duck']) - ] - end - end - end - - describe 'project' do - it 'projects' do - @relation \ - .project(@relation[:id]) \ - .let do |relation| - relation.call.should == [ - Row.new(relation, [1]), - Row.new(relation, [2]), - Row.new(relation, [3]) - ] - end - end - end - - describe 'skip' do - it 'slices' do - @relation \ - .skip(1) \ - .let do |relation| - relation.call.should == [ - Row.new(relation, [2, 'duck']), - Row.new(relation, [3, 'goose']), - ] - end - end - end - - describe 'take' do - it 'dices' do - @relation \ - .take(2) \ - .let do |relation| - relation.call.should == [ - Row.new(relation, [1, 'duck']), - Row.new(relation, [2, 'duck']), - ] - end - end - end - - describe 'join' do - end end end end \ No newline at end of file diff --git a/spec/arel/engines/memory/unit/relations/order_spec.rb b/spec/arel/engines/memory/unit/relations/order_spec.rb new file mode 100644 index 0000000000..3ecb31068b --- /dev/null +++ b/spec/arel/engines/memory/unit/relations/order_spec.rb @@ -0,0 +1,27 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Order do + before do + @relation = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + end + + describe '#call' do + it 'sorts the relation with the provided ordering' do + @relation \ + .order(@relation[:id].desc) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [3, 'goose']), + Row.new(relation, [2, 'duck' ]), + Row.new(relation, [1, 'duck' ]) + ] + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/engines/memory/unit/relations/project_spec.rb b/spec/arel/engines/memory/unit/relations/project_spec.rb new file mode 100644 index 0000000000..1d1224cfdc --- /dev/null +++ b/spec/arel/engines/memory/unit/relations/project_spec.rb @@ -0,0 +1,27 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Project do + before do + @relation = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + end + + describe '#call' do + it 'retains only the attributes that are provided' do + @relation \ + .project(@relation[:id]) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [1]), + Row.new(relation, [2]), + Row.new(relation, [3]) + ] + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/engines/memory/unit/relations/skip_spec.rb b/spec/arel/engines/memory/unit/relations/skip_spec.rb new file mode 100644 index 0000000000..86db45ef61 --- /dev/null +++ b/spec/arel/engines/memory/unit/relations/skip_spec.rb @@ -0,0 +1,26 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Skip do + before do + @relation = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + end + + describe '#call' do + it 'removes the first n rows' do + @relation \ + .skip(1) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [2, 'duck']), + Row.new(relation, [3, 'goose']), + ] + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/engines/memory/unit/relations/take_spec.rb b/spec/arel/engines/memory/unit/relations/take_spec.rb new file mode 100644 index 0000000000..8b774987e0 --- /dev/null +++ b/spec/arel/engines/memory/unit/relations/take_spec.rb @@ -0,0 +1,26 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Take do + before do + @relation = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + end + + describe '#call' do + it 'removes the rows after the first n' do + @relation \ + .take(2) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [1, 'duck']), + Row.new(relation, [2, 'duck']), + ] + end + end + end + end +end \ No newline at end of file diff --git a/spec/arel/engines/memory/unit/relations/where_spec.rb b/spec/arel/engines/memory/unit/relations/where_spec.rb new file mode 100644 index 0000000000..d75ee5dcbe --- /dev/null +++ b/spec/arel/engines/memory/unit/relations/where_spec.rb @@ -0,0 +1,39 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Where do + before do + @relation = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + end + + describe '#call' do + it 'filters the relation with the provided predicate' do + @relation \ + .where(@relation[:id].lt(3)) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [1, 'duck']), + Row.new(relation, [2, 'duck']), + ] + end + end + + describe 'when filtering a where relation' do + it 'further filters the already-filtered relation with the provided predicate' do + @relation \ + .where(@relation[:id].gt(1)) \ + .where(@relation[:id].lt(3)) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [2, 'duck']) + ] + end + end + end + end + end +end \ No newline at end of file -- cgit v1.2.3 From 2fe585328d6a24df310d3e60059c9c7b05b64bac Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Tue, 27 May 2008 14:19:59 -0700 Subject: performing in memory joins --- .../engines/memory/unit/relations/join_spec.rb | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 spec/arel/engines/memory/unit/relations/join_spec.rb (limited to 'spec') diff --git a/spec/arel/engines/memory/unit/relations/join_spec.rb b/spec/arel/engines/memory/unit/relations/join_spec.rb new file mode 100644 index 0000000000..920cc55d0a --- /dev/null +++ b/spec/arel/engines/memory/unit/relations/join_spec.rb @@ -0,0 +1,32 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Join do + before do + @relation1 = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + @relation2 = @relation1.alias + @relation3 = @relation1.alias + end + + describe InnerJoin do + describe '#call' do + it 'combines the two tables where the predicate obtains' do + @relation1 \ + .join(@relation2) \ + .on(@relation1[:id].eq(@relation2[:id])) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [1, 'duck', 1, 'duck' ]), + Row.new(relation, [2, 'duck', 2, 'duck' ]), + Row.new(relation, [3, 'goose', 3, 'goose']) + ] + end + end + end + end + end +end \ No newline at end of file -- cgit v1.2.3 From 20b28b441b651d0404d64049253898c061a039be Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Tue, 27 May 2008 14:37:11 -0700 Subject: using in memory relations as results from sql relation Conflicts: lib/arel/algebra/primitives/expression.rb lib/arel/algebra/relations/relation.rb --- spec/arel/algebra/unit/primitives/attribute_spec.rb | 12 ++++++++++-- spec/arel/algebra/unit/primitives/expression_spec.rb | 2 +- spec/arel/algebra/unit/relations/relation_spec.rb | 10 +++++----- 3 files changed, 16 insertions(+), 8 deletions(-) (limited to 'spec') diff --git a/spec/arel/algebra/unit/primitives/attribute_spec.rb b/spec/arel/algebra/unit/primitives/attribute_spec.rb index bab9fad3d5..dcac5abf65 100644 --- a/spec/arel/algebra/unit/primitives/attribute_spec.rb +++ b/spec/arel/algebra/unit/primitives/attribute_spec.rb @@ -26,8 +26,16 @@ module Arel end describe '#to_attribute' do - it "returns self" do - @attribute.to_attribute.should == @attribute + describe 'when the given relation is the same as the attributes relation' do + it "returns self" do + @attribute.to_attribute(@relation).should == @attribute + end + end + + describe 'when the given relation differs from the attributes relation' do + it 'binds to the new relation' do + @attribute.to_attribute(new_relation = @relation.alias).should == @attribute.bind(new_relation) + end end end end diff --git a/spec/arel/algebra/unit/primitives/expression_spec.rb b/spec/arel/algebra/unit/primitives/expression_spec.rb index 10bdb56302..dfd2100048 100644 --- a/spec/arel/algebra/unit/primitives/expression_spec.rb +++ b/spec/arel/algebra/unit/primitives/expression_spec.rb @@ -31,7 +31,7 @@ module Arel describe '#to_attribute' do it "manufactures an attribute with the expression as an ancestor" do - @expression.to_attribute.should == Attribute.new(@expression.relation, @expression.alias, :ancestor => @expression) + @expression.to_attribute(@relation).should == Attribute.new(@relation, @expression.alias, :ancestor => @expression) end end end diff --git a/spec/arel/algebra/unit/relations/relation_spec.rb b/spec/arel/algebra/unit/relations/relation_spec.rb index 3286f373f5..9707f2887c 100644 --- a/spec/arel/algebra/unit/relations/relation_spec.rb +++ b/spec/arel/algebra/unit/relations/relation_spec.rb @@ -40,7 +40,7 @@ module Arel describe "when given a string" do it "manufactures a join operation with the string passed through" do - @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) + @relation.join(arbitrary_string = "ASDF").should == StringJoin.new(@relation, arbitrary_string) end end @@ -159,7 +159,7 @@ module Arel describe '#insert' do it 'manufactures an insertion relation' do Session.start do - record = {@relation[:name] => 'carl'} + record = { @relation[:name] => 'carl' } mock(Session.new).create(Insert.new(@relation, record)) @relation.insert(record) end @@ -169,7 +169,7 @@ module Arel describe '#update' do it 'manufactures an update relation' do Session.start do - assignments = {@relation[:name] => Value.new('bob', @relation)} + assignments = { @relation[:name] => Value.new('bob', @relation) } mock(Session.new).update(Update.new(@relation, assignments)) @relation.update(assignments) end @@ -180,8 +180,8 @@ module Arel describe Relation::Enumerable do it "implements enumerable" do - @relation.collect.should == @relation.session.read(@relation) - @relation.first.should == @relation.session.read(@relation).first + @relation.collect.should == @relation.session.read(@relation).collect + @relation.first.should == @relation.session.read(@relation).first end end end -- cgit v1.2.3 From 07833d39c2885a5cddf38eeb860d79353c0f447b Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 15:47:10 -0400 Subject: basic implementation of in memory insertions Conflicts: lib/arel/engines/memory/relations.rb --- .../engines/memory/unit/relations/insert_spec.rb | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 spec/arel/engines/memory/unit/relations/insert_spec.rb (limited to 'spec') diff --git a/spec/arel/engines/memory/unit/relations/insert_spec.rb b/spec/arel/engines/memory/unit/relations/insert_spec.rb new file mode 100644 index 0000000000..4b5e8833a0 --- /dev/null +++ b/spec/arel/engines/memory/unit/relations/insert_spec.rb @@ -0,0 +1,28 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Insert do + before do + @relation = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + end + + describe '#call' do + it "manufactures an array of hashes of attributes to values" do + @relation \ + .insert(@relation[:id] => 4, @relation[:name] => 'guinea fowl') \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [1, 'duck']), + Row.new(relation, [2, 'duck']), + Row.new(relation, [3, 'goose']), + Row.new(relation, [4, 'guinea fowl']) + ] + end + end + end + end +end \ No newline at end of file -- cgit v1.2.3 From 7a51983efc50c8f9092785b1b586f8884dedc01a Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 16:02:10 -0400 Subject: initial implementation of cross-engine join Conflicts: lib/arel/engines/memory/relations/array.rb lib/arel/engines/sql/primitives.rb --- .../memory/integration/joins/cross_engine_spec.rb | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 spec/arel/engines/memory/integration/joins/cross_engine_spec.rb (limited to 'spec') diff --git a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb new file mode 100644 index 0000000000..dd923ee6eb --- /dev/null +++ b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb @@ -0,0 +1,31 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Join do + before do + @users = Array.new([ + [1, 'bryan' ], + [2, 'emilio' ], + [3, 'nick'] + ], [:id, :name]) + @photos = Table.new(:photos) + @photos.delete + @photos \ + .insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6) \ + .insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42) + end + + it 'joins across engines' do + @users \ + .join(@photos) \ + .on(@users[:id].eq(@photos[:user_id])) \ + .project(@users[:name], @photos[:camera_id]) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, ['bryan', '6']), + Row.new(relation, ['emilio', '42']) + ] + end + end + end +end \ No newline at end of file -- cgit v1.2.3 From 44743bed5568b3065e4f9da7972e3ea1d0d9e728 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 16:14:28 -0400 Subject: joining across engines in either direction Conflicts: spec/arel/engines/memory/integration/joins/cross_engine_spec.rb --- .../memory/integration/joins/cross_engine_spec.rb | 39 ++++++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'spec') diff --git a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb index dd923ee6eb..4862300052 100644 --- a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb +++ b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb @@ -14,17 +14,34 @@ module Arel .insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6) \ .insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42) end - - it 'joins across engines' do - @users \ - .join(@photos) \ - .on(@users[:id].eq(@photos[:user_id])) \ - .project(@users[:name], @photos[:camera_id]) \ - .let do |relation| - relation.call.should == [ - Row.new(relation, ['bryan', '6']), - Row.new(relation, ['emilio', '42']) - ] + + describe 'when the in memory relation is on the left' do + it 'joins across engines' do + @users \ + .join(@photos) \ + .on(@users[:id].eq(@photos[:user_id])) \ + .project(@users[:name], @photos[:camera_id]) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, ['bryan', '6']), + Row.new(relation, ['emilio', '42']) + ] + end + end + end + + describe 'when the in memory relation is on the right' do + it 'joins across engines' do + @photos \ + .join(@users) \ + .on(@users[:id].eq(@photos[:user_id])) \ + .project(@users[:name], @photos[:camera_id]) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, ['bryan', '6']), + Row.new(relation, ['emilio', '42']) + ] + end end end end -- cgit v1.2.3 From dc7b51883b1cc8ad7e525b7315fb575ae77a5b3d Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 16:20:40 -0400 Subject: Whitespace --- spec/arel/algebra/unit/predicates/binary_spec.rb | 8 +-- spec/arel/algebra/unit/predicates/equality_spec.rb | 10 ++-- spec/arel/algebra/unit/predicates/in_spec.rb | 2 +- .../arel/algebra/unit/primitives/attribute_spec.rb | 58 +++++++++++----------- .../algebra/unit/primitives/expression_spec.rb | 12 ++--- spec/arel/algebra/unit/primitives/value_spec.rb | 2 +- spec/arel/algebra/unit/relations/alias_spec.rb | 4 +- spec/arel/algebra/unit/relations/delete_spec.rb | 2 +- spec/arel/algebra/unit/relations/group_spec.rb | 2 +- spec/arel/algebra/unit/relations/insert_spec.rb | 2 +- spec/arel/algebra/unit/relations/join_spec.rb | 6 +-- spec/arel/algebra/unit/relations/order_spec.rb | 2 +- spec/arel/algebra/unit/relations/project_spec.rb | 10 ++-- spec/arel/algebra/unit/relations/relation_spec.rb | 36 +++++++------- spec/arel/algebra/unit/relations/skip_spec.rb | 2 +- spec/arel/algebra/unit/relations/table_spec.rb | 10 ++-- spec/arel/algebra/unit/relations/take_spec.rb | 2 +- spec/arel/algebra/unit/relations/update_spec.rb | 2 +- spec/arel/algebra/unit/relations/where_spec.rb | 6 +-- spec/arel/algebra/unit/session/session_spec.rb | 22 ++++---- .../memory/integration/joins/cross_engine_spec.rb | 2 +- .../engines/memory/unit/relations/array_spec.rb | 2 +- .../engines/memory/unit/relations/insert_spec.rb | 4 +- .../engines/memory/unit/relations/join_spec.rb | 4 +- .../engines/memory/unit/relations/order_spec.rb | 4 +- .../engines/memory/unit/relations/project_spec.rb | 4 +- .../engines/memory/unit/relations/skip_spec.rb | 4 +- .../engines/memory/unit/relations/take_spec.rb | 4 +- .../engines/memory/unit/relations/where_spec.rb | 6 +-- .../engines/sql/unit/predicates/binary_spec.rb | 2 +- .../engines/sql/unit/predicates/equality_spec.rb | 2 +- spec/arel/engines/sql/unit/predicates/in_spec.rb | 2 +- .../engines/sql/unit/primitives/attribute_spec.rb | 4 +- spec/arel/engines/sql/unit/relations/group_spec.rb | 2 +- spec/arel/engines/sql/unit/relations/join_spec.rb | 2 +- spec/arel/engines/sql/unit/relations/skip_spec.rb | 2 +- spec/arel/engines/sql/unit/relations/table_spec.rb | 2 +- spec/arel/engines/sql/unit/relations/take_spec.rb | 2 +- spec/arel/unit/predicates/predicates_spec.rb | 2 +- spec/connections/mysql_connection.rb | 2 +- spec/doubles/hash.rb | 8 +-- spec/matchers/be_like.rb | 10 ++-- spec/matchers/disambiguate_attributes.rb | 10 ++-- spec/matchers/hash_the_same_as.rb | 10 ++-- spec/schemas/mysql_schema.rb | 2 +- spec/schemas/sqlite3_schema.rb | 2 +- 46 files changed, 150 insertions(+), 150 deletions(-) (limited to 'spec') diff --git a/spec/arel/algebra/unit/predicates/binary_spec.rb b/spec/arel/algebra/unit/predicates/binary_spec.rb index 9022a543d1..14fd7ab21b 100644 --- a/spec/arel/algebra/unit/predicates/binary_spec.rb +++ b/spec/arel/algebra/unit/predicates/binary_spec.rb @@ -9,19 +9,19 @@ module Arel class ConcreteBinary < Binary 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). \ @@ -30,4 +30,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/predicates/equality_spec.rb b/spec/arel/algebra/unit/predicates/equality_spec.rb index 9a56ed5eaf..af91f8b51b 100644 --- a/spec/arel/algebra/unit/predicates/equality_spec.rb +++ b/spec/arel/algebra/unit/predicates/equality_spec.rb @@ -8,20 +8,20 @@ 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 end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/predicates/in_spec.rb b/spec/arel/algebra/unit/predicates/in_spec.rb index 91c154763c..a8a15ce4e3 100644 --- a/spec/arel/algebra/unit/predicates/in_spec.rb +++ b/spec/arel/algebra/unit/predicates/in_spec.rb @@ -7,4 +7,4 @@ module Arel @attribute = @relation[:id] end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/primitives/attribute_spec.rb b/spec/arel/algebra/unit/primitives/attribute_spec.rb index dcac5abf65..89e338e377 100644 --- a/spec/arel/algebra/unit/primitives/attribute_spec.rb +++ b/spec/arel/algebra/unit/primitives/attribute_spec.rb @@ -6,32 +6,32 @@ 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 describe 'when the given relation is the same as the attributes relation' do it "returns self" do @attribute.to_attribute(@relation).should == @attribute end end - + describe 'when the given relation differs from the attributes relation' do it 'binds to the new relation' do @attribute.to_attribute(new_relation = @relation.alias).should == @attribute.bind(new_relation) @@ -39,32 +39,32 @@ module Arel 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]) \ @@ -75,98 +75,98 @@ module Arel 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 == Count.new(@attribute) end end - + describe '#sum' do it "manufactures a sum Expression" do @attribute.sum.should == Sum.new(@attribute) end end - + describe '#maximum' do it "manufactures a maximum Expression" do @attribute.maximum.should == Maximum.new(@attribute) end end - + describe '#minimum' do it "manufactures a minimum Expression" do @attribute.minimum.should == Minimum.new(@attribute) end end - + describe '#average' do it "manufactures an average Expression" do @attribute.average.should == Average.new(@attribute) end - end + end end - + describe Attribute::Orderings do describe '#asc' do it 'manufactures an ascending ordering' do pending end end - + describe '#desc' do it 'manufactures a descending ordering' do pending @@ -174,4 +174,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/primitives/expression_spec.rb b/spec/arel/algebra/unit/primitives/expression_spec.rb index dfd2100048..82d12d53f9 100644 --- a/spec/arel/algebra/unit/primitives/expression_spec.rb +++ b/spec/arel/algebra/unit/primitives/expression_spec.rb @@ -6,29 +6,29 @@ module Arel @relation = Table.new(:users) @attribute = @relation[:id] end - + describe Expression::Transformations do before do @expression = Count.new(@attribute) end - + describe '#bind' do it "manufactures an attribute with a rebound relation and self as the ancestor" do derived_relation = @relation.where(@relation[:id].eq(1)) @expression.bind(derived_relation).should == Count.new(@attribute.bind(derived_relation), nil, @expression) end - + it "returns self if the substituting to the same relation" do @expression.bind(@relation).should == @expression end end - + describe '#as' do it "manufactures an aliased expression" do @expression.as(:alias).should == Expression.new(@attribute, :alias, @expression) end end - + describe '#to_attribute' do it "manufactures an attribute with the expression as an ancestor" do @expression.to_attribute(@relation).should == Attribute.new(@relation, @expression.alias, :ancestor => @expression) @@ -36,4 +36,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/primitives/value_spec.rb b/spec/arel/algebra/unit/primitives/value_spec.rb index 8774ca78c5..45208e6c5d 100644 --- a/spec/arel/algebra/unit/primitives/value_spec.rb +++ b/spec/arel/algebra/unit/primitives/value_spec.rb @@ -12,4 +12,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/alias_spec.rb b/spec/arel/algebra/unit/relations/alias_spec.rb index c87a0ca2dd..a5d716a638 100644 --- a/spec/arel/algebra/unit/relations/alias_spec.rb +++ b/spec/arel/algebra/unit/relations/alias_spec.rb @@ -5,7 +5,7 @@ module Arel before do @relation = Table.new(:users) end - + describe '==' do it "obtains if the objects are the same" do Alias.new(@relation).should_not == Alias.new(@relation) @@ -13,4 +13,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/delete_spec.rb b/spec/arel/algebra/unit/relations/delete_spec.rb index 075e59e724..7578e12a3e 100644 --- a/spec/arel/algebra/unit/relations/delete_spec.rb +++ b/spec/arel/algebra/unit/relations/delete_spec.rb @@ -6,4 +6,4 @@ module Arel @relation = Table.new(:users) end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/group_spec.rb b/spec/arel/algebra/unit/relations/group_spec.rb index 050de2993d..58f9252356 100644 --- a/spec/arel/algebra/unit/relations/group_spec.rb +++ b/spec/arel/algebra/unit/relations/group_spec.rb @@ -7,4 +7,4 @@ module Arel @attribute = @relation[:id] end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/insert_spec.rb b/spec/arel/algebra/unit/relations/insert_spec.rb index 184cd2a926..feb1a5eae4 100644 --- a/spec/arel/algebra/unit/relations/insert_spec.rb +++ b/spec/arel/algebra/unit/relations/insert_spec.rb @@ -6,4 +6,4 @@ module Arel @relation = Table.new(:users) end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/join_spec.rb b/spec/arel/algebra/unit/relations/join_spec.rb index 5b512cc7f6..f5a8bd32aa 100644 --- a/spec/arel/algebra/unit/relations/join_spec.rb +++ b/spec/arel/algebra/unit/relations/join_spec.rb @@ -7,14 +7,14 @@ module Arel @relation2 = Table.new(:photos) @predicate = @relation1[:id].eq(@relation2[:user_id]) end - + describe 'hashing' do it 'implements hash equality' do InnerJoin.new(@relation1, @relation2, @predicate) \ .should hash_the_same_as(InnerJoin.new(@relation1, @relation2, @predicate)) end end - + describe '#attributes' do it 'combines the attributes of the two relations' do join = InnerJoin.new(@relation1, @relation2, @predicate) @@ -23,4 +23,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/order_spec.rb b/spec/arel/algebra/unit/relations/order_spec.rb index 0e1b1a0e54..4f163894c8 100644 --- a/spec/arel/algebra/unit/relations/order_spec.rb +++ b/spec/arel/algebra/unit/relations/order_spec.rb @@ -8,4 +8,4 @@ module Arel end end end - \ No newline at end of file + diff --git a/spec/arel/algebra/unit/relations/project_spec.rb b/spec/arel/algebra/unit/relations/project_spec.rb index b71acf5e91..9f4358ea54 100644 --- a/spec/arel/algebra/unit/relations/project_spec.rb +++ b/spec/arel/algebra/unit/relations/project_spec.rb @@ -6,24 +6,24 @@ module Arel @relation = Table.new(:users) @attribute = @relation[:id] end - + describe '#attributes' do before do @projection = Project.new(@relation, @attribute) end - + it "manufactures attributes associated with the projection relation" do @projection.attributes.should == [@attribute].collect { |a| a.bind(@projection) } end end - + describe '#externalizable?' do describe 'when the projections are attributes' do it 'returns false' do Project.new(@relation, @attribute).should_not be_externalizable end end - + describe 'when the projections include an aggregation' do it "obtains" do Project.new(@relation, @attribute.sum).should be_externalizable @@ -31,4 +31,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/relation_spec.rb b/spec/arel/algebra/unit/relations/relation_spec.rb index 9707f2887c..adf82847ac 100644 --- a/spec/arel/algebra/unit/relations/relation_spec.rb +++ b/spec/arel/algebra/unit/relations/relation_spec.rb @@ -7,14 +7,14 @@ module Arel @attribute1 = @relation[:id] @attribute2 = @relation[:name] end - + describe '[]' do describe 'when given an', Attribute do it "return the attribute congruent to the provided attribute" do @relation[@attribute1].should == @attribute1 end end - + describe 'when given a', Symbol, String do it "returns the attribute with the same name, if it exists" do @relation[:id].should == @attribute1 @@ -23,13 +23,13 @@ module Arel end end end - + describe Relation::Operable do describe 'joins' do before do @predicate = @relation[:id].eq(@relation[:id]) end - + describe '#join' do describe 'when given a relation' do it "manufactures an inner join operation between those two relations" do @@ -37,13 +37,13 @@ module Arel should == InnerJoin.new(@relation, @relation, @predicate) end end - + describe "when given a string" do it "manufactures a join operation with the string passed through" do - @relation.join(arbitrary_string = "ASDF").should == StringJoin.new(@relation, arbitrary_string) + @relation.join(arbitrary_string = "ASDF").should == StringJoin.new(@relation, arbitrary_string) end end - + describe "when given something blank" do it "returns self" do @relation.join.should == @relation @@ -64,7 +64,7 @@ module Arel @relation.project(@attribute1, @attribute2). \ should == Project.new(@relation, @attribute1, @attribute2) end - + describe "when given blank attributes" do it "returns self" do @relation.project.should == @relation @@ -97,36 +97,36 @@ module Arel end end end - + describe '#order' do it "manufactures an order relation" do @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2) end - + describe 'when given a blank ordering' do it 'returns self' do @relation.order.should == @relation end end end - + describe '#take' do it "manufactures a take relation" do @relation.take(5).should == Take.new(@relation, 5) end - + describe 'when given a blank number of items' do it 'returns self' do @relation.take.should == @relation end end end - + describe '#skip' do it "manufactures a skip relation" do @relation.skip(4).should == Skip.new(@relation, 4) end - + describe 'when given a blank number of items' do it 'returns self' do @relation.skip.should == @relation @@ -138,14 +138,14 @@ module Arel it 'manufactures a group relation' do @relation.group(@attribute1, @attribute2).should == Group.new(@relation, @attribute1, @attribute2) end - + describe 'when given blank groupings' do it 'returns self' do @relation.group.should == @relation end end end - + describe Relation::Operable::Writable do describe '#delete' do it 'manufactures a deletion relation' do @@ -177,7 +177,7 @@ module Arel end end end - + describe Relation::Enumerable do it "implements enumerable" do @relation.collect.should == @relation.session.read(@relation).collect @@ -185,4 +185,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/skip_spec.rb b/spec/arel/algebra/unit/relations/skip_spec.rb index ff57e03d1c..a41913436e 100644 --- a/spec/arel/algebra/unit/relations/skip_spec.rb +++ b/spec/arel/algebra/unit/relations/skip_spec.rb @@ -7,4 +7,4 @@ module Arel @skipped = 4 end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/table_spec.rb b/spec/arel/algebra/unit/relations/table_spec.rb index 4821d92299..dfe457043c 100644 --- a/spec/arel/algebra/unit/relations/table_spec.rb +++ b/spec/arel/algebra/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,22 +18,22 @@ 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 end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/take_spec.rb b/spec/arel/algebra/unit/relations/take_spec.rb index 6f8b4fd36e..2bc17db5a1 100644 --- a/spec/arel/algebra/unit/relations/take_spec.rb +++ b/spec/arel/algebra/unit/relations/take_spec.rb @@ -7,4 +7,4 @@ module Arel @taken = 4 end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/update_spec.rb b/spec/arel/algebra/unit/relations/update_spec.rb index c27afb48b2..e9642ffc99 100644 --- a/spec/arel/algebra/unit/relations/update_spec.rb +++ b/spec/arel/algebra/unit/relations/update_spec.rb @@ -6,4 +6,4 @@ module Arel @relation = Table.new(:users) end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/relations/where_spec.rb b/spec/arel/algebra/unit/relations/where_spec.rb index 3f37b53138..6c3074a3a5 100644 --- a/spec/arel/algebra/unit/relations/where_spec.rb +++ b/spec/arel/algebra/unit/relations/where_spec.rb @@ -6,13 +6,13 @@ module Arel @relation = Table.new(:users) @predicate = @relation[:id].eq(1) end - + describe '#initialize' do it "manufactures nested where relations if multiple predicates are provided" do - another_predicate = @relation[:name].lt(2) + another_predicate = @relation[:name].lt(2) Where.new(@relation, @predicate, another_predicate). \ should == Where.new(Where.new(@relation, another_predicate), @predicate) end end end -end \ No newline at end of file +end diff --git a/spec/arel/algebra/unit/session/session_spec.rb b/spec/arel/algebra/unit/session/session_spec.rb index e17b5d638a..ca0a43f278 100644 --- a/spec/arel/algebra/unit/session/session_spec.rb +++ b/spec/arel/algebra/unit/session/session_spec.rb @@ -6,7 +6,7 @@ module Arel @relation = Table.new(:users) @session = Session.new end - + describe '::start' do describe '::instance' do it "it is a singleton within the started session" do @@ -23,13 +23,13 @@ module Arel end end end - + it "manufactures new sessions outside of the started session" do Session.new.should_not == Session.new end end end - + describe Session::CRUD do before do @insert = Insert.new(@relation, @relation[:name] => 'nick') @@ -37,34 +37,34 @@ module Arel @delete = Deletion.new(@relation) @read = @relation end - + describe '#create' do it "executes an insertion on the connection" do mock(@insert).call @session.create(@insert) end end - + describe '#read' do it "executes an selection on the connection" do mock(@read).call @session.read(@read) end - + it "is memoized" do mock(@read).call.once @session.read(@read) @session.read(@read) end end - + describe '#update' do it "executes an update on the connection" do mock(@update).call @session.update(@update) end end - + describe '#delete' do it "executes a delete on the connection" do mock(@delete).call @@ -72,13 +72,13 @@ module Arel end end end - + describe 'Transactions' do describe '#begin' do end - + describe '#end' do end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb index 4862300052..bffecc9182 100644 --- a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb +++ b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb @@ -45,4 +45,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/memory/unit/relations/array_spec.rb b/spec/arel/engines/memory/unit/relations/array_spec.rb index 4fe24c77fa..dd9da41569 100644 --- a/spec/arel/engines/memory/unit/relations/array_spec.rb +++ b/spec/arel/engines/memory/unit/relations/array_spec.rb @@ -29,4 +29,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/memory/unit/relations/insert_spec.rb b/spec/arel/engines/memory/unit/relations/insert_spec.rb index 4b5e8833a0..59e43328a3 100644 --- a/spec/arel/engines/memory/unit/relations/insert_spec.rb +++ b/spec/arel/engines/memory/unit/relations/insert_spec.rb @@ -9,7 +9,7 @@ module Arel [3, 'goose'] ], [:id, :name]) end - + describe '#call' do it "manufactures an array of hashes of attributes to values" do @relation \ @@ -25,4 +25,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/memory/unit/relations/join_spec.rb b/spec/arel/engines/memory/unit/relations/join_spec.rb index 920cc55d0a..df08fd4a96 100644 --- a/spec/arel/engines/memory/unit/relations/join_spec.rb +++ b/spec/arel/engines/memory/unit/relations/join_spec.rb @@ -11,7 +11,7 @@ module Arel @relation2 = @relation1.alias @relation3 = @relation1.alias end - + describe InnerJoin do describe '#call' do it 'combines the two tables where the predicate obtains' do @@ -29,4 +29,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/memory/unit/relations/order_spec.rb b/spec/arel/engines/memory/unit/relations/order_spec.rb index 3ecb31068b..1e9690bbbf 100644 --- a/spec/arel/engines/memory/unit/relations/order_spec.rb +++ b/spec/arel/engines/memory/unit/relations/order_spec.rb @@ -9,7 +9,7 @@ module Arel [3, 'goose'] ], [:id, :name]) end - + describe '#call' do it 'sorts the relation with the provided ordering' do @relation \ @@ -24,4 +24,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/memory/unit/relations/project_spec.rb b/spec/arel/engines/memory/unit/relations/project_spec.rb index 1d1224cfdc..1690910026 100644 --- a/spec/arel/engines/memory/unit/relations/project_spec.rb +++ b/spec/arel/engines/memory/unit/relations/project_spec.rb @@ -9,7 +9,7 @@ module Arel [3, 'goose'] ], [:id, :name]) end - + describe '#call' do it 'retains only the attributes that are provided' do @relation \ @@ -24,4 +24,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/memory/unit/relations/skip_spec.rb b/spec/arel/engines/memory/unit/relations/skip_spec.rb index 86db45ef61..3411c5493b 100644 --- a/spec/arel/engines/memory/unit/relations/skip_spec.rb +++ b/spec/arel/engines/memory/unit/relations/skip_spec.rb @@ -9,7 +9,7 @@ module Arel [3, 'goose'] ], [:id, :name]) end - + describe '#call' do it 'removes the first n rows' do @relation \ @@ -23,4 +23,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/memory/unit/relations/take_spec.rb b/spec/arel/engines/memory/unit/relations/take_spec.rb index 8b774987e0..5e7c4fb462 100644 --- a/spec/arel/engines/memory/unit/relations/take_spec.rb +++ b/spec/arel/engines/memory/unit/relations/take_spec.rb @@ -9,7 +9,7 @@ module Arel [3, 'goose'] ], [:id, :name]) end - + describe '#call' do it 'removes the rows after the first n' do @relation \ @@ -23,4 +23,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/memory/unit/relations/where_spec.rb b/spec/arel/engines/memory/unit/relations/where_spec.rb index d75ee5dcbe..1d2c2eb39c 100644 --- a/spec/arel/engines/memory/unit/relations/where_spec.rb +++ b/spec/arel/engines/memory/unit/relations/where_spec.rb @@ -9,7 +9,7 @@ module Arel [3, 'goose'] ], [:id, :name]) end - + describe '#call' do it 'filters the relation with the provided predicate' do @relation \ @@ -21,7 +21,7 @@ module Arel ] end end - + describe 'when filtering a where relation' do it 'further filters the already-filtered relation with the provided predicate' do @relation \ @@ -36,4 +36,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/sql/unit/predicates/binary_spec.rb b/spec/arel/engines/sql/unit/predicates/binary_spec.rb index 679147067e..befd2878d9 100644 --- a/spec/arel/engines/sql/unit/predicates/binary_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/binary_spec.rb @@ -102,4 +102,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/sql/unit/predicates/equality_spec.rb b/spec/arel/engines/sql/unit/predicates/equality_spec.rb index e8c8c42675..688a6a20be 100644 --- a/spec/arel/engines/sql/unit/predicates/equality_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/equality_spec.rb @@ -43,4 +43,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/sql/unit/predicates/in_spec.rb b/spec/arel/engines/sql/unit/predicates/in_spec.rb index d977937e4e..d3e75cfb84 100644 --- a/spec/arel/engines/sql/unit/predicates/in_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/in_spec.rb @@ -83,4 +83,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/sql/unit/primitives/attribute_spec.rb b/spec/arel/engines/sql/unit/primitives/attribute_spec.rb index e71ab949f1..6cb72f3c19 100644 --- a/spec/arel/engines/sql/unit/primitives/attribute_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/attribute_spec.rb @@ -12,7 +12,7 @@ module Arel @attribute.column.should == @relation.column_for(@attribute) end end - + describe '#to_sql' do describe 'for a simple attribute' do it "manufactures sql with an alias" do @@ -29,4 +29,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/sql/unit/relations/group_spec.rb b/spec/arel/engines/sql/unit/relations/group_spec.rb index b7279a23d9..5e0c675c8b 100644 --- a/spec/arel/engines/sql/unit/relations/group_spec.rb +++ b/spec/arel/engines/sql/unit/relations/group_spec.rb @@ -53,4 +53,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/sql/unit/relations/join_spec.rb b/spec/arel/engines/sql/unit/relations/join_spec.rb index 1f43101133..f904b61870 100644 --- a/spec/arel/engines/sql/unit/relations/join_spec.rb +++ b/spec/arel/engines/sql/unit/relations/join_spec.rb @@ -54,4 +54,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/sql/unit/relations/skip_spec.rb b/spec/arel/engines/sql/unit/relations/skip_spec.rb index f8e5ceaad3..c14bd1ce95 100644 --- a/spec/arel/engines/sql/unit/relations/skip_spec.rb +++ b/spec/arel/engines/sql/unit/relations/skip_spec.rb @@ -29,4 +29,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/sql/unit/relations/table_spec.rb b/spec/arel/engines/sql/unit/relations/table_spec.rb index 3f5a5ac248..9797b38822 100644 --- a/spec/arel/engines/sql/unit/relations/table_spec.rb +++ b/spec/arel/engines/sql/unit/relations/table_spec.rb @@ -66,4 +66,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/engines/sql/unit/relations/take_spec.rb b/spec/arel/engines/sql/unit/relations/take_spec.rb index 1263ed4795..8f1240fc17 100644 --- a/spec/arel/engines/sql/unit/relations/take_spec.rb +++ b/spec/arel/engines/sql/unit/relations/take_spec.rb @@ -29,4 +29,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/arel/unit/predicates/predicates_spec.rb b/spec/arel/unit/predicates/predicates_spec.rb index 8f9cec5376..ac842998af 100644 --- a/spec/arel/unit/predicates/predicates_spec.rb +++ b/spec/arel/unit/predicates/predicates_spec.rb @@ -50,4 +50,4 @@ module Arel end end end -end \ No newline at end of file +end diff --git a/spec/connections/mysql_connection.rb b/spec/connections/mysql_connection.rb index 789628b95d..b4e27f2380 100644 --- a/spec/connections/mysql_connection.rb +++ b/spec/connections/mysql_connection.rb @@ -10,4 +10,4 @@ ActiveRecord::Base.configurations = { } } -ActiveRecord::Base.establish_connection 'unit' \ No newline at end of file +ActiveRecord::Base.establish_connection 'unit' diff --git a/spec/doubles/hash.rb b/spec/doubles/hash.rb index 97d25742cb..32c5b98058 100644 --- a/spec/doubles/hash.rb +++ b/spec/doubles/hash.rb @@ -2,19 +2,19 @@ class Hash def ordered_array to_a.sort { |(key1, value1), (key2, value2)| key1.hash <=> key2.hash } end - + def keys ordered_array.collect(&:first) end - + def values ordered_array.collect { |_, v| v } end - + def each(&block) ordered_array.each(&block) end - + def shift returning to_a.first do |k, v| delete(k) diff --git a/spec/matchers/be_like.rb b/spec/matchers/be_like.rb index 4ff5bc532f..c9d4d4b979 100644 --- a/spec/matchers/be_like.rb +++ b/spec/matchers/be_like.rb @@ -3,22 +3,22 @@ module BeLikeMatcher def initialize(expected) @expected = expected end - + def matches?(actual) @actual = actual @expected.gsub(/\s+/, ' ').strip == @actual.gsub(/\s+/, ' ').strip end - + def failure_message "expected\n#{@actual}\nto be like\n#{@expected}" end - + def negative_failure_message "expected\n#{@actual}\nto be unlike\n#{@expected}" end end - + def be_like(expected) BeLike.new(expected) end -end \ No newline at end of file +end diff --git a/spec/matchers/disambiguate_attributes.rb b/spec/matchers/disambiguate_attributes.rb index bee7d22b0c..bc4a5215d4 100644 --- a/spec/matchers/disambiguate_attributes.rb +++ b/spec/matchers/disambiguate_attributes.rb @@ -3,7 +3,7 @@ module DisambiguateAttributesMatcher def initialize(attributes) @attributes = attributes end - + def matches?(actual) @actual = actual attribute1, attribute2 = @attributes @@ -11,18 +11,18 @@ module DisambiguateAttributesMatcher !@actual[attribute1].descends_from?(attribute2) && @actual[attribute2].descends_from?(attribute2) end - + def failure_message "" # "expected #{@actual} to disambiguate its attributes" end - + def negative_failure_message "expected #{@actual} to not disambiguate its attributes" end end - + def disambiguate_attributes(*attributes) DisambiguateAttributes.new(attributes) end -end \ No newline at end of file +end diff --git a/spec/matchers/hash_the_same_as.rb b/spec/matchers/hash_the_same_as.rb index c1903b62b4..03e955a0cb 100644 --- a/spec/matchers/hash_the_same_as.rb +++ b/spec/matchers/hash_the_same_as.rb @@ -3,24 +3,24 @@ module HashTheSameAsMatcher def initialize(expected) @expected = expected end - + def matches?(actual) @actual = actual hash = {} hash[@expected] = :some_arbitrary_value hash[@actual] == :some_arbitrary_value end - + def failure_message "expected #{@actual} to hash the same as #{@expected}; they must be `eql?` and have the same `#hash` value" end - + def negative_failure_message "expected #{@actual} to hash differently than #{@expected}; they must not be `eql?` or have a differing `#hash` values" end end - + def hash_the_same_as(expected) HashTheSameAs.new(expected) end -end \ No newline at end of file +end diff --git a/spec/schemas/mysql_schema.rb b/spec/schemas/mysql_schema.rb index 1123f4582e..dc2558fd6a 100644 --- a/spec/schemas/mysql_schema.rb +++ b/spec/schemas/mysql_schema.rb @@ -15,4 +15,4 @@ SQL sql.split(/;/).select(&:present?).each do |sql_statement| ActiveRecord::Base.connection.execute sql_statement -end \ No newline at end of file +end diff --git a/spec/schemas/sqlite3_schema.rb b/spec/schemas/sqlite3_schema.rb index 6c98a4f934..94d224520e 100644 --- a/spec/schemas/sqlite3_schema.rb +++ b/spec/schemas/sqlite3_schema.rb @@ -15,4 +15,4 @@ SQL sql.split(/;/).select(&:present?).each do |sql_statement| ActiveRecord::Base.connection.execute sql_statement -end \ No newline at end of file +end -- cgit v1.2.3 From 16707d1b96365ab569e7a5e47a3d694c14d4132c Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 16:44:38 -0400 Subject: Add spec for Attribute#inspect --- spec/arel/algebra/unit/primitives/attribute_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'spec') diff --git a/spec/arel/algebra/unit/primitives/attribute_spec.rb b/spec/arel/algebra/unit/primitives/attribute_spec.rb index 89e338e377..afcf1237e0 100644 --- a/spec/arel/algebra/unit/primitives/attribute_spec.rb +++ b/spec/arel/algebra/unit/primitives/attribute_spec.rb @@ -7,6 +7,12 @@ module Arel @attribute = @relation[:id] end + describe "#inspect" do + it "returns a simple, short inspect string" do + @attribute.inspect.should == "" + end + end + describe Attribute::Transformations do describe '#as' do it "manufactures an aliased attributed" do -- cgit v1.2.3 From d2988420fc6dd91ca751d96ed648fd1ed52ce342 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 17:53:40 -0400 Subject: Added PostgreSQL to build --- .../engines/sql/unit/predicates/binary_spec.rb | 18 ++++++++-- .../arel/engines/sql/unit/primitives/value_spec.rb | 9 ++++- .../arel/engines/sql/unit/relations/insert_spec.rb | 20 ++++++++++-- .../arel/engines/sql/unit/relations/update_spec.rb | 38 +++++++++++++++++++--- spec/arel/unit/predicates/predicates_spec.rb | 16 +++++++-- spec/connections/postgresql_connection.rb | 12 +++++++ spec/schemas/postgresql_schema.rb | 18 ++++++++++ spec/spec_helper.rb | 14 +++++++- 8 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 spec/connections/postgresql_connection.rb create mode 100644 spec/schemas/postgresql_schema.rb (limited to 'spec') diff --git a/spec/arel/engines/sql/unit/predicates/binary_spec.rb b/spec/arel/engines/sql/unit/predicates/binary_spec.rb index befd2878d9..b1400e2588 100644 --- a/spec/arel/engines/sql/unit/predicates/binary_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/binary_spec.rb @@ -28,7 +28,11 @@ module Arel sql.should be_like(%Q{(`users`.`id` <=> 1 OR `users`.`name` <=> 'name')}) end - adapter_is_not :mysql do + adapter_is :postgresql do + sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> E'name')}) + end + + adapter_is :sqlite3 do sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> 'name')}) end end @@ -44,9 +48,13 @@ module Arel sql.should be_like(%Q{(`users`.`id` <=> 1 AND `users`.`name` <=> 'name')}) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> 'name')}) end + + adapter_is :postgresql do + sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> E'name')}) + end end end end @@ -94,9 +102,13 @@ module Arel sql.should be_like(%Q{`users`.`name` <=> '1-asdf'}) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{"users"."name" <=> '1-asdf'}) end + + adapter_is :postgresql do + sql.should be_like(%Q{"users"."name" <=> E'1-asdf'}) + end end end end diff --git a/spec/arel/engines/sql/unit/primitives/value_spec.rb b/spec/arel/engines/sql/unit/primitives/value_spec.rb index da5a163d3b..ff3533f6ef 100644 --- a/spec/arel/engines/sql/unit/primitives/value_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/value_spec.rb @@ -9,7 +9,14 @@ module Arel describe '#to_sql' do it "appropriately quotes the value" do Value.new(1, @relation).to_sql.should be_like('1') - Value.new('asdf', @relation).to_sql.should be_like("'asdf'") + + adapter_is_not :postgresql do + Value.new('asdf', @relation).to_sql.should be_like("'asdf'") + end + + adapter_is :postgresql do + Value.new('asdf', @relation).to_sql.should be_like("E'asdf'") + end end end diff --git a/spec/arel/engines/sql/unit/relations/insert_spec.rb b/spec/arel/engines/sql/unit/relations/insert_spec.rb index dd1995cced..29a5e0bf42 100644 --- a/spec/arel/engines/sql/unit/relations/insert_spec.rb +++ b/spec/arel/engines/sql/unit/relations/insert_spec.rb @@ -30,13 +30,21 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do @insertion.to_sql.should be_like(%Q{ INSERT INTO "users" ("id", "name") VALUES (1, 'nick') }) end + + adapter_is :postgresql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO "users" + ("id", "name") VALUES (1, E'nick') + }) + end end describe 'when given values whose types correspond to the types of the attributes' do @@ -53,13 +61,21 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do @insertion.to_sql.should be_like(%Q{ INSERT INTO "users" ("name") VALUES ('nick') }) end + + adapter_is :postgresql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO "users" + ("name") VALUES (E'nick') + }) + end end end diff --git a/spec/arel/engines/sql/unit/relations/update_spec.rb b/spec/arel/engines/sql/unit/relations/update_spec.rb index f553490ef5..4d728eb241 100644 --- a/spec/arel/engines/sql/unit/relations/update_spec.rb +++ b/spec/arel/engines/sql/unit/relations/update_spec.rb @@ -17,12 +17,19 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{ UPDATE "users" SET "id" = 1, "name" = 'nick' }) end + + adapter_is :postgresql do + sql.should be_like(%Q{ + UPDATE "users" + SET "id" = 1, "name" = E'nick' + }) + end end it "manufactures sql updating attributes when given a ranged relation" do @@ -36,13 +43,21 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{ UPDATE "users" SET "name" = 'nick' LIMIT 1 }) end + + adapter_is :postgresql do + sql.should be_like(%Q{ + UPDATE "users" + SET "name" = E'nick' + LIMIT 1 + }) + end end describe 'when given values whose types correspond to the types of the attributes' do @@ -58,12 +73,19 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do @update.to_sql.should be_like(%Q{ UPDATE "users" SET "name" = 'nick' }) end + + adapter_is :postgresql do + @update.to_sql.should be_like(%Q{ + UPDATE "users" + SET "name" = E'nick' + }) + end end end @@ -106,13 +128,21 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do @update.to_sql.should be_like(%Q{ UPDATE "users" SET "name" = 'nick' WHERE "users"."id" = 1 }) end + + adapter_is :postgresql do + @update.to_sql.should be_like(%Q{ + UPDATE "users" + SET "name" = E'nick' + WHERE "users"."id" = 1 + }) + end end end end diff --git a/spec/arel/unit/predicates/predicates_spec.rb b/spec/arel/unit/predicates/predicates_spec.rb index ac842998af..8e7e0b1d9f 100644 --- a/spec/arel/unit/predicates/predicates_spec.rb +++ b/spec/arel/unit/predicates/predicates_spec.rb @@ -21,11 +21,17 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{ ("users"."id" = 1 AND "users"."name" = 'name') }) end + + adapter_is :postgresql do + sql.should be_like(%Q{ + ("users"."id" = 1 AND "users"."name" = E'name') + }) + end end end end @@ -41,11 +47,17 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{ ("users"."id" = 1 OR "users"."name" = 'name') }) end + + adapter_is :postgresql do + sql.should be_like(%Q{ + ("users"."id" = 1 OR "users"."name" = E'name') + }) + end end end end diff --git a/spec/connections/postgresql_connection.rb b/spec/connections/postgresql_connection.rb new file mode 100644 index 0000000000..505dcdd1ef --- /dev/null +++ b/spec/connections/postgresql_connection.rb @@ -0,0 +1,12 @@ +require "activerecord" +puts "Using native PostgreSQL" + +ActiveRecord::Base.configurations = { + 'unit' => { + :adapter => 'postgresql', + :encoding => 'utf8', + :database => 'arel_unit', + } +} + +ActiveRecord::Base.establish_connection 'unit' diff --git a/spec/schemas/postgresql_schema.rb b/spec/schemas/postgresql_schema.rb new file mode 100644 index 0000000000..30fa665902 --- /dev/null +++ b/spec/schemas/postgresql_schema.rb @@ -0,0 +1,18 @@ +sql = <<-SQL + DROP TABLE IF EXISTS users; + CREATE TABLE users ( + id SERIAL PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL + ); + + DROP TABLE IF EXISTS photos; + CREATE TABLE photos ( + id SERIAL PRIMARY KEY NOT NULL, + 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6a9a2ef23c..beb634fbd3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,15 +13,27 @@ end module AdapterGuards def adapter_is(name) + verify_adapter_name(name) yield if name.to_s == adapter_name end def adapter_is_not(name) + verify_adapter_name(name) yield if name.to_s != adapter_name end def adapter_name - Arel::Table.engine.connection.class.name.underscore.split("/").last.gsub(/_adapter/, '') + name = ActiveRecord::Base.configurations["unit"][:adapter] + verify_adapter_name(name) + name + end + + def verify_adapter_name(name) + raise "Invalid adapter name: #{name}" unless valid_adapters.include?(name.to_s) + end + + def valid_adapters + %w[mysql postgresql sqlite3] end end -- cgit v1.2.3 From c106c1ef59527331c9945f5f95e6b4e27194ac06 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 18:08:13 -0400 Subject: Moving SQL predicates spec to correct dir --- .../engines/sql/unit/predicates/predicates_spec.rb | 65 ++++++++++++++++++++++ spec/arel/unit/predicates/predicates_spec.rb | 65 ---------------------- 2 files changed, 65 insertions(+), 65 deletions(-) create mode 100644 spec/arel/engines/sql/unit/predicates/predicates_spec.rb delete mode 100644 spec/arel/unit/predicates/predicates_spec.rb (limited to 'spec') diff --git a/spec/arel/engines/sql/unit/predicates/predicates_spec.rb b/spec/arel/engines/sql/unit/predicates/predicates_spec.rb new file mode 100644 index 0000000000..d55e178e43 --- /dev/null +++ b/spec/arel/engines/sql/unit/predicates/predicates_spec.rb @@ -0,0 +1,65 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Predicate do + before do + @relation = Table.new(:users) + @attribute1 = @relation[:id] + @attribute2 = @relation[:name] + @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 + 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 :sqlite3 do + sql.should be_like(%Q{ + ("users"."id" = 1 AND "users"."name" = 'name') + }) + end + + adapter_is :postgresql do + sql.should be_like(%Q{ + ("users"."id" = 1 AND "users"."name" = E'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 + 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 :sqlite3 do + sql.should be_like(%Q{ + ("users"."id" = 1 OR "users"."name" = 'name') + }) + end + + adapter_is :postgresql do + sql.should be_like(%Q{ + ("users"."id" = 1 OR "users"."name" = E'name') + }) + end + end + end + end + end +end diff --git a/spec/arel/unit/predicates/predicates_spec.rb b/spec/arel/unit/predicates/predicates_spec.rb deleted file mode 100644 index 8e7e0b1d9f..0000000000 --- a/spec/arel/unit/predicates/predicates_spec.rb +++ /dev/null @@ -1,65 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module Arel - describe Predicate do - before do - @relation = Table.new(:users) - @attribute1 = @relation[:id] - @attribute2 = @relation[:name] - @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 - 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 :sqlite3 do - sql.should be_like(%Q{ - ("users"."id" = 1 AND "users"."name" = 'name') - }) - end - - adapter_is :postgresql do - sql.should be_like(%Q{ - ("users"."id" = 1 AND "users"."name" = E'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 - 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 :sqlite3 do - sql.should be_like(%Q{ - ("users"."id" = 1 OR "users"."name" = 'name') - }) - end - - adapter_is :postgresql do - sql.should be_like(%Q{ - ("users"."id" = 1 OR "users"."name" = E'name') - }) - end - end - end - end - end -end -- cgit v1.2.3 From 10c4b42c35f61145ac6ebb8d36f504344eef47cd Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 18:21:56 -0400 Subject: Fix bug in Order equality where Descending.new(attribute) was == Ascending.new(attribute) --- spec/arel/algebra/unit/primitives/attribute_spec.rb | 4 ++-- spec/arel/algebra/unit/relations/order_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/spec/arel/algebra/unit/primitives/attribute_spec.rb b/spec/arel/algebra/unit/primitives/attribute_spec.rb index afcf1237e0..2ca63ba48e 100644 --- a/spec/arel/algebra/unit/primitives/attribute_spec.rb +++ b/spec/arel/algebra/unit/primitives/attribute_spec.rb @@ -169,13 +169,13 @@ module Arel describe Attribute::Orderings do describe '#asc' do it 'manufactures an ascending ordering' do - pending + @attribute.asc.should == Ascending.new(@attribute) end end describe '#desc' do it 'manufactures a descending ordering' do - pending + @attribute.desc.should == Descending.new(@attribute) end end end diff --git a/spec/arel/algebra/unit/relations/order_spec.rb b/spec/arel/algebra/unit/relations/order_spec.rb index 4f163894c8..8b3c932fb9 100644 --- a/spec/arel/algebra/unit/relations/order_spec.rb +++ b/spec/arel/algebra/unit/relations/order_spec.rb @@ -6,6 +6,16 @@ module Arel @relation = Table.new(:users) @attribute = @relation[:id] end + + describe "#==" do + it "returns true when the Orders are for the same attribute and direction" do + Ascending.new(@attribute).should == Ascending.new(@attribute) + end + + it "returns false when the Orders are for a diferent direction" do + Ascending.new(@attribute).should_not == Descending.new(@attribute) + end + end end end -- cgit v1.2.3 From 9560650fa79212b33d6ab349ff83b4f617d395b7 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 18:36:08 -0400 Subject: Adding skeleton of spec for CRUD operations in Sql::Engine --- spec/arel/engines/sql/unit/engine_spec.rb | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 spec/arel/engines/sql/unit/engine_spec.rb (limited to 'spec') diff --git a/spec/arel/engines/sql/unit/engine_spec.rb b/spec/arel/engines/sql/unit/engine_spec.rb new file mode 100644 index 0000000000..67a5d62320 --- /dev/null +++ b/spec/arel/engines/sql/unit/engine_spec.rb @@ -0,0 +1,36 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Sql::Engine do + before do + @relation = Table.new(:users) + end + + describe "CRUD" do + describe "#create" do + it "inserts into the relation" do + @relation.insert @relation[:name] => "Bryan" + end + end + + describe "#read" do + it "reads from the relation" do + @relation.each do |row| + end + end + end + + describe "#update" do + it "updates the relation" do + @relation.update @relation[:name] => "Bryan" + end + end + + describe "#delete" do + it "deletes from the relation" do + @relation.delete + end + end + end + end +end -- cgit v1.2.3 From d3c343c6afdc65ca959beaeb20d71dd9f2fcf93f Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 18 May 2009 00:59:01 -0400 Subject: Remvoing unused variable from spec --- spec/arel/engines/memory/unit/relations/join_spec.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'spec') diff --git a/spec/arel/engines/memory/unit/relations/join_spec.rb b/spec/arel/engines/memory/unit/relations/join_spec.rb index df08fd4a96..110fdb03b7 100644 --- a/spec/arel/engines/memory/unit/relations/join_spec.rb +++ b/spec/arel/engines/memory/unit/relations/join_spec.rb @@ -9,7 +9,6 @@ module Arel [3, 'goose'] ], [:id, :name]) @relation2 = @relation1.alias - @relation3 = @relation1.alias end describe InnerJoin do -- cgit v1.2.3 From 082079169caab2a00f62d9de9c4bdb5dcbc591aa Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 18 May 2009 01:49:50 -0400 Subject: Better inspect output for Expressions --- spec/arel/algebra/unit/primitives/expression_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'spec') diff --git a/spec/arel/algebra/unit/primitives/expression_spec.rb b/spec/arel/algebra/unit/primitives/expression_spec.rb index 82d12d53f9..768bb492a7 100644 --- a/spec/arel/algebra/unit/primitives/expression_spec.rb +++ b/spec/arel/algebra/unit/primitives/expression_spec.rb @@ -7,6 +7,12 @@ module Arel @attribute = @relation[:id] end + describe "#inspect" do + it "returns a simple, short inspect string" do + @attribute.count.inspect.should == ">" + end + end + describe Expression::Transformations do before do @expression = Count.new(@attribute) -- cgit v1.2.3 From eede6962a8c1ee86b49907c1937fb0e3a38cf819 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 18 May 2009 01:50:39 -0400 Subject: Log queries to debug.log when running specs --- spec/connections/mysql_connection.rb | 2 ++ spec/connections/postgresql_connection.rb | 2 ++ spec/connections/sqlite3_connection.rb | 2 ++ 3 files changed, 6 insertions(+) (limited to 'spec') diff --git a/spec/connections/mysql_connection.rb b/spec/connections/mysql_connection.rb index b4e27f2380..a58ddc35ef 100644 --- a/spec/connections/mysql_connection.rb +++ b/spec/connections/mysql_connection.rb @@ -1,6 +1,8 @@ require "activerecord" puts "Using native MySQL" +ActiveRecord::Base.logger = Logger.new("debug.log") + ActiveRecord::Base.configurations = { 'unit' => { :adapter => 'mysql', diff --git a/spec/connections/postgresql_connection.rb b/spec/connections/postgresql_connection.rb index 505dcdd1ef..e376d33ec6 100644 --- a/spec/connections/postgresql_connection.rb +++ b/spec/connections/postgresql_connection.rb @@ -1,6 +1,8 @@ require "activerecord" puts "Using native PostgreSQL" +ActiveRecord::Base.logger = Logger.new("debug.log") + ActiveRecord::Base.configurations = { 'unit' => { :adapter => 'postgresql', diff --git a/spec/connections/sqlite3_connection.rb b/spec/connections/sqlite3_connection.rb index bae077711d..649492f804 100644 --- a/spec/connections/sqlite3_connection.rb +++ b/spec/connections/sqlite3_connection.rb @@ -1,6 +1,8 @@ require "activerecord" puts "Using native SQLite3" +ActiveRecord::Base.logger = Logger.new("debug.log") + db_file = "spec/fixtures/fixture_database.sqlite3" ActiveRecord::Base.configurations = { -- cgit v1.2.3 From 976fb01a5dd812bcb50cc7b576941c76657908f9 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 18 May 2009 01:50:50 -0400 Subject: Expand sql engine CRUD specs --- spec/arel/engines/sql/unit/engine_spec.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/arel/engines/sql/unit/engine_spec.rb b/spec/arel/engines/sql/unit/engine_spec.rb index 67a5d62320..c607abcfa1 100644 --- a/spec/arel/engines/sql/unit/engine_spec.rb +++ b/spec/arel/engines/sql/unit/engine_spec.rb @@ -3,32 +3,41 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') module Arel describe Sql::Engine do before do - @relation = Table.new(:users) + @users = Table.new(:users) + @users.delete end describe "CRUD" do describe "#create" do it "inserts into the relation" do - @relation.insert @relation[:name] => "Bryan" + @users.insert @users[:name] => "Bryan" + @users.first[@users[:name]].should == "Bryan" end end describe "#read" do it "reads from the relation" do - @relation.each do |row| + @users.insert @users[:name] => "Bryan" + + @users.each do |row| + row[@users[:name]].should == "Bryan" end end end describe "#update" do it "updates the relation" do - @relation.update @relation[:name] => "Bryan" + @users.insert @users[:name] => "Nick" + @users.update @users[:name] => "Bryan" + @users.first[@users[:name]].should == "Bryan" end end describe "#delete" do it "deletes from the relation" do - @relation.delete + @users.insert @users[:name] => "Bryan" + @users.delete + @users.first.should == nil end end end -- cgit v1.2.3 From 86364591af807ed3fa4a7304f53e6f3458cb4961 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Tue, 19 May 2009 21:36:34 -0400 Subject: Adding SqlLiteral with spec for counts --- .../engines/sql/unit/primitives/literal_spec.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 spec/arel/engines/sql/unit/primitives/literal_spec.rb (limited to 'spec') diff --git a/spec/arel/engines/sql/unit/primitives/literal_spec.rb b/spec/arel/engines/sql/unit/primitives/literal_spec.rb new file mode 100644 index 0000000000..c7ff1cf879 --- /dev/null +++ b/spec/arel/engines/sql/unit/primitives/literal_spec.rb @@ -0,0 +1,23 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe SqlLiteral do + before do + @relation = Table.new(:users) + end + + describe '#to_sql' do + it "manufactures sql with a literal SQL fragment" do + sql = @relation.project(Count.new(SqlLiteral.new("*"))).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{SELECT COUNT(*) AS count_id FROM `users`}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{SELECT COUNT(*) AS count_id FROM "users"}) + end + end + end + end +end -- cgit v1.2.3