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,14 +1,14 @@
1
- require 'abstract_unit'
2
- require 'fixtures/post'
3
- require 'fixtures/comment'
4
- require 'fixtures/author'
5
- require 'fixtures/category'
6
- require 'fixtures/categorization'
7
- require 'fixtures/company'
8
- require 'fixtures/topic'
9
- require 'fixtures/reply'
1
+ require "cases/helper"
2
+ require 'models/post'
3
+ require 'models/comment'
4
+ require 'models/author'
5
+ require 'models/category'
6
+ require 'models/categorization'
7
+ require 'models/company'
8
+ require 'models/topic'
9
+ require 'models/reply'
10
10
 
11
- class CascadedEagerLoadingTest < Test::Unit::TestCase
11
+ class CascadedEagerLoadingTest < ActiveRecord::TestCase
12
12
  fixtures :authors, :mixins, :companies, :posts, :topics
13
13
 
14
14
  def test_eager_association_loading_with_cascaded_two_levels
@@ -61,16 +61,17 @@ class CascadedEagerLoadingTest < Test::Unit::TestCase
61
61
 
62
62
  def test_eager_association_loading_with_has_many_sti
63
63
  topics = Topic.find(:all, :include => :replies, :order => 'topics.id')
64
- assert_equal topics(:first, :second), topics
64
+ first, second, = topics(:first).replies.size, topics(:second).replies.size
65
65
  assert_no_queries do
66
- assert_equal 1, topics[0].replies.size
67
- assert_equal 0, topics[1].replies.size
66
+ assert_equal first, topics[0].replies.size
67
+ assert_equal second, topics[1].replies.size
68
68
  end
69
69
  end
70
70
 
71
71
  def test_eager_association_loading_with_belongs_to_sti
72
72
  replies = Reply.find(:all, :include => :topic, :order => 'topics.id')
73
- assert_equal [topics(:second)], replies
73
+ assert replies.include?(topics(:second))
74
+ assert !replies.include?(topics(:first))
74
75
  assert_equal topics(:first), assert_no_queries { replies.first.topic }
75
76
  end
76
77
 
@@ -93,9 +94,9 @@ class CascadedEagerLoadingTest < Test::Unit::TestCase
93
94
  end
94
95
  end
95
96
 
96
- require 'fixtures/vertex'
97
- require 'fixtures/edge'
98
- class CascadedEagerLoadingTest < Test::Unit::TestCase
97
+ require 'models/vertex'
98
+ require 'models/edge'
99
+ class CascadedEagerLoadingTest < ActiveRecord::TestCase
99
100
  fixtures :edges, :vertices
100
101
 
101
102
  def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
