activerecord 2.1.0 → 2.1.1

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 (86) hide show
  1. data/CHANGELOG +34 -0
  2. data/README +0 -0
  3. data/Rakefile +6 -5
  4. data/lib/active_record.rb +8 -10
  5. data/lib/active_record/association_preload.rb +17 -12
  6. data/lib/active_record/associations.rb +45 -27
  7. data/lib/active_record/associations/association_collection.rb +8 -5
  8. data/lib/active_record/associations/association_proxy.rb +2 -6
  9. data/lib/active_record/associations/belongs_to_association.rb +0 -0
  10. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +0 -0
  11. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +2 -3
  12. data/lib/active_record/associations/has_many_association.rb +16 -4
  13. data/lib/active_record/associations/has_many_through_association.rb +1 -1
  14. data/lib/active_record/associations/has_one_association.rb +2 -2
  15. data/lib/active_record/associations/has_one_through_association.rb +4 -0
  16. data/lib/active_record/base.rb +33 -15
  17. data/lib/active_record/calculations.rb +20 -7
  18. data/lib/active_record/callbacks.rb +0 -0
  19. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +9 -6
  20. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +17 -10
  21. data/lib/active_record/connection_adapters/abstract_adapter.rb +0 -0
  22. data/lib/active_record/connection_adapters/mysql_adapter.rb +53 -24
  23. data/lib/active_record/connection_adapters/postgresql_adapter.rb +66 -20
  24. data/lib/active_record/connection_adapters/sqlite_adapter.rb +12 -0
  25. data/lib/active_record/dirty.rb +10 -3
  26. data/lib/active_record/fixtures.rb +0 -0
  27. data/lib/active_record/locking/optimistic.rb +1 -0
  28. data/lib/active_record/migration.rb +35 -8
  29. data/lib/active_record/named_scope.rb +6 -1
  30. data/lib/active_record/observer.rb +7 -5
  31. data/lib/active_record/test_case.rb +13 -2
  32. data/lib/active_record/validations.rb +19 -9
  33. data/lib/active_record/version.rb +1 -1
  34. data/test/cases/active_schema_test_postgresql.rb +2 -2
  35. data/test/cases/adapter_test.rb +1 -1
  36. data/test/cases/associations/belongs_to_associations_test.rb +19 -0
  37. data/test/cases/associations/cascaded_eager_loading_test.rb +13 -1
  38. data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +36 -0
  39. data/test/cases/associations/eager_test.rb +25 -1
  40. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +27 -5
  41. data/test/cases/associations/has_many_associations_test.rb +106 -4
  42. data/test/cases/associations/has_many_through_associations_test.rb +10 -0
  43. data/test/cases/associations/has_one_associations_test.rb +22 -0
  44. data/test/cases/associations/has_one_through_associations_test.rb +44 -5
  45. data/test/cases/associations/join_model_test.rb +7 -0
  46. data/test/cases/associations_test.rb +2 -2
  47. data/test/cases/attribute_methods_test.rb +10 -10
  48. data/test/cases/base_test.rb +39 -16
  49. data/test/cases/calculations_test.rb +53 -1
  50. data/test/cases/column_definition_test.rb +36 -0
  51. data/test/cases/database_statements_test.rb +12 -0
  52. data/test/cases/defaults_test.rb +1 -1
  53. data/test/cases/deprecated_finder_test.rb +0 -0
  54. data/test/cases/dirty_test.rb +94 -0
  55. data/test/cases/finder_test.rb +7 -0
  56. data/test/cases/fixtures_test.rb +0 -0
  57. data/test/cases/helper.rb +5 -5
  58. data/test/cases/inheritance_test.rb +9 -2
  59. data/test/cases/lifecycle_test.rb +54 -1
  60. data/test/cases/locking_test.rb +20 -0
  61. data/test/cases/method_scoping_test.rb +11 -1
  62. data/test/cases/migration_test.rb +147 -22
  63. data/test/cases/multiple_db_test.rb +1 -1
  64. data/test/cases/named_scope_test.rb +50 -1
  65. data/test/cases/query_cache_test.rb +4 -3
  66. data/test/cases/readonly_test.rb +0 -0
  67. data/test/cases/reflection_test.rb +3 -3
  68. data/test/cases/schema_dumper_test.rb +46 -0
  69. data/test/cases/unconnected_test.rb +0 -0
  70. data/test/cases/validations_test.rb +30 -5
  71. data/test/debug.log +358 -0
  72. data/test/fixtures/fixture_database.sqlite3 +0 -0
  73. data/test/fixtures/fixture_database_2.sqlite3 +0 -0
  74. data/test/models/author.rb +4 -0
  75. data/test/models/category.rb +1 -0
  76. data/test/models/company.rb +10 -1
  77. data/test/models/developer.rb +4 -1
  78. data/test/models/person.rb +1 -1
  79. data/test/models/post.rb +6 -1
  80. data/test/models/project.rb +1 -1
  81. data/test/models/reply.rb +0 -0
  82. data/test/models/topic.rb +1 -0
  83. data/test/schema/mysql_specific_schema.rb +2 -2
  84. data/test/schema/schema.rb +8 -0
  85. metadata +11 -5
  86. data/lib/active_record/vendor/db2.rb +0 -362
