aboutsummaryrefslogtreecommitdiffstats
path: root/spec/relations/join_relation_spec.rb
blob: b70bf553b16ec1f41bbefad4ffa50fed5d50fd43 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe JoinRelation do
  before do
    @relation1 = TableRelation.new(:foo)
    @relation2 = TableRelation.new(:bar)
    @predicate = EqualityPredicate.new(@relation1[:id], @relation2[:id])
  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 '#qualify' do
    it 'distributes over the relations and predicates' do
      JoinRelation.new(@relation1, @relation2, @predicate).qualify. \
        should == JoinRelation.new(@relation1.qualify, @relation2.qualify, @predicate.qualify)
    end
  end
  
  describe '#to_sql' do
    before do
      @relation1 = @relation1.select(@relation1[:id] == @relation2[:foo_id])
      class ConcreteJoinRelation < JoinRelation
        def join_type
          :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_s.should == SelectBuilder.new do
        select do
          column :foo, :name
          column :foo, :id
          column :bar, :name
          column :bar, :foo_id
          column :bar, :id
        end
        from :foo do
          inner_join :bar do
            equals do
              column :foo, :id
              column :bar, :id
            end
          end
        end
        where do
          equals do
            column :foo, :id
            column :bar, :foo_id
          end
        end
      end.to_s
    end
  end
end