class JsonSerializationTest

Public Instance Methods

setup() click to toggle source
# File activemodel/test/cases/serializers/json_serialization_test.rb, line 8
def setup
  @contact = Contact.new
  @contact.name = "Konata Izumi"
  @contact.age = 16
  @contact.created_at = Time.utc(2006, 8, 1)
  @contact.awesome = true
  @contact.preferences = { "shows" => "anime" }
end
test_does_not_include_inheritance_column_from_sti() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 139
def test_does_not_include_inheritance_column_from_sti
  @contact = ContactSti.new(@contact.attributes)
  assert_equal "ContactSti", @contact.type

  json = @contact.to_json
  assert_match %r{"name":"Konata Izumi"}, json
  assert_no_match %r{type}, json
  assert_no_match %r{ContactSti}, json
end
test_methods_are_called_on_object() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 92
def test_methods_are_called_on_object
  # Define methods on fixture.
  def @contact.label; "Has cheezburger"; end
  def @contact.favorite_quote; "Constraints are liberating"; end

  # Single method.
  assert_match %r{"label":"Has cheezburger"}, @contact.to_json(only: :name, methods: :label)

  # Both methods.
  methods_json = @contact.to_json(only: :name, methods: [:label, :favorite_quote])
  assert_match %r{"label":"Has cheezburger"}, methods_json
  assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json
end
test_serializable_hash_should_not_modify_options_in_argument() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 164
def test_serializable_hash_should_not_modify_options_in_argument
  options = { only: :name }.freeze
  assert_nothing_raised { @contact.serializable_hash(options) }
end
test_serializable_hash_with_default_except_option_and_excluding_inheritance_column_from_sti() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 149
def test_serializable_hash_with_default_except_option_and_excluding_inheritance_column_from_sti
  @contact = ContactSti.new(@contact.attributes)
  assert_equal "ContactSti", @contact.type

  def @contact.serializable_hash(options = {})
    super({ except: %w(age) }.merge!(options))
  end

  json = @contact.to_json
  assert_match %r{"name":"Konata Izumi"}, json
  assert_no_match %r{age}, json
  assert_no_match %r{type}, json
  assert_no_match %r{ContactSti}, json
end
test_should_allow_attribute_filtering_with_except() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 82
def test_should_allow_attribute_filtering_with_except
  json = @contact.to_json(except: [:name, :age])

  assert_no_match %r{"name":"Konata Izumi"}, json
  assert_no_match %r{"age":16}, json
  assert_match %r{"awesome":true}, json
  assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
  assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
test_should_allow_attribute_filtering_with_only() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 72
def test_should_allow_attribute_filtering_with_only
  json = @contact.to_json(only: [:name, :age])

  assert_match %r{"name":"Konata Izumi"}, json
  assert_match %r{"age":16}, json
  assert_no_match %r{"awesome":true}, json
  assert_not_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
  assert_no_match %r{"preferences":\{"shows":"anime"\}}, json
end
test_should_demodulize_root_in_json() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 41
def test_should_demodulize_root_in_json
  set_include_root_in_json(true) do
    @contact = NamespacedContact.new name: "whatever"
    json = @contact.to_json
    assert_match %r{^\{"namespaced_contact":\{}, json
  end
end
test_should_encode_all_encodable_attributes() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 62
def test_should_encode_all_encodable_attributes
  json = @contact.to_json

  assert_match %r{"name":"Konata Izumi"}, json
  assert_match %r{"age":16}, json
  assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
  assert_match %r{"awesome":true}, json
  assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
test_should_include_root_in_json() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 49
def test_should_include_root_in_json
  set_include_root_in_json(true) do
    json = @contact.to_json

    assert_match %r{^\{"contact":\{}, json
    assert_match %r{"name":"Konata Izumi"}, json
    assert_match %r{"age":16}, json
    assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})
    assert_match %r{"awesome":true}, json
    assert_match %r{"preferences":\{"shows":"anime"\}}, json
  end
end
test_uses_serializable_hash_with_except_option() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 128
def test_uses_serializable_hash_with_except_option
  def @contact.serializable_hash(options = nil)
    super(except: %w(age))
  end

  json = @contact.to_json
  assert_match %r{"name":"Konata Izumi"}, json
  assert_match %r{"awesome":true}, json
  assert_no_match %r{age}, json
end
test_uses_serializable_hash_with_frozen_hash() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 106
def test_uses_serializable_hash_with_frozen_hash
  def @contact.serializable_hash(options = nil)
    super({ only: %w(name) }.freeze)
  end

  json = @contact.to_json
  assert_match %r{"name":"Konata Izumi"}, json
  assert_no_match %r{awesome}, json
  assert_no_match %r{age}, json
end
test_uses_serializable_hash_with_only_option() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 117
def test_uses_serializable_hash_with_only_option
  def @contact.serializable_hash(options = nil)
    super(only: %w(name))
  end

  json = @contact.to_json
  assert_match %r{"name":"Konata Izumi"}, json
  assert_no_match %r{awesome}, json
  assert_no_match %r{age}, json
end