ibm_db 2.5.17-universal-darwin-13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES +221 -0
  3. data/LICENSE +18 -0
  4. data/MANIFEST +14 -0
  5. data/ParameterizedQueries README +39 -0
  6. data/README +225 -0
  7. data/ext/Makefile.nt32 +181 -0
  8. data/ext/Makefile.nt32.191 +212 -0
  9. data/ext/extconf.rb +127 -0
  10. data/ext/ibm_db.c +11719 -0
  11. data/ext/ruby_ibm_db.h +240 -0
  12. data/ext/ruby_ibm_db_cli.c +845 -0
  13. data/ext/ruby_ibm_db_cli.h +489 -0
  14. data/init.rb +42 -0
  15. data/lib/IBM_DB.rb +16 -0
  16. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +3031 -0
  17. data/lib/active_record/connection_adapters/ibm_db_pstmt.rb +1965 -0
  18. data/lib/active_record/connection_adapters/ibmdb_adapter.rb +2 -0
  19. data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -0
  20. data/lib/linux/rb18x/ibm_db.bundle +0 -0
  21. data/lib/linux/rb19x/ibm_db.bundle +0 -0
  22. data/lib/linux/rb20x/ibm_db.bundle +0 -0
  23. data/lib/linux/rb21x/ibm_db.bundle +0 -0
  24. data/test/cases/adapter_test.rb +207 -0
  25. data/test/cases/associations/belongs_to_associations_test.rb +711 -0
  26. data/test/cases/associations/cascaded_eager_loading_test.rb +181 -0
  27. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +851 -0
  28. data/test/cases/associations/join_model_test.rb +743 -0
  29. data/test/cases/attribute_methods_test.rb +822 -0
  30. data/test/cases/base_test.rb +2133 -0
  31. data/test/cases/calculations_test.rb +482 -0
  32. data/test/cases/migration_test.rb +2408 -0
  33. data/test/cases/persistence_test.rb +642 -0
  34. data/test/cases/query_cache_test.rb +257 -0
  35. data/test/cases/relations_test.rb +1182 -0
  36. data/test/cases/schema_dumper_test.rb +256 -0
  37. data/test/cases/transaction_callbacks_test.rb +300 -0
  38. data/test/cases/validations/uniqueness_validation_test.rb +299 -0
  39. data/test/cases/xml_serialization_test.rb +408 -0
  40. data/test/config.yml +154 -0
  41. data/test/connections/native_ibm_db/connection.rb +44 -0
  42. data/test/ibm_db_test.rb +25 -0
  43. data/test/models/warehouse_thing.rb +5 -0
  44. data/test/schema/i5/ibm_db_specific_schema.rb +137 -0
  45. data/test/schema/ids/ibm_db_specific_schema.rb +140 -0
  46. data/test/schema/luw/ibm_db_specific_schema.rb +137 -0
  47. data/test/schema/schema.rb +751 -0
  48. data/test/schema/zOS/ibm_db_specific_schema.rb +208 -0
  49. metadata +109 -0
