activerecord 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (106) hide show
  1. data/CHANGELOG +581 -0
  2. data/README +361 -0
  3. data/RUNNING_UNIT_TESTS +36 -0
  4. data/dev-utils/eval_debugger.rb +9 -0
  5. data/examples/associations.png +0 -0
  6. data/examples/associations.rb +87 -0
  7. data/examples/shared_setup.rb +15 -0
  8. data/examples/validation.rb +88 -0
  9. data/install.rb +60 -0
  10. data/lib/active_record.rb +48 -0
  11. data/lib/active_record/aggregations.rb +165 -0
  12. data/lib/active_record/associations.rb +536 -0
  13. data/lib/active_record/associations/association_collection.rb +70 -0
  14. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +46 -0
  15. data/lib/active_record/associations/has_many_association.rb +104 -0
  16. data/lib/active_record/base.rb +985 -0
  17. data/lib/active_record/callbacks.rb +337 -0
  18. data/lib/active_record/connection_adapters/abstract_adapter.rb +326 -0
  19. data/lib/active_record/connection_adapters/mysql_adapter.rb +131 -0
  20. data/lib/active_record/connection_adapters/postgresql_adapter.rb +177 -0
  21. data/lib/active_record/connection_adapters/sqlite_adapter.rb +107 -0
  22. data/lib/active_record/deprecated_associations.rb +70 -0
  23. data/lib/active_record/fixtures.rb +172 -0
  24. data/lib/active_record/observer.rb +71 -0
  25. data/lib/active_record/reflection.rb +126 -0
  26. data/lib/active_record/support/class_attribute_accessors.rb +43 -0
  27. data/lib/active_record/support/class_inheritable_attributes.rb +37 -0
  28. data/lib/active_record/support/clean_logger.rb +10 -0
  29. data/lib/active_record/support/inflector.rb +70 -0
  30. data/lib/active_record/transactions.rb +102 -0
  31. data/lib/active_record/validations.rb +205 -0
  32. data/lib/active_record/vendor/mysql.rb +1117 -0
  33. data/lib/active_record/vendor/simple.rb +702 -0
  34. data/lib/active_record/wrappers/yaml_wrapper.rb +15 -0
  35. data/lib/active_record/wrappings.rb +59 -0
  36. data/rakefile +122 -0
  37. data/test/abstract_unit.rb +16 -0
  38. data/test/aggregations_test.rb +34 -0
  39. data/test/all.sh +8 -0
  40. data/test/associations_test.rb +477 -0
  41. data/test/base_test.rb +513 -0
  42. data/test/class_inheritable_attributes_test.rb +33 -0
  43. data/test/connections/native_mysql/connection.rb +24 -0
  44. data/test/connections/native_postgresql/connection.rb +24 -0
  45. data/test/connections/native_sqlite/connection.rb +24 -0
  46. data/test/deprecated_associations_test.rb +336 -0
  47. data/test/finder_test.rb +67 -0
  48. data/test/fixtures/accounts/signals37 +3 -0
  49. data/test/fixtures/accounts/unknown +2 -0
  50. data/test/fixtures/auto_id.rb +4 -0
  51. data/test/fixtures/column_name.rb +3 -0
  52. data/test/fixtures/companies/first_client +6 -0
  53. data/test/fixtures/companies/first_firm +4 -0
  54. data/test/fixtures/companies/second_client +6 -0
  55. data/test/fixtures/company.rb +37 -0
  56. data/test/fixtures/company_in_module.rb +33 -0
  57. data/test/fixtures/course.rb +3 -0
  58. data/test/fixtures/courses/java +2 -0
  59. data/test/fixtures/courses/ruby +2 -0
  60. data/test/fixtures/customer.rb +30 -0
  61. data/test/fixtures/customers/david +6 -0
  62. data/test/fixtures/db_definitions/mysql.sql +96 -0
  63. data/test/fixtures/db_definitions/mysql2.sql +4 -0
  64. data/test/fixtures/db_definitions/postgresql.sql +113 -0
  65. data/test/fixtures/db_definitions/postgresql2.sql +4 -0
  66. data/test/fixtures/db_definitions/sqlite.sql +85 -0
  67. data/test/fixtures/db_definitions/sqlite2.sql +4 -0
  68. data/test/fixtures/default.rb +2 -0
  69. data/test/fixtures/developer.rb +8 -0
  70. data/test/fixtures/developers/david +2 -0
  71. data/test/fixtures/developers/jamis +2 -0
  72. data/test/fixtures/developers_projects/david_action_controller +2 -0
  73. data/test/fixtures/developers_projects/david_active_record +2 -0
  74. data/test/fixtures/developers_projects/jamis_active_record +2 -0
  75. data/test/fixtures/entrant.rb +3 -0
  76. data/test/fixtures/entrants/first +3 -0
  77. data/test/fixtures/entrants/second +3 -0
  78. data/test/fixtures/entrants/third +3 -0
  79. data/test/fixtures/fixture_database.sqlite +0 -0
  80. data/test/fixtures/fixture_database_2.sqlite +0 -0
  81. data/test/fixtures/movie.rb +5 -0
  82. data/test/fixtures/movies/first +2 -0
  83. data/test/fixtures/movies/second +2 -0
  84. data/test/fixtures/project.rb +3 -0
  85. data/test/fixtures/projects/action_controller +2 -0
  86. data/test/fixtures/projects/active_record +2 -0
  87. data/test/fixtures/reply.rb +21 -0
  88. data/test/fixtures/subscriber.rb +5 -0
  89. data/test/fixtures/subscribers/first +2 -0
  90. data/test/fixtures/subscribers/second +2 -0
  91. data/test/fixtures/topic.rb +20 -0
  92. data/test/fixtures/topics/first +9 -0
  93. data/test/fixtures/topics/second +8 -0
  94. data/test/fixtures_test.rb +20 -0
  95. data/test/inflector_test.rb +104 -0
  96. data/test/inheritance_test.rb +125 -0
  97. data/test/lifecycle_test.rb +110 -0
  98. data/test/modules_test.rb +21 -0
  99. data/test/multiple_db_test.rb +46 -0
  100. data/test/pk_test.rb +57 -0
  101. data/test/reflection_test.rb +78 -0
  102. data/test/thread_safety_test.rb +33 -0
  103. data/test/transactions_test.rb +83 -0
  104. data/test/unconnected_test.rb +24 -0
  105. data/test/validations_test.rb +126 -0
  106. metadata +166 -0
