2 require 'models/contact'
4 require 'models/author'
5 require 'models/tagging'
6 require 'models/comment'
8 class XmlSerializationTest
< ActiveRecord
::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_allow_camelized_tags
35 @xml = Contact
.new
.to_xml
:root => 'xml_contact', :camelize => true
36 assert_match
%r
{^
<XmlContact
>}, @xml
37 assert_match
%r
{</XmlContact
>$
}, @xml
38 assert_match
%r
{<CreatedAt
}, @xml
41 def test_should_allow_skipped_types
42 @xml = Contact
.new(:age => 25).to_xml
:skip_types => true
43 assert
%r
{<age
>25</age
>}.match(@xml)
46 def test_should_include_yielded_additions
47 @xml = Contact
.new
.to_xml
do |xml
|
50 assert_match
%r
{<creator
>David
</creator
>}, @xml
54 class DefaultXmlSerializationTest
< ActiveRecord
::TestCase
56 @xml = Contact
.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time
.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }).to_xml
59 def test_should_serialize_string
60 assert_match
%r
{<name
>aaron stack
</name
>}, @xml
63 def test_should_serialize_integer
64 assert_match
%r
{<age type
="integer">25</age
>}, @xml
67 def test_should_serialize_binary
68 assert_match
%r
{YmluYXJ5ZGF0YQ
==\n</avatar
>}, @xml
69 assert_match
%r
{<avatar(.*)(type
="binary")}, @xml
70 assert_match
%r
{<avatar(.*)(encoding
="base64")}, @xml
73 def test_should_serialize_datetime
74 assert_match
%r
{<created-at type
=\"datetime
\">2006-08-01T00
:00:00Z</created-at
>}, @xml
77 def test_should_serialize_boolean
78 assert_match
%r
{<awesome type
=\"boolean
\">false</awesome
>}, @xml
81 def test_should_serialize_yaml
82 assert_match
%r
{<preferences type
=\"yaml
\">--- \n:gem: ruby
\n</preferences
>}, @xml
86 class NilXmlSerializationTest
< ActiveRecord
::TestCase
88 @xml = Contact
.new
.to_xml(:root => 'xml_contact')
91 def test_should_serialize_string
92 assert_match
%r
{<name
nil="true"></name
>}, @xml
95 def test_should_serialize_integer
96 assert
%r
{<age (.*)></age
>}.match(@xml)
98 assert_match
%r
{nil="true"}, attributes
99 assert_match
%r
{type
="integer"}, attributes
102 def test_should_serialize_binary
103 assert
%r
{<avatar (.*)></avatar
>}.match(@xml)
105 assert_match
%r
{type
="binary"}, attributes
106 assert_match
%r
{encoding
="base64"}, attributes
107 assert_match
%r
{nil="true"}, attributes
110 def test_should_serialize_datetime
111 assert
%r
{<created-at
(.*)></created-at
>}.match(@xml)
113 assert_match
%r
{nil="true"}, attributes
114 assert_match
%r
{type
="datetime"}, attributes
117 def test_should_serialize_boolean
118 assert
%r
{<awesome (.*)></awesome
>}.match(@xml)
120 assert_match
%r
{type
="boolean"}, attributes
121 assert_match
%r
{nil="true"}, attributes
124 def test_should_serialize_yaml
125 assert
%r
{<preferences(.*)></preferences
>}.match(@xml)
127 assert_match
%r
{type
="yaml"}, attributes
128 assert_match
%r
{nil="true"}, attributes
132 class DatabaseConnectedXmlSerializationTest
< ActiveRecord
::TestCase
133 fixtures
:authors, :posts
134 # to_xml used to mess with the hash the user provided which
135 # caused the builder to be reused. This meant the document kept
136 # getting appended to.
137 def test_passing_hash_shouldnt_reuse_builder
138 options
= {:include=>:posts}
139 david
= authors(:david)
140 first_xml_size
= david
.to_xml(options
).size
141 second_xml_size
= david
.to_xml(options
).size
142 assert_equal first_xml_size
, second_xml_size
145 def test_include_uses_association_name
146 xml
= authors(:david).to_xml
:include=>:hello_posts, :indent => 0
147 assert_match
%r
{<hello-posts type
="array">}, xml
148 assert_match
%r
{<hello-post type
="Post">}, xml
149 assert_match
%r
{<hello-post type
="StiPost">}, xml
152 def test_included_associations_should_skip_types
153 xml
= authors(:david).to_xml
:include=>:hello_posts, :indent => 0, :skip_types => true
154 assert_match
%r
{<hello-posts
>}, xml
155 assert_match
%r
{<hello-post
>}, xml
156 assert_match
%r
{<hello-post
>}, xml
159 def test_methods_are_called_on_object
160 xml
= authors(:david).to_xml
:methods => :label, :indent => 0
161 assert_match
%r
{<label
>.*</label
>}, xml
164 def test_should_not_call_methods_on_associations_that_dont_respond
165 xml
= authors(:david).to_xml
:include=>:hello_posts, :methods => :label, :indent => 2
166 assert
!authors(:david).hello_posts
.first
.respond_to
?(:label)
167 assert_match
%r
{^
<label
>.*</label
>}, xml
168 assert_no_match
%r
{^
<label
>}, xml
171 def test_procs_are_called_on_object
172 proc
= Proc
.new
{ |options
| options
[:builder].tag
!('nationality', 'Danish') }
173 xml
= authors(:david).to_xml(:procs => [ proc
])
174 assert_match
%r
{<nationality
>Danish
</nationality
>}, xml
177 def test_top_level_procs_arent_applied_to_associations
178 author_proc
= Proc
.new
{ |options
| options
[:builder].tag
!('nationality', 'Danish') }
179 xml
= authors(:david).to_xml(:procs => [ author_proc
], :include => :posts, :indent => 2)
181 assert_match
%r
{^
<nationality
>Danish
</nationality
>}, xml
182 assert_no_match
%r
{^
{6}<nationality
>Danish
</nationality
>}, xml
185 def test_procs_on_included_associations_are_called
186 posts_proc
= Proc
.new
{ |options
| options
[:builder].tag
!('copyright', 'DHH') }
187 xml
= authors(:david).to_xml(
190 :posts => { :procs => [ posts_proc
] }
194 assert_no_match
%r
{^
<copyright
>DHH
</copyright
>}, xml
195 assert_match
%r
{^
{6}<copyright
>DHH
</copyright
>}, xml
198 def test_should_include_empty_has_many_as_empty_array
199 authors(:david).posts
.delete_all
200 xml
= authors(:david).to_xml
:include=>:posts, :indent => 2
202 assert_equal
[], Hash
.from_xml(xml
)['author']['posts']
203 assert_match
%r
{^
<posts type
="array"/>}, xml
206 def test_should_has_many_array_elements_should_include_type_when_different_from_guessed_value
207 xml
= authors(:david).to_xml
:include=>:posts_with_comments, :indent => 2
209 assert Hash
.from_xml(xml
)
210 assert_match
%r
{^
<posts-with-comments type
="array">}, xml
211 assert_match
%r
{^
<posts-with-comment type
="Post">}, xml
212 assert_match
%r
{^
<posts-with-comment type
="StiPost">}, xml
214 types
= Hash
.from_xml(xml
)['author']['posts_with_comments'].collect
{|t
| t
['type'] }
215 assert types
.include?('SpecialPost')
216 assert types
.include?('Post')
217 assert types
.include?('StiPost')