linkage 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,390 +0,0 @@
1
- require 'helper'
2
-
3
- class UnitTests::TestExpectation < Test::Unit::TestCase
4
- test "new must expectation with two fields" do
5
- Linkage::MustExpectation.new(:==, stub_field('field 1'), stub_field('field 2'))
6
- end
7
-
8
- test "get" do
9
- assert_equal Linkage::MustExpectation, Linkage::Expectation.get(:must)
10
- end
11
-
12
- test "kind when two fields from different datasets" do
13
- dataset_1 = mock('dataset 1')
14
- dataset_2 = mock('dataset 2')
15
- field_1 = stub_field('field 1', :dataset => dataset_1)
16
- field_2 = stub_field('field 2', :dataset => dataset_2)
17
- field_1.expects(:==).with(field_2).returns(false)
18
- dataset_1.expects(:==).with(dataset_2).returns(false)
19
- exp = Linkage::MustExpectation.new(:==, field_1, field_2)
20
- assert_equal :dual, exp.kind
21
- end
22
-
23
- test "kind when one field and one function from different datasets" do
24
- dataset_1 = mock('dataset 1')
25
- dataset_2 = mock('dataset 2')
26
- field = stub_field('field', :dataset => dataset_1)
27
- func = stub_function('function', :dataset => dataset_2, :static? => false)
28
- field.expects(:==).with(func).returns(false)
29
- dataset_1.expects(:==).with(dataset_2).returns(false)
30
- exp = Linkage::MustExpectation.new(:==, field, func)
31
- assert_equal :dual, exp.kind
32
- end
33
-
34
- test "kind when two functions from different datasets" do
35
- dataset_1 = mock('dataset 1')
36
- dataset_2 = mock('dataset 2')
37
- func_1 = stub_function('function 1', :dataset => dataset_1, :static? => false)
38
- func_2 = stub_function('function 2', :dataset => dataset_2, :static? => false)
39
- func_1.expects(:==).with(func_2).returns(false)
40
- dataset_1.expects(:==).with(dataset_2).returns(false)
41
- exp = Linkage::MustExpectation.new(:==, func_1, func_2)
42
- assert_equal :dual, exp.kind
43
- end
44
-
45
- test "kind when two identical fields from the same dataset" do
46
- field_1 = stub_field('field 1')
47
- field_2 = stub_field('field 2')
48
- field_1.expects(:==).with(field_2).returns(true)
49
- exp = Linkage::MustExpectation.new(:==, field_1, field_2)
50
- assert_equal :self, exp.kind
51
- end
52
-
53
- test "kind when two identical functions from the same dataset" do
54
- func_1 = stub_function('function 1')
55
- func_2 = stub_function('function 2')
56
- func_1.expects(:==).with(func_2).returns(true)
57
- exp = Linkage::MustExpectation.new(:==, func_1, func_2)
58
- assert_equal :self, exp.kind
59
- end
60
-
61
- test "kind when two different fields from the same dataset" do
62
- dataset_1 = mock('dataset 1')
63
- dataset_2 = mock('dataset 2')
64
- field_1 = stub_field('field 1', :dataset => dataset_1)
65
- field_2 = stub_field('field 2', :dataset => dataset_2)
66
- field_1.expects(:==).with(field_2).returns(false)
67
- dataset_1.expects(:==).with(dataset_2).returns(true)
68
- exp = Linkage::MustExpectation.new(:==, field_1, field_2)
69
- assert_equal :cross, exp.kind
70
- end
71
-
72
- test "kind when two different functions from the same dataset" do
73
- dataset_1 = mock('dataset 1')
74
- dataset_2 = mock('dataset 2')
75
- func_1 = stub_function('function 1', :dataset => dataset_1)
76
- func_2 = stub_function('function 2', :dataset => dataset_2)
77
- func_1.expects(:==).with(func_2).returns(false)
78
- dataset_1.expects(:==).with(dataset_2).returns(true)
79
- exp = Linkage::MustExpectation.new(:==, func_1, func_2)
80
- assert_equal :cross, exp.kind
81
- end
82
-
83
- test "manually set kind with two different fields from the same dataset" do
84
- dataset = mock('dataset 1')
85
- field_1 = stub_field('field 1', :dataset => dataset)
86
- field_2 = stub_field('field 2', :dataset => dataset)
87
- exp = Linkage::MustExpectation.new(:==, field_1, field_2, :filter)
88
- assert_equal :filter, exp.kind
89
- end
90
-
91
- test "apply a two-field dual expectation to two datasets" do
92
- dataset_1 = stub('dataset 1')
93
- dataset_2 = stub('dataset 2')
94
- field_1 = stub_field('field 1', :name => :foo, :dataset => dataset_1) do
95
- stubs(:belongs_to?).with(dataset_1).returns(true)
96
- stubs(:belongs_to?).with(dataset_2).returns(false)
97
- end
98
- field_2 = stub_field('field 2', :name => :foo, :dataset => dataset_2) do
99
- stubs(:belongs_to?).with(dataset_2).returns(true)
100
- stubs(:belongs_to?).with(dataset_1).returns(false)
101
- end
102
- merged_field = stub_field('merged field', :name => :foo)
103
- field_1.stubs(:merge).with(field_2).returns(merged_field)
104
- exp = Linkage::MustExpectation.new(:==, field_1, field_2)
105
-
106
- dataset_1.expects(:add_order).with(field_1)
107
- dataset_1.expects(:add_select).with(field_1, nil)
108
- exp.apply_to(dataset_1)
109
-
110
- dataset_2.expects(:add_order).with(field_2)
111
- dataset_2.expects(:add_select).with(field_2, nil)
112
- exp.apply_to(dataset_2)
113
- end
114
-
115
- test "apply a two-function dual expectation to two datasets" do
116
- dataset_1 = stub('dataset 1')
117
- dataset_2 = stub('dataset 2')
118
- func_1 = stub_function('function 1', :name => :foo, :dataset => dataset_1) do
119
- stubs(:belongs_to?).with(dataset_1).returns(true)
120
- stubs(:belongs_to?).with(dataset_2).returns(false)
121
- end
122
- func_2 = stub_function('function 2', :name => :bar, :dataset => dataset_2) do
123
- stubs(:belongs_to?).with(dataset_2).returns(true)
124
- stubs(:belongs_to?).with(dataset_1).returns(false)
125
- end
126
- merged_field = stub_field('merged field', :name => :foo_bar)
127
- func_1.stubs(:merge).with(func_2).returns(merged_field)
128
- exp = Linkage::MustExpectation.new(:==, func_1, func_2)
129
-
130
- dataset_1.expects(:add_order).with(func_1)
131
- dataset_1.expects(:add_select).with(func_1, :foo_bar)
132
- exp.apply_to(dataset_1)
133
-
134
- dataset_2.expects(:add_order).with(func_2)
135
- dataset_2.expects(:add_select).with(func_2, :foo_bar)
136
- exp.apply_to(dataset_2)
137
- end
138
-
139
- test "apply a two-field dual expectation to two datasets with an alias" do
140
- dataset_1 = stub('dataset 1')
141
- dataset_2 = stub('dataset 2')
142
- field_1 = stub_field('field 1', :name => :foo, :dataset => dataset_1) do
143
- stubs(:belongs_to?).with(dataset_1).returns(true)
144
- stubs(:belongs_to?).with(dataset_2).returns(false)
145
- end
146
- field_2 = stub_field('field 2', :name => :bar, :dataset => dataset_2) do
147
- stubs(:belongs_to?).with(dataset_2).returns(true)
148
- stubs(:belongs_to?).with(dataset_1).returns(false)
149
- end
150
- merged_field = stub_field('merged field', :name => :foo_bar)
151
- field_1.stubs(:merge).with(field_2).returns(merged_field)
152
- exp = Linkage::MustExpectation.new(:==, field_1, field_2)
153
-
154
- dataset_1.expects(:add_order).with(field_1)
155
- dataset_1.expects(:add_select).with(field_1, :foo_bar)
156
- exp.apply_to(dataset_1)
157
-
158
- dataset_2.expects(:add_order).with(field_2)
159
- dataset_2.expects(:add_select).with(field_2, :foo_bar)
160
- exp.apply_to(dataset_2)
161
- end
162
-
163
- test "apply a two-field cross expectation" do
164
- dataset_1 = stub('dataset 1')
165
- dataset_2 = stub('dataset 2')
166
- dataset_1.stubs(:==).with(dataset_2).returns(true)
167
- field_1 = stub_field('field 1', :name => :foo, :dataset => dataset_1) do
168
- expects(:belongs_to?).with do |*args|
169
- args[0].equal?(dataset_1)
170
- end.returns(true)
171
- expects(:belongs_to?).with do |*args|
172
- args[0].equal?(dataset_2)
173
- end.returns(false)
174
- end
175
- field_2 = stub_field('field 2', :name => :bar, :dataset => dataset_1) do
176
- expects(:belongs_to?).with do |*args|
177
- args[0].equal?(dataset_2)
178
- end.returns(true)
179
- expects(:belongs_to?).with do |*args|
180
- args[0].equal?(dataset_1)
181
- end.returns(false)
182
- end
183
- merged_field = stub_field('merged field', :name => :foo_bar)
184
- field_1.stubs(:merge).with(field_2).returns(merged_field)
185
- exp = Linkage::MustExpectation.new(:==, field_1, field_2)
186
-
187
- dataset_1.expects(:add_order).with(field_1)
188
- dataset_1.expects(:add_select).with(field_1, :foo_bar)
189
- exp.apply_to(dataset_1)
190
-
191
- dataset_2.expects(:add_order).with(field_2)
192
- dataset_2.expects(:add_select).with(field_2, :foo_bar)
193
- exp.apply_to(dataset_2)
194
- end
195
-
196
- test "apply a two-field self expectation" do
197
- dataset = stub('dataset')
198
- field = stub_field('field 1', :name => :foo, :dataset => dataset) do
199
- expects(:belongs_to?).with(dataset).twice.returns(true)
200
- end
201
- exp = Linkage::MustExpectation.new(:==, field, field)
202
-
203
- # Twice is okay, since add_order/add_select will take care of
204
- # duplicates.
205
- dataset.expects(:add_order).twice.with(field, nil)
206
- dataset.expects(:add_select).twice.with(field, nil)
207
- exp.apply_to(dataset)
208
- end
209
-
210
- test "apply a two-function self expectation" do
211
- dataset = stub('dataset')
212
- func = stub_function('function', :name => :foo, :dataset => dataset) do
213
- expects(:belongs_to?).with(dataset).twice.returns(true)
214
- end
215
- exp = Linkage::MustExpectation.new(:==, func, func)
216
-
217
- # Twice is okay, since add_order/add_select will take care of
218
- # duplicates.
219
- dataset.expects(:add_order).twice.with(func)
220
- dataset.expects(:add_select).twice.with(func, :foo)
221
- exp.apply_to(dataset)
222
- end
223
-
224
- test "apply a two-field self expectation like a cross" do
225
- dataset_1 = stub('dataset 1')
226
- dataset_2 = stub('dataset 2')
227
- field_1 = stub_field('field 1', :name => :foo, :dataset => dataset_1) do
228
- expects(:belongs_to?).with(dataset_1).returns(true)
229
- expects(:belongs_to?).with(dataset_2).returns(false)
230
- end
231
- field_2 = stub_field('field 2', :name => :foo, :dataset => dataset_2) do
232
- expects(:belongs_to?).with(dataset_2).returns(true)
233
- expects(:belongs_to?).with(dataset_1).returns(false)
234
- end
235
- exp = Linkage::MustExpectation.new(:==, field_1, field_2, :self)
236
-
237
- dataset_1.expects(:add_order).once.with(field_1, nil)
238
- dataset_1.expects(:add_select).once.with(field_1, nil)
239
- exp.apply_to(dataset_1)
240
-
241
- dataset_2.expects(:add_order).once.with(field_2, nil)
242
- dataset_2.expects(:add_select).once.with(field_2, nil)
243
- exp.apply_to(dataset_2)
244
- end
245
-
246
- test "expectation name for join types" do
247
- field_1 = stub_field('field 1')
248
- field_2 = stub_field('field 2')
249
- merged_field = stub_field('merged field', :name => :foo_bar)
250
- field_1.stubs(:merge).with(field_2).returns(merged_field)
251
-
252
- exp = Linkage::MustExpectation.new(:==, field_1, field_2)
253
- assert_equal :foo_bar, exp.name
254
- end
255
-
256
- test "expectation with static value is of type 'filter'" do
257
- field = stub_field('field')
258
- exp = Linkage::MustExpectation.new(:==, field, 123)
259
- assert_equal :filter, exp.kind
260
- end
261
-
262
- test "expectation with static function is of type 'filter'" do
263
- field = stub_field('field')
264
- func = stub_function('func', :static? => true)
265
- exp = Linkage::MustExpectation.new(:==, field, func)
266
- assert_equal :filter, exp.kind
267
- end
268
-
269
- test "apply filter expectation" do
270
- dataset_1 = stub('dataset 1')
271
- dataset_2 = stub('dataset 2')
272
- field = stub_field('field', :dataset => dataset_1)
273
- exp = Linkage::MustExpectation.new(:==, field, 123)
274
-
275
- dataset_1.expects(:add_filter).with(field, :==, 123)
276
- field.expects(:belongs_to?).with(dataset_1).returns(true)
277
- exp.apply_to(dataset_1)
278
- field.expects(:belongs_to?).with(dataset_2).returns(false)
279
- exp.apply_to(dataset_2)
280
- end
281
-
282
- test "apply filter expectation, static value first" do
283
- dataset_1 = stub('dataset 1')
284
- dataset_2 = stub('dataset 2')
285
- field = stub_field('field', :dataset => dataset_1)
286
- exp = Linkage::MustExpectation.new(:==, 123, field)
287
-
288
- dataset_1.expects(:add_filter).with(field, :==, 123)
289
- field.expects(:belongs_to?).with(dataset_1).returns(true)
290
- exp.apply_to(dataset_1)
291
- field.expects(:belongs_to?).with(dataset_2).returns(false)
292
- exp.apply_to(dataset_2)
293
- end
294
-
295
- test "apply two-field filter expectation" do
296
- dataset = stub('dataset')
297
- field_1 = stub_field('field 1', :dataset => dataset)
298
- field_2 = stub_field('field 2', :dataset => dataset)
299
- exp = Linkage::MustExpectation.new(:==, field_1, field_2, :filter)
300
-
301
- dataset.expects(:add_filter).with(field_1, :==, field_2)
302
- field_1.expects(:belongs_to?).with(dataset).returns(true)
303
- exp.apply_to(dataset)
304
- end
305
-
306
- test "creating expectation with two non-fields raises ArgumentError" do
307
- assert_raises(ArgumentError) do
308
- exp = Linkage::MustExpectation.new(:==, 123, 456)
309
- end
310
- end
311
-
312
- test "raise error if operator is not supported" do
313
- field = stub_field('field')
314
- assert_raises(ArgumentError) do
315
- exp = Linkage::MustExpectation.new(:foo, field, 456)
316
- end
317
- end
318
-
319
- [:>, :<, :>=, :<=, :'!='].each do |operator|
320
- test "#{operator} filter expectation" do
321
- dataset_1 = stub('dataset 1')
322
- dataset_2 = stub('dataset 2')
323
- field = stub_field('field', :dataset => dataset_1)
324
- exp = Linkage::MustExpectation.new(operator, field, 123)
325
-
326
- dataset_1.expects(:add_filter).with(field, operator, 123)
327
- field.expects(:belongs_to?).with(dataset_1).returns(true)
328
- exp.apply_to(dataset_1)
329
- field.expects(:belongs_to?).with(dataset_2).returns(false)
330
- exp.apply_to(dataset_2)
331
- end
332
- end
333
-
334
- test "only allows :== for non-filter expectations between two fields" do
335
- field_1 = stub_field('field 1')
336
- field_2 = stub_field('field 2')
337
- assert_raises(ArgumentError) do
338
- Linkage::MustExpectation.new(:>, field_1, field_2)
339
- end
340
- end
341
-
342
- test "applies_to? with filter expectation" do
343
- dataset = stub('dataset')
344
- field = stub_field('field')
345
- exp = Linkage::MustExpectation.new(:==, field, 123)
346
- assert_equal :filter, exp.kind
347
-
348
- field.expects(:belongs_to?).with(dataset).returns(true)
349
- assert exp.applies_to?(dataset)
350
- end
351
-
352
- test "applies_to? with non-filter expectation" do
353
- dataset_1 = stub('dataset 1')
354
- field_1 = stub_field('field 1')
355
- dataset_2 = stub('dataset 2')
356
- field_2 = stub_field('field 2')
357
- exp = Linkage::MustExpectation.new(:==, field_1, field_2)
358
- assert_not_equal :filter, exp.kind
359
-
360
- field_1.expects(:belongs_to?).with(dataset_1).returns(true)
361
- assert exp.applies_to?(dataset_1)
362
-
363
- field_1.expects(:belongs_to?).with(dataset_2).returns(false)
364
- field_2.expects(:belongs_to?).with(dataset_2).returns(true)
365
- assert exp.applies_to?(dataset_2)
366
- end
367
-
368
- test "MustNotExpectation negates operator" do
369
- dataset_1 = stub('dataset 1')
370
- dataset_2 = stub('dataset 2')
371
- field = stub_field('field', :dataset => dataset_1)
372
- exp = Linkage::MustNotExpectation.new(:==, field, 123)
373
-
374
- dataset_1.expects(:add_filter).with(field, :'!=', 123)
375
- field.expects(:belongs_to?).with(dataset_1).returns(true)
376
- exp.apply_to(dataset_1)
377
- field.expects(:belongs_to?).with(dataset_2).returns(false)
378
- exp.apply_to(dataset_2)
379
- end
380
-
381
- test "allows a dynamic function filter" do
382
- func = stub_function("foo", :static? => false)
383
- exp = Linkage::MustNotExpectation.new(:==, func, 123)
384
- end
385
-
386
- test "does not allow a static function filter" do
387
- func = stub_function("foo", :static? => true)
388
- assert_raises(ArgumentError) { Linkage::MustNotExpectation.new(:==, func, 123) }
389
- end
390
- end