2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
2014-06-07 15:46:22 -04:00
|
|
|
|
2017-10-19 12:45:07 -04:00
|
|
|
module ActiveModel
|
|
|
|
class AttributeTest < ActiveModel::TestCase
|
2014-06-07 15:46:22 -04:00
|
|
|
setup do
|
2014-08-02 12:00:28 -04:00
|
|
|
@type = Minitest::Mock.new
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
assert @type.verify
|
|
|
|
end
|
|
|
|
|
|
|
|
test "from_database + read type casts from database" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@type.expect(:deserialize, "type cast from database", ["a value"])
|
|
|
|
attribute = Attribute.from_database(nil, "a value", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
|
|
|
|
type_cast_value = attribute.value
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "type cast from database", type_cast_value
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "from_user + read type casts from user" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@type.expect(:cast, "type cast from user", ["a value"])
|
|
|
|
attribute = Attribute.from_user(nil, "a value", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
|
|
|
|
type_cast_value = attribute.value
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "type cast from user", type_cast_value
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "reading memoizes the value" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@type.expect(:deserialize, "from the database", ["whatever"])
|
|
|
|
attribute = Attribute.from_database(nil, "whatever", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
|
|
|
|
type_cast_value = attribute.value
|
|
|
|
second_read = attribute.value
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "from the database", type_cast_value
|
2014-06-07 15:46:22 -04:00
|
|
|
assert_same type_cast_value, second_read
|
|
|
|
end
|
|
|
|
|
|
|
|
test "reading memoizes falsy values" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@type.expect(:deserialize, false, ["whatever"])
|
|
|
|
attribute = Attribute.from_database(nil, "whatever", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
|
|
|
|
attribute.value
|
|
|
|
attribute.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test "read_before_typecast returns the given value" do
|
2016-08-06 12:26:20 -04:00
|
|
|
attribute = Attribute.from_database(nil, "raw value", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
|
|
|
|
raw_value = attribute.value_before_type_cast
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "raw value", raw_value
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "from_database + read_for_database type casts to and from database" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@type.expect(:deserialize, "read from database", ["whatever"])
|
|
|
|
@type.expect(:serialize, "ready for database", ["read from database"])
|
|
|
|
attribute = Attribute.from_database(nil, "whatever", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
|
2015-02-17 15:35:23 -05:00
|
|
|
serialize = attribute.value_for_database
|
2014-06-07 15:46:22 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "ready for database", serialize
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "from_user + read_for_database type casts from the user to the database" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@type.expect(:cast, "read from user", ["whatever"])
|
|
|
|
@type.expect(:serialize, "ready for database", ["read from user"])
|
|
|
|
attribute = Attribute.from_user(nil, "whatever", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
|
2015-02-17 15:35:23 -05:00
|
|
|
serialize = attribute.value_for_database
|
2014-06-07 15:46:22 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "ready for database", serialize
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "duping dups the value" do
|
2018-05-17 04:32:27 -04:00
|
|
|
@type.expect(:deserialize, +"type cast", ["a value"])
|
2016-08-06 12:26:20 -04:00
|
|
|
attribute = Attribute.from_database(nil, "a value", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
|
|
|
|
value_from_orig = attribute.value
|
|
|
|
value_from_clone = attribute.dup.value
|
2016-08-06 12:26:20 -04:00
|
|
|
value_from_orig << " foo"
|
2014-06-07 15:46:22 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "type cast foo", value_from_orig
|
|
|
|
assert_equal "type cast", value_from_clone
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "duping does not dup the value if it is not dupable" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@type.expect(:deserialize, false, ["a value"])
|
|
|
|
attribute = Attribute.from_database(nil, "a value", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
|
|
|
|
assert_same attribute.value, attribute.dup.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test "duping does not eagerly type cast if we have not yet type cast" do
|
2016-08-06 12:26:20 -04:00
|
|
|
attribute = Attribute.from_database(nil, "a value", @type)
|
2014-06-07 15:46:22 -04:00
|
|
|
attribute.dup
|
|
|
|
end
|
2014-06-22 18:25:40 -04:00
|
|
|
|
|
|
|
class MyType
|
2015-02-17 15:39:42 -05:00
|
|
|
def cast(value)
|
2014-06-22 18:25:40 -04:00
|
|
|
value + " from user"
|
|
|
|
end
|
|
|
|
|
2015-02-17 13:29:51 -05:00
|
|
|
def deserialize(value)
|
2014-06-22 18:25:40 -04:00
|
|
|
value + " from database"
|
|
|
|
end
|
2015-09-24 13:50:11 -04:00
|
|
|
|
|
|
|
def assert_valid_value(*)
|
|
|
|
end
|
2014-06-22 18:25:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with_value_from_user returns a new attribute with the value from the user" do
|
2014-06-22 18:57:40 -04:00
|
|
|
old = Attribute.from_database(nil, "old", MyType.new)
|
2014-06-22 18:25:40 -04:00
|
|
|
new = old.with_value_from_user("new")
|
|
|
|
|
|
|
|
assert_equal "old from database", old.value
|
|
|
|
assert_equal "new from user", new.value
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with_value_from_database returns a new attribute with the value from the database" do
|
2014-06-22 18:57:40 -04:00
|
|
|
old = Attribute.from_user(nil, "old", MyType.new)
|
2014-06-22 18:25:40 -04:00
|
|
|
new = old.with_value_from_database("new")
|
|
|
|
|
|
|
|
assert_equal "old from user", old.value
|
|
|
|
assert_equal "new from database", new.value
|
|
|
|
end
|
2014-06-22 18:57:40 -04:00
|
|
|
|
|
|
|
test "uninitialized attributes yield their name if a block is given to value" do
|
|
|
|
block = proc { |name| name.to_s + "!" }
|
|
|
|
foo = Attribute.uninitialized(:foo, nil)
|
|
|
|
bar = Attribute.uninitialized(:bar, nil)
|
|
|
|
|
|
|
|
assert_equal "foo!", foo.value(&block)
|
|
|
|
assert_equal "bar!", bar.value(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "uninitialized attributes have no value" do
|
|
|
|
assert_nil Attribute.uninitialized(:foo, nil).value
|
|
|
|
end
|
2014-08-15 15:37:53 -04:00
|
|
|
|
|
|
|
test "attributes equal other attributes with the same constructor arguments" do
|
|
|
|
first = Attribute.from_database(:foo, 1, Type::Integer.new)
|
|
|
|
second = Attribute.from_database(:foo, 1, Type::Integer.new)
|
|
|
|
assert_equal first, second
|
|
|
|
end
|
|
|
|
|
|
|
|
test "attributes do not equal attributes with different names" do
|
|
|
|
first = Attribute.from_database(:foo, 1, Type::Integer.new)
|
|
|
|
second = Attribute.from_database(:bar, 1, Type::Integer.new)
|
|
|
|
assert_not_equal first, second
|
|
|
|
end
|
|
|
|
|
|
|
|
test "attributes do not equal attributes with different types" do
|
|
|
|
first = Attribute.from_database(:foo, 1, Type::Integer.new)
|
|
|
|
second = Attribute.from_database(:foo, 1, Type::Float.new)
|
|
|
|
assert_not_equal first, second
|
|
|
|
end
|
|
|
|
|
|
|
|
test "attributes do not equal attributes with different values" do
|
|
|
|
first = Attribute.from_database(:foo, 1, Type::Integer.new)
|
|
|
|
second = Attribute.from_database(:foo, 2, Type::Integer.new)
|
|
|
|
assert_not_equal first, second
|
|
|
|
end
|
|
|
|
|
|
|
|
test "attributes do not equal attributes of other classes" do
|
|
|
|
first = Attribute.from_database(:foo, 1, Type::Integer.new)
|
|
|
|
second = Attribute.from_user(:foo, 1, Type::Integer.new)
|
|
|
|
assert_not_equal first, second
|
|
|
|
end
|
2015-01-20 16:09:53 -05:00
|
|
|
|
|
|
|
test "an attribute has not been read by default" do
|
|
|
|
attribute = Attribute.from_database(:foo, 1, Type::Value.new)
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate attribute, :has_been_read?
|
2015-01-20 16:09:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "an attribute has been read when its value is calculated" do
|
|
|
|
attribute = Attribute.from_database(:foo, 1, Type::Value.new)
|
|
|
|
attribute.value
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate attribute, :has_been_read?
|
2015-01-20 16:09:53 -05:00
|
|
|
end
|
|
|
|
|
2015-09-28 17:12:28 -04:00
|
|
|
test "an attribute is not changed if it hasn't been assigned or mutated" do
|
|
|
|
attribute = Attribute.from_database(:foo, 1, Type::Value.new)
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate attribute, :changed?
|
2015-09-28 17:12:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "an attribute is changed if it's been assigned a new value" do
|
|
|
|
attribute = Attribute.from_database(:foo, 1, Type::Value.new)
|
|
|
|
changed = attribute.with_value_from_user(2)
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate changed, :changed?
|
2015-09-28 17:12:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "an attribute is not changed if it's assigned the same value" do
|
|
|
|
attribute = Attribute.from_database(:foo, 1, Type::Value.new)
|
|
|
|
unchanged = attribute.with_value_from_user(1)
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate unchanged, :changed?
|
2015-09-28 17:12:28 -04:00
|
|
|
end
|
|
|
|
|
2015-01-20 16:09:53 -05:00
|
|
|
test "an attribute can not be mutated if it has not been read,
|
|
|
|
and skips expensive calculations" do
|
|
|
|
type_which_raises_from_all_methods = Object.new
|
|
|
|
attribute = Attribute.from_database(:foo, "bar", type_which_raises_from_all_methods)
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate attribute, :changed_in_place?
|
2015-09-28 17:12:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "an attribute is changed if it has been mutated" do
|
|
|
|
attribute = Attribute.from_database(:foo, "bar", Type::String.new)
|
|
|
|
attribute.value << "!"
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate attribute, :changed_in_place?
|
|
|
|
assert_predicate attribute, :changed?
|
2015-09-28 17:12:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "an attribute can forget its changes" do
|
|
|
|
attribute = Attribute.from_database(:foo, "bar", Type::String.new)
|
|
|
|
changed = attribute.with_value_from_user("foo")
|
|
|
|
forgotten = changed.forgetting_assignment
|
|
|
|
|
|
|
|
assert changed.changed? # sanity check
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate forgotten, :changed?
|
2015-01-20 16:09:53 -05:00
|
|
|
end
|
2015-09-24 13:50:11 -04:00
|
|
|
|
|
|
|
test "with_value_from_user validates the value" do
|
|
|
|
type = Type::Value.new
|
|
|
|
type.define_singleton_method(:assert_valid_value) do |value|
|
|
|
|
if value == 1
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attribute = Attribute.from_database(:foo, 1, type)
|
|
|
|
assert_equal 1, attribute.value
|
|
|
|
assert_equal 2, attribute.with_value_from_user(2).value
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
attribute.with_value_from_user(1)
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:06:01 -04:00
|
|
|
|
|
|
|
test "with_type preserves mutations" do
|
2018-05-17 04:32:27 -04:00
|
|
|
attribute = Attribute.from_database(:foo, +"", Type::Value.new)
|
2016-06-02 11:06:01 -04:00
|
|
|
attribute.value << "1"
|
|
|
|
|
|
|
|
assert_equal 1, attribute.with_type(Type::Integer.new).value
|
|
|
|
end
|
2014-06-07 15:46:22 -04:00
|
|
|
end
|
|
|
|
end
|