activerecord 2.2.3 → 2.3.2

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 (120) hide show
  1. data/CHANGELOG +438 -396
  2. data/Rakefile +4 -2
  3. data/lib/active_record.rb +46 -43
  4. data/lib/active_record/association_preload.rb +34 -19
  5. data/lib/active_record/associations.rb +193 -251
  6. data/lib/active_record/associations/association_collection.rb +38 -21
  7. data/lib/active_record/associations/association_proxy.rb +11 -4
  8. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +2 -2
  9. data/lib/active_record/associations/has_many_association.rb +2 -2
  10. data/lib/active_record/associations/has_many_through_association.rb +8 -8
  11. data/lib/active_record/associations/has_one_association.rb +11 -2
  12. data/lib/active_record/attribute_methods.rb +1 -0
  13. data/lib/active_record/autosave_association.rb +349 -0
  14. data/lib/active_record/base.rb +292 -106
  15. data/lib/active_record/batches.rb +73 -0
  16. data/lib/active_record/calculations.rb +34 -16
  17. data/lib/active_record/callbacks.rb +37 -8
  18. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +16 -0
  19. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +3 -0
  20. data/lib/active_record/connection_adapters/abstract/database_statements.rb +103 -15
  21. data/lib/active_record/connection_adapters/abstract/query_cache.rb +6 -6
  22. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +28 -25
  23. data/lib/active_record/connection_adapters/abstract_adapter.rb +29 -5
  24. data/lib/active_record/connection_adapters/mysql_adapter.rb +50 -21
  25. data/lib/active_record/connection_adapters/postgresql_adapter.rb +26 -41
  26. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
  27. data/lib/active_record/connection_adapters/sqlite_adapter.rb +41 -21
  28. data/lib/active_record/dirty.rb +1 -1
  29. data/lib/active_record/dynamic_scope_match.rb +25 -0
  30. data/lib/active_record/fixtures.rb +193 -198
  31. data/lib/active_record/locale/en.yml +1 -1
  32. data/lib/active_record/locking/optimistic.rb +33 -0
  33. data/lib/active_record/migration.rb +8 -2
  34. data/lib/active_record/named_scope.rb +13 -6
  35. data/lib/active_record/nested_attributes.rb +329 -0
  36. data/lib/active_record/query_cache.rb +25 -13
  37. data/lib/active_record/reflection.rb +6 -1
  38. data/lib/active_record/schema_dumper.rb +2 -0
  39. data/lib/active_record/serialization.rb +3 -1
  40. data/lib/active_record/serializers/json_serializer.rb +19 -0
  41. data/lib/active_record/serializers/xml_serializer.rb +28 -13
  42. data/lib/active_record/session_store.rb +318 -0
  43. data/lib/active_record/test_case.rb +15 -9
  44. data/lib/active_record/timestamp.rb +2 -2
  45. data/lib/active_record/transactions.rb +58 -8
  46. data/lib/active_record/validations.rb +29 -24
  47. data/lib/active_record/version.rb +2 -2
  48. data/test/cases/ar_schema_test.rb +0 -1
  49. data/test/cases/associations/belongs_to_associations_test.rb +35 -131
  50. data/test/cases/associations/cascaded_eager_loading_test.rb +8 -0
  51. data/test/cases/associations/eager_load_nested_include_test.rb +29 -0
  52. data/test/cases/associations/eager_test.rb +137 -7
  53. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +45 -7
  54. data/test/cases/associations/has_many_associations_test.rb +110 -149
  55. data/test/cases/associations/has_many_through_associations_test.rb +39 -7
  56. data/test/cases/associations/has_one_associations_test.rb +39 -92
  57. data/test/cases/associations/has_one_through_associations_test.rb +34 -3
  58. data/test/cases/associations/inner_join_association_test.rb +0 -5
  59. data/test/cases/associations/join_model_test.rb +5 -7
  60. data/test/cases/attribute_methods_test.rb +13 -1
  61. data/test/cases/autosave_association_test.rb +901 -0
  62. data/test/cases/base_test.rb +41 -21
  63. data/test/cases/batches_test.rb +61 -0
  64. data/test/cases/calculations_test.rb +37 -17
  65. data/test/cases/callbacks_test.rb +43 -5
  66. data/test/cases/connection_pool_test.rb +25 -0
  67. data/test/cases/copy_table_test_sqlite.rb +11 -0
  68. data/test/cases/datatype_test_postgresql.rb +1 -0
  69. data/test/cases/defaults_test.rb +37 -26
  70. data/test/cases/dirty_test.rb +26 -2
  71. data/test/cases/finder_test.rb +79 -44
  72. data/test/cases/fixtures_test.rb +15 -19
  73. data/test/cases/helper.rb +26 -19
  74. data/test/cases/inheritance_test.rb +2 -2
  75. data/test/cases/json_serialization_test.rb +1 -1
  76. data/test/cases/locking_test.rb +23 -5
  77. data/test/cases/method_scoping_test.rb +126 -3
  78. data/test/cases/migration_test.rb +253 -237
  79. data/test/cases/named_scope_test.rb +73 -3
  80. data/test/cases/nested_attributes_test.rb +509 -0
  81. data/test/cases/query_cache_test.rb +0 -4
  82. data/test/cases/reflection_test.rb +13 -3
  83. data/test/cases/reload_models_test.rb +3 -1
  84. data/test/cases/repair_helper.rb +50 -0
  85. data/test/cases/schema_dumper_test.rb +0 -1
  86. data/test/cases/transactions_test.rb +177 -12
  87. data/test/cases/validations_i18n_test.rb +288 -294
  88. data/test/cases/validations_test.rb +230 -180
  89. data/test/cases/xml_serialization_test.rb +19 -1
  90. data/test/fixtures/fixture_database.sqlite3 +0 -0
  91. data/test/fixtures/fixture_database_2.sqlite3 +0 -0
  92. data/test/fixtures/member_types.yml +6 -0
  93. data/test/fixtures/members.yml +3 -1
  94. data/test/fixtures/people.yml +10 -1
  95. data/test/fixtures/toys.yml +4 -0
  96. data/test/models/author.rb +1 -2
  97. data/test/models/bird.rb +3 -0
  98. data/test/models/category.rb +1 -0
  99. data/test/models/company.rb +3 -0
  100. data/test/models/developer.rb +12 -0
  101. data/test/models/event.rb +3 -0
  102. data/test/models/member.rb +1 -0
  103. data/test/models/member_detail.rb +1 -0
  104. data/test/models/member_type.rb +3 -0
  105. data/test/models/owner.rb +2 -1
  106. data/test/models/parrot.rb +2 -0
  107. data/test/models/person.rb +6 -0
  108. data/test/models/pet.rb +2 -1
  109. data/test/models/pirate.rb +55 -1
  110. data/test/models/post.rb +6 -0
  111. data/test/models/project.rb +1 -0
  112. data/test/models/reply.rb +6 -0
  113. data/test/models/ship.rb +8 -1
  114. data/test/models/ship_part.rb +5 -0
  115. data/test/models/topic.rb +13 -1
  116. data/test/models/toy.rb +4 -0
  117. data/test/schema/schema.rb +35 -2
  118. metadata +70 -9
  119. data/test/fixtures/fixture_database.sqlite +0 -0
  120. data/test/fixtures/fixture_database_2.sqlite +0 -0
