ohm-contrib 0.0.31 → 0.0.33
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +2 -1
- data/README.markdown +2 -1
- data/Rakefile +4 -53
- data/lib/ohm/contrib.rb +2 -2
- data/lib/ohm/contrib/boundaries.rb +1 -0
- data/lib/ohm/contrib/callbacks.rb +5 -4
- data/lib/ohm/contrib/date_validations.rb +2 -1
- data/lib/ohm/contrib/extra_validations.rb +1 -0
- data/lib/ohm/contrib/length_validations.rb +1 -0
- data/lib/ohm/contrib/locking.rb +2 -1
- data/lib/ohm/contrib/lunar_macros.rb +5 -4
- data/lib/ohm/contrib/number_validations.rb +1 -0
- data/lib/ohm/contrib/slug.rb +1 -0
- data/lib/ohm/contrib/timestamping.rb +1 -0
- data/lib/ohm/contrib/typecast.rb +1 -0
- data/lib/ohm/contrib/web_validations.rb +2 -1
- data/test/autoload_test.rb +19 -0
- data/test/boundaries_test.rb +45 -0
- data/test/callbacks_lint.rb +105 -0
- data/test/date_validations_test.rb +29 -0
- data/test/helper.rb +19 -10
- data/test/instance_callbacks_test.rb +45 -0
- data/test/length_validations_test.rb +31 -0
- data/test/lunar_macros_test.rb +146 -0
- data/test/macro_callbacks_test.rb +57 -0
- data/test/membership_validation_test.rb +31 -0
- data/test/number_validations_test.rb +37 -0
- data/test/slug_test.rb +30 -0
- data/test/timestamping_test.rb +32 -0
- data/test/typecast_array_test.rb +154 -0
- data/test/typecast_boolean_test.rb +43 -0
- data/test/typecast_date_test.rb +77 -0
- data/test/typecast_decimal_test.rb +77 -0
- data/test/typecast_float_test.rb +52 -0
- data/test/typecast_hash_test.rb +117 -0
- data/test/typecast_integer_test.rb +66 -0
- data/test/typecast_string_test.rb +38 -0
- data/test/typecast_time_test.rb +67 -0
- data/test/typecast_timezone_test.rb +37 -0
- data/test/web_validations_test.rb +79 -0
- metadata +47 -56
- data/.document +0 -5
- data/.gitignore +0 -25
- data/VERSION +0 -1
- data/lib/ohm/contrib/to_hash.rb +0 -39
- data/ohm-contrib.gemspec +0 -103
- data/test/test_ohm_boundaries.rb +0 -74
- data/test/test_ohm_contrib.rb +0 -70
- data/test/test_ohm_contrib_callbacks.rb +0 -375
- data/test/test_ohm_date_validations.rb +0 -30
- data/test/test_ohm_extra_validations.rb +0 -32
- data/test/test_ohm_length_validations.rb +0 -32
- data/test/test_ohm_lunar_macros.rb +0 -165
- data/test/test_ohm_number_validations.rb +0 -59
- data/test/test_ohm_slug.rb +0 -29
- data/test/test_ohm_timestamping.rb +0 -64
- data/test/test_ohm_to_hash.rb +0 -67
- data/test/test_ohm_typecast.rb +0 -707
- data/test/test_ohm_web_validations.rb +0 -114
data/test/test_ohm_typecast.rb
DELETED
@@ -1,707 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestOhmTypecast < Test::Unit::TestCase
|
4
|
-
context "the default case of just an attribute" do
|
5
|
-
class Post < Ohm::Model
|
6
|
-
include Ohm::Typecast
|
7
|
-
|
8
|
-
attribute :content
|
9
|
-
end
|
10
|
-
|
11
|
-
test "handles nil case correctly" do
|
12
|
-
post = Post.create(:content => nil)
|
13
|
-
post = Post[post.id]
|
14
|
-
|
15
|
-
assert_nil post.content
|
16
|
-
end
|
17
|
-
|
18
|
-
test "still responds to string methods properly" do
|
19
|
-
post = Post.create(:content => "FooBar")
|
20
|
-
post = Post[post.id]
|
21
|
-
|
22
|
-
assert_equal "foobar", post.content.downcase
|
23
|
-
end
|
24
|
-
|
25
|
-
test "mutating methods like upcase!" do
|
26
|
-
post = Post.create(:content => "FooBar")
|
27
|
-
post = Post[post.id]
|
28
|
-
|
29
|
-
post.content.upcase!
|
30
|
-
|
31
|
-
assert_equal "FOOBAR", post.content.to_s
|
32
|
-
end
|
33
|
-
|
34
|
-
test "inspecting" do
|
35
|
-
post = Post.new(:content => "FooBar")
|
36
|
-
assert_equal 'FooBar', post.content
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context "when using a decimal" do
|
41
|
-
class PostDecimal < Ohm::Model
|
42
|
-
include Ohm::Typecast
|
43
|
-
|
44
|
-
attribute :price, Decimal
|
45
|
-
end
|
46
|
-
|
47
|
-
test "handles nil case correctly" do
|
48
|
-
post = PostDecimal.create(:price => nil)
|
49
|
-
post = PostDecimal[post.id]
|
50
|
-
|
51
|
-
assert_nil post.price
|
52
|
-
end
|
53
|
-
|
54
|
-
test "handles empty string case correctly" do
|
55
|
-
post = PostDecimal.create(:price => "")
|
56
|
-
post = PostDecimal[post.id]
|
57
|
-
|
58
|
-
assert_equal "", post.price.to_s
|
59
|
-
end
|
60
|
-
|
61
|
-
test "allows for real arithmetic" do
|
62
|
-
post = PostDecimal.create(:price => "0.01")
|
63
|
-
post = PostDecimal[post.id]
|
64
|
-
|
65
|
-
assert_equal 0.02, post.price + post.price
|
66
|
-
assert_equal 0.0, post.price - post.price
|
67
|
-
assert_equal 0.0001, post.price * post.price
|
68
|
-
assert_equal 1.0, post.price / post.price
|
69
|
-
end
|
70
|
-
|
71
|
-
test "is accurate accdg to the decimal spec" do
|
72
|
-
post = PostDecimal.create(:price => "0.0001")
|
73
|
-
post = PostDecimal[post.id]
|
74
|
-
|
75
|
-
sum = 0
|
76
|
-
1_000.times { sum += post.price }
|
77
|
-
assert_equal 0.1, sum
|
78
|
-
end
|
79
|
-
|
80
|
-
test "using += with price" do
|
81
|
-
post = PostDecimal.create(:price => "0.0001")
|
82
|
-
post = PostDecimal[post.id]
|
83
|
-
|
84
|
-
post.price += 1
|
85
|
-
assert_equal 1.0001, post.price.to_f
|
86
|
-
end
|
87
|
-
|
88
|
-
test "assigning a raw BigDecimal" do
|
89
|
-
post = PostDecimal.create(:price => BigDecimal("399.50"))
|
90
|
-
post = PostDecimal[post.id]
|
91
|
-
|
92
|
-
assert_kind_of String, post.price.to_s
|
93
|
-
end
|
94
|
-
|
95
|
-
test "equality and comparable matching" do
|
96
|
-
post = PostDecimal.create(:price => "399.50")
|
97
|
-
assert (post.price == "399.50")
|
98
|
-
assert (post.price < 399.51)
|
99
|
-
assert (post.price > 399.49)
|
100
|
-
assert (post.price <= 399.50)
|
101
|
-
assert (post.price <= 399.51)
|
102
|
-
assert (post.price >= 399.50)
|
103
|
-
assert (post.price >= 399.49)
|
104
|
-
end
|
105
|
-
|
106
|
-
test "inspecting a Decimal" do
|
107
|
-
post = PostDecimal.new(:price => 399.50)
|
108
|
-
assert_equal '"399.5"', post.price.inspect
|
109
|
-
|
110
|
-
post.price = 'FooBar'
|
111
|
-
assert_equal '"FooBar"', post.price.inspect
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
context "when using an integer" do
|
116
|
-
class Post < Ohm::Model
|
117
|
-
include Ohm::Typecast
|
118
|
-
|
119
|
-
attribute :price, Integer
|
120
|
-
end
|
121
|
-
|
122
|
-
test "handles nil case correctly" do
|
123
|
-
post = Post.create(:price => nil)
|
124
|
-
post = Post[post.id]
|
125
|
-
|
126
|
-
assert_nil post.price
|
127
|
-
end
|
128
|
-
|
129
|
-
test "handles empty string case correctly" do
|
130
|
-
post = Post.create(:price => "")
|
131
|
-
post = Post[post.id]
|
132
|
-
|
133
|
-
assert_equal "", post.price.to_s
|
134
|
-
end
|
135
|
-
|
136
|
-
test "allows respond_to on an invalid integer" do
|
137
|
-
post = Post.new(:price => "FooBar")
|
138
|
-
assert_nothing_raised ArgumentError do
|
139
|
-
post.price.respond_to?(:**)
|
140
|
-
end
|
141
|
-
|
142
|
-
assert ! post.price.respond_to?(:**)
|
143
|
-
end
|
144
|
-
|
145
|
-
test "falls back to String#respond_to? when invalid" do
|
146
|
-
post = Post.new(:price => "FooBar")
|
147
|
-
assert post.price.respond_to?(:capitalize)
|
148
|
-
end
|
149
|
-
|
150
|
-
test "allows for real arithmetic" do
|
151
|
-
post = Post.create(:price => "3")
|
152
|
-
post = Post[post.id]
|
153
|
-
|
154
|
-
assert_equal 6, post.price + post.price
|
155
|
-
assert_equal 0, post.price - post.price
|
156
|
-
assert_equal 9, post.price * post.price
|
157
|
-
assert_equal 1, post.price / post.price
|
158
|
-
end
|
159
|
-
|
160
|
-
test "raises when trying to do arithmetic ops on a non-int" do
|
161
|
-
post = Post.create(:price => "FooBar")
|
162
|
-
post = Post[post.id]
|
163
|
-
|
164
|
-
assert_raise ArgumentError do
|
165
|
-
post.price * post.price
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
test "inspecting" do
|
170
|
-
post = Post.new(:price => "50000")
|
171
|
-
assert_equal '"50000"', post.price.inspect
|
172
|
-
|
173
|
-
post.price = 'FooBar'
|
174
|
-
assert_equal '"FooBar"', post.price.inspect
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
context "when using an float" do
|
179
|
-
class Post < Ohm::Model
|
180
|
-
include Ohm::Typecast
|
181
|
-
|
182
|
-
attribute :price, Float
|
183
|
-
end
|
184
|
-
|
185
|
-
test "handles nil case correctly" do
|
186
|
-
post = Post.create(:price => nil)
|
187
|
-
post = Post[post.id]
|
188
|
-
|
189
|
-
assert_nil post.price
|
190
|
-
end
|
191
|
-
|
192
|
-
test "handles empty string case correctly" do
|
193
|
-
post = Post.create(:price => "")
|
194
|
-
post = Post[post.id]
|
195
|
-
|
196
|
-
assert_equal "", post.price.to_s
|
197
|
-
end
|
198
|
-
|
199
|
-
test "allows for real arithmetic" do
|
200
|
-
post = Post.create(:price => "3")
|
201
|
-
post = Post[post.id]
|
202
|
-
|
203
|
-
assert_equal 6.0, post.price + post.price
|
204
|
-
assert_equal 0.0, post.price - post.price
|
205
|
-
assert_equal 9.0, post.price * post.price
|
206
|
-
assert_equal 1.0, post.price / post.price
|
207
|
-
end
|
208
|
-
|
209
|
-
test "raises when trying to do arithmetic ops on a non-float" do
|
210
|
-
post = Post.create(:price => "FooBar")
|
211
|
-
post = Post[post.id]
|
212
|
-
|
213
|
-
assert_raise ArgumentError do
|
214
|
-
post.price * post.price
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
test "inspecting" do
|
219
|
-
post = Post.new(:price => "12345.67890")
|
220
|
-
assert_equal '"12345.67890"', post.price.inspect
|
221
|
-
|
222
|
-
post.price = 'FooBar'
|
223
|
-
assert_equal '"FooBar"', post.price.inspect
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
context "when using a time" do
|
228
|
-
class Post < Ohm::Model
|
229
|
-
include Ohm::Typecast
|
230
|
-
|
231
|
-
attribute :created_at, Time
|
232
|
-
|
233
|
-
def now
|
234
|
-
Time.now
|
235
|
-
end
|
236
|
-
|
237
|
-
def new_time
|
238
|
-
Time.new
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
test "still able to access top level Time" do
|
243
|
-
assert_equal Post.new.now.to_s, Time.now.to_s
|
244
|
-
end
|
245
|
-
|
246
|
-
test "should be able to use Time.new inside the class" do
|
247
|
-
assert_equal Post.new.new_time.to_s, Time.new.to_s
|
248
|
-
end
|
249
|
-
|
250
|
-
test "handles nil case correctly" do
|
251
|
-
post = Post.create(:created_at => nil)
|
252
|
-
post = Post[post.id]
|
253
|
-
|
254
|
-
assert_nil post.created_at
|
255
|
-
end
|
256
|
-
|
257
|
-
test "handles empty string case correctly" do
|
258
|
-
post = Post.create(:created_at => "")
|
259
|
-
post = Post[post.id]
|
260
|
-
|
261
|
-
assert_equal "", post.created_at.to_s
|
262
|
-
end
|
263
|
-
|
264
|
-
test "allows for real time operations" do
|
265
|
-
post = Post.create(:created_at => "2010-05-10T00:00Z")
|
266
|
-
post = Post[post.id]
|
267
|
-
|
268
|
-
assert_respond_to post.created_at, :strftime
|
269
|
-
assert_equal "2010-05-10", post.created_at.strftime('%Y-%m-%d')
|
270
|
-
end
|
271
|
-
|
272
|
-
test "raises when trying to do non-time operations" do
|
273
|
-
post = Post.create(:created_at => "FooBar")
|
274
|
-
post = Post[post.id]
|
275
|
-
|
276
|
-
assert ! post.created_at.respond_to?(:abs)
|
277
|
-
|
278
|
-
assert_raise NoMethodError do
|
279
|
-
post.created_at.abs
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
test "inspecting" do
|
284
|
-
post = Post.create(:created_at => Time.utc(2010, 05, 05))
|
285
|
-
assert_equal '"2010-05-05 00:00:00 UTC"', post.created_at.inspect
|
286
|
-
|
287
|
-
post.created_at = 'FooBar'
|
288
|
-
assert_equal '"FooBar"', post.created_at.inspect
|
289
|
-
end
|
290
|
-
end
|
291
|
-
|
292
|
-
context "timezones and the Time type" do
|
293
|
-
class Post < Ohm::Model
|
294
|
-
include Ohm::Typecast
|
295
|
-
|
296
|
-
attribute :printed_at, Time
|
297
|
-
end
|
298
|
-
|
299
|
-
test "2010-08-07T00:00Z is parsed as 2010-08-07 00:00:00 UTC" do
|
300
|
-
post = Post.new(:printed_at => "2010-08-07T00:00Z")
|
301
|
-
assert_equal Time.utc(2010, 8, 7).to_s, post.printed_at.utc.to_s
|
302
|
-
|
303
|
-
post.save
|
304
|
-
post = Post[post.id]
|
305
|
-
assert_equal Time.utc(2010, 8, 7).to_s, post.printed_at.utc.to_s
|
306
|
-
end
|
307
|
-
|
308
|
-
test "2010-08-07 18:29Z is parsed as 2010-08-07 18:29:00 UTC" do
|
309
|
-
post = Post.new(:printed_at => "2010-08-07 18:29Z")
|
310
|
-
assert_equal Time.utc(2010, 8, 7, 18, 29).to_s, post.printed_at.utc.to_s
|
311
|
-
|
312
|
-
post.save
|
313
|
-
post = Post[post.id]
|
314
|
-
assert_equal Time.utc(2010, 8, 7, 18, 29).to_s, post.printed_at.utc.to_s
|
315
|
-
end
|
316
|
-
|
317
|
-
test "2010-08-07T18:29:31Z is parsed as 2010-08-07 18:29:31 UTC" do
|
318
|
-
post = Post.new(:printed_at => "2010-08-07T18:29:31Z")
|
319
|
-
assert_equal Time.utc(2010, 8, 7, 18, 29, 31).to_s, post.printed_at.utc.to_s
|
320
|
-
|
321
|
-
post.save
|
322
|
-
post = Post[post.id]
|
323
|
-
assert_equal Time.utc(2010, 8, 7, 18, 29, 31).to_s, post.printed_at.utc.to_s
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
context "when using a date" do
|
328
|
-
class Post < Ohm::Model
|
329
|
-
include Ohm::Typecast
|
330
|
-
|
331
|
-
attribute :created_on, Date
|
332
|
-
|
333
|
-
def today
|
334
|
-
::Date.today
|
335
|
-
end
|
336
|
-
|
337
|
-
def date
|
338
|
-
Date
|
339
|
-
end
|
340
|
-
|
341
|
-
def may_5
|
342
|
-
Date.new(2010, 05, 05)
|
343
|
-
end
|
344
|
-
|
345
|
-
def base_today
|
346
|
-
Date.today
|
347
|
-
end
|
348
|
-
end
|
349
|
-
|
350
|
-
test "still able to get top level methods" do
|
351
|
-
assert_equal Date.today, Post.new.base_today
|
352
|
-
end
|
353
|
-
|
354
|
-
test "allows instantiation of dates" do
|
355
|
-
assert_equal Date.new(2010, 05, 05), Post.new.may_5
|
356
|
-
end
|
357
|
-
|
358
|
-
test "handles nil case correctly" do
|
359
|
-
post = Post.create(:created_on => nil)
|
360
|
-
post = Post[post.id]
|
361
|
-
|
362
|
-
assert_nil post.created_on
|
363
|
-
end
|
364
|
-
|
365
|
-
test "handles empty string case correctly" do
|
366
|
-
post = Post.create(:created_on => "")
|
367
|
-
post = Post[post.id]
|
368
|
-
|
369
|
-
assert_equal "", post.created_on.to_s
|
370
|
-
end
|
371
|
-
|
372
|
-
test "allows for real time operations" do
|
373
|
-
post = Post.create(:created_on => "2010-05-10")
|
374
|
-
post = Post[post.id]
|
375
|
-
|
376
|
-
assert_respond_to post.created_on, :strftime
|
377
|
-
assert_equal "2010-05-10", post.created_on.strftime('%Y-%m-%d')
|
378
|
-
end
|
379
|
-
|
380
|
-
test "raises when trying to do date operations on a non-date" do
|
381
|
-
post = Post.create(:created_on => "FooBar")
|
382
|
-
post = Post[post.id]
|
383
|
-
|
384
|
-
assert_raise ArgumentError do
|
385
|
-
post.created_on.strftime("%Y")
|
386
|
-
end
|
387
|
-
end
|
388
|
-
|
389
|
-
test "still able to access Date" do
|
390
|
-
assert_equal Date.today, Post.new.today
|
391
|
-
end
|
392
|
-
|
393
|
-
test "inspecting" do
|
394
|
-
post = Post.create(:created_on => Date.new(2010, 5, 5))
|
395
|
-
assert_equal '"2010-05-05"', post.created_on.inspect
|
396
|
-
|
397
|
-
post.created_on = 'FooBar'
|
398
|
-
assert_equal '"FooBar"', post.created_on.inspect
|
399
|
-
end
|
400
|
-
end
|
401
|
-
|
402
|
-
context "when using a Hash" do
|
403
|
-
class Post < Ohm::Model
|
404
|
-
include Ohm::Typecast
|
405
|
-
|
406
|
-
attribute :address, Hash
|
407
|
-
|
408
|
-
def hash
|
409
|
-
Hash.new
|
410
|
-
end
|
411
|
-
|
412
|
-
def top_level_hash
|
413
|
-
Hash
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
test "importing" do
|
418
|
-
assert_equal Hash.new, Ohm::Types::Hash[nil]
|
419
|
-
assert_equal Hash.new, Ohm::Types::Hash[""]
|
420
|
-
assert_equal Hash.new, Ohm::Types::Hash[{}]
|
421
|
-
|
422
|
-
assert_equal Hash[:a => "b", :c => "d"],
|
423
|
-
Ohm::Types::Hash[{ :a => "b", :c => "d" }]
|
424
|
-
end
|
425
|
-
|
426
|
-
test "exporting / dumping" do
|
427
|
-
assert_equal "{}", Ohm::Types::Hash[nil].to_s
|
428
|
-
assert_equal "{}", Ohm::Types::Hash[""].to_s
|
429
|
-
assert_equal "{}", Ohm::Types::Hash[{}].to_s
|
430
|
-
|
431
|
-
assert_equal %q{{"a":"b","c":"d"}},
|
432
|
-
Ohm::Types::Hash[{ :a => "b", :c => "d" }].to_s
|
433
|
-
end
|
434
|
-
|
435
|
-
test "still able to get top level methods" do
|
436
|
-
assert_equal({}, Post.new.hash)
|
437
|
-
assert_equal Hash, Post.new.top_level_hash
|
438
|
-
end
|
439
|
-
|
440
|
-
test "handles nil case correctly" do
|
441
|
-
post = Post.create(:address => nil)
|
442
|
-
assert_equal({}, post.address)
|
443
|
-
|
444
|
-
post = Post[post.id]
|
445
|
-
assert_equal({}, post.address)
|
446
|
-
end
|
447
|
-
|
448
|
-
test "handles empty string case correctly" do
|
449
|
-
post = Post.create(:address => "")
|
450
|
-
assert_equal({}, post.address)
|
451
|
-
|
452
|
-
post = Post[post.id]
|
453
|
-
assert_equal({}, post.address)
|
454
|
-
end
|
455
|
-
|
456
|
-
test "handles populated hashes" do
|
457
|
-
address = { "address1" => "#123", "city" => "Singapore", "country" => "SG"}
|
458
|
-
post = Post.create(:address => address)
|
459
|
-
assert_equal address, post.address
|
460
|
-
|
461
|
-
post = Post[post.id]
|
462
|
-
assert_equal address, post.address
|
463
|
-
end
|
464
|
-
|
465
|
-
test "allows for hash operations" do
|
466
|
-
address = { "address1" => "#123", "city" => "Singapore", "country" => "SG"}
|
467
|
-
post = Post.create(:address => address)
|
468
|
-
|
469
|
-
assert_equal ["address1", "city", "country"], post.address.keys
|
470
|
-
assert_equal ["#123", "Singapore", "SG"], post.address.values
|
471
|
-
|
472
|
-
post = Post[post.id]
|
473
|
-
assert_equal ["address1", "city", "country"], post.address.keys
|
474
|
-
assert_equal ["#123", "Singapore", "SG"], post.address.values
|
475
|
-
end
|
476
|
-
|
477
|
-
test "handles mutation" do
|
478
|
-
address = { "address1" => "#123", "city" => "Singapore", "country" => "SG"}
|
479
|
-
post = Post.create(:address => address)
|
480
|
-
|
481
|
-
post.address["address1"] = "#456"
|
482
|
-
post.save
|
483
|
-
|
484
|
-
assert_equal ["address1", "city", "country"], post.address.keys
|
485
|
-
assert_equal ["#456", "Singapore", "SG"], post.address.values
|
486
|
-
|
487
|
-
post = Post[post.id]
|
488
|
-
assert_equal ["address1", "city", "country"], post.address.keys
|
489
|
-
assert_equal ["#456", "Singapore", "SG"], post.address.values
|
490
|
-
end
|
491
|
-
|
492
|
-
Address = Class.new(Struct.new(:city, :country))
|
493
|
-
|
494
|
-
test "raises when trying to assign a non-hash" do
|
495
|
-
assert_raise TypeError do
|
496
|
-
Post.new(:address => [])
|
497
|
-
end
|
498
|
-
|
499
|
-
assert_raise TypeError do
|
500
|
-
Post.new(:address => Address.new)
|
501
|
-
end
|
502
|
-
end
|
503
|
-
|
504
|
-
test "inspecting" do
|
505
|
-
post = Post.create(:address => { "address1" => "#456",
|
506
|
-
"city" => "Singapore",
|
507
|
-
"country" => "SG" })
|
508
|
-
|
509
|
-
assert_equal %q{{"address1":"#456","city":"Singapore","country":"SG"}},
|
510
|
-
post.address.inspect
|
511
|
-
|
512
|
-
post.address = 'FooBar'
|
513
|
-
assert_equal %{"\\\"FooBar\\\""}, post.address.inspect
|
514
|
-
end
|
515
|
-
end
|
516
|
-
|
517
|
-
context "when using an Array" do
|
518
|
-
class Post < Ohm::Model
|
519
|
-
include Ohm::Typecast
|
520
|
-
|
521
|
-
attribute :addresses, Array
|
522
|
-
|
523
|
-
def array
|
524
|
-
Array.new
|
525
|
-
end
|
526
|
-
|
527
|
-
def top_level_array
|
528
|
-
Array
|
529
|
-
end
|
530
|
-
end
|
531
|
-
|
532
|
-
test "importing" do
|
533
|
-
assert_equal [], Ohm::Types::Array[nil]
|
534
|
-
assert_equal [], Ohm::Types::Array[""]
|
535
|
-
assert_equal [], Ohm::Types::Array[[]]
|
536
|
-
|
537
|
-
assert_equal ['a', 'b', 'c', 'd'],
|
538
|
-
Ohm::Types::Array[['a', 'b', 'c', 'd']]
|
539
|
-
end
|
540
|
-
|
541
|
-
test "exporting / dumping" do
|
542
|
-
assert_equal "[]", Ohm::Types::Array[nil].to_s
|
543
|
-
assert_equal "[]", Ohm::Types::Array[""].to_s
|
544
|
-
assert_equal "[]", Ohm::Types::Array[[]].to_s
|
545
|
-
|
546
|
-
assert_equal %q{["a","b","c","d"]},
|
547
|
-
Ohm::Types::Array[['a', 'b', 'c', 'd']].to_s
|
548
|
-
end
|
549
|
-
|
550
|
-
test "still able to get top level methods" do
|
551
|
-
assert_equal([], Post.new.array)
|
552
|
-
assert_equal Array, Post.new.top_level_array
|
553
|
-
end
|
554
|
-
|
555
|
-
test "handles nil case correctly" do
|
556
|
-
post = Post.create(:addresses => nil)
|
557
|
-
assert_equal([], post.addresses)
|
558
|
-
|
559
|
-
post = Post[post.id]
|
560
|
-
assert_equal([], post.addresses)
|
561
|
-
end
|
562
|
-
|
563
|
-
test "handles empty string case correctly" do
|
564
|
-
post = Post.create(:addresses => "")
|
565
|
-
assert_equal([], post.addresses)
|
566
|
-
|
567
|
-
post = Post[post.id]
|
568
|
-
assert_equal([], post.addresses)
|
569
|
-
end
|
570
|
-
|
571
|
-
test "handles populated arrays" do
|
572
|
-
addresses = [{"city" => "Singapore", "country" => "SG"},
|
573
|
-
{"city" => "Manila", "country" => "PH"}]
|
574
|
-
|
575
|
-
post = Post.create(:addresses => addresses)
|
576
|
-
assert_equal addresses, post.addresses
|
577
|
-
|
578
|
-
post = Post[post.id]
|
579
|
-
assert_equal addresses, post.addresses
|
580
|
-
end
|
581
|
-
|
582
|
-
class AddressArr < Class.new(Struct.new(:city, :country))
|
583
|
-
def to_json(*args)
|
584
|
-
[city, country].to_json(*args)
|
585
|
-
end
|
586
|
-
end
|
587
|
-
|
588
|
-
test "handles an arbitrary class as an element of the array" do
|
589
|
-
addresses = [AddressArr.new("Singapore", "SG"),
|
590
|
-
AddressArr.new("Philippines", "PH")]
|
591
|
-
|
592
|
-
post = Post.create(:addresses => addresses)
|
593
|
-
assert_equal [['Singapore', 'SG'], ['Philippines', 'PH']], post.addresses
|
594
|
-
|
595
|
-
post = Post[post.id]
|
596
|
-
assert_equal [['Singapore', 'SG'], ['Philippines', 'PH']], post.addresses
|
597
|
-
end
|
598
|
-
|
599
|
-
test "allows for array operations" do
|
600
|
-
addresses = [{"city" => "Singapore", "country" => "SG"},
|
601
|
-
{"city" => "Manila", "country" => "PH"}]
|
602
|
-
|
603
|
-
|
604
|
-
post = Post.create(:addresses => addresses)
|
605
|
-
assert_equal 2, post.addresses.size
|
606
|
-
assert_equal addresses + [{"city" => "Hong Kong", "country" => "ZN"}],
|
607
|
-
post.addresses.push({"city" => "Hong Kong", "country" => "ZN"})
|
608
|
-
|
609
|
-
post = Post[post.id]
|
610
|
-
assert_equal 2, post.addresses.size
|
611
|
-
assert_equal addresses + [{"city" => "Hong Kong", "country" => "ZN"}],
|
612
|
-
post.addresses.push({"city" => "Hong Kong", "country" => "ZN"})
|
613
|
-
end
|
614
|
-
|
615
|
-
test "looping! and other enumerablems" do
|
616
|
-
array = [1, 2, 3]
|
617
|
-
post = Post.create(:addresses => array)
|
618
|
-
|
619
|
-
total = 0
|
620
|
-
post.addresses.each { |e| total += e }
|
621
|
-
assert_equal 6, total
|
622
|
-
|
623
|
-
post = Post[post.id]
|
624
|
-
total = 0
|
625
|
-
post.addresses.each { |e| total += e }
|
626
|
-
assert_equal 6, total
|
627
|
-
end
|
628
|
-
|
629
|
-
test "handles mutation" do
|
630
|
-
post = Post.create(:addresses => [1, 2, 3])
|
631
|
-
|
632
|
-
post.addresses.push(4, 5, 6)
|
633
|
-
post.save
|
634
|
-
|
635
|
-
assert_equal 6, post.addresses.size
|
636
|
-
assert_equal [1, 2, 3, 4, 5, 6], post.addresses
|
637
|
-
|
638
|
-
post = Post[post.id]
|
639
|
-
assert_equal 6, post.addresses.size
|
640
|
-
assert_equal [1, 2, 3, 4, 5, 6], post.addresses
|
641
|
-
end
|
642
|
-
|
643
|
-
|
644
|
-
test "raises when trying to assign a non-array" do
|
645
|
-
assert_raise TypeError do
|
646
|
-
Post.new(:addresses => {})
|
647
|
-
end
|
648
|
-
|
649
|
-
assert_raise TypeError do
|
650
|
-
Post.new(:addresses => AddressArr.new)
|
651
|
-
end
|
652
|
-
end
|
653
|
-
|
654
|
-
test "inspecting" do
|
655
|
-
post = Post.create(:addresses => [{ "address1" => "#456",
|
656
|
-
"city" => "Singapore",
|
657
|
-
"country" => "SG" }])
|
658
|
-
|
659
|
-
assert_equal %q{[{"address1":"#456","city":"Singapore","country":"SG"}]},
|
660
|
-
post.addresses.inspect
|
661
|
-
|
662
|
-
post.addresses = 'FooBar'
|
663
|
-
assert_equal %{"\\\"FooBar\\\""}, post.addresses.inspect
|
664
|
-
end
|
665
|
-
end
|
666
|
-
|
667
|
-
context "when using Boolean" do
|
668
|
-
class User < Ohm::Model
|
669
|
-
include Ohm::Typecast
|
670
|
-
|
671
|
-
attribute :is_admin, Boolean
|
672
|
-
end
|
673
|
-
|
674
|
-
test "empty is nil" do
|
675
|
-
assert_equal nil, User.new.is_admin
|
676
|
-
|
677
|
-
u = User.create
|
678
|
-
u = User[u.id]
|
679
|
-
|
680
|
-
assert_equal nil, User.new.is_admin
|
681
|
-
end
|
682
|
-
|
683
|
-
test "false, 0, '0' is false" do
|
684
|
-
[false, 0, '0'].each do |val|
|
685
|
-
assert_equal false, User.new(:is_admin => val).is_admin
|
686
|
-
end
|
687
|
-
|
688
|
-
[false, 0, '0'].each do |val|
|
689
|
-
u = User.create(:is_admin => val)
|
690
|
-
u = User[u.id]
|
691
|
-
assert_equal false, u.is_admin
|
692
|
-
end
|
693
|
-
end
|
694
|
-
|
695
|
-
test "true, 1, '1' is true" do
|
696
|
-
[true, 1, '1'].each do |val|
|
697
|
-
assert_equal true, User.new(:is_admin => val).is_admin
|
698
|
-
end
|
699
|
-
|
700
|
-
[true, 1, '1'].each do |val|
|
701
|
-
u = User.create(:is_admin => val)
|
702
|
-
u = User[u.id]
|
703
|
-
assert_equal true, u.is_admin
|
704
|
-
end
|
705
|
-
end
|
706
|
-
end
|
707
|
-
end
|