activerecord 2.0.5 → 2.1.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 (289) hide show
  1. data/CHANGELOG +168 -6
  2. data/README +27 -22
  3. data/RUNNING_UNIT_TESTS +7 -4
  4. data/Rakefile +22 -25
  5. data/lib/active_record.rb +8 -2
  6. data/lib/active_record/aggregations.rb +21 -12
  7. data/lib/active_record/association_preload.rb +277 -0
  8. data/lib/active_record/associations.rb +481 -295
  9. data/lib/active_record/associations/association_collection.rb +162 -37
  10. data/lib/active_record/associations/association_proxy.rb +71 -7
  11. data/lib/active_record/associations/belongs_to_association.rb +5 -3
  12. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +5 -6
  13. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +12 -64
  14. data/lib/active_record/associations/has_many_association.rb +8 -73
  15. data/lib/active_record/associations/has_many_through_association.rb +68 -117
  16. data/lib/active_record/associations/has_one_association.rb +7 -5
  17. data/lib/active_record/associations/has_one_through_association.rb +28 -0
  18. data/lib/active_record/attribute_methods.rb +69 -19
  19. data/lib/active_record/base.rb +496 -275
  20. data/lib/active_record/calculations.rb +28 -21
  21. data/lib/active_record/callbacks.rb +9 -38
  22. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +3 -2
  23. data/lib/active_record/connection_adapters/abstract/database_statements.rb +2 -2
  24. data/lib/active_record/connection_adapters/abstract/query_cache.rb +6 -0
  25. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +232 -45
  26. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +141 -27
  27. data/lib/active_record/connection_adapters/abstract_adapter.rb +9 -13
  28. data/lib/active_record/connection_adapters/mysql_adapter.rb +57 -24
  29. data/lib/active_record/connection_adapters/postgresql_adapter.rb +143 -42
  30. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
  31. data/lib/active_record/connection_adapters/sqlite_adapter.rb +18 -10
  32. data/lib/active_record/dirty.rb +158 -0
  33. data/lib/active_record/fixtures.rb +121 -156
  34. data/lib/active_record/locking/optimistic.rb +14 -11
  35. data/lib/active_record/locking/pessimistic.rb +2 -2
  36. data/lib/active_record/migration.rb +157 -77
  37. data/lib/active_record/named_scope.rb +163 -0
  38. data/lib/active_record/observer.rb +19 -5
  39. data/lib/active_record/reflection.rb +34 -14
  40. data/lib/active_record/schema.rb +7 -14
  41. data/lib/active_record/schema_dumper.rb +4 -4
  42. data/lib/active_record/serialization.rb +5 -5
  43. data/lib/active_record/serializers/json_serializer.rb +37 -28
  44. data/lib/active_record/serializers/xml_serializer.rb +52 -29
  45. data/lib/active_record/test_case.rb +36 -0
  46. data/lib/active_record/timestamp.rb +4 -4
  47. data/lib/active_record/transactions.rb +3 -3
  48. data/lib/active_record/validations.rb +182 -248
  49. data/lib/active_record/version.rb +2 -2
  50. data/test/{fixtures → assets}/example.log +0 -0
  51. data/test/{fixtures → assets}/flowers.jpg +0 -0
  52. data/test/cases/aaa_create_tables_test.rb +24 -0
  53. data/test/cases/active_schema_test_mysql.rb +95 -0
  54. data/test/cases/active_schema_test_postgresql.rb +24 -0
  55. data/test/{adapter_test.rb → cases/adapter_test.rb} +15 -14
  56. data/test/{adapter_test_sqlserver.rb → cases/adapter_test_sqlserver.rb} +95 -95
  57. data/test/{aggregations_test.rb → cases/aggregations_test.rb} +20 -20
  58. data/test/{ar_schema_test.rb → cases/ar_schema_test.rb} +6 -6
  59. data/test/cases/associations/belongs_to_associations_test.rb +412 -0
  60. data/test/{associations → cases/associations}/callbacks_test.rb +24 -10
  61. data/test/{associations → cases/associations}/cascaded_eager_loading_test.rb +18 -17
  62. data/test/cases/associations/eager_load_nested_include_test.rb +83 -0
  63. data/test/{associations → cases/associations}/eager_singularization_test.rb +5 -5
  64. data/test/{associations → cases/associations}/eager_test.rb +216 -51
  65. data/test/{associations → cases/associations}/extension_test.rb +8 -8
  66. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +684 -0
  67. data/test/cases/associations/has_many_associations_test.rb +932 -0
  68. data/test/cases/associations/has_many_through_associations_test.rb +190 -0
  69. data/test/cases/associations/has_one_associations_test.rb +323 -0
  70. data/test/cases/associations/has_one_through_associations_test.rb +74 -0
  71. data/test/{associations → cases/associations}/inner_join_association_test.rb +20 -20
  72. data/test/{associations → cases/associations}/join_model_test.rb +175 -35
  73. data/test/cases/associations_test.rb +262 -0
  74. data/test/{attribute_methods_test.rb → cases/attribute_methods_test.rb} +103 -11
  75. data/test/{base_test.rb → cases/base_test.rb} +338 -191
  76. data/test/{binary_test.rb → cases/binary_test.rb} +6 -4
  77. data/test/{calculations_test.rb → cases/calculations_test.rb} +35 -23
  78. data/test/{callbacks_test.rb → cases/callbacks_test.rb} +7 -7
  79. data/test/{class_inheritable_attributes_test.rb → cases/class_inheritable_attributes_test.rb} +3 -3
  80. data/test/{column_alias_test.rb → cases/column_alias_test.rb} +3 -3
  81. data/test/{connection_test_firebird.rb → cases/connection_test_firebird.rb} +2 -2
  82. data/test/{connection_test_mysql.rb → cases/connection_test_mysql.rb} +2 -2
  83. data/test/{copy_table_test_sqlite.rb → cases/copy_table_test_sqlite.rb} +13 -13
  84. data/test/{datatype_test_postgresql.rb → cases/datatype_test_postgresql.rb} +8 -8
  85. data/test/{date_time_test.rb → cases/date_time_test.rb} +5 -5
  86. data/test/{default_test_firebird.rb → cases/default_test_firebird.rb} +3 -3
  87. data/test/{defaults_test.rb → cases/defaults_test.rb} +8 -6
  88. data/test/{deprecated_finder_test.rb → cases/deprecated_finder_test.rb} +3 -3
  89. data/test/cases/dirty_test.rb +163 -0
  90. data/test/cases/finder_respond_to_test.rb +76 -0
  91. data/test/{finder_test.rb → cases/finder_test.rb} +266 -33
  92. data/test/{fixtures_test.rb → cases/fixtures_test.rb} +88 -72
  93. data/test/cases/helper.rb +47 -0
  94. data/test/{inheritance_test.rb → cases/inheritance_test.rb} +61 -17
  95. data/test/cases/invalid_date_test.rb +24 -0
  96. data/test/{json_serialization_test.rb → cases/json_serialization_test.rb} +36 -11
  97. data/test/{lifecycle_test.rb → cases/lifecycle_test.rb} +16 -13
  98. data/test/{locking_test.rb → cases/locking_test.rb} +17 -10
  99. data/test/{method_scoping_test.rb → cases/method_scoping_test.rb} +75 -39
  100. data/test/{migration_test.rb → cases/migration_test.rb} +420 -80
  101. data/test/{migration_test_firebird.rb → cases/migration_test_firebird.rb} +3 -3
  102. data/test/{mixin_test.rb → cases/mixin_test.rb} +7 -6
  103. data/test/{modules_test.rb → cases/modules_test.rb} +11 -6
  104. data/test/{multiple_db_test.rb → cases/multiple_db_test.rb} +5 -5
  105. data/test/cases/named_scope_test.rb +157 -0
  106. data/test/{pk_test.rb → cases/pk_test.rb} +10 -10
  107. data/test/{query_cache_test.rb → cases/query_cache_test.rb} +12 -10
  108. data/test/{readonly_test.rb → cases/readonly_test.rb} +11 -11
  109. data/test/{reflection_test.rb → cases/reflection_test.rb} +15 -14
  110. data/test/{reserved_word_test_mysql.rb → cases/reserved_word_test_mysql.rb} +4 -5
  111. data/test/{schema_authorization_test_postgresql.rb → cases/schema_authorization_test_postgresql.rb} +5 -5
  112. data/test/cases/schema_dumper_test.rb +138 -0
  113. data/test/cases/schema_test_postgresql.rb +102 -0
  114. data/test/{serialization_test.rb → cases/serialization_test.rb} +7 -7
  115. data/test/{synonym_test_oracle.rb → cases/synonym_test_oracle.rb} +5 -5
  116. data/test/{table_name_test_sqlserver.rb → cases/table_name_test_sqlserver.rb} +3 -3
  117. data/test/{threaded_connections_test.rb → cases/threaded_connections_test.rb} +7 -7
  118. data/test/{transactions_test.rb → cases/transactions_test.rb} +31 -5
  119. data/test/{unconnected_test.rb → cases/unconnected_test.rb} +2 -2
  120. data/test/{validations_test.rb → cases/validations_test.rb} +141 -39
  121. data/test/{xml_serialization_test.rb → cases/xml_serialization_test.rb} +12 -12
  122. data/test/config.rb +5 -0
  123. data/test/connections/native_db2/connection.rb +1 -1
  124. data/test/connections/native_firebird/connection.rb +1 -1
  125. data/test/connections/native_frontbase/connection.rb +1 -1
  126. data/test/connections/native_mysql/connection.rb +1 -1
  127. data/test/connections/native_openbase/connection.rb +1 -1
  128. data/test/connections/native_oracle/connection.rb +1 -1
  129. data/test/connections/native_postgresql/connection.rb +1 -3
  130. data/test/connections/native_sqlite/connection.rb +2 -2
  131. data/test/connections/native_sqlite3/connection.rb +2 -2
  132. data/test/connections/native_sqlite3/in_memory_connection.rb +3 -3
  133. data/test/connections/native_sybase/connection.rb +1 -1
  134. data/test/fixtures/author_addresses.yml +5 -0
  135. data/test/fixtures/authors.yml +2 -0
  136. data/test/fixtures/clubs.yml +6 -0
  137. data/test/fixtures/jobs.yml +7 -0
  138. data/test/fixtures/members.yml +4 -0
  139. data/test/fixtures/memberships.yml +20 -0
  140. data/test/fixtures/owners.yml +7 -0
  141. data/test/fixtures/people.yml +4 -1
  142. data/test/fixtures/pets.yml +14 -0
  143. data/test/fixtures/posts.yml +1 -0
  144. data/test/fixtures/price_estimates.yml +7 -0
  145. data/test/fixtures/readers.yml +5 -0
  146. data/test/fixtures/references.yml +17 -0
  147. data/test/fixtures/sponsors.yml +9 -0
  148. data/test/fixtures/subscribers.yml +7 -0
  149. data/test/fixtures/subscriptions.yml +12 -0
  150. data/test/fixtures/taggings.yml +4 -1
  151. data/test/fixtures/topics.yml +22 -2
  152. data/test/fixtures/warehouse-things.yml +3 -0
  153. data/test/{fixtures/migrations_with_decimal → migrations/decimal}/1_give_me_big_numbers.rb +0 -0
  154. data/test/{fixtures/migrations_with_duplicate → migrations/duplicate}/1_people_have_last_names.rb +1 -1
  155. data/test/{fixtures/migrations_with_duplicate → migrations/duplicate}/2_we_need_reminders.rb +1 -1
  156. data/test/{fixtures/migrations_with_duplicate → migrations/duplicate}/3_foo.rb +0 -0
  157. data/test/{fixtures/migrations → migrations/duplicate}/3_innocent_jointable.rb +0 -0
  158. data/test/migrations/duplicate_names/20080507052938_chunky.rb +7 -0
  159. data/test/migrations/duplicate_names/20080507053028_chunky.rb +7 -0
  160. data/test/{fixtures/migrations_with_duplicate → migrations/interleaved/pass_1}/3_innocent_jointable.rb +0 -0
  161. data/test/{fixtures/migrations → migrations/interleaved/pass_2}/1_people_have_last_names.rb +1 -1
  162. data/test/{fixtures/migrations_with_missing_versions/4_innocent_jointable.rb → migrations/interleaved/pass_2/3_innocent_jointable.rb} +0 -0
  163. data/test/{fixtures/migrations_with_missing_versions → migrations/interleaved/pass_3}/1_people_have_last_names.rb +1 -1
  164. data/test/migrations/interleaved/pass_3/2_i_raise_on_down.rb +8 -0
  165. data/test/migrations/interleaved/pass_3/3_innocent_jointable.rb +12 -0
  166. data/test/{fixtures/migrations_with_missing_versions → migrations/missing}/1000_people_have_middle_names.rb +1 -1
  167. data/test/migrations/missing/1_people_have_last_names.rb +9 -0
  168. data/test/{fixtures/migrations_with_missing_versions → migrations/missing}/3_we_need_reminders.rb +1 -1
  169. data/test/migrations/missing/4_innocent_jointable.rb +12 -0
  170. data/test/migrations/valid/1_people_have_last_names.rb +9 -0
  171. data/test/{fixtures/migrations → migrations/valid}/2_we_need_reminders.rb +1 -1
  172. data/test/migrations/valid/3_innocent_jointable.rb +12 -0
  173. data/test/{fixtures → models}/author.rb +28 -4
  174. data/test/{fixtures → models}/auto_id.rb +0 -0
  175. data/test/{fixtures → models}/binary.rb +0 -0
  176. data/test/{fixtures → models}/book.rb +0 -0
  177. data/test/{fixtures → models}/categorization.rb +0 -0
  178. data/test/{fixtures → models}/category.rb +8 -5
  179. data/test/{fixtures → models}/citation.rb +0 -0
  180. data/test/models/club.rb +7 -0
  181. data/test/{fixtures → models}/column_name.rb +0 -0
  182. data/test/{fixtures → models}/comment.rb +5 -3
  183. data/test/{fixtures → models}/company.rb +15 -6
  184. data/test/{fixtures → models}/company_in_module.rb +5 -3
  185. data/test/{fixtures → models}/computer.rb +0 -1
  186. data/test/{fixtures → models}/contact.rb +1 -1
  187. data/test/{fixtures → models}/course.rb +0 -0
  188. data/test/{fixtures → models}/customer.rb +8 -8
  189. data/test/{fixtures → models}/default.rb +0 -0
  190. data/test/{fixtures → models}/developer.rb +14 -10
  191. data/test/{fixtures → models}/edge.rb +0 -0
  192. data/test/{fixtures → models}/entrant.rb +0 -0
  193. data/test/models/guid.rb +2 -0
  194. data/test/{fixtures → models}/item.rb +0 -0
  195. data/test/models/job.rb +5 -0
  196. data/test/{fixtures → models}/joke.rb +0 -0
  197. data/test/{fixtures → models}/keyboard.rb +0 -0
  198. data/test/{fixtures → models}/legacy_thing.rb +0 -0
  199. data/test/{fixtures → models}/matey.rb +0 -0
  200. data/test/models/member.rb +9 -0
  201. data/test/models/membership.rb +9 -0
  202. data/test/{fixtures → models}/minimalistic.rb +0 -0
  203. data/test/{fixtures → models}/mixed_case_monkey.rb +0 -0
  204. data/test/{fixtures → models}/movie.rb +0 -0
  205. data/test/{fixtures → models}/order.rb +2 -2
  206. data/test/models/owner.rb +4 -0
  207. data/test/{fixtures → models}/parrot.rb +0 -0
  208. data/test/models/person.rb +10 -0
  209. data/test/models/pet.rb +4 -0
  210. data/test/models/pirate.rb +9 -0
  211. data/test/{fixtures → models}/post.rb +23 -2
  212. data/test/models/price_estimate.rb +3 -0
  213. data/test/{fixtures → models}/project.rb +1 -0
  214. data/test/{fixtures → models}/reader.rb +0 -0
  215. data/test/models/reference.rb +4 -0
  216. data/test/{fixtures → models}/reply.rb +7 -5
  217. data/test/{fixtures → models}/ship.rb +0 -0
  218. data/test/models/sponsor.rb +4 -0
  219. data/test/{fixtures → models}/subject.rb +0 -0
  220. data/test/{fixtures → models}/subscriber.rb +2 -0
  221. data/test/models/subscription.rb +4 -0
  222. data/test/{fixtures → models}/tag.rb +0 -0
  223. data/test/{fixtures → models}/tagging.rb +0 -0
  224. data/test/{fixtures → models}/task.rb +0 -0
  225. data/test/{fixtures → models}/topic.rb +32 -4
  226. data/test/{fixtures → models}/treasure.rb +2 -0
  227. data/test/{fixtures → models}/vertex.rb +0 -0
  228. data/test/models/warehouse_thing.rb +5 -0
  229. data/test/schema/mysql_specific_schema.rb +12 -0
  230. data/test/schema/postgresql_specific_schema.rb +103 -0
  231. data/test/schema/schema.rb +421 -0
  232. data/test/schema/schema2.rb +6 -0
  233. data/test/schema/sqlite_specific_schema.rb +25 -0
  234. data/test/schema/sqlserver_specific_schema.rb +5 -0
  235. metadata +192 -176
  236. data/test/aaa_create_tables_test.rb +0 -72
  237. data/test/abstract_unit.rb +0 -84
  238. data/test/active_schema_test_mysql.rb +0 -46
  239. data/test/all.sh +0 -8
  240. data/test/association_inheritance_reload.rb +0 -14
  241. data/test/associations_test.rb +0 -2177
  242. data/test/fixtures/bad_fixtures/attr_with_numeric_first_char +0 -1
  243. data/test/fixtures/bad_fixtures/attr_with_spaces +0 -1
  244. data/test/fixtures/bad_fixtures/blank_line +0 -3
  245. data/test/fixtures/bad_fixtures/duplicate_attributes +0 -3
  246. data/test/fixtures/bad_fixtures/missing_value +0 -1
  247. data/test/fixtures/db_definitions/db2.drop.sql +0 -33
  248. data/test/fixtures/db_definitions/db2.sql +0 -235
  249. data/test/fixtures/db_definitions/db22.drop.sql +0 -2
  250. data/test/fixtures/db_definitions/db22.sql +0 -5
  251. data/test/fixtures/db_definitions/firebird.drop.sql +0 -65
  252. data/test/fixtures/db_definitions/firebird.sql +0 -310
  253. data/test/fixtures/db_definitions/firebird2.drop.sql +0 -2
  254. data/test/fixtures/db_definitions/firebird2.sql +0 -6
  255. data/test/fixtures/db_definitions/frontbase.drop.sql +0 -33
  256. data/test/fixtures/db_definitions/frontbase.sql +0 -273
  257. data/test/fixtures/db_definitions/frontbase2.drop.sql +0 -1
  258. data/test/fixtures/db_definitions/frontbase2.sql +0 -4
  259. data/test/fixtures/db_definitions/openbase.drop.sql +0 -2
  260. data/test/fixtures/db_definitions/openbase.sql +0 -318
  261. data/test/fixtures/db_definitions/openbase2.drop.sql +0 -2
  262. data/test/fixtures/db_definitions/openbase2.sql +0 -7
  263. data/test/fixtures/db_definitions/oracle.drop.sql +0 -67
  264. data/test/fixtures/db_definitions/oracle.sql +0 -330
  265. data/test/fixtures/db_definitions/oracle2.drop.sql +0 -2
  266. data/test/fixtures/db_definitions/oracle2.sql +0 -6
  267. data/test/fixtures/db_definitions/postgresql.drop.sql +0 -44
  268. data/test/fixtures/db_definitions/postgresql.sql +0 -292
  269. data/test/fixtures/db_definitions/postgresql2.drop.sql +0 -2
  270. data/test/fixtures/db_definitions/postgresql2.sql +0 -4
  271. data/test/fixtures/db_definitions/schema.rb +0 -354
  272. data/test/fixtures/db_definitions/schema2.rb +0 -11
  273. data/test/fixtures/db_definitions/sqlite.drop.sql +0 -33
  274. data/test/fixtures/db_definitions/sqlite.sql +0 -219
  275. data/test/fixtures/db_definitions/sqlite2.drop.sql +0 -2
  276. data/test/fixtures/db_definitions/sqlite2.sql +0 -5
  277. data/test/fixtures/db_definitions/sybase.drop.sql +0 -35
  278. data/test/fixtures/db_definitions/sybase.sql +0 -222
  279. data/test/fixtures/db_definitions/sybase2.drop.sql +0 -4
  280. data/test/fixtures/db_definitions/sybase2.sql +0 -5
  281. data/test/fixtures/developers_projects/david_action_controller +0 -3
  282. data/test/fixtures/developers_projects/david_active_record +0 -3
  283. data/test/fixtures/developers_projects/jamis_active_record +0 -2
  284. data/test/fixtures/person.rb +0 -4
  285. data/test/fixtures/pirate.rb +0 -5
  286. data/test/fixtures/subscribers/first +0 -2
  287. data/test/fixtures/subscribers/second +0 -2
  288. data/test/schema_dumper_test.rb +0 -131
  289. data/test/schema_test_postgresql.rb +0 -64
