aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/serialized_attribute_test.rb
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2014-07-14 10:38:14 -0700
committerGodfrey Chan <godfreykfc@gmail.com>2014-07-15 08:43:54 -0700
commitf4c8a4b71fcce46c3259240b50f30e134e14c4a2 (patch)
tree3aa042102c2f2aa21a3060a0adb4e9dd994ff7df /activerecord/test/cases/serialized_attribute_test.rb
parenta66aeb84e215c8a6e365a7fd0197c5703a14f241 (diff)
downloadrails-f4c8a4b71fcce46c3259240b50f30e134e14c4a2.tar.gz
rails-f4c8a4b71fcce46c3259240b50f30e134e14c4a2.tar.bz2
rails-f4c8a4b71fcce46c3259240b50f30e134e14c4a2.zip
Merge pull request #16162 from chancancode/fix_json_coder
Fixed JSON coder when loading NULL from DB
Diffstat (limited to 'activerecord/test/cases/serialized_attribute_test.rb')
-rw-r--r--activerecord/test/cases/serialized_attribute_test.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb
index a8f667c732..96d1e365f0 100644
--- a/activerecord/test/cases/serialized_attribute_test.rb
+++ b/activerecord/test/cases/serialized_attribute_test.rb
@@ -81,6 +81,24 @@ 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
+ 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)"
+ t = Topic.find(id)
+
+ assert_nil t.content
+
+ t.save!
+
+ # 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
+ end
+
def test_serialized_attribute_declared_in_subclass
hash = { 'important1' => 'value1', 'important2' => 'value2' }
important_topic = ImportantTopic.create("important" => hash)