@@ -21,6 +21,10 @@ private
21
21
  end
22
22
  end
23
23
 
24
+ class NumericData < ActiveRecord::Base
25
+ self.table_name = 'numeric_data'
26
+ end
27
+
24
28
  class DirtyTest < ActiveRecord::TestCase
25
29
  def test_attribute_changes
26
30
  # New record - no changes.
@@ -68,6 +72,26 @@ class DirtyTest < ActiveRecord::TestCase
68
72
  end
69
73
  end
70
74
 
75
+ def test_nullable_decimal_not_marked_as_changed_if_new_value_is_blank
76
+ numeric_data = NumericData.new
77
+
78
+ ["", nil].each do |value|
79
+ numeric_data.bank_balance = value
80
+ assert !numeric_data.bank_balance_changed?
81
+ assert_nil numeric_data.bank_balance_change
82
+ end
83
+ end
84
+
85
+ def test_nullable_float_not_marked_as_changed_if_new_value_is_blank
86
+ numeric_data = NumericData.new
87
+
88
+ ["", nil].each do |value|
89
+ numeric_data.temperature = value
90
+ assert !numeric_data.temperature_changed?
91
+ assert_nil numeric_data.temperature_change
92
+ end
93
+ end
94
+
71
95
  def test_nullable_integer_zero_to_string_zero_not_marked_as_changed
