~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activerecord/test/cases/xml_serialization_test.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
require 'models/company_in_module'
8
 
 
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
14
 
  end
15
 
 
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
20
 
  end
21
 
 
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
26
 
  end
27
 
 
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
33
 
  end
34
 
 
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
40
 
  end
41
 
 
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)
45
 
  end
46
 
 
47
 
  def test_should_include_yielded_additions
48
 
    @xml = Contact.new.to_xml do |xml|
49
 
      xml.creator "David"
50
 
    end
51
 
    assert_match %r{<creator>David</creator>}, @xml
52
 
  end
53
 
end
54
 
 
55
 
class DefaultXmlSerializationTest < ActiveRecord::TestCase
56
 
  def setup
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
58
 
  end
59
 
 
60
 
  def test_should_serialize_string
61
 
    assert_match %r{<name>aaron stack</name>},     @xml
62
 
  end
63
 
 
64
 
  def test_should_serialize_integer
65
 
    assert_match %r{<age type="integer">25</age>}, @xml
66
 
  end
67
 
 
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
72
 
  end
73
 
 
74
 
  def test_should_serialize_datetime
75
 
    assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml
76
 
  end
77
 
 
78
 
  def test_should_serialize_boolean
79
 
    assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml
80
 
  end
81
 
 
82
 
  def test_should_serialize_yaml
83
 
    assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml
84
 
  end
85
 
end
86
 
 
87
 
class NilXmlSerializationTest < ActiveRecord::TestCase
88
 
  def setup
89
 
    @xml = Contact.new.to_xml(:root => 'xml_contact')
90
 
  end
91
 
 
92
 
  def test_should_serialize_string
93
 
    assert_match %r{<name nil="true"></name>},     @xml
94
 
  end
95
 
 
96
 
  def test_should_serialize_integer
97
 
    assert %r{<age (.*)></age>}.match(@xml)
98
 
    attributes = $1
99
 
    assert_match %r{nil="true"}, attributes
100
 
    assert_match %r{type="integer"}, attributes
101
 
  end
102
 
 
103
 
  def test_should_serialize_binary
104
 
    assert %r{<avatar (.*)></avatar>}.match(@xml)
105
 
    attributes = $1
106
 
    assert_match %r{type="binary"}, attributes
107
 
    assert_match %r{encoding="base64"}, attributes
108
 
    assert_match %r{nil="true"}, attributes
109
 
  end
110
 
 
111
 
  def test_should_serialize_datetime
112
 
    assert %r{<created-at (.*)></created-at>}.match(@xml)
113
 
    attributes = $1
114
 
    assert_match %r{nil="true"}, attributes
115
 
    assert_match %r{type="datetime"}, attributes
116
 
  end
117
 
 
118
 
  def test_should_serialize_boolean
119
 
    assert %r{<awesome (.*)></awesome>}.match(@xml)
120
 
    attributes = $1
121
 
    assert_match %r{type="boolean"}, attributes
122
 
    assert_match %r{nil="true"}, attributes
123
 
  end
124
 
 
125
 
  def test_should_serialize_yaml
126
 
    assert %r{<preferences(.*)></preferences>}.match(@xml)
127
 
    attributes = $1
128
 
    assert_match %r{type="yaml"}, attributes
129
 
    assert_match %r{nil="true"}, attributes
130
 
  end
131
 
end
132
 
 
133
 
class DatabaseConnectedXmlModuleSerializationTest < ActiveRecord::TestCase
134
 
 
135
 
  fixtures :projects, :developers, :developers_projects
136
 
 
137
 
  def test_module
138
 
    project = MyApplication::Business::Project.find :first
139
 
    xml = project.to_xml
140
 
    assert_match %r{<my-application-business-project>}, xml
141
 
    assert_match %r{</my-application-business-project>}, xml
142
 
  end
143
 
 
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
149
 
  end
150
 
end
151
 
 
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
163
 
  end
164
 
 
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
170
 
  end
171
 
 
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
177
 
  end
178
 
 
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
182
 
  end
183
 
 
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
189
 
  end
190
 
 
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
195
 
  end
196
 
 
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)
200
 
 
201
 
    assert_match %r{^  <nationality>Danish</nationality>}, xml
202
 
    assert_no_match %r{^ {6}<nationality>Danish</nationality>}, xml
203
 
  end
204
 
 
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(
208
 
      :indent => 2,
209
 
      :include => {
210
 
        :posts => { :procs => [ posts_proc ] }
211
 
      }
212
 
    )
213
 
 
214
 
    assert_no_match %r{^  <copyright>DHH</copyright>}, xml
215
 
    assert_match %r{^ {6}<copyright>DHH</copyright>}, xml
216
 
  end
217
 
 
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
221
 
 
222
 
    assert_equal [], Hash.from_xml(xml)['author']['posts']
223
 
    assert_match %r{^  <posts type="array"/>}, xml
224
 
  end
225
 
 
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
228
 
 
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
233
 
 
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')
238
 
  end
239
 
 
240
 
end