aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/serializeration
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/test/cases/serializeration')
-rw-r--r--activemodel/test/cases/serializeration/json_serialization_test.rb148
-rw-r--r--activemodel/test/cases/serializeration/xml_serialization_test.rb131
2 files changed, 0 insertions, 279 deletions
diff --git a/activemodel/test/cases/serializeration/json_serialization_test.rb b/activemodel/test/cases/serializeration/json_serialization_test.rb
deleted file mode 100644
index 500a5c575f..0000000000
--- a/activemodel/test/cases/serializeration/json_serialization_test.rb
+++ /dev/null
@@ -1,148 +0,0 @@
-require 'cases/helper'
-require 'models/contact'
-require 'models/automobile'
-require 'active_support/core_ext/object/instance_variables'
-
-class Contact
- extend ActiveModel::Naming
- include ActiveModel::Serializers::JSON
- include ActiveModel::Validations
-
- def attributes
- instance_values
- end unless method_defined?(:attributes)
-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
- 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
- end
-
- test "should not include root in json" do
- begin
- Contact.include_root_in_json = false
- json = @contact.to_json
-
- assert_no_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 = true
- end
- end
-
- test "should include custom root in json" do
- json = @contact.to_json(:root => 'json_contact')
-
- assert_match %r{^\{"json_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
- 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 "methods 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
-
- test "should return OrderedHash for errors" do
- contact = Contact.new
- contact.errors.add :name, "can't be blank"
- contact.errors.add :name, "is too short (minimum is 2 characters)"
- contact.errors.add :age, "must be 16 or over"
-
- hash = ActiveSupport::OrderedHash.new
- hash[:name] = ["can't be blank", "is too short (minimum is 2 characters)"]
- hash[:age] = ["must be 16 or over"]
- assert_equal hash.to_json, contact.errors.to_json
- end
-
- test "serializable_hash should not modify options passed in argument" do
- options = { :except => :name }
- @contact.serializable_hash(options)
-
- assert_nil options[:only]
- assert_equal :name, options[:except]
- end
-
- test "as_json should return a hash" do
- json = @contact.as_json
-
- assert_kind_of Hash, json
- assert_kind_of Hash, json['contact']
- %w(name age created_at awesome preferences).each do |field|
- assert_equal @contact.send(field), json['contact'][field]
- end
- end
-
- test "custom as_json should be honored when generating json" do
- def @contact.as_json(options); { :name => name, :created_at => created_at }; end
- json = @contact.to_json
-
- assert_match %r{"name":"Konata Izumi"}, json
- assert_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json
- assert_no_match %r{"awesome":}, json
- assert_no_match %r{"preferences":}, json
- end
-
-end
diff --git a/activemodel/test/cases/serializeration/xml_serialization_test.rb b/activemodel/test/cases/serializeration/xml_serialization_test.rb
deleted file mode 100644
index b6a2f88667..0000000000
--- a/activemodel/test/cases/serializeration/xml_serialization_test.rb
+++ /dev/null
@@ -1,131 +0,0 @@
-require 'cases/helper'
-require 'models/contact'
-require 'active_support/core_ext/object/instance_variables'
-require 'ostruct'
-
-class Contact
- extend ActiveModel::Naming
- include ActiveModel::Serializers::Xml
-
- def attributes
- instance_values
- end unless method_defined?(:attributes)
-end
-
-module Admin
- class Contact < ::Contact
- end
-end
-
-class Customer < Struct.new(:name)
-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
- customer = Customer.new
- customer.name = "John"
- @contact.preferences = customer
- 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 namespaced root" do
- @xml = Admin::Contact.new(@contact.attributes).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 lower-camelized tags" do
- @xml = @contact.to_xml :root => 'xml_contact', :camelize => :lower
- 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_match %r{<age>25</age>}, @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 array" do
- assert_match %r{<social type=\"array\">\s*<social>twitter</social>\s*<social>github</social>\s*</social>}, @contact.to_xml(:methods => :social)
- end
-
- test "should serialize hash" do
- assert_match %r{<network>\s*<git type=\"symbol\">github</git>\s*</network>}, @contact.to_xml(:methods => :network)
- end
-
- test "should serialize yaml" do
- assert_match %r{<preferences type=\"yaml\">--- !ruby/struct:Customer(\s*)\nname: John\n</preferences>}, @contact.to_xml
- end
-
- test "should call proc on object" do
- proc = Proc.new { |options| options[:builder].tag!('nationality', 'unknown') }
- xml = @contact.to_xml(:procs => [ proc ])
- assert_match %r{<nationality>unknown</nationality>}, xml
- end
-
- test 'should supply serializable to second proc argument' do
- proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) }
- xml = @contact.to_xml(:procs => [ proc ])
- assert_match %r{<name-reverse>kcats noraa</name-reverse>}, xml
- end
-end