@@ -0,0 +1,83 @@
1
+ require 'cases/helper'
2
+
3
+
4
+ class ShapeExpression < ActiveRecord::Base
5
+ belongs_to :shape, :polymorphic => true
6
+ belongs_to :paint, :polymorphic => true
7
+ end
8
+
9
+ class Circle < ActiveRecord::Base
10
+ has_many :shape_expressions, :as => :shape
11
+ end
12
+ class Square < ActiveRecord::Base
13
+ has_many :shape_expressions, :as => :shape
14
+ end
15
+ class Triangle < ActiveRecord::Base
16
+ has_many :shape_expressions, :as => :shape
17
+ end
18
+ class PaintColor < ActiveRecord::Base
19
+ has_many :shape_expressions, :as => :paint
20
+ belongs_to :non_poly, :foreign_key => "non_poly_one_id", :class_name => "NonPolyOne"
21
+ end
22
+ class PaintTexture < ActiveRecord::Base
23
+ has_many :shape_expressions, :as => :paint
24
+ belongs_to :non_poly, :foreign_key => "non_poly_two_id", :class_name => "NonPolyTwo"
25
+ end
26
+ class NonPolyOne < ActiveRecord::Base
27
+ has_many :paint_colors
28
+ end
29
+ class NonPolyTwo < ActiveRecord::Base
30
+ has_many :paint_textures
31
+ end
32
+
33
+
34
+
35
+ class EagerLoadPolyAssocsTest < ActiveRecord::TestCase
36
+ NUM_SIMPLE_OBJS = 50
37
+ NUM_SHAPE_EXPRESSIONS = 100
38
+
39
+ def setup
40
+ generate_test_object_graphs
41
+ end
42
+
43
+ def teardown
44
+ [Circle, Square, Triangle, PaintColor, PaintTexture,
45
+ ShapeExpression, NonPolyOne, NonPolyTwo].each do |c|
46
+ c.delete_all
47
+ end
48
+
49
+ end
50
+
51
+
52
+ # meant to be supplied as an ID, never returns 0
53
+ def rand_simple
54
+ val = (NUM_SIMPLE_OBJS * rand).round
55
+ val == 0 ? 1 : val
56
+ end
57
+
58
+ def generate_test_object_graphs
59
+ 1.upto(NUM_SIMPLE_OBJS) do
60
+ [Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
61
+ end
62
+ 1.upto(NUM_SIMPLE_OBJS) do |i|
63
+ PaintColor.create!(:non_poly_one_id => rand_simple)
64
+ PaintTexture.create!(:non_poly_two_id => rand_simple)
65
+ end
66
+ 1.upto(NUM_SHAPE_EXPRESSIONS) do |i|
67
+ ShapeExpression.create!(:shape_type => [Circle, Square, Triangle].rand.to_s, :shape_id => rand_simple,
68
+ :paint_type => [PaintColor, PaintTexture].rand.to_s, :paint_id => rand_simple)
69
+ end
70
+ end
71
+
72
+ def test_include_query
73
+ res = 0
74
+ res = ShapeExpression.find :all, :include => [ :shape, { :paint => :non_poly } ]
75
+ assert_equal NUM_SHAPE_EXPRESSIONS, res.size
76
+ assert_queries(0) do
77
+ res.each do |se|
78
+ assert_not_nil se.paint.non_poly, "this is the association that was loading incorrectly before the change"
79
+ assert_not_nil se.shape, "just making sure other associations still work"
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,4 +1,4 @@
1
- require 'abstract_unit'
1
+ require "cases/helper"
2
2
 
3
3
  class Virus < ActiveRecord::Base
4
4
  belongs_to :octopus
@@ -39,7 +39,7 @@ class Compress < ActiveRecord::Base
39
39
  end
40
40
 
41
41
 
42
- class EagerSingularizationTest < Test::Unit::TestCase
42
+ class EagerSingularizationTest < ActiveRecord::TestCase
43
43
 
44
44
  def setup
45
45
  if ActiveRecord::Base.connection.supports_migrations?
@@ -85,7 +85,7 @@ class EagerSingularizationTest < Test::Unit::TestCase
85
85
  @have_tables = false
86
86
  end
87
87
  end
88
-
88
+
89
89
  def teardown
90
90
  ActiveRecord::Base.connection.drop_table :viri
91
91
  ActiveRecord::Base.connection.drop_table :octopi
@@ -99,7 +99,7 @@ class EagerSingularizationTest < Test::Unit::TestCase
99
99
  ActiveRecord::Base.connection.drop_table :dresses
100
100
  ActiveRecord::Base.connection.drop_table :compresses
101
101
  end
102
-
102
+
103
103
  def test_eager_no_extra_singularization_belongs_to
104
104
  return unless @have_tables
105
105
  assert_nothing_raised do
@@ -135,7 +135,7 @@ class EagerSingularizationTest < Test::Unit::TestCase
135
135
  Crisis.find(:all, :include => :successes)
136
136
  end
137
137
  end
138
-
138
+
139
139
  def test_eager_no_extra_singularization_has_many_through_has_many
140
140
  return unless @have_tables
141
141
  assert_nothing_raised do
@@ -1,15 +1,24 @@
1
- require 'abstract_unit'
2
- require 'fixtures/post'
3
- require 'fixtures/comment'
4
- require 'fixtures/author'
5
- require 'fixtures/category'
6
- require 'fixtures/company'
7
- require 'fixtures/person'
8
- require 'fixtures/reader'
9
-
10
- class EagerAssociationTest < Test::Unit::TestCase
1
+ require "cases/helper"
2
+ require 'models/post'
3
+ require 'models/tagging'
4
+ require 'models/comment'
5
+ require 'models/author'
6
+ require 'models/category'
7
+ require 'models/company'
8
+ require 'models/person'
9
+ require 'models/reader'
10
+ require 'models/owner'
11
+ require 'models/pet'
12
+ require 'models/reference'
13
+ require 'models/job'
14
+ require 'models/subscriber'
15
+ require 'models/subscription'
16
+ require 'models/book'
17
+
18
+ class EagerAssociationTest < ActiveRecord::TestCase
11
19
  fixtures :posts, :comments, :authors, :categories, :categories_posts,
12
- :companies, :accounts, :tags, :people, :readers
20
+ :companies, :accounts, :tags, :taggings, :people, :readers,
21
+ :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books
13
22
 
14
23
  def test_loading_with_one_association
15
24
  posts = Post.find(:all, :include => :comments)
@@ -20,6 +29,10 @@ class EagerAssociationTest < Test::Unit::TestCase
20
29
  post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
21
30
  assert_equal 2, post.comments.size
22
31
  assert post.comments.include?(comments(:greetings))
32
+
33
+ posts = Post.find(:all, :include => :last_comment)
34
+ post = posts.find { |p| p.id == 1 }
35
+ assert_equal Post.find(1).last_comment, post.last_comment
23
36
  end
24
37
 
25
38
  def test_loading_conditions_with_or
@@ -56,6 +69,39 @@ class EagerAssociationTest < Test::Unit::TestCase
56
69
  assert posts.first.comments.include?(comments(:greetings))
57
70
  end
58
71
 
72
+ def test_duplicate_middle_objects
73
+ comments = Comment.find :all, :conditions => 'post_id = 1', :include => [:post => :author]
74
+ assert_no_queries do
75
+ comments.each {|comment| comment.post.author.name}
76
+ end
77
+ end
78
+
79
+ def test_including_duplicate_objects_from_belongs_to
80
+ popular_post = Post.create!(:title => 'foo', :body => "I like cars!")
81
+ comment = popular_post.comments.create!(:body => "lol")
82
+ popular_post.readers.create!(:person => people(:michael))
83
+ popular_post.readers.create!(:person => people(:david))
84
+
85
+ readers = Reader.find(:all, :conditions => ["post_id = ?", popular_post.id],
86
+ :include => {:post => :comments})
87
+ readers.each do |reader|
88
+ assert_equal [comment], reader.post.comments
89
+ end
90
+ end
91
+
92
+ def test_including_duplicate_objects_from_has_many
93
+ car_post = Post.create!(:title => 'foo', :body => "I like cars!")
94
+ car_post.categories << categories(:general)
95
+ car_post.categories << categories(:technology)
96
+
97
+ comment = car_post.comments.create!(:body => "hmm")
98
+ categories = Category.find(:all, :conditions => ["posts.id=?", car_post.id],
99
+ :include => {:posts => :comments})
100
+ categories.each do |category|
101
+ assert_equal [comment], category.posts[0].comments
102
+ end
103
+ end
104
+
59
105
  def test_loading_from_an_association
60
106
  posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
61
107
  assert_equal 2, posts.first.comments.size
@@ -65,6 +111,17 @@ class EagerAssociationTest < Test::Unit::TestCase
65
111
  assert_nil Post.find(posts(:authorless).id, :include => :author).author
66
112
  end
67
113
 
114
+ def test_nested_loading_with_no_associations
115
+ assert_nothing_raised do
116
+ Post.find(posts(:authorless).id, :include => {:author => :author_addresss})
117
+ end
118
+ end
119
+
120
+ def test_eager_association_loading_with_belongs_to_and_foreign_keys
121
+ pets = Pet.find(:all, :include => :owner)
122
+ assert_equal 3, pets.length
123
+ end
124
+
68
125
  def test_eager_association_loading_with_belongs_to
69
126
  comments = Comment.find(:all, :include => :post)
70
127
  assert_equal 10, comments.length
@@ -72,7 +129,7 @@ class EagerAssociationTest < Test::Unit::TestCase
72
129
  assert titles.include?(posts(:welcome).title)
73
130
  assert titles.include?(posts(:sti_post_and_comments).title)
74
131
  end
75
-
132
+
76
133
  def test_eager_association_loading_with_belongs_to_and_limit
77
134
  comments = Comment.find(:all, :include => :post, :limit => 5, :order => 'comments.id')
78
135
  assert_equal 5, comments.length
@@ -96,35 +153,103 @@ class EagerAssociationTest < Test::Unit::TestCase
96
153
  assert_equal 3, comments.length
97
154
  assert_equal [6,7,8], comments.collect { |c| c.id }
98
155
  end
99
-
156
+
100
157
  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
101
158
  comments = Comment.find(:all, :include => :post, :conditions => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id')
102
159
  assert_equal 3, comments.length
103
160
  assert_equal [6,7,8], comments.collect { |c| c.id }
104
161
  end
105
162
 
163
+ def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
164
+ assert_nothing_raised do
165
+ Comment.find(:all, :include => :post, :conditions => ['posts.id = ?',4])
166
+ end
167
+ end
168
+
169
+ def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
170
+ quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
171
+ assert_nothing_raised do
172
+ Comment.find(:all, :include => :post, :conditions => ["#{quoted_posts_id} = ?",4])
173
+ end
174
+ end
175
+
176
+ def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
177
+ assert_nothing_raised do
178
+ Comment.find(:all, :include => :post, :order => 'posts.id')
179
+ end
180
+ end
181
+
182
+ def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
183
+ quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
184
+ assert_nothing_raised do
185
+ Comment.find(:all, :include => :post, :order => quoted_posts_id)
186
+ end
187
+ end
188
+
106
189
  def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
107
190
  posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
108
191
  assert_equal 1, posts.length
109
192
  assert_equal [1], posts.collect { |p| p.id }
110
193
  end
111
-
194
+
112
195
  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
113
196
  posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id')
114
197
  assert_equal 1, posts.length
115
198
  assert_equal [2], posts.collect { |p| p.id }
116
199
  end
117
-
200
+
118
201
  def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
119
202
  author_favorite = AuthorFavorite.find(:first, :include => :favorite_author)
120
203
  assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
121
204
  end
122
205
 
206
+ def test_eager_load_belongs_to_quotes_table_and_column_names
207
+ job = Job.find jobs(:unicyclist).id, :include => :ideal_reference
208
+ references(:michael_unicyclist)
209
+ assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
210
+ end
211
+
212
+ def test_eager_load_has_one_quotes_table_and_column_names
213
+ michael = Person.find(people(:michael), :include => :favourite_reference)
214
+ references(:michael_unicyclist)
215
+ assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
216
+ end
217
+
218
+ def test_eager_load_has_many_quotes_table_and_column_names
219
+ michael = Person.find(people(:michael), :include => :references)
220
+ references(:michael_magician,:michael_unicyclist)
221
+ assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) }
222
+ end
223
+
224
+ def test_eager_load_has_many_through_quotes_table_and_column_names
225
+ michael = Person.find(people(:michael), :include => :jobs)
226
+ jobs(:magician, :unicyclist)
227
+ assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) }
228
+ end
229
+
230
+ def test_eager_load_has_many_with_string_keys
231
+ subscriptions = subscriptions(:webster_awdr, :webster_rfr)
232
+ subscriber =Subscriber.find(subscribers(:second).id, :include => :subscriptions)
233
+ assert_equal subscriptions, subscriber.subscriptions.sort_by(&:id)
234
+ end
235
+
236
+ def test_eager_load_has_many_through_with_string_keys
237
+ books = books(:awdr, :rfr)
238
+ subscriber = Subscriber.find(subscribers(:second).id, :include => :books)
239
+ assert_equal books, subscriber.books.sort_by(&:id)
240
+ end
241
+
242
+ def test_eager_load_belongs_to_with_string_keys
243
+ subscriber = subscribers(:second)
244
+ subscription = Subscription.find(subscriptions(:webster_awdr).id, :include => :subscriber)
245
+ assert_equal subscriber, subscription.subscriber
246
+ end
247
+
123
248
  def test_eager_association_loading_with_explicit_join