72
96
  pirate = Pirate.new
73
97
  pirate.parrot_id = 0
@@ -142,7 +166,7 @@ class DirtyTest < ActiveRecord::TestCase
142
166
 
143
167
  def test_association_assignment_changes_foreign_key
144
168
  pirate = Pirate.create!(:catchphrase => 'jarl')
145
- pirate.parrot = Parrot.create!
169
+ pirate.parrot = Parrot.create!(:name => 'Lorre')
146
170
  assert pirate.changed?
147
171
  assert_equal %w(parrot_id), pirate.changed
148
172
  end
@@ -204,7 +228,7 @@ class DirtyTest < ActiveRecord::TestCase
204
228
 
205
229
  pirate = Pirate.new
206
230
  pirate.parrot_id = 1
207
- assert_raises(ActiveRecord::RecordInvalid) { pirate.save! }
231
+ assert_raise(ActiveRecord::RecordInvalid) { pirate.save! }
208
232
  check_pirate_after_save_failure(pirate)
209
233
  end
210
234
 
@@ -94,7 +94,16 @@ class FinderTest < ActiveRecord::TestCase
94
94
 
95
95
  assert_raise(NoMethodError) { Topic.exists?([1,2]) }
96
96
  end
97
-
97
+
98
+ def test_exists_returns_true_with_one_record_and_no_args
99
+ assert Topic.exists?
100
+ end
101
+
102
+ def test_does_not_exist_with_empty_table_and_no_args_given
103
+ Topic.delete_all
104
+ assert !Topic.exists?
105
+ end
106
+
98
107
  def test_exists_with_aggregate_having_three_mappings
99
108
  existing_address = customers(:david).address
100
109
  assert Customer.exists?(:address => existing_address)
@@ -137,7 +146,7 @@ class FinderTest < ActiveRecord::TestCase
137
146
  end
138
147
 
139
148
  def test_find_by_ids_missing_one
140
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, 2, 45) }
149
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, 2, 45) }
141
150
  end
142
151
 
143
152
  def test_find_all_with_limit
@@ -175,6 +184,20 @@ class FinderTest < ActiveRecord::TestCase
175
184
  assert_equal 4, developers.map(&:salary).uniq.size
176
185
  end
177
186
 
187
+ def test_find_with_group_and_having
188
+ developers = Developer.find(:all, :group => "salary", :having => "sum(salary) > 10000", :select => "salary")
189
+ assert_equal 3, developers.size
190
+ assert_equal 3, developers.map(&:salary).uniq.size
191
+ assert developers.all? { |developer| developer.salary > 10000 }
192
+ end
193
+
194
+ def test_find_with_group_and_sanitized_having
195
+ developers = Developer.find(:all, :group => "salary", :having => ["sum(salary) > ?", 10000], :select => "salary")
196
+ assert_equal 3, developers.size
197
+ assert_equal 3, developers.map(&:salary).uniq.size
198
+ assert developers.all? { |developer| developer.salary > 10000 }
199
+ end
200
+
178
201
  def test_find_with_entire_select_statement
179
202
  topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
180
203
 
@@ -213,7 +236,7 @@ class FinderTest < ActiveRecord::TestCase
213
236
  end
214
237
 
215
238
  def test_unexisting_record_exception_handling