@@ -0,0 +1,299 @@
1
+ # encoding: utf-8
2
+ require "cases/helper"
3
+ require 'models/topic'
4
+ require 'models/reply'
5
+ require 'models/warehouse_thing'
6
+ require 'models/guid'
7
+ require 'models/event'
8
+
9
+ class Wizard < ActiveRecord::Base
10
+ self.abstract_class = true
11
+
12
+ validates_uniqueness_of :name
13
+ end
14
+
15
+ class IneptWizard < Wizard
16
+ validates_uniqueness_of :city
17
+ end
18
+
19
+ class Conjurer < IneptWizard
20
+ end
21
+
22
+ class Thaumaturgist < IneptWizard
23
+ end
24
+
25
+ class UniquenessValidationTest < ActiveRecord::TestCase
26
+ fixtures :topics, 'warehouse_things', :developers
27
+
28
+ repair_validations(Topic, Reply)
29
+
30
+ def test_validate_uniqueness
31
+ Topic.validates_uniqueness_of(:title)
32
+
33
+ t = Topic.new("title" => "I'm uniqué!")
34
+ assert t.save, "Should save t as unique"
35
+
36
+ t.content = "Remaining unique"
37
+ assert t.save, "Should still save t as unique"
38
+
39
+ t2 = Topic.new("title" => "I'm uniqué!")
40
+ assert !t2.valid?, "Shouldn't be valid"
41
+ assert !t2.save, "Shouldn't save t2 as unique"
42
+ assert_equal ["has already been taken"], t2.errors[:title]
43
+
44
+ t2.title = "Now Im really also unique"
45
+ assert t2.save, "Should now save t2 as unique"
46
+ end
47
+
48
+ def test_validates_uniqueness_with_validates
49
+ Topic.validates :title, :uniqueness => true
50
+ Topic.create!('title' => 'abc')
51
+
52
+ t2 = Topic.new('title' => 'abc')
53
+ assert !t2.valid?
54
+ assert t2.errors[:title]
55
+ end
56
+
57
+ def test_validates_uniqueness_with_newline_chars
58
+ Topic.validates_uniqueness_of(:title, :case_sensitive => false)
59
+
60
+ t = Topic.new("title" => "new\nline")
61
+ assert t.save, "Should save t as unique"
62
+ end
63
+
64
+ def test_validate_uniqueness_with_scope
65
+ Reply.validates_uniqueness_of(:content, :scope => "parent_id")
66
+
67
+ t = Topic.create("title" => "I'm unique!")
68
+
69
+ r1 = t.replies.create "title" => "r1", "content" => "hello world"
70
+ assert r1.valid?, "Saving r1"
71
+
72
+ r2 = t.replies.create "title" => "r2", "content" => "hello world"
73
+ assert !r2.valid?, "Saving r2 first time"
74
+
75
+ r2.content = "something else"
76
+ assert r2.save, "Saving r2 second time"
77
+
78
+ t2 = Topic.create("title" => "I'm unique too!")
79
+ r3 = t2.replies.create "title" => "r3", "content" => "hello world"
80
+ assert r3.valid?, "Saving r3"
81
+ end
82
+
83
+ def test_validate_uniqueness_scoped_to_defining_class
84
+ t = Topic.create("title" => "What, me worry?")
85
+
86
+ r1 = t.unique_replies.create "title" => "r1", "content" => "a barrel of fun"
87
+ assert r1.valid?, "Saving r1"
88
+
89
+ r2 = t.silly_unique_replies.create "title" => "r2", "content" => "a barrel of fun"
90
+ assert !r2.valid?, "Saving r2"
91
+
92
+ # Should succeed as validates_uniqueness_of only applies to
93
+ # UniqueReply and its subclasses
94
+ r3 = t.replies.create "title" => "r2", "content" => "a barrel of fun"
95
+ assert r3.valid?, "Saving r3"
96
+ end
97
+
98
+ def test_validate_uniqueness_with_scope_array
99
+ Reply.validates_uniqueness_of(:author_name, :scope => [:author_email_address, :parent_id])
100
+
101
+ t = Topic.create("title" => "The earth is actually flat!")
102
+
103
+ r1 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply"
104
+ assert r1.valid?, "Saving r1"
105
+
106
+ r2 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply again..."
107
+ assert !r2.valid?, "Saving r2. Double reply by same author."
108
+
109
+ r2.author_email_address = "jeremy_alt_email@rubyonrails.com"
110
+ assert r2.save, "Saving r2 the second time."
111
+
112
+ r3 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy_alt_email@rubyonrails.com", "title" => "You're wrong", "content" => "It's cubic"
113
+ assert !r3.valid?, "Saving r3"
114
+
115
+ r3.author_name = "jj"
116
+ assert r3.save, "Saving r3 the second time."
117
+
118
+ r3.author_name = "jeremy"
119
+ assert !r3.save, "Saving r3 the third time."
120
+ end
121
+
122
+ def test_validate_case_insensitive_uniqueness
123
+ Topic.validates_uniqueness_of(:title, :parent_id, :case_sensitive => false, :allow_nil => true)
124
+
125
+ t = Topic.new("title" => "I'm unique!", :parent_id => 2)
126
+ assert t.save, "Should save t as unique"
127
+
128
+ t.content = "Remaining unique"
129
+ assert t.save, "Should still save t as unique"
130
+
131
+ t2 = Topic.new("title" => "I'm UNIQUE!", :parent_id => 1)
132
+ assert !t2.valid?, "Shouldn't be valid"
133
+ assert !t2.save, "Shouldn't save t2 as unique"
134
+ assert t2.errors[:title].any?
135
+ assert t2.errors[:parent_id].any?
136
+ assert_equal ["has already been taken"], t2.errors[:title]
137
+
138
+ t2.title = "I'm truly UNIQUE!"
139
+ assert !t2.valid?, "Shouldn't be valid"
140
+ assert !t2.save, "Shouldn't save t2 as unique"
141
+ assert t2.errors[:title].empty?
142
+ assert t2.errors[:parent_id].any?
143
+
144
+ t2.parent_id = 4
145
+ assert t2.save, "Should now save t2 as unique"
146
+
147
+ t2.parent_id = nil
148
+ t2.title = nil
149
+ assert t2.valid?, "should validate with nil"
150
+ assert t2.save, "should save with nil"
151
+
152
+ with_kcode('UTF8') do
153
+ t_utf8 = Topic.new("title" => "Я тоже уникальный!")
154
+ assert t_utf8.save, "Should save t_utf8 as unique"
155
+
156
+ # If database hasn't UTF-8 character set, this test fails
157
+ if Topic.find(t_utf8, :select => 'LOWER(title) AS title').title == "я тоже уникальный!"
158
+ t2_utf8 = Topic.new("title" => "я тоже УНИКАЛЬНЫЙ!")
159
+ assert !t2_utf8.valid?, "Shouldn't be valid"
160
+ assert !t2_utf8.save, "Shouldn't save t2_utf8 as unique"
161
+ end
162
+ end
163
+ end
164
+
165
+ def test_validate_case_sensitive_uniqueness_with_special_sql_like_chars
166
+ Topic.validates_uniqueness_of(:title, :case_sensitive => true)
167
+
168
+ t = Topic.new("title" => "I'm unique!")
169
+ assert t.save, "Should save t as unique"
170
+
171
+ t2 = Topic.new("title" => "I'm %")
172
+ assert t2.save, "Should save t2 as unique"
173
+
174
+ t3 = Topic.new("title" => "I'm uniqu_!")
175
+ assert t3.save, "Should save t3 as unique"
176
+ end
177
+
178
+ def test_validate_case_insensitive_uniqueness_with_special_sql_like_chars
179
+ Topic.validates_uniqueness_of(:title, :case_sensitive => false)
180
+
181
+ t = Topic.new("title" => "I'm unique!")
182
+ assert t.save, "Should save t as unique"
183
+
184
+ t2 = Topic.new("title" => "I'm %")
185
+ assert t2.save, "Should save t2 as unique"
186
+
187
+ t3 = Topic.new("title" => "I'm uniqu_!")
188
+ assert t3.save, "Should save t3 as unique"
189
+ end
190
+
191
+ def test_validate_case_sensitive_uniqueness
192
+ Topic.validates_uniqueness_of(:title, :case_sensitive => true, :allow_nil => true)
193
+
194
+ t = Topic.new("title" => "I'm unique!")
195
+ assert t.save, "Should save t as unique"
196
+
197
+ t.content = "Remaining unique"
198
+ assert t.save, "Should still save t as unique"
199
+
200
+ t2 = Topic.new("title" => "I'M UNIQUE!")
201
+ assert t2.valid?, "Should be valid"
202
+ assert t2.save, "Should save t2 as unique"
203
+ assert t2.errors[:title].empty?
204
+ assert t2.errors[:parent_id].empty?
205
+ assert_not_equal ["has already been taken"], t2.errors[:title]
206
+
207
+ t3 = Topic.new("title" => "I'M uNiQUe!")
208
+ assert t3.valid?, "Should be valid"
209
+ assert t3.save, "Should save t2 as unique"
210
+ assert t3.errors[:title].empty?
211
+ assert t3.errors[:parent_id].empty?
212
+ assert_not_equal ["has already been taken"], t3.errors[:title]
213
+ end
214
+
215
+ def test_validate_case_sensitive_uniqueness_with_attribute_passed_as_integer
216
+ Topic.validates_uniqueness_of(:title, :case_sensitve => true)
217
+ Topic.create!('title' => 101)
218
+
219
+ t2 = Topic.new('title' => 101)
220
+ assert !t2.valid?
221
+ assert t2.errors[:title]
222
+ end
223
+
224
+ def test_validate_uniqueness_with_non_standard_table_names
225
+ i1 = WarehouseThing.create(:value => 1000)
226
+ assert !i1.valid?, "i1 should not be valid"
227
+ assert i1.errors[:value].any?, "Should not be empty"
228
+ end
229
+
230
+ def test_validates_uniqueness_inside_with_scope
231
+ Topic.validates_uniqueness_of(:title)
232
+
233
+ Topic.send(:with_scope, :find => { :conditions => { :author_name => "David" } }) do
234
+ t1 = Topic.new("title" => "I'm unique!", "author_name" => "Mary")
235
+ assert t1.save
236
+ t2 = Topic.new("title" => "I'm unique!", "author_name" => "David")
237
+ assert !t2.valid?
238
+ end
239
+ end
240
+
241
+ def test_validate_uniqueness_with_columns_which_are_sql_keywords
242
+ repair_validations(Guid) do
243
+ Guid.validates_uniqueness_of :key
244
+ g = Guid.new
245
+ g.key = "foo"
246
+ assert_nothing_raised { !g.valid? }
247
+ end
248
+ end
249
+
250
+ def test_validate_uniqueness_with_limit
251
+ # Event.title is limited to 5 characters
252
+ e1 = Event.create(:title => "abcde")
253
+ assert e1.valid?, "Could not create an event with a unique, 5 character title"
254
+ e2 = Event.create(:title => "abcdefgh")
255
+ assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique"
256
+ end
257
+
258
+ def test_validate_uniqueness_with_limit_and_utf8
259
+ unless current_adapter?(:IBM_DBAdapter)
260
+ # Limit for the varchar field is number of bytes and not characters for DB2. Hence the below test cases is expected to fail.
261
+ with_kcode('UTF8') do
262
+ # Event.title is limited to 5 characters
263
+ e1 = Event.create(:title => "一二三四五")
264
+ assert e1.valid?, "Could not create an event with a unique, 5 character title"
265
+ e2 = Event.create(:title => "一二三四五六七八")
266
+ assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique"
267
+ end
268
+ end
269
+ end
270
+
271
+ def test_validate_straight_inheritance_uniqueness
272
+ w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork")
273
+ assert w1.valid?, "Saving w1"
274
+
275
+ # Should use validation from base class (which is abstract)
276
+ w2 = IneptWizard.new(:name => "Rincewind", :city => "Quirm")
277
+ assert !w2.valid?, "w2 shouldn't be valid"
278
+ assert w2.errors[:name].any?, "Should have errors for name"
279
+ assert_equal ["has already been taken"], w2.errors[:name], "Should have uniqueness message for name"
280
+
281
+ w3 = Conjurer.new(:name => "Rincewind", :city => "Quirm")
282
+ assert !w3.valid?, "w3 shouldn't be valid"
283
+ assert w3.errors[:name].any?, "Should have errors for name"
284
+ assert_equal ["has already been taken"], w3.errors[:name], "Should have uniqueness message for name"
285
+
286
+ w4 = Conjurer.create(:name => "The Amazing Bonko", :city => "Quirm")
287
+ assert w4.valid?, "Saving w4"
288
+
289
+ w5 = Thaumaturgist.new(:name => "The Amazing Bonko", :city => "Lancre")
290
+ assert !w5.valid?, "w5 shouldn't be valid"
291
+ assert w5.errors[:name].any?, "Should have errors for name"
292
+ assert_equal ["has already been taken"], w5.errors[:name], "Should have uniqueness message for name"
293
+
294
+ w6 = Thaumaturgist.new(:name => "Mustrum Ridcully", :city => "Quirm")
295
+ assert !w6.valid?, "w6 shouldn't be valid"
296
+ assert w6.errors[:city].any?, "Should have errors for city"
297
+ assert_equal ["has already been taken"], w6.errors[:city], "Should have uniqueness message for city"
298
+ end
299
+ end
@@ -0,0 +1,408 @@
1
+ require "cases/helper"
2
+ require 'models/contact'
3
+ require 'models/post'
4
+ require 'models/author'
5
+ require 'models/comment'
6
+ require 'models/company_in_module'
7
+ require 'models/toy'
8
+ require 'models/topic'
9
+ require 'models/reply'
10
+ require 'models/company'
11
+
12
+ class XmlSerializationTest < ActiveRecord::TestCase
13
+ def test_should_serialize_default_root
14
+ @xml = Contact.new.to_xml
15
+ assert_match %r{^<contact>}, @xml
16
+ assert_match %r{</contact>$}, @xml
17
+ end
18
+
19
+ def test_should_serialize_default_root_with_namespace
20
+ @xml = Contact.new.to_xml :namespace=>"http://xml.rubyonrails.org/contact"
21
+ assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml
22
+ assert_match %r{</contact>$}, @xml
23
+ end
24
+
25
+ def test_should_serialize_custom_root
26
+ @xml = Contact.new.to_xml :root => 'xml_contact'
27
+ assert_match %r{^<xml-contact>}, @xml
28
+ assert_match %r{</xml-contact>$}, @xml
29
+ end
30
+
31
+ def test_should_allow_undasherized_tags
32
+ @xml = Contact.new.to_xml :root => 'xml_contact', :dasherize => false
33
+ assert_match %r{^<xml_contact>}, @xml
34
+ assert_match %r{</xml_contact>$}, @xml
35
+ assert_match %r{<created_at}, @xml
36
+ end
37
+
38
+ def test_should_allow_camelized_tags
39
+ @xml = Contact.new.to_xml :root => 'xml_contact', :camelize => true
40
+ assert_match %r{^<XmlContact>}, @xml
41
+ assert_match %r{</XmlContact>$}, @xml
42
+ assert_match %r{<CreatedAt}, @xml
43
+ end
44
+
45
+ def test_should_allow_skipped_types
46
+ @xml = Contact.new(:age => 25).to_xml :skip_types => true
47
+ assert %r{<age>25</age>}.match(@xml)
48
+ end
49
+
50
+ def test_should_include_yielded_additions
51
+ @xml = Contact.new.to_xml do |xml|
52
+ xml.creator "David"
53
+ end
54
+ assert_match %r{<creator>David</creator>}, @xml
55
+ end
56
+
57
+ def test_to_xml_with_block
58
+ value = "Rockin' the block"
59
+ xml = Contact.new.to_xml(:skip_instruct => true) do |_xml|
60
+ _xml.tag! "arbitrary-element", value
61
+ end
62
+ assert_equal "<contact>", xml.first(9)
63
+ assert xml.include?(%(<arbitrary-element>#{value}</arbitrary-element>))
64
+ end
65
+
66
+ def test_should_skip_instruct_for_included_records
67
+ @contact = Contact.new
68
+ @contact.alternative = Contact.new(:name => 'Copa Cabana')
69
+ @xml = @contact.to_xml(:include => [ :alternative ])
70
+ assert_equal @xml.index('<?xml '), 0
71
+ assert_nil @xml.index('<?xml ', 1)
72
+ end
73
+ end
74
+
75
+ class DefaultXmlSerializationTest < ActiveRecord::TestCase
76
+ def setup
77
+ @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }).to_xml
78
+ end
79
+
80
+ def test_should_serialize_string
81
+ assert_match %r{<name>aaron stack</name>}, @xml
82
+ end
83
+
84
+ def test_should_serialize_integer
85
+ assert_match %r{<age type="integer">25</age>}, @xml
86
+ end
87
+
88
+ def test_should_serialize_binary
89
+ assert_match %r{YmluYXJ5ZGF0YQ==\n</avatar>}, @xml
90
+ assert_match %r{<avatar(.*)(type="binary")}, @xml
91
+ assert_match %r{<avatar(.*)(encoding="base64")}, @xml
92
+ end
93
+
94
+ def test_should_serialize_datetime
95
+ assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml
96
+ end
97
+
98
+ def test_should_serialize_boolean
99
+ assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml
100
+ end
101
+
102
+ def test_should_serialize_hash
103
+ assert_match %r{<preferences>\s*<gem>ruby</gem>\s*</preferences>}m, @xml
104
+ end
105
+ end
106
+
107
+ class DefaultXmlSerializationTimezoneTest < ActiveRecord::TestCase
108
+ def test_should_serialize_datetime_with_timezone
109
+ timezone, Time.zone = Time.zone, "Pacific Time (US & Canada)"
110
+
111
+ toy = Toy.create(:name => 'Mickey', :updated_at => Time.utc(2006, 8, 1))
112
+ unless current_adapter?(:IBM_DBAdapter)
113
+ assert_match %r{<updated-at type=\"datetime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
114
+ else
115
+ assert_match %r{<updated-at type=\"timestamp\">2006-07-31 17:00:00 -0700</updated-at>}, toy.to_xml
116
+ end
117
+ ensure
118
+ Time.zone = timezone
119
+ end
120
+
121
+ def test_should_serialize_datetime_with_timezone_reloaded
122
+ timezone, Time.zone = Time.zone, "Pacific Time (US & Canada)"
123
+
124
+ toy = Toy.create(:name => 'Minnie', :updated_at => Time.utc(2006, 8, 1)).reload
125
+ unless current_adapter?(:IBM_DBAdapter)
126
+ assert_match %r{<updated-at type=\"datetime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
127
+ else
128
+ assert_match %r{<updated-at type=\"timestamp\">2006-07-31 17:00:00 -0700</updated-at>}, toy.to_xml
129
+ end
130
+ ensure
131
+ Time.zone = timezone
132
+ end
133
+ end
134
+
135
+ class NilXmlSerializationTest < ActiveRecord::TestCase
136
+ def setup
137
+ @xml = Contact.new.to_xml(:root => 'xml_contact')
138
+ end
139
+
140
+ def test_should_serialize_string
141
+ assert_match %r{<name nil="true"></name>}, @xml
142
+ end
143
+
144
+ def test_should_serialize_integer
145
+ assert %r{<age (.*)></age>}.match(@xml)
146
+ attributes = $1
147
+ assert_match %r{nil="true"}, attributes
148
+ assert_match %r{type="integer"}, attributes
149
+ end
150
+
151
+ def test_should_serialize_binary
152
+ assert %r{<avatar (.*)></avatar>}.match(@xml)
153
+ attributes = $1
154
+ assert_match %r{type="binary"}, attributes
155
+ assert_match %r{encoding="base64"}, attributes
156
+ assert_match %r{nil="true"}, attributes
157
+ end
158
+
159
+ def test_should_serialize_datetime
160
+ assert %r{<created-at (.*)></created-at>}.match(@xml)
161
+ attributes = $1
162
+ assert_match %r{nil="true"}, attributes
163
+ assert_match %r{type="datetime"}, attributes
164
+ end
165
+
166
+ def test_should_serialize_boolean
167
+ assert %r{<awesome (.*)></awesome>}.match(@xml)
168
+ attributes = $1
169
+ assert_match %r{type="boolean"}, attributes
170
+ assert_match %r{nil="true"}, attributes
171
+ end
172
+
173
+ def test_should_serialize_yaml
174
+ assert_match %r{<preferences nil=\"true\"></preferences>}, @xml
175
+ end
176
+ end
177
+
178
+ class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase
179
+ fixtures :topics, :companies, :accounts, :authors, :posts, :projects
180
+
181
+ def test_to_xml
182
+ xml = REXML::Document.new(topics(:first).to_xml(:indent => 0))
183
+ bonus_time_in_current_timezone = topics(:first).bonus_time.xmlschema
184
+ written_on_in_current_timezone = topics(:first).written_on.xmlschema
185
+ last_read_in_current_timezone = topics(:first).last_read.xmlschema
186
+
187
+ assert_equal "topic", xml.root.name
188
+ assert_equal "The First Topic" , xml.elements["//title"].text
189
+ assert_equal "David" , xml.elements["//author-name"].text
190
+ assert_match "Have a nice day", xml.elements["//content"].text
191
+
192
+ assert_equal "1", xml.elements["//id"].text
193
+ assert_equal "integer" , xml.elements["//id"].attributes['type']
194
+
195
+ assert_equal "1", xml.elements["//replies-count"].text
196
+ assert_equal "integer" , xml.elements["//replies-count"].attributes['type']
197
+
198
+ assert_equal written_on_in_current_timezone, xml.elements["//written-on"].text
199
+ assert_equal "datetime" , xml.elements["//written-on"].attributes['type']
200
+
201
+ assert_equal "david@loudthinking.com", xml.elements["//author-email-address"].text
202
+
203
+ assert_equal nil, xml.elements["//parent-id"].text
204
+ assert_equal "integer", xml.elements["//parent-id"].attributes['type']
205
+ assert_equal "true", xml.elements["//parent-id"].attributes['nil']
206
+
207
+ if current_adapter?(:SybaseAdapter)
208
+ assert_equal last_read_in_current_timezone, xml.elements["//last-read"].text
209
+ assert_equal "datetime" , xml.elements["//last-read"].attributes['type']
210
+ else
211
+ # Oracle enhanced adapter allows to define Date attributes in model class (see topic.rb)
212
+ assert_equal "2004-04-15", xml.elements["//last-read"].text
213
+ assert_equal "date" , xml.elements["//last-read"].attributes['type']
214
+ end
215
+
216
+ # Oracle and DB2 don't have true boolean or time-only fields
217
+ unless current_adapter?(:OracleAdapter, :DB2Adapter)
218
+ assert_equal "false", xml.elements["//approved"].text
219
+ assert_equal "boolean" , xml.elements["//approved"].attributes['type']
220
+
221
+ assert_equal bonus_time_in_current_timezone, xml.elements["//bonus-time"].text
222
+ assert_equal "datetime" , xml.elements["//bonus-time"].attributes['type']
223
+ end
224
+ end
225
+
226
+ def test_except_option
227
+ xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :replies_count])
228
+ assert_equal "<topic>", xml.first(7)
229
+ assert !xml.include?(%(<title>The First Topic</title>))
230
+ assert xml.include?(%(<author-name>David</author-name>))
231
+
232
+ xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :author_name, :replies_count])
233
+ assert !xml.include?(%(<title>The First Topic</title>))
234
+ assert !xml.include?(%(<author-name>David</author-name>))
235
+ end
236
+
237
+ # to_xml used to mess with the hash the user provided which
238
+ # caused the builder to be reused. This meant the document kept
239
+ # getting appended to.
240
+
241
+ def test_modules
242
+ projects = MyApplication::Business::Project.all
243
+ xml = projects.to_xml
244
+ root = projects.first.class.to_s.underscore.pluralize.tr('/','_').dasherize
245
+ assert_match "<#{root} type=\"array\">", xml
246
+ assert_match "</#{root}>", xml
247
+ end
248
+
249
+ def test_passing_hash_shouldnt_reuse_builder
250
+ options = {:include=>:posts}
251
+ david = authors(:david)
252
+ first_xml_size = david.to_xml(options).size
253
+ second_xml_size = david.to_xml(options).size
254
+ assert_equal first_xml_size, second_xml_size
255
+ end
256
+
257
+ def test_include_uses_association_name
258
+ xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0
259
+ assert_match %r{<hello-posts type="array">}, xml
260
+ assert_match %r{<hello-post type="Post">}, xml
261
+ assert_match %r{<hello-post type="StiPost">}, xml
262
+ end
263
+
264
+ def test_included_associations_should_skip_types
265
+ xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0, :skip_types => true
266
+ assert_match %r{<hello-posts>}, xml
267
+ assert_match %r{<hello-post>}, xml
268
+ assert_match %r{<hello-post>}, xml
269
+ end
270
+
271
+ def test_including_has_many_association
272
+ xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies, :except => :replies_count)
273
+ assert_equal "<topic>", xml.first(7)
274
+ assert xml.include?(%(<replies type="array"><reply>))
275
+ assert xml.include?(%(<title>The Second Topic of the day</title>))
276
+ end
277
+
278
+ def test_including_belongs_to_association
279
+ xml = companies(:first_client).to_xml(:indent => 0, :skip_instruct => true, :include => :firm)
280
+ assert !xml.include?("<firm>")
281
+
282
+ xml = companies(:second_client).to_xml(:indent => 0, :skip_instruct => true, :include => :firm)
283
+ assert xml.include?("<firm>")
284
+ end
285
+
286
+ def test_including_multiple_associations
287
+ xml = companies(:first_firm).to_xml(:indent => 0, :skip_instruct => true, :include => [ :clients, :account ])
288
+ assert_equal "<firm>", xml.first(6)
289
+ assert xml.include?(%(<account>))
290
+ assert xml.include?(%(<clients type="array"><client>))
291
+ end
292
+
293
+ def test_including_association_with_options
294
+ xml = companies(:first_firm).to_xml(
295
+ :indent => 0, :skip_instruct => true,
296
+ :include => { :clients => { :only => :name } }
297
+ )
298
+
299
+ assert_equal "<firm>", xml.first(6)
300
+ assert xml.include?(%(<client><name>Summit</name></client>))
301
+ assert xml.include?(%(<clients type="array"><client>))
302
+ end
303
+
304
+ def test_methods_are_called_on_object
305
+ xml = authors(:david).to_xml :methods => :label, :indent => 0
306
+ assert_match %r{<label>.*</label>}, xml
307
+ end
308
+
309
+ def test_should_not_call_methods_on_associations_that_dont_respond
310
+ xml = authors(:david).to_xml :include=>:hello_posts, :methods => :label, :indent => 2
311
+ assert !authors(:david).hello_posts.first.respond_to?(:label)
312
+ assert_match %r{^ <label>.*</label>}, xml
313
+ assert_no_match %r{^ <label>}, xml
314
+ end
315
+
316
+ def test_procs_are_called_on_object
317
+ proc = Proc.new { |options| options[:builder].tag!('nationality', 'Danish') }
318
+ xml = authors(:david).to_xml(:procs => [ proc ])
319
+ assert_match %r{<nationality>Danish</nationality>}, xml
320
+ end
321
+
322
+ def test_dual_arity_procs_are_called_on_object
323
+ proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) }
324
+ xml = authors(:david).to_xml(:procs => [ proc ])
325
+ assert_match %r{<name-reverse>divaD</name-reverse>}, xml
326
+ end
327
+
328
+ def test_top_level_procs_arent_applied_to_associations
329
+ author_proc = Proc.new { |options| options[:builder].tag!('nationality', 'Danish') }
330
+ xml = authors(:david).to_xml(:procs => [ author_proc ], :include => :posts, :indent => 2)
331
+
332
+ assert_match %r{^ <nationality>Danish</nationality>}, xml
333
+ assert_no_match %r{^ {6}<nationality>Danish</nationality>}, xml
334
+ end
335
+
336
+ def test_procs_on_included_associations_are_called
337
+ posts_proc = Proc.new { |options| options[:builder].tag!('copyright', 'DHH') }
338
+ xml = authors(:david).to_xml(
339
+ :indent => 2,
340
+ :include => {
341
+ :posts => { :procs => [ posts_proc ] }
342
+ }
343
+ )
344
+
345
+ assert_no_match %r{^ <copyright>DHH</copyright>}, xml
346
+ assert_match %r{^ {6}<copyright>DHH</copyright>}, xml
347
+ end
348
+
349
+ def test_should_include_empty_has_many_as_empty_array
350
+ authors(:david).posts.delete_all
351
+ xml = authors(:david).to_xml :include=>:posts, :indent => 2
352
+
353
+ assert_equal [], Hash.from_xml(xml)['author']['posts']
354
+ assert_match %r{^ <posts type="array"/>}, xml
355
+ end
356
+
357
+ def test_should_has_many_array_elements_should_include_type_when_different_from_guessed_value
358
+ xml = authors(:david).to_xml :include=>:posts_with_comments, :indent => 2
359
+
360
+ assert Hash.from_xml(xml)
361
+ assert_match %r{^ <posts-with-comments type="array">}, xml
362
+ assert_match %r{^ <posts-with-comment type="Post">}, xml
363
+ assert_match %r{^ <posts-with-comment type="StiPost">}, xml
364
+
365
+ types = Hash.from_xml(xml)['author']['posts_with_comments'].collect {|t| t['type'] }
366
+ assert types.include?('SpecialPost')
367
+ assert types.include?('Post')
368
+ assert types.include?('StiPost')
369
+ end
370
+
371
+ def test_should_produce_xml_for_methods_returning_array
372
+ xml = authors(:david).to_xml(:methods => :social)
373
+ array = Hash.from_xml(xml)['author']['social']
374
+ assert_equal 2, array.size
375
+ assert array.include? 'twitter'
376
+ assert array.include? 'github'
377
+ end
378
+
379
+ def test_should_support_aliased_attributes
380
+ xml = Author.select("name as firstname").to_xml
381
+ array = Hash.from_xml(xml)['authors']
382
+ assert_equal array.size, array.select { |author| author.has_key? 'firstname' }.size
383
+ end
384
+
385
+ def test_array_to_xml_including_has_many_association
386
+ xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :include => :replies)
387
+ assert xml.include?(%(<replies type="array"><reply>))
388
+ end
389
+
390
+ def test_array_to_xml_including_methods
391
+ xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :methods => [ :topic_id ])
392
+ assert xml.include?(%(<topic-id type="integer">#{topics(:first).topic_id}</topic-id>)), xml
393
+ assert xml.include?(%(<topic-id type="integer">#{topics(:second).topic_id}</topic-id>)), xml
394
+ end
395
+
396
+ def test_array_to_xml_including_has_one_association
397
+ xml = [ companies(:first_firm), companies(:rails_core) ].to_xml(:indent => 0, :skip_instruct => true, :include => :account)
398
+ assert xml.include?(companies(:first_firm).account.to_xml(:indent => 0, :skip_instruct => true))
399
+ assert xml.include?(companies(:rails_core).account.to_xml(:indent => 0, :skip_instruct => true))
400
+ end
401
+
402
+ def test_array_to_xml_including_belongs_to_association
403
+ xml = [ companies(:first_client), companies(:second_client), companies(:another_client) ].to_xml(:indent => 0, :skip_instruct => true, :include => :firm)
404
+ assert xml.include?(companies(:first_client).to_xml(:indent => 0, :skip_instruct => true))
405
+ assert xml.include?(companies(:second_client).firm.to_xml(:indent => 0, :skip_instruct => true))
406
+ assert xml.include?(companies(:another_client).firm.to_xml(:indent => 0, :skip_instruct => true))
407
+ end
408
+ end