124
249
  posts = Post.find(:all, :include => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id')
125
250
  assert_equal 1, posts.length
126
251
  end
127
-
252
+
128
253
  def test_eager_with_has_many_through
129
254
  posts_with_comments = people(:michael).posts.find(:all, :include => :comments)
130
255
  posts_with_author = people(:michael).posts.find(:all, :include => :author )
@@ -138,7 +263,7 @@ class EagerAssociationTest < Test::Unit::TestCase
138
263
  author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id')
139
264
  assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
140
265
  end
141
-
266
+
142
267
  def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
143
268
  author = Author.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
144
269
  assert_equal [], author.special_nonexistant_post_comments
@@ -150,6 +275,17 @@ class EagerAssociationTest < Test::Unit::TestCase
150
275
  Author.find(:first, :order => 'authors.id').hello_post_comments.sort_by(&:id)
151
276
  end
152
277
 
278
+ def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
279
+ assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
280
+ end
281
+
282
+ def test_eager_with_has_many_through_join_model_with_include
283
+ author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
284
+ assert_no_queries do
285
+ author_comments.first.post.title
286
+ end
287
+ end
288
+
153
289
  def test_eager_with_has_many_and_limit
154
290
  posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
155
291
  assert_equal 2, posts.size
@@ -173,13 +309,13 @@ class EagerAssociationTest < Test::Unit::TestCase
173
309
  posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id")
174
310
  end
175
311
  assert_equal 2, posts.size
176
- assert_equal [4,5], posts.collect { |p| p.id }
312
+ assert_equal [4,5], posts.collect { |p| p.id }
177
313
  end
178
314
 
179
315
  def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
180
316
  posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
181
317
  assert_equal 2, posts.size
182
-
318
+
183
319
  count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
184
320
  assert_equal count, posts.size
185
321
  end
@@ -198,12 +334,18 @@ class EagerAssociationTest < Test::Unit::TestCase
198
334
  posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'")
199
335
  assert_equal 0, posts.size
200
336
  end
201
-
337
+
202
338
  def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
203
339
  author = authors(:david)
204
340
  author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
205
341
  assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null')
206
342
  end
343
+
344
+ def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
345
+ person = people(:michael)
346
+ person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
347
+ assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
348
+ end
207
349
 
208
350
  def test_eager_with_has_and_belongs_to_many_and_limit
209
351
  posts = Post.find(:all, :include => :categories, :order => "posts.id", :limit => 3)
@@ -216,15 +358,15 @@ class EagerAssociationTest < Test::Unit::TestCase
216
358
  end
217
359
 
218
360
  def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
219
- posts = authors(:david).posts.find(:all,
220
- :include => :comments,
361
+ posts = authors(:david).posts.find(:all,
362
+ :include => :comments,
221
363
  :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
222
364
  :limit => 2
223
365
  )
224
366
  assert_equal 2, posts.size
225
-
367
+
226
368
  count = Post.count(
227
- :include => [ :comments, :author ],
369
+ :include => [ :comments, :author ],
228
370
  :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
229
371
  :limit => 2
230
372
  )
@@ -234,15 +376,15 @@ class EagerAssociationTest < Test::Unit::TestCase
234
376
  def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
235
377
  posts = nil
236
378
  Post.with_scope(:find => {
237
- :include => :comments,
379
+ :include => :comments,
238
380
  :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
239
381
  }) do
240
382
  posts = authors(:david).posts.find(:all, :limit => 2)
241
383
  assert_equal 2, posts.size
242
384
  end
243
-
385
+
244
386
  Post.with_scope(:find => {
245
- :include => [ :comments, :author ],
387
+ :include => [ :comments, :author ],
246
388
  :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
247
389
  }) do
248
390
  count = Post.count(:limit => 2)
@@ -252,15 +394,15 @@ class EagerAssociationTest < Test::Unit::TestCase
252
394
 
253
395
  def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
254
396
  Post.with_scope(:find => { :conditions => "1=1" }) do
255
- posts = authors(:david).posts.find(:all,
256
- :include => :comments,
397
+ posts = authors(:david).posts.find(:all,
398
+ :include => :comments,
257
399
  :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
258
400
  :limit => 2
259
401
  )
260
402
  assert_equal 2, posts.size
261
-
403
+
262
404
  count = Post.count(
263
- :include => [ :comments, :author ],
405
+ :include => [ :comments, :author ],
264
406
  :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
265
407
  :limit => 2
266
408
  )
@@ -316,7 +458,7 @@ class EagerAssociationTest < Test::Unit::TestCase
316
458
  assert_not_nil f.account
317
459
  assert_equal companies(:first_firm, :reload).account, f.account
318
460
  end
319
-
461
+
320
462
  def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
321
463
  author = authors(:david)
322
464
  posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
@@ -338,21 +480,32 @@ class EagerAssociationTest < Test::Unit::TestCase
338
480
  post = Post.find(6, :include=>[ :monkeys, :elephants ])
339
481
  }
340
482
  end
341
-
483
+
342
484
  def find_all_ordered(className, include=nil)
343
485
  className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
344
486
  end
345
-
487
+
346
488
  def test_limited_eager_with_order
347
489
  assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title)', :limit => 2, :offset => 1)
348
490
  assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC', :limit => 2, :offset => 1)
349
491
  end
350
-
492
+
351
493
  def test_limited_eager_with_multiple_order_columns
352
494
  assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title), posts.id', :limit => 2, :offset => 1)