@@ -1,9 +1,9 @@
1
- require 'abstract_unit'
1
+ require "cases/helper"
2
2
 
3
3
  class TestRecord < ActiveRecord::Base
4
4
  end
5
5
 
6
- class TestUnconnectedAdapter < Test::Unit::TestCase
6
+ class TestUnconnectedAdapter < ActiveRecord::TestCase
7
7
  self.use_transactional_fixtures = false
8
8
 
9
9
  def setup
@@ -1,17 +1,23 @@
1
- require 'abstract_unit'
2
- require 'fixtures/topic'
3
- require 'fixtures/reply'
4
- require 'fixtures/person'
5
- require 'fixtures/developer'
1
+ # encoding: utf-8
2
+ require "cases/helper"
3
+ require 'models/topic'
4
+ require 'models/reply'
5
+ require 'models/person'
6
+ require 'models/developer'
7
+ require 'models/warehouse_thing'
8
+ require 'models/guid'
6
9
 
7
10
  # The following methods in Topic are used in test_conditional_validation_*
8
11
  class Topic
12
+ has_many :unique_replies, :dependent => :destroy, :foreign_key => "parent_id"
13
+ has_many :silly_unique_replies, :dependent => :destroy, :foreign_key => "parent_id"
14
+
9
15
  def condition_is_true
