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_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