353
495
  assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC, posts.id', :limit => 2, :offset => 1)
354
496
  end
355
497
 
498
+ def test_preload_with_interpolation
499
+ assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
500
+ end
501
+
502
+ def test_polymorphic_type_condition
503
+ post = Post.find(posts(:thinking).id, :include => :taggings)
504
+ assert post.taggings.include?(taggings(:thinking_general))
505
+ post = SpecialPost.find(posts(:thinking).id, :include => :taggings)
506
+ assert post.taggings.include?(taggings(:thinking_general))
507
+ end
508
+
356
509
  def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
357
510
  # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
358
511
  def assert_equal_after_sort(item1, item2, item3 = nil)
@@ -366,8 +519,8 @@ class EagerAssociationTest < Test::Unit::TestCase
366
519
  [Author, Category].each do |className|
367
520
  d1 = find_all_ordered(className)
368
521
  # test including all post types at once
369
- d2 = find_all_ordered(className, post_types)
370
- d1.each_index do |i|
522
+ d2 = find_all_ordered(className, post_types)
523
+ d1.each_index do |i|
371
524
  assert_equal(d1[i], d2[i])
372
525
  assert_equal_after_sort(d1[i].posts, d2[i].posts)
373
526
  post_types[1..-1].each do |post_type|
