require 'abstract_unit' require 'fixtures/post' require 'fixtures/author' class Contact < ActiveRecord::Base # mock out self.columns so no pesky db is needed for these tests def self.columns() @columns ||= []; end def self.column(name, sql_type = nil, default = nil, null = true) columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) end column :name, :string column :age, :integer column :avatar, :binary column :created_at, :datetime column :awesome, :boolean end class XmlSerializationTest < Test::Unit::TestCase def test_should_serialize_default_root @xml = Contact.new.to_xml assert_match %r{^}, @xml assert_match %r{$}, @xml end def test_should_serialize_default_root_with_namespace @xml = Contact.new.to_xml :namespace=>"http://xml.rubyonrails.org/contact" assert_match %r{^}, @xml assert_match %r{$}, @xml end def test_should_serialize_custom_root @xml = Contact.new.to_xml :root => 'xml_contact' assert_match %r{^}, @xml assert_match %r{$}, @xml end def test_should_allow_undasherized_tags @xml = Contact.new.to_xml :root => 'xml_contact', :dasherize => false assert_match %r{^}, @xml assert_match %r{$}, @xml assert_match %r{ [:age, :name] assert_match %r{ [:age, :name] assert_no_match %r{ 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false).to_xml end def test_should_serialize_string assert_match %r{aaron stack}, @xml end def test_should_serialize_integer assert_match %r{25}, @xml end def test_should_serialize_binary assert_match %r{YmluYXJ5ZGF0YQ==\n}, @xml assert_match %r{2006-08-01T00:00:00Z}, @xml end def test_should_serialize_boolean assert_match %r{false}, @xml end end class NilXmlSerializationTest < Test::Unit::TestCase def setup @xml = Contact.new.to_xml(:root => 'xml_contact') end def test_should_serialize_string assert_match %r{}, @xml end def test_should_serialize_integer assert_match %r{}, @xml end def test_should_serialize_binary assert_match %r{>}, @xml assert_match %r{}, @xml end def test_should_serialize_boolean assert_match %r{}, @xml end end class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase fixtures :authors, :posts # to_xml used to mess with the hash the user provided which # caused the builder to be reused def test_passing_hash_shouldnt_reuse_builder options = {:include=>:posts} david = authors(:david) first_xml_size = david.to_xml(options).size second_xml_size = david.to_xml(options).size assert_equal first_xml_size, second_xml_size end end