aboutsummaryrefslogtreecommitdiffstats
path: root/spec/active_relation/unit/predicates/binary_spec.rb
blob: f6466d910527701f5e7c472ebb5edf4fad3e2955 (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')

module ActiveRelation
  describe Binary do
    before do
      @relation1 = Table.new(:users)
      @relation2 = Table.new(:photos)
      @attribute1 = @relation1[:id]
      @attribute2 = @relation2[:id]
      class ConcreteBinary < Binary
        def predicate_sql
          "<=>"
        end
      end
    end
  
    describe '==' do
      it "obtains if attribute1 and attribute2 are identical" do
        Binary.new(@attribute1, @attribute2).should == Binary.new(@attribute1, @attribute2)
        Binary.new(@attribute1, @attribute2).should_not == Binary.new(@attribute1, @attribute1)
      end
    
      it "obtains if the concrete type of the Predicates::Binarys are identical" do
        Binary.new(@attribute1, @attribute2).should == Binary.new(@attribute1, @attribute2)
        Binary.new(@attribute1, @attribute2).should_not == ConcreteBinary.new(@attribute1, @attribute2)
      end
    end
  
    describe '#qualify' do
      it "descends" do
        ConcreteBinary.new(@attribute1, @attribute2).qualify \
          .should == ConcreteBinary.new(@attribute1, @attribute2).descend(&:qualify)
      end
    end
    
    describe '#descend' do
      it "distributes over the predicates and attributes" do
        ConcreteBinary.new(@attribute1, @attribute2).descend(&:qualify). \
          should == ConcreteBinary.new(@attribute1.qualify, @attribute2.qualify)
      end
    end
    
    describe '#bind' do
      it "distributes over the predicates and attributes" do
        ConcreteBinary.new(@attribute1, @attribute2).bind(@relation2). \
          should == ConcreteBinary.new(@attribute1.bind(@relation2), @attribute2.bind(@relation2))
      end
    end
  
    describe '#to_sql' do
      it 'manufactures sql with a binary operation' do
        ConcreteBinary.new(@attribute1, @attribute2).to_sql.should be_like("
          `users`.`id` <=> `photos`.`id`
        ")
      end
      
      it 'appropriately quotes scalars' do
        ConcreteBinary.new(@attribute1, "1-asdf").to_sql.should be_like("
          `users`.`id` <=> 1
        ")        
      end
    end
  end
end