10
- return true
16
+ true
11
17
  end
12
18
 
13
19
  def condition_is_true_but_its_not
14
- return false
20
+ false
15
21
  end
16
22
  end
17
23
 
@@ -32,11 +38,6 @@ end
32
38
  class SillyUniqueReply < UniqueReply
33
39
  end
34
40
 
35
- class Topic < ActiveRecord::Base
36
- has_many :unique_replies, :dependent => :destroy, :foreign_key => "parent_id"
37
- has_many :silly_unique_replies, :dependent => :destroy, :foreign_key => "parent_id"
38
- end
39
-
40
41
  class Wizard < ActiveRecord::Base
41
42
  self.abstract_class = true
42
43
 
@@ -53,13 +54,14 @@ end
53
54
  class Thaumaturgist < IneptWizard
54
55
  end
55
56
 
56
- class ValidationsTest < Test::Unit::TestCase
57
- fixtures :topics, :developers
57
+
58
+ class ValidationsTest < ActiveRecord::TestCase
59
+ fixtures :topics, :developers, 'warehouse-things'
58
60
 
59
61
  def setup
60
- Topic.write_inheritable_attribute(:validate, nil)
61
- Topic.write_inheritable_attribute(:validate_on_create, nil)
62
- Topic.write_inheritable_attribute(:validate_on_update, nil)
62
+ Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
63
+ Topic.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
64
+ Topic.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
63
65
  end
