diff options
| author | José Valim <jose.valim@gmail.com> | 2009-07-04 20:16:36 +0200 |
|---|---|---|
| committer | José Valim <jose.valim@gmail.com> | 2009-07-04 20:16:36 +0200 |
| commit | 20e2140ce7cd6ece8769fb09a7e615e961446b02 (patch) | |
| tree | 2bf6a38b51863d86b195551ed740bde719c9d460 /activemodel/test/cases/serializeration/json_serialization_test.rb | |
| parent | 44633dc7a587424d21917413500b2d71fa3d31bb (diff) | |
| parent | 783db25e0c640c1588732967a87d65c10fddc08e (diff) | |
| download | rails-20e2140ce7cd6ece8769fb09a7e615e961446b02.tar.gz rails-20e2140ce7cd6ece8769fb09a7e615e961446b02.tar.bz2 rails-20e2140ce7cd6ece8769fb09a7e615e961446b02.zip | |
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'activemodel/test/cases/serializeration/json_serialization_test.rb')
| -rw-r--r-- | activemodel/test/cases/serializeration/json_serialization_test.rb | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/activemodel/test/cases/serializeration/json_serialization_test.rb b/activemodel/test/cases/serializeration/json_serialization_test.rb new file mode 100644 index 0000000000..3e69db110e --- /dev/null +++ b/activemodel/test/cases/serializeration/json_serialization_test.rb @@ -0,0 +1,77 @@ +require 'cases/helper' +require 'models/contact' + +class Contact + include ActiveModel::Serializers::JSON +end + +class JsonSerializationTest < ActiveModel::TestCase + 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 "should include root in json" do + begin + Contact.include_root_in_json = true + json = @contact.to_json + + 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 + ensure + Contact.include_root_in_json = false + end + end + + test "should encode all encodable attributes" do + json = @contact.to_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 + end + + test "should allow attribute filtering with only" do + 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 !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_no_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "should allow attribute filtering with except" do + 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 json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "methds are called on object" do + # 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 +end |