216
- assert_raises(ActiveRecord::RecordNotFound) {
239
+ assert_raise(ActiveRecord::RecordNotFound) {
217
240
  Topic.find(1).parent
218
241
  }
219
242
 
@@ -222,7 +245,7 @@ class FinderTest < ActiveRecord::TestCase
222
245
 
223
246
  def test_find_only_some_columns
224
247
  topic = Topic.find(1, :select => "author_name")
225
- assert_raises(ActiveRecord::MissingAttributeError) {topic.title}
248
+ assert_raise(ActiveRecord::MissingAttributeError) {topic.title}
226
249
  assert_equal "David", topic.author_name
227
250
  assert !topic.attribute_present?("title")
228
251
  #assert !topic.respond_to?("title")
@@ -244,22 +267,22 @@ class FinderTest < ActiveRecord::TestCase
244
267
 
245
268
  def test_find_on_array_conditions
246
269
  assert Topic.find(1, :conditions => ["approved = ?", false])
247
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => ["approved = ?", true]) }
270
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => ["approved = ?", true]) }
248
271
  end
249
272
 
250
273
  def test_find_on_hash_conditions
251
274
  assert Topic.find(1, :conditions => { :approved => false })
252
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :approved => true }) }
275
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :approved => true }) }
253
276
  end
254
277
 
255
278
  def test_find_on_hash_conditions_with_explicit_table_name
256
279
  assert Topic.find(1, :conditions => { 'topics.approved' => false })
257
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { 'topics.approved' => true }) }
280
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { 'topics.approved' => true }) }
258
281
  end
259
282
 
260
283
  def test_find_on_hash_conditions_with_hashed_table_name
261
284
  assert Topic.find(1, :conditions => {:topics => { :approved => false }})
262
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => {:topics => { :approved => true }}) }
285
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => {:topics => { :approved => true }}) }
263
286
  end
264
287
 
265
288
  def test_find_with_hash_conditions_on_joined_table
@@ -277,7 +300,7 @@ class FinderTest < ActiveRecord::TestCase
277
300
  def test_find_on_hash_conditions_with_explicit_table_name_and_aggregate
278
301
  david = customers(:david)
279
302
  assert Customer.find(david.id, :conditions => { 'customers.name' => david.name, :address => david.address })
280
- assert_raises(ActiveRecord::RecordNotFound) {
303
+ assert_raise(ActiveRecord::RecordNotFound) {
281
304
  Customer.find(david.id, :conditions => { 'customers.name' => david.name + "1", :address => david.address })
282
305
  }
283
306
  end
@@ -288,7 +311,13 @@ class FinderTest < ActiveRecord::TestCase
288
311
 
289
312
  def test_find_on_hash_conditions_with_range
290
313
  assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1..2 }).map(&:id).sort
291
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) }
314
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) }
315
+ end
316
+
317
+ def test_find_on_hash_conditions_with_end_exclusive_range
318
+ assert_equal [1,2,3], Topic.find(:all, :conditions => { :id => 1..3 }).map(&:id).sort
319
+ assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1...3 }).map(&:id).sort
320
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(3, :conditions => { :id => 2...3 }) }
292
321
  end
293
322
 
294
323
  def test_find_on_hash_conditions_with_multiple_ranges
@@ -298,9 +327,9 @@ class FinderTest < ActiveRecord::TestCase
298
327
 
299
328
  def test_find_on_multiple_hash_conditions
300
329
  assert Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => false })
301
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
302
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "HHC", :replies_count => 1, :approved => false }) }
303
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
330
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
331
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "HHC", :replies_count => 1, :approved => false }) }
332
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
304
333
  end
305
334
 
306
335
  def test_condition_interpolation
@@ -324,7 +353,7 @@ class FinderTest < ActiveRecord::TestCase
324
353
  end
325
354
 
326
355
  def test_hash_condition_find_malformed
327
- assert_raises(ActiveRecord::StatementInvalid) {
356
+ assert_raise(ActiveRecord::StatementInvalid) {
328
357
  Company.find(:first, :conditions => { :id => 2, :dhh => true })
329
358
  }
330
359
  end
@@ -393,10 +422,10 @@ class FinderTest < ActiveRecord::TestCase
393
422
  assert_nil Company.find(:first, :conditions => ["name = ?", "37signals!"])
394
423
  assert_nil Company.find(:first, :conditions => ["name = ?", "37signals!' OR 1=1"])
395
424
  assert_kind_of Time, Topic.find(:first, :conditions => ["id = ?", 1]).written_on
396
- assert_raises(ActiveRecord::PreparedStatementInvalid) {
425
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
397
426
  Company.find(:first, :conditions => ["id=? AND name = ?", 2])
398
427
  }
399
- assert_raises(ActiveRecord::PreparedStatementInvalid) {
428
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
400
429
  Company.find(:first, :conditions => ["id=?", 2, 3, 4])
401
430
  }
402
431
  end
@@ -413,11 +442,11 @@ class FinderTest < ActiveRecord::TestCase
413
442
 
414
443
  def test_bind_arity
415
444
  assert_nothing_raised { bind '' }
416
- assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '', 1 }
445
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '', 1 }
417
446
 
