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,8 +1,8 @@
1
1
  module ActiveRecord
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 2
4
- MINOR = 0
5
- TINY = 5
4
+ MINOR = 1
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
File without changes
File without changes
@@ -0,0 +1,24 @@
1
+ # The filename begins with "aaa" to ensure this is the first test.
2
+ require "cases/helper"
3
+
4
+ class AAACreateTablesTest < ActiveRecord::TestCase
5
+ self.use_transactional_fixtures = false
6
+
7
+ def test_load_schema
8
+ eval(File.read(SCHEMA_ROOT + "/schema.rb"))
9
+ if File.exists?(adapter_specific_schema_file)
10
+ eval(File.read(adapter_specific_schema_file))
11
+ end
12
+ assert true
13
+ end
14
+
15
+ def test_drop_and_create_courses_table
16
+ eval(File.read(SCHEMA_ROOT + "/schema2.rb"))
17
+ assert true
18
+ end
19
+
20
+ private
21
+ def adapter_specific_schema_file
22
+ SCHEMA_ROOT + '/' + ActiveRecord::Base.connection.adapter_name.downcase + '_specific_schema.rb'
23
+ end
24
+ end
@@ -0,0 +1,95 @@
1
+ require "cases/helper"
2
+
3
+ class ActiveSchemaTest < ActiveRecord::TestCase
4
+ def setup
5
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
6
+ alias_method :execute_without_stub, :execute
7
+ def execute(sql, name = nil) return sql end
8
+ end
9
+ end
10
+
11
+ def teardown
12
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
13
+ remove_method :execute
14
+ alias_method :execute, :execute_without_stub
15
+ end
16
+ end
17
+
18
+ def test_drop_table
19
+ assert_equal "DROP TABLE `people`", drop_table(:people)
20
+ end
21
+
22
+ if current_adapter?(:MysqlAdapter)
23
+ def test_create_mysql_database_with_encoding
24
+ assert_equal "CREATE DATABASE `matt` DEFAULT CHARACTER SET `utf8`", create_database(:matt)
25
+ assert_equal "CREATE DATABASE `aimonetti` DEFAULT CHARACTER SET `latin1`", create_database(:aimonetti, {:charset => 'latin1'})
26
+ assert_equal "CREATE DATABASE `matt_aimonetti` DEFAULT CHARACTER SET `big5` COLLATE `big5_chinese_ci`", create_database(:matt_aimonetti, {:charset => :big5, :collation => :big5_chinese_ci})
27
+ end
28
+ end
29
+
30
+ def test_add_column
31
+ assert_equal "ALTER TABLE `people` ADD `last_name` varchar(255)", add_column(:people, :last_name, :string)
32
+ end
33
+
34
+ def test_add_column_with_limit
35
+ assert_equal "ALTER TABLE `people` ADD `key` varchar(32)", add_column(:people, :key, :string, :limit => 32)
36
+ end
37
+
38
+ def test_drop_table_with_specific_database
39
+ assert_equal "DROP TABLE `otherdb`.`people`", drop_table('otherdb.people')
40
+ end
41
+
42
+ def test_add_timestamps
43
+ with_real_execute do
44
+ begin
45
+ ActiveRecord::Base.connection.create_table :delete_me do |t|
46
+ end
47
+ ActiveRecord::Base.connection.add_timestamps :delete_me
48
+ assert column_present?('delete_me', 'updated_at', 'datetime')
49
+ assert column_present?('delete_me', 'created_at', 'datetime')
50
+ ensure
51
+ ActiveRecord::Base.connection.drop_table :delete_me rescue nil
52
+ end
53
+ end
54
+ end
55
+
56
+ def test_remove_timestamps
57
+ with_real_execute do
58
+ begin
59
+ ActiveRecord::Base.connection.create_table :delete_me do |t|
60
+ t.timestamps
61
+ end
62
+ ActiveRecord::Base.connection.remove_timestamps :delete_me
63
+ assert !column_present?('delete_me', 'updated_at', 'datetime')
64
+ assert !column_present?('delete_me', 'created_at', 'datetime')
65
+ ensure
66
+ ActiveRecord::Base.connection.drop_table :delete_me rescue nil
67
+ end
68
+ end
69
+ end
70
+
71
+ private
72
+ def with_real_execute
73
+ #we need to actually modify some data, so we make execute point to the original method
74
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
75
+ alias_method :execute_with_stub, :execute
76
+ alias_method :execute, :execute_without_stub
77
+ end
78
+ yield
79
+ ensure
80
+ #before finishing, we restore the alias to the mock-up method
81
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
82
+ alias_method :execute, :execute_with_stub
83
+ end
84
+ end
85
+
86
+
87
+ def method_missing(method_symbol, *arguments)
88
+ ActiveRecord::Base.connection.send(method_symbol, *arguments)
89
+ end
90
+
91
+ def column_present?(table_name, column_name, type)
92
+ results = ActiveRecord::Base.connection.select_all("SHOW FIELDS FROM #{table_name} LIKE '#{column_name}'")
93
+ results.first && results.first['Type'] == type
94
+ end
95
+ end
@@ -0,0 +1,24 @@
1
+ require 'cases/helper'
2
+
3
+ class PostgresqlActiveSchemaTest < Test::Unit::TestCase
4
+ def setup
5
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
6
+ alias_method :real_execute, :execute
7
+ def execute(sql, name = nil) sql end
8
+ end
9
+ end
10
+
11
+ def teardown
12
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:alias_method, :execute, :real_execute)
13
+ end
14
+
15
+ def test_create_database_with_encoding
16
+ assert_equal "CREATE DATABASE matt ENCODING = 'utf8'", create_database(:matt)
17
+ assert_equal "CREATE DATABASE aimonetti ENCODING = 'latin1'", create_database(:aimonetti, :encoding => :latin1)
18
+ end
19
+
20
+ private
21
+ def method_missing(method_symbol, *arguments)
22
+ ActiveRecord::Base.connection.send(method_symbol, *arguments)
23
+ end
24
+ end
@@ -1,20 +1,21 @@
1
- require 'abstract_unit'
1
+ require "cases/helper"
2
2
 
