ibm_db 1.0.2 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -92,6 +92,14 @@ class CalculationsTest < ActiveRecord::TestCase
92
92
  assert_equal 60, c[2]
93
93
  end
94
94
 
95
+ def test_should_group_by_summed_field_having_sanitized_condition
96
+ c = Account.sum(:credit_limit, :group => :firm_id,
97
+ :having => ['sum(credit_limit) > ?', 50])
98
+ assert_nil c[1]
99
+ assert_equal 105, c[6]
100
+ assert_equal 60, c[2]
101
+ end
102
+
95
103
  def test_should_group_by_summed_association
96
104
  c = Account.sum(:credit_limit, :group => :firm)
97
105
  assert_equal 50, c[companies(:first_firm)]
@@ -156,24 +164,23 @@ class CalculationsTest < ActiveRecord::TestCase
156
164
  assert_equal 1, c[companies(:first_client)]
157
165
  end
158
166
 
159
- uses_mocha 'group_by_non_numeric_foreign_key_association' do
160
- def test_should_group_by_association_with_non_numeric_foreign_key
161
- ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}])
167
+ def test_should_group_by_association_with_non_numeric_foreign_key
168
+ ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}])
162
169
 
163
- firm = mock()
164
- firm.expects(:id).returns("ABC")
165
- firm.expects(:class).returns(Firm)
166
- Company.expects(:find).with(["ABC"]).returns([firm])
170
+ firm = mock()
171
+ firm.expects(:id).returns("ABC")
172
+ firm.expects(:class).returns(Firm)
173
+ Company.expects(:find).with(["ABC"]).returns([firm])
167
174
 
168
- column = mock()
169
- column.expects(:name).at_least_once.returns(:firm_id)
170
- column.expects(:type_cast).with("ABC").returns("ABC")
171
- Account.expects(:columns).at_least_once.returns([column])
175
+ column = mock()
176
+ column.expects(:name).at_least_once.returns(:firm_id)
177
+ column.expects(:type_cast).with("ABC").returns("ABC")
178
+ Account.expects(:columns).at_least_once.returns([column])
172
179
 
173
- c = Account.count(:all, :group => :firm)
174
- assert_equal Firm, c.first.first.class
175
- assert_equal 1, c.first.last
176
- end
180
+ c = Account.count(:all, :group => :firm)
181
+ first_key = c.keys.first
182
+ assert_equal Firm, first_key.class
183
+ assert_equal 1, c[first_key]
177
184
  end
178
185
 
179
186
  def test_should_calculate_grouped_association_with_foreign_key_option
@@ -248,8 +255,8 @@ class CalculationsTest < ActiveRecord::TestCase
248
255
  Company.send(:validate_calculation_options, :count, :include => true)
249
256
  end
250
257
 
251
- assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
252
- assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
258
+ assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
259
+ assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
253
260
  end
254
261
 
255
262
  def test_should_count_selected_field_with_include
@@ -257,6 +264,19 @@ class CalculationsTest < ActiveRecord::TestCase
257
264
  assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
258
265
  end
259
266
 
267
+ def test_should_count_scoped_select
268
+ Account.update_all("credit_limit = NULL")
269
+ assert_equal 0, Account.scoped(:select => "credit_limit").count
270
+ end
271
+
272
+ def test_should_count_scoped_select_with_options
273
+ Account.update_all("credit_limit = NULL")
274
+ Account.last.update_attribute('credit_limit', 49)
275
+ Account.first.update_attribute('credit_limit', 51)
276
+
277
+ assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50'])
278
+ end
279
+
260
280
  def test_should_count_manual_select_with_include
261
281
  assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
262
282
  end
@@ -95,6 +95,15 @@ class FinderTest < ActiveRecord::TestCase
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
 
@@ -325,7 +354,7 @@ class FinderTest < ActiveRecord::TestCase
325
354
  end
326
355
 
327
356
  def test_hash_condition_find_malformed
