-
Notifications
You must be signed in to change notification settings - Fork 43
/
custom_type_test.rb
68 lines (52 loc) · 1.52 KB
/
custom_type_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require_relative '../test_helper'
class CustomTypeTest < Minitest::Test
Alba.register_type :iso8601, converter: ->(time) { time.iso8601(3) }, auto_convert: true
Alba.register_type :iso8601_no_auto, converter: ->(time) { time.iso8601(3) }
Alba.register_type :less_than_three, check: ->(obj) { obj.is_a?(Integer) && obj < 3 }
User = Struct.new(:id, :created_at)
def setup
@t = Time.now
@user = User.new(1, @t)
end
class UserResource
include Alba::Resource
attributes :id, created_at: :iso8601
end
def test_custom_type_with_auto_convert
assert_equal(
%({"id":1,"created_at":"#{@t.iso8601(3)}"}),
UserResource.new(@user).serialize
)
end
class UserResource2
include Alba::Resource
attributes :id, created_at: :iso8601_no_auto
end
def test_custom_type_without_auto_convert
assert_raises(TypeError) { UserResource2.new(@user).serialize }
end
class UserResource3
include Alba::Resource
attributes :id, created_at: [:iso8601_no_auto, true]
end
def test_custom_type_without_auto_convert_but_with_true
assert_equal(
%({"id":1,"created_at":"#{@t.iso8601(3)}"}),
UserResource.new(@user).serialize
)
end
class UserResource4
include Alba::Resource
attributes id: :less_than_three
end
def test_custom_type_check_only
t = Time.now
user = User.new(4, t)
assert_raises(TypeError) { UserResource4.new(user).serialize }
assert_equal(
%({"id":1}),
UserResource4.new(@user).serialize
)
end
Alba.reset!
end