64
66
 
65
67
  def test_single_field_validation
@@ -132,6 +134,22 @@ class ValidationsTest < Test::Unit::TestCase
132
134
  Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
133
135
  end
134
136
  end
137
+
138
+ def test_exception_on_create_bang_with_block
139
+ assert_raises(ActiveRecord::RecordInvalid) do
140
+ Reply.create!({ "title" => "OK" }) do |r|
141
+ r.content = nil
142
+ end
143
+ end
144
+ end
145
+
146
+ def test_exception_on_create_bang_many_with_block
147
+ assert_raises(ActiveRecord::RecordInvalid) do
148
+ Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
149
+ r.content = nil
150
+ end
151
+ end
152
+ end
135
153
 
136
154
  def test_scoped_create_without_attributes
137
155
  Reply.with_scope(:create => {}) do
@@ -426,7 +444,7 @@ class ValidationsTest < Test::Unit::TestCase
426
444
  assert_nil t2.errors.on(:title)
427
445
  assert t2.errors.on(:parent_id)
428
446
 
429
- t2.parent_id = 3
447
+ t2.parent_id = 4
430
448
  assert t2.save, "Should now save t2 as unique"
431
449
 
432
450
  t2.parent_id = nil
@@ -435,6 +453,54 @@ class ValidationsTest < Test::Unit::TestCase
435
453
  assert t2.save, "should save with nil"