@@ -44,19 +44,23 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
44
44
  def test_has_one_through_polymorphic
45
45
  assert_equal clubs(:moustache_club), @member.sponsor_club
46
46
  end
47
-
47
+
48
48
  def has_one_through_to_has_many
49
49
  assert_equal 2, @member.fellow_members.size
50
50
  end
51
-
51
+
52
52
  def test_has_one_through_eager_loading
53
- members = Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"])
53
+ members = assert_queries(3) do #base table, through table, clubs table
54
+ Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"])
55
+ end
54
56
  assert_equal 1, members.size
55
57
  assert_not_nil assert_no_queries {members[0].club}
56
58
  end
57
-
59
+
58
60
  def test_has_one_through_eager_loading_through_polymorphic
59
- members = Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"])
61
+ members = assert_queries(3) do #base table, through table, clubs table
62
+ Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"])
63
+ end
60
64
  assert_equal 1, members.size
61
65
  assert_not_nil assert_no_queries {members[0].sponsor_club}
62
66
  end
@@ -71,4 +75,39 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
71
75
  assert_not_nil assert_no_queries {clubs[0].sponsored_member}
72
76
  end
73
77
 
78
+ def test_has_one_through_nonpreload_eagerloading
79
+ members = assert_queries(1) do
80
+ Member.find(:all, :include => :club, :conditions => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name') #force fallback
81
+ end
82
+ assert_equal 1, members.size
83
+ assert_not_nil assert_no_queries {members[0].club}
84
+ end
85
+
86
+ def test_has_one_through_nonpreload_eager_loading_through_polymorphic
87
+ members = assert_queries(1) do
88
+ Member.find(:all, :include => :sponsor_club, :conditions => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name') #force fallback
89
+ end
90
+ assert_equal 1, members.size
91
+ assert_not_nil assert_no_queries {members[0].sponsor_club}
92
+ end
93
+
94
+ def test_has_one_through_nonpreload_eager_loading_through_polymorphic_with_more_than_one_through_record
95
+ Sponsor.new(:sponsor_club => clubs(:crazy_club), :sponsorable => members(:groucho)).save!
96
+ members = assert_queries(1) do
97
+ Member.find(:all, :include => :sponsor_club, :conditions => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name DESC') #force fallback
98
+ end
99
+ assert_equal 1, members.size
100
+ assert_not_nil assert_no_queries { members[0].sponsor_club }
101
+ assert_equal clubs(:crazy_club), members[0].sponsor_club
102
+ end
103
+
104
+ def test_uninitialized_has_one_through_should_return_nil_for_unsaved_record
105
+ assert_nil Member.new.club
106
+ end
107
+
108
+ def test_assigning_association_correctly_assigns_target
109
+ new_member = Member.create(:name => "Chris")
110
+ new_member.club = new_club = Club.create(:name => "LRUG")
111
+ assert_equal new_club, new_member.club.target
112
+ end
74
113
  end
@@ -694,6 +694,13 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
694
694
  assert ! david.categories.include?(category)
695
695
  end
696
696
 
697
+ def test_has_many_through_goes_through_all_sti_classes
698
+ sub_sti_post = SubStiPost.create!(:title => 'test', :body => 'test', :author_id => 1)
699
+ new_comment = sub_sti_post.comments.create(:body => 'test')
700
+
701
+ assert_equal [9, 10, new_comment.id], authors(:david).sti_post_comments.map(&:id).sort
702
+ end
703
+
697
704
  private
698
705
  # create dynamic Post models to allow different dependency options
699
706
  def find_post_with_dependency(post_id, association, association_name, dependency)
@@ -27,7 +27,7 @@ require 'models/sponsor'
27
27
 
28
28
  class AssociationsTest < ActiveRecord::TestCase
29
29
  fixtures :accounts, :companies, :developers, :projects, :developers_projects,
30
- :computers
30
+ :computers, :people, :readers
31
31
 
32
32
  def test_include_with_order_works
33
33
  assert_nothing_raised {Account.find(:first, :order => 'id', :include => :firm)}
@@ -45,7 +45,7 @@ class AssociationsTest < ActiveRecord::TestCase
45
45
  assert_equal [], person.readers.find(:all)
46
46
  person.save!
47
47
  reader = Reader.create! :person => person, :post => Post.new(:title => "foo", :body => "bar")
48
- assert_equal [reader], person.readers.find(:all)
48
+ assert person.readers.find(reader.id)
49
49
  end
50
50
 
51
51
  def test_force_reload
@@ -137,7 +137,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
137
137
  end
138
138
  end
139
139
  end
140
-
140
+
141
141
  def test_time_attributes_are_retrieved_in_current_time_zone
142
142
  in_time_zone "Pacific Time (US & Canada)" do
143
143
  utc_time = Time.utc(2008, 1, 1)
@@ -145,7 +145,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
145
145
  record[:written_on] = utc_time
146
146
  assert_equal utc_time, record.written_on # record.written on is equal to (i.e., simultaneous with) utc_time
147
147
  assert_kind_of ActiveSupport::TimeWithZone, record.written_on # but is a TimeWithZone
148
- assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone # and is in the current Time.zone
148
+ assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone # and is in the current Time.zone
149
149
  assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time # and represents time values adjusted accordingly
150
150
  end
151
151
  end
@@ -156,7 +156,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
156
156
  record = @target.new
157
157
  record.written_on = utc_time
158
158
  assert_equal utc_time, record.written_on
159
- assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
159
+ assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
160
160
  assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time
161
161
  end
162
162
  end
@@ -168,7 +168,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
168
168
  record = @target.new
169
169
  record.written_on = cst_time
170
170
  assert_equal utc_time, record.written_on
171
- assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
171
+ assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
172
172
  assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time
173
173
  end
174
174
  end
@@ -181,12 +181,12 @@ class AttributeMethodsTest < ActiveRecord::TestCase
181
181
  record = @target.new
182
182
  record.written_on = time_string
183
183
  assert_equal Time.zone.parse(time_string), record.written_on
184
- assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
184
+ assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
185
185
  assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time
186
186
  end
187
187
  end
188
188
  end
189
-
189
+
190
190
  def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil
191
191
  in_time_zone "Pacific Time (US & Canada)" do
192
192
  record = @target.new
@@ -202,7 +202,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
202
202
  record = @target.new
203
203
  record.written_on = time_string
204
204
  assert_equal Time.zone.parse(time_string), record.written_on
205
- assert_equal TimeZone[timezone_offset], record.written_on.time_zone
205
+ assert_equal ActiveSupport::TimeZone[timezone_offset], record.written_on.time_zone
206
206
  assert_equal Time.utc(2008, 1, 1), record.written_on.time
207
207
  end
208
208
  end
@@ -214,7 +214,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
214
214
  record = @target.new
215
215
  record.written_on = utc_time.in_time_zone
216
216
  assert_equal utc_time, record.written_on
217
- assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
217
+ assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone
218
218
  assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time
219
219
  end
220
220
  end
@@ -223,12 +223,12 @@ class AttributeMethodsTest < ActiveRecord::TestCase
223
223
  def time_related_columns_on_topic
224
224
  Topic.columns.select{|c| [:time, :date, :datetime, :timestamp].include?(c.type)}.map(&:name)
225
225
  end
226
-
226
+
227
227
  def in_time_zone(zone)
228
228
  old_zone = Time.zone
229
229
  old_tz = ActiveRecord::Base.time_zone_aware_attributes
230
230
 
231
- Time.zone = zone ? TimeZone[zone] : nil
231
+ Time.zone = zone ? ActiveSupport::TimeZone[zone] : nil
232
232
  ActiveRecord::Base.time_zone_aware_attributes = !zone.nil?
233
233
  yield
234
234
  ensure
@@ -19,6 +19,7 @@ require 'models/warehouse_thing'
19
19
  require 'rexml/document'
20
20
 
21
21
  class Category < ActiveRecord::Base; end
22
+ class Categorization < ActiveRecord::Base; end
22
23
  class Smarts < ActiveRecord::Base; end
23
24
  class CreditCard < ActiveRecord::Base
24
25
  class PinNumber < ActiveRecord::Base
@@ -75,7 +76,7 @@ class TopicWithProtectedContentAndAccessibleAuthorName < ActiveRecord::Base
75
76
  end
76
77
 
77
78
  class BasicsTest < ActiveRecord::TestCase
78
- fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors
79
+ fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories
79
80
 
80
81
  def test_table_exists
81
82
  assert !NonExistentTable.table_exists?
@@ -130,22 +131,22 @@ class BasicsTest < ActiveRecord::TestCase
130
131
 
131
132
  def test_read_attributes_before_type_cast
132
133
  category = Category.new({:name=>"Test categoty", :type => nil})
133
- category_attrs = {"name"=>"Test categoty", "type" => nil}
134
+ category_attrs = {"name"=>"Test categoty", "type" => nil, "categorizations_count" => nil}
134
135
  assert_equal category_attrs , category.attributes_before_type_cast
135
136
  end
136
-
137
+
137
138
  if current_adapter?(:MysqlAdapter)
138
139
  def test_read_attributes_before_type_cast_on_boolean
139
140
  bool = Booleantest.create({ "value" => false })
140
141
  assert_equal 0, bool.attributes_before_type_cast["value"]
141
142
  end
142
143
  end
143
-
144
+
144
145
  def test_read_attributes_before_type_cast_on_datetime
145
146
  developer = Developer.find(:first)
146
147
  assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"]
147
148
  end
148
-
149
+
149
150
  def test_hash_content
150
151
  topic = Topic.new
151
152
  topic.content = { "one" => 1, "two" => 2 }
@@ -251,7 +252,7 @@ class BasicsTest < ActiveRecord::TestCase
251
252
  topic = Topic.create("title" => "New Topic")
252
253
  topicReloaded = Topic.find(topic.id)
253
254
  assert_equal(topic, topicReloaded)
254
- end
255
+ end
255
256
 
256
257
  def test_create_through_factory_with_block
257
258
  topic = Topic.create("title" => "New Topic") do |t|
@@ -576,7 +577,7 @@ class BasicsTest < ActiveRecord::TestCase
576
577
  def test_destroy_all
577
578
  original_count = Topic.count
578
579
  topics_by_mary = Topic.count(:conditions => mary = "author_name = 'Mary'")
579
-
580
+
580
581
  Topic.destroy_all mary
581
582
  assert_equal original_count - topics_by_mary, Topic.count
582
583
  end
@@ -614,6 +615,22 @@ class BasicsTest < ActiveRecord::TestCase
614
615
  assert_equal -2, Topic.find(2).replies_count
615
616
  end
616
617
 
618
+ def test_update_counter
619
+ category = categories(:general)
620
+ assert_nil category.categorizations_count
621
+ assert_equal 2, category.categorizations.count
622
+
623
+ Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
624
+ category.reload
625
+ assert_not_nil category.categorizations_count
626
+ assert_equal 2, category.categorizations_count
627
+
628
+ Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
629
+ category.reload
630
+ assert_not_nil category.categorizations_count
631
+ assert_equal 4, category.categorizations_count
632
+ end
633
+
617
634
  def test_update_all
618
635
  assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
619
636
  assert_equal "bulk updated!", Topic.find(1).content
@@ -665,7 +682,7 @@ class BasicsTest < ActiveRecord::TestCase
665
682
 
666
683
  def test_delete_all
667
684
  assert Topic.count > 0
668
-
685
+
669
686
  assert_equal Topic.count, Topic.delete_all
670
687
  end
671
688
 
@@ -970,7 +987,7 @@ class BasicsTest < ActiveRecord::TestCase
970
987
  topic.attributes = attributes
971
988
  assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
972
989
  end
973
-
990
+
974
991
  def test_multiparameter_attributes_on_time_with_old_date
975
992
  attributes = {
976
993
  "written_on(1i)" => "1850", "written_on(2i)" => "6", "written_on(3i)" => "24",
@@ -998,7 +1015,7 @@ class BasicsTest < ActiveRecord::TestCase
998
1015
  def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes
999
1016
  ActiveRecord::Base.time_zone_aware_attributes = true
1000
1017
  ActiveRecord::Base.default_timezone = :utc
1001
- Time.zone = TimeZone[-28800]
1018
+ Time.zone = ActiveSupport::TimeZone[-28800]
1002
1019
  attributes = {
1003
1020
  "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
1004
1021
  "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
@@ -1016,7 +1033,7 @@ class BasicsTest < ActiveRecord::TestCase
1016
1033
 
1017
1034
  def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes_false
1018
1035
  ActiveRecord::Base.time_zone_aware_attributes = false
1019
- Time.zone = TimeZone[-28800]
1036
+ Time.zone = ActiveSupport::TimeZone[-28800]
1020
1037
  attributes = {
1021
1038
  "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
1022
1039
  "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
@@ -1032,7 +1049,7 @@ class BasicsTest < ActiveRecord::TestCase
1032
1049
  def test_multiparameter_attributes_on_time_with_skip_time_zone_conversion_for_attributes
1033
1050
  ActiveRecord::Base.time_zone_aware_attributes = true
1034
1051
  ActiveRecord::Base.default_timezone = :utc
1035
- Time.zone = TimeZone[-28800]
1052
+ Time.zone = ActiveSupport::TimeZone[-28800]
1036
1053
  Topic.skip_time_zone_conversion_for_attributes = [:written_on]
1037
1054
  attributes = {
1038
1055
  "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
@@ -1336,6 +1353,12 @@ class BasicsTest < ActiveRecord::TestCase
1336
1353
  assert_equal(myobj, topic.content)
1337
1354
  end
1338
1355
 
1356
+ def test_serialized_time_attribute
1357
+ myobj = Time.local(2008,1,1,1,0)
1358
+ topic = Topic.create("content" => myobj).reload
1359
+ assert_equal(myobj, topic.content)
1360
+ end
1361
+
1339
1362
  def test_nil_serialized_attribute_with_class_constraint
1340
1363
  myobj = MyObject.new('value1', 'value2')
1341
1364
  topic = Topic.new
@@ -1647,7 +1670,7 @@ class BasicsTest < ActiveRecord::TestCase
1647
1670
  last = Developer.find :last
1648
1671
  assert_equal last, Developer.find(:first, :order => 'id desc')
1649
1672
  end
1650
-
1673
+
1651
1674
  def test_last
1652
1675
  assert_equal Developer.find(:first, :order => 'id desc'), Developer.last
1653
1676
  end
@@ -1655,7 +1678,7 @@ class BasicsTest < ActiveRecord::TestCase
1655
1678
  def test_all_with_conditions
1656
1679
  assert_equal Developer.find(:all, :order => 'id desc'), Developer.all(:order => 'id desc')
1657
1680
  end
1658
-
1681
+
1659
1682
  def test_find_ordered_last
1660
1683
  last = Developer.find :last, :order => 'developers.salary ASC'
1661
1684
  assert_equal last, Developer.find(:all, :order => 'developers.salary ASC').last
@@ -1670,14 +1693,14 @@ class BasicsTest < ActiveRecord::TestCase
1670
1693
  last = Developer.find :last, :order => 'developers.name, developers.salary DESC'
1671
1694
  assert_equal last, Developer.find(:all, :order => 'developers.name, developers.salary DESC').last
1672
1695
  end
1673
-
1696
+
1674
1697
  def test_find_scoped_ordered_last
1675
1698
  last_developer = Developer.with_scope(:find => { :order => 'developers.salary ASC' }) do
1676
1699
  Developer.find(:last)
1677
1700
  end
1678
1701
  assert_equal last_developer, Developer.find(:all, :order => 'developers.salary ASC').last
1679
1702
  end
1680
-
1703
+
1681
1704
  def test_abstract_class
1682
1705
  assert !ActiveRecord::Base.abstract_class?
1683
1706
  assert LoosePerson.abstract_class?
@@ -1,6 +1,7 @@
1
1
  require "cases/helper"
2
2
  require 'models/company'
3
3
  require 'models/topic'
4
+ require 'models/edge'
4
5
 
5
6
  Company.has_many :accounts
6
7
 
@@ -99,6 +100,12 @@ class CalculationsTest < ActiveRecord::TestCase
99
100
 
100
101
  def test_should_return_zero_if_sum_conditions_return_nothing
101
102
  assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
103
+ assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2')
104
+ end
105
+
106
+ def test_sum_should_return_valid_values_for_decimals
107
+ NumericData.create(:bank_balance => 19.83)
108
+ assert_equal 19.83, NumericData.sum(:bank_balance)
102
109
  end
103
110
 
104
111
  def test_should_group_by_summed_field_with_conditions
@@ -266,6 +273,51 @@ class CalculationsTest < ActiveRecord::TestCase
266
273
  end
267
274
 
268
275
  def test_should_sum_expression
269
- assert_equal "636", Account.sum("2 * credit_limit")
276
+ assert_equal 636, Account.sum("2 * credit_limit")
277
+ end
278
+
279
+ def test_count_with_from_option
280
+ assert_equal Company.count(:all), Company.count(:all, :from => 'companies')
281
+ assert_equal Account.count(:all, :conditions => "credit_limit = 50"),
282
+ Account.count(:all, :from => 'accounts', :conditions => "credit_limit = 50")
283
+ assert_equal Company.count(:type, :conditions => {:type => "Firm"}),
284
+ Company.count(:type, :conditions => {:type => "Firm"}, :from => 'companies')
285
+ end
286
+
287
+ def test_sum_with_from_option
288
+ assert_equal Account.sum(:credit_limit), Account.sum(:credit_limit, :from => 'accounts')
289
+ assert_equal Account.sum(:credit_limit, :conditions => "credit_limit > 50"),
290
+ Account.sum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
291
+ end
292
+
293
+ def test_average_with_from_option
294
+ assert_equal Account.average(:credit_limit), Account.average(:credit_limit, :from => 'accounts')
295
+ assert_equal Account.average(:credit_limit, :conditions => "credit_limit > 50"),
296
+ Account.average(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
297
+ end
298
+
299
+ def test_minimum_with_from_option
300
+ assert_equal Account.minimum(:credit_limit), Account.minimum(:credit_limit, :from => 'accounts')
301
+ assert_equal Account.minimum(:credit_limit, :conditions => "credit_limit > 50"),
302
+ Account.minimum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
303
+ end
304
+
305
+ def test_maximum_with_from_option
306
+ assert_equal Account.maximum(:credit_limit), Account.maximum(:credit_limit, :from => 'accounts')
307
+ assert_equal Account.maximum(:credit_limit, :conditions => "credit_limit > 50"),
308
+ Account.maximum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
270
309
  end
310
+
311
+ def test_from_option_with_specified_index
312
+ if Edge.connection.adapter_name == 'MySQL'
313
+ assert_equal Edge.count(:all), Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)')
314
+ assert_equal Edge.count(:all, :conditions => 'sink_id < 5'),
315
+ Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)', :conditions => 'sink_id < 5')
316
+ end
317
+ end
318
+
319
+ def test_from_option_with_table_different_than_class
320
+ assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
321
+ end
322
+
271
323
  end
@@ -0,0 +1,36 @@
1
+ require "cases/helper"
2
+
3
+ class ColumnDefinitionTest < ActiveRecord::TestCase
4
+ def setup
5
+ @adapter = ActiveRecord::ConnectionAdapters::AbstractAdapter.new(nil)
6
+ def @adapter.native_database_types
7
+ {:string => "varchar"}
8
+ end
9
+ end
10
+
11
+ # Avoid column definitions in create table statements like:
12
+ # `title` varchar(255) DEFAULT NULL
13
+ def test_should_not_include_default_clause_when_default_is_null
14
+ column = ActiveRecord::ConnectionAdapters::Column.new("title", nil, "varchar(20)")
15
+ column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
16
+ @adapter, column.name, "string",
17
+ column.limit, column.precision, column.scale, column.default, column.null)
18
+ assert_equal "title varchar(20)", column_def.to_sql
19
+ end
20
+
21
+ def test_should_include_default_clause_when_default_is_present
22
+ column = ActiveRecord::ConnectionAdapters::Column.new("title", "Hello", "varchar(20)")
23
+ column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
24
+ @adapter, column.name, "string",
25
+ column.limit, column.precision, column.scale, column.default, column.null)
26
+ assert_equal %Q{title varchar(20) DEFAULT 'Hello'}, column_def.to_sql
27
+ end
28
+
29
+ def test_should_specify_not_null_if_null_option_is_false
30
+ column = ActiveRecord::ConnectionAdapters::Column.new("title", "Hello", "varchar(20)", false)
31
+ column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
32
+ @adapter, column.name, "string",
33
+ column.limit, column.precision, column.scale, column.default, column.null)
34
+ assert_equal %Q{title varchar(20) DEFAULT 'Hello' NOT NULL}, column_def.to_sql
35
+ end
36
+ end