418
- assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?' }
447
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '?' }
419
448
  assert_nothing_raised { bind '?', 1 }
420
- assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 }
449
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 }
421
450
  end
422
451
 
423
452
  def test_named_bind_variables
@@ -500,21 +529,19 @@ class FinderTest < ActiveRecord::TestCase
500
529
  assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
501
530
  end
502
531
 
503
- uses_mocha('test_dynamic_finder_should_go_through_the_find_class_method') do
504
- def test_dynamic_finders_should_go_through_the_find_class_method
505
- Topic.expects(:find).with(:first, :conditions => { :title => 'The First Topic!' })
506
- Topic.find_by_title("The First Topic!")
532
+ def test_dynamic_finders_should_go_through_the_find_class_method
533
+ Topic.expects(:find).with(:first, :conditions => { :title => 'The First Topic!' })
534
+ Topic.find_by_title("The First Topic!")
507
535
 
508
- Topic.expects(:find).with(:last, :conditions => { :title => 'The Last Topic!' })
509
- Topic.find_last_by_title("The Last Topic!")
536
+ Topic.expects(:find).with(:last, :conditions => { :title => 'The Last Topic!' })
537
+ Topic.find_last_by_title("The Last Topic!")
510
538
 
511
- Topic.expects(:find).with(:all, :conditions => { :title => 'A Topic.' })
512
- Topic.find_all_by_title("A Topic.")
539
+ Topic.expects(:find).with(:all, :conditions => { :title => 'A Topic.' })
540
+ Topic.find_all_by_title("A Topic.")
513
541
 
514
- Topic.expects(:find).with(:first, :conditions => { :title => 'Does not exist yet for sure!' }).times(2)
515
- Topic.find_or_initialize_by_title('Does not exist yet for sure!')
516
- Topic.find_or_create_by_title('Does not exist yet for sure!')
517
- end
542
+ Topic.expects(:find).with(:first, :conditions => { :title => 'Does not exist yet for sure!' }).times(2)
543
+ Topic.find_or_initialize_by_title('Does not exist yet for sure!')
544
+ Topic.find_or_create_by_title('Does not exist yet for sure!')
518
545
  end
519
546
 
520
547
  def test_find_by_one_attribute
@@ -524,7 +551,7 @@ class FinderTest < ActiveRecord::TestCase
524
551
 
525
552
  def test_find_by_one_attribute_bang
526
553
  assert_equal topics(:first), Topic.find_by_title!("The First Topic")
527
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
554
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
528
555
  end
529
556
 
530
557
  def test_find_by_one_attribute_caches_dynamic_finder
@@ -605,14 +632,14 @@ class FinderTest < ActiveRecord::TestCase
605
632
  end
606
633
 
607
634
  def test_find_by_one_missing_attribute