436
454
  end
437
455
 
456
+ def test_validate_case_sensitive_uniqueness
457
+ Topic.validates_uniqueness_of(:title, :case_sensitive => true, :allow_nil => true)
458
+
459
+ t = Topic.new("title" => "I'm unique!")
460
+ assert t.save, "Should save t as unique"
461
+
462
+ t.content = "Remaining unique"
463
+ assert t.save, "Should still save t as unique"
464
+
465
+ t2 = Topic.new("title" => "I'M UNIQUE!")
466
+ assert t2.valid?, "Should be valid"
467
+ assert t2.save, "Should save t2 as unique"
468
+ assert !t2.errors.on(:title)
469
+ assert !t2.errors.on(:parent_id)
470
+ assert_not_equal "has already been taken", t2.errors.on(:title)
471
+
472
+ t3 = Topic.new("title" => "I'M uNiQUe!")
473
+ assert t3.valid?, "Should be valid"
474
+ assert t3.save, "Should save t2 as unique"
475
+ assert !t3.errors.on(:title)
476
+ assert !t3.errors.on(:parent_id)
477
+ assert_not_equal "has already been taken", t3.errors.on(:title)
478
+ end
479
+
480
+ def test_validate_uniqueness_with_non_standard_table_names
481
+ i1 = WarehouseThing.create(:value => 1000)
482
+ assert !i1.valid?, "i1 should not be valid"
483
+ assert i1.errors.on(:value), "Should not be empty"
484
+ end
485
+
486
+ def test_validates_uniqueness_inside_with_scope
487
+ Topic.validates_uniqueness_of(:title)
488
+
489
+ Topic.with_scope(:find => { :conditions => { :author_name => "David" } }) do
490
+ t1 = Topic.new("title" => "I'm unique!", "author_name" => "Mary")
491
+ assert t1.save
492
+ t2 = Topic.new("title" => "I'm unique!", "author_name" => "David")
493
+ assert !t2.valid?
494
+ end
495
+ end
496
+
497
+ def test_validate_uniqueness_with_columns_which_are_sql_keywords
498
+ Guid.validates_uniqueness_of :key
499
+ g = Guid.new
500
+ g.key = "foo"
501
+ assert_nothing_raised { !g.valid? }
502
+ end
503
+
438
504
  def test_validate_straight_inheritance_uniqueness
