aboutsummaryrefslogtreecommitdiffstats
path: root/spec/relations/selection_relation_spec.rb
blob: 395a3f76931a109983845aa6c4498dfc91eb64e6 (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
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe SelectionRelation do
  before do
    @relation1 = TableRelation.new(:foo)
    @relation2 = TableRelation.new(:bar)
    @predicate1 = EqualityPredicate.new(@relation1[:id], @relation2[:foo_id])
    @predicate2 = LessThanPredicate.new(@relation1[:age], 2)
  end
  
  describe '==' do    
    it "obtains if both the predicate and the relation are identical" do
      SelectionRelation.new(@relation1, @predicate1). \
        should == SelectionRelation.new(@relation1, @predicate1)
      SelectionRelation.new(@relation1, @predicate1). \
        should_not == SelectionRelation.new(@relation2, @predicate1)
      SelectionRelation.new(@relation1, @predicate1). \
        should_not == SelectionRelation.new(@relation1, @predicate2)
    end
  end
  
  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 '#qualify' do
    it "manufactures a selection relation with qualified predicates and qualified relation" do
      SelectionRelation.new(@relation1, @predicate1).qualify. \
        should == SelectionRelation.new(@relation1.qualify, @predicate1.qualify)
    end
  end
  
  describe '#to_sql' do
    it "manufactures sql with where clause conditions" do
      SelectionRelation.new(@relation1, @predicate1).to_sql.to_s.should == SelectBuilder.new do
        select do
          column :foo, :name
          column :foo, :id
        end
        from :foo
        where do
          equals do
            column :foo, :id
            column :bar, :foo_id
          end
        end
      end.to_s
    end
  end
end