@@ -0,0 +1,513 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/topic'
3
+ require 'fixtures/reply'
4
+ require 'fixtures/company'
5
+ require 'fixtures/default'
6
+ require 'fixtures/auto_id'
7
+ require 'fixtures/column_name'
8
+
9
+ class Category < ActiveRecord::Base; end
10
+ class Smarts < ActiveRecord::Base; end
11
+ class CreditCard < ActiveRecord::Base; end
12
+ class MasterCreditCard < ActiveRecord::Base; end
13
+
14
+ class LoosePerson < ActiveRecord::Base
15
+ attr_protected :credit_rating, :administrator
16
+ end
17
+
18
+ class TightPerson < ActiveRecord::Base
19
+ attr_accessible :name, :address
20
+ end
21
+
22
+ class TightDescendent < TightPerson
23
+ attr_accessible :phone_number
24
+ end
25
+
26
+ class Booleantest < ActiveRecord::Base; end
27
+
28
+ class BasicsTest < Test::Unit::TestCase
29
+ def setup
30
+ @topic_fixtures, @companies = create_fixtures "topics", "companies"
31
+ end
32
+
33
+ def test_set_attributes
34
+ topic = Topic.find(1)
35
+ topic.attributes = { "title" => "Budget", "author_name" => "Jason" }
36
+ topic.save
37
+ assert_equal("Budget", topic.title)
38
+ assert_equal("Jason", topic.author_name)
39
+ assert_equal(@topic_fixtures["first"]["author_email_address"], Topic.find(1).author_email_address)
40
+ end
41
+
42
+ def test_integers_as_nil
43
+ Topic.update(1, "approved" => "")
44
+ assert_nil Topic.find(1).approved
45
+ end
46
+
47
+ def test_set_attributes_with_block
48
+ topic = Topic.new do |t|
49
+ t.title = "Budget"
50
+ t.author_name = "Jason"
51
+ end
52
+
53
+ assert_equal("Budget", topic.title)
54
+ assert_equal("Jason", topic.author_name)
55
+ end
56
+
57
+ def test_respond_to?
58
+ topic = Topic.find(1)
59
+ assert topic.respond_to?("title")
60
+ assert topic.respond_to?("title?")
61
+ assert topic.respond_to?("title=")
62
+ assert topic.respond_to?("author_name")
63
+ assert topic.respond_to?("attribute_names")
64
+ assert !topic.respond_to?("nothingness")
65
+ end
66
+
67
+ def test_array_content
68
+ topic = Topic.new
69
+ topic.content = %w( one two three )
70
+ topic.save
71
+
72
+ assert_equal(%w( one two three ), Topic.find(topic.id).content)
73
+ end
74
+
75
+ def test_hash_content
76
+ topic = Topic.new
77
+ topic.content = { "one" => 1, "two" => 2 }
78
+ topic.save
79
+
80
+ assert_equal 2, Topic.find(topic.id).content["two"]
81
+
82
+ topic.content["three"] = 3
83
+ topic.save
84
+
85
+ assert_equal 3, Topic.find(topic.id).content["three"]
86
+ end
87
+
88
+ def test_update_array_content
89
+ topic = Topic.new
90
+ topic.content = %w( one two three )
91
+
92
+ topic.content.push "four"
93
+ assert_equal(%w( one two three four ), topic.content)
94
+
95
+ topic.save
96
+
97
+ topic = Topic.find(topic.id)
98
+ topic.content << "five"
99
+ assert_equal(%w( one two three four five ), topic.content)
100
+ end
101
+
102
+ def test_create
103
+ topic = Topic.new
104
+ topic.title = "New Topic"
105
+ topic.save
106
+ id = topic.id
107
+ topicReloaded = Topic.find(id)
108
+ assert_equal("New Topic", topicReloaded.title)
109
+ end
110
+
111
+ def test_create_through_factory
112
+ topic = Topic.create("title" => "New Topic")
113
+ topicReloaded = Topic.find(topic.id)
114
+ assert_equal(topic, topicReloaded)
115
+ end
116
+
117
+ def test_update
118
+ topic = Topic.new
119
+ topic.title = "Another New Topic"
120
+ topic.written_on = "2003-12-12 23:23"
121
+ topic.save
122
+ id = topic.id
123
+ assert_equal(id, topic.id)
124
+
125
+ topicReloaded = Topic.find(id)
126
+ assert_equal("Another New Topic", topicReloaded.title)
127
+
128
+ topicReloaded.title = "Updated topic"
129
+ topicReloaded.save
130
+
131
+ topicReloadedAgain = Topic.find(id)
132
+
133
+ assert_equal("Updated topic", topicReloadedAgain.title)
134
+ end
135
+
136
+ def test_preserving_objects
137
+ assert_kind_of(
138
+ Time, Topic.find(1).written_on,
139
+ "The written_on attribute should be of the Time class"
140
+ )
141
+
142
+ assert_kind_of(
143
+ Date, Topic.find(1).last_read,
144
+ "The last_read attribute should be of the Date class"
145
+ )
146
+ end
147
+
148
+ def test_destroy
149
+ topic = Topic.new
150
+ topic.title = "Yet Another New Topic"
151
+ topic.written_on = "2003-12-12 23:23"
152
+ topic.save
153
+ id = topic.id
154
+ topic.destroy
155
+
156
+ assert_raises(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(id) }
157
+ end
158
+
159
+ def test_record_not_found_exception
160
+ assert_raises(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(id) }
161
+ end
162
+
163
+ def test_initialize_with_attributes
164
+ topic = Topic.new({
165
+ "title" => "initialized from attributes", "written_on" => "2003-12-12 23:23"
166
+ })
167
+
168
+ assert_equal("initialized from attributes", topic.title)
169
+ end
170
+
171
+ def test_load
172
+ topics = Topic.find_all nil, "id"
173
+ assert_equal(2, topics.size)
174
+ assert_equal(@topic_fixtures["first"]["title"], topics.first.title)
175
+ end
176
+
177
+ def test_load_with_condition
178
+ topics = Topic.find_all "author_name = 'Mary'"
179
+
180
+ assert_equal(1, topics.size)
181
+ assert_equal(@topic_fixtures["second"]["title"], topics.first.title)
182
+ end
183
+
184
+ def test_table_name_guesses
185
+ assert_equal "topics", Topic.table_name
186
+
187
+ assert_equal "categories", Category.table_name
188
+ assert_equal "smarts", Smarts.table_name
189
+ assert_equal "credit_cards", CreditCard.table_name
190
+ assert_equal "master_credit_cards", MasterCreditCard.table_name
191
+
192
+ ActiveRecord::Base.pluralize_table_names = false
193
+ assert_equal "category", Category.table_name
194
+ assert_equal "smarts", Smarts.table_name
195
+ assert_equal "credit_card", CreditCard.table_name
196
+ assert_equal "master_credit_card", MasterCreditCard.table_name
197
+ ActiveRecord::Base.pluralize_table_names = true
198
+
199
+ ActiveRecord::Base.table_name_prefix = "test_"
200
+ assert_equal "test_categories", Category.table_name
201
+ ActiveRecord::Base.table_name_suffix = "_test"
202
+ assert_equal "test_categories_test", Category.table_name
203
+ ActiveRecord::Base.table_name_prefix = ""
204
+ assert_equal "categories_test", Category.table_name
205
+ ActiveRecord::Base.table_name_suffix = ""
206
+ assert_equal "categories", Category.table_name
207
+
208
+ ActiveRecord::Base.pluralize_table_names = false
209
+ ActiveRecord::Base.table_name_prefix = "test_"
210
+ assert_equal "test_category", Category.table_name
211
+ ActiveRecord::Base.table_name_suffix = "_test"
212
+ assert_equal "test_category_test", Category.table_name
213
+ ActiveRecord::Base.table_name_prefix = ""
214
+ assert_equal "category_test", Category.table_name
215
+ ActiveRecord::Base.table_name_suffix = ""
216
+ assert_equal "category", Category.table_name
217
+ ActiveRecord::Base.pluralize_table_names = true
218
+ end
219
+
220
+ def test_destroy_all
221
+ assert_equal(2, Topic.find_all.size)
222
+
223
+ Topic.destroy_all "author_name = 'Mary'"
224
+ assert_equal(1, Topic.find_all.size)
225
+ end
226
+
227
+ def test_boolean_attributes
228
+ assert ! Topic.find(1).approved?
229
+ assert Topic.find(2).approved?
230
+ end
231
+
232
+ def test_increment_counter
233
+ Topic.increment_counter("replies_count", 1)
234
+ assert_equal 1, Topic.find(1).replies_count
235
+
236
+ Topic.increment_counter("replies_count", 1)
237
+ assert_equal 2, Topic.find(1).replies_count
238
+ end
239
+
240
+ def test_decrement_counter
241
+ Topic.decrement_counter("replies_count", 2)
242
+ assert_equal 1, Topic.find(2).replies_count
243
+
244
+ Topic.decrement_counter("replies_count", 2)
245
+ assert_equal 0, Topic.find(1).replies_count
246
+ end
247
+
248
+ def test_update_all
249
+ Topic.update_all "content = 'bulk updated!'"
250
+ assert_equal "bulk updated!", Topic.find(1).content
251
+ assert_equal "bulk updated!", Topic.find(2).content
252
+ end
253
+
254
+ def test_update_by_condition
255
+ Topic.update_all "content = 'bulk updated!'", "approved = 1"
256
+ assert_equal "Have a nice day", Topic.find(1).content
257
+ assert_equal "bulk updated!", Topic.find(2).content
258
+ end
259
+
260
+ def test_attribute_present
261
+ t = Topic.new
262
+ t.title = "hello there!"
263
+ t.written_on = Time.now
264
+ assert t.attribute_present?("title")
265
+ assert t.attribute_present?("written_on")
266
+ assert !t.attribute_present?("content")
267
+ end
268
+
269
+ def test_attribute_keys_on_new_instance
270
+ t = Topic.new
271
+ assert_equal nil, t.title, "The topics table has a title column, so it should be nil"
272
+ assert_raises(NoMethodError) { t.title2 }
273
+ end
274
+
275
+ def test_class_name
276
+ assert_equal "Firm", ActiveRecord::Base.class_name("firms")
277
+ assert_equal "Category", ActiveRecord::Base.class_name("categories")
278
+ assert_equal "AccountHolder", ActiveRecord::Base.class_name("account_holder")
279
+
280
+ ActiveRecord::Base.pluralize_table_names = false
281
+ assert_equal "Firms", ActiveRecord::Base.class_name( "firms" )
282
+ ActiveRecord::Base.pluralize_table_names = true
283
+
284
+ ActiveRecord::Base.table_name_prefix = "test_"
285
+ assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms" )
286
+ ActiveRecord::Base.table_name_suffix = "_tests"
287
+ assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms_tests" )
288
+ ActiveRecord::Base.table_name_prefix = ""
289
+ assert_equal "Firm", ActiveRecord::Base.class_name( "firms_tests" )
290
+ ActiveRecord::Base.table_name_suffix = ""
291
+ assert_equal "Firm", ActiveRecord::Base.class_name( "firms" )
292
+ end
293
+
294
+ def test_null_fields
295
+ assert_nil Topic.find(1).parent_id
296
+ assert_nil Topic.create("title" => "Hey you").parent_id
297
+ end
298
+
299
+ def test_default_values
300
+ topic = Topic.new
301
+ assert_equal 1, topic.approved
302
+ assert_nil topic.written_on
303
+ assert_nil topic.last_read
304
+
305
+ topic.save
306
+
307
+ topic = Topic.find(topic.id)
308
+ assert_equal 1, topic.approved
309
+ assert_nil topic.last_read
310
+ end
311
+
312
+ def test_default_values_on_empty_strings
313
+ topic = Topic.new
314
+ topic.approved = nil
315
+ topic.last_read = nil
316
+
317
+ topic.save
318
+
319
+ topic = Topic.find(topic.id)
320
+ assert_nil topic.last_read
321
+ assert_nil topic.approved
322
+ end
323
+
324
+ def test_equality
325
+ assert_equal Topic.find(1), Topic.find(2).parent
326
+ end
327
+
328
+ def test_destroy_new_record
329
+ client = Client.new
330
+ client.destroy
331
+ assert client.frozen?
332
+ end
333
+
334
+ def test_update_attribute
335
+ assert !Topic.find(1).approved?
336
+ Topic.find(1).update_attribute("approved", true)
337
+ assert Topic.find(1).approved?
338
+ end
339
+
340
+ def test_mass_assignment_protection
341
+ firm = Firm.new
342
+ firm.attributes = { "name" => "Next Angle", "rating" => 5 }
343
+ assert_equal 1, firm.rating
344
+ end
345
+
346
+ def test_mass_assignment_accessible
347
+ reply = Reply.new("title" => "hello", "content" => "world", "approved" => 0)
348
+ reply.save
349
+
350
+ assert_equal 1, reply.approved
351
+
352
+ reply.approved = 0
353
+ reply.save
354
+
355
+ assert_equal 0, reply.approved
356
+ end
357
+
358
+ def test_mass_assignment_protection_inheritance
359
+ assert_equal [ :credit_rating, :administrator ], LoosePerson.protected_attributes
360
+ assert_nil TightPerson.protected_attributes
361
+ end
362
+
363
+ def test_multiparameter_attributes_on_date
364
+ attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" }
365
+ topic = Topic.find(1)
366
+ topic.attributes = attributes
367
+ assert_equal Date.new(2004, 6, 24).to_s, topic.last_read.to_s
368
+ end
369
+
370
+ def test_multiparameter_attributes_on_date_with_empty_date
371
+ attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "" }
372
+ topic = Topic.find(1)
373
+ topic.attributes = attributes
374
+ assert_equal Date.new(2004, 6, 1).to_s, topic.last_read.to_s
375
+ end
376
+
377
+ def test_multiparameter_attributes_on_date_with_all_empty
378
+ attributes = { "last_read(1i)" => "", "last_read(2i)" => "", "last_read(3i)" => "" }
379
+ topic = Topic.find(1)
380
+ topic.attributes = attributes
381
+ assert_nil topic.last_read
382
+ end
383
+
384
+ def test_multiparameter_attributes_on_time
385
+ attributes = {
386
+ "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
387
+ "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
388
+ }
389
+ topic = Topic.find(1)
390
+ topic.attributes = attributes
391
+ assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
392
+ end
393
+
394
+ def test_multiparameter_attributes_on_time_with_empty_seconds
395
+ attributes = {
396
+ "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
397
+ "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => ""
398
+ }
399
+ topic = Topic.find(1)
400
+ topic.attributes = attributes
401
+ assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
402
+ end
403
+
404
+ def test_boolean
405
+ b_false = Booleantest.create({ "value" => false })
406
+ false_id = b_false.id
407
+ b_true = Booleantest.create({ "value" => true })
408
+ true_id = b_true.id
409
+
410
+ b_false = Booleantest.find(false_id)
411
+ assert !b_false.value?
412
+ b_true = Booleantest.find(true_id)
413
+ assert b_true.value?
414
+ end
415
+
416
+ def test_clone
417
+ topic = Topic.find(1)
418
+ cloned_topic = topic.clone
419
+ assert_equal topic.title, cloned_topic.title
420
+ assert cloned_topic.new_record?
421
+
422
+ # test if the attributes have been cloned
423
+ topic.title = "a"
424
+ cloned_topic.title = "b"
425
+ assert_equal "a", topic.title
426
+ assert_equal "b", cloned_topic.title
427
+
428
+ # test if the attribute values have been cloned
429
+ topic.title = {"a" => "b"}
430
+ cloned_topic = topic.clone
431
+ cloned_topic.title["a"] = "c"
432
+ assert_equal "b", topic.title["a"]
433
+ end
434
+
435
+ def test_bignum
436
+ company = Company.find(1)
437
+ company.rating = 2147483647
438
+ company.save
439
+ company = Company.find(1)
440
+ assert_equal 2147483647, company.rating
441
+ end
442
+
443
+ def test_default
444
+ if Default.connection.class.name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
445
+ default = Default.new
446
+
447
+ # dates / timestampts
448
+ time_format = "%m/%d/%Y %H:%M"
449
+ assert_equal Time.now.strftime(time_format), default.modified_time.strftime(time_format)
450
+ assert_equal Date.today, default.modified_date
451
+
452
+ # fixed dates / times
453
+ assert_equal Date.new(2004, 1, 1), default.fixed_date
454
+ assert_equal Time.local(2004, 1,1,0,0,0,0), default.fixed_time
455
+
456
+ # char types
457
+ assert_equal 'Y', default.char1
458
+ assert_equal 'a varchar field', default.char2
459
+ assert_equal 'a text field', default.char3
460
+ end
461
+ end
462
+
463
+ def test_auto_id
464
+ auto = AutoId.new
465
+ auto.save
466
+ assert (auto.id > 0)
467
+ end
468
+
469
+ def quote_column_name(name)
470
+ "<#{name}>"
471
+ end
472
+
473
+ def test_quote_keys
474
+ ar = AutoId.new
475
+ source = {"foo" => "bar", "baz" => "quux"}
476
+ actual = ar.send(:quote_columns, self, source)
477
+ inverted = actual.invert
478
+ assert_equal("<foo>", inverted["bar"])
479
+ assert_equal("<baz>", inverted["quux"])
480
+ end
481
+
482
+ def test_column_name_properly_quoted
483
+ col_record = ColumnName.new
484
+ col_record.references = 40
485
+ col_record.save
486
+ col_record.references = 41
487
+ col_record.save
488
+ c2 = ColumnName.find(col_record.id)
489
+ assert_equal(41, c2.references)
490
+ end
491
+
492
+ MyObject = Struct.new :attribute1, :attribute2
493
+
494
+ def test_serialized_attribute
495
+ myobj = MyObject.new('value1', 'value2')
496
+ topic = Topic.create("content" => myobj)
497
+ Topic.serialize("content", MyObject)
498
+ assert_equal(myobj, topic.content)
499
+ end
500
+
501
+ def test_serialized_attribute_with_class_constraint
502
+ myobj = MyObject.new('value1', 'value2')
503
+ topic = Topic.create("content" => myobj)
504
+ Topic.serialize(:content, Hash)
505
+
506
+ assert_raises(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content }
507
+
508
+ settings = { "color" => "blue" }
509
+ Topic.find(topic.id).update_attribute("content", settings)
510
+ assert_equal(settings, Topic.find(topic.id).content)
511
+ Topic.serialize(:content)
512
+ end
513
+ end