328
- assert_raises(ActiveRecord::StatementInvalid) {
357
+ assert_raise(ActiveRecord::StatementInvalid) {
329
358
  Company.find(:first, :conditions => { :id => 2, :dhh => true })
330
359
  }
331
360
  end
@@ -394,10 +423,10 @@ class FinderTest < ActiveRecord::TestCase
394
423
  assert_nil Company.find(:first, :conditions => ["name = ?", "37signals!"])
395
424
  assert_nil Company.find(:first, :conditions => ["name = ?", "37signals!' OR 1=1"])
396
425
  assert_kind_of Time, Topic.find(:first, :conditions => ["id = ?", 1]).written_on
397
- assert_raises(ActiveRecord::PreparedStatementInvalid) {
426
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
398
427
  Company.find(:first, :conditions => ["id=? AND name = ?", 2])
399
428
  }
400
- assert_raises(ActiveRecord::PreparedStatementInvalid) {
429
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
401
430
  Company.find(:first, :conditions => ["id=?", 2, 3, 4])
402
431
  }
403
432
  end
@@ -414,11 +443,11 @@ class FinderTest < ActiveRecord::TestCase
414
443
 
415
444
  def test_bind_arity
416
445
  assert_nothing_raised { bind '' }
417
- assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '', 1 }
418
-
419
- assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?' }
446
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '', 1 }
447
+
448
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '?' }
420
449
  assert_nothing_raised { bind '?', 1 }
421
- assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 }
450
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 }
422
451
  end
423
452
 
424
453
  def test_named_bind_variables
@@ -501,21 +530,19 @@ class FinderTest < ActiveRecord::TestCase
501
530
  assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
502
531
  end
503
532
 
504
- uses_mocha('test_dynamic_finder_should_go_through_the_find_class_method') do
505
- def test_dynamic_finders_should_go_through_the_find_class_method
506
- Topic.expects(:find).with(:first, :conditions => { :title => 'The First Topic!' })
507
- Topic.find_by_title("The First Topic!")
533
+ def test_dynamic_finders_should_go_through_the_find_class_method
534
+ Topic.expects(:find).with(:first, :conditions => { :title => 'The First Topic!' })
535
+ Topic.find_by_title("The First Topic!")
508
536
 
509
- Topic.expects(:find).with(:last, :conditions => { :title => 'The Last Topic!' })
510
- Topic.find_last_by_title("The Last Topic!")
537
+ Topic.expects(:find).with(:last, :conditions => { :title => 'The Last Topic!' })
538
+ Topic.find_last_by_title("The Last Topic!")
511
539
 
512
- Topic.expects(:find).with(:all, :conditions => { :title => 'A Topic.' })
513
- Topic.find_all_by_title("A Topic.")
540
+ Topic.expects(:find).with(:all, :conditions => { :title => 'A Topic.' })
541
+ Topic.find_all_by_title("A Topic.")
514
542
 
515
- Topic.expects(:find).with(:first, :conditions => { :title => 'Does not exist yet for sure!' }).times(2)
516
- Topic.find_or_initialize_by_title('Does not exist yet for sure!')
517
- Topic.find_or_create_by_title('Does not exist yet for sure!')
518
- end
543
+ Topic.expects(:find).with(:first, :conditions => { :title => 'Does not exist yet for sure!' }).times(2)
544
+ Topic.find_or_initialize_by_title('Does not exist yet for sure!')
545
+ Topic.find_or_create_by_title('Does not exist yet for sure!')
519
546
  end
520
547
 
521
548
  def test_find_by_one_attribute
@@ -525,7 +552,7 @@ class FinderTest < ActiveRecord::TestCase
525
552
 
526
553
  def test_find_by_one_attribute_bang
527
554
  assert_equal topics(:first), Topic.find_by_title!("The First Topic")
528
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
555
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
529
556
  end
530
557
 
531
558
  def test_find_by_one_attribute_caches_dynamic_finder
@@ -606,14 +633,14 @@ class FinderTest < ActiveRecord::TestCase
606
633
  end
607
634
 
608
635
  def test_find_by_one_missing_attribute
