linkage 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,447 @@
1
+ require 'helper'
2
+
3
+ class UnitTests::TestField < Test::Unit::TestCase
4
+ def new_field(name, schema, ruby_type, dataset = nil)
5
+ f = Linkage::Field.new(name, schema, ruby_type)
6
+ f.dataset = dataset || stub('dataset')
7
+ f
8
+ end
9
+
10
+ test "initialize with schema info" do
11
+ schema = {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil}
12
+ field = Linkage::Field.new(:id, schema)
13
+ assert_equal :id, field.name
14
+ assert_equal schema, field.schema
15
+ end
16
+
17
+ test "initialize with ruby type" do
18
+ info = {:type => Integer}
19
+ field = Linkage::Field.new(:id, nil, info)
20
+ assert_equal :id, field.name
21
+ assert_equal info, field.ruby_type
22
+ end
23
+
24
+ test "merge two identical fields" do
25
+ field_1 = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
26
+ field_2 = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
27
+ expected_type = {:type => Integer}
28
+
29
+ result_field = field_1.merge(field_2)
30
+ assert_equal(:id, result_field.name)
31
+ assert_equal(expected_type, result_field.ruby_type)
32
+
33
+ result_field = field_2.merge(field_1)
34
+ assert_equal(:id, result_field.name)
35
+ assert_equal(expected_type, result_field.ruby_type)
36
+ end
37
+
38
+ test "merge fields with different names" do
39
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
40
+ field_2 = Linkage::Field.new(:bar, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
41
+
42
+ result_field = field_1.merge(field_2)
43
+ assert_equal(:foo_bar, result_field.name)
44
+ result_field = field_2.merge(field_1)
45
+ assert_equal(:bar_foo, result_field.name)
46
+ end
47
+
48
+ test "merge two boolean fields" do
49
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
50
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
51
+ expected_type = {:type => TrueClass}
52
+
53
+ result_field = field_1.merge(field_2)
54
+ assert_equal(:foo, result_field.name)
55
+ assert_equal(expected_type, result_field.ruby_type)
56
+
57
+ result_field = field_2.merge(field_1)
58
+ assert_equal(:foo, result_field.name)
59
+ assert_equal(expected_type, result_field.ruby_type)
60
+ end
61
+
62
+ test "merge a boolean field and an integer field" do
63
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
64
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
65
+ expected_type = {:type => Integer}
66
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
67
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
68
+ end
69
+
70
+ test "merge a boolean field and a bignum field" do
71
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
72
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
73
+ expected_type = {:type => Bignum}
74
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
75
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
76
+ end
77
+
78
+ test "merge a boolean field and a float field" do
79
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
80
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
81
+ expected_type = {:type => Float}
82
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
83
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
84
+ end
85
+
86
+ test "merge a boolean field and a decimal field" do
87
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
88
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
89
+ expected_type = {:type => BigDecimal}
90
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
91
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
92
+ end
93
+
94
+ test "merge a boolean field and a decimal field with size" do
95
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(10,2)", :type=>:decimal, :ruby_default=>nil})
96
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
97
+ expected_type = {:type => BigDecimal, :opts => {:size => [10, 2]}}
98
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
99
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
100
+ end
101
+
102
+ test "merge a boolean field and a string field" do
103
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(10)", :type=>:string, :ruby_default=>nil})
104
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
105
+ expected_type = {:type => String, :opts => {:size => 10}}
106
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
107
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
108
+ end
109
+
110
+ test "merge a boolean field and a text field" do
111
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"text", :type=>:text, :ruby_default=>nil})
112
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
113
+ expected_type = {:type => String, :opts => {:text => true}}
114
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
115
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
116
+ end
117
+
118
+ test "merge a boolean field and a fixed string field" do
119
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"char(10)", :type=>:string, :ruby_default=>nil})
120
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"boolean", :type=>:boolean, :ruby_default=>nil})
121
+ expected_type = {:type => String, :opts => {:size => 10, :fixed => true}}
122
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
123
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
124
+ end
125
+
126
+ test "merge two integer fields" do
127
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
128
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
129
+ expected_type = {:type => Integer}
130
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
131
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
132
+ end
133
+
134
+ test "merge an integer field and a bignum field" do
135
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
136
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
137
+ expected_type = {:type => Bignum}
138
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
139
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
140
+ end
141
+
142
+ test "merge an integer field and a float field" do
143
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
144
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
145
+ expected_type = {:type => Float}
146
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
147
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
148
+ end
149
+
150
+ test "merge an integer field and a decimal field" do
151
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
152
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
153
+ expected_type = {:type => BigDecimal}
154
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
155
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
156
+ end
157
+
158
+ test "merge an integer field and a decimal field with size" do
159
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(10,2)", :type=>:decimal, :ruby_default=>nil})
160
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
161
+ expected_type = {:type => BigDecimal, :opts => {:size => [10, 2]}}
162
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
163
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
164
+ end
165
+
166
+ test "merge an integer field and a string field" do
167
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(10)", :type=>:string, :ruby_default=>nil})
168
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
169
+ expected_type = {:type => String, :opts => {:size => 10}}
170
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
171
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
172
+ end
173
+
174
+ test "merge an integer field and a text field" do
175
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"text", :type=>:text, :ruby_default=>nil})
176
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
177
+ expected_type = {:type => String, :opts => {:text => true}}
178
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
179
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
180
+ end
181
+
182
+ test "merge an integer field and a fixed string field" do
183
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"char(10)", :type=>:string, :ruby_default=>nil})
184
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
185
+ expected_type = {:type => String, :opts => {:size => 10, :fixed => true}}
186
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
187
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
188
+ end
189
+
190
+ test "merge two bignum fields" do
191
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
192
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
193
+ expected_type = {:type => Bignum}
194
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
195
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
196
+ end
197
+
198
+ test "merge an bignum field and a decimal field" do
199
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
200
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
201
+ expected_type = {:type => BigDecimal}
202
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
203
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
204
+ end
205
+
206
+ test "merge an bignum field and a decimal field with size" do
207
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(10,2)", :type=>:decimal, :ruby_default=>nil})
208
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
209
+ expected_type = {:type => BigDecimal, :opts => {:size => [10, 2]}}
210
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
211
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
212
+ end
213
+
214
+ test "merge an bignum field and a string field" do
215
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(10)", :type=>:string, :ruby_default=>nil})
216
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
217
+ expected_type = {:type => String, :opts => {:size => 10}}
218
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
219
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
220
+ end
221
+
222
+ test "merge an bignum field and a text field" do
223
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"text", :type=>:text, :ruby_default=>nil})
224
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
225
+ expected_type = {:type => String, :opts => {:text => true}}
226
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
227
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
228
+ end
229
+
230
+ test "merge an bignum field and a fixed string field" do
231
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"char(10)", :type=>:string, :ruby_default=>nil})
232
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
233
+ expected_type = {:type => String, :opts => {:size => 10, :fixed => true}}
234
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
235
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
236
+ end
237
+
238
+ test "merge two float fields" do
239
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
240
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
241
+ expected_type = {:type => Float}
242
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
243
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
244
+ end
245
+
246
+ test "merge a float field and a bignum field" do
247
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"bigint", :type=>:bigint, :ruby_default=>nil})
248
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
249
+ expected_type = {:type => BigDecimal}
250
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
251
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
252
+ end
253
+
254
+ test "merge a float field and a decimal field" do
255
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
256
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
257
+ expected_type = {:type => BigDecimal}
258
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
259
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
260
+ end
261
+
262
+ test "merge a float field and a decimal field with size" do
263
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(10,2)", :type=>:decimal, :ruby_default=>nil})
264
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
265
+ expected_type = {:type => BigDecimal, :opts => {:size => [10, 2]}}
266
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
267
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
268
+ end
269
+
270
+ test "merge a float field and a string field" do
271
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(10)", :type=>:string, :ruby_default=>nil})
272
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
273
+ expected_type = {:type => String, :opts => {:size => 10}}
274
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
275
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
276
+ end
277
+
278
+ test "merge a float field and a text field" do
279
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"text", :type=>:text, :ruby_default=>nil})
280
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
281
+ expected_type = {:type => String, :opts => {:text => true}}
282
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
283
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
284
+ end
285
+
286
+ test "merge a float field and a fixed string field" do
287
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"char(10)", :type=>:string, :ruby_default=>nil})
288
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"float", :type=>:float, :ruby_default=>nil})
289
+ expected_type = {:type => String, :opts => {:size => 10, :fixed => true}}
290
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
291
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
292
+ end
293
+
294
+ test "merge two decimal fields" do
295
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
296
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
297
+ expected_type = {:type => BigDecimal}
298
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
299
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
300
+ end
301
+
302
+ test "merge two decimal fields, one with size" do
303
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(10,2)", :type=>:decimal, :ruby_default=>nil})
304
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
305
+ expected_type = {:type => BigDecimal, :opts => {:size => [10, 2]}}
306
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
307
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
308
+ end
309
+
310
+ test "merge two decimal fields, both with size" do
311
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(10,2)", :type=>:decimal, :ruby_default=>nil})
312
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(12,1)", :type=>:decimal, :ruby_default=>nil})
313
+ expected_type = {:type => BigDecimal, :opts => {:size => [12, 2]}}
314
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
315
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
316
+ end
317
+
318
+ test "merge two decimal fields, both with size, one without scale" do
319
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(10)", :type=>:decimal, :ruby_default=>nil})
320
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(12,1)", :type=>:decimal, :ruby_default=>nil})
321
+ expected_type = {:type => BigDecimal, :opts => {:size => [12, 1]}}
322
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
323
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
324
+ end
325
+
326
+ test "merge a decimal field and a string field" do
327
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(10)", :type=>:string, :ruby_default=>nil})
328
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
329
+ expected_type = {:type => String, :opts => {:size => 10}}
330
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
331
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
332
+ end
333
+
334
+ test "merge a decimal field with size and a string field" do
335
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(10)", :type=>:string, :ruby_default=>nil})
336
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(12,2)", :type=>:decimal, :ruby_default=>nil})
337
+ expected_type = {:type => String, :opts => {:size => 13}}
338
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
339
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
340
+ end
341
+
342
+ test "merge a decimal field and a text field" do
343
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"text", :type=>:text, :ruby_default=>nil})
344
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
345
+ expected_type = {:type => String, :opts => {:text => true}}
346
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
347
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
348
+ end
349
+
350
+ test "merge a decimal field with size and a text field" do
351
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"text", :type=>:text, :ruby_default=>nil})
352
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal(12,2)", :type=>:decimal, :ruby_default=>nil})
353
+ expected_type = {:type => String, :opts => {:text => true}}
354
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
355
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
356
+ end
357
+
358
+ test "merge a decimal field and a fixed string field" do
359
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"char(10)", :type=>:string, :ruby_default=>nil})
360
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"decimal", :type=>:decimal, :ruby_default=>nil})
361
+ expected_type = {:type => String, :opts => {:size => 10, :fixed => true}}
362
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
363
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
364
+ end
365
+
366
+ test "merge two string fields" do
367
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(20)", :type=>:string, :ruby_default=>nil})
368
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(20)", :type=>:string, :ruby_default=>nil})
369
+ expected_type = {:type => String, :opts => {:size => 20}}
370
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
371
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
372
+ end
373
+
374
+ test "merge two string fields with different sizes" do
375
+ field_1 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(15)", :type=>:string, :ruby_default=>nil})
376
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"varchar(20)", :type=>:string, :ruby_default=>nil})
377
+ expected_type = {:type => String, :opts => {:size => 20}}
378
+ assert_equal(expected_type, field_1.merge(field_2).ruby_type)
379
+ assert_equal(expected_type, field_2.merge(field_1).ruby_type)
380
+ end
381
+
382
+ test "merge two fields and specify name" do
383
+ field_1 = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
384
+ field_2 = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
385
+
386
+ result_field = field_1.merge(field_2, 'foo')
387
+ assert_equal :foo, result_field.name
388
+
389
+ result_field = field_2.merge(field_1, 'foo')
390
+ assert_equal :foo, result_field.name
391
+ end
392
+
393
+ test "== returns true when fields have the same name and are from the same dataset" do
394
+ dataset_1 = stub('dataset')
395
+ field_1 = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
396
+ field_1.dataset = dataset_1
397
+ dataset_2 = stub('dataset clone')
398
+ field_2 = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
399
+ field_2.dataset = dataset_2
400
+
401
+ dataset_1.expects(:==).with(dataset_2).returns(true)
402
+ assert_equal field_1, field_2
403
+ end
404
+
405
+ test "== returns false when fields have the same name but are from different datasets" do
406
+ dataset_1 = stub('dataset 1')
407
+ field_1 = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
408
+ field_1.dataset = dataset_1
409
+ dataset_2 = stub('dataset 2')
410
+ field_2 = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
411
+ field_2.dataset = dataset_2
412
+
413
+ dataset_1.expects(:==).with(dataset_2).returns(false)
414
+ assert_not_equal field_1, field_2
415
+ end
416
+
417
+ test "belongs_to? dataset" do
418
+ dataset = stub('dataset 1', :id => 1)
419
+ field = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
420
+ field.dataset = dataset
421
+ assert field.belongs_to?(dataset)
422
+ end
423
+
424
+ test "belongs_to? dataset with same id" do
425
+ dataset_1 = stub('dataset 1', :id => 1)
426
+ dataset_2 = stub('dataset 2', :id => 1)
427
+ field = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
428
+ field.dataset = dataset_1
429
+ assert field.belongs_to?(dataset_2)
430
+ end
431
+
432
+ test "belongs_to? dataset with different id" do
433
+ dataset_1 = stub('dataset 1', :id => 1)
434
+ dataset_2 = stub('dataset 2', :id => 2)
435
+ field = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
436
+ field.dataset = dataset_1
437
+ assert !field.belongs_to?(dataset_2)
438
+ end
439
+
440
+ test "primary_key? returns true if primary key" do
441
+ field_1 = Linkage::Field.new(:id, {:allow_null=>true, :default=>nil, :primary_key=>true, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
442
+ assert field_1.primary_key?
443
+
444
+ field_2 = Linkage::Field.new(:foo, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"integer", :type=>:integer, :ruby_default=>nil})
445
+ assert !field_2.primary_key?
446
+ end
447
+ end
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ class UnitTests::TestGroup < Test::Unit::TestCase
4
+ test "matches?" do
5
+ g = Linkage::Group.new(:test => 'test')
6
+ assert g.matches?({:test => 'test'})
7
+ assert !g.matches?({:foo => 'bar'})
8
+ end
9
+
10
+ test "add_record adds a record" do
11
+ g = Linkage::Group.new(:test => 'test')
12
+ g.add_record(123)
13
+ assert_equal [123], g.records
14
+ end
15
+
16
+ test "count returns number of records" do
17
+ g = Linkage::Group.new(:test => 'test')
18
+ g.add_record(123)
19
+ assert_equal 1, g.count
20
+ end
21
+ end