2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/contact'
|
|
|
|
require 'models/post'
|
|
|
|
require 'models/author'
|
|
|
|
require 'models/tagging'
|
|
|
|
require 'models/tag'
|
|
|
|
require 'models/comment'
|
2007-09-28 17:11:14 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class JsonSerializationTest < ActiveRecord::TestCase
|
2008-04-01 16:09:45 -04:00
|
|
|
class NamespacedContact < Contact
|
2010-06-07 19:02:45 -04:00
|
|
|
column :name, :string
|
2008-04-01 16:09:45 -04:00
|
|
|
end
|
|
|
|
|
2007-09-28 17:11:14 -04:00
|
|
|
def setup
|
|
|
|
@contact = Contact.new(
|
|
|
|
:name => 'Konata Izumi',
|
|
|
|
:age => 16,
|
|
|
|
:avatar => 'binarydata',
|
|
|
|
:created_at => Time.utc(2006, 8, 1),
|
|
|
|
:awesome => true,
|
|
|
|
:preferences => { :shows => 'anime' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2008-04-01 16:09:45 -04:00
|
|
|
def test_should_demodulize_root_in_json
|
|
|
|
@contact = NamespacedContact.new :name => 'whatever'
|
|
|
|
json = @contact.to_json
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{^\{"namespaced_contact":\{}, json
|
2008-04-01 16:09:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_include_root_in_json
|
|
|
|
json = @contact.to_json
|
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{^\{"contact":\{}, json
|
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_match %r{"age":16}, json
|
|
|
|
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
|
|
|
assert_match %r{"awesome":true}, json
|
|
|
|
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
2008-04-01 16:09:45 -04:00
|
|
|
end
|
|
|
|
|
2007-09-28 17:11:14 -04:00
|
|
|
def test_should_encode_all_encodable_attributes
|
|
|
|
json = @contact.to_json
|
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_match %r{"age":16}, json
|
|
|
|
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
|
|
|
assert_match %r{"awesome":true}, json
|
|
|
|
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_attribute_filtering_with_only
|
|
|
|
json = @contact.to_json(:only => [:name, :age])
|
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_match %r{"age":16}, json
|
|
|
|
assert_no_match %r{"awesome":true}, json
|
|
|
|
assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
|
|
|
assert_no_match %r{"preferences":\{"shows":"anime"\}}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_attribute_filtering_with_except
|
|
|
|
json = @contact.to_json(:except => [:name, :age])
|
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_no_match %r{"name":"Konata Izumi"}, json
|
|
|
|
assert_no_match %r{"age":16}, json
|
|
|
|
assert_match %r{"awesome":true}, json
|
|
|
|
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
|
|
|
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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.
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"label":"Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)
|
2007-09-28 17:11:14 -04:00
|
|
|
|
|
|
|
# Both methods.
|
|
|
|
methods_json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote])
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"label":"Has cheezburger"}, methods_json
|
|
|
|
assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
2010-08-29 10:10:31 -04:00
|
|
|
|
|
|
|
def test_serializable_hash_should_not_modify_options_in_argument
|
|
|
|
options = { :only => :name }
|
|
|
|
@contact.serializable_hash(options)
|
|
|
|
|
|
|
|
assert_nil options[:except]
|
|
|
|
end
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
|
2007-09-28 17:11:14 -04:00
|
|
|
fixtures :authors, :posts, :comments, :tags, :taggings
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@david = authors(:david)
|
2007-10-03 23:28:42 -04:00
|
|
|
@mary = authors(:mary)
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_includes_uses_association_name
|
|
|
|
json = @david.to_json(:include => :posts)
|
2007-10-03 23:28:42 -04:00
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"posts":\[}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"id":1}, json
|
|
|
|
assert_match %r{"name":"David"}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
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
|
2007-09-28 17:11:14 -04:00
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"title":"So I was thinking"}, json
|
|
|
|
assert_match %r{"body":"Like I hopefully always am"}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_includes_uses_association_name_and_applies_attribute_filters
|
|
|
|
json = @david.to_json(:include => { :posts => { :only => :title } })
|
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"name":"David"}, json
|
|
|
|
assert_match %r{"posts":\[}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"title":"Welcome to the weblog"}, json
|
|
|
|
assert_no_match %r{"body":"Such a lovely day"}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"title":"So I was thinking"}, json
|
|
|
|
assert_no_match %r{"body":"Like I hopefully always am"}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_includes_fetches_second_level_associations
|
|
|
|
json = @david.to_json(:include => { :posts => { :include => { :comments => { :only => :body } } } })
|
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"name":"David"}, json
|
|
|
|
assert_match %r{"posts":\[}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
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
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_includes_fetches_nth_level_associations
|
|
|
|
json = @david.to_json(
|
|
|
|
:include => {
|
|
|
|
:posts => {
|
|
|
|
:include => {
|
|
|
|
:taggings => {
|
|
|
|
:include => {
|
|
|
|
:tag => { :only => :name }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"name":"David"}, json
|
|
|
|
assert_match %r{"posts":\[}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"taggings":\[}, json
|
|
|
|
assert_match %r{"tag":\{"name":"General"\}}, json
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_match %r{"favorite_quote":"Constraints are liberating"}, json
|
|
|
|
assert_equal %r{"favorite_quote":}.match(json).size, 1
|
2007-09-28 17:11:14 -04:00
|
|
|
end
|
2007-10-03 23:28:42 -04:00
|
|
|
|
|
|
|
def test_should_allow_only_option_for_list_of_authors
|
2010-06-07 19:02:45 -04:00
|
|
|
ActiveRecord::Base.include_root_in_json = false
|
2007-10-03 23:28:42 -04:00
|
|
|
authors = [@david, @mary]
|
2009-06-08 22:25:56 -04:00
|
|
|
assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name)
|
2010-06-07 19:02:45 -04:00
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.include_root_in_json = true
|
2007-10-03 23:28:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_except_option_for_list_of_authors
|
2010-06-07 19:02:45 -04:00
|
|
|
ActiveRecord::Base.include_root_in_json = false
|
2007-10-03 23:28:42 -04:00
|
|
|
authors = [@david, @mary]
|
2009-06-08 22:25:56 -04:00
|
|
|
assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id])
|
2010-06-07 19:02:45 -04:00
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.include_root_in_json = true
|
2007-10-03 23:28:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_includes_for_list_of_authors
|
|
|
|
authors = [@david, @mary]
|
2009-06-08 22:25:56 -04:00
|
|
|
json = ActiveSupport::JSON.encode(authors,
|
2007-10-03 23:28:42 -04:00
|
|
|
:only => :name,
|
|
|
|
:include => {
|
|
|
|
:posts => { :only => :id }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
['"name":"David"', '"posts":[', '{"id":1}', '{"id":2}', '{"id":4}',
|
|
|
|
'{"id":5}', '{"id":6}', '"name":"Mary"', '"posts":[{"id":7}]'].each do |fragment|
|
2007-10-05 20:40:28 -04:00
|
|
|
assert json.include?(fragment), json
|
|
|
|
end
|
2007-10-03 23:28:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_options_for_hash_of_authors
|
|
|
|
authors_hash = {
|
|
|
|
1 => @david,
|
|
|
|
2 => @mary
|
|
|
|
}
|
2010-06-07 19:02:45 -04:00
|
|
|
assert_equal %({"1":{"author":{"name":"David"}}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])
|
2007-10-03 23:28:42 -04:00
|
|
|
end
|
2010-07-08 16:17:21 -04:00
|
|
|
|
|
|
|
def test_should_be_able_to_encode_relation
|
|
|
|
authors_relation = Author.where(:id => [@david.id, @mary.id])
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-07-08 16:17:21 -04:00
|
|
|
json = ActiveSupport::JSON.encode authors_relation, :only => :name
|
|
|
|
assert_equal '[{"author":{"name":"David"}},{"author":{"name":"Mary"}}]', json
|
|
|
|
end
|
2007-10-05 20:33:51 -04:00
|
|
|
end
|