609
- assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
636
+ assert_raise(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
610
637
  end
611
638
 
612
639
  def test_find_by_invalid_method_syntax
613
- assert_raises(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
614
- assert_raises(NoMethodError) { Topic.find_by_title?("The First Topic") }
615
- assert_raises(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
616
- assert_raises(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
640
+ assert_raise(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
641
+ assert_raise(NoMethodError) { Topic.find_by_title?("The First Topic") }
642
+ assert_raise(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
643
+ assert_raise(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
617
644
  end
618
645
 
619
646
  def test_find_by_two_attributes
@@ -635,8 +662,8 @@ class FinderTest < ActiveRecord::TestCase
635
662
  end
636
663
 
637
664
  def test_find_last_by_invalid_method_syntax
638
- assert_raises(NoMethodError) { Topic.fail_to_find_last_by_title("The First Topic") }
639
- assert_raises(NoMethodError) { Topic.find_last_by_title?("The First Topic") }
665
+ assert_raise(NoMethodError) { Topic.fail_to_find_last_by_title("The First Topic") }
666
+ assert_raise(NoMethodError) { Topic.find_last_by_title?("The First Topic") }
640
667
  end
641
668
 
642
669
  def test_find_last_by_one_attribute_with_several_options
@@ -644,7 +671,7 @@ class FinderTest < ActiveRecord::TestCase
644
671
  end
645
672
 
646
673
  def test_find_last_by_one_missing_attribute
647
- assert_raises(NoMethodError) { Topic.find_last_by_undertitle("The Last Topic!") }
674
+ assert_raise(NoMethodError) { Topic.find_last_by_undertitle("The Last Topic!") }
648
675
  end
649
676
 
650
677
  def test_find_last_by_two_attributes
@@ -897,16 +924,16 @@ class FinderTest < ActiveRecord::TestCase
897
924
  end
898
925
 
899
926
  def test_find_with_bad_sql
900
- assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
927
+ assert_raise(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
901
928
  end
902
929
 
903
930
  def test_find_with_invalid_params
904
- assert_raises(ArgumentError) { Topic.find :first, :join => "It should be `joins'" }
905
- assert_raises(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
931
+ assert_raise(ArgumentError) { Topic.find :first, :join => "It should be `joins'" }
932
+ assert_raise(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
906
933
  end
907
934
 
908
935
  def test_dynamic_finder_with_invalid_params
909
- assert_raises(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
936
+ assert_raise(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
910
937
  end
911
938
 
912
939
  def test_find_all_with_limit
@@ -1040,6 +1067,14 @@ class FinderTest < ActiveRecord::TestCase
1040
1067
  assert_equal [0, 1, 1], posts.map(&:author_id).sort
1041
1068
  end
1042
1069
 
1070
+ def test_finder_with_scoped_from
1071
+ all_topics = Topic.all
1072
+
1073
+ Topic.with_scope(:find => { :from => 'fake_topics' }) do
1074
+ assert_equal all_topics, Topic.all(:from => 'topics')
1075
+ end
1076
+ end
1077
+
1043
1078
  protected
1044
1079
  def bind(statement, *vars)
1045
1080
  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
@@ -269,12 +269,10 @@ class FixturesWithoutInstantiationTest < ActiveRecord::TestCase
269
269
  assert_raise(StandardError) { topics([:first, :second]) }
270
270
  end
271
271
 
272
- uses_mocha 'reloading_fixtures_through_accessor_methods' do
273
- def test_reloading_fixtures_through_accessor_methods
274
- assert_equal "The First Topic", topics(:first).title
275
- @loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
276
- assert_equal "Fresh Topic!", topics(:first, true).title
277
- end
272
+ def test_reloading_fixtures_through_accessor_methods
273
+ assert_equal "The First Topic", topics(:first).title
274
+ @loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
275
+ assert_equal "Fresh Topic!", topics(:first, true).title
278
276
  end
279
277
  end
280
278
 
@@ -426,7 +424,7 @@ class InvalidTableNameFixturesTest < ActiveRecord::TestCase
426
424
  self.use_transactional_fixtures = false
427
425
 
428
426
  def test_raises_error
429
- assert_raises FixtureClassNotFound do
427
+ assert_raise FixtureClassNotFound do
430
428
  funny_jokes(:a_joke)
431
429
  end
432
430
  end
@@ -643,17 +641,15 @@ class ActiveSupportSubclassWithFixturesTest < ActiveRecord::TestCase
643
641
  end
644
642
 
645
643
  class FixtureLoadingTest < ActiveRecord::TestCase
646
- uses_mocha 'reloading_fixtures_through_accessor_methods' do
647
- def test_logs_message_for_failed_dependency_load
648
- Test::Unit::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError)
649
- ActiveRecord::Base.logger.expects(:warn)
650
- Test::Unit::TestCase.try_to_load_dependency(:does_not_exist)
651
- end
644
+ def test_logs_message_for_failed_dependency_load
645
+ ActiveRecord::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError)
646
+ ActiveRecord::Base.logger.expects(:warn)
647
+ ActiveRecord::TestCase.try_to_load_dependency(:does_not_exist)
648
+ end
652
649
 
653
- def test_does_not_logs_message_for_successful_dependency_load
654
- Test::Unit::TestCase.expects(:require_dependency).with(:works_out_fine)
655
- ActiveRecord::Base.logger.expects(:warn).never
656
- Test::Unit::TestCase.try_to_load_dependency(:works_out_fine)
657
- end
650
+ def test_does_not_logs_message_for_successful_dependency_load
651
+ ActiveRecord::TestCase.expects(:require_dependency).with(:works_out_fine)
652
+ ActiveRecord::Base.logger.expects(:warn).never
653
+ ActiveRecord::TestCase.try_to_load_dependency(:works_out_fine)
658
654
  end
659
655
  end
@@ -97,6 +97,35 @@ if ActiveRecord::Base.connection.supports_migrations?
97
97
  end
98
98
  end
99
99
 
100
+ def testing_table_with_only_foo_attribute
101
+ Person.connection.create_table :testings, :id => false do |t|
102
+ t.column :foo, :string
103
+ end
104
+
105
+ yield Person.connection
106
+ ensure
107
+ Person.connection.drop_table :testings rescue nil
108
+ end
109
+ protected :testing_table_with_only_foo_attribute
110
+
111
+ def test_create_table_without_id
112
+ testing_table_with_only_foo_attribute do |connection|
113
+ assert_equal connection.columns(:testings).size, 1
114
+ end
115
+ end
116
+
117
+ unless current_adapter?(:IBM_DBAdapter)
118
+ # Cannot add a primary key to a table with some rows already in it as it violates the unique constraint
119
+ # Secondly GENERATED BY DEFAULT AS IDENTITY cannot be applied in a alter table command.
120
+ # as this will be wrong sql syntax for DB2
121
+ def test_add_column_with_primary_key_attribute
122
+ testing_table_with_only_foo_attribute do |connection|
123
+ assert_nothing_raised { connection.add_column :testings, :id, :primary_key }
124
+ assert_equal connection.columns(:testings).size, 2
125
+ end
126
+ end
127
+ end
128
+
100
129
  def test_create_table_adds_id
101
130
  Person.connection.create_table :testings do |t|
102
131
  t.column :foo, :string
@@ -115,7 +144,7 @@ if ActiveRecord::Base.connection.supports_migrations?
115
144
  end
116
145
  end
117
146
 
118
- assert_raises(ActiveRecord::StatementInvalid) do
147
+ assert_raise(ActiveRecord::StatementInvalid) do
119
148
  Person.connection.execute "insert into testings (foo) values (NULL)"
120
149
  end
121
150
  ensure
@@ -272,22 +301,20 @@ if ActiveRecord::Base.connection.supports_migrations?
272
301
  ActiveRecord::Base.primary_key_prefix_type = nil
273
302
  end
274
303
 
275
- uses_mocha('test_create_table_with_force_true_does_not_drop_nonexisting_table') do
276
- def test_create_table_with_force_true_does_not_drop_nonexisting_table
277
- if Person.connection.table_exists?(:testings2)
278
- Person.connection.drop_table :testings2
279
- end
304
+ def test_create_table_with_force_true_does_not_drop_nonexisting_table
305
+ if Person.connection.table_exists?(:testings2)
306
+ Person.connection.drop_table :testings2
307
+ end
280
308
 
281
- # using a copy as we need the drop_table method to
282
- # continue to work for the ensure block of the test
283
- temp_conn = Person.connection.dup
284
- temp_conn.expects(:drop_table).never
285
- temp_conn.create_table :testings2, :force => true do |t|
286
- t.column :foo, :string
287
- end
288
- ensure
289
- Person.connection.drop_table :testings2 rescue nil
309
+ # using a copy as we need the drop_table method to
310
+ # continue to work for the ensure block of the test
311
+ temp_conn = Person.connection.dup
312
+ temp_conn.expects(:drop_table).never
313
+ temp_conn.create_table :testings2, :force => true do |t|
314
+ t.column :foo, :string
290
315
  end
316
+ ensure
317
+ Person.connection.drop_table :testings2 rescue nil
291
318
  end
292
319
 
293
320
  def test_create_table_with_timestamps_should_create_datetime_columns
@@ -333,7 +360,7 @@ if ActiveRecord::Base.connection.supports_migrations?
333
360
  end
334
361
  Person.connection.add_column :testings, :bar, :string, :null => false
335
362
 
336
- assert_raises(ActiveRecord::StatementInvalid) do
363
+ assert_raise(ActiveRecord::StatementInvalid) do
337
364
  Person.connection.execute "insert into testings (foo, bar) values ('hello', NULL)"
338
365
  end
339
366
  ensure
@@ -352,7 +379,7 @@ if ActiveRecord::Base.connection.supports_migrations?
352
379
  Person.connection.enable_identity_insert("testings", false) if current_adapter?(:SybaseAdapter)
353
380
  assert_nothing_raised {Person.connection.add_column :testings, :bar, :string, :null => false, :default => "default" }
354
381
 
355
- assert_raises(ActiveRecord::StatementInvalid) do
382
+ assert_raise(ActiveRecord::StatementInvalid) do
356
383
  unless current_adapter?(:OpenBaseAdapter)
357
384
  Person.connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}, #{con.quote_column_name('bar')}) values (2, 'hello', NULL)"
358
385
  else
@@ -601,7 +628,7 @@ if ActiveRecord::Base.connection.supports_migrations?
601
628
  else
602
629
  ActiveRecord::ActiveRecordError
603
630
  end
604
- assert_raises(exception) do
631
+ assert_raise(exception) do
605
632
  Person.connection.rename_column "hats", "nonexistent", "should_fail"
606
633
  end
607
634
  ensure
@@ -873,6 +900,10 @@ if ActiveRecord::Base.connection.supports_migrations?
873
900
  end
874
901
 
875
902
  def test_change_column_default_to_null
903
+ if current_adapter?(:IBM_DBAdapter)
904
+ #Ensure that first_name had some default value else an error is thrown saying operation not valid
905
+ Person.connection.change_column_default "people", "first_name", "foo"
906
+ end
876
907
  Person.connection.change_column_default "people", "first_name", nil
877
908
  Person.reset_column_information
878
909
  assert_nil Person.new.first_name
@@ -887,7 +918,7 @@ if ActiveRecord::Base.connection.supports_migrations?
887
918
  assert_equal "hello world", Reminder.find(:first).content
888
919
 
889
920
  WeNeedReminders.down
890
- assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
921
+ assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
891
922
  end
892
923
 
893
924
  def test_add_table_with_decimals
@@ -960,7 +991,7 @@ if ActiveRecord::Base.connection.supports_migrations?
960
991
  end
961
992
 
962
993
  GiveMeBigNumbers.down
963
- assert_raises(ActiveRecord::StatementInvalid) { BigNumber.find(:first) }
994
+ assert_raise(ActiveRecord::StatementInvalid) { BigNumber.find(:first) }
964
995
  end
965
996
 
966
997
  def test_migrator
@@ -980,7 +1011,7 @@ if ActiveRecord::Base.connection.supports_migrations?
980
1011
  assert_equal 0, ActiveRecord::Migrator.current_version
981
1012
  Person.reset_column_information
982
1013
  assert !Person.column_methods_hash.include?(:last_name)
983
- assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
1014
+ assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
984
1015
  end
985
1016
 
986
1017
  def test_migrator_one_up
@@ -1032,11 +1063,11 @@ if ActiveRecord::Base.connection.supports_migrations?
1032
1063
  assert_equal(0, ActiveRecord::Migrator.current_version)
1033
1064
  end
1034
1065
 
1035
- if current_adapter?(:PostgreSQLAdapter)
1066
+ if ActiveRecord::Base.connection.supports_ddl_transactions?
1036
1067
  def test_migrator_one_up_with_exception_and_rollback
1037
1068
  assert !Person.column_methods_hash.include?(:last_name)
1038
1069
 
1039
- e = assert_raises(StandardError) do
1070
+ e = assert_raise(StandardError) do
1040
1071
  ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/broken", 100)
1041
1072
  end
1042
1073
 
@@ -1049,11 +1080,10 @@ if ActiveRecord::Base.connection.supports_migrations?
1049
1080
 
1050
1081
  def test_finds_migrations
1051
1082
  migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/valid").migrations
1052
- [['1', 'people_have_last_names'],
1053
- ['2', 'we_need_reminders'],
1054
- ['3', 'innocent_jointable']].each_with_index do |pair, i|
1055
- migrations[i].version == pair.first
1056
- migrations[1].name == pair.last
1083
+
1084
+ [[1, 'PeopleHaveLastNames'], [2, 'WeNeedReminders'], [3, 'InnocentJointable']].each_with_index do |pair, i|
1085
+ assert_equal migrations[i].version, pair.first
1086
+ assert_equal migrations[i].name, pair.last
1057
1087
  end
1058
1088
  end
1059
1089
 
@@ -1061,8 +1091,8 @@ if ActiveRecord::Base.connection.supports_migrations?
1061
1091
  ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2", 1)
1062
1092
  migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/interleaved/pass_2").pending_migrations
1063
1093
  assert_equal 1, migrations.size
1064
- migrations[0].version == '3'
1065
- migrations[0].name == 'innocent_jointable'
1094
+ assert_equal migrations[0].version, 3
1095
+ assert_equal migrations[0].name, 'InnocentJointable'
1066
1096
  end
1067
1097
 
1068
1098
  def test_only_loads_pending_migrations
@@ -1211,7 +1241,7 @@ if ActiveRecord::Base.connection.supports_migrations?
1211
1241
  assert_equal "hello world", Reminder.find(:first).content
1212
1242
 
1213
1243
  WeNeedReminders.down
1214
- assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
1244
+ assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
1215
1245
  ensure
1216
1246
  ActiveRecord::Base.table_name_prefix = ''
1217
1247
  ActiveRecord::Base.table_name_suffix = ''
@@ -1241,13 +1271,13 @@ if ActiveRecord::Base.connection.supports_migrations?
1241
1271
  end
1242
1272
 
1243
1273
  def test_migrator_with_duplicates
1244
- assert_raises(ActiveRecord::DuplicateMigrationVersionError) do
1274
+ assert_raise(ActiveRecord::DuplicateMigrationVersionError) do
1245
1275
  ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/duplicate", nil)
1246
1276
  end
1247
1277
  end
1248
1278
 
1249
1279
  def test_migrator_with_duplicate_names
1250
- assert_raises(ActiveRecord::DuplicateMigrationNameError, "Multiple migrations have the name Chunky") do
1280
+ assert_raise(ActiveRecord::DuplicateMigrationNameError, "Multiple migrations have the name Chunky") do
1251
1281
  ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/duplicate_names", nil)
1252
1282
  end
1253
1283
  end
@@ -1263,7 +1293,7 @@ if ActiveRecord::Base.connection.supports_migrations?
1263
1293
 
1264
1294
  # table name is 29 chars, the standard sequence name will
1265
1295
  # be 33 chars and fail
1266
- assert_raises(ActiveRecord::StatementInvalid) do
1296
+ assert_raise(ActiveRecord::StatementInvalid) do
1267
1297
  begin
1268
1298
  Person.connection.create_table :table_with_name_thats_just_ok do |t|
1269
1299
  t.column :foo, :string, :null => false
@@ -1290,7 +1320,7 @@ if ActiveRecord::Base.connection.supports_migrations?
1290
1320
  end
1291
1321
 
1292
1322
  # confirm the custom sequence got dropped
1293
- assert_raises(ActiveRecord::StatementInvalid) do
1323
+ assert_raise(ActiveRecord::StatementInvalid) do
1294
1324
  Person.connection.execute("select suitably_short_seq.nextval from dual")
1295
1325
  end
1296
1326
  end
@@ -1305,30 +1335,29 @@ if ActiveRecord::Base.connection.supports_migrations?
1305
1335
 
1306
1336
  end
1307
1337
 
1308
- uses_mocha 'Sexy migration tests' do
1309
- class SexyMigrationsTest < ActiveRecord::TestCase
1310
- def test_references_column_type_adds_id
1311
- with_new_table do |t|
1312
- t.expects(:column).with('customer_id', :integer, {})
1313
- t.references :customer
1314
- end
1338
+ class SexyMigrationsTest < ActiveRecord::TestCase
1339
+ def test_references_column_type_adds_id
1340
+ with_new_table do |t|
1341
+ t.expects(:column).with('customer_id', :integer, {})
1342
+ t.references :customer
1315
1343
  end
1344
+ end
1316
1345
 
1317
- def test_references_column_type_with_polymorphic_adds_type
1318
- with_new_table do |t|
1319
- t.expects(:column).with('taggable_type', :string, {})
1320
- t.expects(:column).with('taggable_id', :integer, {})
1321
- t.references :taggable, :polymorphic => true
1322
- end
1346
+ def test_references_column_type_with_polymorphic_adds_type
1347
+ with_new_table do |t|
1348
+ t.expects(:column).with('taggable_type', :string, {})
1349
+ t.expects(:column).with('taggable_id', :integer, {})
1350
+ t.references :taggable, :polymorphic => true
1323
1351
  end
1352
+ end
1324
1353
 
1325
- def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
1326
- with_new_table do |t|
1327
- t.expects(:column).with('taggable_type', :string, {:null => false})
1328
- t.expects(:column).with('taggable_id', :integer, {:null => false})
1329
- t.references :taggable, :polymorphic => true, :null => false
1330
- end
1354
+ def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
1355
+ with_new_table do |t|
1356
+ t.expects(:column).with('taggable_type', :string, {:null => false})
1357
+ t.expects(:column).with('taggable_id', :integer, {:null => false})
1358
+ t.references :taggable, :polymorphic => true, :null => false
1331
1359
  end
1360
+ end
1332
1361
 
1333
1362
  def test_belongs_to_works_like_references
1334
1363
  with_new_table do |t|
@@ -1370,16 +1399,14 @@ if ActiveRecord::Base.connection.supports_migrations?
1370
1399
  Person.connection.drop_table :delete_me rescue nil
1371
1400
  end
1372
1401
 
1373
- end # SexyMigrationsTest
1374
- end # uses_mocha
1402
+ end # SexyMigrationsTest
1375
1403
 
1376
- uses_mocha 'ChangeTable migration tests' do
1377
- class ChangeTableMigrationsTest < ActiveRecord::TestCase
1378
- def setup
1379
- @connection = Person.connection
1380
- @connection.create_table :delete_me, :force => true do |t|
1381
- end
1404
+ class ChangeTableMigrationsTest < ActiveRecord::TestCase
1405
+ def setup
1406
+ @connection = Person.connection
1407
+ @connection.create_table :delete_me, :force => true do |t|
1382
1408
  end
1409
+ end
1383
1410
 
1384
1411
  def teardown
1385
1412
  Person.connection.drop_table :delete_me rescue nil
@@ -1574,8 +1601,5 @@ if ActiveRecord::Base.connection.supports_migrations?
1574
1601
  yield t
1575
1602
  end
1576
1603
  end
1577
-
1578
- end # ChangeTable test
1579
- end # uses_mocha
1580
-
1581
- end
1604
+ end
1605
+ end