aboutsummaryrefslogtreecommitdiffstats
path: root/spec/active_relation/relations/relation_spec.rb
blob: a25dc4b1db3529e3342ed0a383a5c6c108182941 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')

module ActiveRelation
  describe Relation do
    before do
      @relation = Table.new(:users)
      @attribute1 = @relation[:id]
      @attribute2 = @relation[:name]
    end
  
    describe '[]' do
      describe 'when given a', Range do
        it "manufactures a range relation when given a range" do
          @relation[1..2].should == Range.new(@relation, 1..2)
        end
      end
      
      describe 'when given an', Attribute do
        it "return the attribute congruent to the provided attribute" do
          @relation[@attribute1].should == @attribute1
        end
      end
      
      describe 'when given a', Symbol, String do
        it "returns the attribute with the same name, if it exists" do
          @relation[:id].should == @attribute1
          @relation['id'].should == @attribute1
          @relation[:does_not_exist].should be_nil
        end
      end
    end
  
    describe '#include?' do
      it "manufactures an inclusion predicate" do
        @relation.include?(@attribute1).should be_kind_of(RelationInclusion)
      end
    end
    
    describe '#Expression?' do
      it "returns false" do
        @relation.should_not be_aggregation
      end
    end

    describe 'read operations' do
      describe 'joins' do
        before do
          @predicate = @relation[:id].equals(@relation[:id])
        end
      
        describe '#join' do
          it "manufactures an inner join operation between those two relations" do
            @relation.join(@relation).on(@predicate).should == Join.new("INNER JOIN", @relation, @relation, @predicate)
          end
        end
    
        describe '#outer_join' do
          it "manufactures a left outer join operation between those two relations" do
            @relation.outer_join(@relation).on(@predicate).should == Join.new("LEFT OUTER JOIN", @relation, @relation, @predicate)
          end      
        end
      end
  
      describe '#project' do
        it "manufactures a projection relation" do
          @relation.project(@attribute1, @attribute2).should == Projection.new(@relation, @attribute1, @attribute2)
        end
      end
    
      describe '#as' do
        it "manufactures an alias relation" do
          @relation.as(:thucydides).should == Alias.new(@relation, :thucydides)
        end
      end
  
      describe '#rename' do
        it "manufactures a rename relation" do
          @relation.rename(@attribute1, :users).should == Rename.new(@relation, @attribute1 => :users)
        end
      end
  
      describe '#select' do
        before do
          @predicate = Equality.new(@attribute1, @attribute2)
        end
    
        it "manufactures a selection relation" do
          @relation.select(@predicate).should == Selection.new(@relation, @predicate)
        end
    
        it "accepts arbitrary strings" do
          @relation.select("arbitrary").should == Selection.new(@relation, "arbitrary")
        end
      end
  
      describe '#order' do
        it "manufactures an order relation" do
          @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2)
        end
      end
      
      describe '#aggregate' do
        it 'manufactures a group relation' do
          @relation.aggregate(@expression1, @expression2).group(@attribute1, @attribute2). \
            should == Aggregation.new(@relation, :expressions => [@expresion, @expression2], :groupings => [@attribute1, @attribute2])
        end
      end
    end
  
    describe 'write operations' do
      describe '#delete' do
        it 'manufactures a deletion relation' do
          @relation.delete.should be_kind_of(Deletion)
        end
      end
    
      describe '#insert' do
        it 'manufactures an insertion relation' do
          @relation.insert(record = {:id => 1}).should be_kind_of(Insertion)
        end
      end
    end
  end
end