608
- assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
635
+ assert_raise(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
609
636
  end
610
637
 
611
638
  def test_find_by_invalid_method_syntax
612
- assert_raises(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
613
- assert_raises(NoMethodError) { Topic.find_by_title?("The First Topic") }
614
- assert_raises(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
615
- assert_raises(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
639
+ assert_raise(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
640
+ assert_raise(NoMethodError) { Topic.find_by_title?("The First Topic") }
641
+ assert_raise(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
642
+ assert_raise(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
616
643
  end
617
644
 
618
645
  def test_find_by_two_attributes
@@ -634,8 +661,8 @@ class FinderTest < ActiveRecord::TestCase
634
661
  end
635
662
 
636
663
  def test_find_last_by_invalid_method_syntax
637
- assert_raises(NoMethodError) { Topic.fail_to_find_last_by_title("The First Topic") }
638
- assert_raises(NoMethodError) { Topic.find_last_by_title?("The First Topic") }
664
+ assert_raise(NoMethodError) { Topic.fail_to_find_last_by_title("The First Topic") }
665
+ assert_raise(NoMethodError) { Topic.find_last_by_title?("The First Topic") }
639
666
  end
640
667
 
641
668
  def test_find_last_by_one_attribute_with_several_options
@@ -643,7 +670,7 @@ class FinderTest < ActiveRecord::TestCase
643
670
  end
644
671
 
645
672
  def test_find_last_by_one_missing_attribute
646
- assert_raises(NoMethodError) { Topic.find_last_by_undertitle("The Last Topic!") }
673
+ assert_raise(NoMethodError) { Topic.find_last_by_undertitle("The Last Topic!") }
647
674
  end
648
675
 
649
676
  def test_find_last_by_two_attributes
@@ -896,16 +923,16 @@ class FinderTest < ActiveRecord::TestCase
896
923
  end
897
924
 
898
925
  def test_find_with_bad_sql
899
- assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
926
+ assert_raise(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
900
927
  end
901
928
 
902
929
  def test_find_with_invalid_params
903
- assert_raises(ArgumentError) { Topic.find :first, :join => "It should be `joins'" }
904
- assert_raises(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
930
+ assert_raise(ArgumentError) { Topic.find :first, :join => "It should be `joins'" }
931
+ assert_raise(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
905
932
  end
906
933
 
907
934
  def test_dynamic_finder_with_invalid_params
908
- assert_raises(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
935
+ assert_raise(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
909
936
  end
910
937
 
911
938
  def test_find_all_with_limit
@@ -1037,6 +1064,14 @@ class FinderTest < ActiveRecord::TestCase
1037
1064
  assert_equal [0, 1, 1], posts.map(&:author_id).sort
1038
1065
  end
1039
1066
 
1067
+ def test_finder_with_scoped_from
1068
+ all_topics = Topic.all
1069
+
1070
+ Topic.with_scope(:find => { :from => 'fake_topics' }) do
1071
+ assert_equal all_topics, Topic.all(:from => 'topics')
1072
+ end
1073
+ end
1074
+
1040
1075
  protected
1041
1076
  def bind(statement, *vars)
1042
1077
  if vars.first.is_a?(Hash)
@@ -151,7 +151,7 @@ class FixturesTest < ActiveRecord::TestCase
151
151
  end
152
152
 
153
153
  def test_dirty_dirty_yaml_file
154
- assert_raises(Fixture::FormatError) do
154
+ assert_raise(Fixture::FormatError) do
155
155
  Fixtures.new( Account.connection, "courses", 'Course', FIXTURES_ROOT + "/naked/yml/courses")
156
156
  end
157
157
  end
@@ -265,12 +265,10 @@ class FixturesWithoutInstantiationTest < ActiveRecord::TestCase
265
265
  assert_raise(StandardError) { topics([:first, :second]) }
266
266
  end
267
267
 
268
- uses_mocha 'reloading_fixtures_through_accessor_methods' do
269
- def test_reloading_fixtures_through_accessor_methods
270
- assert_equal "The First Topic", topics(:first).title
271
- @loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
272
- assert_equal "Fresh Topic!", topics(:first, true).title
273
- end
268
+ def test_reloading_fixtures_through_accessor_methods
269
+ assert_equal "The First Topic", topics(:first).title
270
+ @loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
271
+ assert_equal "Fresh Topic!", topics(:first, true).title
274
272
  end
275
273
  end
276
274
 
@@ -422,7 +420,7 @@ class InvalidTableNameFixturesTest < ActiveRecord::TestCase
422
420
  self.use_transactional_fixtures = false
423
421
 
424
422
  def test_raises_error
425
- assert_raises FixtureClassNotFound do
423
+ assert_raise FixtureClassNotFound do
426
424
  funny_jokes(:a_joke)
427
425
  end
428
426
  end
@@ -639,17 +637,15 @@ class ActiveSupportSubclassWithFixturesTest < ActiveRecord::TestCase
639
637
  end
640
638
 
641
639
  class FixtureLoadingTest < ActiveRecord::TestCase
642
- uses_mocha 'reloading_fixtures_through_accessor_methods' do
643
- def test_logs_message_for_failed_dependency_load
644
- Test::Unit::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError)
645
- ActiveRecord::Base.logger.expects(:warn)
646
- Test::Unit::TestCase.try_to_load_dependency(:does_not_exist)
647
- end
640
+ def test_logs_message_for_failed_dependency_load
641
+ ActiveRecord::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError)
642
+ ActiveRecord::Base.logger.expects(:warn)
643
+ ActiveRecord::TestCase.try_to_load_dependency(:does_not_exist)
644
+ end
648
645
 
649
- def test_does_not_logs_message_for_successful_dependency_load
650
- Test::Unit::TestCase.expects(:require_dependency).with(:works_out_fine)
651
- ActiveRecord::Base.logger.expects(:warn).never
652
- Test::Unit::TestCase.try_to_load_dependency(:works_out_fine)
653
- end
646
+ def test_does_not_logs_message_for_successful_dependency_load
647
+ ActiveRecord::TestCase.expects(:require_dependency).with(:works_out_fine)
648
+ ActiveRecord::Base.logger.expects(:warn).never
649
+ ActiveRecord::TestCase.try_to_load_dependency(:works_out_fine)
654
650
  end
655
651
  end
@@ -2,13 +2,19 @@ $:.unshift(File.dirname(__FILE__) + '/../../lib')
2
2
  $:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib')
3
3
 
4
4
  require 'config'
5
+
6
+ require 'rubygems'
5
7
  require 'test/unit'
8
+ gem 'mocha', '>= 0.9.5'
9
+ require 'mocha'
6
10
 
7
11
  require 'active_record'
8
- require 'active_record/fixtures'
9
12
  require 'active_record/test_case'
13
+ require 'active_record/fixtures'
10
14
  require 'connection'
11
15
 
16
+ require 'cases/repair_helper'
17
+
12
18
  # Show backtraces for deprecated behavior for quicker cleanup.
13
19
  ActiveSupport::Deprecation.debug = true
14
20
 
@@ -22,16 +28,8 @@ def current_adapter?(*types)
22
28
  end
23
29
  end
24
30
 
25
- def uses_mocha(description)
26
- require 'rubygems'
27
- require 'mocha'
28
- yield
29
- rescue LoadError
30
- $stderr.puts "Skipping #{description} tests. `gem install mocha` and try again."
31
- end
32
-
33
31
  ActiveRecord::Base.connection.class.class_eval do
34
- IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /SHOW FIELDS/]
32
+ IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /SHOW FIELDS/]
35
33
 
36
34
  def execute_with_query_record(sql, name = nil, &block)
37
35
  $queries_executed ||= []
@@ -48,15 +46,24 @@ class << ActiveRecord::Base
48
46
  end
49
47
 
50
48
  unless ENV['FIXTURE_DEBUG']
51
- module Test #:nodoc:
52
- module Unit #:nodoc:
53
- class << TestCase #:nodoc:
54
- def try_to_load_dependency_with_silence(*args)
55
- ActiveRecord::Base.logger.silence { try_to_load_dependency_without_silence(*args)}
56
- end
57
-
58
- alias_method_chain :try_to_load_dependency, :silence
59
- end
49
+ module ActiveRecord::TestFixtures::ClassMethods
50
+ def try_to_load_dependency_with_silence(*args)
51
+ ActiveRecord::Base.logger.silence { try_to_load_dependency_without_silence(*args)}
60
52
  end
53
+
54
+ alias_method_chain :try_to_load_dependency, :silence
55
+ end
56
+ end
57
+
58
+ class ActiveSupport::TestCase
59
+ include ActiveRecord::TestFixtures
60
+ include ActiveRecord::Testing::RepairHelper
61
+
62
+ self.fixture_path = FIXTURES_ROOT
63
+ self.use_instantiated_fixtures = false
64
+ self.use_transactional_fixtures = true
65
+
66
+ def create_fixtures(*table_names, &block)
67
+ Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block)
61
68
  end
62
69
  end