3
- class AdapterTest < Test::Unit::TestCase
3
+ class AdapterTest < ActiveRecord::TestCase
4
4
  def setup
5
5
  @connection = ActiveRecord::Base.connection
6
6
  end
7
7
 
8
8
  def test_tables
9
- if @connection.respond_to?(:tables)
10
- tables = @connection.tables
11
- assert tables.include?("accounts")
12
- assert tables.include?("authors")
13
- assert tables.include?("tasks")
14
- assert tables.include?("topics")
15
- else
16
- warn "#{@connection.class} does not respond to #tables"
17
- end
9
+ tables = @connection.tables
10
+ assert tables.include?("accounts")
11
+ assert tables.include?("authors")
12
+ assert tables.include?("tasks")
13
+ assert tables.include?("topics")
14
+ end
15
+
16
+ def test_table_exists?
17
+ assert @connection.table_exists?("accounts")
18
+ assert !@connection.table_exists?("nonexistingtable")
18
19
  end
19
20
 
20
21
  def test_indexes
@@ -83,8 +84,8 @@ class AdapterTest < Test::Unit::TestCase
83
84
 
84
85
  # test resetting sequences in odd tables in postgreSQL
85
86
  if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
86
- require 'fixtures/movie'
87
- require 'fixtures/subscriber'
87
+ require 'models/movie'
88
+ require 'models/subscriber'
88
89
 
89
90
  def test_reset_empty_table_with_custom_pk
90
91
  Movie.delete_all
@@ -117,7 +118,7 @@ class AdapterTest < Test::Unit::TestCase
117
118
  sql_inject = "1, 7 procedure help()"
118
119
  if current_adapter?(:MysqlAdapter)
119
120
  assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject)
120
- assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit=> '1 ; DROP TABLE USERS', :offset=>7)
121
+ assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
121
122
  else
122
123
  assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject)
123
124
  assert_equal " LIMIT 1,7 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
