|
4 | 4 |
|
5 | 5 | module ActiveModel
|
6 | 6 | class AttributeTest < ActiveModel::TestCase
|
7 |
| - setup do |
8 |
| - @type = Minitest::Mock.new |
| 7 | + class InscribingType |
| 8 | + def cast(value) |
| 9 | + "cast(#{value})" |
| 10 | + end |
| 11 | + |
| 12 | + def serialize(value) |
| 13 | + "serialize(#{value})" |
| 14 | + end |
| 15 | + |
| 16 | + def deserialize(value) |
| 17 | + "deserialize(#{value})" |
| 18 | + end |
9 | 19 | end
|
10 | 20 |
|
11 |
| - teardown do |
12 |
| - assert @type.verify |
| 21 | + setup do |
| 22 | + @type = InscribingType.new |
13 | 23 | end
|
14 | 24 |
|
15 | 25 | test "from_database + read type casts from database" do
|
16 |
| - @type.expect(:deserialize, "type cast from database", ["a value"]) |
17 | 26 | attribute = Attribute.from_database(nil, "a value", @type)
|
18 | 27 |
|
19 |
| - type_cast_value = attribute.value |
20 |
| - |
21 |
| - assert_equal "type cast from database", type_cast_value |
| 28 | + assert_equal "deserialize(a value)", attribute.value |
22 | 29 | end
|
23 | 30 |
|
24 | 31 | test "from_user + read type casts from user" do
|
25 |
| - @type.expect(:cast, "type cast from user", ["a value"]) |
26 | 32 | attribute = Attribute.from_user(nil, "a value", @type)
|
27 | 33 |
|
28 |
| - type_cast_value = attribute.value |
29 |
| - |
30 |
| - assert_equal "type cast from user", type_cast_value |
| 34 | + assert_equal "cast(a value)", attribute.value |
31 | 35 | end
|
32 | 36 |
|
33 | 37 | test "reading memoizes the value" do
|
34 |
| - @type.expect(:deserialize, "from the database", ["whatever"]) |
35 |
| - attribute = Attribute.from_database(nil, "whatever", @type) |
| 38 | + count = 0 |
| 39 | + @type.define_singleton_method(:deserialize) do |value| |
| 40 | + count += 1 |
| 41 | + value |
| 42 | + end |
36 | 43 |
|
37 |
| - type_cast_value = attribute.value |
38 |
| - second_read = attribute.value |
| 44 | + attribute = Attribute.from_database(nil, "whatever", @type) |
39 | 45 |
|
40 |
| - assert_equal "from the database", type_cast_value |
41 |
| - assert_same type_cast_value, second_read |
| 46 | + attribute.value |
| 47 | + attribute.value |
| 48 | + assert_equal 1, count |
42 | 49 | end
|
43 | 50 |
|
44 | 51 | test "reading memoizes falsy values" do
|
45 |
| - @type.expect(:deserialize, false, ["whatever"]) |
| 52 | + count = 0 |
| 53 | + @type.define_singleton_method(:deserialize) do |value| |
| 54 | + count += 1 |
| 55 | + false |
| 56 | + end |
| 57 | + |
46 | 58 | attribute = Attribute.from_database(nil, "whatever", @type)
|
47 | 59 |
|
48 | 60 | attribute.value
|
49 | 61 | attribute.value
|
| 62 | + assert_equal 1, count |
50 | 63 | end
|
51 | 64 |
|
52 |
| - test "read_before_typecast returns the given value" do |
| 65 | + test "value_before_type_cast returns the given value" do |
53 | 66 | attribute = Attribute.from_database(nil, "raw value", @type)
|
54 | 67 |
|
55 | 68 | raw_value = attribute.value_before_type_cast
|
56 | 69 |
|
57 | 70 | assert_equal "raw value", raw_value
|
58 | 71 | end
|
59 | 72 |
|
60 |
| - test "from_database + read_for_database type casts to and from database" do |
61 |
| - @type.expect(:deserialize, "read from database", ["whatever"]) |
62 |
| - @type.expect(:serialize, "ready for database", ["read from database"]) |
| 73 | + test "from_database + value_for_database type casts to and from database" do |
63 | 74 | attribute = Attribute.from_database(nil, "whatever", @type)
|
64 | 75 |
|
65 |
| - serialize = attribute.value_for_database |
66 |
| - |
67 |
| - assert_equal "ready for database", serialize |
| 76 | + assert_equal "serialize(deserialize(whatever))", attribute.value_for_database |
68 | 77 | end
|
69 | 78 |
|
70 |
| - test "from_user + read_for_database type casts from the user to the database" do |
71 |
| - @type.expect(:cast, "read from user", ["whatever"]) |
72 |
| - @type.expect(:serialize, "ready for database", ["read from user"]) |
| 79 | + test "from_user + value_for_database type casts from the user to the database" do |
73 | 80 | attribute = Attribute.from_user(nil, "whatever", @type)
|
74 | 81 |
|
75 |
| - serialize = attribute.value_for_database |
76 |
| - |
77 |
| - assert_equal "ready for database", serialize |
| 82 | + assert_equal "serialize(cast(whatever))", attribute.value_for_database |
78 | 83 | end
|
79 | 84 |
|
80 | 85 | test "duping dups the value" do
|
81 |
| - @type.expect(:deserialize, +"type cast", ["a value"]) |
82 | 86 | attribute = Attribute.from_database(nil, "a value", @type)
|
83 | 87 |
|
84 |
| - value_from_orig = attribute.value |
85 |
| - value_from_clone = attribute.dup.value |
86 |
| - value_from_orig << " foo" |
87 |
| - |
88 |
| - assert_equal "type cast foo", value_from_orig |
89 |
| - assert_equal "type cast", value_from_clone |
| 88 | + assert_not_same attribute.value, attribute.dup.value |
90 | 89 | end
|
91 | 90 |
|
92 | 91 | test "duping does not dup the value if it is not dupable" do
|
93 |
| - @type.expect(:deserialize, false, ["a value"]) |
94 |
| - attribute = Attribute.from_database(nil, "a value", @type) |
| 92 | + @type.define_singleton_method(:deserialize) { |value| value } |
| 93 | + attribute = Attribute.from_database(nil, false, @type) |
95 | 94 |
|
96 | 95 | assert_same attribute.value, attribute.dup.value
|
97 | 96 | end
|
98 | 97 |
|
99 | 98 | test "duping does not eagerly type cast if we have not yet type cast" do
|
| 99 | + @type.define_singleton_method(:deserialize) { flunk } |
100 | 100 | attribute = Attribute.from_database(nil, "a value", @type)
|
| 101 | + |
101 | 102 | attribute.dup
|
102 | 103 | end
|
103 | 104 |
|
|
0 commit comments