aboutsummaryrefslogtreecommitdiffstats
path: root/spec/arel/engines
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-05-26 21:02:23 -0700
committerBryan Helmkamp <bryan@brynary.com>2009-05-17 15:36:05 -0400
commita7aacd29460aa137c3b06ee214ff8ffdff8ee365 (patch)
treee88f8894c830b4f95822ecb970294ca4c793a722 /spec/arel/engines
parent9d77c08cf8a75636b058c1b85af52ef96e07cee5 (diff)
downloadrails-a7aacd29460aa137c3b06ee214ff8ffdff8ee365.tar.gz
rails-a7aacd29460aa137c3b06ee214ff8ffdff8ee365.tar.bz2
rails-a7aacd29460aa137c3b06ee214ff8ffdff8ee365.zip
reorganizing tests
Diffstat (limited to 'spec/arel/engines')
-rw-r--r--spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb209
-rw-r--r--spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb167
-rw-r--r--spec/arel/engines/sql/integration/joins/with_compounds_spec.rb107
-rw-r--r--spec/arel/engines/sql/unit/predicates/binary_spec.rb125
-rw-r--r--spec/arel/engines/sql/unit/predicates/equality_spec.rb61
-rw-r--r--spec/arel/engines/sql/unit/predicates/in_spec.rb86
-rw-r--r--spec/arel/engines/sql/unit/primitives/attribute_spec.rb171
-rw-r--r--spec/arel/engines/sql/unit/primitives/expression_spec.rb53
-rw-r--r--spec/arel/engines/sql/unit/primitives/value_spec.rb28
-rw-r--r--spec/arel/engines/sql/unit/relations/alias_spec.rb50
-rw-r--r--spec/arel/engines/sql/unit/relations/array_spec.rb87
-rw-r--r--spec/arel/engines/sql/unit/relations/delete_spec.rb63
-rw-r--r--spec/arel/engines/sql/unit/relations/group_spec.rb56
-rw-r--r--spec/arel/engines/sql/unit/relations/insert_spec.rb91
-rw-r--r--spec/arel/engines/sql/unit/relations/join_spec.rb78
-rw-r--r--spec/arel/engines/sql/unit/relations/order_spec.rb113
-rw-r--r--spec/arel/engines/sql/unit/relations/project_spec.rb134
-rw-r--r--spec/arel/engines/sql/unit/relations/relation_spec.rb169
-rw-r--r--spec/arel/engines/sql/unit/relations/skip_spec.rb32
-rw-r--r--spec/arel/engines/sql/unit/relations/table_spec.rb99
-rw-r--r--spec/arel/engines/sql/unit/relations/take_spec.rb32
-rw-r--r--spec/arel/engines/sql/unit/relations/update_spec.rb121
-rw-r--r--spec/arel/engines/sql/unit/relations/where_spec.rb64
-rw-r--r--spec/arel/engines/sql/unit/session/session_spec.rb84
24 files changed, 2280 insertions, 0 deletions
diff --git a/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb b/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb
new file mode 100644
index 0000000000..50b0908441
--- /dev/null
+++ b/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb
@@ -0,0 +1,209 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Join do
+ before do
+ @relation1 = Table(:users)
+ @relation2 = @relation1.alias
+ @predicate = @relation1[:id].eq(@relation2[:id])
+ end
+
+ describe 'when joining a relation to itself' do
+ describe '#to_sql' do
+ it 'manufactures sql aliasing the table and attributes properly in the join predicate and the where clause' do
+ sql = @relation1.join(@relation2).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
+ FROM `users`
+ INNER JOIN `users` AS `users_2`
+ ON `users`.`id` = `users_2`.`id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
+ FROM "users"
+ INNER JOIN "users" AS "users_2"
+ ON "users"."id" = "users_2"."id"
+ })
+ end
+ end
+
+ describe 'when joining with a where on the same relation' do
+ it 'manufactures sql aliasing the tables properly' do
+ sql = @relation1 \
+ .join(@relation2.where(@relation2[:id].eq(1))) \
+ .on(@predicate) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
+ FROM `users`
+ INNER JOIN `users` AS `users_2`
+ ON `users`.`id` = `users_2`.`id` AND `users_2`.`id` = 1
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
+ FROM "users"
+ INNER JOIN "users" AS "users_2"
+ ON "users"."id" = "users_2"."id" AND "users_2"."id" = 1
+ })
+ end
+ end
+
+ describe 'when the where occurs before the alias' do
+ it 'manufactures sql aliasing the predicates properly' do
+ relation2 = @relation1.where(@relation1[:id].eq(1)).alias
+
+ sql = @relation1 \
+ .join(relation2) \
+ .on(relation2[:id].eq(@relation1[:id])) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
+ FROM `users`
+ INNER JOIN `users` AS `users_2`
+ ON `users_2`.`id` = `users`.`id` AND `users_2`.`id` = 1
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
+ FROM "users"
+ INNER JOIN "users" AS "users_2"
+ ON "users_2"."id" = "users"."id" AND "users_2"."id" = 1
+ })
+ end
+ end
+ end
+ end
+
+ describe 'when joining the relation to itself multiple times' do
+ before do
+ @relation3 = @relation1.alias
+ end
+
+ describe 'when joining left-associatively' do
+ it 'manufactures sql aliasing the tables properly' do
+ sql = @relation1 \
+ .join(@relation2 \
+ .join(@relation3) \
+ .on(@relation2[:id].eq(@relation3[:id]))) \
+ .on(@relation1[:id].eq(@relation2[:id])) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name`
+ FROM `users`
+ INNER JOIN `users` AS `users_2`
+ ON `users`.`id` = `users_2`.`id`
+ INNER JOIN `users` AS `users_3`
+ ON `users_2`.`id` = `users_3`.`id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name", "users_3"."id", "users_3"."name"
+ FROM "users"
+ INNER JOIN "users" AS "users_2"
+ ON "users"."id" = "users_2"."id"
+ INNER JOIN "users" AS "users_3"
+ ON "users_2"."id" = "users_3"."id"
+ })
+ end
+ end
+ end
+
+ describe 'when joining right-associatively' do
+ it 'manufactures sql aliasing the tables properly' do
+ sql = @relation1 \
+ .join(@relation2).on(@relation1[:id].eq(@relation2[:id])) \
+ .join(@relation3).on(@relation2[:id].eq(@relation3[:id])) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name`
+ FROM `users`
+ INNER JOIN `users` AS `users_2`
+ ON `users`.`id` = `users_2`.`id`
+ INNER JOIN `users` AS `users_3`
+ ON `users_2`.`id` = `users_3`.`id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name", "users_3"."id", "users_3"."name"
+ FROM "users"
+ INNER JOIN "users" AS "users_2"
+ ON "users"."id" = "users_2"."id"
+ INNER JOIN "users" AS "users_3"
+ ON "users_2"."id" = "users_3"."id"
+ })
+ end
+ end
+ end
+ end
+ end
+
+ describe '[]' do
+ describe 'when given an attribute belonging to both sub-relations' do
+ it 'disambiguates the relation that serves as the ancestor to the attribute' do
+ @relation1 \
+ .join(@relation2) \
+ .on(@predicate) \
+ .should disambiguate_attributes(@relation1[:id], @relation2[:id])
+ end
+
+ describe 'when both relations are compound and only one is an alias' do
+ it 'disambiguates the relation that serves as the ancestor to the attribute' do
+ compound1 = @relation1.where(@predicate)
+ compound2 = compound1.alias
+ compound1 \
+ .join(compound2) \
+ .on(@predicate) \
+ .should disambiguate_attributes(compound1[:id], compound2[:id])
+ end
+ end
+
+ describe 'when the left relation is extremely compound' do
+ it 'disambiguates the relation that serves as the ancestor to the attribute' do
+ @relation1 \
+ .where(@predicate) \
+ .where(@predicate) \
+ .join(@relation2) \
+ .on(@predicate) \
+ .should disambiguate_attributes(@relation1[:id], @relation2[:id])
+ end
+ end
+
+ describe 'when the right relation is extremely compound' do
+ it 'disambiguates the relation that serves as the ancestor to the attribute' do
+ @relation1 \
+ .join( \
+ @relation2 \
+ .where(@predicate) \
+ .where(@predicate) \
+ .where(@predicate)) \
+ .on(@predicate) \
+ .should disambiguate_attributes(@relation1[:id], @relation2[:id])
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb b/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb
new file mode 100644
index 0000000000..709ae9f8d1
--- /dev/null
+++ b/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb
@@ -0,0 +1,167 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Join do
+ before do
+ @relation1 = Table(:users)
+ @relation2 = Table(:photos)
+ @predicate = @relation1[:id].eq(@relation2[:user_id])
+ end
+
+ describe 'when joining aggregated relations' do
+ before do
+ @aggregation = @relation2 \
+ .group(@relation2[:user_id]) \
+ .project(@relation2[:user_id], @relation2[:id].count.as(:cnt)) \
+ end
+
+ describe '#to_sql' do
+ # CLEANUP
+ it '' do
+ sql = @relation1.join(@relation2.take(3)).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos_external`.`id`, `photos_external`.`user_id`, `photos_external`.`camera_id`
+ FROM `users`
+ INNER JOIN (SELECT `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id` FROM `photos` LIMIT 3) AS `photos_external`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos_external"."id", "photos_external"."user_id", "photos_external"."camera_id"
+ FROM "users"
+ INNER JOIN (SELECT "photos"."id", "photos"."user_id", "photos"."camera_id" FROM "photos" LIMIT 3) AS "photos_external"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+
+ describe 'with the aggregation on the right' do
+ it 'manufactures sql joining the left table to a derived table' do
+ sql = @relation1.join(@aggregation).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos_external`.`user_id`, `photos_external`.`cnt`
+ FROM `users`
+ INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos_external"."user_id", "photos_external"."cnt"
+ FROM "users"
+ INNER JOIN (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+
+ describe 'with the aggregation on the left' do
+ it 'manufactures sql joining the right table to a derived table' do
+ sql = @aggregation.join(@relation1).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `photos_external`.`user_id`, `photos_external`.`cnt`, `users`.`id`, `users`.`name`
+ FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external`
+ INNER JOIN `users`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "photos_external"."user_id", "photos_external"."cnt", "users"."id", "users"."name"
+ FROM (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external"
+ INNER JOIN "users"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+
+ describe 'with the aggregation on both sides' do
+ it 'it properly aliases the aggregations' do
+ aggregation2 = @aggregation.alias
+ sql = @aggregation.join(aggregation2).on(aggregation2[:user_id].eq(@aggregation[:user_id])).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `photos_external`.`user_id`, `photos_external`.`cnt`, `photos_external_2`.`user_id`, `photos_external_2`.`cnt`
+ FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external`
+ INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external_2`
+ ON `photos_external_2`.`user_id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "photos_external"."user_id", "photos_external"."cnt", "photos_external_2"."user_id", "photos_external_2"."cnt"
+ FROM (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external"
+ INNER JOIN (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external_2"
+ ON "photos_external_2"."user_id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+
+ describe 'when the aggration has a where' do
+ describe 'with the aggregation on the left' do
+ it "manufactures sql keeping wheres on the aggregation within the derived table" do
+ sql = @relation1.join(@aggregation.where(@aggregation[:user_id].eq(1))).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos_external`.`user_id`, `photos_external`.`cnt`
+ FROM `users`
+ INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` WHERE `photos`.`user_id` = 1 GROUP BY `photos`.`user_id`) AS `photos_external`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos_external"."user_id", "photos_external"."cnt"
+ FROM "users"
+ INNER JOIN (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" WHERE "photos"."user_id" = 1 GROUP BY "photos"."user_id") AS "photos_external"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+
+ describe 'with the aggregation on the right' do
+ it "manufactures sql keeping wheres on the aggregation within the derived table" do
+ sql = @aggregation.where(@aggregation[:user_id].eq(1)).join(@relation1).on(@predicate).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `photos_external`.`user_id`, `photos_external`.`cnt`, `users`.`id`, `users`.`name`
+ FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` WHERE `photos`.`user_id` = 1 GROUP BY `photos`.`user_id`) AS `photos_external`
+ INNER JOIN `users`
+ ON `users`.`id` = `photos_external`.`user_id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "photos_external"."user_id", "photos_external"."cnt", "users"."id", "users"."name"
+ FROM (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" WHERE "photos"."user_id" = 1 GROUP BY "photos"."user_id") AS "photos_external"
+ INNER JOIN "users"
+ ON "users"."id" = "photos_external"."user_id"
+ })
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb b/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb
new file mode 100644
index 0000000000..4bceef4975
--- /dev/null
+++ b/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb
@@ -0,0 +1,107 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Join do
+ before do
+ @relation1 = Table(:users)
+ @relation2 = Table(:photos)
+ @predicate = @relation1[:id].eq(@relation2[:user_id])
+ end
+
+ describe '#to_sql' do
+ describe 'when the join contains a where' do
+ describe 'and the where is given a string' do
+ it 'does not escape the string' do
+ sql = @relation1 \
+ .join(@relation2.where("asdf")) \
+ .on(@predicate) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
+ FROM `users`
+ INNER JOIN `photos`
+ ON `users`.`id` = `photos`.`user_id` AND asdf
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos"."id", "photos"."user_id", "photos"."camera_id"
+ FROM "users"
+ INNER JOIN "photos"
+ ON "users"."id" = "photos"."user_id" AND asdf
+ })
+ end
+ end
+ end
+ end
+
+ describe 'when a compound contains a join' do
+ describe 'and the compound is a where' do
+ it 'manufactures sql disambiguating the tables' do
+ sql = @relation1 \
+ .where(@relation1[:id].eq(1)) \
+ .join(@relation2) \
+ .on(@predicate) \
+ .where(@relation1[:id].eq(1)) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
+ FROM `users`
+ INNER JOIN `photos`
+ ON `users`.`id` = `photos`.`user_id`
+ WHERE `users`.`id` = 1
+ AND `users`.`id` = 1
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos"."id", "photos"."user_id", "photos"."camera_id"
+ FROM "users"
+ INNER JOIN "photos"
+ ON "users"."id" = "photos"."user_id"
+ WHERE "users"."id" = 1
+ AND "users"."id" = 1
+ })
+ end
+ end
+ end
+
+ describe 'and the compound is a group' do
+ it 'manufactures sql disambiguating the tables' do
+ sql = @relation1 \
+ .join(@relation2) \
+ .on(@predicate) \
+ .group(@relation1[:id]) \
+ .to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{
+ SELECT `users`.`id`, `users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
+ FROM `users`
+ INNER JOIN `photos`
+ ON `users`.`id` = `photos`.`user_id`
+ GROUP BY `users`.`id`
+ })
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{
+ SELECT "users"."id", "users"."name", "photos"."id", "photos"."user_id", "photos"."camera_id"
+ FROM "users"
+ INNER JOIN "photos"
+ ON "users"."id" = "photos"."user_id"
+ GROUP BY "users"."id"
+ })
+ end
+ end
+ end
+ end
+ end
+ end
+end
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