aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-06-11 16:56:33 -0600
committerSean Griffin <sean@thoughtbot.com>2015-06-11 16:56:33 -0600
commitcd3f5dba1e1f187179398e1507f8ef8f87c1c23e (patch)
tree7eebae01976cb17f390a5eec9d1b03d0f7d1a7e3 /activerecord/test/cases
parent850d313c3bd8267ef08c4fdfedbdb510cc419721 (diff)
downloadrails-cd3f5dba1e1f187179398e1507f8ef8f87c1c23e.tar.gz
rails-cd3f5dba1e1f187179398e1507f8ef8f87c1c23e.tar.bz2
rails-cd3f5dba1e1f187179398e1507f8ef8f87c1c23e.zip
Add a missing test case for the persistence behavior of `serialize`
`serialize` makes the contract that if it is given a class name, it will never return something other than an instance of that class. This means that it must cast `nil` to the empty form of that object. As such, we should then persist empty forms of that object as `nil`. While this is techincally under the contract of ``` model.attribute = value assert_equal model.attribute, model.tap(&:save).reload.attribute ``` which we can't actually test universally without property based testing, it has come up more than once and is worth calling out specifically since we aren't looking to change it.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/serialized_attribute_test.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb
index 7c92453ee3..a31c3c828c 100644
--- a/activerecord/test/cases/serialized_attribute_test.rb
+++ b/activerecord/test/cases/serialized_attribute_test.rb
@@ -274,4 +274,17 @@ class SerializedAttributeTest < ActiveRecord::TestCase
assert_equal({}, topic.content)
end
+
+ def test_values_cast_from_nil_are_persisted_as_nil
+ # This is required to fulfil the following contract, which must be universally
+ # true in Active Record:
+ #
+ # model.attribute = value
+ # assert_equal model.attribute, model.tap(&:save).reload.attribute
+ Topic.serialize(:content, Hash)
+ topic = Topic.create!(content: {})
+ topic2 = Topic.create!(content: nil)
+
+ assert_equal [topic, topic2], Topic.where(content: nil)
+ end
end