439
505
  w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork")
440
506
  assert w1.valid?, "Saving w1"
@@ -482,12 +548,12 @@ class ValidationsTest < Test::Unit::TestCase
482
548
  end
483
549
 
484
550
  def test_validate_format_with_allow_blank
485
- Topic.validates_format_of(:title, :with => /^Validation\smacros \w+!$/, :allow_blank=>true)
486
- assert !Topic.create("title" => "Shouldn't be valid").valid?
487
- assert Topic.create("title" => "").valid?
488
- assert Topic.create("title" => nil).valid?
489
- assert Topic.create("title" => "Validation macros rule!").valid?
490
- end
551
+ Topic.validates_format_of(:title, :with => /^Validation\smacros \w+!$/, :allow_blank=>true)
552
+ assert !Topic.create("title" => "Shouldn't be valid").valid?
553
+ assert Topic.create("title" => "").valid?
554
+ assert Topic.create("title" => nil).valid?
555
+ assert Topic.create("title" => "Validation macros rule!").valid?
556
+ end
491
557
 
492
558
  # testing ticket #3142
493
559
  def test_validate_format_numeric
@@ -517,6 +583,12 @@ class ValidationsTest < Test::Unit::TestCase
517
583
  assert_nil t.errors.on(:title)
518
584
  end
519
585
 
586
+ def test_validate_format_with_formatted_message
587
+ Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => "can't be %s")
588
+ t = Topic.create(:title => 'Invalid title')
589
+ assert_equal "can't be Invalid title", t.errors.on(:title)
590
+ end
591
+
520
592
  def test_validates_inclusion_of
521
593
  Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) )
522
594
 
@@ -798,6 +870,20 @@ class ValidationsTest < Test::Unit::TestCase
798
870
  assert t.valid?
799
871
  end
800
872
 
873
+ def test_validates_size_of_association_using_within
874
+ assert_nothing_raised { Topic.validates_size_of :replies, :within => 1..2 }
875
+ t = Topic.new('title' => 'noreplies', 'content' => 'whatever')
876
+ assert !t.save
877
+ assert t.errors.on(:replies)
878
+
879
+ reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain')
880
+ assert t.valid?
881
+
882
+ 2.times { t.replies.build('title' => 'areply', 'content' => 'whateveragain') }
883
+ assert !t.save
884
+ assert t.errors.on(:replies)
885
+ end
886
+
801
887
  def test_validates_length_of_nasty_params
802
888
  assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>6, :maximum=>9) }
803
889
  assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :maximum=>9) }
@@ -901,19 +987,22 @@ class ValidationsTest < Test::Unit::TestCase
901
987
 
902
988
  def test_optionally_validates_length_of_using_within_utf8
903
989
  with_kcode('UTF8') do
904
- Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true
990
+ Topic.validates_length_of :title, :within => 3..5, :allow_nil => true
905
991
 
