From bd5a4d6d22321f10eb716024f01a1f84f1b80d3f Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sun, 30 Dec 2007 23:59:29 -0800 Subject: before doing crazy --- spec/relations/attribute_spec.rb | 23 ++++++++++++-------- spec/relations/join_operation_spec.rb | 6 +++--- spec/relations/join_relation_spec.rb | 35 ++++++++++++++++++++++++++----- spec/relations/selection_relation_spec.rb | 16 ++++++++++++-- 4 files changed, 61 insertions(+), 19 deletions(-) (limited to 'spec/relations') diff --git a/spec/relations/attribute_spec.rb b/spec/relations/attribute_spec.rb index 5f2d70ec48..78d602abf9 100644 --- a/spec/relations/attribute_spec.rb +++ b/spec/relations/attribute_spec.rb @@ -6,7 +6,7 @@ describe Attribute do @relation2 = TableRelation.new(:bar) end - describe Attribute, '#eql?' do + describe '#eql?' do it "obtains if the relation and attribute name are identical" do Attribute.new(@relation1, :attribute_name).should be_eql(Attribute.new(@relation1, :attribute_name)) Attribute.new(@relation1, :attribute_name).should_not be_eql(Attribute.new(@relation1, :another_attribute_name)) @@ -14,47 +14,52 @@ describe Attribute do end end - describe Attribute, 'predications' do + describe 'predications' do before do @attribute1 = Attribute.new(@relation1, :attribute_name) @attribute2 = Attribute.new(@relation2, :attribute_name) end - describe Attribute, '==' do + describe '==' do it "manufactures an equality predicate" do (@attribute1 == @attribute2).should == EqualityPredicate.new(@attribute1, @attribute2) end end - describe Attribute, '<' do + describe '<' do it "manufactures a less-than predicate" do (@attribute1 < @attribute2).should == LessThanPredicate.new(@attribute1, @attribute2) end end - describe Attribute, '<=' do + describe '<=' do it "manufactures a less-than or equal-to predicate" do (@attribute1 <= @attribute2).should == LessThanOrEqualToPredicate.new(@attribute1, @attribute2) end end - describe Attribute, '>' do + describe '>' do it "manufactures a greater-than predicate" do (@attribute1 > @attribute2).should == GreaterThanPredicate.new(@attribute1, @attribute2) end end - describe Attribute, '>=' do + describe '>=' do it "manufactures a greater-than or equal to predicate" do (@attribute1 >= @attribute2).should == GreaterThanOrEqualToPredicate.new(@attribute1, @attribute2) end end - describe Attribute, '=~' do + describe '=~' do it "manufactures a match predicate" do (@attribute1 =~ /.*/).should == MatchPredicate.new(@attribute1, @attribute2) end end - + end + + describe '#to_sql' do + it "manufactures a column" do + Attribute.new(@relation1, :attribute_name).to_sql.should == ColumnBuilder.new(@relation1.table, :attribute_name) + end end end diff --git a/spec/relations/join_operation_spec.rb b/spec/relations/join_operation_spec.rb index d75d2d5c93..db30198f6e 100644 --- a/spec/relations/join_operation_spec.rb +++ b/spec/relations/join_operation_spec.rb @@ -1,12 +1,12 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper') -describe JoinOperation, 'between two relations' do +describe 'between two relations' do before do @relation1 = TableRelation.new(:foo) @relation2 = TableRelation.new(:bar) end - describe JoinOperation, '==' do + describe '==' do it "obtains if the relations of both joins are identical" do JoinOperation.new(@relation1, @relation2).should == JoinOperation.new(@relation1, @relation2) JoinOperation.new(@relation1, @relation2).should_not == JoinOperation.new(@relation1, @relation1) @@ -17,7 +17,7 @@ describe JoinOperation, 'between two relations' do end end - describe JoinOperation, 'on' do + describe 'on' do before do @predicate = Predicate.new @join_operation = JoinOperation.new(@relation1, @relation2) diff --git a/spec/relations/join_relation_spec.rb b/spec/relations/join_relation_spec.rb index 7309563cd5..c6fae90ff3 100644 --- a/spec/relations/join_relation_spec.rb +++ b/spec/relations/join_relation_spec.rb @@ -1,20 +1,45 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper') -describe JoinRelation, 'between two relations' do +describe 'between two relations' do before do @relation1 = TableRelation.new(:foo) @relation2 = TableRelation.new(:bar) - @predicate = Predicate.new + @predicate = EqualityPredicate.new(@relation1[:a], @relation2[:b]) end - describe JoinRelation, '==' do - it "obtains if the two relations and the predicate are identical" do + describe '==' do + it 'obtains if the two relations and the predicate are identical' do JoinRelation.new(@relation1, @relation2, @predicate).should == JoinRelation.new(@relation1, @relation2, @predicate) JoinRelation.new(@relation1, @relation2, @predicate).should_not == JoinRelation.new(@relation1, @relation1, @predicate) end - it "is commutative on the relations" do + it 'is commutative on the relations' do JoinRelation.new(@relation1, @relation2, @predicate).should == JoinRelation.new(@relation2, @relation1, @predicate) end end + + describe '#to_sql' do + before do + @relation1 = @relation1.select(@relation1[:c] == @relation2[:d]) + class ConcreteJoinRelation < JoinRelation + def join_name + :inner_join + end + end + end + + it 'manufactures sql joining the two tables on the predicate, merging the selects' do + ConcreteJoinRelation.new(@relation1, @relation2, @predicate).to_sql.to_s.should == SelectBuilder.new do + select :* + from :foo do + inner_join :bar do + equals 'foo.a', 'bar.b' + end + end + where do + equals 'foo.c', 'bar.d' + end + end.to_s + end + end end \ No newline at end of file diff --git a/spec/relations/selection_relation_spec.rb b/spec/relations/selection_relation_spec.rb index 0cfd55449c..1f8b760272 100644 --- a/spec/relations/selection_relation_spec.rb +++ b/spec/relations/selection_relation_spec.rb @@ -8,7 +8,7 @@ describe SelectionRelation do @predicate2 = LessThanPredicate.new(@relation1[:age], 2) end - describe SelectionRelation, '==' do + describe '==' do it "obtains if both the predicate and the relation are identical" do SelectionRelation.new(@relation1, @predicate1). \ should == SelectionRelation.new(@relation1, @predicate1) @@ -19,10 +19,22 @@ describe SelectionRelation do end end - describe SelectionRelation, '#initialize' do + describe '#initialize' do it "manufactures nested selection relations if multiple predicates are provided" do SelectionRelation.new(@relation1, @predicate1, @predicate2). \ should == SelectionRelation.new(SelectionRelation.new(@relation1, @predicate2), @predicate1) end end + + describe '#to_sql' do + it "manufactures sql with where clause conditions" do + SelectionRelation.new(@relation1, @predicate1).to_sql.should == SelectBuilder.new do + select :* + from :foo + where do + equals 'foo.id', 'bar.foo_id' + end + end + end + end end \ No newline at end of file -- cgit v1.2.3