@@ -380,21 +533,21 @@ class EagerAssociationTest < Test::Unit::TestCase
380
533
  end
381
534
  end
382
535
  end
383
-
536
+
384
537
  def test_eager_with_multiple_associations_with_same_table_has_one
385
538
  d1 = find_all_ordered(Firm)
386
539
  d2 = find_all_ordered(Firm, :account)
387
- d1.each_index do |i|
540
+ d1.each_index do |i|
388
541
  assert_equal(d1[i], d2[i])
389
542
  assert_equal(d1[i].account, d2[i].account)
390
543
  end
391
544
  end
392
-
545
+
393
546
  def test_eager_with_multiple_associations_with_same_table_belongs_to
394
547
  firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
395
548
  d1 = find_all_ordered(Client)
396
549
  d2 = find_all_ordered(Client, firm_types)
397
- d1.each_index do |i|
550
+ d1.each_index do |i|
398
551
  assert_equal(d1[i], d2[i])
399
552
  firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
400
553
  end
@@ -405,36 +558,42 @@ class EagerAssociationTest < Test::Unit::TestCase
405
558
 
406
559
  def test_preconfigured_includes_with_belongs_to
407
560
  author = posts(:welcome).author_with_posts
408
- assert_equal 5, author.posts.size
561
+ assert_no_queries {assert_equal 5, author.posts.size}
409
562
  end