906
- t = Topic.create('title' => '一二三', 'content' => '一二三四五')
907
- assert t.valid?
992
+ t = Topic.create(:title => "一二三四五")
993
+ assert t.valid?, t.errors.inspect
994
+
995
+ t = Topic.create(:title => "一二三")
996
+ assert t.valid?, t.errors.inspect
908
997
 
909
998
  t.title = nil
910
- assert t.valid?
999
+ assert t.valid?, t.errors.inspect
911
1000
  end
912
1001
  end
913
1002
 
914
1003
  def test_optionally_validates_length_of_using_within_on_create_utf8
915
1004
  with_kcode('UTF8') do
916
- Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => "長すぎます: %d"
1005
+ Topic.validates_length_of :title, :within => 5..10, :on => :create, :too_long => "長すぎます: %d"
917
1006
 
918
1007
  t = Topic.create("title" => "一二三四五六七八九十A", "content" => "whatever")
919
1008
  assert !t.save
@@ -936,7 +1025,7 @@ class ValidationsTest < Test::Unit::TestCase
936
1025
 
937
1026
  def test_optionally_validates_length_of_using_within_on_update_utf8
938
1027
  with_kcode('UTF8') do
939
- Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => "短すぎます: %d"
1028
+ Topic.validates_length_of :title, :within => 5..10, :on => :update, :too_short => "短すぎます: %d"
940
1029
 
941
1030
  t = Topic.create("title" => "一二三4", "content" => "whatever")
942
1031
  assert !t.save
@@ -947,12 +1036,11 @@ class ValidationsTest < Test::Unit::TestCase
947
1036
  assert t.errors.on(:title)
948
1037
  assert_equal "短すぎます: 5", t.errors[:title]
949
1038
 
950
- t.title = "valid"
951
- t.content = "一二三四五六七八九十A"
1039
+ t.title = "一二三四五六七八九十A"
952
1040
  assert !t.save
953
- assert t.errors.on(:content)
1041
+ assert t.errors.on(:title)
954
1042
 
955
- t.content = "一二345"
1043
+ t.title = "一二345"
956
1044
  assert t.save
957
1045
  end
958
1046
  end
@@ -1017,7 +1105,7 @@ class ValidationsTest < Test::Unit::TestCase
1017
1105
 
1018
1106
  def test_invalid_validator
1019
1107
  Topic.validate 3
1020
- assert_raise(ActiveRecord::ActiveRecordError) { t = Topic.create }
1108
+ assert_raise(ArgumentError) { t = Topic.create }
1021
1109
  end
1022
1110
 
1023
1111
  def test_throw_away_typing
@@ -1281,7 +1369,7 @@ class ValidationsTest < Test::Unit::TestCase
1281
1369
  end
1282
1370
 
1283
1371
 
1284
- class ValidatesNumericalityTest < Test::Unit::TestCase
1372
+ class ValidatesNumericalityTest < ActiveRecord::TestCase
1285
1373
  NIL = [nil]
1286
1374
  BLANK = ["", " ", " \t \r \n"]
1287
1375
  BIGDECIMAL_STRINGS = %w(12345678901234567890.1234567890) # 30 significent digits
@@ -1293,9 +1381,9 @@ class ValidatesNumericalityTest < Test::Unit::TestCase
1293
1381
  JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
1294
1382
 
1295
1383
  def setup
1296
- Topic.write_inheritable_attribute(:validate, nil)
1297
- Topic.write_inheritable_attribute(:validate_on_create, nil)
1298
- Topic.write_inheritable_attribute(:validate_on_update, nil)
1384
+ Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
1385
+ Topic.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
1386
+ Topic.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
1299
1387
  end
1300
1388
 
1301
1389
  def test_default_validates_numericality_of
@@ -1382,6 +1470,20 @@ class ValidatesNumericalityTest < Test::Unit::TestCase
1382
1470
  valid!([2])
1383
1471
  end
1384
1472
 
1473
+ def test_validates_numericality_with_numeric_message
1474
+ Topic.validates_numericality_of :approved, :less_than => 4, :message => "smaller than %d"
1475
+ topic = Topic.new("title" => "numeric test", "approved" => 10)
1476
+
1477
+ assert !topic.valid?
1478
+ assert_equal "smaller than 4", topic.errors.on(:approved)
1479
+
1480
+ Topic.validates_numericality_of :approved, :greater_than => 4, :message => "greater than %d"
1481
+ topic = Topic.new("title" => "numeric test", "approved" => 1)
1482
+
1483
+ assert !topic.valid?
1484
+ assert_equal "greater than 4", topic.errors.on(:approved)
1485
+ end
1486
+
1385
1487
  private
1386
1488
  def invalid!(values, error=nil)
1387
1489
  with_each_topic_approved_value(values) do |topic, value|
@@ -1,11 +1,11 @@
1
- require 'abstract_unit'
2
- require 'fixtures/contact'
3
- require 'fixtures/post'
4
- require 'fixtures/author'
5
- require 'fixtures/tagging'
6
- require 'fixtures/comment'
7
-
8
- class XmlSerializationTest < Test::Unit::TestCase
1
+ require "cases/helper"
2
+ require 'models/contact'
3
+ require 'models/post'
4
+ require 'models/author'
5
+ require 'models/tagging'
6
+ require 'models/comment'
7
+
8
+ class XmlSerializationTest < ActiveRecord::TestCase
9
9
  def test_should_serialize_default_root