@@ -1,95 +1,95 @@
1
- require 'abstract_unit'
2
- require 'fixtures/default'
3
- require 'fixtures/post'
4
- require 'fixtures/task'
5
-
6
- class SqlServerAdapterTest < Test::Unit::TestCase
7
- class TableWithRealColumn < ActiveRecord::Base; end
8
-
9
- fixtures :posts, :tasks
10
-
11
- def setup
12
- @connection = ActiveRecord::Base.connection
13
- end
14
-
15
- def teardown
16
- @connection.execute("SET LANGUAGE us_english") rescue nil
17
- end
18
-
19
- def test_real_column_has_float_type
20
- assert_equal :float, TableWithRealColumn.columns_hash["real_number"].type
21
- end
22
-
23
- # SQL Server 2000 has a bug where some unambiguous date formats are not
24
- # correctly identified if the session language is set to german
25
- def test_date_insertion_when_language_is_german
26
- @connection.execute("SET LANGUAGE deutsch")
27
-
28
- assert_nothing_raised do
29
- Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31))
30
- end
31
- end
32
-
33
- def test_indexes_with_descending_order
34
- # Make sure we have an index with descending order
35
- @connection.execute "CREATE INDEX idx_credit_limit ON accounts (credit_limit DESC)" rescue nil
36
- assert_equal ["credit_limit"], @connection.indexes('accounts').first.columns
37
- ensure
38
- @connection.execute "DROP INDEX accounts.idx_credit_limit"
39
- end
40
-
41
- def test_execute_without_block_closes_statement
42
- assert_all_statements_used_are_closed do
43
- @connection.execute("SELECT 1")
44
- end
45
- end
46
-
47
- def test_execute_with_block_closes_statement
48
- assert_all_statements_used_are_closed do
49
- @connection.execute("SELECT 1") do |sth|
50
- assert !sth.finished?, "Statement should still be alive within block"
51
- end
52
- end
53
- end
54
-
55
- def test_insert_with_identity_closes_statement
56
- assert_all_statements_used_are_closed do
57
- @connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)")
58
- end
59
- end
60
-
61
- def test_insert_without_identity_closes_statement
62
- assert_all_statements_used_are_closed do
63
- @connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)")
64
- end
65
- end
66
-
67
- def test_active_closes_statement
68
- assert_all_statements_used_are_closed do
69
- @connection.active?
70
- end
71
- end
72
-
73
- def assert_all_statements_used_are_closed(&block)
74
- existing_handles = []
75
- ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle}
76
- GC.disable
77
-
78
- yield
79
-
80
- used_handles = []
81
- ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle}
82
-
83
- assert_block "No statements were used within given block" do
84
- used_handles.size > 0
85
- end
86
-
87
- ObjectSpace.each_object(DBI::StatementHandle) do |handle|
88
- assert_block "Statement should have been closed within given block" do
89
- handle.finished?
90
- end
91
- end
92
- ensure
93
- GC.enable
94
- end
95
- end
1
+ require "cases/helper"
2
+ require 'models/default'
3
+ require 'models/post'
4
+ require 'models/task'
5
+
6
+ class SqlServerAdapterTest < ActiveRecord::TestCase
7
+ class TableWithRealColumn < ActiveRecord::Base; end
8
+
9
+ fixtures :posts, :tasks
10
+
11
+ def setup
12
+ @connection = ActiveRecord::Base.connection
13
+ end
14
+
15
+ def teardown
16
+ @connection.execute("SET LANGUAGE us_english") rescue nil
17
+ end
18
+
19
+ def test_real_column_has_float_type
20
+ assert_equal :float, TableWithRealColumn.columns_hash["real_number"].type
21
+ end
22
+
23
+ # SQL Server 2000 has a bug where some unambiguous date formats are not
24
+ # correctly identified if the session language is set to german
25
+ def test_date_insertion_when_language_is_german
26
+ @connection.execute("SET LANGUAGE deutsch")
27
+
28
+ assert_nothing_raised do
29
+ Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31))
30
+ end
31
+ end
32
+
33
+ def test_indexes_with_descending_order
34
+ # Make sure we have an index with descending order
35
+ @connection.execute "CREATE INDEX idx_credit_limit ON accounts (credit_limit DESC)" rescue nil
36
+ assert_equal ["credit_limit"], @connection.indexes('accounts').first.columns
37
+ ensure
38
+ @connection.execute "DROP INDEX accounts.idx_credit_limit"
39
+ end
40
+
41
+ def test_execute_without_block_closes_statement
42
+ assert_all_statements_used_are_closed do
43
+ @connection.execute("SELECT 1")
44
+ end
45
+ end
46
+
47
+ def test_execute_with_block_closes_statement
48
+ assert_all_statements_used_are_closed do
49
+ @connection.execute("SELECT 1") do |sth|
50
+ assert !sth.finished?, "Statement should still be alive within block"
51
+ end
52
+ end
53
+ end
54
+
55
+ def test_insert_with_identity_closes_statement
56
+ assert_all_statements_used_are_closed do
57
+ @connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)")
58
+ end
59
+ end
60
+
61
+ def test_insert_without_identity_closes_statement
62
+ assert_all_statements_used_are_closed do
63
+ @connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)")
64
+ end
65
+ end
66
+
67
+ def test_active_closes_statement
68
+ assert_all_statements_used_are_closed do
69
+ @connection.active?
70
+ end
71
+ end
72
+
73
+ def assert_all_statements_used_are_closed(&block)
74
+ existing_handles = []
75
+ ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle}
76
+ GC.disable
77
+
78
+ yield
79
+
80
+ used_handles = []
81
+ ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle}
82
+
83
+ assert_block "No statements were used within given block" do
84
+ used_handles.size > 0
85
+ end
86
+
87
+ ObjectSpace.each_object(DBI::StatementHandle) do |handle|
88
+ assert_block "Statement should have been closed within given block" do
89
+ handle.finished?
90
+ end
91
+ end
92
+ ensure
93
+ GC.enable
94
+ end
95
+ end
@@ -1,7 +1,7 @@
1
- require 'abstract_unit'
2
- require 'fixtures/customer'
1
+ require "cases/helper"
2
+ require 'models/customer'
3
3
 
