aboutsummaryrefslogtreecommitdiffstats
path: root/spec/algebra/unit/relations
diff options
context:
space:
mode:
authorCarl Lerche <carllerche@mac.com>2010-03-12 14:46:37 -0800
committerCarl Lerche <carllerche@mac.com>2010-03-12 14:46:37 -0800
commite13420c86afb5c31e90cff800f121bd49255b939 (patch)
tree7089d188061b2a676143add52227e107a5cf9b45 /spec/algebra/unit/relations
parent0b8b87fb947a746d4e58d11ea73ef20cfb23f576 (diff)
downloadrails-e13420c86afb5c31e90cff800f121bd49255b939.tar.gz
rails-e13420c86afb5c31e90cff800f121bd49255b939.tar.bz2
rails-e13420c86afb5c31e90cff800f121bd49255b939.zip
We're obviously writing specs for arel. No need for a sub directory.
Diffstat (limited to 'spec/algebra/unit/relations')
-rw-r--r--spec/algebra/unit/relations/alias_spec.rb16
-rw-r--r--spec/algebra/unit/relations/delete_spec.rb9
-rw-r--r--spec/algebra/unit/relations/group_spec.rb10
-rw-r--r--spec/algebra/unit/relations/insert_spec.rb9
-rw-r--r--spec/algebra/unit/relations/join_spec.rb26
-rw-r--r--spec/algebra/unit/relations/order_spec.rb21
-rw-r--r--spec/algebra/unit/relations/project_spec.rb34
-rw-r--r--spec/algebra/unit/relations/relation_spec.rb187
-rw-r--r--spec/algebra/unit/relations/skip_spec.rb10
-rw-r--r--spec/algebra/unit/relations/table_spec.rb38
-rw-r--r--spec/algebra/unit/relations/take_spec.rb10
-rw-r--r--spec/algebra/unit/relations/update_spec.rb9
-rw-r--r--spec/algebra/unit/relations/where_spec.rb18
13 files changed, 397 insertions, 0 deletions
diff --git a/spec/algebra/unit/relations/alias_spec.rb b/spec/algebra/unit/relations/alias_spec.rb
new file mode 100644
index 0000000000..eaf31652b9
--- /dev/null
+++ b/spec/algebra/unit/relations/alias_spec.rb
@@ -0,0 +1,16 @@
+require '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
+ check Alias.new(@relation).should_not == Alias.new(@relation)
+ (aliaz = Alias.new(@relation)).should == aliaz
+ end
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/delete_spec.rb b/spec/algebra/unit/relations/delete_spec.rb
new file mode 100644
index 0000000000..c244be8631
--- /dev/null
+++ b/spec/algebra/unit/relations/delete_spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+module Arel
+ describe Deletion do
+ before do
+ @relation = Table.new(:users)
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/group_spec.rb b/spec/algebra/unit/relations/group_spec.rb
new file mode 100644
index 0000000000..48fc818682
--- /dev/null
+++ b/spec/algebra/unit/relations/group_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+module Arel
+ describe Group do
+ before do
+ @relation = Table.new(:users)
+ @attribute = @relation[:id]
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/insert_spec.rb b/spec/algebra/unit/relations/insert_spec.rb
new file mode 100644
index 0000000000..3141fa2fc4
--- /dev/null
+++ b/spec/algebra/unit/relations/insert_spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+module Arel
+ describe Insert do
+ before do
+ @relation = Table.new(:users)
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/join_spec.rb b/spec/algebra/unit/relations/join_spec.rb
new file mode 100644
index 0000000000..9c1422c571
--- /dev/null
+++ b/spec/algebra/unit/relations/join_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+module Arel
+ describe Join do
+ before do
+ @relation1 = Table.new(:users)
+ @relation2 = Table.new(:photos)
+ @predicate = @relation1[:id].eq(@relation2[:user_id])
+ end
+
+ describe 'hashing' do
+ it 'implements hash equality' do
+ InnerJoin.new(@relation1, @relation2, @predicate) \
+ .should hash_the_same_as(InnerJoin.new(@relation1, @relation2, @predicate))
+ end
+ end
+
+ describe '#attributes' do
+ it 'combines the attributes of the two relations' do
+ join = InnerJoin.new(@relation1, @relation2, @predicate)
+ join.attributes.should ==
+ (@relation1.attributes + @relation2.attributes).collect { |a| a.bind(join) }
+ end
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/order_spec.rb b/spec/algebra/unit/relations/order_spec.rb
new file mode 100644
index 0000000000..9fcdffe340
--- /dev/null
+++ b/spec/algebra/unit/relations/order_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+module Arel
+ describe Order do
+ before do
+ @relation = Table.new(:users)
+ @attribute = @relation[:id]
+ end
+
+ describe "#==" do
+ it "returns true when the Orders are for the same attribute and direction" do
+ Ascending.new(@attribute).should == Ascending.new(@attribute)
+ end
+
+ it "returns false when the Orders are for a diferent direction" do
+ Ascending.new(@attribute).should_not == Descending.new(@attribute)
+ end
+ end
+ end
+end
+
diff --git a/spec/algebra/unit/relations/project_spec.rb b/spec/algebra/unit/relations/project_spec.rb
new file mode 100644
index 0000000000..8886e65a2e
--- /dev/null
+++ b/spec/algebra/unit/relations/project_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+module Arel
+ describe Project do
+ before do
+ @relation = Table.new(:users)
+ @attribute = @relation[:id]
+ end
+
+ describe '#attributes' do
+ before do
+ @projection = Project.new(@relation, @attribute)
+ end
+
+ it "manufactures attributes associated with the projection relation" do
+ @projection.attributes.should == [@attribute].collect { |a| a.bind(@projection) }
+ end
+ end
+
+ describe '#externalizable?' do
+ describe 'when the projections are attributes' do
+ it 'returns false' do
+ Project.new(@relation, @attribute).should_not be_externalizable
+ end
+ end
+
+ describe 'when the projections include an aggregation' do
+ it "obtains" do
+ Project.new(@relation, @attribute.sum).should be_externalizable
+ end
+ end
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/relation_spec.rb b/spec/algebra/unit/relations/relation_spec.rb
new file mode 100644
index 0000000000..a1d1793d92
--- /dev/null
+++ b/spec/algebra/unit/relations/relation_spec.rb
@@ -0,0 +1,187 @@
+require '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" do
+ check @relation[:id].should == @attribute1
+ check @relation['id'].should == @attribute1
+ 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 == StringJoin.new(@relation, arbitrary_string)
+ 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 = Predicates::Equality.new(@attribute1, @attribute2)
+ end
+
+ it "manufactures a where relation" do
+ @relation.where(@predicate).should == Where.new(@relation, @predicate)
+ end
+
+ it "accepts arbitrary strings" do
+ @relation.where("arbitrary").should == Where.new(@relation, "arbitrary")
+ end
+
+ describe 'when given a blank predicate' do
+ it 'returns self' do
+ @relation.where.should == @relation
+ end
+ end
+ end
+
+ describe '#order' do
+ it "manufactures an order relation" do
+ @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2)
+ end
+
+ describe 'when given a blank ordering' do
+ it 'returns self' do
+ @relation.order.should == @relation
+ end
+ end
+ end
+
+ describe '#take' do
+ it "manufactures a take relation" do
+ @relation.take(5).should == Take.new(@relation, 5)
+ end
+
+ describe 'when given a blank number of items' do
+ it 'returns self' do
+ @relation.take.should == @relation
+ end
+ end
+ end
+
+ describe '#skip' do
+ it "manufactures a skip relation" do
+ @relation.skip(4).should == Skip.new(@relation, 4)
+ end
+
+ describe 'when given a blank number of items' do
+ it 'returns self' do
+ @relation.skip.should == @relation
+ end
+ end
+ end
+
+ describe '#group' do
+ it 'manufactures a group relation' do
+ @relation.group(@attribute1, @attribute2).should == Group.new(@relation, @attribute1, @attribute2)
+ end
+
+ describe 'when given blank groupings' do
+ it 'returns self' do
+ @relation.group.should == @relation
+ end
+ end
+ end
+
+ describe Relation::Operable::Writable do
+ describe '#delete' do
+ it 'manufactures a deletion relation' do
+ Session.start do
+ Session.new.should_receive(:delete).with(Deletion.new(@relation))
+ @relation.delete
+ end
+ end
+ end
+
+ describe '#insert' do
+ it 'manufactures an insertion relation' do
+ Session.start do
+ record = { @relation[:name] => 'carl' }
+ Session.new.should_receive(:create).with(Insert.new(@relation, record))
+ @relation.insert(record)
+ end
+ end
+ end
+
+ describe '#update' do
+ it 'manufactures an update relation' do
+ Session.start do
+ assignments = { @relation[:name] => Value.new('bob', @relation) }
+ Session.new.should_receive(:update).with(Update.new(@relation, assignments))
+ @relation.update(assignments)
+ end
+ end
+ end
+ end
+ end
+
+ describe Relation::Enumerable do
+ it "implements enumerable" do
+ @relation.map { |value| value }.should ==
+ @relation.session.read(@relation).map { |value| value }
+ end
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/skip_spec.rb b/spec/algebra/unit/relations/skip_spec.rb
new file mode 100644
index 0000000000..e7dea6c1cf
--- /dev/null
+++ b/spec/algebra/unit/relations/skip_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+module Arel
+ describe Skip do
+ before do
+ @relation = Table.new(:users)
+ @skipped = 4
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/table_spec.rb b/spec/algebra/unit/relations/table_spec.rb
new file mode 100644
index 0000000000..d93446f1b9
--- /dev/null
+++ b/spec/algebra/unit/relations/table_spec.rb
@@ -0,0 +1,38 @@
+require '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
+ check @relation[:id].should == Attribute.new(@relation, :id)
+ end
+ end
+
+ describe 'when given an', Attribute do
+ it "returns the attribute if the attribute is within the relation" do
+ @relation[@relation[:id]].should == @relation[:id]
+ end
+
+ it "returns nil if the attribtue is not within the relation" do
+ another_relation = Table.new(:photos)
+ @relation[another_relation[:id]].should be_nil
+ end
+ end
+
+ describe 'when given an', Expression do
+ before do
+ @expression = @relation[:id].count
+ end
+
+ it "returns the Expression if the Expression is within the relation" do
+ @relation[@expression].should be_nil
+ end
+ end
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/take_spec.rb b/spec/algebra/unit/relations/take_spec.rb
new file mode 100644
index 0000000000..3ad8d269f1
--- /dev/null
+++ b/spec/algebra/unit/relations/take_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+module Arel
+ describe Take do
+ before do
+ @relation = Table.new(:users)
+ @taken = 4
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/update_spec.rb b/spec/algebra/unit/relations/update_spec.rb
new file mode 100644
index 0000000000..9ed20a446b
--- /dev/null
+++ b/spec/algebra/unit/relations/update_spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+module Arel
+ describe Update do
+ before do
+ @relation = Table.new(:users)
+ end
+ end
+end
diff --git a/spec/algebra/unit/relations/where_spec.rb b/spec/algebra/unit/relations/where_spec.rb
new file mode 100644
index 0000000000..96b95b5823
--- /dev/null
+++ b/spec/algebra/unit/relations/where_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+module Arel
+ describe Where do
+ before do
+ @relation = Table.new(:users)
+ @predicate = @relation[:id].eq(1)
+ end
+
+ describe '#initialize' do
+ it "manufactures nested where relations if multiple predicates are provided" do
+ another_predicate = @relation[:name].lt(2)
+ Where.new(@relation, @predicate, another_predicate). \
+ should == Where.new(Where.new(@relation, another_predicate), @predicate)
+ end
+ end
+ end
+end