Froze rails gems
[depot.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_include_yielded_additions
35 @xml = Contact.new.to_xml do |xml|
36 xml.creator "David"
37 end
38
39 assert_match %r{<creator>David</creator>}, @xml
40 end
41 end
42
43 class DefaultXmlSerializationTest < ActiveRecord::TestCase
44 def setup
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
46 end
47
48 def test_should_serialize_string
49 assert_match %r{<name>aaron stack</name>}, @xml
50 end
51
52 def test_should_serialize_integer
53 assert_match %r{<age type="integer">25</age>}, @xml
54 end
55
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
60 end
61
62 def test_should_serialize_datetime
63 assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml
64 end
65
66 def test_should_serialize_boolean
67 assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml
68 end
69
70 def test_should_serialize_yaml
71 assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml
72 end
73 end
74
75 class NilXmlSerializationTest < ActiveRecord::TestCase
76 def setup
77 @xml = Contact.new.to_xml(:root => 'xml_contact')
78 end
79
80 def test_should_serialize_string
81 assert_match %r{<name nil="true"></name>}, @xml
82 end
83
84 def test_should_serialize_integer
85 assert %r{<age (.*)></age>}.match(@xml)
86 attributes = $1
87 assert_match %r{nil="true"}, attributes
88 assert_match %r{type="integer"}, attributes
89 end
90
91 def test_should_serialize_binary
92 assert %r{<avatar (.*)></avatar>}.match(@xml)
93 attributes = $1
94 assert_match %r{type="binary"}, attributes
95 assert_match %r{encoding="base64"}, attributes
96 assert_match %r{nil="true"}, attributes
97 end
98
99 def test_should_serialize_datetime
100 assert %r{<created-at (.*)></created-at>}.match(@xml)
101 attributes = $1
102 assert_match %r{nil="true"}, attributes
103 assert_match %r{type="datetime"}, attributes
104 end
105
106 def test_should_serialize_boolean
107 assert %r{<awesome (.*)></awesome>}.match(@xml)
108 attributes = $1
109 assert_match %r{type="boolean"}, attributes
110 assert_match %r{nil="true"}, attributes
111 end
112
113 def test_should_serialize_yaml
114 assert %r{<preferences(.*)></preferences>}.match(@xml)
115 attributes = $1
116 assert_match %r{type="yaml"}, attributes
117 assert_match %r{nil="true"}, attributes
118 end
119 end
120
121 class DatabaseConnectedXmlSerializationTest < ActiveRecord::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
132 end
133
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
139 end
140
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
144 end
145
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
151 end
152
153 def test_procs_are_called_on_object
154 proc = Proc.new { |options| options[:builder].tag!('nationality', 'Danish') }
155 xml = authors(:david).to_xml(:procs => [ proc ])
156 assert_match %r{<nationality>Danish</nationality>}, xml
157 end
158
159 def test_top_level_procs_arent_applied_to_associations
160 author_proc = Proc.new { |options| options[:builder].tag!('nationality', 'Danish') }
161 xml = authors(:david).to_xml(:procs => [ author_proc ], :include => :posts, :indent => 2)
162
163 assert_match %r{^ <nationality>Danish</nationality>}, xml
164 assert_no_match %r{^ {6}<nationality>Danish</nationality>}, xml
165 end
166
167 def test_procs_on_included_associations_are_called
168 posts_proc = Proc.new { |options| options[:builder].tag!('copyright', 'DHH') }
169 xml = authors(:david).to_xml(
170 :indent => 2,
171 :include => {
172 :posts => { :procs => [ posts_proc ] }
173 }
174 )
175
176 assert_no_match %r{^ <copyright>DHH</copyright>}, xml
177 assert_match %r{^ {6}<copyright>DHH</copyright>}, xml
178 end
179
180 def test_should_include_empty_has_many_as_empty_array
181 authors(:david).posts.delete_all
182 xml = authors(:david).to_xml :include=>:posts, :indent => 2
183
184 assert_equal [], Hash.from_xml(xml)['author']['posts']
185 assert_match %r{^ <posts type="array"/>}, xml
186 end
187
188 def test_should_has_many_array_elements_should_include_type_when_different_from_guessed_value
189 xml = authors(:david).to_xml :include=>:posts_with_comments, :indent => 2
190
191 assert Hash.from_xml(xml)
192 assert_match %r{^ <posts-with-comments type="array">}, xml
193 assert_match %r{^ <posts-with-comment type="Post">}, xml
194 assert_match %r{^ <posts-with-comment type="StiPost">}, xml
195
196 types = Hash.from_xml(xml)['author']['posts_with_comments'].collect {|t| t['type'] }
197 assert types.include?('SpecialPost')
198 assert types.include?('Post')
199 assert types.include?('StiPost')
200 end
201
202 end