aboutsummaryrefslogtreecommitdiffstats
path: root/spec/algebra/unit/relations/relation_spec.rb
blob: a1ffd901d7f19370899cdee9ef3d483d81d45a2b (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require 'spec_helper'

module Arel
  describe Relation do
    before do
      @relation = Table.new(:users)
      @attribute1 = @relation[:id]
      @attribute2 = @relation[:name]
    end

    describe '[]' do
      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" do
          check @relation[:id].should == @attribute1
          check @relation['id'].should == @attribute1
        end
      end
    end

    describe 'Relation::Operable' do
      describe 'joins' do
        before do
          @predicate = @relation[:id].eq(@relation[:id])
        end

        describe '#join' do
          describe 'when given a relation' do
            it "manufactures an inner join operation between those two relations" do
              join = @relation.join(@relation).on(@predicate)
              join.relation1.should == @relation
              join.relation2.should == @relation
              join.predicates.should == [@predicate]
              join.should be_kind_of(InnerJoin)
            end
          end

          describe "when given a string" do
            it "manufactures a join operation with the string passed through" do
              arbitrary_string = "ASDF"

              join = @relation.join(arbitrary_string)
              join.relation1.should == @relation
              join.relation2.should == arbitrary_string
              join.predicates.should == []
              join.should be_kind_of StringJoin
            end
          end

          describe "when given something blank" do
            it "returns self" do
              @relation.join.should == @relation
            end
          end
        end

        describe '#outer_join' do
          it "manufactures a left outer join operation between those two relations" do
            join = @relation.outer_join(@relation).on(@predicate)
            join.relation1.should == @relation
            join.relation2.should == @relation
            join.predicates.should == [@predicate]
            join.should be_kind_of OuterJoin
          end
        end
      end

      describe '#project' do
        it "manufactures a projection relation" do
          project = @relation.project(@attribute1, @attribute2)
          project.relation.should == @relation
          project.projections.should == [@attribute1, @attribute2]
          project.should be_kind_of Project
        end

        describe "when given blank attributes" do
          it "returns self" do
            @relation.project.should == @relation
          end
        end
      end

      describe '#alias' do
        it "manufactures an alias relation" do
          @relation.alias.relation.should == Alias.new(@relation).relation
        end
      end

      describe '#where' do
        before do
          @predicate = Predicates::Equality.new(@attribute1, @attribute2)
        end

        it "manufactures a where relation" do
          where = @relation.where(@predicate)
          where.relation.should == @relation
          where.predicates.should == [@predicate]
          where.should be_kind_of Where
        end

        it "accepts arbitrary strings" do
          where = @relation.where("arbitrary")
          where.relation.should == @relation

          where.predicates.length.should == 1
          where.predicates.first.value.should == "arbitrary"

          where.should be_kind_of Where
        end

        describe 'when given a blank predicate' do
          it 'returns self' do
            @relation.where.should == @relation
          end
        end
      end

      describe '#order' do
        it "manufactures an order relation" do
          order = @relation.order(@attribute1, @attribute2)
          order.relation.should == @relation
          order.orderings.should == [@attribute1, @attribute2]
          order.should be_kind_of Order
        end

        describe 'when given a blank ordering' do
          it 'returns self' do
            @relation.order.should == @relation
          end
        end
      end

      describe '#take' do
        it "manufactures a take relation" do
          take = @relation.take(5)
          take.relation.should == @relation
          take.taken.should == 5
        end

        describe 'when given a blank number of items' do
          it 'raises error' do
            lambda { @relation.take }.should raise_exception
          end
        end
      end

      describe '#skip' do
        it "manufactures a skip relation" do
          skip = @relation.skip(4)
          skip.relation.should == @relation
          skip.skipped.should == 4
          skip.should be_kind_of Skip
        end

        describe 'when given a blank number of items' do
          it 'returns self' do
            @relation.skip.should == @relation
          end
        end
      end

      describe '#group' do
        it 'manufactures a group relation' do
          group = @relation.group(@attribute1, @attribute2)
          group.relation.should == @relation
          group.groupings.should == [@attribute1, @attribute2]
          group.should be_kind_of Group
          sql = group.to_sql
          sql.should =~ /GROUP BY/
        end

        describe 'when given blank groupings' do
          it 'returns self' do
            @relation.group.should == @relation
          end
        end
      end

      describe 'relation is writable' do
        describe '#delete' do
          it 'manufactures a deletion relation' do
            Session.start do |s|
              s.should_receive(:delete) do |delete|
                delete.relation.should == @relation
                delete.should be_kind_of Deletion
              end
              @relation.delete
            end
          end
        end

        describe '#insert' do
          it 'manufactures an insertion relation' do
            Session.start do |s|
              record = { @relation[:name] => Value.new('carl', @relation) }
              s.should_receive(:create) do |insert|
                insert.relation.should == @relation
                insert.record.should == record
                insert.should be_kind_of Insert
                insert
              end
              @relation.insert(record)
            end
          end
        end

        describe '#update' do
          it 'manufactures an update relation' do
            Session.start do |s|
              assignments = { @relation[:name] => Value.new('bob', @relation) }
              s.should_receive(:update) do |update|
                update.relation.should == @relation
                update.assignments.should == assignments
                update.should be_kind_of Update
                update
              end
              @relation.update(assignments)
            end
          end
        end
      end
    end

    describe 'is enumerable' do
      it "implements enumerable" do
        @relation.map { |value| value }.should ==
        @relation.session.read(@relation).map { |value| value }
      end
    end
  end
end