1 require 'abstract_unit'
2 require 'fixtures/contact'
3 require 'fixtures/post'
4 require 'fixtures/author'
5 require 'fixtures/tagging'
6 require 'fixtures/comment'
8 class XmlSerializationTest < Test::Unit::TestCase
9 def test_should_serialize_default_root
10 @xml = Contact.new.to_xml
11 assert_match %r{^<contact>}, @xml
12 assert_match %r{</contact>$}, @xml
15 def test_should_serialize_default_root_with_namespace
16 @xml = Contact.new.to_xml :namespace=>"http://xml.rubyonrails.org/contact"
17 assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml
18 assert_match %r{</contact>$}, @xml
21 def test_should_serialize_custom_root
22 @xml = Contact.new.to_xml :root => 'xml_contact'
23 assert_match %r{^<xml-contact>}, @xml
24 assert_match %r{</xml-contact>$}, @xml
27 def test_should_allow_undasherized_tags
28 @xml = Contact.new.to_xml :root => 'xml_contact', :dasherize => false
29 assert_match %r{^<xml_contact>}, @xml
30 assert_match %r{</xml_contact>$}, @xml
31 assert_match %r{<created_at}, @xml
34 def test_should_include_yielded_additions
35 @xml = Contact.new.to_xml do |xml|
39 assert_match %r{<creator>David</creator>}, @xml
43 class DefaultXmlSerializationTest < Test::Unit::TestCase
45 @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }).to_xml
48 def test_should_serialize_string
49 assert_match %r{<name>aaron stack</name>}, @xml
52 def test_should_serialize_integer
53 assert_match %r{<age type="integer">25</age>}, @xml
56 def test_should_serialize_binary
57 assert_match %r{YmluYXJ5ZGF0YQ==\n</avatar>}, @xml
58 assert_match %r{<avatar(.*)(type="binary")}, @xml
59 assert_match %r{<avatar(.*)(encoding="base64")}, @xml
62 def test_should_serialize_datetime
63 assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml
66 def test_should_serialize_boolean
67 assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml
70 def test_should_serialize_yaml
71 assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml
75 class NilXmlSerializationTest < Test::Unit::TestCase
77 @xml = Contact.new.to_xml(:root => 'xml_contact')
80 def test_should_serialize_string
81 assert_match %r{<name nil="true"></name>}, @xml
84 def test_should_serialize_integer
85 assert %r{<age (.*)></age>}.match(@xml)
87 assert_match %r{nil="true"}, attributes
88 assert_match %r{type="integer"}, attributes
91 def test_should_serialize_binary
92 assert %r{<avatar (.*)></avatar>}.match(@xml)
94 assert_match %r{type="binary"}, attributes
95 assert_match %r{encoding="base64"}, attributes
96 assert_match %r{nil="true"}, attributes
99 def test_should_serialize_datetime
100 assert %r{<created-at (.*)></created-at>}.match(@xml)
102 assert_match %r{nil="true"}, attributes
103 assert_match %r{type="datetime"}, attributes
106 def test_should_serialize_boolean
107 assert %r{<awesome (.*)></awesome>}.match(@xml)
109 assert_match %r{type="boolean"}, attributes
110 assert_match %r{nil="true"}, attributes
113 def test_should_serialize_yaml
114 assert %r{<preferences(.*)></preferences>}.match(@xml)
116 assert_match %r{type="yaml"}, attributes
117 assert_match %r{nil="true"}, attributes
121 class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
122 fixtures :authors, :posts
123 # to_xml used to mess with the hash the user provided which
124 # caused the builder to be reused. This meant the document kept
125 # getting appended to.
126 def test_passing_hash_shouldnt_reuse_builder
127 options = {:include=>:posts}
128 david = authors(:david)
129 first_xml_size = david.to_xml(options).size
130 second_xml_size = david.to_xml(options).size
131 assert_equal first_xml_size, second_xml_size
134 def test_include_uses_association_name
135 xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0
136 assert_match %r{<hello-posts type="array">}, xml
137 assert_match %r{<hello-post type="Post">}, xml
138 assert_match %r{<hello-post type="StiPost">}, xml
141 def test_methods_are_called_on_object
142 xml = authors(:david).to_xml :methods => :label, :indent => 0
143 assert_match %r{<label>.*</label>}, xml
146 def test_should_not_call_methods_on_associations_that_dont_respond
147 xml = authors(:david).to_xml :include=>:hello_posts, :methods => :label, :indent => 2
148 assert !authors(:david).hello_posts.first.respond_to?(:label)
149 assert_match %r{^ <label>.*</label>}, xml
150 assert_no_match %r{^ <label>}, xml
153 def test_should_include_empty_has_many_as_empty_array
154 authors(:david).posts.delete_all
155 xml = authors(:david).to_xml :include=>:posts, :indent => 2
157 assert_equal [], Hash.from_xml(xml)['author']['posts']
158 assert_match %r{^ <posts type="array"/>}, xml
161 def test_should_has_many_array_elements_should_include_type_when_different_from_guessed_value
162 xml = authors(:david).to_xml :include=>:posts_with_comments, :indent => 2
164 assert Hash.from_xml(xml)
165 assert_match %r{^ <posts-with-comments type="array">}, xml
166 assert_match %r{^ <posts-with-comment type="Post">}, xml
167 assert_match %r{^ <posts-with-comment type="StiPost">}, xml
169 types = Hash.from_xml(xml)['author']['posts_with_comments'].collect {|t| t['type'] }
170 assert types.include?('SpecialPost')
171 assert types.include?('Post')
172 assert types.include?('StiPost')