-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize_spec.rb
200 lines (151 loc) · 5.33 KB
/
optimize_spec.rb
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
# encoding: utf-8
require 'spec_helper'
describe Algebra::Union, '#optimize' do
subject { object.optimize }
let(:header) { [[:id, Integer]] }
let(:left_body) { LazyEnumerable.new([[1]]) }
let(:right_body) { LazyEnumerable.new([[2]]) }
let(:original_left) { Relation.new(header, left_body) }
let(:original_right) { Relation.new(header, right_body) }
let(:object) { described_class.new(left, right) }
before do
# skip dup of the left and right body to avoid clearing the method stubs
left_body.stub(:frozen?).and_return(true)
right_body.stub(:frozen?).and_return(true)
end
context 'left is an empty relation' do
let(:left) { Relation.new(header) }
let(:right) { original_right }
it { should be(right) }
it 'returns an equivalent relation to the unoptimized operation' do
should == object
end
it 'does not execute left_body#each' do
left_body.should_not_receive(:each)
subject
end
it 'does not execute right_body#each' do
right_body.should_not_receive(:each)
subject
end
it_should_behave_like 'an optimize method'
end
context 'right is an empty relation' do
let(:left) { original_left }
let(:right) { Relation.new(header) }
it { should be(left) }
it 'returns an equivalent relation to the unoptimized operation' do
should == object
end
it 'does not execute left_body#each' do
left_body.should_not_receive(:each)
subject
end
it 'does not execute right_body#each' do
right_body.should_not_receive(:each)
subject
end
it_should_behave_like 'an optimize method'
end
context 'left is an empty relation when optimized' do
let(:left) { Algebra::Restriction.new(original_left, Function::Proposition::Contradiction.instance) }
let(:right) { original_right }
it { should be(right) }
it 'returns an equivalent relation to the unoptimized operation' do
should == object
end
it 'does not execute left_body#each' do
left_body.should_not_receive(:each)
subject
end
it 'does not execute right_body#each' do
right_body.should_not_receive(:each)
subject
end
it_should_behave_like 'an optimize method'
end
context 'right is an empty relation when optimized' do
let(:left) { original_left }
let(:right) { Algebra::Restriction.new(original_right, Function::Proposition::Contradiction.instance) }
it { should be(left) }
it 'returns an equivalent relation to the unoptimized operation' do
should == object
end
it 'does not execute left_body#each' do
left_body.should_not_receive(:each)
subject
end
it 'does not execute right_body#each' do
right_body.should_not_receive(:each)
subject
end
it_should_behave_like 'an optimize method'
end
context 'left and right are equivalent relations' do
let(:left_body) { LazyEnumerable.new([[1]]) }
let(:right_body) { LazyEnumerable.new([[1]]) }
let(:left) { original_left }
let(:right) { original_right }
it { should be(left) }
it 'returns an equivalent relation to the unoptimized operation' do
should == object
end
it 'does not execute left_body#each' do
pending 'TODO: should only compare left and right with #eql? if they are materialized' do
left_body.should_not_receive(:each)
subject
end
end
it 'does not execute right_body#each' do
pending 'TODO: should only compare left and right with #eql? if they are materialized' do
right_body.should_not_receive(:each)
subject
end
end
it_should_behave_like 'an optimize method'
end
context 'left and right are equivalent materialized relations' do
let(:left_body) { [[1]] }
let(:right_body) { [[1]] }
let(:left) { original_left }
let(:right) { original_right }
it { should be(left) }
it 'returns an equivalent relation to the unoptimized operation' do
should == object
end
it 'executes left_body#each' do
pending 'only receive #each once'
left_body.should_receive(:each)
subject
end
it 'executes right_body#each' do
pending 'only receive #each once'
right_body.should_receive(:each)
subject
end
it_should_behave_like 'an optimize method'
end
context 'left and right are not empty relations' do
let(:left) { original_left }
let(:right) { original_right }
it { should be(object) }
it 'executes left_body#each' do
left_body.should_receive(:each)
subject
end
it 'executes right_body#each' do
right_body.should_receive(:each)
subject
end
it_should_behave_like 'an optimize method'
end
context 'left and right are materialized relations' do
let(:left) { Relation.new(header, [[1], [2]]) }
let(:right) { Relation.new(header, [[1]]) }
it { should eql(Relation::Materialized.new(header, [[1], [2]])) }
it 'returns an equivalent relation to the unoptimized operation' do
should == object
end
it_should_behave_like 'an optimize method'
end
end