diff options
Diffstat (limited to 'activemodel/test/cases/serializeration')
-rw-r--r-- | activemodel/test/cases/serializeration/json_serialization_test.rb | 77 | ||||
-rw-r--r-- | activemodel/test/cases/serializeration/xml_serialization_test.rb | 81 |
2 files changed, 158 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 diff --git a/activemodel/test/cases/serializeration/xml_serialization_test.rb b/activemodel/test/cases/serializeration/xml_serialization_test.rb new file mode 100644 index 0000000000..57792e900e --- /dev/null +++ b/activemodel/test/cases/serializeration/xml_serialization_test.rb @@ -0,0 +1,81 @@ +require 'cases/helper' +require 'models/contact' + +class Contact + include ActiveModel::Serializers::Xml +end + +class XmlSerializationTest < ActiveModel::TestCase + def setup + @contact = Contact.new + @contact.name = 'aaron stack' + @contact.age = 25 + @contact.created_at = Time.utc(2006, 8, 1) + @contact.awesome = false + @contact.preferences = { :gem => 'ruby' } + end + + test "should serialize default root" do + @xml = @contact.to_xml + assert_match %r{^<contact>}, @xml + assert_match %r{</contact>$}, @xml + end + + test "should serialize default root with namespace" do + @xml = @contact.to_xml :namespace => "http://xml.rubyonrails.org/contact" + assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml + assert_match %r{</contact>$}, @xml + end + + test "should serialize custom root" do + @xml = @contact.to_xml :root => 'xml_contact' + assert_match %r{^<xml-contact>}, @xml + assert_match %r{</xml-contact>$}, @xml + end + + test "should allow undasherized tags" do + @xml = @contact.to_xml :root => 'xml_contact', :dasherize => false + assert_match %r{^<xml_contact>}, @xml + assert_match %r{</xml_contact>$}, @xml + assert_match %r{<created_at}, @xml + end + + test "should allow camelized tags" do + @xml = @contact.to_xml :root => 'xml_contact', :camelize => true + assert_match %r{^<XmlContact>}, @xml + assert_match %r{</XmlContact>$}, @xml + assert_match %r{<CreatedAt}, @xml + end + + test "should allow skipped types" do + @xml = @contact.to_xml :skip_types => true + assert %r{<age>25</age>}.match(@xml) + end + + test "should include yielded additions" do + @xml = @contact.to_xml do |xml| + xml.creator "David" + end + assert_match %r{<creator>David</creator>}, @xml + end + + test "should serialize string" do + assert_match %r{<name>aaron stack</name>}, @contact.to_xml + end + + test "should serialize integer" do + assert_match %r{<age type="integer">25</age>}, @contact.to_xml + end + + test "should serialize datetime" do + assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @contact.to_xml + end + + test "should serialize boolean" do + assert_match %r{<awesome type=\"boolean\">false</awesome>}, @contact.to_xml + end + + test "should serialize yaml" do + assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @contact.to_xml + end +end |