4
- class AggregationsTest < Test::Unit::TestCase
4
+ class AggregationsTest < ActiveRecord::TestCase
5
5
  fixtures :customers
6
6
 
7
7
  def test_find_single_value_object
@@ -9,36 +9,36 @@ class AggregationsTest < Test::Unit::TestCase
9
9
  assert_kind_of Money, customers(:david).balance
10
10
  assert_equal 300, customers(:david).balance.exchange_to("DKK").amount
11
11
  end
12
-
12
+
13
13
  def test_find_multiple_value_object
14
14
  assert_equal customers(:david).address_street, customers(:david).address.street
15
15
  assert(
16
16
  customers(:david).address.close_to?(Address.new("Different Street", customers(:david).address_city, customers(:david).address_country))
17
17
  )
18
18
  end
19
-
19
+
20
20
  def test_change_single_value_object
21
21
  customers(:david).balance = Money.new(100)
22
22
  customers(:david).save
23
23
  assert_equal 100, customers(:david).reload.balance.amount
24
24
  end
25
-
25
+
26
26
  def test_immutable_value_objects
27
27
  customers(:david).balance = Money.new(100)
28
- assert_raises(TypeError) { customers(:david).balance.instance_eval { @amount = 20 } }
29
- end
30
-
28
+ assert_raise(ActiveSupport::FrozenObjectError) { customers(:david).balance.instance_eval { @amount = 20 } }
29
+ end
30
+
31
31
  def test_inferred_mapping
32
32
  assert_equal "35.544623640962634", customers(:david).gps_location.latitude
33
33
  assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
34
-
34
+
35
35
  customers(:david).gps_location = GpsLocation.new("39x-110")
36
36
 
37
37
  assert_equal "39", customers(:david).gps_location.latitude
38
38
  assert_equal "-110", customers(:david).gps_location.longitude
39
-
39
+
40
40
  customers(:david).save
41
-
41
+
42
42
  customers(:david).reload
43
43
 
44
44
  assert_equal "39", customers(:david).gps_location.latitude
@@ -53,7 +53,7 @@ class AggregationsTest < Test::Unit::TestCase
53
53
  customers(:david).reload
54
54
  assert_equal '24x113', customers(:david)['gps_location']
55
55
 
56
- assert_equal GpsLocation.new('24x113'), customers(:david).gps_location
56
+ assert_equal GpsLocation.new('24x113'), customers(:david).gps_location
57
57
  end
58
58
 
59
59
  def test_gps_equality
@@ -63,34 +63,34 @@ class AggregationsTest < Test::Unit::TestCase
63
63
  def test_gps_inequality
64
64
  assert GpsLocation.new('39x110') != GpsLocation.new('39x111')
65
65
  end
66
-
66
+
67
67
  def test_allow_nil_gps_is_nil
68
68
  assert_equal nil, customers(:zaphod).gps_location
69
69
  end
70
-
70
+
71
71
  def test_allow_nil_gps_set_to_nil
72
72
  customers(:david).gps_location = nil
73
73
  customers(:david).save
74
74
  customers(:david).reload
75
75
  assert_equal nil, customers(:david).gps_location
76
76
  end
77
-
77
+
78
78
  def test_allow_nil_set_address_attributes_to_nil
79
79
  customers(:zaphod).address = nil
80
80
  assert_equal nil, customers(:zaphod).attributes[:address_street]
81
81
  assert_equal nil, customers(:zaphod).attributes[:address_city]
82
82
  assert_equal nil, customers(:zaphod).attributes[:address_country]
83
83
  end
84
-
84
+
85
85
  def test_allow_nil_address_set_to_nil
86
86
  customers(:zaphod).address = nil
87
87
  customers(:zaphod).save
88
88
  customers(:zaphod).reload
89
89
  assert_equal nil, customers(:zaphod).address
90
90
  end
91
-
91
+
92
92
  def test_nil_raises_error_when_allow_nil_is_false
93
- assert_raises(NoMethodError) { customers(:david).balance = nil }
93
+ assert_raise(NoMethodError) { customers(:david).balance = nil }
94
94
  end
95
95
 
96
96
  def test_allow_nil_address_loaded_when_only_some_attributes_are_nil
@@ -109,7 +109,7 @@ class AggregationsTest < Test::Unit::TestCase
109
109
  end
110
110
  end
111
111
 
112
- class OverridingAggregationsTest < Test::Unit::TestCase
112
+ class OverridingAggregationsTest < ActiveRecord::TestCase
113
113
  class Name; end
114
114
  class DifferentName; end
115
115