From da6472c916be5645ea4d761e7cbf44d486ca07a8 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Tue, 15 Jul 2014 09:08:31 -0700 Subject: Document the change in `nil` handling for serialized attributes Also updated the test case to reflect that --- .../test/cases/serialized_attribute_test.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index d7e6d3465f..f8d87a3661 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -81,22 +81,25 @@ class SerializedAttributeTest < ActiveRecord::TestCase assert_equal(my_post.title, t.content["title"]) end - # This is to ensure that the JSON coder is behaving the same way as 4.0, but - # we can consider changing this in the future. - def test_json_db_null + def test_json_read_legacy_null Topic.serialize :content, JSON - # Force a row to have a database NULL instead of a JSON "null" - id = Topic.connection.insert "INSERT INTO topics (content) VALUES(NULL)" + # Force a row to have a JSON "null" instead of a database NULL (this is how + # null values are saved on 4.1 and before) + id = Topic.connection.insert "INSERT INTO topics (content) VALUES('null')" t = Topic.find(id) assert_nil t.content + end - t.save! + def test_json_read_db_null + Topic.serialize :content, JSON - # On 4.0, re-saving a row with a database NULL will turn that into a JSON - # "null" - assert_equal 1, Topic.where("content = 'null'").count + # Force a row to have a database NULL instead of a JSON "null" + id = Topic.connection.insert "INSERT INTO topics (content) VALUES(NULL)" + t = Topic.find(id) + + assert_nil t.content end def test_serialized_attribute_declared_in_subclass -- cgit v1.2.3