-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
attributes_test.rb
152 lines (121 loc) · 4.3 KB
/
attributes_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# frozen_string_literal: true
require "cases/helper"
module ActiveModel
class AttributesTest < ActiveModel::TestCase
class ModelForAttributesTest
include ActiveModel::Model
include ActiveModel::Attributes
attribute :integer_field, :integer
attribute :string_field, :string
attribute :decimal_field, :decimal
attribute :string_with_default, :string, default: "default string"
attribute :date_field, :date, default: -> { Date.new(2016, 1, 1) }
attribute :boolean_field, :boolean
end
class ChildModelForAttributesTest < ModelForAttributesTest
end
class GrandchildModelForAttributesTest < ChildModelForAttributesTest
attribute :integer_field, :string
attribute :string_field, default: "default string"
end
test "properties assignment" do
data = ModelForAttributesTest.new(
integer_field: "2.3",
string_field: "Rails FTW",
decimal_field: "12.3",
boolean_field: "0"
)
assert_equal 2, data.integer_field
assert_equal "Rails FTW", data.string_field
assert_equal BigDecimal("12.3"), data.decimal_field
assert_equal "default string", data.string_with_default
assert_equal Date.new(2016, 1, 1), data.date_field
assert_equal false, data.boolean_field
data.integer_field = 10
data.string_with_default = nil
data.boolean_field = "1"
assert_equal 10, data.integer_field
assert_nil data.string_with_default
assert_equal true, data.boolean_field
end
test "reading attributes" do
data = ModelForAttributesTest.new(
integer_field: 1.1,
string_field: 1.1,
decimal_field: 1.1,
boolean_field: 1.1
)
expected_attributes = {
integer_field: 1,
string_field: "1.1",
decimal_field: BigDecimal("1.1"),
string_with_default: "default string",
date_field: Date.new(2016, 1, 1),
boolean_field: true
}.stringify_keys
assert_equal expected_attributes, data.attributes
end
test "reading attribute names" do
names = [
"integer_field",
"string_field",
"decimal_field",
"string_with_default",
"date_field",
"boolean_field"
]
assert_equal names, ModelForAttributesTest.attribute_names
assert_equal names, ModelForAttributesTest.new.attribute_names
end
test "nonexistent attribute" do
assert_raise ActiveModel::UnknownAttributeError do
ModelForAttributesTest.new(nonexistent: "nonexistent")
end
end
test "children inherit attributes" do
data = ChildModelForAttributesTest.new(integer_field: "4.4")
assert_equal 4, data.integer_field
end
test "children can override parents" do
klass = GrandchildModelForAttributesTest
assert_instance_of Type::String, klass.attribute_types["integer_field"]
assert_instance_of Type::String, klass.attribute_types["string_field"]
data = GrandchildModelForAttributesTest.new(integer_field: "4.4")
assert_equal "4.4", data.integer_field
assert_equal "default string", data.string_field
end
test "attributes with proc defaults can be marshalled" do
data = ModelForAttributesTest.new
attributes = data.instance_variable_get(:@attributes)
round_tripped = Marshal.load(Marshal.dump(data))
new_attributes = round_tripped.instance_variable_get(:@attributes)
assert_equal attributes, new_attributes
end
test "attributes can be dup-ed" do
data = ModelForAttributesTest.new
data.integer_field = 1
duped = data.dup
assert_equal 1, data.integer_field
assert_equal 1, duped.integer_field
duped.integer_field = 2
assert_equal 1, data.integer_field
assert_equal 2, duped.integer_field
end
test "can't modify attributes if frozen" do
data = ModelForAttributesTest.new
data.freeze
assert data.frozen?
assert_raise(FrozenError) { data.integer_field = 1 }
end
test "attributes can be frozen again" do
data = ModelForAttributesTest.new
data.freeze
assert_nothing_raised { data.freeze }
end
test "unknown type error is raised" do
assert_raise(ArgumentError) do
ModelForAttributesTest.attribute :foo, :unknown
end
end
end
end