aboutsummaryrefslogtreecommitdiffstats
path: root/spec/arel
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-04-18 12:59:29 -0700
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-04-18 12:59:29 -0700
commitd51139751eae2be6ee32b44edec39fcf09ed2333 (patch)
treec87d1bbb97cc5ba9f496f7c76fcf2fe7eaeee9f4 /spec/arel
parentd27ab7bb8ba0d8f136af2ed955d9e489ba45daec (diff)
downloadrails-d51139751eae2be6ee32b44edec39fcf09ed2333.tar.gz
rails-d51139751eae2be6ee32b44edec39fcf09ed2333.tar.bz2
rails-d51139751eae2be6ee32b44edec39fcf09ed2333.zip
officially renamed active_relation to arel
Diffstat (limited to 'spec/arel')
-rw-r--r--spec/arel/unit/predicates/binary_spec.rb71
-rw-r--r--spec/arel/unit/predicates/equality_spec.rb49
-rw-r--r--spec/arel/unit/predicates/in_spec.rb58
-rw-r--r--spec/arel/unit/primitives/attribute_spec.rb177
-rw-r--r--spec/arel/unit/primitives/expression_spec.rb45
-rw-r--r--spec/arel/unit/primitives/value_spec.rb28
-rw-r--r--spec/arel/unit/relations/alias_spec.rb16
-rw-r--r--spec/arel/unit/relations/compound_spec.rb31
-rw-r--r--spec/arel/unit/relations/deletion_spec.rb42
-rw-r--r--spec/arel/unit/relations/grouping_spec.rb33
-rw-r--r--spec/arel/unit/relations/insertion_spec.rb71
-rw-r--r--spec/arel/unit/relations/join_spec.rb169
-rw-r--r--spec/arel/unit/relations/order_spec.rb77
-rw-r--r--spec/arel/unit/relations/projection_spec.rb81
-rw-r--r--spec/arel/unit/relations/relation_spec.rb215
-rw-r--r--spec/arel/unit/relations/selection_spec.rb43
-rw-r--r--spec/arel/unit/relations/skip_spec.rb20
-rw-r--r--spec/arel/unit/relations/table_spec.rb95
-rw-r--r--spec/arel/unit/relations/take_spec.rb20
-rw-r--r--spec/arel/unit/relations/update_spec.rb81
-rw-r--r--spec/arel/unit/session/session_spec.rb92
21 files changed, 1514 insertions, 0 deletions
diff --git a/spec/arel/unit/predicates/binary_spec.rb b/spec/arel/unit/predicates/binary_spec.rb
new file mode 100644
index 0000000000..f39b37d913
--- /dev/null
+++ b/spec/arel/unit/predicates/binary_spec.rb
@@ -0,0 +1,71 @@
+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 '#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`
+ ")
+ 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
+ ")
+ 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'
+ ")
+ end
+ end
+ end
+ end
+
+ describe '==' do
+ it "obtains if attribute1 and attribute2 are identical" do
+ Binary.new(@attribute1, @attribute2).should == Binary.new(@attribute1, @attribute2)
+ Binary.new(@attribute1, @attribute2).should_not == Binary.new(@attribute1, @attribute1)
+ end
+
+ it "obtains if the concrete type of the predicates are identical" do
+ Binary.new(@attribute1, @attribute2).should == Binary.new(@attribute1, @attribute2)
+ Binary.new(@attribute1, @attribute2).should_not == ConcreteBinary.new(@attribute1, @attribute2)
+ end
+ end
+
+ describe '#bind' do
+ before do
+ @another_relation = Table.new(:photos)
+ end
+
+ it "descends" do
+ ConcreteBinary.new(@attribute1, @attribute2).bind(@another_relation). \
+ should == ConcreteBinary.new(@attribute1.bind(@another_relation), @attribute2.bind(@another_relation))
+ 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
new file mode 100644
index 0000000000..8a58ba3096
--- /dev/null
+++ b/spec/arel/unit/predicates/equality_spec.rb
@@ -0,0 +1,49 @@
+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
+ Equality.new(@attribute1, @attribute2).to_sql.should be_like("
+ `users`.`id` = `photos`.`user_id`
+ ")
+ 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
+ ")
+ 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
new file mode 100644
index 0000000000..797798a77f
--- /dev/null
+++ b/spec/arel/unit/predicates/in_spec.rb
@@ -0,0 +1,58 @@
+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
+ In.new(@attribute, @array).to_sql.should be_like("
+ `users`.`id` IN (1, 2, 3)
+ ")
+ 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)
+ ")
+ 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
+ ")
+ 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`)
+ ")
+ 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
new file mode 100644
index 0000000000..ac9afa85c7
--- /dev/null
+++ b/spec/arel/unit/primitives/attribute_spec.rb
@@ -0,0 +1,177 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Attribute do
+ before do
+ @relation = Table.new(:users)
+ @attribute = Attribute.new(@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.select(@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 '#qualified_name' do
+ it "manufactures an attribute name prefixed with the relation's name" do
+ @attribute.qualified_name.should == "#{@relation.prefix_for(@attribute)}.id"
+ 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
+
+ it "obtains if the attributes are identical" do
+ Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name)
+ end
+
+ it "obtains if the attributes have an overlapping history" do
+ Attribute.new(@relation, :name, :ancestor => Attribute.new(@relation, :name)).should =~ Attribute.new(@relation, :name)
+ Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name, :ancestor => Attribute.new(@relation, :name))
+ end
+ end
+
+ describe 'hashing' do
+ it "implements hash equality" do
+ Attribute.new(@relation, 'name').should hash_the_same_as(Attribute.new(@relation, 'name'))
+ Attribute.new(@relation, 'name').should_not hash_the_same_as(Attribute.new(@relation, 'id'))
+ 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`")
+ end
+ end
+
+ describe 'for an attribute in a join relation where the source relation is aliased' do
+ before do
+ another_relation = Table.new(:photos)
+ @join_with_alias = @relation.as(:alias).join(another_relation).on(@relation[:id].eq(another_relation[:user_id]))
+ end
+
+ it "manufactures sql with an alias" do
+ @join_with_alias[@attribute].to_sql.should be_like("`alias`.`id`")
+ 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 == 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 \ No newline at end of file
diff --git a/spec/arel/unit/primitives/expression_spec.rb b/spec/arel/unit/primitives/expression_spec.rb
new file mode 100644
index 0000000000..8a990231f6
--- /dev/null
+++ b/spec/arel/unit/primitives/expression_spec.rb
@@ -0,0 +1,45 @@
+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 = Expression.new(@attribute, "COUNT")
+ end
+
+ describe '#bind' do
+ it "manufactures an attribute with a rebound relation and self as the ancestor" do
+ derived_relation = @relation.select(@relation[:id].eq(1))
+ @expression.bind(derived_relation).should == Expression.new(@attribute.bind(derived_relation), "COUNT", 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, "COUNT", :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
+ Expression.new(@attribute, "COUNT", :alias).to_sql.should == "COUNT(`users`.`id`) AS `alias`"
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/spec/arel/unit/primitives/value_spec.rb b/spec/arel/unit/primitives/value_spec.rb
new file mode 100644
index 0000000000..87425c780d
--- /dev/null
+++ b/spec/arel/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 \ No newline at end of file
diff --git a/spec/arel/unit/relations/alias_spec.rb b/spec/arel/unit/relations/alias_spec.rb
new file mode 100644
index 0000000000..420a91c806
--- /dev/null
+++ b/spec/arel/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)
+ @alias_relation = Alias.new(@relation, :foo)
+ end
+
+ describe '#alias' do
+ it "returns the alias" do
+ @alias_relation.alias.should == :foo
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/spec/arel/unit/relations/compound_spec.rb b/spec/arel/unit/relations/compound_spec.rb
new file mode 100644
index 0000000000..763e447db3
--- /dev/null
+++ b/spec/arel/unit/relations/compound_spec.rb
@@ -0,0 +1,31 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Compound do
+ before do
+ class ConcreteCompound < Compound
+ def initialize(relation)
+ @relation = relation
+ end
+
+ def ==(other)
+ true
+ end
+ end
+ @relation = Table.new(:users)
+ @compound_relation = ConcreteCompound.new(@relation)
+ end
+
+ describe '#attributes' do
+ it 'manufactures attributes associated with the compound relation' do
+ @compound_relation.attributes.should == @relation.attributes.collect { |a| a.bind(@compound_relation) }
+ end
+ end
+
+ describe 'hashing' do
+ it 'implements hash equality' do
+ ConcreteCompound.new(@relation).should hash_the_same_as(ConcreteCompound.new(@relation))
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/spec/arel/unit/relations/deletion_spec.rb b/spec/arel/unit/relations/deletion_spec.rb
new file mode 100644
index 0000000000..f975720d83
--- /dev/null
+++ b/spec/arel/unit/relations/deletion_spec.rb
@@ -0,0 +1,42 @@
+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
+ Deletion.new(@relation).to_sql.should be_like("
+ DELETE
+ FROM `users`
+ ")
+ end
+
+ it 'manufactures sql deleting a selection relation' do
+ Deletion.new(@relation.select(@relation[:id].eq(1))).to_sql.should be_like("
+ DELETE
+ FROM `users`
+ WHERE `users`.`id` = 1
+ ")
+ end
+
+ it "manufactures sql deleting a ranged relation" do
+ Deletion.new(@relation.take(1)).to_sql.should be_like("
+ DELETE
+ FROM `users`
+ LIMIT 1
+ ")
+ 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 \ No newline at end of file
diff --git a/spec/arel/unit/relations/grouping_spec.rb b/spec/arel/unit/relations/grouping_spec.rb
new file mode 100644
index 0000000000..bef3566896
--- /dev/null
+++ b/spec/arel/unit/relations/grouping_spec.rb
@@ -0,0 +1,33 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Grouping 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
+ Grouping.new(@relation, @attribute).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ GROUP BY `users`.`id`
+ ")
+ end
+ end
+
+ describe 'when given a string' do
+ it "passes the string through to the where clause" do
+ pending 'it should not quote asdf'
+ Grouping.new(@relation, 'asdf').to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ GROUP BY asdf
+ ")
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/spec/arel/unit/relations/insertion_spec.rb b/spec/arel/unit/relations/insertion_spec.rb
new file mode 100644
index 0000000000..10b70a2036
--- /dev/null
+++ b/spec/arel/unit/relations/insertion_spec.rb
@@ -0,0 +1,71 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Insertion 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'
+ @insertion = Insertion.new(@relation, [@relation[:name] => "nick", @relation[:name] => "bryan"])
+
+ @insertion.to_sql.should be_like("
+ INSERT
+ INTO `users`
+ (`users`.`name`) VALUES ('nick'), ('bryan')
+ ")
+ end
+
+ it 'manufactures sql inserting data when given multiple values' do
+ @insertion = Insertion.new(@relation, @relation[:id] => "1", @relation[:name] => "nick")
+
+ @insertion.to_sql.should be_like("
+ INSERT
+ INTO `users`
+ (`users`.`id`, `users`.`name`) VALUES (1, 'nick')
+ ")
+ end
+
+ describe 'when given values whose types correspond to the types of the attributes' do
+ before do
+ @insertion = Insertion.new(@relation, @relation[:name] => "nick")
+ end
+
+ it 'manufactures sql inserting data' do
+ @insertion.to_sql.should be_like("
+ INSERT
+ INTO `users`
+ (`users`.`name`) VALUES ('nick')
+ ")
+ end
+ end
+
+ describe 'when given values whose types differ from from the types of the attributes' do
+ before do
+ @insertion = Insertion.new(@relation, @relation[:id] => '1-asdf')
+ end
+
+ it 'manufactures sql inserting data' do
+ @insertion.to_sql.should be_like("
+ INSERT
+ INTO `users`
+ (`users`.`id`) VALUES (1)
+ ")
+ end
+ end
+ end
+
+ describe '#call' do
+ before do
+ @insertion = Insertion.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 \ No newline at end of file
diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb
new file mode 100644
index 0000000000..f9d0d9561e
--- /dev/null
+++ b/spec/arel/unit/relations/join_spec.rb
@@ -0,0 +1,169 @@
+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 '==' do
+ before do
+ @another_predicate = @relation1[:id].eq(1)
+ @another_relation = Table.new(:cameras)
+ end
+
+ it 'obtains if the two relations and the predicate are identical' do
+ Join.new("INNER JOIN", @relation1, @relation2, @predicate).should == Join.new("INNER JOIN", @relation1, @relation2, @predicate)
+ Join.new("INNER JOIN", @relation1, @relation2, @predicate).should_not == Join.new("INNER JOIN", @relation1, @another_relation, @predicate)
+ Join.new("INNER JOIN", @relation1, @relation2, @predicate).should_not == Join.new("INNER JOIN", @relation1, @relation2, @another_predicate)
+ end
+
+ it 'is commutative on the relations' do
+ Join.new("INNER JOIN", @relation1, @relation2, @predicate).should == Join.new("INNER JOIN", @relation2, @relation1, @predicate)
+ end
+ 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 '#prefix_for' do
+ describe 'when the joined relations are simple' do
+ it "returns the name of the relation containing the attribute" do
+ Join.new("INNER JOIN", @relation1, @relation2, @predicate).prefix_for(@relation1[:id]) \
+ .should == @relation1.prefix_for(@relation1[:id])
+ Join.new("INNER JOIN", @relation1, @relation2, @predicate).prefix_for(@relation2[:id]) \
+ .should == @relation2.prefix_for(@relation2[:id])
+
+ end
+ end
+
+ describe 'when one of the joined relations is an alias' do
+ before do
+ @aliased_relation = @relation1.as(:alias)
+ end
+
+ it "returns the alias of the relation containing the attribute" do
+ Join.new("INNER JOIN", @aliased_relation, @relation2, @predicate).prefix_for(@aliased_relation[:id]) \
+ .should == @aliased_relation.alias
+ Join.new("INNER JOIN", @aliased_relation, @relation2, @predicate).prefix_for(@relation2[:id]) \
+ .should == @relation2.prefix_for(@relation2[:id])
+ end
+ 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 'when joining simple relations' do
+ describe '#attributes' do
+ it 'combines the attributes of the two relations' do
+ join = Join.new("INNER JOIN", @relation1, @relation2, @predicate)
+ join.attributes.should ==
+ (@relation1.attributes + @relation2.attributes).collect { |a| a.bind(join) }
+ end
+ end
+
+ describe '#to_sql' 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`
+ ")
+ end
+
+ it 'manufactures sql joining the two tables, merging any selects' do
+ Join.new("INNER JOIN", @relation1.select(@relation1[:id].eq(1)),
+ @relation2.select(@relation2[:id].eq(2)), @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`
+ WHERE `users`.`id` = 1
+ AND `photos`.`id` = 2
+ ")
+ end
+ end
+ end
+
+ describe 'when joining aggregated relations' do
+ before do
+ @aggregation = @relation2 \
+ .group(@relation2[:user_id]) \
+ .project(@relation2[:user_id], @relation2[:id].count.as(:cnt)) \
+ .as('photo_count')
+ end
+
+ describe '#attributes' do
+ it 'it transforms aggregate expressions into attributes' do
+ join_with_aggregation = Join.new("INNER JOIN", @relation1, @aggregation, @predicate)
+ join_with_aggregation.attributes.should ==
+ (@relation1.attributes + @aggregation.attributes).collect(&:to_attribute).collect { |a| a.bind(join_with_aggregation) }
+ end
+ end
+
+ describe '#to_sql' do
+ describe 'with the aggregation on the right' do
+ it 'manufactures sql joining the left table to a derived table' do
+ Join.new("INNER JOIN", @relation1, @aggregation, @predicate).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`, `photo_count`.`user_id`, `photo_count`.`cnt`
+ FROM `users`
+ INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photo_count`
+ ON `users`.`id` = `photo_count`.`user_id`
+ ")
+ end
+ end
+
+ describe 'with the aggregation on the left' do
+ it 'manufactures sql joining the right table to a derived table' do
+ Join.new("INNER JOIN", @aggregation, @relation1, @predicate).to_sql.should be_like("
+ SELECT `photo_count`.`user_id`, `photo_count`.`cnt`, `users`.`id`, `users`.`name`
+ FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photo_count`
+ INNER JOIN `users`
+ ON `users`.`id` = `photo_count`.`user_id`
+ ")
+ end
+ end
+
+ it "keeps selects on the aggregation within the derived table" do
+ Join.new("INNER JOIN", @relation1, @aggregation.select(@aggregation[:user_id].eq(1)), @predicate).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`, `photo_count`.`user_id`, `photo_count`.`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 `photo_count`
+ ON `users`.`id` = `photo_count`.`user_id`
+ ")
+ end
+ end
+ end
+
+ describe 'when joining aliased relations' do
+ it 'aliases the table and attributes properly' do
+ aliased_relation = @relation1.as(:alias)
+ @relation1.join(aliased_relation).on(@relation1[:id].eq(aliased_relation[:id])).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`, `alias`.`id`, `alias`.`name`
+ FROM `users`
+ INNER JOIN `users` AS `alias`
+ ON `users`.`id` = `alias`.`id`
+ ")
+ 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
+ ")
+ 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
new file mode 100644
index 0000000000..838a2f141e
--- /dev/null
+++ b/spec/arel/unit/relations/order_spec.rb
@@ -0,0 +1,77 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Order do
+ before do
+ @relation = Table.new(:users)
+ @attribute = @relation[:id]
+ end
+
+ describe '#initialize' do
+ before do
+ @another_attribtue = @relation[:name]
+ end
+
+ it "manufactures nested Order relations if multiple predicates are provided" do
+ Order.new(@relation, @predicate, @another_attribute). \
+ should == Order.new(Order.new(@relation, @another_attribute), @predicate)
+ end
+ end
+
+ 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`
+ ")
+ 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`
+ ")
+ 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
+ ")
+ 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 an order clause populated by comma-separated attributes" do
+ Order.new(@ordered_relation, @another_attribute).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ ORDER BY `users`.`id`, `users`.`name`
+ ")
+ end
+ end
+ end
+ end
+end
+ \ No newline at end of file
diff --git a/spec/arel/unit/relations/projection_spec.rb b/spec/arel/unit/relations/projection_spec.rb
new file mode 100644
index 0000000000..cd59d6c383
--- /dev/null
+++ b/spec/arel/unit/relations/projection_spec.rb
@@ -0,0 +1,81 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Projection do
+ before do
+ @relation = Table.new(:users)
+ @attribute = @relation[:id]
+ end
+
+ describe '#attributes' do
+ before do
+ @projection = Projection.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 '==' do
+ before do
+ @another_relation = Table.new(:photos)
+ @another_attribute = @relation[:name]
+ end
+
+ it "obtains if the relations and attributes are identical" do
+ Projection.new(@relation, @attribute).should == Projection.new(@relation, @attribute)
+ Projection.new(@relation, @attribute).should_not == Projection.new(@another_relation, @attribute)
+ Projection.new(@relation, @attribute).should_not == Projection.new(@relation, @another_attribute)
+ end
+ end
+
+ describe '#to_sql' do
+ describe 'when given an attribute' do
+ it "manufactures sql with a limited select clause" do
+ Projection.new(@relation, @attribute).to_sql.should be_like("
+ SELECT `users`.`id`
+ FROM `users`
+ ")
+ end
+ end
+
+ describe 'when given a relation' do
+ before do
+ @scalar_relation = Projection.new(@relation, @relation[:name])
+ end
+
+ it "manufactures sql with scalar selects" do
+ Projection.new(@relation, @scalar_relation).to_sql.should be_like("
+ SELECT (SELECT `users`.`name` FROM `users`) FROM `users`
+ ")
+ end
+ end
+
+ describe 'when given a string' do
+ it "passes the string through to the select clause" do
+ Projection.new(@relation, 'asdf').to_sql.should be_like("
+ SELECT asdf FROM `users`
+ ")
+ end
+ end
+ end
+
+ describe Projection::Externalizable do
+ describe '#aggregation?' do
+ describe 'when the projections are attributes' do
+ it 'returns false' do
+ Projection.new(@relation, @attribute).should_not be_aggregation
+ end
+ end
+
+ describe 'when the projections include an aggregation' do
+ it "obtains" do
+ Projection.new(@relation, @attribute.sum).should be_aggregation
+ end
+ end
+ end
+
+ end
+ end
+end \ No newline at end of file
diff --git a/spec/arel/unit/relations/relation_spec.rb b/spec/arel/unit/relations/relation_spec.rb
new file mode 100644
index 0000000000..46c3ee250d
--- /dev/null
+++ b/spec/arel/unit/relations/relation_spec.rb
@@ -0,0 +1,215 @@
+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::Externalizable do
+ describe '#aggregation?' do
+ it "returns false" do
+ @relation.should_not be_aggregation
+ end
+ end
+
+ describe '#alias?' do
+ it "returns false" do
+ @relation.should_not be_alias
+ end
+ end
+ end
+
+ describe Relation::Operations 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 == Join.new("INNER JOIN", @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 == Join.new("LEFT OUTER JOIN", @relation, @relation, @predicate)
+ end
+ end
+ end
+
+ describe '#project' do
+ it "manufactures a projection relation" do
+ @relation.project(@attribute1, @attribute2). \
+ should == Projection.new(@relation, @attribute1, @attribute2)
+ end
+
+ describe "when given blank attributes" do
+ it "returns self" do
+ @relation.project.should == @relation
+ end
+ end
+ end
+
+ describe '#as' do
+ it "manufactures an alias relation" do
+ @relation.as(:paul).should == Alias.new(@relation, :paul)
+ end
+
+ describe 'when given a blank alias' do
+ it 'returns self' do
+ @relation.as.should == @relation
+ end
+ end
+ end
+
+ describe '#select' do
+ before do
+ @predicate = Equality.new(@attribute1, @attribute2)
+ end
+
+ it "manufactures a selection relation" do
+ @relation.select(@predicate).should == Selection.new(@relation, @predicate)
+ end
+
+ it "accepts arbitrary strings" do
+ @relation.select("arbitrary").should == Selection.new(@relation, "arbitrary")
+ end
+
+ describe 'when given a blank predicate' do
+ it 'returns self' do
+ @relation.select.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 == Grouping.new(@relation, @attribute1, @attribute2)
+ end
+
+ describe 'when given blank groupings' do
+ it 'returns self' do
+ @relation.group.should == @relation
+ end
+ end
+ end
+
+ describe Relation::Operations::Writes do
+ describe '#delete' do
+ it 'manufactures a deletion relation' do
+ Session.start do
+ mock(Session.new).delete(Deletion.new(@relation))
+ @relation.delete.should == @relation
+ end
+ end
+ end
+
+ describe '#insert' do
+ it 'manufactures an insertion relation' do
+ Session.start do
+ record = {@relation[:name] => 'carl'}
+ mock(Session.new).create(Insertion.new(@relation, record))
+ @relation.insert(record).should == @relation
+ 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).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
+
+ describe '#call' do
+ it 'executes a select_all on the connection' do
+ mock(connection = Object.new).select_all(@relation.to_sql)
+ @relation.call(connection)
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/spec/arel/unit/relations/selection_spec.rb b/spec/arel/unit/relations/selection_spec.rb
new file mode 100644
index 0000000000..61d9787df2
--- /dev/null
+++ b/spec/arel/unit/relations/selection_spec.rb
@@ -0,0 +1,43 @@
+require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
+
+module Arel
+ describe Selection do
+ before do
+ @relation = Table.new(:users)
+ @predicate = @relation[:id].eq(1)
+ end
+
+ describe '#initialize' do
+ before do
+ @another_predicate = @relation[:name].lt(2)
+ end
+
+ it "manufactures nested selection relations if multiple predicates are provided" do
+ Selection.new(@relation, @predicate, @another_predicate). \
+ should == Selection.new(Selection.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
+ Selection.new(@relation, @predicate).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ WHERE `users`.`id` = 1
+ ")
+ end
+ end
+
+ describe 'when given a string' do
+ it "passes the string through to the where clause" do
+ Selection.new(@relation, 'asdf').to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ WHERE asdf
+ ")
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/spec/arel/unit/relations/skip_spec.rb b/spec/arel/unit/relations/skip_spec.rb
new file mode 100644
index 0000000000..d83c969aa8
--- /dev/null
+++ b/spec/arel/unit/relations/skip_spec.rb
@@ -0,0 +1,20 @@
+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
+ Skip.new(@relation, @skipped).to_s.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ OFFSET #{@skipped}
+ ")
+ 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
new file mode 100644
index 0000000000..eb765ce777
--- /dev/null
+++ b/spec/arel/unit/relations/table_spec.rb
@@ -0,0 +1,95 @@
+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
+ @relation.to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ ")
+ 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 '#prefix_for' do
+ it "returns the table name if the relation contains the attribute" do
+ @relation.prefix_for(@relation[:id]).should == 'users'
+ @relation.prefix_for(:does_not_exist).should be_nil
+ 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 = Engine.new
+ Table.new(:users).engine.should == engine
+ end
+
+ it "can be specified" do
+ Table.new(:users, engine = 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
new file mode 100644
index 0000000000..dca7806057
--- /dev/null
+++ b/spec/arel/unit/relations/take_spec.rb
@@ -0,0 +1,20 @@
+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
+ Take.new(@relation, @taken).to_s.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ LIMIT #{@taken}
+ ")
+ 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
new file mode 100644
index 0000000000..f411781392
--- /dev/null
+++ b/spec/arel/unit/relations/update_spec.rb
@@ -0,0 +1,81 @@
+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
+ Update.new(@relation, @relation[:id] => 1, @relation[:name] => "nick").to_sql.should be_like("
+ UPDATE `users`
+ SET `users`.`id` = 1, `users`.`name` = 'nick'
+ ")
+ 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 `users`.`name` = 'nick'
+ LIMIT 1
+ ")
+ 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
+ @update.to_sql.should be_like("
+ UPDATE `users`
+ SET `users`.`name` = 'nick'
+ ")
+ 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
+ @update.to_sql.should be_like("
+ UPDATE `users`
+ SET `users`.`id` = 1
+ ")
+ end
+ end
+
+ describe 'when the relation is a selection' do
+ before do
+ @update = Update.new(
+ @relation.select(@relation[:id].eq(1)),
+ @relation[:name] => "nick"
+ )
+ end
+
+ it 'manufactures sql updating a selection relation' do
+ @update.to_sql.should be_like("
+ UPDATE `users`
+ SET `users`.`name` = 'nick'
+ WHERE `users`.`id` = 1
+ ")
+ end
+ 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 \ No newline at end of file
diff --git a/spec/arel/unit/session/session_spec.rb b/spec/arel/unit/session/session_spec.rb
new file mode 100644
index 0000000000..c2eb9a4555
--- /dev/null
+++ b/spec/arel/unit/session/session_spec.rb
@@ -0,0 +1,92 @@
+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 = Insertion.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(@insert.engine.connection)
+ @session.create(@insert)
+ end
+ end
+
+ describe '#read' do
+ it "executes an selection on the connection" do
+ mock(@read).call(@read.engine.connection)
+ @session.read(@read)
+ end
+
+ it "is memoized" do
+ mock(@read).call(@read.engine.connection).once
+ @session.read(@read)
+ @session.read(@read)
+ end
+ end
+
+ describe '#update' do
+ it "executes an update on the connection" do
+ mock(@update).call(@update.engine.connection)
+ @session.update(@update)
+ end
+ end
+
+ describe '#delete' do
+ it "executes a delete on the connection" do
+ mock(@delete).call(@delete.engine.connection)
+ @session.delete(@delete)
+ end
+ end
+ end
+
+ describe Session::Transactions do
+ describe '#begin' do
+ end
+
+ describe '#end' do
+ end
+ end
+
+ describe Session::UnitOfWork do
+ describe '#flush' do
+ end
+
+ describe '#clear' do
+ end
+ end
+ end
+end \ No newline at end of file