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,317 @@
1
+ require "cases/helper"
2
+ require 'models/club'
3
+ require 'models/member_type'
4
+ require 'models/member'
5
+ require 'models/membership'
6
+ require 'models/sponsor'
7
+ require 'models/organization'
8
+ require 'models/member_detail'
9
+ require 'models/minivan'
10
+ require 'models/dashboard'
11
+ require 'models/speedometer'
12
+ require 'models/category'
13
+ require 'models/author'
14
+ require 'models/essay'
15
+ require 'models/owner'
16
+ require 'models/post'
17
+ require 'models/comment'
18
+
19
+ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
20
+ fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations, :minivans,
21
+ :dashboards, :speedometers, :authors, :posts, :comments, :categories, :essays, :owners
22
+
23
+ def setup
24
+ @member = members(:groucho)
25
+ end
26
+
27
+ def test_has_one_through_with_has_one
28
+ assert_equal clubs(:boring_club), @member.club
29
+ end
30
+
31
+ def test_creating_association_creates_through_record
32
+ new_member = Member.create(:name => "Chris")
33
+ new_member.club = Club.create(:name => "LRUG")
34
+ assert_not_nil new_member.current_membership
35
+ assert_not_nil new_member.club
36
+ end
37
+
38
+ def test_creating_association_builds_through_record_for_new
39
+ new_member = Member.new(:name => "Jane")
40
+ new_member.club = clubs(:moustache_club)
41
+ assert new_member.current_membership
42
+ assert_equal clubs(:moustache_club), new_member.current_membership.club
43
+ assert_equal clubs(:moustache_club), new_member.club
44
+ assert new_member.save
45
+ assert_equal clubs(:moustache_club), new_member.club
46
+ end
47
+
48
+ def test_replace_target_record
49
+ new_club = Club.create(:name => "Marx Bros")
50
+ @member.club = new_club
51
+ @member.reload
52
+ assert_equal new_club, @member.club
53
+ end
54
+
55
+ def test_replacing_target_record_deletes_old_association
56
+ assert_no_difference "Membership.count" do
57
+ new_club = Club.create(:name => "Bananarama")
58
+ @member.club = new_club
59
+ @member.reload
60
+ end
61
+ end
62
+
63
+ def test_set_record_to_nil_should_delete_association
64
+ @member.club = nil
65
+ @member.reload
66
+ assert_equal nil, @member.current_membership
67
+ assert_nil @member.club
68
+ end
69
+
70
+ def test_has_one_through_polymorphic
71
+ assert_equal clubs(:moustache_club), @member.sponsor_club
72
+ end
73
+
74
+ def test_has_one_through_eager_loading
75
+ members = assert_queries(3) do #base table, through table, clubs table
76
+ Member.all.merge!(:includes => :club, :where => ["name = ?", "Groucho Marx"]).to_a
77
+ end
78
+ assert_equal 1, members.size
79
+ assert_not_nil assert_no_queries {members[0].club}
80
+ end
81
+
82
+ def test_has_one_through_eager_loading_through_polymorphic
83
+ members = assert_queries(3) do #base table, through table, clubs table
84
+ Member.all.merge!(:includes => :sponsor_club, :where => ["name = ?", "Groucho Marx"]).to_a
85
+ end
86
+ assert_equal 1, members.size
87
+ assert_not_nil assert_no_queries {members[0].sponsor_club}
88
+ end
89
+
90
+ def test_has_one_through_with_conditions_eager_loading
91
+ # conditions on the through table
92
+ assert_equal clubs(:moustache_club), Member.all.merge!(:includes => :favourite_club).find(@member.id).favourite_club
93
+ memberships(:membership_of_favourite_club).update_columns(favourite: false)
94
+ assert_equal nil, Member.all.merge!(:includes => :favourite_club).find(@member.id).reload.favourite_club
95
+
96
+ # conditions on the source table
97
+ assert_equal clubs(:moustache_club), Member.all.merge!(:includes => :hairy_club).find(@member.id).hairy_club
98
+ clubs(:moustache_club).update_columns(name: "Association of Clean-Shaven Persons")
99
+ assert_equal nil, Member.all.merge!(:includes => :hairy_club).find(@member.id).reload.hairy_club
100
+ end
101
+
102
+ def test_has_one_through_polymorphic_with_source_type
103
+ assert_equal members(:groucho), clubs(:moustache_club).sponsored_member
104
+ end
105
+
106
+ def test_eager_has_one_through_polymorphic_with_source_type
107
+ clubs = Club.all.merge!(:includes => :sponsored_member, :where => ["name = ?","Moustache and Eyebrow Fancier Club"]).to_a
108
+ # Only the eyebrow fanciers club has a sponsored_member
109
+ assert_not_nil assert_no_queries {clubs[0].sponsored_member}
110
+ end
111
+
112
+ def test_has_one_through_nonpreload_eagerloading
113
+ members = assert_queries(1) do
114
+ Member.all.merge!(:includes => :club, :where => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name').to_a #force fallback
115
+ end
116
+ assert_equal 1, members.size
117
+ assert_not_nil assert_no_queries {members[0].club}
118
+ end
119
+
120
+ def test_has_one_through_nonpreload_eager_loading_through_polymorphic
121
+ members = assert_queries(1) do
122
+ Member.all.merge!(:includes => :sponsor_club, :where => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name').to_a #force fallback
123
+ end
124
+ assert_equal 1, members.size
125
+ assert_not_nil assert_no_queries {members[0].sponsor_club}
126
+ end
127
+
128
+ def test_has_one_through_nonpreload_eager_loading_through_polymorphic_with_more_than_one_through_record
129
+ Sponsor.new(:sponsor_club => clubs(:crazy_club), :sponsorable => members(:groucho)).save!
130
+ members = assert_queries(1) do
131
+ Member.all.merge!(:includes => :sponsor_club, :where => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name DESC').to_a #force fallback
132
+ end
133
+ assert_equal 1, members.size
134
+ assert_not_nil assert_no_queries { members[0].sponsor_club }
135
+ assert_equal clubs(:crazy_club), members[0].sponsor_club
136
+ end
137
+
138
+ def test_uninitialized_has_one_through_should_return_nil_for_unsaved_record
139
+ assert_nil Member.new.club
140
+ end
141
+
142
+ def test_assigning_association_correctly_assigns_target
143
+ new_member = Member.create(:name => "Chris")
144
+ new_member.club = new_club = Club.create(:name => "LRUG")
145
+ assert_equal new_club, new_member.association(:club).target
146
+ end
147
+
148
+ def test_has_one_through_proxy_should_not_respond_to_private_methods
149
+ assert_raise(NoMethodError) { clubs(:moustache_club).private_method }
150
+ assert_raise(NoMethodError) { @member.club.private_method }
151
+ end
152
+
153
+ def test_has_one_through_proxy_should_respond_to_private_methods_via_send
154
+ clubs(:moustache_club).send(:private_method)
155
+ @member.club.send(:private_method)
156
+ end
157
+
158
+ def test_assigning_to_has_one_through_preserves_decorated_join_record
159
+ @organization = organizations(:nsa)
160
+ assert_difference 'MemberDetail.count', 1 do
161
+ @member_detail = MemberDetail.new(:extra_data => 'Extra')
162
+ @member.member_detail = @member_detail
163
+ @member.organization = @organization
164
+ end
165
+ assert_equal @organization, @member.organization
166
+ assert @organization.members.include?(@member)
167
+ assert_equal 'Extra', @member.member_detail.extra_data
168
+ end
169
+
170
+ def test_reassigning_has_one_through
171
+ @organization = organizations(:nsa)
172
+ @new_organization = organizations(:discordians)
173
+
174
+ assert_difference 'MemberDetail.count', 1 do
175
+ @member_detail = MemberDetail.new(:extra_data => 'Extra')
176
+ @member.member_detail = @member_detail
177
+ @member.organization = @organization
178
+ end
179
+ assert_equal @organization, @member.organization
180
+ assert_equal 'Extra', @member.member_detail.extra_data
181
+ assert @organization.members.include?(@member)
182
+ assert !@new_organization.members.include?(@member)
183
+
184
+ assert_no_difference 'MemberDetail.count' do
185
+ @member.organization = @new_organization
186
+ end
187
+ assert_equal @new_organization, @member.organization
188
+ assert_equal 'Extra', @member.member_detail.extra_data
189
+ assert !@organization.members.include?(@member)
190
+ assert @new_organization.members.include?(@member)
191
+ end
192
+
193
+ def test_preloading_has_one_through_on_belongs_to
194
+ assert_not_nil @member.member_type
195
+ @organization = organizations(:nsa)
196
+ @member_detail = MemberDetail.new
197
+ @member.member_detail = @member_detail
198
+ @member.organization = @organization
199
+ @member_details = assert_queries(3) do
200
+ MemberDetail.all.merge!(:includes => :member_type).to_a
201
+ end
202
+ @new_detail = @member_details[0]
203
+ assert @new_detail.send(:association, :member_type).loaded?
204
+ assert_not_nil assert_no_queries { @new_detail.member_type }
205
+ end
206
+
207
+ def test_save_of_record_with_loaded_has_one_through
208
+ @club = @member.club
209
+ assert_not_nil @club.sponsored_member
210
+
211
+ assert_nothing_raised do
212
+ Club.find(@club.id).save!
213
+ Club.all.merge!(:includes => :sponsored_member).find(@club.id).save!
214
+ end
215
+
216
+ @club.sponsor.destroy
217
+
218
+ assert_nothing_raised do
219
+ Club.find(@club.id).save!
220
+ Club.all.merge!(:includes => :sponsored_member).find(@club.id).save!
221
+ end
222
+ end
223
+
224
+ def test_through_belongs_to_after_destroy
225
+ @member_detail = MemberDetail.new(:extra_data => 'Extra')
226
+ @member.member_detail = @member_detail
227
+ @member.save!
228
+
229
+ assert_not_nil @member_detail.member_type
230
+ @member_detail.destroy
231
+ assert_queries(1) do
232
+ assert_not_nil @member_detail.member_type(true)
233
+ end
234
+
235
+ @member_detail.member.destroy
236
+ assert_queries(1) do
237
+ assert_nil @member_detail.member_type(true)
238
+ end
239
+ end
240
+
241
+ def test_value_is_properly_quoted
242
+ minivan = Minivan.find('m1')
243
+ assert_nothing_raised do
244
+ minivan.dashboard
245
+ end
246
+ end
247
+
248
+ def test_has_one_through_polymorphic_with_primary_key_option
249
+ assert_equal categories(:general), authors(:david).essay_category
250
+
251
+ authors = Author.joins(:essay_category).where('categories.id' => categories(:general).id)
252
+ assert_equal authors(:david), authors.first
253
+
254
+ assert_equal owners(:blackbeard), authors(:david).essay_owner
255
+
256
+ authors = Author.joins(:essay_owner).where("owners.name = 'blackbeard'")
257
+ assert_equal authors(:david), authors.first
258
+ end
259
+
260
+ def test_has_one_through_with_primary_key_option
261
+ assert_equal categories(:general), authors(:david).essay_category_2
262
+
263
+ authors = Author.joins(:essay_category_2).where('categories.id' => categories(:general).id)
264
+ assert_equal authors(:david), authors.first
265
+ end
266
+
267
+ def test_has_one_through_with_default_scope_on_join_model
268
+ assert_equal posts(:welcome).comments.order('id').first, authors(:david).comment_on_first_post
269
+ end
270
+
271
+ def test_has_one_through_many_raises_exception
272
+ assert_raise(ActiveRecord::HasOneThroughCantAssociateThroughCollection) do
273
+ members(:groucho).club_through_many
274
+ end
275
+ end
276
+
277
+ def test_has_one_through_belongs_to_should_update_when_the_through_foreign_key_changes
278
+ minivan = minivans(:cool_first)
279
+
280
+ minivan.dashboard
281
+ proxy = minivan.send(:association_instance_get, :dashboard)
282
+
283
+ assert !proxy.stale_target?
284
+ assert_equal dashboards(:cool_first), minivan.dashboard
285
+
286
+ minivan.speedometer_id = speedometers(:second).id
287
+
288
+ assert proxy.stale_target?
289
+ assert_equal dashboards(:second), minivan.dashboard
290
+ end
291
+
292
+ def test_has_one_through_belongs_to_setting_belongs_to_foreign_key_after_nil_target_loaded
293
+ minivan = Minivan.new
294
+
295
+ minivan.dashboard
296
+ proxy = minivan.send(:association_instance_get, :dashboard)
297
+
298
+ minivan.speedometer_id = speedometers(:second).id
299
+
300
+ assert proxy.stale_target?
301
+ assert_equal dashboards(:second), minivan.dashboard
302
+ end
303
+
304
+ def test_assigning_has_one_through_belongs_to_with_new_record_owner
305
+ minivan = Minivan.new
306
+ dashboard = dashboards(:cool_first)
307
+
308
+ minivan.dashboard = dashboard
309
+
310
+ assert_equal dashboard, minivan.dashboard
311
+ assert_equal dashboard, minivan.speedometer.dashboard
312
+ end
313
+
314
+ def test_has_one_through_with_custom_select_on_join_model_default_scope
315
+ assert_equal clubs(:boring_club), members(:groucho).selected_club
316
+ end
317
+ end
@@ -0,0 +1,115 @@
1
+ require "cases/helper"
2
+ require 'models/post'
3
+ require 'models/comment'
4
+ require 'models/author'
5
+ require 'models/essay'
6
+ require 'models/category'
7
+ require 'models/categorization'
8
+ require 'models/person'
9
+ require 'models/tagging'
10
+ require 'models/tag'
11
+
12
+ class InnerJoinAssociationTest < ActiveRecord::TestCase
13
+ fixtures :authors, :essays, :posts, :comments, :categories, :categories_posts, :categorizations,
14
+ :taggings, :tags
15
+
16
+ def test_construct_finder_sql_applies_aliases_tables_on_association_conditions
17
+ result = Author.joins(:thinking_posts, :welcome_posts).to_a
18
+ assert_equal authors(:david), result.first
19
+ end
20
+
21
+ def test_construct_finder_sql_does_not_table_name_collide_on_duplicate_associations
22
+ assert_nothing_raised do
23
+ sql = Person.joins(:agents => {:agents => :agents}).joins(:agents => {:agents => {:primary_contact => :agents}}).to_sql
24
+ assert_match(/agents_people_4/i, sql)
25
+ end
26
+ end
27
+
28
+ def test_construct_finder_sql_ignores_empty_joins_hash
29
+ sql = Author.joins({}).to_sql
30
+ assert_no_match(/JOIN/i, sql)
31
+ end
32
+
33
+ def test_construct_finder_sql_ignores_empty_joins_array
34
+ sql = Author.joins([]).to_sql
35
+ assert_no_match(/JOIN/i, sql)
36
+ end
37
+
38
+ def test_join_conditions_added_to_join_clause
39
+ sql = Author.joins(:essays).to_sql
40
+ assert_match(/writer_type.*?=.*?Author/i, sql)
41
+ assert_no_match(/WHERE/i, sql)
42
+ end
43
+
44
+ def test_join_conditions_allow_nil_associations
45
+ authors = Author.includes(:essays).where(:essays => {:id => nil})
46
+ assert_equal 2, authors.count
47
+ end
48
+
49
+ def test_find_with_implicit_inner_joins_without_select_does_not_imply_readonly
50
+ authors = Author.joins(:posts)
51
+ assert_not authors.empty?, "expected authors to be non-empty"
52
+ assert authors.none? {|a| a.readonly? }, "expected no authors to be readonly"
53
+ end
54
+
55
+ def test_find_with_implicit_inner_joins_honors_readonly_with_select
56
+ authors = Author.joins(:posts).select('authors.*').to_a
57
+ assert !authors.empty?, "expected authors to be non-empty"
58
+ assert authors.all? {|a| !a.readonly? }, "expected no authors to be readonly"
59
+ end
60
+
61
+ def test_find_with_implicit_inner_joins_honors_readonly_false
62
+ authors = Author.joins(:posts).readonly(false).to_a
63
+ assert !authors.empty?, "expected authors to be non-empty"
64
+ assert authors.all? {|a| !a.readonly? }, "expected no authors to be readonly"
65
+ end
66
+
67
+ def test_find_with_implicit_inner_joins_does_not_set_associations
68
+ authors = Author.joins(:posts).select('authors.*')
69
+ assert !authors.empty?, "expected authors to be non-empty"
70
+ assert authors.all? { |a| !a.instance_variable_defined?(:@posts) }, "expected no authors to have the @posts association loaded"
71
+ end
72
+
73
+ def test_count_honors_implicit_inner_joins
74
+ real_count = Author.all.to_a.sum{|a| a.posts.count }
75
+ assert_equal real_count, Author.joins(:posts).count, "plain inner join count should match the number of referenced posts records"
76
+ end
77
+
78
+ def test_calculate_honors_implicit_inner_joins
79
+ real_count = Author.all.to_a.sum{|a| a.posts.count }
80
+ assert_equal real_count, Author.joins(:posts).calculate(:count, 'authors.id'), "plain inner join count should match the number of referenced posts records"
81
+ end
82
+
83
+ def test_calculate_honors_implicit_inner_joins_and_distinct_and_conditions
84
+ real_count = Author.all.to_a.select {|a| a.posts.any? {|p| p.title =~ /^Welcome/} }.length
85
+ authors_with_welcoming_post_titles = Author.all.merge!(joins: :posts, where: "posts.title like 'Welcome%'").distinct.calculate(:count, 'authors.id')
86
+ assert_equal real_count, authors_with_welcoming_post_titles, "inner join and conditions should have only returned authors posting titles starting with 'Welcome'"
87
+ end
88
+
89
+ def test_find_with_sti_join
90
+ scope = Post.joins(:special_comments).where(:id => posts(:sti_comments).id)
91
+
92
+ # The join should match SpecialComment and its subclasses only
93
+ assert scope.where("comments.type" => "Comment").empty?
94
+ assert !scope.where("comments.type" => "SpecialComment").empty?
95
+ assert !scope.where("comments.type" => "SubSpecialComment").empty?
96
+ end
97
+
98
+ def test_find_with_conditions_on_reflection
99
+ assert !posts(:welcome).comments.empty?
100
+ assert Post.joins(:nonexistant_comments).where(:id => posts(:welcome).id).empty? # [sic!]
101
+ end
102
+
103
+ def test_find_with_conditions_on_through_reflection
104
+ assert !posts(:welcome).tags.empty?
105
+ assert Post.joins(:misc_tags).where(:id => posts(:welcome).id).empty?
106
+ end
107
+
108
+ test "the default scope of the target is applied when joining associations" do
109
+ author = Author.create! name: "Jon"
110
+ author.categorizations.create!
111
+ author.categorizations.create! special: true
112
+
113
+ assert_equal [author], Author.where(id: author).joins(:special_categorizations)
114
+ end
115
+ end
@@ -0,0 +1,631 @@
1
+ require "cases/helper"
2
+ require 'models/man'
3
+ require 'models/face'
4
+ require 'models/interest'
5
+ require 'models/zine'
6
+ require 'models/club'
7
+ require 'models/sponsor'
8
+ require 'models/rating'
9
+ require 'models/comment'
10
+ require 'models/car'
11
+ require 'models/bulb'
12
+
13
+ class AutomaticInverseFindingTests < ActiveRecord::TestCase
14
+ fixtures :ratings, :comments, :cars
15
+
16
+ def test_has_one_and_belongs_to_should_find_inverse_automatically
17
+ car_reflection = Car.reflect_on_association(:bulb)
18
+ bulb_reflection = Bulb.reflect_on_association(:car)
19
+
20
+ assert_respond_to car_reflection, :has_inverse?
21
+ assert car_reflection.has_inverse?, "The Car reflection should have an inverse"
22
+ assert_equal bulb_reflection, car_reflection.inverse_of, "The Car reflection's inverse should be the Bulb reflection"
23
+
24
+ assert_respond_to bulb_reflection, :has_inverse?
25
+ assert bulb_reflection.has_inverse?, "The Bulb reflection should have an inverse"
26
+ assert_equal car_reflection, bulb_reflection.inverse_of, "The Bulb reflection's inverse should be the Car reflection"
27
+ end
28
+
29
+ def test_has_many_and_belongs_to_should_find_inverse_automatically
30
+ comment_reflection = Comment.reflect_on_association(:ratings)
31
+ rating_reflection = Rating.reflect_on_association(:comment)
32
+
33
+ assert_respond_to comment_reflection, :has_inverse?
34
+ assert comment_reflection.has_inverse?, "The Comment reflection should have an inverse"
35
+ assert_equal rating_reflection, comment_reflection.inverse_of, "The Comment reflection's inverse should be the Rating reflection"
36
+ end
37
+
38
+ def test_has_one_and_belongs_to_automatic_inverse_shares_objects
39
+ car = Car.first
40
+ bulb = Bulb.create!(car: car)
41
+
42
+ assert_equal car.bulb, bulb, "The Car's bulb should be the original bulb"
43
+
44
+ car.bulb.color = "Blue"
45
+ assert_equal car.bulb.color, bulb.color, "Changing the bulb's color on the car association should change the bulb's color"
46
+
47
+ bulb.color = "Red"
48
+ assert_equal bulb.color, car.bulb.color, "Changing the bulb's color should change the bulb's color on the car association"
49
+ end
50
+
51
+ def test_has_many_and_belongs_to_automatic_inverse_shares_objects_on_rating
52
+ comment = Comment.first
53
+ rating = Rating.create!(comment: comment)
54
+
55
+ assert_equal rating.comment, comment, "The Rating's comment should be the original Comment"
56
+
57
+ rating.comment.body = "Brogramming is the act of programming, like a bro."
58
+ assert_equal rating.comment.body, comment.body, "Changing the Comment's body on the association should change the original Comment's body"
59
+
60
+ comment.body = "Broseiden is the king of the sea of bros."
61
+ assert_equal comment.body, rating.comment.body, "Changing the original Comment's body should change the Comment's body on the association"
62
+ end
63
+
64
+ def test_has_many_and_belongs_to_automatic_inverse_shares_objects_on_comment
65
+ rating = Rating.create!
66
+ comment = Comment.first
67
+ rating.comment = comment
68
+
69
+ assert_equal rating.comment, comment, "The Rating's comment should be the original Comment"
70
+
71
+ rating.comment.body = "Brogramming is the act of programming, like a bro."
72
+ assert_equal rating.comment.body, comment.body, "Changing the Comment's body on the association should change the original Comment's body"
73
+
74
+ comment.body = "Broseiden is the king of the sea of bros."
75
+ assert_equal comment.body, rating.comment.body, "Changing the original Comment's body should change the Comment's body on the association"
76
+ end
77
+
78
+ def test_polymorphic_and_has_many_through_relationships_should_not_have_inverses
79
+ sponsor_reflection = Sponsor.reflect_on_association(:sponsorable)
80
+
81
+ assert_respond_to sponsor_reflection, :has_inverse?
82
+ assert !sponsor_reflection.has_inverse?, "A polymorphic association should not find an inverse automatically"
83
+
84
+ club_reflection = Club.reflect_on_association(:members)
85
+
86
+ assert_respond_to club_reflection, :has_inverse?
87
+ assert !club_reflection.has_inverse?, "A has_many_through association should not find an inverse automatically"
88
+ end
89
+ end
90
+
91
+ class InverseAssociationTests < ActiveRecord::TestCase
92
+ def test_should_allow_for_inverse_of_options_in_associations
93
+ assert_nothing_raised(ArgumentError, 'ActiveRecord should allow the inverse_of options on has_many') do
94
+ Class.new(ActiveRecord::Base).has_many(:wheels, :inverse_of => :car)
95
+ end
96
+
97
+ assert_nothing_raised(ArgumentError, 'ActiveRecord should allow the inverse_of options on has_one') do
98
+ Class.new(ActiveRecord::Base).has_one(:engine, :inverse_of => :car)
99
+ end
100
+
101
+ assert_nothing_raised(ArgumentError, 'ActiveRecord should allow the inverse_of options on belongs_to') do
102
+ Class.new(ActiveRecord::Base).belongs_to(:car, :inverse_of => :driver)
103
+ end
104
+ end
105
+
106
+ def test_should_be_able_to_ask_a_reflection_if_it_has_an_inverse
107
+ has_one_with_inverse_ref = Man.reflect_on_association(:face)
108
+ assert_respond_to has_one_with_inverse_ref, :has_inverse?
109
+ assert has_one_with_inverse_ref.has_inverse?
110
+
111
+ has_many_with_inverse_ref = Man.reflect_on_association(:interests)
112
+ assert_respond_to has_many_with_inverse_ref, :has_inverse?
113
+ assert has_many_with_inverse_ref.has_inverse?
114
+
115
+ belongs_to_with_inverse_ref = Face.reflect_on_association(:man)
116
+ assert_respond_to belongs_to_with_inverse_ref, :has_inverse?
117
+ assert belongs_to_with_inverse_ref.has_inverse?
118
+
119
+ has_one_without_inverse_ref = Club.reflect_on_association(:sponsor)
120
+ assert_respond_to has_one_without_inverse_ref, :has_inverse?
121
+ assert !has_one_without_inverse_ref.has_inverse?
122
+
123
+ has_many_without_inverse_ref = Club.reflect_on_association(:memberships)
124
+ assert_respond_to has_many_without_inverse_ref, :has_inverse?
125
+ assert !has_many_without_inverse_ref.has_inverse?
126
+
127
+ belongs_to_without_inverse_ref = Sponsor.reflect_on_association(:sponsor_club)
128
+ assert_respond_to belongs_to_without_inverse_ref, :has_inverse?
129
+ assert !belongs_to_without_inverse_ref.has_inverse?
130
+ end
131
+
132
+ def test_should_be_able_to_ask_a_reflection_what_it_is_the_inverse_of
133
+ has_one_ref = Man.reflect_on_association(:face)
134
+ assert_respond_to has_one_ref, :inverse_of
135
+
136
+ has_many_ref = Man.reflect_on_association(:interests)
137
+ assert_respond_to has_many_ref, :inverse_of
138
+
139
+ belongs_to_ref = Face.reflect_on_association(:man)
140
+ assert_respond_to belongs_to_ref, :inverse_of
141
+ end
142
+
143
+ def test_inverse_of_method_should_supply_the_actual_reflection_instance_it_is_the_inverse_of
144
+ has_one_ref = Man.reflect_on_association(:face)
145
+ assert_equal Face.reflect_on_association(:man), has_one_ref.inverse_of
146
+
147
+ has_many_ref = Man.reflect_on_association(:interests)
148
+ assert_equal Interest.reflect_on_association(:man), has_many_ref.inverse_of
149
+
150
+ belongs_to_ref = Face.reflect_on_association(:man)
151
+ assert_equal Man.reflect_on_association(:face), belongs_to_ref.inverse_of
152
+ end
153
+
154
+ def test_associations_with_no_inverse_of_should_return_nil
155
+ has_one_ref = Club.reflect_on_association(:sponsor)
156
+ assert_nil has_one_ref.inverse_of
157
+
158
+ has_many_ref = Club.reflect_on_association(:memberships)
159
+ assert_nil has_many_ref.inverse_of
160
+
161
+ belongs_to_ref = Sponsor.reflect_on_association(:sponsor_club)
162
+ assert_nil belongs_to_ref.inverse_of
163
+ end
164
+ end
165
+
166
+ class InverseHasOneTests < ActiveRecord::TestCase
167
+ fixtures :men, :faces
168
+
169
+ def test_parent_instance_should_be_shared_with_child_on_find
170
+ m = men(:gordon)
171
+ f = m.face
172
+ assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
173
+ m.name = 'Bongo'
174
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
175
+ f.man.name = 'Mungo'
176
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"
177
+ end
178
+
179
+
180
+ def test_parent_instance_should_be_shared_with_eager_loaded_child_on_find
181
+ m = Man.all.merge!(:where => {:name => 'Gordon'}, :includes => :face).first
182
+ f = m.face
183
+ assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
184
+ m.name = 'Bongo'
185
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
186
+ f.man.name = 'Mungo'
187
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"
188
+
189
+ m = Man.all.merge!(:where => {:name => 'Gordon'}, :includes => :face, :order => 'faces.id').first
190
+ f = m.face
191
+ assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
192
+ m.name = 'Bongo'
193
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
194
+ f.man.name = 'Mungo'
195
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"
196
+ end
197
+
198
+ def test_parent_instance_should_be_shared_with_newly_built_child
199
+ m = Man.first
200
+ f = m.build_face(:description => 'haunted')
201
+ assert_not_nil f.man
202
+ assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
203
+ m.name = 'Bongo'
204
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
205
+ f.man.name = 'Mungo'
206
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
207
+ end
208
+
209
+ def test_parent_instance_should_be_shared_with_newly_created_child
210
+ m = Man.first
211
+ f = m.create_face(:description => 'haunted')
212
+ assert_not_nil f.man
213
+ assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
214
+ m.name = 'Bongo'
215
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
216
+ f.man.name = 'Mungo'
217
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
218
+ end
219
+
220
+ def test_parent_instance_should_be_shared_with_newly_created_child_via_bang_method
221
+ m = Man.first
222
+ f = m.create_face!(:description => 'haunted')
223
+ assert_not_nil f.man
224
+ assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
225
+ m.name = 'Bongo'
226
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
227
+ f.man.name = 'Mungo'
228
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
229
+ end
230
+
231
+ def test_parent_instance_should_be_shared_with_replaced_via_accessor_child
232
+ m = Man.first
233
+ f = Face.new(:description => 'haunted')
234
+ m.face = f
235
+ assert_not_nil f.man
236
+ assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
237
+ m.name = 'Bongo'
238
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
239
+ f.man.name = 'Mungo'
240
+ assert_equal m.name, f.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
241
+ end
242
+
243
+ def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
244
+ assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.dirty_face }
245
+ end
246
+ end
247
+
248
+ class InverseHasManyTests < ActiveRecord::TestCase
249
+ fixtures :men, :interests
250
+
251
+ def test_parent_instance_should_be_shared_with_every_child_on_find
252
+ m = men(:gordon)
253
+ is = m.interests
254
+ is.each do |i|
255
+ assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
256
+ m.name = 'Bongo'
257
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
258
+ i.man.name = 'Mungo'
259
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
260
+ end
261
+ end
262
+
263
+ def test_parent_instance_should_be_shared_with_eager_loaded_children
264
+ m = Man.all.merge!(:where => {:name => 'Gordon'}, :includes => :interests).first
265
+ is = m.interests
266
+ is.each do |i|
267
+ assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
268
+ m.name = 'Bongo'
269
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
270
+ i.man.name = 'Mungo'
271
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
272
+ end
273
+
274
+ m = Man.all.merge!(:where => {:name => 'Gordon'}, :includes => :interests, :order => 'interests.id').first
275
+ is = m.interests
276
+ is.each do |i|
277
+ assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
278
+ m.name = 'Bongo'
279
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
280
+ i.man.name = 'Mungo'
281
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
282
+ end
283
+ end
284
+
285
+ def test_parent_instance_should_be_shared_with_newly_block_style_built_child
286
+ m = Man.first
287
+ i = m.interests.build {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
288
+ assert_not_nil i.topic, "Child attributes supplied to build via blocks should be populated"
289
+ assert_not_nil i.man
290
+ assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
291
+ m.name = 'Bongo'
292
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
293
+ i.man.name = 'Mungo'
294
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
295
+ end
296
+
297
+ def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child
298
+ m = Man.first
299
+ i = m.interests.create!(:topic => 'Industrial Revolution Re-enactment')
300
+ assert_not_nil i.man
301
+ assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
302
+ m.name = 'Bongo'
303
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
304
+ i.man.name = 'Mungo'
305
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
306
+ end
307
+
308
+ def test_parent_instance_should_be_shared_with_newly_block_style_created_child
309
+ m = Man.first
310
+ i = m.interests.create {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
311
+ assert_not_nil i.topic, "Child attributes supplied to create via blocks should be populated"
312
+ assert_not_nil i.man
313
+ assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
314
+ m.name = 'Bongo'
315
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
316
+ i.man.name = 'Mungo'
317
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
318
+ end
319
+
320
+ def test_parent_instance_should_be_shared_within_create_block_of_new_child
321
+ man = Man.first
322
+ interest = man.interests.build do |i|
323
+ assert i.man.equal?(man), "Man of child should be the same instance as a parent"
324
+ end
325
+ assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent"
326
+ end
327
+
328
+ def test_parent_instance_should_be_shared_within_build_block_of_new_child
329
+ man = Man.first
330
+ interest = man.interests.build do |i|
331
+ assert i.man.equal?(man), "Man of child should be the same instance as a parent"
332
+ end
333
+ assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent"
334
+ end
335
+
336
+ def test_parent_instance_should_be_shared_with_poked_in_child
337
+ m = men(:gordon)
338
+ i = Interest.create(:topic => 'Industrial Revolution Re-enactment')
339
+ m.interests << i
340
+ assert_not_nil i.man
341
+ assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
342
+ m.name = 'Bongo'
343
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
344
+ i.man.name = 'Mungo'
345
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
346
+ end
347
+
348
+ def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
349
+ m = Man.first
350
+ i = Interest.new(:topic => 'Industrial Revolution Re-enactment')
351
+ m.interests = [i]
352
+ assert_not_nil i.man
353
+ assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
354
+ m.name = 'Bongo'
355
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
356
+ i.man.name = 'Mungo'
357
+ assert_equal m.name, i.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
358
+ end
359
+
360
+ def test_parent_instance_should_be_shared_with_first_and_last_child
361
+ man = Man.first
362
+
363
+ assert man.interests.first.man.equal? man
364
+ assert man.interests.last.man.equal? man
365
+ end
366
+
367
+ def test_parent_instance_should_be_shared_with_first_n_and_last_n_children
368
+ man = Man.first
369
+
370
+ interests = man.interests.first(2)
371
+ assert interests[0].man.equal? man
372
+ assert interests[1].man.equal? man
373
+
374
+ interests = man.interests.last(2)
375
+ assert interests[0].man.equal? man
376
+ assert interests[1].man.equal? man
377
+ end
378
+
379
+ def test_parent_instance_should_find_child_instance_using_child_instance_id
380
+ man = Man.create!
381
+ interest = Interest.create!
382
+ man.interests = [interest]
383
+
384
+ assert interest.equal?(man.interests.first), "The inverse association should use the interest already created and held in memory"
385
+ assert interest.equal?(man.interests.find(interest.id)), "The inverse association should use the interest already created and held in memory"
386
+ assert man.equal?(man.interests.first.man), "Two inversion should lead back to the same object that was originally held"
387
+ assert man.equal?(man.interests.find(interest.id).man), "Two inversions should lead back to the same object that was originally held"
388
+ end
389
+
390
+ def test_parent_instance_should_find_child_instance_using_child_instance_id_when_created
391
+ man = Man.create!
392
+ interest = Interest.create!(man: man)
393
+
394
+ assert man.equal?(man.interests.first.man), "Two inverses should lead back to the same object that was originally held"
395
+ assert man.equal?(man.interests.find(interest.id).man), "Two inversions should lead back to the same object that was originally held"
396
+
397
+ assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match before the name is changed"
398
+ man.name = "Ben Bitdiddle"
399
+ assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match after the parent name is changed"
400
+ man.interests.find(interest.id).man.name = "Alyssa P. Hacker"
401
+ assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match after the child name is changed"
402
+ end
403
+
404
+ def test_find_on_child_instance_with_id_should_not_load_all_child_records
405
+ man = Man.create!
406
+ interest = Interest.create!(man: man)
407
+
408
+ man.interests.find(interest.id)
409
+ refute man.interests.loaded?
410
+ end
411
+
412
+ def test_raise_record_not_found_error_when_invalid_ids_are_passed
413
+ # delete all interest records to ensure that hard coded invalid_id(s)
414
+ # are indeed invalid.
415
+ Interest.delete_all
416
+
417
+ man = Man.create!
418
+
419
+ invalid_id = 245324523
420
+ assert_raise(ActiveRecord::RecordNotFound) { man.interests.find(invalid_id) }
421
+
422
+ invalid_ids = [8432342, 2390102913, 2453245234523452]
423
+ assert_raise(ActiveRecord::RecordNotFound) { man.interests.find(invalid_ids) }
424
+ end
425
+
426
+ def test_raise_record_not_found_error_when_no_ids_are_passed
427
+ man = Man.create!
428
+
429
+ assert_raise(ActiveRecord::RecordNotFound) { man.interests.find() }
430
+ end
431
+
432
+ def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
433
+ assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.secret_interests }
434
+ end
435
+ end
436
+
437
+ class InverseBelongsToTests < ActiveRecord::TestCase
438
+ fixtures :men, :faces, :interests
439
+
440
+ def test_child_instance_should_be_shared_with_parent_on_find
441
+ f = faces(:trusting)
442
+ m = f.man
443
+ assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
444
+ f.description = 'gormless'
445
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
446
+ m.face.description = 'pleasing'
447
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"
448
+ end
449
+
450
+ def test_eager_loaded_child_instance_should_be_shared_with_parent_on_find
451
+ f = Face.all.merge!(:includes => :man, :where => {:description => 'trusting'}).first
452
+ m = f.man
453
+ assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
454
+ f.description = 'gormless'
455
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
456
+ m.face.description = 'pleasing'
457
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"
458
+
459
+ f = Face.all.merge!(:includes => :man, :order => 'men.id', :where => {:description => 'trusting'}).first
460
+ m = f.man
461
+ assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
462
+ f.description = 'gormless'
463
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
464
+ m.face.description = 'pleasing'
465
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"
466
+ end
467
+
468
+ def test_child_instance_should_be_shared_with_newly_built_parent
469
+ f = faces(:trusting)
470
+ m = f.build_man(:name => 'Charles')
471
+ assert_not_nil m.face
472
+ assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
473
+ f.description = 'gormless'
474
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
475
+ m.face.description = 'pleasing'
476
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to just-built-parent-owned instance"
477
+ end
478
+
479
+ def test_child_instance_should_be_shared_with_newly_created_parent
480
+ f = faces(:trusting)
481
+ m = f.create_man(:name => 'Charles')
482
+ assert_not_nil m.face
483
+ assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
484
+ f.description = 'gormless'
485
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
486
+ m.face.description = 'pleasing'
487
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to newly-created-parent-owned instance"
488
+ end
489
+
490
+ def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
491
+ i = interests(:trainspotting)
492
+ m = i.man
493
+ assert_not_nil m.interests
494
+ iz = m.interests.detect { |_iz| _iz.id == i.id}
495
+ assert_not_nil iz
496
+ assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
497
+ i.topic = 'Eating cheese with a spoon'
498
+ assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child"
499
+ iz.topic = 'Cow tipping'
500
+ assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
501
+ end
502
+
503
+ def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
504
+ f = Face.first
505
+ m = Man.new(:name => 'Charles')
506
+ f.man = m
507
+ assert_not_nil m.face
508
+ assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
509
+ f.description = 'gormless'
510
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
511
+ m.face.description = 'pleasing'
512
+ assert_equal f.description, m.face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
513
+ end
514
+
515
+ def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
516
+ assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_man }
517
+ end
518
+ end
519
+
520
+ class InversePolymorphicBelongsToTests < ActiveRecord::TestCase
521
+ fixtures :men, :faces, :interests
522
+
523
+ def test_child_instance_should_be_shared_with_parent_on_find
524
+ f = Face.all.merge!(:where => {:description => 'confused'}).first
525
+ m = f.polymorphic_man
526
+ assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance"
527
+ f.description = 'gormless'
528
+ assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to child instance"
529
+ m.polymorphic_face.description = 'pleasing'
530
+ assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance"
531
+ end
532
+
533
+ def test_eager_loaded_child_instance_should_be_shared_with_parent_on_find
534
+ f = Face.all.merge!(:where => {:description => 'confused'}, :includes => :man).first
535
+ m = f.polymorphic_man
536
+ assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance"
537
+ f.description = 'gormless'
538
+ assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to child instance"
539
+ m.polymorphic_face.description = 'pleasing'
540
+ assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance"
541
+
542
+ f = Face.all.merge!(:where => {:description => 'confused'}, :includes => :man, :order => 'men.id').first
543
+ m = f.polymorphic_man
544
+ assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance"
545
+ f.description = 'gormless'
546
+ assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to child instance"
547
+ m.polymorphic_face.description = 'pleasing'
548
+ assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance"
549
+ end
550
+
551
+ def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
552
+ face = faces(:confused)
553
+ new_man = Man.new
554
+
555
+ assert_not_nil face.polymorphic_man
556
+ face.polymorphic_man = new_man
557
+
558
+ assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same before changes to parent instance"
559
+ face.description = 'Bongo'
560
+ assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to parent instance"
561
+ new_man.polymorphic_face.description = 'Mungo'
562
+ assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
563
+ end
564
+
565
+ def test_child_instance_should_be_shared_with_replaced_via_method_parent
566
+ face = faces(:confused)
567
+ new_man = Man.new
568
+
569
+ assert_not_nil face.polymorphic_man
570
+ face.polymorphic_man = new_man
571
+
572
+ assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same before changes to parent instance"
573
+ face.description = 'Bongo'
574
+ assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to parent instance"
575
+ new_man.polymorphic_face.description = 'Mungo'
576
+ assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
577
+ end
578
+
579
+ def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
580
+ i = interests(:llama_wrangling)
581
+ m = i.polymorphic_man
582
+ assert_not_nil m.polymorphic_interests
583
+ iz = m.polymorphic_interests.detect { |_iz| _iz.id == i.id}
584
+ assert_not_nil iz
585
+ assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
586
+ i.topic = 'Eating cheese with a spoon'
587
+ assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child"
588
+ iz.topic = 'Cow tipping'
589
+ assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
590
+ end
591
+
592
+ def test_trying_to_access_inverses_that_dont_exist_shouldnt_raise_an_error
593
+ # Ideally this would, if only for symmetry's sake with other association types
594
+ assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_polymorphic_man }
595
+ end
596
+
597
+ def test_trying_to_set_polymorphic_inverses_that_dont_exist_at_all_should_raise_an_error
598
+ # fails because no class has the correct inverse_of for horrible_polymorphic_man
599
+ assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_polymorphic_man = Man.first }
600
+ end
601
+
602
+ def test_trying_to_set_polymorphic_inverses_that_dont_exist_on_the_instance_being_set_should_raise_an_error
603
+ # passes because Man does have the correct inverse_of
604
+ assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.polymorphic_man = Man.first }
605
+ # fails because Interest does have the correct inverse_of
606
+ assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.polymorphic_man = Interest.first }
607
+ end
608
+ end
609
+
610
+ # NOTE - these tests might not be meaningful, ripped as they were from the parental_control plugin
611
+ # which would guess the inverse rather than look for an explicit configuration option.
612
+ class InverseMultipleHasManyInversesForSameModel < ActiveRecord::TestCase
613
+ fixtures :men, :interests, :zines
614
+
615
+ def test_that_we_can_load_associations_that_have_the_same_reciprocal_name_from_different_models
616
+ assert_nothing_raised(ActiveRecord::AssociationTypeMismatch) do
617
+ i = Interest.first
618
+ i.zine
619
+ i.man
620
+ end
621
+ end
622
+
623
+ def test_that_we_can_create_associations_that_have_the_same_reciprocal_name_from_different_models
624
+ assert_nothing_raised(ActiveRecord::AssociationTypeMismatch) do
625
+ i = Interest.first
626
+ i.build_zine(:title => 'Get Some in Winter! 2008')
627
+ i.build_man(:name => 'Gordon')
628
+ i.save!
629
+ end
630
+ end
631
+ end