activerecord-nuodb-adapter 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (456) hide show
  1. data/.DS_Store +0 -0
  2. data/.travis.yml +26 -2
  3. data/{CONTRIBUTION.rdoc → CONTRIBUTION.md} +16 -15
  4. data/Gemfile +1 -1
  5. data/README.md +48 -0
  6. data/active_setup_linux.sh +97 -0
  7. data/activerecord-nuodb-adapter.gemspec +1 -1
  8. data/lib/active_record/connection_adapters/nuodb/version.rb +1 -1
  9. data/lib/active_record/connection_adapters/nuodb_adapter.rb +45 -9
  10. data/test/.DS_Store +0 -0
  11. data/test/.gitignore +1 -0
  12. data/test/active_record/connection_adapters/fake_adapter.rb +45 -0
  13. data/test/ar_schema_test.rb +70 -0
  14. data/test/assets/example.log +1 -0
  15. data/test/assets/flowers.jpg +0 -0
  16. data/test/assets/test.txt +1 -0
  17. data/test/base_test.rb +1481 -0
  18. data/test/cases/.DS_Store +0 -0
  19. data/test/cases/adapter_test.rb +211 -0
  20. data/test/cases/adapters/firebird/connection_test.rb +8 -0
  21. data/test/cases/adapters/firebird/default_test.rb +16 -0
  22. data/test/cases/adapters/firebird/migration_test.rb +124 -0
  23. data/test/cases/adapters/mysql/active_schema_test.rb +143 -0
  24. data/test/cases/adapters/mysql/case_sensitivity_test.rb +35 -0
  25. data/test/cases/adapters/mysql/connection_test.rb +172 -0
  26. data/test/cases/adapters/mysql/enum_test.rb +10 -0
  27. data/test/cases/adapters/mysql/mysql_adapter_test.rb +125 -0
  28. data/test/cases/adapters/mysql/quoting_test.rb +25 -0
  29. data/test/cases/adapters/mysql/reserved_word_test.rb +153 -0
  30. data/test/cases/adapters/mysql/schema_test.rb +62 -0
  31. data/test/cases/adapters/mysql/sp_test.rb +15 -0
  32. data/test/cases/adapters/mysql/sql_types_test.rb +14 -0
  33. data/test/cases/adapters/mysql/statement_pool_test.rb +23 -0
  34. data/test/cases/adapters/mysql2/active_schema_test.rb +143 -0
  35. data/test/cases/adapters/mysql2/bind_parameter_test.rb +50 -0
  36. data/test/cases/adapters/mysql2/case_sensitivity_test.rb +35 -0
  37. data/test/cases/adapters/mysql2/connection_test.rb +97 -0
  38. data/test/cases/adapters/mysql2/enum_test.rb +10 -0
  39. data/test/cases/adapters/mysql2/explain_test.rb +26 -0
  40. data/test/cases/adapters/mysql2/reserved_word_test.rb +152 -0
  41. data/test/cases/adapters/mysql2/schema_migrations_test.rb +26 -0
  42. data/test/cases/adapters/mysql2/schema_test.rb +70 -0
  43. data/test/cases/adapters/mysql2/sql_types_test.rb +14 -0
  44. data/test/cases/adapters/oracle/synonym_test.rb +17 -0
  45. data/test/cases/adapters/postgresql/active_schema_test.rb +58 -0
  46. data/test/cases/adapters/postgresql/array_test.rb +104 -0
  47. data/test/cases/adapters/postgresql/bytea_test.rb +104 -0
  48. data/test/cases/adapters/postgresql/connection_test.rb +163 -0
  49. data/test/cases/adapters/postgresql/datatype_test.rb +604 -0
  50. data/test/cases/adapters/postgresql/explain_test.rb +28 -0
  51. data/test/cases/adapters/postgresql/hstore_test.rb +200 -0
  52. data/test/cases/adapters/postgresql/json_test.rb +99 -0
  53. data/test/cases/adapters/postgresql/ltree_test.rb +41 -0
  54. data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +288 -0
  55. data/test/cases/adapters/postgresql/quoting_test.rb +58 -0
  56. data/test/cases/adapters/postgresql/schema_authorization_test.rb +114 -0
  57. data/test/cases/adapters/postgresql/schema_test.rb +376 -0
  58. data/test/cases/adapters/postgresql/sql_types_test.rb +18 -0
  59. data/test/cases/adapters/postgresql/statement_pool_test.rb +39 -0
  60. data/test/cases/adapters/postgresql/timestamp_test.rb +113 -0
  61. data/test/cases/adapters/postgresql/utils_test.rb +20 -0
  62. data/test/cases/adapters/postgresql/uuid_test.rb +95 -0
  63. data/test/cases/adapters/postgresql/view_test.rb +49 -0
  64. data/test/cases/adapters/sqlite3/copy_table_test.rb +99 -0
  65. data/test/cases/adapters/sqlite3/explain_test.rb +26 -0
  66. data/test/cases/adapters/sqlite3/quoting_test.rb +102 -0
  67. data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +366 -0
  68. data/test/cases/adapters/sqlite3/statement_pool_test.rb +24 -0
  69. data/test/cases/aggregations_test.rb +158 -0
  70. data/test/cases/ar_schema_test.rb +70 -0
  71. data/test/cases/associations/association_scope_test.rb +15 -0
  72. data/test/cases/associations/belongs_to_associations_test.rb +831 -0
  73. data/test/cases/associations/callbacks_test.rb +168 -0
  74. data/test/cases/associations/cascaded_eager_loading_test.rb +179 -0
  75. data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +36 -0
  76. data/test/cases/associations/eager_load_nested_include_test.rb +128 -0
  77. data/test/cases/associations/eager_singularization_test.rb +145 -0
  78. data/test/cases/associations/eager_test.rb +1179 -0
  79. data/test/cases/associations/extension_test.rb +80 -0
  80. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +779 -0
  81. data/test/cases/associations/has_many_associations_test.rb +1699 -0
  82. data/test/cases/associations/has_many_through_associations_test.rb +950 -0
  83. data/test/cases/associations/has_one_associations_test.rb +525 -0
  84. data/test/cases/associations/has_one_through_associations_test.rb +317 -0
  85. data/test/cases/associations/inner_join_association_test.rb +115 -0
  86. data/test/cases/associations/inverse_associations_test.rb +631 -0
  87. data/test/cases/associations/join_dependency_test.rb +8 -0
  88. data/test/cases/associations/join_model_test.rb +750 -0
  89. data/test/cases/associations/nested_through_associations_test.rb +575 -0
  90. data/test/cases/associations_test.rb +344 -0
  91. data/test/cases/attribute_methods/read_test.rb +58 -0
  92. data/test/cases/attribute_methods/serialization_test.rb +29 -0
  93. data/test/cases/attribute_methods_test.rb +819 -0
  94. data/test/cases/autosave_association_test.rb +1447 -0
  95. data/test/cases/base_test.rb +1481 -0
  96. data/test/cases/batches_test.rb +173 -0
  97. data/test/cases/binary_test.rb +49 -0
  98. data/test/cases/bind_parameter_test.rb +89 -0
  99. data/test/cases/calculations_test.rb +589 -0
  100. data/test/cases/callbacks_test.rb +535 -0
  101. data/test/cases/clone_test.rb +40 -0
  102. data/test/cases/coders/yaml_column_test.rb +63 -0
  103. data/test/cases/column_alias_test.rb +17 -0
  104. data/test/cases/column_definition_test.rb +143 -0
  105. data/test/cases/column_test.rb +115 -0
  106. data/test/cases/connection_adapters/abstract_adapter_test.rb +62 -0
  107. data/test/cases/connection_adapters/connection_handler_test.rb +53 -0
  108. data/test/cases/connection_adapters/connection_specification_test.rb +12 -0
  109. data/test/cases/connection_adapters/quoting_test.rb +13 -0
  110. data/test/cases/connection_adapters/schema_cache_test.rb +56 -0
  111. data/test/cases/connection_management_test.rb +112 -0
  112. data/test/cases/connection_pool_test.rb +344 -0
  113. data/test/cases/connection_specification/resolver_test.rb +63 -0
  114. data/test/cases/core_test.rb +33 -0
  115. data/test/cases/counter_cache_test.rb +163 -0
  116. data/test/cases/custom_locking_test.rb +17 -0
  117. data/test/cases/database_statements_test.rb +19 -0
  118. data/test/cases/date_time_test.rb +43 -0
  119. data/test/cases/defaults_test.rb +214 -0
  120. data/test/cases/dirty_test.rb +626 -0
  121. data/test/cases/disconnected_test.rb +27 -0
  122. data/test/cases/dup_test.rb +136 -0
  123. data/test/cases/explain_subscriber_test.rb +59 -0
  124. data/test/cases/explain_test.rb +72 -0
  125. data/test/cases/finder_respond_to_test.rb +50 -0
  126. data/test/cases/finder_test.rb +886 -0
  127. data/test/cases/fixture_set/file_test.rb +83 -0
  128. data/test/cases/fixtures_test.rb +808 -0
  129. data/test/cases/forbidden_attributes_protection_test.rb +64 -0
  130. data/test/cases/habtm_destroy_order_test.rb +61 -0
  131. data/test/cases/helper.rb +151 -0
  132. data/test/cases/hot_compatibility_test.rb +54 -0
  133. data/test/cases/i18n_test.rb +45 -0
  134. data/test/cases/inheritance_test.rb +355 -0
  135. data/test/cases/integration_test.rb +87 -0
  136. data/test/cases/invalid_connection_test.rb +22 -0
  137. data/test/cases/invalid_date_test.rb +32 -0
  138. data/test/cases/invertible_migration_test.rb +259 -0
  139. data/test/cases/json_serialization_test.rb +300 -0
  140. data/test/cases/locking_test.rb +453 -0
  141. data/test/cases/log_subscriber_test.rb +129 -0
  142. data/test/cases/migration/change_schema_test.rb +379 -0
  143. data/test/cases/migration/change_table_test.rb +204 -0
  144. data/test/cases/migration/column_attributes_test.rb +186 -0
  145. data/test/cases/migration/column_positioning_test.rb +60 -0
  146. data/test/cases/migration/columns_test.rb +279 -0
  147. data/test/cases/migration/command_recorder_test.rb +247 -0
  148. data/test/cases/migration/create_join_table_test.rb +125 -0
  149. data/test/cases/migration/helper.rb +43 -0
  150. data/test/cases/migration/index_test.rb +209 -0
  151. data/test/cases/migration/logger_test.rb +37 -0
  152. data/test/cases/migration/references_index_test.rb +102 -0
  153. data/test/cases/migration/references_statements_test.rb +111 -0
  154. data/test/cases/migration/rename_table_test.rb +90 -0
  155. data/test/cases/migration/table_and_index_test.rb +24 -0
  156. data/test/cases/migration_test.rb +861 -0
  157. data/test/cases/migrator_test.rb +378 -0
  158. data/test/cases/mixin_test.rb +96 -0
  159. data/test/cases/modules_test.rb +143 -0
  160. data/test/cases/multiparameter_attributes_test.rb +350 -0
  161. data/test/cases/multiple_db_test.rb +108 -0
  162. data/test/cases/nested_attributes_test.rb +1056 -0
  163. data/test/cases/persistence_test.rb +802 -0
  164. data/test/cases/pooled_connections_test.rb +51 -0
  165. data/test/cases/primary_keys_test.rb +218 -0
  166. data/test/cases/query_cache_test.rb +276 -0
  167. data/test/cases/quoting_test.rb +229 -0
  168. data/test/cases/readonly_test.rb +111 -0
  169. data/test/cases/reaper_test.rb +81 -0
  170. data/test/cases/reflection_test.rb +386 -0
  171. data/test/cases/relation/where_chain_test.rb +80 -0
  172. data/test/cases/relation/where_test.rb +120 -0
  173. data/test/cases/relation_test.rb +315 -0
  174. data/test/cases/relations_test.rb +1559 -0
  175. data/test/cases/reload_models_test.rb +22 -0
  176. data/test/cases/result_test.rb +32 -0
  177. data/test/cases/sanitize_test.rb +34 -0
  178. data/test/cases/schema_dumper_test.rb +391 -0
  179. data/test/cases/scoping/default_scoping_test.rb +374 -0
  180. data/test/cases/scoping/named_scoping_test.rb +451 -0
  181. data/test/cases/scoping/relation_scoping_test.rb +331 -0
  182. data/test/cases/serialization_test.rb +68 -0
  183. data/test/cases/serialized_attribute_test.rb +252 -0
  184. data/test/cases/statement_cache_test.rb +64 -0
  185. data/test/cases/store_test.rb +153 -0
  186. data/test/cases/tasks/database_tasks_test.rb +315 -0
  187. data/test/cases/tasks/mysql_rake_test.rb +301 -0
  188. data/test/cases/tasks/postgresql_rake_test.rb +236 -0
  189. data/test/cases/tasks/sqlite_rake_test.rb +191 -0
  190. data/test/cases/test_case.rb +95 -0
  191. data/test/cases/test_simple.rb +194 -0
  192. data/test/cases/timestamp_test.rb +308 -0
  193. data/test/cases/transaction_callbacks_test.rb +317 -0
  194. data/test/cases/transaction_isolation_test.rb +114 -0
  195. data/test/cases/transactions_test.rb +595 -0
  196. data/test/cases/unconnected_test.rb +33 -0
  197. data/test/cases/validations/association_validation_test.rb +121 -0
  198. data/test/cases/validations/i18n_generate_message_validation_test.rb +76 -0
  199. data/test/cases/validations/i18n_validation_test.rb +89 -0
  200. data/test/cases/validations/presence_validation_test.rb +51 -0
  201. data/test/cases/validations/uniqueness_validation_test.rb +379 -0
  202. data/test/cases/validations_repair_helper.rb +23 -0
  203. data/test/cases/validations_test.rb +124 -0
  204. data/test/cases/xml_serialization_test.rb +455 -0
  205. data/test/cases/yaml_serialization_test.rb +52 -0
  206. data/test/config.example.yml +154 -0
  207. data/test/config.rb +5 -0
  208. data/test/database_statements_test.rb +19 -0
  209. data/test/fixtures/.gitignore +1 -0
  210. data/test/fixtures/accounts.yml +29 -0
  211. data/test/fixtures/admin/accounts.yml +2 -0
  212. data/test/fixtures/admin/randomly_named_a9.yml +7 -0
  213. data/test/fixtures/admin/randomly_named_b0.yml +7 -0
  214. data/test/fixtures/admin/users.yml +10 -0
  215. data/test/fixtures/all/developers.yml +0 -0
  216. data/test/fixtures/all/people.yml +0 -0
  217. data/test/fixtures/all/tasks.yml +0 -0
  218. data/test/fixtures/author_addresses.yml +5 -0
  219. data/test/fixtures/author_favorites.yml +4 -0
  220. data/test/fixtures/authors.yml +15 -0
  221. data/test/fixtures/binaries.yml +133 -0
  222. data/test/fixtures/books.yml +9 -0
  223. data/test/fixtures/cars.yml +9 -0
  224. data/test/fixtures/categories.yml +19 -0
  225. data/test/fixtures/categories/special_categories.yml +9 -0
  226. data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
  227. data/test/fixtures/categories_ordered.yml +7 -0
  228. data/test/fixtures/categories_posts.yml +31 -0
  229. data/test/fixtures/categorizations.yml +23 -0
  230. data/test/fixtures/clubs.yml +8 -0
  231. data/test/fixtures/collections.yml +3 -0
  232. data/test/fixtures/colleges.yml +3 -0
  233. data/test/fixtures/comments.yml +65 -0
  234. data/test/fixtures/companies.yml +59 -0
  235. data/test/fixtures/computers.yml +4 -0
  236. data/test/fixtures/courses.yml +8 -0
  237. data/test/fixtures/customers.yml +26 -0
  238. data/test/fixtures/dashboards.yml +6 -0
  239. data/test/fixtures/developers.yml +21 -0
  240. data/test/fixtures/developers_projects.yml +17 -0
  241. data/test/fixtures/dog_lovers.yml +7 -0
  242. data/test/fixtures/dogs.yml +4 -0
  243. data/test/fixtures/edges.yml +5 -0
  244. data/test/fixtures/entrants.yml +14 -0
  245. data/test/fixtures/essays.yml +6 -0
  246. data/test/fixtures/faces.yml +11 -0
  247. data/test/fixtures/fk_test_has_fk.yml +3 -0
  248. data/test/fixtures/fk_test_has_pk.yml +2 -0
  249. data/test/fixtures/friendships.yml +4 -0
  250. data/test/fixtures/funny_jokes.yml +10 -0
  251. data/test/fixtures/interests.yml +33 -0
  252. data/test/fixtures/items.yml +3 -0
  253. data/test/fixtures/jobs.yml +7 -0
  254. data/test/fixtures/legacy_things.yml +3 -0
  255. data/test/fixtures/mateys.yml +4 -0
  256. data/test/fixtures/member_details.yml +8 -0
  257. data/test/fixtures/member_types.yml +6 -0
  258. data/test/fixtures/members.yml +11 -0
  259. data/test/fixtures/memberships.yml +34 -0
  260. data/test/fixtures/men.yml +5 -0
  261. data/test/fixtures/minimalistics.yml +2 -0
  262. data/test/fixtures/minivans.yml +5 -0
  263. data/test/fixtures/mixed_case_monkeys.yml +6 -0
  264. data/test/fixtures/mixins.yml +29 -0
  265. data/test/fixtures/movies.yml +7 -0
  266. data/test/fixtures/naked/csv/accounts.csv +1 -0
  267. data/test/fixtures/naked/yml/accounts.yml +1 -0
  268. data/test/fixtures/naked/yml/companies.yml +1 -0
  269. data/test/fixtures/naked/yml/courses.yml +1 -0
  270. data/test/fixtures/organizations.yml +5 -0
  271. data/test/fixtures/other_topics.yml +42 -0
  272. data/test/fixtures/owners.yml +8 -0
  273. data/test/fixtures/parrots.yml +27 -0
  274. data/test/fixtures/parrots_pirates.yml +7 -0
  275. data/test/fixtures/people.yml +24 -0
  276. data/test/fixtures/peoples_treasures.yml +3 -0
  277. data/test/fixtures/pets.yml +19 -0
  278. data/test/fixtures/pirates.yml +9 -0
  279. data/test/fixtures/posts.yml +82 -0
  280. data/test/fixtures/price_estimates.yml +7 -0
  281. data/test/fixtures/products.yml +4 -0
  282. data/test/fixtures/projects.yml +7 -0
  283. data/test/fixtures/randomly_named_a9.yml +7 -0
  284. data/test/fixtures/ratings.yml +14 -0
  285. data/test/fixtures/readers.yml +11 -0
  286. data/test/fixtures/references.yml +17 -0
  287. data/test/fixtures/reserved_words/distinct.yml +5 -0
  288. data/test/fixtures/reserved_words/distinct_select.yml +11 -0
  289. data/test/fixtures/reserved_words/group.yml +14 -0
  290. data/test/fixtures/reserved_words/select.yml +8 -0
  291. data/test/fixtures/reserved_words/values.yml +7 -0
  292. data/test/fixtures/ships.yml +6 -0
  293. data/test/fixtures/speedometers.yml +8 -0
  294. data/test/fixtures/sponsors.yml +12 -0
  295. data/test/fixtures/string_key_objects.yml +7 -0
  296. data/test/fixtures/subscribers.yml +11 -0
  297. data/test/fixtures/subscriptions.yml +12 -0
  298. data/test/fixtures/taggings.yml +78 -0
  299. data/test/fixtures/tags.yml +11 -0
  300. data/test/fixtures/tasks.yml +7 -0
  301. data/test/fixtures/teapots.yml +3 -0
  302. data/test/fixtures/topics.yml +42 -0
  303. data/test/fixtures/toys.yml +14 -0
  304. data/test/fixtures/traffic_lights.yml +10 -0
  305. data/test/fixtures/treasures.yml +10 -0
  306. data/test/fixtures/variants.yml +4 -0
  307. data/test/fixtures/vegetables.yml +20 -0
  308. data/test/fixtures/vertices.yml +4 -0
  309. data/test/fixtures/warehouse-things.yml +3 -0
  310. data/test/fixtures/zines.yml +5 -0
  311. data/test/migration_test.rb +861 -0
  312. data/test/migrations/10_urban/9_add_expressions.rb +11 -0
  313. data/test/migrations/decimal/1_give_me_big_numbers.rb +15 -0
  314. data/test/migrations/empty/.gitkeep +0 -0
  315. data/test/migrations/magic/1_currencies_have_symbols.rb +12 -0
  316. data/test/migrations/missing/1000_people_have_middle_names.rb +9 -0
  317. data/test/migrations/missing/1_people_have_last_names.rb +9 -0
  318. data/test/migrations/missing/3_we_need_reminders.rb +12 -0
  319. data/test/migrations/missing/4_innocent_jointable.rb +12 -0
  320. data/test/migrations/rename/1_we_need_things.rb +11 -0
  321. data/test/migrations/rename/2_rename_things.rb +9 -0
  322. data/test/migrations/to_copy/1_people_have_hobbies.rb +9 -0
  323. data/test/migrations/to_copy/2_people_have_descriptions.rb +9 -0
  324. data/test/migrations/to_copy2/1_create_articles.rb +7 -0
  325. data/test/migrations/to_copy2/2_create_comments.rb +7 -0
  326. data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +9 -0
  327. data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +9 -0
  328. data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +9 -0
  329. data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +7 -0
  330. data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +7 -0
  331. data/test/migrations/valid/1_valid_people_have_last_names.rb +9 -0
  332. data/test/migrations/valid/2_we_need_reminders.rb +12 -0
  333. data/test/migrations/valid/3_innocent_jointable.rb +12 -0
  334. data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +9 -0
  335. data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +12 -0
  336. data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +12 -0
  337. data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +9 -0
  338. data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +12 -0
  339. data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +12 -0
  340. data/test/models/admin.rb +5 -0
  341. data/test/models/admin/account.rb +3 -0
  342. data/test/models/admin/randomly_named_c1.rb +3 -0
  343. data/test/models/admin/user.rb +39 -0
  344. data/test/models/aircraft.rb +4 -0
  345. data/test/models/arunit2_model.rb +3 -0
  346. data/test/models/author.rb +196 -0
  347. data/test/models/auto_id.rb +4 -0
  348. data/test/models/autoloadable/extra_firm.rb +2 -0
  349. data/test/models/binary.rb +2 -0
  350. data/test/models/bird.rb +12 -0
  351. data/test/models/book.rb +9 -0
  352. data/test/models/boolean.rb +2 -0
  353. data/test/models/bulb.rb +45 -0
  354. data/test/models/car.rb +28 -0
  355. data/test/models/categorization.rb +19 -0
  356. data/test/models/category.rb +34 -0
  357. data/test/models/citation.rb +6 -0
  358. data/test/models/club.rb +15 -0
  359. data/test/models/college.rb +5 -0
  360. data/test/models/column_name.rb +3 -0
  361. data/test/models/comment.rb +38 -0
  362. data/test/models/company.rb +214 -0
  363. data/test/models/company_in_module.rb +80 -0
  364. data/test/models/computer.rb +3 -0
  365. data/test/models/contact.rb +40 -0
  366. data/test/models/contract.rb +19 -0
  367. data/test/models/country.rb +7 -0
  368. data/test/models/course.rb +6 -0
  369. data/test/models/customer.rb +77 -0
  370. data/test/models/dashboard.rb +3 -0
  371. data/test/models/default.rb +2 -0
  372. data/test/models/developer.rb +250 -0
  373. data/test/models/dog.rb +5 -0
  374. data/test/models/dog_lover.rb +5 -0
  375. data/test/models/edge.rb +5 -0
  376. data/test/models/electron.rb +3 -0
  377. data/test/models/engine.rb +4 -0
  378. data/test/models/entrant.rb +3 -0
  379. data/test/models/essay.rb +5 -0
  380. data/test/models/event.rb +3 -0
  381. data/test/models/eye.rb +37 -0
  382. data/test/models/face.rb +7 -0
  383. data/test/models/friendship.rb +6 -0
  384. data/test/models/guid.rb +2 -0
  385. data/test/models/interest.rb +5 -0
  386. data/test/models/invoice.rb +4 -0
  387. data/test/models/item.rb +7 -0
  388. data/test/models/job.rb +7 -0
  389. data/test/models/joke.rb +7 -0
  390. data/test/models/keyboard.rb +3 -0
  391. data/test/models/legacy_thing.rb +3 -0
  392. data/test/models/lesson.rb +11 -0
  393. data/test/models/line_item.rb +3 -0
  394. data/test/models/liquid.rb +4 -0
  395. data/test/models/man.rb +9 -0
  396. data/test/models/matey.rb +4 -0
  397. data/test/models/member.rb +36 -0
  398. data/test/models/member_detail.rb +7 -0
  399. data/test/models/member_type.rb +3 -0
  400. data/test/models/membership.rb +15 -0
  401. data/test/models/minimalistic.rb +2 -0
  402. data/test/models/minivan.rb +9 -0
  403. data/test/models/mixed_case_monkey.rb +3 -0
  404. data/test/models/molecule.rb +4 -0
  405. data/test/models/movie.rb +5 -0
  406. data/test/models/order.rb +4 -0
  407. data/test/models/organization.rb +12 -0
  408. data/test/models/owner.rb +5 -0
  409. data/test/models/parrot.rb +29 -0
  410. data/test/models/person.rb +128 -0
  411. data/test/models/pet.rb +15 -0
  412. data/test/models/pirate.rb +86 -0
  413. data/test/models/possession.rb +3 -0
  414. data/test/models/post.rb +194 -0
  415. data/test/models/price_estimate.rb +4 -0
  416. data/test/models/project.rb +30 -0
  417. data/test/models/randomly_named_c1.rb +3 -0
  418. data/test/models/rating.rb +4 -0
  419. data/test/models/reader.rb +21 -0
  420. data/test/models/reference.rb +22 -0
  421. data/test/models/reply.rb +61 -0
  422. data/test/models/ship.rb +19 -0
  423. data/test/models/ship_part.rb +7 -0
  424. data/test/models/shop.rb +12 -0
  425. data/test/models/speedometer.rb +6 -0
  426. data/test/models/sponsor.rb +7 -0
  427. data/test/models/string_key_object.rb +3 -0
  428. data/test/models/student.rb +3 -0
  429. data/test/models/subject.rb +16 -0
  430. data/test/models/subscriber.rb +8 -0
  431. data/test/models/subscription.rb +4 -0
  432. data/test/models/tag.rb +7 -0
  433. data/test/models/tagging.rb +13 -0
  434. data/test/models/task.rb +5 -0
  435. data/test/models/topic.rb +121 -0
  436. data/test/models/toy.rb +6 -0
  437. data/test/models/traffic_light.rb +4 -0
  438. data/test/models/treasure.rb +11 -0
  439. data/test/models/treaty.rb +7 -0
  440. data/test/models/tyre.rb +3 -0
  441. data/test/models/vegetables.rb +24 -0
  442. data/test/models/vertex.rb +9 -0
  443. data/test/models/warehouse_thing.rb +5 -0
  444. data/test/models/wheel.rb +3 -0
  445. data/test/models/without_table.rb +3 -0
  446. data/test/models/zine.rb +3 -0
  447. data/test/schema/mysql2_specific_schema.rb +58 -0
  448. data/test/schema/mysql_specific_schema.rb +70 -0
  449. data/test/schema/oracle_specific_schema.rb +46 -0
  450. data/test/schema/postgresql_specific_schema.rb +224 -0
  451. data/test/schema/schema.rb +809 -0
  452. data/test/schema/sqlite_specific_schema.rb +25 -0
  453. data/test/support/config.rb +43 -0
  454. data/test/support/connection.rb +21 -0
  455. metadata +898 -6
  456. data/README.rdoc +0 -42