10
10
  @xml = Contact.new.to_xml
11
11
  assert_match %r{^<contact>}, @xml
@@ -40,7 +40,7 @@ class XmlSerializationTest < Test::Unit::TestCase
40
40
  end
41
41
  end
42
42
 
43
- class DefaultXmlSerializationTest < Test::Unit::TestCase
43
+ class DefaultXmlSerializationTest < ActiveRecord::TestCase
44
44
  def setup
45
45
  @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }).to_xml
46
46
  end
@@ -72,7 +72,7 @@ class DefaultXmlSerializationTest < Test::Unit::TestCase
72
72
  end
73
73
  end
74
74
 
75
- class NilXmlSerializationTest < Test::Unit::TestCase
75
+ class NilXmlSerializationTest < ActiveRecord::TestCase
76
76
  def setup
77
77
  @xml = Contact.new.to_xml(:root => 'xml_contact')
78
78
  end
@@ -118,7 +118,7 @@ class NilXmlSerializationTest < Test::Unit::TestCase
118
118
  end
119
119
  end
120
120
 
121
- class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
121
+ class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase
122
122
  fixtures :authors, :posts
123
123
  # to_xml used to mess with the hash the user provided which
124
124
  # caused the builder to be reused. This meant the document kept
@@ -199,4 +199,4 @@ class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
199
199
  assert types.include?('StiPost')
200
200
  end
201
201
 
202
- end
202
+ end
@@ -0,0 +1,5 @@
1
+ TEST_ROOT = File.expand_path(File.dirname(__FILE__))
2
+ ASSETS_ROOT = TEST_ROOT + "/assets"
3
+ FIXTURES_ROOT = TEST_ROOT + "/fixtures"
4
+ MIGRATIONS_ROOT = TEST_ROOT + "/migrations"
5
+ SCHEMA_ROOT = TEST_ROOT + "/schema"
@@ -1,5 +1,5 @@
1
1
  print "Using native DB2\n"
2
- require_dependency 'fixtures/course'
2
+ require_dependency 'models/course'
3
3
  require 'logger'
4
4
 
5
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
@@ -1,5 +1,5 @@
1
1
  print "Using native Firebird\n"
2
- require_dependency 'fixtures/course'
2
+ require_dependency 'models/course'
3
3
  require 'logger'
4
4
 
5
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
@@ -1,5 +1,5 @@
1
1
  puts 'Using native Frontbase'
2
- require_dependency 'fixtures/course'
2
+ require_dependency 'models/course'
3
3
  require 'logger'
4
4
 
5
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
@@ -1,5 +1,5 @@
1
1
  print "Using native MySQL\n"
2
- require_dependency 'fixtures/course'
2
+ require_dependency 'models/course'
3
3
  require 'logger'
4
4
 
5
5
  RAILS_DEFAULT_LOGGER = Logger.new('debug.log')
@@ -1,5 +1,5 @@
1
1
  print "Using native OpenBase\n"
2
- require_dependency 'fixtures/course'
2
+ require_dependency 'models/course'
3
3
  require 'logger'
4
4
 
5
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
@@ -1,5 +1,5 @@
1
1
  print "Using Oracle\n"
2
- require_dependency 'fixtures/course'
2
+ require_dependency 'models/course'
3
3
  require 'logger'
4
4
 
5
5
  ActiveRecord::Base.logger = Logger.new STDOUT
@@ -1,5 +1,5 @@
1
1
  print "Using native PostgreSQL\n"
2
- require_dependency 'fixtures/course'
2
+ require_dependency 'models/course'
3
3
  require 'logger'
4
4
 
5
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
@@ -7,13 +7,11 @@ ActiveRecord::Base.logger = Logger.new("debug.log")
7
7
  ActiveRecord::Base.configurations = {
8
8
  'arunit' => {
9
9
  :adapter => 'postgresql',
10
- :username => 'postgres',
11
10
  :database => 'activerecord_unittest',
12
11
  :min_messages => 'warning'
13
12
  },
14
13
  'arunit2' => {
15
14
  :adapter => 'postgresql',
16
- :username => 'postgres',
17
15
  :database => 'activerecord_unittest2',
18
16
  :min_messages => 'warning'
19
17
  }
@@ -1,12 +1,12 @@
1
1
  print "Using native SQlite\n"
2
- require_dependency 'fixtures/course'
2
+ require_dependency 'models/course'
3
3
  require 'logger'
4
4
  ActiveRecord::Base.logger = Logger.new("debug.log")
5
5
 
6
6
  class SqliteError < StandardError
7
7
  end
8
8
 
9
- BASE_DIR = File.expand_path(File.dirname(__FILE__) + '/../../fixtures')
9
+ BASE_DIR = FIXTURES_ROOT
10
10
  sqlite_test_db = "#{BASE_DIR}/fixture_database.sqlite"
11
11
  sqlite_test_db2 = "#{BASE_DIR}/fixture_database_2.sqlite"
12
12