linkage 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.vimrc +34 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +44 -0
- data/Guardfile +12 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +64 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/lib/linkage.rb +15 -0
- data/lib/linkage/configuration.rb +178 -0
- data/lib/linkage/dataset.rb +205 -0
- data/lib/linkage/expectation.rb +138 -0
- data/lib/linkage/field.rb +227 -0
- data/lib/linkage/group.rb +43 -0
- data/lib/linkage/import_buffer.rb +39 -0
- data/lib/linkage/runner.rb +59 -0
- data/lib/linkage/runner/single_threaded.rb +114 -0
- data/lib/linkage/utils.rb +164 -0
- data/linkage.gemspec +106 -0
- data/test/helper.rb +43 -0
- data/test/integration/test_cross_linkage.rb +68 -0
- data/test/integration/test_dual_linkage.rb +85 -0
- data/test/integration/test_self_linkage.rb +209 -0
- data/test/unit/test_configuration.rb +145 -0
- data/test/unit/test_dataset.rb +274 -0
- data/test/unit/test_expectation.rb +294 -0
- data/test/unit/test_field.rb +447 -0
- data/test/unit/test_group.rb +21 -0
- data/test/unit/test_import_buffer.rb +51 -0
- data/test/unit/test_linkage.rb +6 -0
- data/test/unit/test_runner.rb +14 -0
- data/test/unit/test_single_threaded_runner.rb +12 -0
- data/test/unit/test_utils.rb +341 -0
- metadata +272 -0
@@ -0,0 +1,274 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class UnitTests::TestDataset < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@database = stub("database")
|
6
|
+
Sequel.stubs(:connect).yields(@database)
|
7
|
+
@schema = [
|
8
|
+
[:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil}],
|
9
|
+
[:first_name, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(255)", :type=>:string, :ruby_default=>nil}],
|
10
|
+
[:last_name, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(255)", :type=>:string, :ruby_default=>nil}]
|
11
|
+
]
|
12
|
+
@database.stubs(:schema).returns(@schema)
|
13
|
+
@dataset = stub("dataset")
|
14
|
+
@database.stubs(:[]).returns(@dataset)
|
15
|
+
|
16
|
+
@id_field = stub("id field", :dataset= => nil, :name => :id)
|
17
|
+
Linkage::Field.stubs(:new).with(:id, kind_of(Hash)).returns(@id_field)
|
18
|
+
@first_name_field = stub("first_name field", :dataset= => nil, :name => :first_name)
|
19
|
+
Linkage::Field.stubs(:new).with(:first_name, kind_of(Hash)).returns(@first_name_field)
|
20
|
+
@last_name_field = stub("last_name field", :dataset= => nil, :name => :last_name)
|
21
|
+
Linkage::Field.stubs(:new).with(:last_name, kind_of(Hash)).returns(@last_name_field)
|
22
|
+
end
|
23
|
+
|
24
|
+
def expr(&block)
|
25
|
+
Sequel.virtual_row(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
test "initialize with uri and table name" do
|
29
|
+
Sequel.expects(:connect).with("foo:/bar", {}).yields(@database)
|
30
|
+
@database.expects(:schema).with(:baz).returns(@schema)
|
31
|
+
primary_key_field = mock(:dataset= => nil)
|
32
|
+
Linkage::Field.expects(:new).with(*@schema[0]).returns(primary_key_field)
|
33
|
+
Linkage::Field.expects(:new).with(*@schema[1]).returns(mock(:dataset= => nil))
|
34
|
+
Linkage::Field.expects(:new).with(*@schema[2]).returns(mock(:dataset= => nil))
|
35
|
+
|
36
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
37
|
+
assert_equal primary_key_field, ds.primary_key
|
38
|
+
end
|
39
|
+
|
40
|
+
test "initialize with sequel options" do
|
41
|
+
Sequel.expects(:connect).with("foo:/bar", :junk => 123).yields(@database)
|
42
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz", :junk => 123)
|
43
|
+
end
|
44
|
+
|
45
|
+
test "dataset id increments" do
|
46
|
+
dataset_1 = Linkage::Dataset.new("foo:/bar", "baz")
|
47
|
+
assert_kind_of Fixnum, dataset_1.id
|
48
|
+
dataset_2 = Linkage::Dataset.new("foo:/qux", "corge")
|
49
|
+
assert_equal dataset_1.id + 1, dataset_2.id
|
50
|
+
end
|
51
|
+
|
52
|
+
test "== compares uri and table name" do
|
53
|
+
dataset_1 = Linkage::Dataset.new("foo:/bar", "baz")
|
54
|
+
dataset_2 = Linkage::Dataset.new("foo:/bar", "baz")
|
55
|
+
dataset_3 = Linkage::Dataset.new("foo:/qux", "corge")
|
56
|
+
assert_equal dataset_1, dataset_2
|
57
|
+
assert_not_equal dataset_1, dataset_3
|
58
|
+
end
|
59
|
+
|
60
|
+
test "dup" do
|
61
|
+
dataset_1 = Linkage::Dataset.new("foo:/bar", "baz")
|
62
|
+
|
63
|
+
ds = stub('new dataset')
|
64
|
+
Linkage::Dataset.expects(:new).with('foo:/bar', :baz).returns(ds)
|
65
|
+
dataset_2 = dataset_1.dup
|
66
|
+
assert_equal ds, dataset_2
|
67
|
+
end
|
68
|
+
|
69
|
+
test "clone doesn't shallow copy fields" do
|
70
|
+
field_1 = stub('field 1', :name => :id, :dataset= => nil)
|
71
|
+
Linkage::Field.expects(:new).with(*@schema[0]).returns(field_1)
|
72
|
+
field_2 = stub('field 2', :dataset= => nil)
|
73
|
+
Linkage::Field.expects(:new).with(*@schema[1]).returns(field_2)
|
74
|
+
field_3 = stub('field 3', :dataset= => nil)
|
75
|
+
Linkage::Field.expects(:new).with(*@schema[2]).returns(field_3)
|
76
|
+
dataset_1 = Linkage::Dataset.new("foo:/bar", "baz")
|
77
|
+
|
78
|
+
field_1.expects(:clone).returns(mock('field 1 clone', :dataset= => nil))
|
79
|
+
field_2.expects(:clone).returns(mock('field 2 clone', :dataset= => nil))
|
80
|
+
field_3.expects(:clone).returns(mock('field 3 clone', :dataset= => nil))
|
81
|
+
dataset_2 = dataset_1.clone
|
82
|
+
dataset_2.fields.each_pair do |name, field|
|
83
|
+
assert !field.equal?(dataset_1.fields[name])
|
84
|
+
end
|
85
|
+
assert !dataset_2.primary_key.equal?(dataset_1.primary_key)
|
86
|
+
assert_equal dataset_1.id, dataset_2.id
|
87
|
+
end
|
88
|
+
|
89
|
+
test "clone doesn't shallow copy @order" do
|
90
|
+
dataset_1 = Linkage::Dataset.new("foo:/bar", "baz")
|
91
|
+
dataset_2 = dataset_1.clone
|
92
|
+
dataset_1.add_order(@first_name_field)
|
93
|
+
assert_empty dataset_2.instance_variable_get(:@order)
|
94
|
+
end
|
95
|
+
|
96
|
+
test "clone doesn't shallow copy @select" do
|
97
|
+
dataset_1 = Linkage::Dataset.new("foo:/bar", "baz")
|
98
|
+
dataset_2 = dataset_1.clone
|
99
|
+
dataset_1.add_select(@first_name_field)
|
100
|
+
assert_empty dataset_2.instance_variable_get(:@select)
|
101
|
+
end
|
102
|
+
|
103
|
+
test "clone doesn't shallow copy @filter" do
|
104
|
+
dataset_1 = Linkage::Dataset.new("foo:/bar", "baz")
|
105
|
+
dataset_2 = dataset_1.clone
|
106
|
+
dataset_1.add_filter(@first_name_field, :==, "foo")
|
107
|
+
assert_empty dataset_2.instance_variable_get(:@filter)
|
108
|
+
end
|
109
|
+
|
110
|
+
test "add_order, then each" do
|
111
|
+
field = stub('field', :name => :last_name)
|
112
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
113
|
+
ds.add_order(field)
|
114
|
+
@dataset.expects(:order).with(:last_name).returns(@dataset)
|
115
|
+
row = {:id => 123, :last_name => 'foo'}
|
116
|
+
@dataset.expects(:each).yields(row)
|
117
|
+
|
118
|
+
ran = false
|
119
|
+
ds.each do |yielded_row|
|
120
|
+
ran = true
|
121
|
+
assert_equal({:pk => 123, :values => {:last_name => 'foo'}}, yielded_row)
|
122
|
+
end
|
123
|
+
assert ran
|
124
|
+
end
|
125
|
+
|
126
|
+
test "add_order descending, then each" do
|
127
|
+
field = stub('field', :name => :last_name)
|
128
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
129
|
+
ds.add_order(field, :desc)
|
130
|
+
@dataset.expects(:order).with(:last_name.desc).returns(@dataset)
|
131
|
+
row = {:id => 123, :last_name => 'foo'}
|
132
|
+
@dataset.expects(:each).yields(row)
|
133
|
+
|
134
|
+
ran = false
|
135
|
+
ds.each do |yielded_row|
|
136
|
+
ran = true
|
137
|
+
assert_equal({:pk => 123, :values => {:last_name => 'foo'}}, yielded_row)
|
138
|
+
end
|
139
|
+
assert ran
|
140
|
+
end
|
141
|
+
|
142
|
+
test "add_order weeds out duplicates" do
|
143
|
+
field = stub('field', :name => :last_name)
|
144
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
145
|
+
ds.add_order(field)
|
146
|
+
ds.add_order(field)
|
147
|
+
@dataset.expects(:order).with(:last_name).returns(@dataset)
|
148
|
+
@dataset.expects(:each)
|
149
|
+
ds.each { }
|
150
|
+
end
|
151
|
+
|
152
|
+
test "add_select, then each" do
|
153
|
+
field = stub('field', :name => :last_name)
|
154
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
155
|
+
ds.add_select(field)
|
156
|
+
@dataset.expects(:select).with(:id, :last_name).returns(@dataset)
|
157
|
+
row = {:id => 123, :last_name => 'foo'}
|
158
|
+
@dataset.expects(:each).yields(row)
|
159
|
+
|
160
|
+
ran = false
|
161
|
+
ds.each do |yielded_row|
|
162
|
+
ran = true
|
163
|
+
assert_equal({:pk => 123, :values => {:last_name => 'foo'}}, yielded_row)
|
164
|
+
end
|
165
|
+
assert ran
|
166
|
+
end
|
167
|
+
|
168
|
+
test "add_select with an alias, then each" do
|
169
|
+
field = stub('field', :name => :last_name)
|
170
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
171
|
+
ds.add_select(field, :junk)
|
172
|
+
@dataset.expects(:select).with(:id, :last_name.as(:junk)).returns(@dataset)
|
173
|
+
row = {:id => 123, :junk => 'foo'}
|
174
|
+
@dataset.expects(:each).yields(row)
|
175
|
+
|
176
|
+
ran = false
|
177
|
+
ds.each do |yielded_row|
|
178
|
+
ran = true
|
179
|
+
assert_equal({:pk => 123, :values => {:junk => 'foo'}}, yielded_row)
|
180
|
+
end
|
181
|
+
assert ran
|
182
|
+
end
|
183
|
+
|
184
|
+
test "add_select weeds out duplicates" do
|
185
|
+
field = stub('field', :name => :last_name)
|
186
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
187
|
+
ds.add_select(field)
|
188
|
+
ds.add_select(field)
|
189
|
+
@dataset.expects(:select).with(:id, :last_name).returns(@dataset)
|
190
|
+
@dataset.expects(:each)
|
191
|
+
ds.each { }
|
192
|
+
end
|
193
|
+
|
194
|
+
test "add_filter with :==, then each" do
|
195
|
+
field = stub('field', :name => :age)
|
196
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
197
|
+
ds.add_filter(field, :==, 30)
|
198
|
+
@dataset.expects(:filter).with(:age => 30).returns(@dataset)
|
199
|
+
row = {:id => 123, :junk => 'foo'}
|
200
|
+
@dataset.expects(:each).yields(row)
|
201
|
+
|
202
|
+
ran = false
|
203
|
+
ds.each do |yielded_row|
|
204
|
+
ran = true
|
205
|
+
assert_equal({:pk => 123, :values => {:junk => 'foo'}}, yielded_row)
|
206
|
+
end
|
207
|
+
assert ran
|
208
|
+
end
|
209
|
+
|
210
|
+
test "add_filter with :== between two fields, then each" do
|
211
|
+
field_1 = stub_field('field 1', :name => :foo)
|
212
|
+
field_2 = stub_field('field 2', :name => :bar)
|
213
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
214
|
+
ds.add_filter(field_1, :==, field_2)
|
215
|
+
@dataset.expects(:filter).with(:foo => :bar).returns(@dataset)
|
216
|
+
@dataset.expects(:each)
|
217
|
+
ds.each { }
|
218
|
+
end
|
219
|
+
|
220
|
+
test "add_filter with :>, then each" do
|
221
|
+
field = stub('field', :name => :age)
|
222
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
223
|
+
ds.add_filter(field, :>, 30)
|
224
|
+
@dataset.expects(:filter).with(expr{age > 30}).returns(@dataset)
|
225
|
+
@dataset.expects(:each)
|
226
|
+
ds.each { }
|
227
|
+
end
|
228
|
+
|
229
|
+
test "add_filter with :<, then each" do
|
230
|
+
field = stub('field', :name => :age)
|
231
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
232
|
+
ds.add_filter(field, :<, 30)
|
233
|
+
@dataset.expects(:filter).with(expr{age < 30}).returns(@dataset)
|
234
|
+
@dataset.expects(:each)
|
235
|
+
ds.each { }
|
236
|
+
end
|
237
|
+
|
238
|
+
test "add_filter with :>=, then each" do
|
239
|
+
field = stub('field', :name => :age)
|
240
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
241
|
+
ds.add_filter(field, :>=, 30)
|
242
|
+
@dataset.expects(:filter).with(expr{age >= 30}).returns(@dataset)
|
243
|
+
@dataset.expects(:each)
|
244
|
+
ds.each { }
|
245
|
+
end
|
246
|
+
|
247
|
+
test "add_filter with :<=, then each" do
|
248
|
+
field = stub('field', :name => :age)
|
249
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
250
|
+
ds.add_filter(field, :<=, 30)
|
251
|
+
@dataset.expects(:filter).with(expr{age <= 30}).returns(@dataset)
|
252
|
+
@dataset.expects(:each)
|
253
|
+
ds.each { }
|
254
|
+
end
|
255
|
+
|
256
|
+
test "add_filter with :!=, then each" do
|
257
|
+
field = stub('field', :name => :age)
|
258
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
259
|
+
ds.add_filter(field, :'!=', 30)
|
260
|
+
@dataset.expects(:filter).with(~{:age => 30}).returns(@dataset)
|
261
|
+
@dataset.expects(:each)
|
262
|
+
ds.each { }
|
263
|
+
end
|
264
|
+
|
265
|
+
test "add_filter with :> field, then each" do
|
266
|
+
field_1 = stub_field('field 1', :name => :age)
|
267
|
+
field_2 = stub_field('field 2', :name => :age_2)
|
268
|
+
ds = Linkage::Dataset.new("foo:/bar", "baz")
|
269
|
+
ds.add_filter(field_1, :>, field_2)
|
270
|
+
@dataset.expects(:filter).with(expr{age > age_2}).returns(@dataset)
|
271
|
+
@dataset.expects(:each)
|
272
|
+
ds.each { }
|
273
|
+
end
|
274
|
+
end
|
@@ -0,0 +1,294 @@
|
|
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 two identical fields from the same dataset" do
|
24
|
+
field_1 = stub_field('field 1')
|
25
|
+
field_2 = stub_field('field 2')
|
26
|
+
field_1.expects(:==).with(field_2).returns(true)
|
27
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2)
|
28
|
+
assert_equal :self, exp.kind
|
29
|
+
end
|
30
|
+
|
31
|
+
test "kind when two different fields from the same dataset" do
|
32
|
+
dataset_1 = mock('dataset 1')
|
33
|
+
dataset_2 = mock('dataset 2')
|
34
|
+
field_1 = stub_field('field 1', :dataset => dataset_1)
|
35
|
+
field_2 = stub_field('field 2', :dataset => dataset_2)
|
36
|
+
field_1.expects(:==).with(field_2).returns(false)
|
37
|
+
dataset_1.expects(:==).with(dataset_2).returns(true)
|
38
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2)
|
39
|
+
assert_equal :cross, exp.kind
|
40
|
+
end
|
41
|
+
|
42
|
+
test "manually set kind with two different fields from the same dataset" do
|
43
|
+
dataset = mock('dataset 1')
|
44
|
+
field_1 = stub_field('field 1', :dataset => dataset)
|
45
|
+
field_2 = stub_field('field 2', :dataset => dataset)
|
46
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2, :filter)
|
47
|
+
assert_equal :filter, exp.kind
|
48
|
+
end
|
49
|
+
|
50
|
+
test "apply a two-field dual expectation to two datasets" do
|
51
|
+
dataset_1 = stub('dataset 1')
|
52
|
+
dataset_2 = stub('dataset 2')
|
53
|
+
field_1 = stub_field('field 1', :name => :foo, :dataset => dataset_1) do
|
54
|
+
stubs(:belongs_to?).with(dataset_1).returns(true)
|
55
|
+
stubs(:belongs_to?).with(dataset_2).returns(false)
|
56
|
+
end
|
57
|
+
field_2 = stub_field('field 2', :name => :foo, :dataset => dataset_2) do
|
58
|
+
stubs(:belongs_to?).with(dataset_2).returns(true)
|
59
|
+
stubs(:belongs_to?).with(dataset_1).returns(false)
|
60
|
+
end
|
61
|
+
merged_field = stub_field('merged field', :name => :foo)
|
62
|
+
field_1.stubs(:merge).with(field_2).returns(merged_field)
|
63
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2)
|
64
|
+
|
65
|
+
dataset_1.expects(:add_order).with(field_1)
|
66
|
+
dataset_1.expects(:add_select).with(field_1, nil)
|
67
|
+
exp.apply_to(dataset_1)
|
68
|
+
|
69
|
+
dataset_2.expects(:add_order).with(field_2)
|
70
|
+
dataset_2.expects(:add_select).with(field_2, nil)
|
71
|
+
exp.apply_to(dataset_2)
|
72
|
+
end
|
73
|
+
|
74
|
+
test "apply a two-field dual expectation to two datasets with an alias" do
|
75
|
+
dataset_1 = stub('dataset 1')
|
76
|
+
dataset_2 = stub('dataset 2')
|
77
|
+
field_1 = stub_field('field 1', :name => :foo, :dataset => dataset_1) do
|
78
|
+
stubs(:belongs_to?).with(dataset_1).returns(true)
|
79
|
+
stubs(:belongs_to?).with(dataset_2).returns(false)
|
80
|
+
end
|
81
|
+
field_2 = stub_field('field 2', :name => :bar, :dataset => dataset_2) do
|
82
|
+
stubs(:belongs_to?).with(dataset_2).returns(true)
|
83
|
+
stubs(:belongs_to?).with(dataset_1).returns(false)
|
84
|
+
end
|
85
|
+
merged_field = stub_field('merged field', :name => :foo_bar)
|
86
|
+
field_1.stubs(:merge).with(field_2).returns(merged_field)
|
87
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2)
|
88
|
+
|
89
|
+
dataset_1.expects(:add_order).with(field_1)
|
90
|
+
dataset_1.expects(:add_select).with(field_1, :foo_bar)
|
91
|
+
exp.apply_to(dataset_1)
|
92
|
+
|
93
|
+
dataset_2.expects(:add_order).with(field_2)
|
94
|
+
dataset_2.expects(:add_select).with(field_2, :foo_bar)
|
95
|
+
exp.apply_to(dataset_2)
|
96
|
+
end
|
97
|
+
|
98
|
+
test "apply a two-field cross expectation" do
|
99
|
+
dataset_1 = stub('dataset 1')
|
100
|
+
dataset_2 = stub('dataset 2')
|
101
|
+
dataset_1.stubs(:==).with(dataset_2).returns(true)
|
102
|
+
field_1 = stub_field('field 1', :name => :foo, :dataset => dataset_1) do
|
103
|
+
expects(:belongs_to?).with do |*args|
|
104
|
+
args[0].equal?(dataset_1)
|
105
|
+
end.returns(true)
|
106
|
+
expects(:belongs_to?).with do |*args|
|
107
|
+
args[0].equal?(dataset_2)
|
108
|
+
end.returns(false)
|
109
|
+
end
|
110
|
+
field_2 = stub_field('field 2', :name => :bar, :dataset => dataset_1) do
|
111
|
+
expects(:belongs_to?).with do |*args|
|
112
|
+
args[0].equal?(dataset_2)
|
113
|
+
end.returns(true)
|
114
|
+
expects(:belongs_to?).with do |*args|
|
115
|
+
args[0].equal?(dataset_1)
|
116
|
+
end.returns(false)
|
117
|
+
end
|
118
|
+
merged_field = stub_field('merged field', :name => :foo_bar)
|
119
|
+
field_1.stubs(:merge).with(field_2).returns(merged_field)
|
120
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2)
|
121
|
+
|
122
|
+
dataset_1.expects(:add_order).with(field_1)
|
123
|
+
dataset_1.expects(:add_select).with(field_1, :foo_bar)
|
124
|
+
exp.apply_to(dataset_1)
|
125
|
+
|
126
|
+
dataset_2.expects(:add_order).with(field_2)
|
127
|
+
dataset_2.expects(:add_select).with(field_2, :foo_bar)
|
128
|
+
exp.apply_to(dataset_2)
|
129
|
+
end
|
130
|
+
|
131
|
+
test "apply a two-field self expectation" do
|
132
|
+
dataset = stub('dataset')
|
133
|
+
field = stub_field('field 1', :name => :foo, :dataset => dataset) do
|
134
|
+
expects(:belongs_to?).with(dataset).twice.returns(true)
|
135
|
+
end
|
136
|
+
exp = Linkage::MustExpectation.new(:==, field, field)
|
137
|
+
|
138
|
+
# Twice is okay, since add_order/add_select will take care of
|
139
|
+
# duplicates.
|
140
|
+
dataset.expects(:add_order).twice.with(field, nil)
|
141
|
+
dataset.expects(:add_select).twice.with(field, nil)
|
142
|
+
exp.apply_to(dataset)
|
143
|
+
end
|
144
|
+
|
145
|
+
test "apply a two-field self expectation like a cross" do
|
146
|
+
dataset_1 = stub('dataset 1')
|
147
|
+
dataset_2 = stub('dataset 2')
|
148
|
+
field_1 = stub_field('field 1', :name => :foo, :dataset => dataset_1) do
|
149
|
+
expects(:belongs_to?).with(dataset_1).returns(true)
|
150
|
+
expects(:belongs_to?).with(dataset_2).returns(false)
|
151
|
+
end
|
152
|
+
field_2 = stub_field('field 2', :name => :foo, :dataset => dataset_2) do
|
153
|
+
expects(:belongs_to?).with(dataset_2).returns(true)
|
154
|
+
expects(:belongs_to?).with(dataset_1).returns(false)
|
155
|
+
end
|
156
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2, :self)
|
157
|
+
|
158
|
+
dataset_1.expects(:add_order).once.with(field_1, nil)
|
159
|
+
dataset_1.expects(:add_select).once.with(field_1, nil)
|
160
|
+
exp.apply_to(dataset_1)
|
161
|
+
|
162
|
+
dataset_2.expects(:add_order).once.with(field_2, nil)
|
163
|
+
dataset_2.expects(:add_select).once.with(field_2, nil)
|
164
|
+
exp.apply_to(dataset_2)
|
165
|
+
end
|
166
|
+
|
167
|
+
test "expectation name for join types" do
|
168
|
+
field_1 = stub_field('field 1')
|
169
|
+
field_2 = stub_field('field 2')
|
170
|
+
merged_field = stub_field('merged field', :name => :foo_bar)
|
171
|
+
field_1.stubs(:merge).with(field_2).returns(merged_field)
|
172
|
+
|
173
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2)
|
174
|
+
assert_equal :foo_bar, exp.name
|
175
|
+
end
|
176
|
+
|
177
|
+
test "expectation with static value" do
|
178
|
+
field = stub_field('field')
|
179
|
+
exp = Linkage::MustExpectation.new(:==, field, 123)
|
180
|
+
assert_equal :filter, exp.kind
|
181
|
+
end
|
182
|
+
|
183
|
+
test "apply filter expectation" do
|
184
|
+
dataset_1 = stub('dataset 1')
|
185
|
+
dataset_2 = stub('dataset 2')
|
186
|
+
field = stub_field('field', :dataset => dataset_1)
|
187
|
+
exp = Linkage::MustExpectation.new(:==, field, 123)
|
188
|
+
|
189
|
+
dataset_1.expects(:add_filter).with(field, :==, 123)
|
190
|
+
field.expects(:belongs_to?).with(dataset_1).returns(true)
|
191
|
+
exp.apply_to(dataset_1)
|
192
|
+
field.expects(:belongs_to?).with(dataset_2).returns(false)
|
193
|
+
exp.apply_to(dataset_2)
|
194
|
+
end
|
195
|
+
|
196
|
+
test "apply filter expectation, static value first" do
|
197
|
+
dataset_1 = stub('dataset 1')
|
198
|
+
dataset_2 = stub('dataset 2')
|
199
|
+
field = stub_field('field', :dataset => dataset_1)
|
200
|
+
exp = Linkage::MustExpectation.new(:==, 123, field)
|
201
|
+
|
202
|
+
dataset_1.expects(:add_filter).with(field, :==, 123)
|
203
|
+
field.expects(:belongs_to?).with(dataset_1).returns(true)
|
204
|
+
exp.apply_to(dataset_1)
|
205
|
+
field.expects(:belongs_to?).with(dataset_2).returns(false)
|
206
|
+
exp.apply_to(dataset_2)
|
207
|
+
end
|
208
|
+
|
209
|
+
test "apply two-field filter expectation" do
|
210
|
+
dataset = stub('dataset')
|
211
|
+
field_1 = stub_field('field 1', :dataset => dataset)
|
212
|
+
field_2 = stub_field('field 2', :dataset => dataset)
|
213
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2, :filter)
|
214
|
+
|
215
|
+
dataset.expects(:add_filter).with(field_1, :==, field_2)
|
216
|
+
field_1.expects(:belongs_to?).with(dataset).returns(true)
|
217
|
+
exp.apply_to(dataset)
|
218
|
+
end
|
219
|
+
|
220
|
+
test "creating expectation with two non-fields raises ArgumentError" do
|
221
|
+
assert_raises(ArgumentError) do
|
222
|
+
exp = Linkage::MustExpectation.new(:==, 123, 456)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
test "raise error if operator is not supported" do
|
227
|
+
field = stub_field('field')
|
228
|
+
assert_raises(ArgumentError) do
|
229
|
+
exp = Linkage::MustExpectation.new(:foo, field, 456)
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
[:>, :<, :>=, :<=, :'!='].each do |operator|
|
234
|
+
test "#{operator} filter expectation" do
|
235
|
+
dataset_1 = stub('dataset 1')
|
236
|
+
dataset_2 = stub('dataset 2')
|
237
|
+
field = stub_field('field', :dataset => dataset_1)
|
238
|
+
exp = Linkage::MustExpectation.new(operator, field, 123)
|
239
|
+
|
240
|
+
dataset_1.expects(:add_filter).with(field, operator, 123)
|
241
|
+
field.expects(:belongs_to?).with(dataset_1).returns(true)
|
242
|
+
exp.apply_to(dataset_1)
|
243
|
+
field.expects(:belongs_to?).with(dataset_2).returns(false)
|
244
|
+
exp.apply_to(dataset_2)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
test "only allows :== for non-filter expectations between two fields" do
|
249
|
+
field_1 = stub_field('field 1')
|
250
|
+
field_2 = stub_field('field 2')
|
251
|
+
assert_raises(ArgumentError) do
|
252
|
+
Linkage::MustExpectation.new(:>, field_1, field_2)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
test "applies_to? with filter expectation" do
|
257
|
+
dataset = stub('dataset')
|
258
|
+
field = stub_field('field')
|
259
|
+
exp = Linkage::MustExpectation.new(:==, field, 123)
|
260
|
+
assert_equal :filter, exp.kind
|
261
|
+
|
262
|
+
field.expects(:belongs_to?).with(dataset).returns(true)
|
263
|
+
assert exp.applies_to?(dataset)
|
264
|
+
end
|
265
|
+
|
266
|
+
test "applies_to? with non-filter expectation" do
|
267
|
+
dataset_1 = stub('dataset 1')
|
268
|
+
field_1 = stub_field('field 1')
|
269
|
+
dataset_2 = stub('dataset 2')
|
270
|
+
field_2 = stub_field('field 2')
|
271
|
+
exp = Linkage::MustExpectation.new(:==, field_1, field_2)
|
272
|
+
assert_not_equal :filter, exp.kind
|
273
|
+
|
274
|
+
field_1.expects(:belongs_to?).with(dataset_1).returns(true)
|
275
|
+
assert exp.applies_to?(dataset_1)
|
276
|
+
|
277
|
+
field_1.expects(:belongs_to?).with(dataset_2).returns(false)
|
278
|
+
field_2.expects(:belongs_to?).with(dataset_2).returns(true)
|
279
|
+
assert exp.applies_to?(dataset_2)
|
280
|
+
end
|
281
|
+
|
282
|
+
test "MustNotExpectation negates operator" do
|
283
|
+
dataset_1 = stub('dataset 1')
|
284
|
+
dataset_2 = stub('dataset 2')
|
285
|
+
field = stub_field('field', :dataset => dataset_1)
|
286
|
+
exp = Linkage::MustNotExpectation.new(:==, field, 123)
|
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
|
+
end
|