@@ -0,0 +1,145 @@
1
+ require "cases/helper"
2
+
3
+ class Virus < ActiveRecord::Base
4
+ belongs_to :octopus
5
+ end
6
+ class Octopus < ActiveRecord::Base
7
+ has_one :virus
8
+ end
9
+ class Pass < ActiveRecord::Base
10
+ belongs_to :bus
11
+ end
12
+ class Bus < ActiveRecord::Base
13
+ has_many :passes
14
+ end
15
+ class Mess < ActiveRecord::Base
16
+ has_and_belongs_to_many :crises
17
+ end
18
+ class Crisis < ActiveRecord::Base
19
+ has_and_belongs_to_many :messes
20
+ has_many :analyses, :dependent => :destroy
21
+ has_many :successes, :through => :analyses
22
+ has_many :dresses, :dependent => :destroy
23
+ has_many :compresses, :through => :dresses
24
+ end
25
+ class Analysis < ActiveRecord::Base
26
+ belongs_to :crisis
27
+ belongs_to :success
28
+ end
29
+ class Success < ActiveRecord::Base
30
+ has_many :analyses, :dependent => :destroy
31
+ has_many :crises, :through => :analyses
32
+ end
33
+ class Dress < ActiveRecord::Base
34
+ belongs_to :crisis
35
+ has_many :compresses
36
+ end
37
+ class Compress < ActiveRecord::Base
38
+ belongs_to :dress
39
+ end
40
+
41
+
42
+ class EagerSingularizationTest < ActiveRecord::TestCase
43
+
44
+ def setup
45
+ if ActiveRecord::Base.connection.supports_migrations?
46
+ ActiveRecord::Base.connection.create_table :viri do |t|
47
+ t.column :octopus_id, :integer
48
+ t.column :species, :string
49
+ end
50
+ ActiveRecord::Base.connection.create_table :octopi do |t|
51
+ t.column :species, :string
52
+ end
53
+ ActiveRecord::Base.connection.create_table :passes do |t|
54
+ t.column :bus_id, :integer
55
+ t.column :rides, :integer
56
+ end
57
+ ActiveRecord::Base.connection.create_table :buses do |t|
58
+ t.column :name, :string
59
+ end
60
+ ActiveRecord::Base.connection.create_table :crises_messes, :id => false do |t|
61
+ t.column :crisis_id, :integer
62
+ t.column :mess_id, :integer
63
+ end
64
+ ActiveRecord::Base.connection.create_table :messes do |t|
65
+ t.column :name, :string
66
+ end
67
+ ActiveRecord::Base.connection.create_table :crises do |t|
68
+ t.column :name, :string
69
+ end
70
+ ActiveRecord::Base.connection.create_table :successes do |t|
71
+ t.column :name, :string
72
+ end
73
+ ActiveRecord::Base.connection.create_table :analyses do |t|
74
+ t.column :crisis_id, :integer
75
+ t.column :success_id, :integer
76
+ end
77
+ ActiveRecord::Base.connection.create_table :dresses do |t|
78
+ t.column :crisis_id, :integer
79
+ end
80
+ ActiveRecord::Base.connection.create_table :compresses do |t|
81
+ t.column :dress_id, :integer
82
+ end
83
+ @have_tables = true
84
+ else
85
+ @have_tables = false
86
+ end
87
+ end
88
+
89
+ def teardown
90
+ ActiveRecord::Base.connection.drop_table :viri
91
+ ActiveRecord::Base.connection.drop_table :octopi
92
+ ActiveRecord::Base.connection.drop_table :passes
93
+ ActiveRecord::Base.connection.drop_table :buses
94
+ ActiveRecord::Base.connection.drop_table :crises_messes
95
+ ActiveRecord::Base.connection.drop_table :messes
96
+ ActiveRecord::Base.connection.drop_table :crises
97
+ ActiveRecord::Base.connection.drop_table :successes
98
+ ActiveRecord::Base.connection.drop_table :analyses
99
+ ActiveRecord::Base.connection.drop_table :dresses
100
+ ActiveRecord::Base.connection.drop_table :compresses
101
+ end
102
+
103
+ def test_eager_no_extra_singularization_belongs_to
104
+ return unless @have_tables
105
+ assert_nothing_raised do
106
+ Virus.all.merge!(:includes => :octopus).to_a
107
+ end
108
+ end
109
+
110
+ def test_eager_no_extra_singularization_has_one
111
+ return unless @have_tables
112
+ assert_nothing_raised do
113
+ Octopus.all.merge!(:includes => :virus).to_a
114
+ end
115
+ end
116
+
117
+ def test_eager_no_extra_singularization_has_many
118
+ return unless @have_tables
119
+ assert_nothing_raised do
120
+ Bus.all.merge!(:includes => :passes).to_a
121
+ end
122
+ end
123
+
124
+ def test_eager_no_extra_singularization_has_and_belongs_to_many
125
+ return unless @have_tables
126
+ assert_nothing_raised do
127
+ Crisis.all.merge!(:includes => :messes).to_a
128
+ Mess.all.merge!(:includes => :crises).to_a
129
+ end
130
+ end
131
+
132
+ def test_eager_no_extra_singularization_has_many_through_belongs_to
133
+ return unless @have_tables
134
+ assert_nothing_raised do
135
+ Crisis.all.merge!(:includes => :successes).to_a
136
+ end
137
+ end
138
+
139
+ def test_eager_no_extra_singularization_has_many_through_has_many
140
+ return unless @have_tables
141
+ assert_nothing_raised do
142
+ Crisis.all.merge!(:includes => :compresses).to_a
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,1179 @@
1
+ require "cases/helper"
2
+ require 'models/post'
3
+ require 'models/tagging'
4
+ require 'models/tag'
5
+ require 'models/comment'
6
+ require 'models/author'
7
+ require 'models/category'
8
+ require 'models/company'
9
+ require 'models/person'
10
+ require 'models/reader'
11
+ require 'models/owner'
12
+ require 'models/pet'
13
+ require 'models/reference'
14
+ require 'models/job'
15
+ require 'models/subscriber'
16
+ require 'models/subscription'
17
+ require 'models/book'
18
+ require 'models/developer'
19
+ require 'models/project'
20
+ require 'models/member'
21
+ require 'models/membership'
22
+ require 'models/club'
23
+ require 'models/categorization'
24
+ require 'models/sponsor'
25
+
26
+ class EagerAssociationTest < ActiveRecord::TestCase
27
+ fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
28
+ :companies, :accounts, :tags, :taggings, :people, :readers, :categorizations,
29
+ :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
30
+ :developers, :projects, :developers_projects, :members, :memberships, :clubs, :sponsors
31
+
32
+ def test_eager_with_has_one_through_join_model_with_conditions_on_the_through
33
+ member = Member.all.merge!(:includes => :favourite_club).find(members(:some_other_guy).id)
34
+ assert_nil member.favourite_club
35
+ end
36
+
37
+ def test_loading_with_one_association
38
+ posts = Post.all.merge!(:includes => :comments).to_a
39
+ post = posts.find { |p| p.id == 1 }
40
+ assert_equal 2, post.comments.size
41
+ assert post.comments.include?(comments(:greetings))
42
+
43
+ post = Post.all.merge!(:includes => :comments, :where => "posts.title = 'Welcome to the weblog'").first
44
+ assert_equal 2, post.comments.size
45
+ assert post.comments.include?(comments(:greetings))
46
+
47
+ posts = Post.all.merge!(:includes => :last_comment).to_a
48
+ post = posts.find { |p| p.id == 1 }
49
+ assert_equal Post.find(1).last_comment, post.last_comment
50
+ end
51
+
52
+ def test_loading_with_one_association_with_non_preload
53
+ posts = Post.all.merge!(:includes => :last_comment, :order => 'comments.id DESC').to_a
54
+ post = posts.find { |p| p.id == 1 }
55
+ assert_equal Post.find(1).last_comment, post.last_comment
56
+ end
57
+
58
+ def test_loading_conditions_with_or
59
+ posts = authors(:david).posts.references(:comments).merge(
60
+ :includes => :comments,
61
+ :where => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'"
62
+ ).to_a
63
+ assert_nil posts.detect { |p| p.author_id != authors(:david).id },
64
+ "expected to find only david's posts"
65
+ end
66
+
67
+ def test_with_ordering
68
+ list = Post.all.merge!(:includes => :comments, :order => "posts.id DESC").to_a
69
+ [:other_by_mary, :other_by_bob, :misc_by_mary, :misc_by_bob, :eager_other,
70
+ :sti_habtm, :sti_post_and_comments, :sti_comments, :authorless, :thinking, :welcome
71
+ ].each_with_index do |post, index|
72
+ assert_equal posts(post), list[index]
73
+ end
74
+ end
75
+
76
+ def test_has_many_through_with_order
77
+ authors = Author.includes(:favorite_authors).to_a
78
+ assert_no_queries { authors.map(&:favorite_authors) }
79
+ end
80
+
81
+ def test_with_two_tables_in_from_without_getting_double_quoted
82
+ posts = Post.select("posts.*").from("authors, posts").eager_load(:comments).where("posts.author_id = authors.id").order("posts.id").to_a
83
+ assert_equal 2, posts.first.comments.size
84
+ end
85
+
86
+ def test_loading_with_multiple_associations
87
+ posts = Post.all.merge!(:includes => [ :comments, :author, :categories ], :order => "posts.id").to_a
88
+ assert_equal 2, posts.first.comments.size
89
+ assert_equal 2, posts.first.categories.size
90
+ assert posts.first.comments.include?(comments(:greetings))
91
+ end
92
+
93
+ def test_duplicate_middle_objects
94
+ comments = Comment.all.merge!(:where => 'post_id = 1', :includes => [:post => :author]).to_a
95
+ assert_no_queries do
96
+ comments.each {|comment| comment.post.author.name}
97
+ end
98
+ end
99
+
100
+ def test_preloading_has_many_in_multiple_queries_with_more_ids_than_database_can_handle
101
+ Comment.connection.expects(:in_clause_length).at_least_once.returns(5)
102
+ posts = Post.all.merge!(:includes=>:comments).to_a
103
+ assert_equal 11, posts.size
104
+ end
105
+
106
+ def test_preloading_has_many_in_one_queries_when_database_has_no_limit_on_ids_it_can_handle
107
+ Comment.connection.expects(:in_clause_length).at_least_once.returns(nil)
108
+ posts = Post.all.merge!(:includes=>:comments).to_a
109
+ assert_equal 11, posts.size
110
+ end
111
+
112
+ def test_preloading_habtm_in_multiple_queries_with_more_ids_than_database_can_handle
113
+ Comment.connection.expects(:in_clause_length).at_least_once.returns(5)
114
+ posts = Post.all.merge!(:includes=>:categories).to_a
115
+ assert_equal 11, posts.size
116
+ end
117
+
118
+ def test_preloading_habtm_in_one_queries_when_database_has_no_limit_on_ids_it_can_handle
119
+ Comment.connection.expects(:in_clause_length).at_least_once.returns(nil)
120
+ posts = Post.all.merge!(:includes=>:categories).to_a
121
+ assert_equal 11, posts.size
122
+ end
123
+
124
+ def test_load_associated_records_in_one_query_when_adapter_has_no_limit
125
+ Comment.connection.expects(:in_clause_length).at_least_once.returns(nil)
126
+
127
+ post = posts(:welcome)
128
+ assert_queries(2) do
129
+ Post.includes(:comments).where(:id => post.id).to_a
130
+ end
131
+ end
132
+
133
+ def test_load_associated_records_in_several_queries_when_many_ids_passed
134
+ Comment.connection.expects(:in_clause_length).at_least_once.returns(1)
135
+
136
+ post1, post2 = posts(:welcome), posts(:thinking)
137
+ assert_queries(3) do
138
+ Post.includes(:comments).where(:id => [post1.id, post2.id]).to_a
139
+ end
140
+ end
141
+
142
+ def test_load_associated_records_in_one_query_when_a_few_ids_passed
143
+ Comment.connection.expects(:in_clause_length).at_least_once.returns(3)
144
+
145
+ post = posts(:welcome)
146
+ assert_queries(2) do
147
+ Post.includes(:comments).where(:id => post.id).to_a
148
+ end
149
+ end
150
+
151
+ def test_including_duplicate_objects_from_belongs_to
152
+ popular_post = Post.create!(:title => 'foo', :body => "I like cars!")
153
+ comment = popular_post.comments.create!(:body => "lol")
154
+ popular_post.readers.create!(:person => people(:michael))
155
+ popular_post.readers.create!(:person => people(:david))
156
+
157
+ readers = Reader.all.merge!(:where => ["post_id = ?", popular_post.id],
158
+ :includes => {:post => :comments}).to_a
159
+ readers.each do |reader|
160
+ assert_equal [comment], reader.post.comments
161
+ end
162
+ end
163
+
164
+ def test_including_duplicate_objects_from_has_many
165
+ car_post = Post.create!(:title => 'foo', :body => "I like cars!")
166
+ car_post.categories << categories(:general)
167
+ car_post.categories << categories(:technology)
168
+
169
+ comment = car_post.comments.create!(:body => "hmm")
170
+ categories = Category.all.merge!(:where => { 'posts.id' => car_post.id },
171
+ :includes => {:posts => :comments}).to_a
172
+ categories.each do |category|
173
+ assert_equal [comment], category.posts[0].comments
174
+ end
175
+ end
176
+
177
+ def test_associations_loaded_for_all_records
178
+ post = Post.create!(:title => 'foo', :body => "I like cars!")
179
+ SpecialComment.create!(:body => 'Come on!', :post => post)
180
+ first_category = Category.create! :name => 'First!', :posts => [post]
181
+ second_category = Category.create! :name => 'Second!', :posts => [post]
182
+
183
+ categories = Category.where(:id => [first_category.id, second_category.id]).includes(:posts => :special_comments)
184
+ assert_equal categories.map { |category| category.posts.first.special_comments.loaded? }, [true, true]
185
+ end
186
+
187
+ def test_finding_with_includes_on_has_many_association_with_same_include_includes_only_once
188
+ author_id = authors(:david).id
189
+ author = assert_queries(3) { Author.all.merge!(:includes => {:posts_with_comments => :comments}).find(author_id) } # find the author, then find the posts, then find the comments
190
+ author.posts_with_comments.each do |post_with_comments|
191
+ assert_equal post_with_comments.comments.length, post_with_comments.comments.count
192
+ assert_nil post_with_comments.comments.to_a.uniq!
193
+ end
194
+ end
195
+
196
+ def test_finding_with_includes_on_has_one_association_with_same_include_includes_only_once
197
+ author = authors(:david)
198
+ post = author.post_about_thinking_with_last_comment
199
+ last_comment = post.last_comment
200
+ author = assert_queries(3) { Author.all.merge!(:includes => {:post_about_thinking_with_last_comment => :last_comment}).find(author.id)} # find the author, then find the posts, then find the comments
201
+ assert_no_queries do
202
+ assert_equal post, author.post_about_thinking_with_last_comment
203
+ assert_equal last_comment, author.post_about_thinking_with_last_comment.last_comment
204
+ end
205
+ end
206
+
207
+ def test_finding_with_includes_on_belongs_to_association_with_same_include_includes_only_once
208
+ post = posts(:welcome)
209
+ author = post.author
210
+ author_address = author.author_address
211
+ post = assert_queries(3) { Post.all.merge!(:includes => {:author_with_address => :author_address}).find(post.id) } # find the post, then find the author, then find the address
212
+ assert_no_queries do
213
+ assert_equal author, post.author_with_address
214
+ assert_equal author_address, post.author_with_address.author_address
215
+ end
216
+ end
217
+
218
+ def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
219
+ post = posts(:welcome)
220
+ post.update!(author: nil)
221
+ post = assert_queries(1) { Post.all.merge!(includes: {author_with_address: :author_address}).find(post.id) }
222
+ # find the post, then find the author which is null so no query for the author or address
223
+ assert_no_queries do
224
+ assert_equal nil, post.author_with_address
225
+ end
226
+ end
227
+
228
+ def test_finding_with_includes_on_null_belongs_to_polymorphic_association
229
+ sponsor = sponsors(:moustache_club_sponsor_for_groucho)
230
+ sponsor.update!(sponsorable: nil)
231
+ sponsor = assert_queries(1) { Sponsor.all.merge!(:includes => :sponsorable).find(sponsor.id) }
232
+ assert_no_queries do
233
+ assert_equal nil, sponsor.sponsorable
234
+ end
235
+ end
236
+
237
+ def test_loading_from_an_association
238
+ posts = authors(:david).posts.merge(:includes => :comments, :order => "posts.id").to_a
239
+ assert_equal 2, posts.first.comments.size
240
+ end
241
+
242
+ def test_loading_from_an_association_that_has_a_hash_of_conditions
243
+ assert_nothing_raised do
244
+ Author.all.merge!(:includes => :hello_posts_with_hash_conditions).to_a
245
+ end
246
+ assert !Author.all.merge!(:includes => :hello_posts_with_hash_conditions).find(authors(:david).id).hello_posts.empty?
247
+ end
248
+
249
+ def test_loading_with_no_associations
250
+ assert_nil Post.all.merge!(:includes => :author).find(posts(:authorless).id).author
251
+ end
252
+
253
+ # Regression test for 21c75e5
254
+ def test_nested_loading_does_not_raise_exception_when_association_does_not_exist
255
+ assert_nothing_raised do
256
+ Post.all.merge!(:includes => {:author => :author_addresss}).find(posts(:authorless).id)
257
+ end
258
+ end
259
+
260
+ def test_nested_loading_through_has_one_association
261
+ aa = AuthorAddress.all.merge!(:includes => {:author => :posts}).find(author_addresses(:david_address).id)
262
+ assert_equal aa.author.posts.count, aa.author.posts.length
263
+ end
264
+
265
+ def test_nested_loading_through_has_one_association_with_order
266
+ aa = AuthorAddress.all.merge!(:includes => {:author => :posts}, :order => 'author_addresses.id').find(author_addresses(:david_address).id)
267
+ assert_equal aa.author.posts.count, aa.author.posts.length
268
+ end
269
+
270
+ def test_nested_loading_through_has_one_association_with_order_on_association
271
+ aa = AuthorAddress.all.merge!(:includes => {:author => :posts}, :order => 'authors.id').find(author_addresses(:david_address).id)
272
+ assert_equal aa.author.posts.count, aa.author.posts.length
273
+ end
274
+
275
+ def test_nested_loading_through_has_one_association_with_order_on_nested_association
276
+ aa = AuthorAddress.all.merge!(:includes => {:author => :posts}, :order => 'posts.id').find(author_addresses(:david_address).id)
277
+ assert_equal aa.author.posts.count, aa.author.posts.length
278
+ end
279
+
280
+ def test_nested_loading_through_has_one_association_with_conditions
281
+ aa = AuthorAddress.references(:author_addresses).merge(
282
+ :includes => {:author => :posts},
283
+ :where => "author_addresses.id > 0"
284
+ ).find author_addresses(:david_address).id
285
+ assert_equal aa.author.posts.count, aa.author.posts.length
286
+ end
287
+
288
+ def test_nested_loading_through_has_one_association_with_conditions_on_association
289
+ aa = AuthorAddress.references(:authors).merge(
290
+ :includes => {:author => :posts},
291
+ :where => "authors.id > 0"
292
+ ).find author_addresses(:david_address).id
293
+ assert_equal aa.author.posts.count, aa.author.posts.length
294
+ end
295
+
296
+ def test_nested_loading_through_has_one_association_with_conditions_on_nested_association
297
+ aa = AuthorAddress.references(:posts).merge(
298
+ :includes => {:author => :posts},
299
+ :where => "posts.id > 0"
300
+ ).find author_addresses(:david_address).id
301
+ assert_equal aa.author.posts.count, aa.author.posts.length
302
+ end
303
+
304
+ def test_eager_association_loading_with_belongs_to_and_foreign_keys
305
+ pets = Pet.all.merge!(:includes => :owner).to_a
306
+ assert_equal 4, pets.length
307
+ end
308
+
309
+ def test_eager_association_loading_with_belongs_to
310
+ comments = Comment.all.merge!(:includes => :post).to_a
311
+ assert_equal 11, comments.length
312
+ titles = comments.map { |c| c.post.title }
313
+ assert titles.include?(posts(:welcome).title)
314
+ assert titles.include?(posts(:sti_post_and_comments).title)
315
+ end
316
+
317
+ def test_eager_association_loading_with_belongs_to_and_limit
318
+ comments = Comment.all.merge!(:includes => :post, :limit => 5, :order => 'comments.id').to_a
319
+ assert_equal 5, comments.length
320
+ assert_equal [1,2,3,5,6], comments.collect { |c| c.id }
321
+ end
322
+
323
+ def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
324
+ comments = Comment.all.merge!(:includes => :post, :where => 'post_id = 4', :limit => 3, :order => 'comments.id').to_a
325
+ assert_equal 3, comments.length
326
+ assert_equal [5,6,7], comments.collect { |c| c.id }
327
+ end
328
+
329
+ def test_eager_association_loading_with_belongs_to_and_limit_and_offset
330
+ comments = Comment.all.merge!(:includes => :post, :limit => 3, :offset => 2, :order => 'comments.id').to_a
331
+ assert_equal 3, comments.length
332
+ assert_equal [3,5,6], comments.collect { |c| c.id }
333
+ end
334
+
335
+ def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
336
+ comments = Comment.all.merge!(:includes => :post, :where => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id').to_a
337
+ assert_equal 3, comments.length
338
+ assert_equal [6,7,8], comments.collect { |c| c.id }
339
+ end
340
+
341
+ def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
342
+ comments = Comment.all.merge!(:includes => :post, :where => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id').to_a
343
+ assert_equal 3, comments.length
344
+ assert_equal [6,7,8], comments.collect { |c| c.id }
345
+ end
346
+
347
+ def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
348
+ assert_nothing_raised do
349
+ Comment.includes(:post).references(:posts).where('posts.id = ?', 4)
350
+ end
351
+ end
352
+
353
+ def test_eager_association_loading_with_belongs_to_and_conditions_hash
354
+ comments = []
355
+ assert_nothing_raised do
356
+ comments = Comment.all.merge!(:includes => :post, :where => {:posts => {:id => 4}}, :limit => 3, :order => 'comments.id').to_a
357
+ end
358
+ assert_equal 3, comments.length
359
+ assert_equal [5,6,7], comments.collect { |c| c.id }
360
+ assert_no_queries do
361
+ comments.first.post
362
+ end
363
+ end
364
+
365
+ def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
366
+ quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
367
+ assert_nothing_raised do
368
+ Comment.includes(:post).references(:posts).where("#{quoted_posts_id} = ?", 4)
369
+ end
370
+ end
371
+
372
+ def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
373
+ assert_nothing_raised do
374
+ Comment.all.merge!(:includes => :post, :order => 'posts.id').to_a
375
+ end
376
+ end
377
+
378
+ def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
379
+ quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
380
+ assert_nothing_raised do
381
+ Comment.includes(:post).references(:posts).order(quoted_posts_id)
382
+ end
383
+ end
384
+
385
+ def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
386
+ posts = Post.all.merge!(:includes => [:author, :very_special_comment], :limit => 1, :order => 'posts.id').to_a
387
+ assert_equal 1, posts.length
388
+ assert_equal [1], posts.collect { |p| p.id }
389
+ end
390
+
391
+ def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
392
+ posts = Post.all.merge!(:includes => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id').to_a
393
+ assert_equal 1, posts.length
394
+ assert_equal [2], posts.collect { |p| p.id }
395
+ end
396
+
397
+ def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
398
+ author_favorite = AuthorFavorite.all.merge!(:includes => :favorite_author).first
399
+ assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
400
+ end
401
+
402
+ def test_eager_load_belongs_to_quotes_table_and_column_names
403
+ job = Job.includes(:ideal_reference).find jobs(:unicyclist).id
404
+ references(:michael_unicyclist)
405
+ assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
406
+ end
407
+
408
+ def test_eager_load_has_one_quotes_table_and_column_names
409
+ michael = Person.all.merge!(:includes => :favourite_reference).find(people(:michael))
410
+ references(:michael_unicyclist)
411
+ assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
412
+ end
413
+
414
+ def test_eager_load_has_many_quotes_table_and_column_names
415
+ michael = Person.all.merge!(:includes => :references).find(people(:michael))
416
+ references(:michael_magician,:michael_unicyclist)
417
+ assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) }
418
+ end
419
+
420
+ def test_eager_load_has_many_through_quotes_table_and_column_names
421
+ michael = Person.all.merge!(:includes => :jobs).find(people(:michael))
422
+ jobs(:magician, :unicyclist)
423
+ assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) }
424
+ end
425
+
426
+ def test_eager_load_has_many_with_string_keys
427
+ subscriptions = subscriptions(:webster_awdr, :webster_rfr)
428
+ subscriber =Subscriber.all.merge!(:includes => :subscriptions).find(subscribers(:second).id)
429
+ assert_equal subscriptions, subscriber.subscriptions.sort_by(&:id)
430
+ end
431
+
432
+ def test_string_id_column_joins
433
+ s = Subscriber.create! do |c|
434
+ c.id = "PL"
435
+ end
436
+
437
+ b = Book.create!
438
+
439
+ Subscription.create!(:subscriber_id => "PL", :book_id => b.id)
440
+ s.reload
441
+ s.book_ids = s.book_ids
442
+ end
443
+
444
+ def test_eager_load_has_many_through_with_string_keys
445
+ books = books(:awdr, :rfr)
446
+ subscriber = Subscriber.all.merge!(:includes => :books).find(subscribers(:second).id)
447
+ assert_equal books, subscriber.books.sort_by(&:id)
448
+ end
449
+
450
+ def test_eager_load_belongs_to_with_string_keys
451
+ subscriber = subscribers(:second)
452
+ subscription = Subscription.all.merge!(:includes => :subscriber).find(subscriptions(:webster_awdr).id)
453
+ assert_equal subscriber, subscription.subscriber
454
+ end
455
+
456
+ def test_eager_association_loading_with_explicit_join
457
+ posts = Post.all.merge!(:includes => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id').to_a
458
+ assert_equal 1, posts.length
459
+ end
460
+
461
+ def test_eager_with_has_many_through
462
+ posts_with_comments = people(:michael).posts.merge(:includes => :comments, :order => 'posts.id').to_a
463
+ posts_with_author = people(:michael).posts.merge(:includes => :author, :order => 'posts.id').to_a
464
+ posts_with_comments_and_author = people(:michael).posts.merge(:includes => [ :comments, :author ], :order => 'posts.id').to_a
465
+ assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum + post.comments.size }
466
+ assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
467
+ assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
468
+ end
469
+
470
+ def test_eager_with_has_many_through_a_belongs_to_association
471
+ author = authors(:mary)
472
+ Post.create!(:author => author, :title => "TITLE", :body => "BODY")
473
+ author.author_favorites.create(:favorite_author_id => 1)
474
+ author.author_favorites.create(:favorite_author_id => 2)
475
+ posts_with_author_favorites = author.posts.merge(:includes => :author_favorites).to_a
476
+ assert_no_queries { posts_with_author_favorites.first.author_favorites.first.author_id }
477
+ end
478
+
479
+ def test_eager_with_has_many_through_an_sti_join_model
480
+ author = Author.all.merge!(:includes => :special_post_comments, :order => 'authors.id').first
481
+ assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
482
+ end
483
+
484
+ def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
485
+ author = Author.all.merge!(:includes => :special_nonexistant_post_comments, :order => 'authors.id').first
486
+ assert_equal [], author.special_nonexistant_post_comments
487
+ end
488
+
489
+ def test_eager_with_has_many_through_join_model_with_conditions
490
+ assert_equal Author.all.merge!(:includes => :hello_post_comments,
491
+ :order => 'authors.id').first.hello_post_comments.sort_by(&:id),
492
+ Author.all.merge!(:order => 'authors.id').first.hello_post_comments.sort_by(&:id)
493
+ end
494
+
495
+ def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
496
+ assert_equal comments(:more_greetings), Author.all.merge!(:includes => :comments_with_order_and_conditions).find(authors(:david).id).comments_with_order_and_conditions.first
497
+ end
498
+
499
+ def test_eager_with_has_many_through_join_model_with_include
500
+ author_comments = Author.all.merge!(:includes => :comments_with_include).find(authors(:david).id).comments_with_include.to_a
501
+ assert_no_queries do
502
+ author_comments.first.post.title
503
+ end
504
+ end
505
+
506
+ def test_eager_with_has_many_through_with_conditions_join_model_with_include
507
+ post_tags = Post.find(posts(:welcome).id).misc_tags
508
+ eager_post_tags = Post.all.merge!(:includes => :misc_tags).find(1).misc_tags
509
+ assert_equal post_tags, eager_post_tags
510
+ end
511
+
512
+ def test_eager_with_has_many_through_join_model_ignores_default_includes
513
+ assert_nothing_raised do
514
+ authors(:david).comments_on_posts_with_default_include.to_a
515
+ end
516
+ end
517
+
518
+ def test_eager_with_has_many_and_limit
519
+ posts = Post.all.merge!(:order => 'posts.id asc', :includes => [ :author, :comments ], :limit => 2).to_a
520
+ assert_equal 2, posts.size
521
+ assert_equal 3, posts.inject(0) { |sum, post| sum + post.comments.size }
522
+ end
523
+
524
+ def test_eager_with_has_many_and_limit_and_conditions
525
+ if current_adapter?(:OpenBaseAdapter)
526
+ posts = Post.all.merge!(:includes => [ :author, :comments ], :limit => 2, :where => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id").to_a
527
+ else
528
+ posts = Post.all.merge!(:includes => [ :author, :comments ], :limit => 2, :where => "posts.body = 'hello'", :order => "posts.id").to_a
529
+ end
530
+ assert_equal 2, posts.size
531
+ assert_equal [4,5], posts.collect { |p| p.id }
532
+ end
533
+
534
+ def test_eager_with_has_many_and_limit_and_conditions_array
535
+ if current_adapter?(:OpenBaseAdapter)
536
+ posts = Post.all.merge!(:includes => [ :author, :comments ], :limit => 2, :where => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id").to_a
537
+ else
538
+ posts = Post.all.merge!(:includes => [ :author, :comments ], :limit => 2, :where => [ "posts.body = ?", 'hello' ], :order => "posts.id").to_a
539
+ end
540
+ assert_equal 2, posts.size
541
+ assert_equal [4,5], posts.collect { |p| p.id }
542
+ end
543
+
544
+ def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
545
+ posts = Post.includes(:author, :comments).limit(2).references(:author).where("authors.name = ?", 'David')
546
+ assert_equal 2, posts.size
547
+
548
+ count = Post.includes(:author, :comments).limit(2).references(:author).where("authors.name = ?", 'David').count
549
+ assert_equal posts.size, count
550
+ end
551
+
552
+ def test_eager_with_has_many_and_limit_and_high_offset
553
+ posts = Post.all.merge!(:includes => [ :author, :comments ], :limit => 2, :offset => 10, :where => { 'authors.name' => 'David' }).to_a
554
+ assert_equal 0, posts.size
555
+ end
556
+
557
+ def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions
558
+ assert_queries(1) do
559
+ posts = Post.references(:authors, :comments).
560
+ merge(:includes => [ :author, :comments ], :limit => 2, :offset => 10,
561
+ :where => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ]).to_a
562
+ assert_equal 0, posts.size
563
+ end
564
+ end
565
+
566
+ def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions
567
+ assert_queries(1) do
568
+ posts = Post.all.merge!(:includes => [ :author, :comments ], :limit => 2, :offset => 10,
569
+ :where => { 'authors.name' => 'David', 'comments.body' => 'go crazy' }).to_a
570
+ assert_equal 0, posts.size
571
+ end
572
+ end
573
+
574
+ def test_count_eager_with_has_many_and_limit_and_high_offset
575
+ posts = Post.all.merge!(:includes => [ :author, :comments ], :limit => 2, :offset => 10, :where => { 'authors.name' => 'David' }).count(:all)
576
+ assert_equal 0, posts
577
+ end
578
+
579
+ def test_eager_with_has_many_and_limit_with_no_results
580
+ posts = Post.all.merge!(:includes => [ :author, :comments ], :limit => 2, :where => "posts.title = 'magic forest'").to_a
581
+ assert_equal 0, posts.size
582
+ end
583
+
584
+ def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
585
+ author = authors(:david)
586
+ author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
587
+ assert_equal author_posts_without_comments.size, author.posts.includes(:comments).where('comments.id is null').references(:comments).count
588
+ end
589
+
590
+ def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
591
+ person = people(:michael)
592
+ person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
593
+ assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
594
+ end
595
+
596
+ def test_eager_with_has_and_belongs_to_many_and_limit
597
+ posts = Post.all.merge!(:includes => :categories, :order => "posts.id", :limit => 3).to_a
598
+ assert_equal 3, posts.size
599
+ assert_equal 2, posts[0].categories.size
600
+ assert_equal 1, posts[1].categories.size
601
+ assert_equal 0, posts[2].categories.size
602
+ assert posts[0].categories.include?(categories(:technology))
603
+ assert posts[1].categories.include?(categories(:general))
604
+ end
605
+
606
+ # Since the preloader for habtm gets raw row hashes from the database and then
607
+ # instantiates them, this test ensures that it only instantiates one actual
608
+ # object per record from the database.
609
+ def test_has_and_belongs_to_many_should_not_instantiate_same_records_multiple_times
610
+ welcome = posts(:welcome)
611
+ categories = Category.includes(:posts)
612
+
613
+ general = categories.find { |c| c == categories(:general) }
614
+ technology = categories.find { |c| c == categories(:technology) }
615
+
616
+ post1 = general.posts.to_a.find { |p| p == welcome }
617
+ post2 = technology.posts.to_a.find { |p| p == welcome }
618
+
619
+ assert_equal post1.object_id, post2.object_id
620
+ end
621
+
622
+ def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
623
+ posts =
624
+ authors(:david).posts
625
+ .includes(:comments)
626
+ .where("comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'")
627
+ .references(:comments)
628
+ .limit(2)
629
+ .to_a
630
+ assert_equal 2, posts.size
631
+
632
+ count =
633
+ Post.includes(:comments, :author)
634
+ .where("authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')")
635
+ .references(:authors, :comments)
636
+ .limit(2)
637
+ .count
638
+ assert_equal count, posts.size
639
+ end
640
+
641
+ def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
642
+ posts = nil
643
+ Post.includes(:comments)
644
+ .where("comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'")
645
+ .references(:comments)
646
+ .scoping do
647
+
648
+ posts = authors(:david).posts.limit(2).to_a
649
+ assert_equal 2, posts.size
650
+ end
651
+
652
+ Post.includes(:comments, :author)
653
+ .where("authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')")
654
+ .references(:authors, :comments)
655
+ .scoping do
656
+
657
+ count = Post.limit(2).count
658
+ assert_equal count, posts.size
659
+ end
660
+ end
661
+
662
+ def test_eager_association_loading_with_habtm
663
+ posts = Post.all.merge!(:includes => :categories, :order => "posts.id").to_a
664
+ assert_equal 2, posts[0].categories.size
665
+ assert_equal 1, posts[1].categories.size
666
+ assert_equal 0, posts[2].categories.size
667
+ assert posts[0].categories.include?(categories(:technology))
668
+ assert posts[1].categories.include?(categories(:general))
669
+ end
670
+
671
+ def test_eager_with_inheritance
672
+ SpecialPost.all.merge!(:includes => [ :comments ]).to_a
673
+ end
674
+
675
+ def test_eager_has_one_with_association_inheritance
676
+ post = Post.all.merge!(:includes => [ :very_special_comment ]).find(4)
677
+ assert_equal "VerySpecialComment", post.very_special_comment.class.to_s
678
+ end
679
+
680
+ def test_eager_has_many_with_association_inheritance
681
+ post = Post.all.merge!(:includes => [ :special_comments ]).find(4)
682
+ post.special_comments.each do |special_comment|
683
+ assert special_comment.is_a?(SpecialComment)
684
+ end
685
+ end
686
+
687
+ def test_eager_habtm_with_association_inheritance
688
+ post = Post.all.merge!(:includes => [ :special_categories ]).find(6)
689
+ assert_equal 1, post.special_categories.size
690
+ post.special_categories.each do |special_category|
691
+ assert_equal "SpecialCategory", special_category.class.to_s
692
+ end
693
+ end
694
+
695
+ def test_eager_with_has_one_dependent_does_not_destroy_dependent
696
+ assert_not_nil companies(:first_firm).account
697
+ f = Firm.all.merge!(:includes => :account,
698
+ :where => ["companies.name = ?", "37signals"]).first
699
+ assert_not_nil f.account
700
+ assert_equal companies(:first_firm, :reload).account, f.account
701
+ end
702
+
703
+ def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
704
+ author = authors(:david)
705
+ posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
706
+ assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
707
+ assert_equal posts_with_no_comments, author.posts_with_no_comments
708
+ end
709
+
710
+ def test_eager_with_invalid_association_reference
711
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
712
+ Post.all.merge!(:includes=> :monkeys ).find(6)
713
+ }
714
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
715
+ Post.all.merge!(:includes=>[ :monkeys ]).find(6)
716
+ }
717
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
718
+ Post.all.merge!(:includes=>[ 'monkeys' ]).find(6)
719
+ }
720
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
721
+ Post.all.merge!(:includes=>[ :monkeys, :elephants ]).find(6)
722
+ }
723
+ end
724
+
725
+ def test_eager_with_default_scope
726
+ developer = EagerDeveloperWithDefaultScope.where(:name => 'David').first
727
+ projects = Project.order(:id).to_a
728
+ assert_no_queries do
729
+ assert_equal(projects, developer.projects)
730
+ end
731
+ end
732
+
733
+ def test_eager_with_default_scope_as_class_method
734
+ developer = EagerDeveloperWithClassMethodDefaultScope.where(:name => 'David').first
735
+ projects = Project.order(:id).to_a
736
+ assert_no_queries do
737
+ assert_equal(projects, developer.projects)
738
+ end
739
+ end
740
+
741
+ def test_eager_with_default_scope_as_lambda
742
+ developer = EagerDeveloperWithLambdaDefaultScope.where(:name => 'David').first
743
+ projects = Project.order(:id).to_a
744
+ assert_no_queries do
745
+ assert_equal(projects, developer.projects)
746
+ end
747
+ end
748
+
749
+ def test_eager_with_default_scope_as_block
750
+ developer = EagerDeveloperWithBlockDefaultScope.where(:name => 'David').first
751
+ projects = Project.order(:id).to_a
752
+ assert_no_queries do
753
+ assert_equal(projects, developer.projects)
754
+ end
755
+ end
756
+
757
+ def test_eager_with_default_scope_as_callable
758
+ developer = EagerDeveloperWithCallableDefaultScope.where(:name => 'David').first
759
+ projects = Project.order(:id).to_a
760
+ assert_no_queries do
761
+ assert_equal(projects, developer.projects)
762
+ end
763
+ end
764
+
765
+ def find_all_ordered(className, include=nil)
766
+ className.all.merge!(:order=>"#{className.table_name}.#{className.primary_key}", :includes=>include).to_a
767
+ end
768
+
769
+ def test_limited_eager_with_order
770
+ assert_equal(
771
+ posts(:thinking, :sti_comments),
772
+ Post.all.merge!(
773
+ :includes => [:author, :comments], :where => { 'authors.name' => 'David' },
774
+ :order => 'UPPER(posts.title)', :limit => 2, :offset => 1
775
+ ).to_a
776
+ )
777
+ assert_equal(
778
+ posts(:sti_post_and_comments, :sti_comments),
779
+ Post.all.merge!(
780
+ :includes => [:author, :comments], :where => { 'authors.name' => 'David' },
781
+ :order => 'UPPER(posts.title) DESC', :limit => 2, :offset => 1
782
+ ).to_a
783
+ )
784
+ end
785
+
786
+ def test_limited_eager_with_multiple_order_columns
787
+ assert_equal(
788
+ posts(:thinking, :sti_comments),
789
+ Post.all.merge!(
790
+ :includes => [:author, :comments], :where => { 'authors.name' => 'David' },
791
+ :order => ['UPPER(posts.title)', 'posts.id'], :limit => 2, :offset => 1
792
+ ).to_a
793
+ )
794
+ assert_equal(
795
+ posts(:sti_post_and_comments, :sti_comments),
796
+ Post.all.merge!(
797
+ :includes => [:author, :comments], :where => { 'authors.name' => 'David' },
798
+ :order => ['UPPER(posts.title) DESC', 'posts.id'], :limit => 2, :offset => 1
799
+ ).to_a
800
+ )
801
+ end
802
+
803
+ def test_limited_eager_with_numeric_in_association
804
+ assert_equal(
805
+ people(:david, :susan),
806
+ Person.references(:number1_fans_people).merge(
807
+ :includes => [:readers, :primary_contact, :number1_fan],
808
+ :where => "number1_fans_people.first_name like 'M%'",
809
+ :order => 'people.id', :limit => 2, :offset => 0
810
+ ).to_a
811
+ )
812
+ end
813
+
814
+ def test_preload_with_interpolation
815
+ post = Post.includes(:comments_with_interpolated_conditions).find(posts(:welcome).id)
816
+ assert_equal [comments(:greetings)], post.comments_with_interpolated_conditions
817
+
818
+ post = Post.joins(:comments_with_interpolated_conditions).find(posts(:welcome).id)
819
+ assert_equal [comments(:greetings)], post.comments_with_interpolated_conditions
820
+ end
821
+
822
+ def test_polymorphic_type_condition
823
+ post = Post.all.merge!(:includes => :taggings).find(posts(:thinking).id)
824
+ assert post.taggings.include?(taggings(:thinking_general))
825
+ post = SpecialPost.all.merge!(:includes => :taggings).find(posts(:thinking).id)
826
+ assert post.taggings.include?(taggings(:thinking_general))
827
+ end
828
+
829
+ def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
830
+ # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
831
+ def assert_equal_after_sort(item1, item2, item3 = nil)
832
+ assert_equal(item1.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id})
833
+ assert_equal(item3.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) if item3
834
+ end
835
+ # Test regular association, association with conditions, association with
836
+ # STI, and association with conditions assured not to be true
837
+ post_types = [:posts, :other_posts, :special_posts]
838
+ # test both has_many and has_and_belongs_to_many
839
+ [Author, Category].each do |className|
840
+ d1 = find_all_ordered(className)
841
+ # test including all post types at once
842
+ d2 = find_all_ordered(className, post_types)
843
+ d1.each_index do |i|
844
+ assert_equal(d1[i], d2[i])
845
+ assert_equal_after_sort(d1[i].posts, d2[i].posts)
846
+ post_types[1..-1].each do |post_type|
847
+ # test including post_types together
848
+ d3 = find_all_ordered(className, [:posts, post_type])
849
+ assert_equal(d1[i], d3[i])
850
+ assert_equal_after_sort(d1[i].posts, d3[i].posts)
851
+ assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
852
+ end
853
+ end
854
+ end
855
+ end
856
+
857
+ def test_eager_with_multiple_associations_with_same_table_has_one
858
+ d1 = find_all_ordered(Firm)
859
+ d2 = find_all_ordered(Firm, :account)
860
+ d1.each_index do |i|
861
+ assert_equal(d1[i], d2[i])
862
+ assert_equal(d1[i].account, d2[i].account)
863
+ end
864
+ end
865
+
866
+ def test_eager_with_multiple_associations_with_same_table_belongs_to
867
+ firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
868
+ d1 = find_all_ordered(Client)
869
+ d2 = find_all_ordered(Client, firm_types)
870
+ d1.each_index do |i|
871
+ assert_equal(d1[i], d2[i])
872
+ firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
873
+ end
874
+ end
875
+ def test_eager_with_valid_association_as_string_not_symbol
876
+ assert_nothing_raised { Post.all.merge!(:includes => 'comments').to_a }
877
+ end
878
+
879
+ def test_eager_with_floating_point_numbers
880
+ assert_queries(2) do
881
+ # Before changes, the floating point numbers will be interpreted as table names and will cause this to run in one query
882
+ Comment.all.merge!(:where => "123.456 = 123.456", :includes => :post).to_a
883
+ end
884
+ end
885
+
886
+ def test_preconfigured_includes_with_belongs_to
887
+ author = posts(:welcome).author_with_posts
888
+ assert_no_queries {assert_equal 5, author.posts.size}
889
+ end
890
+
891
+ def test_preconfigured_includes_with_has_one
892
+ comment = posts(:sti_comments).very_special_comment_with_post
893
+ assert_no_queries {assert_equal posts(:sti_comments), comment.post}
894
+ end
895
+
896
+ def test_preconfigured_includes_with_has_many
897
+ posts = authors(:david).posts_with_comments
898
+ one = posts.detect { |p| p.id == 1 }
899
+ assert_no_queries do
900
+ assert_equal 5, posts.size
901
+ assert_equal 2, one.comments.size
902
+ end
903
+ end
904
+
905
+ def test_preconfigured_includes_with_habtm
906
+ posts = authors(:david).posts_with_categories
907
+ one = posts.detect { |p| p.id == 1 }
908
+ assert_no_queries do
909
+ assert_equal 5, posts.size
910
+ assert_equal 2, one.categories.size
911
+ end
912
+ end
913
+
914
+ def test_preconfigured_includes_with_has_many_and_habtm
915
+ posts = authors(:david).posts_with_comments_and_categories
916
+ one = posts.detect { |p| p.id == 1 }
917
+ assert_no_queries do
918
+ assert_equal 5, posts.size
919
+ assert_equal 2, one.comments.size
920
+ assert_equal 2, one.categories.size
921
+ end
922
+ end
923
+
924
+ def test_count_with_include
925
+ if current_adapter?(:SybaseAdapter)
926
+ assert_equal 3, authors(:david).posts_with_comments.where("len(comments.body) > 15").references(:comments).count
927
+ elsif current_adapter?(:OpenBaseAdapter)
928
+ assert_equal 3, authors(:david).posts_with_comments.where("length(FETCHBLOB(comments.body)) > 15").references(:comments).count
929
+ else
930
+ assert_equal 3, authors(:david).posts_with_comments.where("length(comments.body) > 15").references(:comments).count
931
+ end
932
+ end
933
+
934
+ def test_load_with_sti_sharing_association
935
+ assert_queries(2) do #should not do 1 query per subclass
936
+ Comment.includes(:post).to_a
937
+ end
938
+ end
939
+
940
+ def test_conditions_on_join_table_with_include_and_limit
941
+ assert_equal 3, Developer.all.merge!(:includes => 'projects', :where => { 'developers_projects.access_level' => 1 }, :limit => 5).to_a.size
942
+ end
943
+
944
+ def test_dont_create_temporary_active_record_instances
945
+ Developer.instance_count = 0
946
+ developers = Developer.all.merge!(:includes => 'projects', :where => { 'developers_projects.access_level' => 1 }, :limit => 5).to_a
947
+ assert_equal developers.count, Developer.instance_count
948
+ end
949
+
950
+ def test_order_on_join_table_with_include_and_limit
951
+ assert_equal 5, Developer.all.merge!(:includes => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).to_a.size
952
+ end
953
+
954
+ def test_eager_loading_with_order_on_joined_table_preloads
955
+ posts = assert_queries(2) do
956
+ Post.all.merge!(:joins => :comments, :includes => :author, :order => 'comments.id DESC').to_a
957
+ end
958
+ assert_equal posts(:eager_other), posts[1]
959
+ assert_equal authors(:mary), assert_no_queries { posts[1].author}
960
+ end
961
+
962
+ def test_eager_loading_with_conditions_on_joined_table_preloads
963
+ posts = assert_queries(2) do
964
+ Post.all.merge!(:select => 'distinct posts.*', :includes => :author, :joins => [:comments], :where => "comments.body like 'Thank you%'", :order => 'posts.id').to_a
965
+ end
966
+ assert_equal [posts(:welcome)], posts
967
+ assert_equal authors(:david), assert_no_queries { posts[0].author}
968
+
969
+ posts = assert_queries(2) do
970
+ Post.all.merge!(:select => 'distinct posts.*', :includes => :author, :joins => [:comments], :where => "comments.body like 'Thank you%'", :order => 'posts.id').to_a
971
+ end
972
+ assert_equal [posts(:welcome)], posts
973
+ assert_equal authors(:david), assert_no_queries { posts[0].author}
974
+
975
+ posts = assert_queries(2) do
976
+ Post.all.merge!(:includes => :author, :joins => {:taggings => :tag}, :where => "tags.name = 'General'", :order => 'posts.id').to_a
977
+ end
978
+ assert_equal posts(:welcome, :thinking), posts
979
+
980
+ posts = assert_queries(2) do
981
+ Post.all.merge!(:includes => :author, :joins => {:taggings => {:tag => :taggings}}, :where => "taggings_tags.super_tag_id=2", :order => 'posts.id').to_a
982
+ end
983
+ assert_equal posts(:welcome, :thinking), posts
984
+ end
985
+
986
+ def test_preload_has_many_with_association_condition_and_default_scope
987
+ post = Post.create!(:title => 'Beaches', :body => "I like beaches!")
988
+ Reader.create! :person => people(:david), :post => post
989
+ LazyReader.create! :person => people(:susan), :post => post
990
+
991
+ assert_equal 1, post.lazy_readers.to_a.size
992
+ assert_equal 2, post.lazy_readers_skimmers_or_not.to_a.size
993
+
994
+ post_with_readers = Post.includes(:lazy_readers_skimmers_or_not).find(post.id)
995
+ assert_equal 2, post_with_readers.lazy_readers_skimmers_or_not.to_a.size
996
+ end
997
+
998
+ def test_eager_loading_with_conditions_on_string_joined_table_preloads
999
+ posts = assert_queries(2) do
1000
+ Post.all.merge!(:select => 'distinct posts.*', :includes => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :where => "comments.body like 'Thank you%'", :order => 'posts.id').to_a
1001
+ end
1002
+ assert_equal [posts(:welcome)], posts
1003
+ assert_equal authors(:david), assert_no_queries { posts[0].author}
1004
+
1005
+ posts = assert_queries(2) do
1006
+ Post.all.merge!(:select => 'distinct posts.*', :includes => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :where => "comments.body like 'Thank you%'", :order => 'posts.id').to_a
1007
+ end
1008
+ assert_equal [posts(:welcome)], posts
1009
+ assert_equal authors(:david), assert_no_queries { posts[0].author}
1010
+ end
1011
+
1012
+ def test_eager_loading_with_select_on_joined_table_preloads
1013
+ posts = assert_queries(2) do
1014
+ Post.all.merge!(:select => 'posts.*, authors.name as author_name', :includes => :comments, :joins => :author, :order => 'posts.id').to_a
1015
+ end
1016
+ assert_equal 'David', posts[0].author_name
1017
+ assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments}
1018
+ end
1019
+
1020
+ def test_eager_loading_with_conditions_on_join_model_preloads
1021
+ authors = assert_queries(2) do
1022
+ Author.all.merge!(:includes => :author_address, :joins => :comments, :where => "posts.title like 'Welcome%'").to_a
1023
+ end
1024
+ assert_equal authors(:david), authors[0]
1025
+ assert_equal author_addresses(:david_address), authors[0].author_address
1026
+ end
1027
+
1028
+ def test_preload_belongs_to_uses_exclusive_scope
1029
+ people = Person.males.merge(:includes => :primary_contact).to_a
1030
+ assert_not_equal people.length, 0
1031
+ people.each do |person|
1032
+ assert_no_queries {assert_not_nil person.primary_contact}
1033
+ assert_equal Person.find(person.id).primary_contact, person.primary_contact
1034
+ end
1035
+ end
1036
+
1037
+ def test_preload_has_many_uses_exclusive_scope
1038
+ people = Person.males.includes(:agents).to_a
1039
+ people.each do |person|
1040
+ assert_equal Person.find(person.id).agents, person.agents
1041
+ end
1042
+ end
1043
+
1044
+ def test_preload_has_many_using_primary_key
1045
+ expected = Firm.first.clients_using_primary_key.to_a
1046
+ firm = Firm.includes(:clients_using_primary_key).first
1047
+ assert_no_queries do
1048
+ assert_equal expected, firm.clients_using_primary_key
1049
+ end
1050
+ end
1051
+
1052
+ def test_include_has_many_using_primary_key
1053
+ expected = Firm.find(1).clients_using_primary_key.sort_by(&:name)
1054
+ # Oracle adapter truncates alias to 30 characters
1055
+ if current_adapter?(:OracleAdapter)
1056
+ firm = Firm.all.merge!(:includes => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies'[0,30]+'.name').find(1)
1057
+ else
1058
+ firm = Firm.all.merge!(:includes => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name').find(1)
1059
+ end
1060
+ assert_no_queries do
1061
+ assert_equal expected, firm.clients_using_primary_key
1062
+ end
1063
+ end
1064
+
1065
+ def test_preload_has_one_using_primary_key
1066
+ expected = accounts(:signals37)
1067
+ firm = Firm.all.merge!(:includes => :account_using_primary_key, :order => 'companies.id').first
1068
+ assert_no_queries do
1069
+ assert_equal expected, firm.account_using_primary_key
1070
+ end
1071
+ end
1072
+
1073
+ def test_include_has_one_using_primary_key
1074
+ expected = accounts(:signals37)
1075
+ firm = Firm.all.merge!(:includes => :account_using_primary_key, :order => 'accounts.id').to_a.detect {|f| f.id == 1}
1076
+ assert_no_queries do
1077
+ assert_equal expected, firm.account_using_primary_key
1078
+ end
1079
+ end
1080
+
1081
+ def test_preloading_empty_belongs_to
1082
+ c = Client.create!(:name => 'Foo', :client_of => Company.maximum(:id) + 1)
1083
+
1084
+ client = assert_queries(2) { Client.preload(:firm).find(c.id) }
1085
+ assert_no_queries { assert_nil client.firm }
1086
+ end
1087
+
1088
+ def test_preloading_empty_belongs_to_polymorphic
1089
+ t = Tagging.create!(:taggable_type => 'Post', :taggable_id => Post.maximum(:id) + 1, :tag => tags(:general))
1090
+
1091
+ tagging = assert_queries(2) { Tagging.preload(:taggable).find(t.id) }
1092
+ assert_no_queries { assert_nil tagging.taggable }
1093
+ end
1094
+
1095
+ def test_preloading_through_empty_belongs_to
1096
+ c = Client.create!(:name => 'Foo', :client_of => Company.maximum(:id) + 1)
1097
+
1098
+ client = assert_queries(2) { Client.preload(:accounts).find(c.id) }
1099
+ assert_no_queries { assert client.accounts.empty? }
1100
+ end
1101
+
1102
+ def test_preloading_has_many_through_with_uniq
1103
+ mary = Author.includes(:unique_categorized_posts).where(:id => authors(:mary).id).first
1104
+ assert_equal 1, mary.unique_categorized_posts.length
1105
+ assert_equal 1, mary.unique_categorized_post_ids.length
1106
+ end
1107
+
1108
+ def test_preloading_polymorphic_with_custom_foreign_type
1109
+ sponsor = sponsors(:moustache_club_sponsor_for_groucho)
1110
+ groucho = members(:groucho)
1111
+
1112
+ sponsor = assert_queries(2) {
1113
+ Sponsor.includes(:thing).where(:id => sponsor.id).first
1114
+ }
1115
+ assert_no_queries { assert_equal groucho, sponsor.thing }
1116
+ end
1117
+
1118
+ def test_joins_with_includes_should_preload_via_joins
1119
+ post = assert_queries(1) { Post.includes(:comments).joins(:comments).order('posts.id desc').to_a.first }
1120
+
1121
+ assert_queries(0) do
1122
+ assert_not_equal 0, post.comments.to_a.count
1123
+ end
1124
+ end
1125
+
1126
+ def test_join_eager_with_empty_order_should_generate_valid_sql
1127
+ assert_nothing_raised(ActiveRecord::StatementInvalid) do
1128
+ Post.includes(:comments).order("").where(:comments => {:body => "Thank you for the welcome"}).first
1129
+ end
1130
+ end
1131
+
1132
+ def test_join_eager_with_nil_order_should_generate_valid_sql
1133
+ assert_nothing_raised(ActiveRecord::StatementInvalid) do
1134
+ Post.includes(:comments).order(nil).where(:comments => {:body => "Thank you for the welcome"}).first
1135
+ end
1136
+ end
1137
+
1138
+ def test_deep_including_through_habtm
1139
+ posts = Post.all.merge!(:includes => {:categories => :categorizations}, :order => "posts.id").to_a
1140
+ assert_no_queries { assert_equal 2, posts[0].categories[0].categorizations.length }
1141
+ assert_no_queries { assert_equal 1, posts[0].categories[1].categorizations.length }
1142
+ assert_no_queries { assert_equal 2, posts[1].categories[0].categorizations.length }
1143
+ end
1144
+
1145
+ test "scoping with a circular preload" do
1146
+ assert_equal Comment.find(1), Comment.preload(:post => :comments).scoping { Comment.find(1) }
1147
+ end
1148
+
1149
+ test "circular preload does not modify unscoped" do
1150
+ expected = FirstPost.unscoped.find(2)
1151
+ FirstPost.preload(:comments => :first_post).find(1)
1152
+ assert_equal expected, FirstPost.unscoped.find(2)
1153
+ end
1154
+
1155
+ test "preload ignores the scoping" do
1156
+ assert_equal(
1157
+ Comment.find(1).post,
1158
+ Post.where('1 = 0').scoping { Comment.preload(:post).find(1).post }
1159
+ )
1160
+ end
1161
+
1162
+ test "preloading does not cache has many association subset when preloaded with a through association" do
1163
+ author = Author.includes(:comments_with_order_and_conditions, :posts).first
1164
+ assert_no_queries { assert_equal 2, author.comments_with_order_and_conditions.size }
1165
+ assert_no_queries { assert_equal 5, author.posts.size, "should not cache a subset of the association" }
1166
+ end
1167
+
1168
+ test "preloading a through association twice does not reset it" do
1169
+ members = Member.includes(current_membership: :club).includes(:club).to_a
1170
+ assert_no_queries {
1171
+ assert_equal 3, members.map(&:current_membership).map(&:club).size
1172
+ }
1173
+ end
1174
+
1175
+ test "works in combination with order(:symbol)" do
1176
+ author = Author.includes(:posts).references(:posts).order(:name).where('posts.title IS NOT NULL').first
1177
+ assert_equal authors(:bob), author
1178
+ end
1179
+ end