2
require 'models/contact'
4
require 'models/author'
5
require 'models/tagging'
6
require 'models/comment'
7
require 'models/company_in_module'
9
class XmlSerializationTest < ActiveRecord::TestCase
10
def test_should_serialize_default_root
11
@xml = Contact.new.to_xml
12
assert_match %r{^<contact>}, @xml
13
assert_match %r{</contact>$}, @xml
16
def test_should_serialize_default_root_with_namespace
17
@xml = Contact.new.to_xml :namespace=>"http://xml.rubyonrails.org/contact"
18
assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml
19
assert_match %r{</contact>$}, @xml
22
def test_should_serialize_custom_root
23
@xml = Contact.new.to_xml :root => 'xml_contact'
24
assert_match %r{^<xml-contact>}, @xml
25
assert_match %r{</xml-contact>$}, @xml
28
def test_should_allow_undasherized_tags
29
@xml = Contact.new.to_xml :root => 'xml_contact', :dasherize => false
30
assert_match %r{^<xml_contact>}, @xml
31
assert_match %r{</xml_contact>$}, @xml
32
assert_match %r{<created_at}, @xml
35
def test_should_allow_camelized_tags
36
@xml = Contact.new.to_xml :root => 'xml_contact', :camelize => true
37
assert_match %r{^<XmlContact>}, @xml
38
assert_match %r{</XmlContact>$}, @xml
39
assert_match %r{<CreatedAt}, @xml
42
def test_should_allow_skipped_types
43
@xml = Contact.new(:age => 25).to_xml :skip_types => true
44
assert %r{<age>25</age>}.match(@xml)
47
def test_should_include_yielded_additions
48
@xml = Contact.new.to_xml do |xml|
51
assert_match %r{<creator>David</creator>}, @xml
55
class DefaultXmlSerializationTest < ActiveRecord::TestCase
57
@xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }).to_xml
60
def test_should_serialize_string
61
assert_match %r{<name>aaron stack</name>}, @xml
64
def test_should_serialize_integer
65
assert_match %r{<age type="integer">25</age>}, @xml
68
def test_should_serialize_binary
69
assert_match %r{YmluYXJ5ZGF0YQ==\n</avatar>}, @xml
70
assert_match %r{<avatar(.*)(type="binary")}, @xml
71
assert_match %r{<avatar(.*)(encoding="base64")}, @xml
74
def test_should_serialize_datetime
75
assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml
78
def test_should_serialize_boolean
79
assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml
82
def test_should_serialize_yaml
83
assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml
87
class NilXmlSerializationTest < ActiveRecord::TestCase
89
@xml = Contact.new.to_xml(:root => 'xml_contact')
92
def test_should_serialize_string
93
assert_match %r{<name nil="true"></name>}, @xml
96
def test_should_serialize_integer
97
assert %r{<age (.*)></age>}.match(@xml)
99
assert_match %r{nil="true"}, attributes
100
assert_match %r{type="integer"}, attributes
103
def test_should_serialize_binary
104
assert %r{<avatar (.*)></avatar>}.match(@xml)
106
assert_match %r{type="binary"}, attributes
107
assert_match %r{encoding="base64"}, attributes
108
assert_match %r{nil="true"}, attributes
111
def test_should_serialize_datetime
112
assert %r{<created-at (.*)></created-at>}.match(@xml)
114
assert_match %r{nil="true"}, attributes
115
assert_match %r{type="datetime"}, attributes
118
def test_should_serialize_boolean
119
assert %r{<awesome (.*)></awesome>}.match(@xml)
121
assert_match %r{type="boolean"}, attributes
122
assert_match %r{nil="true"}, attributes
125
def test_should_serialize_yaml
126
assert %r{<preferences(.*)></preferences>}.match(@xml)
128
assert_match %r{type="yaml"}, attributes
129
assert_match %r{nil="true"}, attributes
133
class DatabaseConnectedXmlModuleSerializationTest < ActiveRecord::TestCase
135
fixtures :projects, :developers, :developers_projects
138
project = MyApplication::Business::Project.find :first
140
assert_match %r{<my-application-business-project>}, xml
141
assert_match %r{</my-application-business-project>}, xml
144
def test_module_with_include
145
project = MyApplication::Business::Project.find :first
146
xml = project.to_xml :include => :developers
147
assert_match %r{<developer type="MyApplication::Business::Developer">}, xml
148
assert_match %r{</developer>}, xml
152
class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase
153
fixtures :authors, :posts
154
# to_xml used to mess with the hash the user provided which
155
# caused the builder to be reused. This meant the document kept
156
# getting appended to.
157
def test_passing_hash_shouldnt_reuse_builder
158
options = {:include=>:posts}
159
david = authors(:david)
160
first_xml_size = david.to_xml(options).size
161
second_xml_size = david.to_xml(options).size
162
assert_equal first_xml_size, second_xml_size
165
def test_include_uses_association_name
166
xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0
167
assert_match %r{<hello-posts type="array">}, xml
168
assert_match %r{<hello-post type="Post">}, xml
169
assert_match %r{<hello-post type="StiPost">}, xml
172
def test_included_associations_should_skip_types
173
xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0, :skip_types => true
174
assert_match %r{<hello-posts>}, xml
175
assert_match %r{<hello-post>}, xml
176
assert_match %r{<hello-post>}, xml
179
def test_methods_are_called_on_object
180
xml = authors(:david).to_xml :methods => :label, :indent => 0
181
assert_match %r{<label>.*</label>}, xml
184
def test_should_not_call_methods_on_associations_that_dont_respond
185
xml = authors(:david).to_xml :include=>:hello_posts, :methods => :label, :indent => 2
186
assert !authors(:david).hello_posts.first.respond_to?(:label)
187
assert_match %r{^ <label>.*</label>}, xml
188
assert_no_match %r{^ <label>}, xml
191
def test_procs_are_called_on_object
192
proc = Proc.new { |options| options[:builder].tag!('nationality', 'Danish') }
193
xml = authors(:david).to_xml(:procs => [ proc ])
194
assert_match %r{<nationality>Danish</nationality>}, xml
197
def test_top_level_procs_arent_applied_to_associations
198
author_proc = Proc.new { |options| options[:builder].tag!('nationality', 'Danish') }
199
xml = authors(:david).to_xml(:procs => [ author_proc ], :include => :posts, :indent => 2)
201
assert_match %r{^ <nationality>Danish</nationality>}, xml
202
assert_no_match %r{^ {6}<nationality>Danish</nationality>}, xml
205
def test_procs_on_included_associations_are_called
206
posts_proc = Proc.new { |options| options[:builder].tag!('copyright', 'DHH') }
207
xml = authors(:david).to_xml(
210
:posts => { :procs => [ posts_proc ] }
214
assert_no_match %r{^ <copyright>DHH</copyright>}, xml
215
assert_match %r{^ {6}<copyright>DHH</copyright>}, xml
218
def test_should_include_empty_has_many_as_empty_array
219
authors(:david).posts.delete_all
220
xml = authors(:david).to_xml :include=>:posts, :indent => 2
222
assert_equal [], Hash.from_xml(xml)['author']['posts']
223
assert_match %r{^ <posts type="array"/>}, xml
226
def test_should_has_many_array_elements_should_include_type_when_different_from_guessed_value
227
xml = authors(:david).to_xml :include=>:posts_with_comments, :indent => 2
229
assert Hash.from_xml(xml)
230
assert_match %r{^ <posts-with-comments type="array">}, xml
231
assert_match %r{^ <posts-with-comment type="Post">}, xml
232
assert_match %r{^ <posts-with-comment type="StiPost">}, xml
234
types = Hash.from_xml(xml)['author']['posts_with_comments'].collect {|t| t['type'] }
235
assert types.include?('SpecialPost')
236
assert types.include?('Post')
237
assert types.include?('StiPost')