410
563
 
411
564
  def test_preconfigured_includes_with_has_one
412
565
  comment = posts(:sti_comments).very_special_comment_with_post
413
- assert_equal posts(:sti_comments), comment.post
566
+ assert_no_queries {assert_equal posts(:sti_comments), comment.post}
414
567
  end
415
568
 
416
569
  def test_preconfigured_includes_with_has_many
417
570
  posts = authors(:david).posts_with_comments
418
571
  one = posts.detect { |p| p.id == 1 }
419
- assert_equal 5, posts.size
420
- assert_equal 2, one.comments.size
572
+ assert_no_queries do
573
+ assert_equal 5, posts.size
574
+ assert_equal 2, one.comments.size
575
+ end
421
576
  end
422
577
 
423
578
  def test_preconfigured_includes_with_habtm
424
579
  posts = authors(:david).posts_with_categories
425
580
  one = posts.detect { |p| p.id == 1 }
426
- assert_equal 5, posts.size
427
- assert_equal 2, one.categories.size
581
+ assert_no_queries do
582
+ assert_equal 5, posts.size
583
+ assert_equal 2, one.categories.size
584
+ end
428
585
  end
429
586
 
430
587
  def test_preconfigured_includes_with_has_many_and_habtm
431
588
  posts = authors(:david).posts_with_comments_and_categories
432
589
  one = posts.detect { |p| p.id == 1 }
433
- assert_equal 5, posts.size
434
- assert_equal 2, one.comments.size
435
- assert_equal 2, one.categories.size
590
+ assert_no_queries do
591
+ assert_equal 5, posts.size
592
+ assert_equal 2, one.comments.size
593
+ assert_equal 2, one.categories.size
594
+ end
436
595
  end
437
-
596
+
438
597
  def test_count_with_include
439
598
  if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
440
599
  assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
@@ -444,4 +603,10 @@ class EagerAssociationTest < Test::Unit::TestCase
444
603
  assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
445
604
  end
446
605
  end
606
+
607
+ def test_load_with_sti_sharing_association
608
+ assert_queries(2) do #should not do 1 query per subclass
609
+ Comment.find :all, :include => :post
610
+ end
611
+ end
447
612
  end