aboutsummaryrefslogtreecommitdiffstats
path: root/spec/relations/join_relation_spec.rb
blob: c6fae90ff3d334bcae590517cc78197e60a9b4ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe 'between two relations' do
  before do
    @relation1 = TableRelation.new(:foo)
    @relation2 = TableRelation.new(:bar)
    @predicate = EqualityPredicate.new(@relation1[:a], @relation2[:b])
  end
  
  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
      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