class DatabaseConnectedJsonEncodingTest

Public Instance Methods

setup() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 175
def setup
  @david = authors(:david)
  @mary = authors(:mary)
end
test_includes_doesnt_merge_opts_from_base() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 242
def test_includes_doesnt_merge_opts_from_base
  json = @david.to_json(
    only: :id,
    include: :posts
  )

  assert_match %{"title":"Welcome to the weblog"}, json
end
test_includes_fetches_nth_level_associations() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 221
def test_includes_fetches_nth_level_associations
  json = @david.to_json(
    include: {
      posts: {
        include: {
          taggings: {
            include: {
              tag: { only: :name }
            }
          }
        }
      }
  })

  assert_match %r{"name":"David"}, json
  assert_match %r{"posts":\[}, json

  assert_match %r{"taggings":\[}, json
  assert_match %r{"tag":\{"name":"General"\}}, json
end
test_includes_fetches_second_level_associations() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 209
def test_includes_fetches_second_level_associations
  json = @david.to_json(include: { posts: { include: { comments: { only: :body } } } })

  assert_match %r{"name":"David"}, json
  assert_match %r{"posts":\[}, json

  assert_match %r{"comments":\[}, json
  assert_match %r{\{"body":"Thank you again for the welcome"\}}, json
  assert_match %r{\{"body":"Don't think too hard"\}}, json
  assert_no_match %r{"post_id":}, json
end
test_includes_uses_association_name() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 180
def test_includes_uses_association_name
  json = @david.to_json(include: :posts)

  assert_match %r{"posts":\[}, json

  assert_match %r{"id":1}, json
  assert_match %r{"name":"David"}, json

  assert_match %r{"author_id":1}, json
  assert_match %r{"title":"Welcome to the weblog"}, json
  assert_match %r{"body":"Such a lovely day"}, json

  assert_match %r{"title":"So I was thinking"}, json
  assert_match %r{"body":"Like I hopefully always am"}, json
end
test_includes_uses_association_name_and_applies_attribute_filters() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 196
def test_includes_uses_association_name_and_applies_attribute_filters
  json = @david.to_json(include: { posts: { only: :title } })

  assert_match %r{"name":"David"}, json
  assert_match %r{"posts":\[}, json

  assert_match %r{"title":"Welcome to the weblog"}, json
  assert_no_match %r{"body":"Such a lovely day"}, json

  assert_match %r{"title":"So I was thinking"}, json
  assert_no_match %r{"body":"Like I hopefully always am"}, json
end
test_should_allow_except_option_for_list_of_authors() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 267
def test_should_allow_except_option_for_list_of_authors
  set_include_root_in_json(false) do
    authors = [@david, @mary]
    encoded = ActiveSupport::JSON.encode(authors, except: [
      :name, :author_address_id, :author_address_extra_id,
      :organization_id, :owned_essay_id
    ])
    assert_equal %([{"id":1},{"id":2}]), encoded
  end
end
test_should_allow_includes_for_list_of_authors() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 278
def test_should_allow_includes_for_list_of_authors
  authors = [@david, @mary]
  json = ActiveSupport::JSON.encode(authors,
    only: :name,
    include: {
      posts: { only: :id }
    }
  )

  ['"name":"David"', '"posts":[', '{"id":1}', '{"id":2}', '{"id":4}',
   '{"id":5}', '{"id":6}', '"name":"Mary"', '"posts":[', '{"id":7}', '{"id":9}'].each do |fragment|
     assert_includes json, fragment, json
   end
end
test_should_allow_only_option_for_list_of_authors() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 260
def test_should_allow_only_option_for_list_of_authors
  set_include_root_in_json(false) do
    authors = [@david, @mary]
    assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, only: :name)
  end
end
test_should_allow_options_for_hash_of_authors() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 293
def test_should_allow_options_for_hash_of_authors
  set_include_root_in_json(true) do
    authors_hash = {
      1 => @david,
      2 => @mary
    }
    assert_equal %({"1":{"author":{"name":"David"}}}), ActiveSupport::JSON.encode(authors_hash, only: [1, :name])
  end
end
test_should_be_able_to_encode_relation() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 303
def test_should_be_able_to_encode_relation
  set_include_root_in_json(true) do
    authors_relation = Author.where(id: [@david.id, @mary.id])

    json = ActiveSupport::JSON.encode authors_relation, only: :name
    assert_equal '[{"author":{"name":"David"}},{"author":{"name":"Mary"}}]', json
  end
end
test_should_not_call_methods_on_associations_that_dont_respond() click to toggle source
# File activerecord/test/cases/json_serialization_test.rb, line 251
def test_should_not_call_methods_on_associations_that_dont_respond
  def @david.favorite_quote; "Constraints are liberating"; end
  json = @david.to_json(include: :posts, methods: :favorite_quote)

  assert !@david.posts.first.respond_to?(:favorite_quote)
  assert_match %r{"favorite_quote":"Constraints are liberating"}, json
  assert_equal 1, %r{"favorite_quote":}.match(json).size
end