Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activerecord / test / xml_serialization_test.rb
blob011f27ad146c0982447709a067bf3504c49a3b46
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
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
34   def test_should_include_yielded_additions
35     @xml = Contact.new.to_xml do |xml|
36       xml.creator "David"
37     end
39     assert_match %r{<creator>David</creator>}, @xml
40   end
41 end
43 class DefaultXmlSerializationTest < Test::Unit::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
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
75 class NilXmlSerializationTest < Test::Unit::TestCase
76   def setup
77     @xml = Contact.new.to_xml(:root => 'xml_contact')
78   end
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
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
132   end
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_should_include_empty_has_many_as_empty_array
154     authors(:david).posts.delete_all    
155     xml = authors(:david).to_xml :include=>:posts, :indent => 2
156     
157     assert_equal [], Hash.from_xml(xml)['author']['posts']
158     assert_match %r{^  <posts type="array"/>}, xml
159   end
160   
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
163     
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')
173   end
174