b49997669e0f165565845eb7911e2a80cbb49724
[feedcatcher.git] / vendor / rails / activerecord / test / cases / xml_serialization_test.rb
1 require "cases/helper"
2 require 'models/contact'
3 require 'models/post'
4 require 'models/author'
5 require 'models/tagging'
6 require 'models/comment'
7
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
13 end
14
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
19 end
20
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
25 end
26
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
32 end
33
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
39 end
40
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)
44 end
45
46 def test_should_include_yielded_additions
47 @xml = Contact.new.to_xml do |xml|
48 xml.creator "David"
49 end
50 assert_match %r{<creator>David</creator>}, @xml
51 end
52 end
53
54 class DefaultXmlSerializationTest < ActiveRecord::TestCase
55 def setup
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
57 end
58
59 def test_should_serialize_string
60 assert_match %r{<name>aaron stack</name>}, @xml
61 end
62
63 def test_should_serialize_integer
64 assert_match %r{<age type="integer">25</age>}, @xml
65 end
66
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
71 end
72
73 def test_should_serialize_datetime
74 assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml
75 end
76
77 def test_should_serialize_boolean
78 assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml
79 end
80
81 def test_should_serialize_yaml
82 assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml
83 end
84 end
85
86 class NilXmlSerializationTest < ActiveRecord::TestCase
87 def setup
88 @xml = Contact.new.to_xml(:root => 'xml_contact')
89 end
90
91 def test_should_serialize_string
92 assert_match %r{<name nil="true"></name>}, @xml
93 end
94
95 def test_should_serialize_integer
96 assert %r{<age (.*)></age>}.match(@xml)
97 attributes = $1
98 assert_match %r{nil="true"}, attributes
99 assert_match %r{type="integer"}, attributes
100 end
101
102 def test_should_serialize_binary
103 assert %r{<avatar (.*)></avatar>}.match(@xml)
104 attributes = $1
105 assert_match %r{type="binary"}, attributes
106 assert_match %r{encoding="base64"}, attributes
107 assert_match %r{nil="true"}, attributes
108 end
109
110 def test_should_serialize_datetime
111 assert %r{<created-at (.*)></created-at>}.match(@xml)
112 attributes = $1
113 assert_match %r{nil="true"}, attributes
114 assert_match %r{type="datetime"}, attributes
115 end
116
117 def test_should_serialize_boolean
118 assert %r{<awesome (.*)></awesome>}.match(@xml)
119 attributes = $1
120 assert_match %r{type="boolean"}, attributes
121 assert_match %r{nil="true"}, attributes
122 end
123
124 def test_should_serialize_yaml
125 assert %r{<preferences(.*)></preferences>}.match(@xml)
126 attributes = $1
127 assert_match %r{type="yaml"}, attributes
128 assert_match %r{nil="true"}, attributes
129 end
130 end
131
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
143 end
144
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
150 end
151
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
157 end
158
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
162 end
163
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
169 end
170
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
175 end
176
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)
180
181 assert_match %r{^ <nationality>Danish</nationality>}, xml
182 assert_no_match %r{^ {6}<nationality>Danish</nationality>}, xml
183 end
184
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(
188 :indent => 2,
189 :include => {
190 :posts => { :procs => [ posts_proc ] }
191 }
192 )
193
194 assert_no_match %r{^ <copyright>DHH</copyright>}, xml
195 assert_match %r{^ {6}<copyright>DHH</copyright>}, xml
196 end
197
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
201
202 assert_equal [], Hash.from_xml(xml)['author']['posts']
203 assert_match %r{^ <posts type="array"/>}, xml
204 end
205
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
208
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
213
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')
218 end
219
220 end