Repository: rails/rails
File: activerecord/test/cases/serialized_attribute_test.rb (Ruby)

Code before:
    # null values are saved on 4.1 and before)
    id = Topic.lease_connection.insert "INSERT INTO topics (content) VALUES('null')"
    t = Topic.find(id)

    assert_nil t.content
  end

  def test_json_read_db_null
    Topic.serialize :content, coder: JSON

    # Force a row to have a database NULL instead of a JSON "null"
    id = Topic.lease_connection.insert "INSERT INTO topics (content) VALUES(NULL)"
    t = Topic.find(id)

    assert_nil t.content
  end

  def test_serialized_attribute_declared_in_subclass
    hash = { "important1" => "value1", "important2" => "value2" }
    important_topic = ImportantTopic.create("important" => hash)
    assert_equal(hash, important_topic.important)

    important_topic.reload
    assert_equal(hash, important_topic.important)
    assert_equal(hash, important_topic.read_attribute(:important))
  end

  def test_serialized_time_attribute
    myobj = Time.local(2008, 1, 1, 1, 0)
    topic = Topic.create("content" => myobj).reload
    assert_equal(myobj, topic.content)
  end

  def test_serialized_string_attribute
    myobj = "Yes"
    topic = Topic.create("content" => myobj).reload
    assert_equal(myobj, topic.content)
  end

  def test_serialized_class_attribute
    ActiveRecord.yaml_column_permitted_classes += [Class]

    topic = ClassifiedTopic.create(important: Symbol).reload
    assert_equal(Symbol, topic.important)
    assert_not_empty ClassifiedTopic.where(important: Symbol)
  end

  def test_serialized_class_does_not_become_frozen
    ActiveRecord.yaml_column_permitted_classes += [Class]

    assert_not_predicate Symbol, :frozen?

Review comment (suggestion):
i think it's in reverse
```ruby
  assert_equal(t.content.deep_symbolize_keys, t.content)
```
(expected first, then actual)

Code after:
    # null values are saved on 4.1 and before)
    id = Topic.lease_connection.insert "INSERT INTO topics (content) VALUES('null')"
    t = Topic.find(id)

    assert_nil t.content
  end

  def test_json_read_db_null
    Topic.serialize :content, coder: JSON

    # Force a row to have a database NULL instead of a JSON "null"
    id = Topic.lease_connection.insert "INSERT INTO topics (content) VALUES(NULL)"
    t = Topic.find(id)

    assert_nil t.content
  end

  def test_json_symbolize_names_returns_symbolized_names
    Topic.serialize :content, coder: ActiveRecord::Coders::JSON.new(symbolize_names: true)
    my_post = posts(:welcome)

    t = Topic.new(content: my_post)
    t.save!
    t.reload

    assert_equal(t.content, t.content.deep_symbolize_keys)
  end

  def test_serialized_attribute_declared_in_subclass
    hash = { "important1" => "value1", "important2" => "value2" }
    important_topic = ImportantTopic.create("important" => hash)
    assert_equal(hash, important_topic.important)

    important_topic.reload
    assert_equal(hash, important_topic.important)
    assert_equal(hash, important_topic.read_attribute(:important))
  end

  def test_serialized_time_attribute
    myobj = Time.local(2008, 1, 1, 1, 0)
    topic = Topic.create("content" => myobj).reload
    assert_equal(myobj, topic.content)
  end

  def test_serialized_string_attribute
    myobj = "Yes"
    topic = Topic.create("content" => myobj).reload
    assert_equal(myobj, topic.content)
  end

  def test_serialized_class_attribute