aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/xml_serialization_test.rb
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-09-01 05:33:21 +0000
committerRick Olson <technoweenie@gmail.com>2006-09-01 05:33:21 +0000
commit5f175edde59e65b991d84fe30cad2b102fc7347b (patch)
treef6109c8dce7730a51f21f0e841f38c4548deeb52 /activerecord/test/xml_serialization_test.rb
parentb1257d96b7b2482fc7811e5dc12d71c9fa091d10 (diff)
downloadrails-5f175edde59e65b991d84fe30cad2b102fc7347b.tar.gz
rails-5f175edde59e65b991d84fe30cad2b102fc7347b.tar.bz2
rails-5f175edde59e65b991d84fe30cad2b102fc7347b.zip
Add some XmlSerialization tests for ActiveRecord [Rick Olson]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4894 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/xml_serialization_test.rb')
-rw-r--r--activerecord/test/xml_serialization_test.rb104
1 files changed, 104 insertions, 0 deletions
diff --git a/activerecord/test/xml_serialization_test.rb b/activerecord/test/xml_serialization_test.rb
new file mode 100644
index 0000000000..216d582970
--- /dev/null
+++ b/activerecord/test/xml_serialization_test.rb
@@ -0,0 +1,104 @@
+require 'abstract_unit'
+
+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{^<contact>}, @xml
+ assert_match %r{</contact>$}, @xml
+ end
+
+ def test_should_serialize_custom_root
+ @xml = Contact.new.to_xml :root => 'xml_contact'
+ assert_match %r{^<xml-contact>}, @xml
+ assert_match %r{</xml-contact>$}, @xml
+ end
+
+ def test_should_allow_undasherized_tags
+ @xml = Contact.new.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
+
+ def test_should_allow_attribute_filtering
+ @xml = Contact.new.to_xml :only => [:age, :name]
+ assert_match %r{<name}, @xml
+ assert_match %r{<age}, @xml
+ assert_no_match %r{<created-at}, @xml
+
+ @xml = Contact.new.to_xml :except => [:age, :name]
+ assert_no_match %r{<name}, @xml
+ assert_no_match %r{<age}, @xml
+ assert_match %r{<created-at}, @xml
+ end
+end
+
+class DefaultXmlSerializationTest < Test::Unit::TestCase
+ def setup
+ @xml = Contact.new(:name => '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{<name>aaron stack</name>}, @xml
+ end
+
+ def test_should_serialize_integer
+ assert_match %r{<age type="integer">25</age>}, @xml
+ end
+
+ def test_should_serialize_binary
+ assert_match %r{YmluYXJ5ZGF0YQ==\n</avatar>}, @xml
+ assert_match %r{<avatar(.*)(type="binary")}, @xml
+ assert_match %r{<avatar(.*)(encoding="base64")}, @xml
+ end
+
+ def test_should_serialize_datetime
+ assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml
+ end
+
+ def test_should_serialize_boolean
+ assert_match %r{<awesome type=\"boolean\">false</awesome>}, @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{<name></name>}, @xml
+ end
+
+ def test_should_serialize_integer
+ assert_match %r{<age type="integer"></age>}, @xml
+ end
+
+ def test_should_serialize_binary
+ assert_match %r{></avatar>}, @xml
+ assert_match %r{<avatar(.*)(type="binary")}, @xml
+ assert_match %r{<avatar(.*)(encoding="base64")}, @xml
+ end
+
+ def test_should_serialize_datetime
+ assert_match %r{<created-at type=\"datetime\"></created-at>}, @xml
+ end
+
+ def test_should_serialize_boolean
+ assert_match %r{<awesome type=\"boolean\"></awesome>}, @xml
+ end
+end \ No newline at end of file