ibm_db 4.0.0-x86-mingw32 → 5.1.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (576) hide show
  1. checksums.yaml +5 -5
  2. data/MANIFEST +14 -14
  3. data/README +208 -208
  4. data/ext/Makefile +269 -0
  5. data/ext/Makefile.nt32 +181 -181
  6. data/ext/Makefile.nt32.191 +212 -212
  7. data/ext/extconf.rb +322 -291
  8. data/ext/gil_release_version.h +3 -0
  9. data/ext/ibm_db-i386-mingw32.def +2 -0
  10. data/ext/ibm_db.c +11879 -11887
  11. data/ext/ibm_db.o +0 -0
  12. data/ext/ibm_db.so +0 -0
  13. data/ext/mkmf.log +110 -0
  14. data/ext/ruby_ibm_db.h +241 -241
  15. data/ext/ruby_ibm_db_cli.c +866 -866
  16. data/ext/ruby_ibm_db_cli.h +500 -500
  17. data/ext/ruby_ibm_db_cli.o +0 -0
  18. data/ext/unicode_support_version.h +3 -0
  19. data/init.rb +41 -41
  20. data/lib/IBM_DB.rb +27 -27
  21. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +3593 -3452
  22. data/lib/active_record/connection_adapters/ibmdb_adapter.rb +5 -5
  23. data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -328
  24. data/lib/mswin32/ibm_db.rb +94 -90
  25. data/lib/mswin32/rb2x/i386/ruby26/ibm_db.so +0 -0
  26. data/lib/mswin32/rb2x/i386/ruby27/ibm_db.so +0 -0
  27. data/test/active_record/connection_adapters/fake_adapter.rb +49 -49
  28. data/test/assets/example.log +1 -1
  29. data/test/assets/test.txt +1 -1
  30. data/test/cases/adapter_test.rb +351 -351
  31. data/test/cases/adapters/mysql2/active_schema_test.rb +193 -193
  32. data/test/cases/adapters/mysql2/bind_parameter_test.rb +50 -50
  33. data/test/cases/adapters/mysql2/boolean_test.rb +100 -100
  34. data/test/cases/adapters/mysql2/case_sensitivity_test.rb +63 -63
  35. data/test/cases/adapters/mysql2/charset_collation_test.rb +54 -54
  36. data/test/cases/adapters/mysql2/connection_test.rb +210 -210
  37. data/test/cases/adapters/mysql2/datetime_precision_quoting_test.rb +45 -45
  38. data/test/cases/adapters/mysql2/enum_test.rb +26 -26
  39. data/test/cases/adapters/mysql2/explain_test.rb +21 -21
  40. data/test/cases/adapters/mysql2/json_test.rb +195 -195
  41. data/test/cases/adapters/mysql2/mysql2_adapter_test.rb +83 -83
  42. data/test/cases/adapters/mysql2/reserved_word_test.rb +152 -152
  43. data/test/cases/adapters/mysql2/schema_migrations_test.rb +59 -59
  44. data/test/cases/adapters/mysql2/schema_test.rb +126 -126
  45. data/test/cases/adapters/mysql2/sp_test.rb +36 -36
  46. data/test/cases/adapters/mysql2/sql_types_test.rb +14 -14
  47. data/test/cases/adapters/mysql2/table_options_test.rb +42 -42
  48. data/test/cases/adapters/mysql2/unsigned_type_test.rb +66 -66
  49. data/test/cases/adapters/postgresql/active_schema_test.rb +98 -98
  50. data/test/cases/adapters/postgresql/array_test.rb +339 -339
  51. data/test/cases/adapters/postgresql/bit_string_test.rb +82 -82
  52. data/test/cases/adapters/postgresql/bytea_test.rb +134 -134
  53. data/test/cases/adapters/postgresql/case_insensitive_test.rb +26 -26
  54. data/test/cases/adapters/postgresql/change_schema_test.rb +38 -38
  55. data/test/cases/adapters/postgresql/cidr_test.rb +25 -25
  56. data/test/cases/adapters/postgresql/citext_test.rb +78 -78
  57. data/test/cases/adapters/postgresql/collation_test.rb +53 -53
  58. data/test/cases/adapters/postgresql/composite_test.rb +132 -132
  59. data/test/cases/adapters/postgresql/connection_test.rb +257 -257
  60. data/test/cases/adapters/postgresql/datatype_test.rb +92 -92
  61. data/test/cases/adapters/postgresql/domain_test.rb +47 -47
  62. data/test/cases/adapters/postgresql/enum_test.rb +91 -91
  63. data/test/cases/adapters/postgresql/explain_test.rb +20 -20
  64. data/test/cases/adapters/postgresql/extension_migration_test.rb +63 -63
  65. data/test/cases/adapters/postgresql/full_text_test.rb +44 -44
  66. data/test/cases/adapters/postgresql/geometric_test.rb +378 -378
  67. data/test/cases/adapters/postgresql/hstore_test.rb +382 -382
  68. data/test/cases/adapters/postgresql/infinity_test.rb +69 -69
  69. data/test/cases/adapters/postgresql/integer_test.rb +25 -25
  70. data/test/cases/adapters/postgresql/json_test.rb +237 -237
  71. data/test/cases/adapters/postgresql/ltree_test.rb +53 -53
  72. data/test/cases/adapters/postgresql/money_test.rb +96 -96
  73. data/test/cases/adapters/postgresql/network_test.rb +94 -94
  74. data/test/cases/adapters/postgresql/numbers_test.rb +49 -49
  75. data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +405 -405
  76. data/test/cases/adapters/postgresql/prepared_statements_test.rb +22 -22
  77. data/test/cases/adapters/postgresql/quoting_test.rb +44 -44
  78. data/test/cases/adapters/postgresql/range_test.rb +343 -343
  79. data/test/cases/adapters/postgresql/referential_integrity_test.rb +111 -111
  80. data/test/cases/adapters/postgresql/rename_table_test.rb +34 -34
  81. data/test/cases/adapters/postgresql/schema_authorization_test.rb +119 -119
  82. data/test/cases/adapters/postgresql/schema_test.rb +597 -597
  83. data/test/cases/adapters/postgresql/serial_test.rb +154 -154
  84. data/test/cases/adapters/postgresql/statement_pool_test.rb +41 -41
  85. data/test/cases/adapters/postgresql/timestamp_test.rb +90 -90
  86. data/test/cases/adapters/postgresql/type_lookup_test.rb +33 -33
  87. data/test/cases/adapters/postgresql/utils_test.rb +62 -62
  88. data/test/cases/adapters/postgresql/uuid_test.rb +294 -294
  89. data/test/cases/adapters/postgresql/xml_test.rb +54 -54
  90. data/test/cases/adapters/sqlite3/collation_test.rb +53 -53
  91. data/test/cases/adapters/sqlite3/copy_table_test.rb +98 -98
  92. data/test/cases/adapters/sqlite3/explain_test.rb +21 -21
  93. data/test/cases/adapters/sqlite3/quoting_test.rb +101 -101
  94. data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +441 -441
  95. data/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb +24 -24
  96. data/test/cases/adapters/sqlite3/statement_pool_test.rb +20 -20
  97. data/test/cases/aggregations_test.rb +168 -168
  98. data/test/cases/ar_schema_test.rb +146 -146
  99. data/test/cases/associations/association_scope_test.rb +16 -16
  100. data/test/cases/associations/belongs_to_associations_test.rb +1141 -1141
  101. data/test/cases/associations/bidirectional_destroy_dependencies_test.rb +41 -41
  102. data/test/cases/associations/callbacks_test.rb +190 -190
  103. data/test/cases/associations/cascaded_eager_loading_test.rb +188 -188
  104. data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +36 -36
  105. data/test/cases/associations/eager_load_nested_include_test.rb +126 -126
  106. data/test/cases/associations/eager_singularization_test.rb +148 -148
  107. data/test/cases/associations/eager_test.rb +1514 -1514
  108. data/test/cases/associations/extension_test.rb +87 -87
  109. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +1004 -1004
  110. data/test/cases/associations/has_many_associations_test.rb +2501 -2501
  111. data/test/cases/associations/has_many_through_associations_test.rb +1271 -1271
  112. data/test/cases/associations/has_one_associations_test.rb +707 -707
  113. data/test/cases/associations/has_one_through_associations_test.rb +383 -383
  114. data/test/cases/associations/inner_join_association_test.rb +139 -139
  115. data/test/cases/associations/inverse_associations_test.rb +733 -733
  116. data/test/cases/associations/join_model_test.rb +777 -777
  117. data/test/cases/associations/left_outer_join_association_test.rb +88 -88
  118. data/test/cases/associations/nested_through_associations_test.rb +579 -579
  119. data/test/cases/associations/required_test.rb +102 -102
  120. data/test/cases/associations_test.rb +385 -385
  121. data/test/cases/attribute_decorators_test.rb +126 -125
  122. data/test/cases/attribute_methods/read_test.rb +60 -60
  123. data/test/cases/attribute_methods_test.rb +1009 -1009
  124. data/test/cases/attribute_set_test.rb +270 -270
  125. data/test/cases/attribute_test.rb +246 -246
  126. data/test/cases/attributes_test.rb +253 -253
  127. data/test/cases/autosave_association_test.rb +1708 -1708
  128. data/test/cases/base_test.rb +1713 -1713
  129. data/test/cases/batches_test.rb +489 -489
  130. data/test/cases/binary_test.rb +44 -44
  131. data/test/cases/bind_parameter_test.rb +110 -110
  132. data/test/cases/cache_key_test.rb +26 -25
  133. data/test/cases/calculations_test.rb +798 -798
  134. data/test/cases/callbacks_test.rb +636 -636
  135. data/test/cases/clone_test.rb +40 -40
  136. data/test/cases/coders/json_test.rb +15 -15
  137. data/test/cases/coders/yaml_column_test.rb +63 -63
  138. data/test/cases/collection_cache_key_test.rb +115 -115
  139. data/test/cases/column_alias_test.rb +17 -17
  140. data/test/cases/column_definition_test.rb +92 -92
  141. data/test/cases/comment_test.rb +145 -143
  142. data/test/cases/connection_adapters/adapter_leasing_test.rb +56 -56
  143. data/test/cases/connection_adapters/connection_handler_test.rb +160 -160
  144. data/test/cases/connection_adapters/connection_specification_test.rb +12 -12
  145. data/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb +255 -255
  146. data/test/cases/connection_adapters/mysql_type_lookup_test.rb +69 -69
  147. data/test/cases/connection_adapters/quoting_test.rb +13 -13
  148. data/test/cases/connection_adapters/schema_cache_test.rb +61 -61
  149. data/test/cases/connection_adapters/type_lookup_test.rb +118 -118
  150. data/test/cases/connection_management_test.rb +112 -112
  151. data/test/cases/connection_pool_test.rb +521 -521
  152. data/test/cases/connection_specification/resolver_test.rb +131 -131
  153. data/test/cases/core_test.rb +112 -112
  154. data/test/cases/counter_cache_test.rb +214 -214
  155. data/test/cases/custom_locking_test.rb +17 -17
  156. data/test/cases/database_statements_test.rb +34 -34
  157. data/test/cases/date_test.rb +44 -44
  158. data/test/cases/date_time_precision_test.rb +107 -106
  159. data/test/cases/date_time_test.rb +61 -61
  160. data/test/cases/defaults_test.rb +219 -218
  161. data/test/cases/dirty_test.rb +763 -763
  162. data/test/cases/disconnected_test.rb +30 -30
  163. data/test/cases/dup_test.rb +157 -157
  164. data/test/cases/enum_test.rb +444 -444
  165. data/test/cases/errors_test.rb +16 -16
  166. data/test/cases/explain_subscriber_test.rb +64 -64
  167. data/test/cases/explain_test.rb +87 -87
  168. data/test/cases/finder_respond_to_test.rb +60 -60
  169. data/test/cases/finder_test.rb +1294 -1294
  170. data/test/cases/fixture_set/file_test.rb +156 -156
  171. data/test/cases/fixtures_test.rb +988 -988
  172. data/test/cases/forbidden_attributes_protection_test.rb +165 -165
  173. data/test/cases/habtm_destroy_order_test.rb +61 -61
  174. data/test/cases/helper.rb +204 -204
  175. data/test/cases/hot_compatibility_test.rb +142 -142
  176. data/test/cases/i18n_test.rb +45 -45
  177. data/test/cases/inheritance_test.rb +606 -606
  178. data/test/cases/integration_test.rb +155 -155
  179. data/test/cases/invalid_connection_test.rb +24 -24
  180. data/test/cases/invertible_migration_test.rb +387 -387
  181. data/test/cases/json_serialization_test.rb +311 -311
  182. data/test/cases/locking_test.rb +493 -493
  183. data/test/cases/log_subscriber_test.rb +225 -225
  184. data/test/cases/migration/change_schema_test.rb +458 -458
  185. data/test/cases/migration/change_table_test.rb +256 -256
  186. data/test/cases/migration/column_attributes_test.rb +176 -176
  187. data/test/cases/migration/column_positioning_test.rb +56 -56
  188. data/test/cases/migration/columns_test.rb +310 -310
  189. data/test/cases/migration/command_recorder_test.rb +350 -350
  190. data/test/cases/migration/compatibility_test.rb +118 -118
  191. data/test/cases/migration/create_join_table_test.rb +157 -157
  192. data/test/cases/migration/foreign_key_test.rb +362 -360
  193. data/test/cases/migration/helper.rb +39 -39
  194. data/test/cases/migration/index_test.rb +218 -218
  195. data/test/cases/migration/logger_test.rb +36 -36
  196. data/test/cases/migration/pending_migrations_test.rb +52 -52
  197. data/test/cases/migration/references_foreign_key_test.rb +221 -216
  198. data/test/cases/migration/references_index_test.rb +101 -101
  199. data/test/cases/migration/references_statements_test.rb +136 -136
  200. data/test/cases/migration/rename_table_test.rb +93 -93
  201. data/test/cases/migration_test.rb +1157 -1157
  202. data/test/cases/migrator_test.rb +471 -470
  203. data/test/cases/mixin_test.rb +68 -68
  204. data/test/cases/modules_test.rb +172 -172
  205. data/test/cases/multiparameter_attributes_test.rb +372 -372
  206. data/test/cases/multiple_db_test.rb +122 -122
  207. data/test/cases/nested_attributes_test.rb +1098 -1098
  208. data/test/cases/nested_attributes_with_callbacks_test.rb +144 -144
  209. data/test/cases/persistence_test.rb +1001 -1001
  210. data/test/cases/pooled_connections_test.rb +81 -81
  211. data/test/cases/primary_keys_test.rb +376 -376
  212. data/test/cases/query_cache_test.rb +446 -446
  213. data/test/cases/quoting_test.rb +202 -202
  214. data/test/cases/readonly_test.rb +119 -119
  215. data/test/cases/reaper_test.rb +85 -85
  216. data/test/cases/reflection_test.rb +509 -509
  217. data/test/cases/relation/delegation_test.rb +63 -63
  218. data/test/cases/relation/merging_test.rb +157 -157
  219. data/test/cases/relation/mutation_test.rb +183 -183
  220. data/test/cases/relation/or_test.rb +92 -92
  221. data/test/cases/relation/predicate_builder_test.rb +16 -16
  222. data/test/cases/relation/record_fetch_warning_test.rb +40 -40
  223. data/test/cases/relation/where_chain_test.rb +105 -105
  224. data/test/cases/relation/where_clause_test.rb +182 -182
  225. data/test/cases/relation/where_test.rb +322 -322
  226. data/test/cases/relation_test.rb +328 -328
  227. data/test/cases/relations_test.rb +2026 -2026
  228. data/test/cases/reload_models_test.rb +22 -22
  229. data/test/cases/result_test.rb +90 -90
  230. data/test/cases/sanitize_test.rb +176 -176
  231. data/test/cases/schema_dumper_test.rb +457 -457
  232. data/test/cases/schema_loading_test.rb +52 -52
  233. data/test/cases/scoping/default_scoping_test.rb +528 -528
  234. data/test/cases/scoping/named_scoping_test.rb +561 -561
  235. data/test/cases/scoping/relation_scoping_test.rb +400 -400
  236. data/test/cases/secure_token_test.rb +32 -32
  237. data/test/cases/serialization_test.rb +104 -104
  238. data/test/cases/serialized_attribute_test.rb +364 -364
  239. data/test/cases/statement_cache_test.rb +136 -136
  240. data/test/cases/store_test.rb +195 -195
  241. data/test/cases/suppressor_test.rb +63 -63
  242. data/test/cases/tasks/database_tasks_test.rb +462 -462
  243. data/test/cases/tasks/mysql_rake_test.rb +345 -345
  244. data/test/cases/tasks/postgresql_rake_test.rb +304 -304
  245. data/test/cases/tasks/sqlite_rake_test.rb +220 -220
  246. data/test/cases/test_case.rb +131 -131
  247. data/test/cases/test_fixtures_test.rb +36 -36
  248. data/test/cases/time_precision_test.rb +103 -102
  249. data/test/cases/timestamp_test.rb +501 -501
  250. data/test/cases/touch_later_test.rb +121 -121
  251. data/test/cases/transaction_callbacks_test.rb +518 -518
  252. data/test/cases/transaction_isolation_test.rb +106 -106
  253. data/test/cases/transactions_test.rb +835 -834
  254. data/test/cases/type/adapter_specific_registry_test.rb +133 -133
  255. data/test/cases/type/date_time_test.rb +14 -14
  256. data/test/cases/type/integer_test.rb +27 -27
  257. data/test/cases/type/string_test.rb +22 -22
  258. data/test/cases/type/type_map_test.rb +177 -177
  259. data/test/cases/type_test.rb +39 -39
  260. data/test/cases/types_test.rb +24 -24
  261. data/test/cases/unconnected_test.rb +33 -33
  262. data/test/cases/validations/absence_validation_test.rb +73 -73
  263. data/test/cases/validations/association_validation_test.rb +97 -97
  264. data/test/cases/validations/i18n_generate_message_validation_test.rb +84 -84
  265. data/test/cases/validations/i18n_validation_test.rb +86 -86
  266. data/test/cases/validations/length_validation_test.rb +79 -79
  267. data/test/cases/validations/presence_validation_test.rb +103 -103
  268. data/test/cases/validations/uniqueness_validation_test.rb +548 -548
  269. data/test/cases/validations_repair_helper.rb +19 -19
  270. data/test/cases/validations_test.rb +194 -194
  271. data/test/cases/view_test.rb +216 -216
  272. data/test/cases/yaml_serialization_test.rb +121 -121
  273. data/test/config.example.yml +97 -97
  274. data/test/config.rb +5 -5
  275. data/test/connections/native_ibm_db/connection.rb +44 -0
  276. data/test/fixtures/accounts.yml +29 -29
  277. data/test/fixtures/admin/accounts.yml +2 -2
  278. data/test/fixtures/admin/users.yml +10 -10
  279. data/test/fixtures/author_addresses.yml +17 -17
  280. data/test/fixtures/author_favorites.yml +3 -3
  281. data/test/fixtures/authors.yml +23 -23
  282. data/test/fixtures/bad_posts.yml +9 -9
  283. data/test/fixtures/binaries.yml +133 -133
  284. data/test/fixtures/books.yml +31 -31
  285. data/test/fixtures/bulbs.yml +5 -5
  286. data/test/fixtures/cars.yml +9 -9
  287. data/test/fixtures/categories.yml +19 -19
  288. data/test/fixtures/categories/special_categories.yml +9 -9
  289. data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -4
  290. data/test/fixtures/categories_ordered.yml +7 -7
  291. data/test/fixtures/categories_posts.yml +31 -31
  292. data/test/fixtures/categorizations.yml +23 -23
  293. data/test/fixtures/clubs.yml +8 -8
  294. data/test/fixtures/collections.yml +3 -3
  295. data/test/fixtures/colleges.yml +3 -3
  296. data/test/fixtures/comments.yml +65 -65
  297. data/test/fixtures/companies.yml +67 -67
  298. data/test/fixtures/computers.yml +10 -10
  299. data/test/fixtures/content.yml +3 -3
  300. data/test/fixtures/content_positions.yml +3 -3
  301. data/test/fixtures/courses.yml +8 -8
  302. data/test/fixtures/customers.yml +25 -25
  303. data/test/fixtures/dashboards.yml +6 -6
  304. data/test/fixtures/dead_parrots.yml +5 -5
  305. data/test/fixtures/developers.yml +22 -22
  306. data/test/fixtures/developers_projects.yml +16 -16
  307. data/test/fixtures/dog_lovers.yml +7 -7
  308. data/test/fixtures/dogs.yml +4 -4
  309. data/test/fixtures/doubloons.yml +3 -3
  310. data/test/fixtures/edges.yml +5 -5
  311. data/test/fixtures/entrants.yml +14 -14
  312. data/test/fixtures/essays.yml +6 -6
  313. data/test/fixtures/faces.yml +11 -11
  314. data/test/fixtures/fk_test_has_fk.yml +3 -3
  315. data/test/fixtures/fk_test_has_pk.yml +1 -1
  316. data/test/fixtures/friendships.yml +4 -4
  317. data/test/fixtures/funny_jokes.yml +10 -10
  318. data/test/fixtures/interests.yml +33 -33
  319. data/test/fixtures/items.yml +3 -3
  320. data/test/fixtures/jobs.yml +7 -7
  321. data/test/fixtures/legacy_things.yml +3 -3
  322. data/test/fixtures/live_parrots.yml +4 -4
  323. data/test/fixtures/mateys.yml +4 -4
  324. data/test/fixtures/member_details.yml +8 -8
  325. data/test/fixtures/member_types.yml +6 -6
  326. data/test/fixtures/members.yml +11 -11
  327. data/test/fixtures/memberships.yml +34 -34
  328. data/test/fixtures/men.yml +5 -5
  329. data/test/fixtures/minimalistics.yml +2 -2
  330. data/test/fixtures/minivans.yml +5 -5
  331. data/test/fixtures/mixed_case_monkeys.yml +6 -6
  332. data/test/fixtures/mixins.yml +29 -29
  333. data/test/fixtures/movies.yml +7 -7
  334. data/test/fixtures/naked/yml/accounts.yml +1 -1
  335. data/test/fixtures/naked/yml/companies.yml +1 -1
  336. data/test/fixtures/naked/yml/courses.yml +1 -1
  337. data/test/fixtures/naked/yml/parrots.yml +2 -2
  338. data/test/fixtures/naked/yml/trees.yml +3 -3
  339. data/test/fixtures/nodes.yml +29 -29
  340. data/test/fixtures/organizations.yml +5 -5
  341. data/test/fixtures/other_comments.yml +6 -6
  342. data/test/fixtures/other_dogs.yml +2 -2
  343. data/test/fixtures/other_posts.yml +7 -7
  344. data/test/fixtures/other_topics.yml +42 -42
  345. data/test/fixtures/owners.yml +9 -9
  346. data/test/fixtures/parrots.yml +27 -27
  347. data/test/fixtures/parrots_pirates.yml +7 -7
  348. data/test/fixtures/people.yml +24 -24
  349. data/test/fixtures/peoples_treasures.yml +3 -3
  350. data/test/fixtures/pets.yml +19 -19
  351. data/test/fixtures/pirates.yml +12 -15
  352. data/test/fixtures/posts.yml +80 -80
  353. data/test/fixtures/price_estimates.yml +16 -16
  354. data/test/fixtures/products.yml +4 -4
  355. data/test/fixtures/projects.yml +7 -7
  356. data/test/fixtures/ratings.yml +14 -14
  357. data/test/fixtures/readers.yml +11 -11
  358. data/test/fixtures/references.yml +17 -17
  359. data/test/fixtures/reserved_words/distinct.yml +5 -5
  360. data/test/fixtures/reserved_words/distinct_select.yml +11 -11
  361. data/test/fixtures/reserved_words/group.yml +14 -14
  362. data/test/fixtures/reserved_words/select.yml +8 -8
  363. data/test/fixtures/reserved_words/values.yml +7 -7
  364. data/test/fixtures/ships.yml +6 -6
  365. data/test/fixtures/speedometers.yml +8 -8
  366. data/test/fixtures/sponsors.yml +12 -12
  367. data/test/fixtures/string_key_objects.yml +7 -7
  368. data/test/fixtures/subscribers.yml +10 -10
  369. data/test/fixtures/subscriptions.yml +12 -12
  370. data/test/fixtures/taggings.yml +78 -78
  371. data/test/fixtures/tags.yml +11 -11
  372. data/test/fixtures/tasks.yml +7 -7
  373. data/test/fixtures/teapots.yml +3 -3
  374. data/test/fixtures/to_be_linked/accounts.yml +2 -2
  375. data/test/fixtures/to_be_linked/users.yml +10 -10
  376. data/test/fixtures/topics.yml +49 -49
  377. data/test/fixtures/toys.yml +14 -14
  378. data/test/fixtures/traffic_lights.yml +9 -9
  379. data/test/fixtures/treasures.yml +10 -10
  380. data/test/fixtures/trees.yml +3 -3
  381. data/test/fixtures/uuid_children.yml +3 -3
  382. data/test/fixtures/uuid_parents.yml +2 -2
  383. data/test/fixtures/variants.yml +4 -4
  384. data/test/fixtures/vegetables.yml +19 -19
  385. data/test/fixtures/vertices.yml +3 -3
  386. data/test/fixtures/warehouse_things.yml +2 -2
  387. data/test/fixtures/zines.yml +5 -5
  388. data/test/migrations/10_urban/9_add_expressions.rb +11 -11
  389. data/test/migrations/decimal/1_give_me_big_numbers.rb +15 -15
  390. data/test/migrations/magic/1_currencies_have_symbols.rb +12 -12
  391. data/test/migrations/missing/1000_people_have_middle_names.rb +9 -9
  392. data/test/migrations/missing/1_people_have_last_names.rb +9 -9
  393. data/test/migrations/missing/3_we_need_reminders.rb +12 -12
  394. data/test/migrations/missing/4_innocent_jointable.rb +12 -12
  395. data/test/migrations/rename/1_we_need_things.rb +11 -11
  396. data/test/migrations/rename/2_rename_things.rb +9 -9
  397. data/test/migrations/to_copy/1_people_have_hobbies.rb +9 -9
  398. data/test/migrations/to_copy/2_people_have_descriptions.rb +9 -9
  399. data/test/migrations/to_copy2/1_create_articles.rb +7 -7
  400. data/test/migrations/to_copy2/2_create_comments.rb +7 -7
  401. data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +9 -9
  402. data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +9 -9
  403. data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +9 -9
  404. data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +7 -7
  405. data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +7 -7
  406. data/test/migrations/valid/1_valid_people_have_last_names.rb +9 -9
  407. data/test/migrations/valid/2_we_need_reminders.rb +12 -12
  408. data/test/migrations/valid/3_innocent_jointable.rb +12 -12
  409. data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +9 -9
  410. data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +12 -12
  411. data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +12 -12
  412. data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +9 -9
  413. data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +12 -12
  414. data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +12 -12
  415. data/test/migrations/version_check/20131219224947_migration_version_check.rb +8 -8
  416. data/test/models/admin.rb +5 -5
  417. data/test/models/admin/account.rb +3 -3
  418. data/test/models/admin/user.rb +40 -40
  419. data/test/models/aircraft.rb +5 -5
  420. data/test/models/arunit2_model.rb +3 -3
  421. data/test/models/author.rb +209 -209
  422. data/test/models/auto_id.rb +4 -4
  423. data/test/models/autoloadable/extra_firm.rb +2 -2
  424. data/test/models/binary.rb +2 -2
  425. data/test/models/bird.rb +12 -12
  426. data/test/models/book.rb +23 -23
  427. data/test/models/boolean.rb +2 -2
  428. data/test/models/bulb.rb +52 -52
  429. data/test/models/cake_designer.rb +3 -3
  430. data/test/models/car.rb +29 -29
  431. data/test/models/carrier.rb +2 -2
  432. data/test/models/cat.rb +10 -10
  433. data/test/models/categorization.rb +19 -19
  434. data/test/models/category.rb +35 -35
  435. data/test/models/chef.rb +8 -8
  436. data/test/models/citation.rb +3 -3
  437. data/test/models/club.rb +25 -25
  438. data/test/models/college.rb +10 -10
  439. data/test/models/column.rb +3 -3
  440. data/test/models/column_name.rb +3 -3
  441. data/test/models/comment.rb +76 -76
  442. data/test/models/company.rb +230 -230
  443. data/test/models/company_in_module.rb +98 -98
  444. data/test/models/computer.rb +3 -3
  445. data/test/models/contact.rb +41 -41
  446. data/test/models/content.rb +40 -40
  447. data/test/models/contract.rb +20 -20
  448. data/test/models/country.rb +7 -7
  449. data/test/models/course.rb +6 -6
  450. data/test/models/customer.rb +83 -83
  451. data/test/models/customer_carrier.rb +14 -14
  452. data/test/models/dashboard.rb +3 -3
  453. data/test/models/default.rb +2 -2
  454. data/test/models/department.rb +4 -4
  455. data/test/models/developer.rb +274 -274
  456. data/test/models/dog.rb +5 -5
  457. data/test/models/dog_lover.rb +5 -5
  458. data/test/models/doubloon.rb +12 -12
  459. data/test/models/drink_designer.rb +3 -3
  460. data/test/models/edge.rb +5 -5
  461. data/test/models/electron.rb +5 -5
  462. data/test/models/engine.rb +4 -4
  463. data/test/models/entrant.rb +3 -3
  464. data/test/models/essay.rb +5 -5
  465. data/test/models/event.rb +3 -3
  466. data/test/models/eye.rb +37 -37
  467. data/test/models/face.rb +9 -9
  468. data/test/models/friendship.rb +6 -6
  469. data/test/models/guid.rb +2 -2
  470. data/test/models/guitar.rb +4 -4
  471. data/test/models/hotel.rb +11 -11
  472. data/test/models/image.rb +3 -3
  473. data/test/models/interest.rb +5 -5
  474. data/test/models/invoice.rb +4 -4
  475. data/test/models/item.rb +7 -7
  476. data/test/models/job.rb +7 -7
  477. data/test/models/joke.rb +7 -7
  478. data/test/models/keyboard.rb +3 -3
  479. data/test/models/legacy_thing.rb +3 -3
  480. data/test/models/lesson.rb +11 -11
  481. data/test/models/line_item.rb +3 -3
  482. data/test/models/liquid.rb +4 -4
  483. data/test/models/man.rb +11 -11
  484. data/test/models/matey.rb +4 -4
  485. data/test/models/member.rb +42 -42
  486. data/test/models/member_detail.rb +8 -8
  487. data/test/models/member_type.rb +3 -3
  488. data/test/models/membership.rb +35 -35
  489. data/test/models/mentor.rb +2 -2
  490. data/test/models/minimalistic.rb +2 -2
  491. data/test/models/minivan.rb +9 -9
  492. data/test/models/mixed_case_monkey.rb +3 -3
  493. data/test/models/mocktail_designer.rb +2 -2
  494. data/test/models/molecule.rb +6 -6
  495. data/test/models/movie.rb +5 -5
  496. data/test/models/node.rb +5 -5
  497. data/test/models/non_primary_key.rb +2 -2
  498. data/test/models/notification.rb +3 -3
  499. data/test/models/order.rb +4 -4
  500. data/test/models/organization.rb +14 -14
  501. data/test/models/other_dog.rb +5 -5
  502. data/test/models/owner.rb +37 -37
  503. data/test/models/parrot.rb +28 -28
  504. data/test/models/person.rb +142 -142
  505. data/test/models/personal_legacy_thing.rb +4 -4
  506. data/test/models/pet.rb +18 -18
  507. data/test/models/pet_treasure.rb +6 -6
  508. data/test/models/pirate.rb +92 -92
  509. data/test/models/possession.rb +3 -3
  510. data/test/models/post.rb +273 -273
  511. data/test/models/price_estimate.rb +4 -4
  512. data/test/models/professor.rb +5 -5
  513. data/test/models/project.rb +40 -40
  514. data/test/models/publisher.rb +2 -2
  515. data/test/models/publisher/article.rb +4 -4
  516. data/test/models/publisher/magazine.rb +3 -3
  517. data/test/models/rating.rb +4 -4
  518. data/test/models/reader.rb +23 -23
  519. data/test/models/recipe.rb +3 -3
  520. data/test/models/record.rb +2 -2
  521. data/test/models/reference.rb +22 -22
  522. data/test/models/reply.rb +61 -61
  523. data/test/models/ship.rb +39 -39
  524. data/test/models/ship_part.rb +8 -8
  525. data/test/models/shop.rb +17 -17
  526. data/test/models/shop_account.rb +6 -6
  527. data/test/models/speedometer.rb +6 -6
  528. data/test/models/sponsor.rb +7 -7
  529. data/test/models/string_key_object.rb +3 -3
  530. data/test/models/student.rb +4 -4
  531. data/test/models/subject.rb +16 -16
  532. data/test/models/subscriber.rb +8 -8
  533. data/test/models/subscription.rb +4 -4
  534. data/test/models/tag.rb +13 -13
  535. data/test/models/tagging.rb +13 -13
  536. data/test/models/task.rb +5 -5
  537. data/test/models/topic.rb +118 -118
  538. data/test/models/toy.rb +6 -6
  539. data/test/models/traffic_light.rb +4 -4
  540. data/test/models/treasure.rb +14 -14
  541. data/test/models/treaty.rb +7 -7
  542. data/test/models/tree.rb +3 -3
  543. data/test/models/tuning_peg.rb +4 -4
  544. data/test/models/tyre.rb +11 -11
  545. data/test/models/user.rb +14 -14
  546. data/test/models/uuid_child.rb +3 -3
  547. data/test/models/uuid_item.rb +6 -6
  548. data/test/models/uuid_parent.rb +3 -3
  549. data/test/models/vegetables.rb +24 -24
  550. data/test/models/vehicle.rb +6 -6
  551. data/test/models/vertex.rb +9 -9
  552. data/test/models/warehouse_thing.rb +5 -5
  553. data/test/models/wheel.rb +3 -3
  554. data/test/models/without_table.rb +3 -3
  555. data/test/models/zine.rb +3 -3
  556. data/test/schema/i5/ibm_db_specific_schema.rb +137 -0
  557. data/test/schema/ids/ibm_db_specific_schema.rb +140 -0
  558. data/test/schema/luw/ibm_db_specific_schema.rb +137 -0
  559. data/test/schema/mysql2_specific_schema.rb +68 -68
  560. data/test/schema/oracle_specific_schema.rb +40 -40
  561. data/test/schema/postgresql_specific_schema.rb +114 -114
  562. data/test/schema/schema.rb +1057 -1057
  563. data/test/schema/schema.rb.original +1057 -1057
  564. data/test/schema/sqlite_specific_schema.rb +18 -18
  565. data/test/schema/zOS/ibm_db_specific_schema.rb +208 -0
  566. data/test/support/config.rb +43 -43
  567. data/test/support/connection.rb +23 -23
  568. data/test/support/connection_helper.rb +14 -14
  569. data/test/support/ddl_helper.rb +8 -8
  570. data/test/support/schema_dumping_helper.rb +20 -20
  571. data/test/support/yaml_compatibility_fixtures/rails_4_1.yml +22 -22
  572. data/test/support/yaml_compatibility_fixtures/rails_4_2_0.yml +182 -182
  573. metadata +30 -14
  574. data/lib/mswin32/rb2x/i386/ibm_db.so +0 -0
  575. data/test/fixtures/author_addresses.original +0 -11
  576. data/test/fixtures/authors.original +0 -17
@@ -1,2501 +1,2501 @@
1
- require "cases/helper"
2
- require 'models/developer'
3
- require 'models/computer'
4
- require 'models/project'
5
- require 'models/company'
6
- require 'models/contract'
7
- require 'models/topic'
8
- require 'models/reply'
9
- require 'models/category'
10
- require 'models/image'
11
- require 'models/post'
12
- require 'models/author'
13
- require 'models/essay'
14
- require 'models/comment'
15
- require 'models/person'
16
- require 'models/reader'
17
- require 'models/tagging'
18
- require 'models/tag'
19
- require 'models/invoice'
20
- require 'models/line_item'
21
- require 'models/car'
22
- require 'models/bulb'
23
- require 'models/engine'
24
- require 'models/categorization'
25
- require 'models/minivan'
26
- require 'models/speedometer'
27
- require 'models/reference'
28
- require 'models/job'
29
- require 'models/college'
30
- require 'models/student'
31
- require 'models/pirate'
32
- require 'models/ship'
33
- require 'models/ship_part'
34
- require 'models/treasure'
35
- require 'models/parrot'
36
- require 'models/tyre'
37
- require 'models/subscriber'
38
- require 'models/subscription'
39
- require 'models/zine'
40
- require 'models/interest'
41
-
42
- class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
43
- fixtures :authors, :posts, :comments
44
-
45
- def test_should_generate_valid_sql
46
- author = authors(:david)
47
- # this can fail on adapters which require ORDER BY expressions to be included in the SELECT expression
48
- # if the reorder clauses are not correctly handled
49
- assert author.posts_with_comments_sorted_by_comment_id.where('comments.id > 0').reorder('posts.comments_count DESC', 'posts.tags_count DESC').last
50
- end
51
- end
52
-
53
- class HasManyAssociationsTestPrimaryKeys < ActiveRecord::TestCase
54
- fixtures :authors, :essays, :subscribers, :subscriptions, :people
55
-
56
- def test_custom_primary_key_on_new_record_should_fetch_with_query
57
- subscriber = Subscriber.new(nick: 'webster132')
58
- assert !subscriber.subscriptions.loaded?
59
-
60
- assert_queries 1 do
61
- assert_equal 2, subscriber.subscriptions.size
62
- end
63
-
64
- assert_equal Subscription.where(subscriber_id: "webster132"), subscriber.subscriptions
65
- end
66
-
67
- def test_association_primary_key_on_new_record_should_fetch_with_query
68
- author = Author.new(:name => "David")
69
- assert !author.essays.loaded?
70
-
71
- assert_queries 1 do
72
- assert_equal 1, author.essays.size
73
- end
74
-
75
- assert_equal Essay.where(writer_id: "David"), author.essays
76
- end
77
-
78
- def test_has_many_custom_primary_key
79
- david = authors(:david)
80
- assert_equal Essay.where(writer_id: "David"), david.essays
81
- end
82
-
83
- def test_ids_on_unloaded_association_with_custom_primary_key
84
- david = people(:david)
85
- assert_equal Essay.where(writer_id: "David").pluck(:id), david.essay_ids
86
- end
87
-
88
- def test_ids_on_loaded_association_with_custom_primary_key
89
- david = people(:david)
90
- david.essays.load
91
- assert_equal Essay.where(writer_id: "David").pluck(:id), david.essay_ids
92
- end
93
-
94
- def test_has_many_assignment_with_custom_primary_key
95
- david = people(:david)
96
-
97
- assert_equal ["A Modest Proposal"], david.essays.map(&:name)
98
- david.essays = [Essay.create!(name: "Remote Work" )]
99
- assert_equal ["Remote Work"], david.essays.map(&:name)
100
- end
101
-
102
- def test_blank_custom_primary_key_on_new_record_should_not_run_queries
103
- author = Author.new
104
- assert !author.essays.loaded?
105
-
106
- assert_queries 0 do
107
- assert_equal 0, author.essays.size
108
- end
109
- end
110
- end
111
-
112
- class HasManyAssociationsTest < ActiveRecord::TestCase
113
- fixtures :accounts, :categories, :companies, :developers, :projects,
114
- :developers_projects, :topics, :authors, :comments,
115
- :posts, :readers, :taggings, :cars, :jobs, :tags,
116
- :categorizations, :zines, :interests
117
-
118
- def setup
119
- Client.destroyed_client_ids.clear
120
- end
121
-
122
- def test_sti_subselect_count
123
- tag = Tag.first
124
- len = Post.tagged_with(tag.id).limit(10).size
125
- assert_operator len, :>, 0
126
- end
127
-
128
- def test_anonymous_has_many
129
- developer = Class.new(ActiveRecord::Base) {
130
- self.table_name = 'developers'
131
- dev = self
132
-
133
- developer_project = Class.new(ActiveRecord::Base) {
134
- self.table_name = 'developers_projects'
135
- belongs_to :developer, :anonymous_class => dev
136
- }
137
- has_many :developer_projects, :anonymous_class => developer_project, :foreign_key => 'developer_id'
138
- }
139
- dev = developer.first
140
- named = Developer.find(dev.id)
141
- assert_operator dev.developer_projects.count, :>, 0
142
- assert_equal named.projects.map(&:id).sort,
143
- dev.developer_projects.map(&:project_id).sort
144
- end
145
-
146
- def test_default_scope_on_relations_is_not_cached
147
- counter = 0
148
- posts = Class.new(ActiveRecord::Base) {
149
- self.table_name = 'posts'
150
- self.inheritance_column = 'not_there'
151
- post = self
152
-
153
- comments = Class.new(ActiveRecord::Base) {
154
- self.table_name = 'comments'
155
- self.inheritance_column = 'not_there'
156
- belongs_to :post, :anonymous_class => post
157
- default_scope -> {
158
- counter += 1
159
- where("id = :inc", :inc => counter)
160
- }
161
- }
162
- has_many :comments, :anonymous_class => comments, :foreign_key => 'post_id'
163
- }
164
- assert_equal 0, counter
165
- post = posts.first
166
- assert_equal 0, counter
167
- sql = capture_sql { post.comments.to_a }
168
- post.comments.reset
169
- assert_not_equal sql, capture_sql { post.comments.to_a }
170
- end
171
-
172
- def test_has_many_build_with_options
173
- college = College.create(name: 'UFMT')
174
- Student.create(active: true, college_id: college.id, name: 'Sarah')
175
-
176
- assert_equal college.students, Student.where(active: true, college_id: college.id)
177
- end
178
-
179
- def test_add_record_to_collection_should_change_its_updated_at
180
- ship = Ship.create(name: 'dauntless')
181
- part = ShipPart.create(name: 'cockpit')
182
- updated_at = part.updated_at
183
-
184
- travel(1.second) do
185
- ship.parts << part
186
- end
187
-
188
- assert_equal part.ship, ship
189
- assert_not_equal part.updated_at, updated_at
190
- end
191
-
192
- def test_clear_collection_should_not_change_updated_at
193
- # GH#17161: .clear calls delete_all (and returns the association),
194
- # which is intended to not touch associated objects's updated_at field
195
- ship = Ship.create(name: 'dauntless')
196
- part = ShipPart.create(name: 'cockpit', ship_id: ship.id)
197
-
198
- ship.parts.clear
199
- part.reload
200
-
201
- assert_equal nil, part.ship
202
- assert !part.updated_at_changed?
203
- end
204
-
205
- def test_create_from_association_should_respect_default_scope
206
- car = Car.create(:name => 'honda')
207
- assert_equal 'honda', car.name
208
-
209
- bulb = Bulb.create
210
- assert_equal 'defaulty', bulb.name
211
-
212
- bulb = car.bulbs.build
213
- assert_equal 'defaulty', bulb.name
214
-
215
- bulb = car.bulbs.create
216
- assert_equal 'defaulty', bulb.name
217
- end
218
-
219
- def test_build_and_create_from_association_should_respect_passed_attributes_over_default_scope
220
- car = Car.create(name: 'honda')
221
-
222
- bulb = car.bulbs.build(name: 'exotic')
223
- assert_equal 'exotic', bulb.name
224
-
225
- bulb = car.bulbs.create(name: 'exotic')
226
- assert_equal 'exotic', bulb.name
227
-
228
- bulb = car.awesome_bulbs.build(frickinawesome: false)
229
- assert_equal false, bulb.frickinawesome
230
-
231
- bulb = car.awesome_bulbs.create(frickinawesome: false)
232
- assert_equal false, bulb.frickinawesome
233
- end
234
-
235
- def test_build_from_association_should_respect_scope
236
- author = Author.new
237
-
238
- post = author.thinking_posts.build
239
- assert_equal 'So I was thinking', post.title
240
- end
241
-
242
- def test_create_from_association_with_nil_values_should_work
243
- car = Car.create(:name => 'honda')
244
-
245
- bulb = car.bulbs.new(nil)
246
- assert_equal 'defaulty', bulb.name
247
-
248
- bulb = car.bulbs.build(nil)
249
- assert_equal 'defaulty', bulb.name
250
-
251
- bulb = car.bulbs.create(nil)
252
- assert_equal 'defaulty', bulb.name
253
- end
254
-
255
- def test_do_not_call_callbacks_for_delete_all
256
- car = Car.create(:name => 'honda')
257
- car.funky_bulbs.create!
258
- assert_nothing_raised { car.reload.funky_bulbs.delete_all }
259
- assert_equal 0, Bulb.count, "bulbs should have been deleted using :delete_all strategy"
260
- end
261
-
262
- def test_delete_all_on_association_is_the_same_as_not_loaded
263
- author = authors :david
264
- author.thinking_posts.create!(:body => "test")
265
- author.reload
266
- expected_sql = capture_sql { author.thinking_posts.delete_all }
267
-
268
- author.thinking_posts.create!(:body => "test")
269
- author.reload
270
- author.thinking_posts.inspect
271
- loaded_sql = capture_sql { author.thinking_posts.delete_all }
272
- assert_equal(expected_sql, loaded_sql)
273
- end
274
-
275
- def test_delete_all_on_association_with_nil_dependency_is_the_same_as_not_loaded
276
- author = authors :david
277
- author.posts.create!(:title => "test", :body => "body")
278
- author.reload
279
- expected_sql = capture_sql { author.posts.delete_all }
280
-
281
- author.posts.create!(:title => "test", :body => "body")
282
- author.reload
283
- author.posts.to_a
284
- loaded_sql = capture_sql { author.posts.delete_all }
285
- assert_equal(expected_sql, loaded_sql)
286
- end
287
-
288
- def test_building_the_associated_object_with_implicit_sti_base_class
289
- firm = DependentFirm.new
290
- company = firm.companies.build
291
- assert_kind_of Company, company, "Expected #{company.class} to be a Company"
292
- end
293
-
294
- def test_building_the_associated_object_with_explicit_sti_base_class
295
- firm = DependentFirm.new
296
- company = firm.companies.build(:type => "Company")
297
- assert_kind_of Company, company, "Expected #{company.class} to be a Company"
298
- end
299
-
300
- def test_building_the_associated_object_with_sti_subclass
301
- firm = DependentFirm.new
302
- company = firm.companies.build(:type => "Client")
303
- assert_kind_of Client, company, "Expected #{company.class} to be a Client"
304
- end
305
-
306
- def test_building_the_associated_object_with_an_invalid_type
307
- firm = DependentFirm.new
308
- assert_raise(ActiveRecord::SubclassNotFound) { firm.companies.build(:type => "Invalid") }
309
- end
310
-
311
- def test_building_the_associated_object_with_an_unrelated_type
312
- firm = DependentFirm.new
313
- assert_raise(ActiveRecord::SubclassNotFound) { firm.companies.build(:type => "Account") }
314
- end
315
-
316
- test "building the association with an array" do
317
- speedometer = Speedometer.new(speedometer_id: "a")
318
- data = [{name: "first"}, {name: "second"}]
319
- speedometer.minivans.build(data)
320
-
321
- assert_equal 2, speedometer.minivans.size
322
- assert speedometer.save
323
- assert_equal ["first", "second"], speedometer.reload.minivans.map(&:name)
324
- end
325
-
326
- def test_association_keys_bypass_attribute_protection
327
- car = Car.create(:name => 'honda')
328
-
329
- bulb = car.bulbs.new
330
- assert_equal car.id, bulb.car_id
331
-
332
- bulb = car.bulbs.new :car_id => car.id + 1
333
- assert_equal car.id, bulb.car_id
334
-
335
- bulb = car.bulbs.build
336
- assert_equal car.id, bulb.car_id
337
-
338
- bulb = car.bulbs.build :car_id => car.id + 1
339
- assert_equal car.id, bulb.car_id
340
-
341
- bulb = car.bulbs.create
342
- assert_equal car.id, bulb.car_id
343
-
344
- bulb = car.bulbs.create :car_id => car.id + 1
345
- assert_equal car.id, bulb.car_id
346
- end
347
-
348
- def test_association_protect_foreign_key
349
- invoice = Invoice.create
350
-
351
- line_item = invoice.line_items.new
352
- assert_equal invoice.id, line_item.invoice_id
353
-
354
- line_item = invoice.line_items.new :invoice_id => invoice.id + 1
355
- assert_equal invoice.id, line_item.invoice_id
356
-
357
- line_item = invoice.line_items.build
358
- assert_equal invoice.id, line_item.invoice_id
359
-
360
- line_item = invoice.line_items.build :invoice_id => invoice.id + 1
361
- assert_equal invoice.id, line_item.invoice_id
362
-
363
- line_item = invoice.line_items.create
364
- assert_equal invoice.id, line_item.invoice_id
365
-
366
- line_item = invoice.line_items.create :invoice_id => invoice.id + 1
367
- assert_equal invoice.id, line_item.invoice_id
368
- end
369
-
370
- # When creating objects on the association, we must not do it within a scope (even though it
371
- # would be convenient), because this would cause that scope to be applied to any callbacks etc.
372
- def test_build_and_create_should_not_happen_within_scope
373
- car = cars(:honda)
374
- scope = car.foo_bulbs.where_values_hash
375
-
376
- bulb = car.foo_bulbs.build
377
- assert_not_equal scope, bulb.scope_after_initialize.where_values_hash
378
-
379
- bulb = car.foo_bulbs.create
380
- assert_not_equal scope, bulb.scope_after_initialize.where_values_hash
381
-
382
- bulb = car.foo_bulbs.create!
383
- assert_not_equal scope, bulb.scope_after_initialize.where_values_hash
384
- end
385
-
386
- def test_no_sql_should_be_fired_if_association_already_loaded
387
- Car.create(:name => 'honda')
388
- bulbs = Car.first.bulbs
389
- bulbs.to_a # to load all instances of bulbs
390
-
391
- assert_no_queries do
392
- bulbs.first()
393
- bulbs.first({})
394
- end
395
-
396
- assert_no_queries do
397
- bulbs.second()
398
- bulbs.second({})
399
- end
400
-
401
- assert_no_queries do
402
- bulbs.third()
403
- bulbs.third({})
404
- end
405
-
406
- assert_no_queries do
407
- bulbs.fourth()
408
- bulbs.fourth({})
409
- end
410
-
411
- assert_no_queries do
412
- bulbs.fifth()
413
- bulbs.fifth({})
414
- end
415
-
416
- assert_no_queries do
417
- bulbs.forty_two()
418
- bulbs.forty_two({})
419
- end
420
-
421
- assert_no_queries do
422
- bulbs.third_to_last()
423
- bulbs.third_to_last({})
424
- end
425
-
426
- assert_no_queries do
427
- bulbs.second_to_last()
428
- bulbs.second_to_last({})
429
- end
430
-
431
- assert_no_queries do
432
- bulbs.last()
433
- bulbs.last({})
434
- end
435
- end
436
-
437
- def test_create_resets_cached_counters
438
- person = Person.create!(:first_name => 'tenderlove')
439
- post = Post.first
440
-
441
- assert_equal [], person.readers
442
- assert_nil person.readers.find_by_post_id(post.id)
443
-
444
- person.readers.create(:post_id => post.id)
445
-
446
- assert_equal 1, person.readers.count
447
- assert_equal 1, person.readers.length
448
- assert_equal post, person.readers.first.post
449
- assert_equal person, person.readers.first.person
450
- end
451
-
452
- def test_update_all_respects_association_scope
453
- person = Person.new
454
- person.first_name = 'Naruto'
455
- person.references << Reference.new
456
- person.id = 10
457
- person.references
458
- person.save!
459
- assert_equal 1, person.references.update_all(favourite: true)
460
- end
461
-
462
- def test_exists_respects_association_scope
463
- person = Person.new
464
- person.first_name = 'Sasuke'
465
- person.references << Reference.new
466
- person.id = 10
467
- person.references
468
- person.save!
469
- assert_predicate person.references, :exists?
470
- end
471
-
472
- def force_signal37_to_load_all_clients_of_firm
473
- companies(:first_firm).clients_of_firm.each {|f| }
474
- end
475
-
476
- # sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
477
- def test_counting_with_counter_sql
478
- assert_equal 3, Firm.all.merge!(:order => "id").first.clients.count
479
- end
480
-
481
- def test_counting
482
- assert_equal 3, Firm.all.merge!(:order => "id").first.plain_clients.count
483
- end
484
-
485
- def test_counting_with_single_hash
486
- assert_equal 1, Firm.all.merge!(:order => "id").first.plain_clients.where(:name => "Microsoft").count
487
- end
488
-
489
- def test_counting_with_column_name_and_hash
490
- assert_equal 3, Firm.all.merge!(:order => "id").first.plain_clients.count(:name)
491
- end
492
-
493
- def test_counting_with_association_limit
494
- firm = companies(:first_firm)
495
- assert_equal firm.limited_clients.length, firm.limited_clients.size
496
- assert_equal firm.limited_clients.length, firm.limited_clients.count
497
- end
498
-
499
- def test_finding
500
- assert_equal 3, Firm.all.merge!(:order => "id").first.clients.length
501
- end
502
-
503
- def test_finding_array_compatibility
504
- assert_equal 3, Firm.order(:id).find{|f| f.id > 0}.clients.length
505
- end
506
-
507
- def test_find_many_with_merged_options
508
- assert_equal 1, companies(:first_firm).limited_clients.size
509
- assert_equal 1, companies(:first_firm).limited_clients.to_a.size
510
- assert_equal 3, companies(:first_firm).limited_clients.limit(nil).to_a.size
511
- end
512
-
513
- def test_find_should_append_to_association_order
514
- ordered_clients = companies(:first_firm).clients_sorted_desc.order('companies.id')
515
- assert_equal ['id DESC', 'companies.id'], ordered_clients.order_values
516
- end
517
-
518
- def test_dynamic_find_should_respect_association_order
519
- assert_equal companies(:another_first_firm_client), companies(:first_firm).clients_sorted_desc.where("type = 'Client'").first
520
- assert_equal companies(:another_first_firm_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client')
521
- end
522
-
523
- def test_taking
524
- posts(:other_by_bob).destroy
525
- assert_equal posts(:misc_by_bob), authors(:bob).posts.take
526
- assert_equal posts(:misc_by_bob), authors(:bob).posts.take!
527
- authors(:bob).posts.to_a
528
- assert_equal posts(:misc_by_bob), authors(:bob).posts.take
529
- assert_equal posts(:misc_by_bob), authors(:bob).posts.take!
530
- end
531
-
532
- def test_taking_not_found
533
- authors(:bob).posts.delete_all
534
- assert_raise(ActiveRecord::RecordNotFound) { authors(:bob).posts.take! }
535
- authors(:bob).posts.to_a
536
- assert_raise(ActiveRecord::RecordNotFound) { authors(:bob).posts.take! }
537
- end
538
-
539
- def test_taking_with_a_number
540
- # taking from unloaded Relation
541
- bob = Author.find(authors(:bob).id)
542
- assert_equal [posts(:misc_by_bob)], bob.posts.take(1)
543
- bob = Author.find(authors(:bob).id)
544
- assert_equal [posts(:misc_by_bob), posts(:other_by_bob)], bob.posts.take(2)
545
-
546
- # taking from loaded Relation
547
- bob.posts.to_a
548
- assert_equal [posts(:misc_by_bob)], authors(:bob).posts.take(1)
549
- assert_equal [posts(:misc_by_bob), posts(:other_by_bob)], authors(:bob).posts.take(2)
550
- end
551
-
552
- def test_taking_with_inverse_of
553
- interests(:woodsmanship).destroy
554
- interests(:survival).destroy
555
-
556
- zine = zines(:going_out)
557
- interest = zine.interests.take
558
- assert_equal interests(:hunting), interest
559
- assert_same zine, interest.zine
560
- end
561
-
562
- def test_cant_save_has_many_readonly_association
563
- authors(:david).readonly_comments.each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
564
- authors(:david).readonly_comments.each { |c| assert c.readonly? }
565
- end
566
-
567
- def test_finding_default_orders
568
- assert_equal "Summit", Firm.all.merge!(:order => "id").first.clients.first.name
569
- end
570
-
571
- def test_finding_with_different_class_name_and_order
572
- assert_equal "Apex", Firm.all.merge!(:order => "id").first.clients_sorted_desc.first.name
573
- end
574
-
575
- def test_finding_with_foreign_key
576
- assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_of_firm.first.name
577
- end
578
-
579
- def test_finding_with_condition
580
- assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_like_ms.first.name
581
- end
582
-
583
- def test_finding_with_condition_hash
584
- assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_like_ms_with_hash_conditions.first.name
585
- end
586
-
587
- def test_finding_using_primary_key
588
- assert_equal "Summit", Firm.all.merge!(:order => "id").first.clients_using_primary_key.first.name
589
- end
590
-
591
- def test_update_all_on_association_accessed_before_save
592
- firm = Firm.new(name: "Firm")
593
- firm.clients << Client.first
594
- firm.save!
595
- assert_equal firm.clients.count, firm.clients.update_all(description: "Great!")
596
- end
597
-
598
- def test_update_all_on_association_accessed_before_save_with_explicit_foreign_key
599
- firm = Firm.new(name: "Firm", id: 100)
600
- firm.clients << Client.first
601
- firm.save!
602
- assert_equal firm.clients.count, firm.clients.update_all(description: "Great!")
603
- end
604
-
605
- def test_belongs_to_sanity
606
- c = Client.new
607
- assert_nil c.firm, "belongs_to failed sanity check on new object"
608
- end
609
-
610
- def test_find_ids
611
- firm = Firm.all.merge!(:order => "id").first
612
-
613
- assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find }
614
-
615
- client = firm.clients.find(2)
616
- assert_kind_of Client, client
617
-
618
- client_ary = firm.clients.find([2])
619
- assert_kind_of Array, client_ary
620
- assert_equal client, client_ary.first
621
-
622
- client_ary = firm.clients.find(2, 3)
623
- assert_kind_of Array, client_ary
624
- assert_equal 2, client_ary.size
625
- assert_equal client, client_ary.first
626
-
627
- assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
628
- end
629
-
630
- def test_find_one_message_on_primary_key
631
- firm = Firm.all.merge!(order: "id").first
632
-
633
- e = assert_raises(ActiveRecord::RecordNotFound) do
634
- firm.clients.find(0)
635
- end
636
- assert_equal 0, e.id
637
- assert_equal "id", e.primary_key
638
- assert_equal "Client", e.model
639
- assert_match (/\ACouldn't find Client with 'id'=0/), e.message
640
- end
641
-
642
- def test_find_ids_and_inverse_of
643
- force_signal37_to_load_all_clients_of_firm
644
-
645
- firm = companies(:first_firm)
646
- client = firm.clients_of_firm.find(3)
647
- assert_kind_of Client, client
648
-
649
- client_ary = firm.clients_of_firm.find([3])
650
- assert_kind_of Array, client_ary
651
- assert_equal client, client_ary.first
652
- end
653
-
654
- def test_find_all
655
- firm = Firm.all.merge!(:order => "id").first
656
- assert_equal 3, firm.clients.where("#{QUOTED_TYPE} = 'Client'").to_a.length
657
- assert_equal 1, firm.clients.where("name = 'Summit'").to_a.length
658
- end
659
-
660
- def test_find_each
661
- firm = companies(:first_firm)
662
-
663
- assert ! firm.clients.loaded?
664
-
665
- assert_queries(4) do
666
- firm.clients.find_each(:batch_size => 1) {|c| assert_equal firm.id, c.firm_id }
667
- end
668
-
669
- assert ! firm.clients.loaded?
670
- end
671
-
672
- def test_find_each_with_conditions
673
- firm = companies(:first_firm)
674
-
675
- assert_queries(2) do
676
- firm.clients.where(name: 'Microsoft').find_each(batch_size: 1) do |c|
677
- assert_equal firm.id, c.firm_id
678
- assert_equal "Microsoft", c.name
679
- end
680
- end
681
-
682
- assert ! firm.clients.loaded?
683
- end
684
-
685
- def test_find_in_batches
686
- firm = companies(:first_firm)
687
-
688
- assert ! firm.clients.loaded?
689
-
690
- assert_queries(2) do
691
- firm.clients.find_in_batches(:batch_size => 2) do |clients|
692
- clients.each {|c| assert_equal firm.id, c.firm_id }
693
- end
694
- end
695
-
696
- assert ! firm.clients.loaded?
697
- end
698
-
699
- def test_find_all_sanitized
700
- # sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
701
- firm = Firm.all.merge!(:order => "id").first
702
- summit = firm.clients.where("name = 'Summit'").to_a
703
- assert_equal summit, firm.clients.where("name = ?", "Summit").to_a
704
- assert_equal summit, firm.clients.where("name = :name", { :name => "Summit" }).to_a
705
- end
706
-
707
- def test_find_first
708
- firm = Firm.all.merge!(:order => "id").first
709
- client2 = Client.find(2)
710
- assert_equal firm.clients.first, firm.clients.order("id").first
711
- assert_equal client2, firm.clients.where("#{QUOTED_TYPE} = 'Client'").order("id").first
712
- end
713
-
714
- def test_find_first_sanitized
715
- firm = Firm.all.merge!(:order => "id").first
716
- client2 = Client.find(2)
717
- assert_equal client2, firm.clients.merge!(:where => ["#{QUOTED_TYPE} = ?", 'Client'], :order => "id").first
718
- assert_equal client2, firm.clients.merge!(:where => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }], :order => "id").first
719
- end
720
-
721
- def test_find_all_with_include_and_conditions
722
- assert_nothing_raised do
723
- Developer.all.merge!(:joins => :audit_logs, :where => {'audit_logs.message' => nil, :name => 'Smith'}).to_a
724
- end
725
- end
726
-
727
- def test_find_in_collection
728
- assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name
729
- assert_raise(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
730
- end
731
-
732
- def test_find_grouped
733
- all_clients_of_firm1 = Client.all.merge!(:where => "firm_id = 1").to_a
734
- grouped_clients_of_firm1 = Client.all.merge!(:where => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count').to_a
735
- assert_equal 3, all_clients_of_firm1.size
736
- assert_equal 1, grouped_clients_of_firm1.size
737
- end
738
-
739
- def test_find_scoped_grouped
740
- assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.size
741
- assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length
742
- assert_equal 3, companies(:first_firm).clients_grouped_by_name.size
743
- assert_equal 3, companies(:first_firm).clients_grouped_by_name.length
744
- end
745
-
746
- def test_find_scoped_grouped_having
747
- assert_equal 1, authors(:david).popular_grouped_posts.length
748
- assert_equal 0, authors(:mary).popular_grouped_posts.length
749
- end
750
-
751
- def test_default_select
752
- assert_equal Comment.column_names.sort, posts(:welcome).comments.first.attributes.keys.sort
753
- end
754
-
755
- def test_select_query_method
756
- assert_equal ['id', 'body'], posts(:welcome).comments.select(:id, :body).first.attributes.keys
757
- end
758
-
759
- def test_select_with_block
760
- assert_equal [1], posts(:welcome).comments.select { |c| c.id == 1 }.map(&:id)
761
- end
762
-
763
- def test_select_without_foreign_key
764
- assert_equal companies(:first_firm).accounts.first.credit_limit, companies(:first_firm).accounts.select(:credit_limit).first.credit_limit
765
- end
766
-
767
- def test_adding
768
- force_signal37_to_load_all_clients_of_firm
769
- natural = Client.new("name" => "Natural Company")
770
- companies(:first_firm).clients_of_firm << natural
771
- assert_equal 3, companies(:first_firm).clients_of_firm.size # checking via the collection
772
- assert_equal 3, companies(:first_firm).clients_of_firm.reload.size # checking using the db
773
- assert_equal natural, companies(:first_firm).clients_of_firm.last
774
- end
775
-
776
- def test_adding_using_create
777
- first_firm = companies(:first_firm)
778
- assert_equal 3, first_firm.plain_clients.size
779
- first_firm.plain_clients.create(:name => "Natural Company")
780
- assert_equal 4, first_firm.plain_clients.length
781
- assert_equal 4, first_firm.plain_clients.size
782
- end
783
-
784
- def test_create_with_bang_on_has_many_when_parent_is_new_raises
785
- error = assert_raise(ActiveRecord::RecordNotSaved) do
786
- firm = Firm.new
787
- firm.plain_clients.create! :name=>"Whoever"
788
- end
789
-
790
- assert_equal "You cannot call create unless the parent is saved", error.message
791
- end
792
-
793
- def test_regular_create_on_has_many_when_parent_is_new_raises
794
- error = assert_raise(ActiveRecord::RecordNotSaved) do
795
- firm = Firm.new
796
- firm.plain_clients.create :name=>"Whoever"
797
- end
798
-
799
- assert_equal "You cannot call create unless the parent is saved", error.message
800
- end
801
-
802
- def test_create_with_bang_on_has_many_raises_when_record_not_saved
803
- assert_raise(ActiveRecord::RecordInvalid) do
804
- firm = Firm.all.merge!(:order => "id").first
805
- firm.plain_clients.create!
806
- end
807
- end
808
-
809
- def test_create_with_bang_on_habtm_when_parent_is_new_raises
810
- error = assert_raise(ActiveRecord::RecordNotSaved) do
811
- Developer.new("name" => "Aredridel").projects.create!
812
- end
813
-
814
- assert_equal "You cannot call create unless the parent is saved", error.message
815
- end
816
-
817
- def test_adding_a_mismatch_class
818
- assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
819
- assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
820
- assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
821
- end
822
-
823
- def test_adding_a_collection
824
- force_signal37_to_load_all_clients_of_firm
825
- companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")])
826
- assert_equal 4, companies(:first_firm).clients_of_firm.size
827
- assert_equal 4, companies(:first_firm).clients_of_firm.reload.size
828
- end
829
-
830
- def test_transactions_when_adding_to_persisted
831
- good = Client.new(:name => "Good")
832
- bad = Client.new(:name => "Bad", :raise_on_save => true)
833
-
834
- begin
835
- companies(:first_firm).clients_of_firm.concat(good, bad)
836
- rescue Client::RaisedOnSave
837
- end
838
-
839
- assert !companies(:first_firm).clients_of_firm.reload.include?(good)
840
- end
841
-
842
- def test_transactions_when_adding_to_new_record
843
- assert_no_queries(ignore_none: false) do
844
- firm = Firm.new
845
- firm.clients_of_firm.concat(Client.new("name" => "Natural Company"))
846
- end
847
- end
848
-
849
- def test_inverse_on_before_validate
850
- firm = companies(:first_firm)
851
- assert_queries(1) do
852
- firm.clients_of_firm << Client.new("name" => "Natural Company")
853
- end
854
- end
855
-
856
- def test_new_aliased_to_build
857
- company = companies(:first_firm)
858
- new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.new("name" => "Another Client") }
859
- assert !company.clients_of_firm.loaded?
860
-
861
- assert_equal "Another Client", new_client.name
862
- assert !new_client.persisted?
863
- assert_equal new_client, company.clients_of_firm.last
864
- end
865
-
866
- def test_build
867
- company = companies(:first_firm)
868
- new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build("name" => "Another Client") }
869
- assert !company.clients_of_firm.loaded?
870
-
871
- assert_equal "Another Client", new_client.name
872
- assert !new_client.persisted?
873
- assert_equal new_client, company.clients_of_firm.last
874
- end
875
-
876
- def test_collection_size_after_building
877
- company = companies(:first_firm) # company already has one client
878
- company.clients_of_firm.build("name" => "Another Client")
879
- company.clients_of_firm.build("name" => "Yet Another Client")
880
- assert_equal 4, company.clients_of_firm.size
881
- end
882
-
883
- def test_collection_not_empty_after_building
884
- company = companies(:first_firm)
885
- assert_predicate company.contracts, :empty?
886
- company.contracts.build
887
- assert_not_predicate company.contracts, :empty?
888
- end
889
-
890
- def test_collection_size_twice_for_regressions
891
- post = posts(:thinking)
892
- assert_equal 0, post.readers.size
893
- # This test needs a post that has no readers, we assert it to ensure it holds,
894
- # but need to reload the post because the very call to #size hides the bug.
895
- post.reload
896
- post.readers.build
897
- size1 = post.readers.size
898
- size2 = post.readers.size
899
- assert_equal size1, size2
900
- end
901
-
902
- def test_build_many
903
- company = companies(:first_firm)
904
- new_clients = assert_no_queries(ignore_none: false) { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }
905
- assert_equal 2, new_clients.size
906
- end
907
-
908
- def test_build_followed_by_save_does_not_load_target
909
- companies(:first_firm).clients_of_firm.build("name" => "Another Client")
910
- assert companies(:first_firm).save
911
- assert !companies(:first_firm).clients_of_firm.loaded?
912
- end
913
-
914
- def test_build_without_loading_association
915
- first_topic = topics(:first)
916
- Reply.column_names
917
-
918
- assert_equal 1, first_topic.replies.length
919
-
920
- assert_no_queries do
921
- first_topic.replies.build(:title => "Not saved", :content => "Superstars")
922
- assert_equal 2, first_topic.replies.size
923
- end
924
-
925
- assert_equal 2, first_topic.replies.to_ary.size
926
- end
927
-
928
- def test_build_via_block
929
- company = companies(:first_firm)
930
- new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build {|client| client.name = "Another Client" } }
931
- assert !company.clients_of_firm.loaded?
932
-
933
- assert_equal "Another Client", new_client.name
934
- assert !new_client.persisted?
935
- assert_equal new_client, company.clients_of_firm.last
936
- end
937
-
938
- def test_build_many_via_block
939
- company = companies(:first_firm)
940
- new_clients = assert_no_queries(ignore_none: false) do
941
- company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) do |client|
942
- client.name = "changed"
943
- end
944
- end
945
-
946
- assert_equal 2, new_clients.size
947
- assert_equal "changed", new_clients.first.name
948
- assert_equal "changed", new_clients.last.name
949
- end
950
-
951
- def test_create_without_loading_association
952
- first_firm = companies(:first_firm)
953
- Firm.column_names
954
- Client.column_names
955
-
956
- assert_equal 2, first_firm.clients_of_firm.size
957
- first_firm.clients_of_firm.reset
958
-
959
- assert_queries(1) do
960
- first_firm.clients_of_firm.create(:name => "Superstars")
961
- end
962
-
963
- assert_equal 3, first_firm.clients_of_firm.size
964
- end
965
-
966
- def test_create
967
- force_signal37_to_load_all_clients_of_firm
968
- new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
969
- assert new_client.persisted?
970
- assert_equal new_client, companies(:first_firm).clients_of_firm.last
971
- assert_equal new_client, companies(:first_firm).clients_of_firm.reload.last
972
- end
973
-
974
- def test_create_many
975
- companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
976
- assert_equal 4, companies(:first_firm).clients_of_firm.reload.size
977
- end
978
-
979
- def test_create_followed_by_save_does_not_load_target
980
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
981
- assert companies(:first_firm).save
982
- assert !companies(:first_firm).clients_of_firm.loaded?
983
- end
984
-
985
- def test_deleting
986
- force_signal37_to_load_all_clients_of_firm
987
- companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
988
- assert_equal 1, companies(:first_firm).clients_of_firm.size
989
- assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
990
- end
991
-
992
- def test_deleting_before_save
993
- new_firm = Firm.new("name" => "A New Firm, Inc.")
994
- new_client = new_firm.clients_of_firm.build("name" => "Another Client")
995
- assert_equal 1, new_firm.clients_of_firm.size
996
- new_firm.clients_of_firm.delete(new_client)
997
- assert_equal 0, new_firm.clients_of_firm.size
998
- end
999
-
1000
- def test_has_many_without_counter_cache_option
1001
- # Ship has a conventionally named `treasures_count` column, but the counter_cache
1002
- # option is not given on the association.
1003
- ship = Ship.create(name: 'Countless', treasures_count: 10)
1004
-
1005
- assert_not Ship.reflect_on_association(:treasures).has_cached_counter?
1006
-
1007
- # Count should come from sql count() of treasures rather than treasures_count attribute
1008
- assert_equal ship.treasures.size, 0
1009
-
1010
- assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed" do
1011
- ship.treasures.create(name: 'Gold')
1012
- end
1013
-
1014
- assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed" do
1015
- ship.treasures.destroy_all
1016
- end
1017
- end
1018
-
1019
- def test_deleting_updates_counter_cache
1020
- topic = Topic.order("id ASC").first
1021
- assert_equal topic.replies.to_a.size, topic.replies_count
1022
-
1023
- topic.replies.delete(topic.replies.first)
1024
- topic.reload
1025
- assert_equal topic.replies.to_a.size, topic.replies_count
1026
- end
1027
-
1028
- def test_counter_cache_updates_in_memory_after_concat
1029
- topic = Topic.create title: "Zoom-zoom-zoom"
1030
-
1031
- topic.replies << Reply.create(title: "re: zoom", content: "speedy quick!")
1032
- assert_equal 1, topic.replies_count
1033
- assert_equal 1, topic.replies.size
1034
- assert_equal 1, topic.reload.replies.size
1035
- end
1036
-
1037
- def test_counter_cache_updates_in_memory_after_create
1038
- topic = Topic.create title: "Zoom-zoom-zoom"
1039
-
1040
- topic.replies.create!(title: "re: zoom", content: "speedy quick!")
1041
- assert_equal 1, topic.replies_count
1042
- assert_equal 1, topic.replies.size
1043
- assert_equal 1, topic.reload.replies.size
1044
- end
1045
-
1046
- def test_counter_cache_updates_in_memory_after_create_with_array
1047
- topic = Topic.create title: "Zoom-zoom-zoom"
1048
-
1049
- topic.replies.create!([
1050
- { title: "re: zoom", content: "speedy quick!" },
1051
- { title: "re: zoom 2", content: "OMG lol!" },
1052
- ])
1053
- assert_equal 2, topic.replies_count
1054
- assert_equal 2, topic.replies.size
1055
- assert_equal 2, topic.reload.replies.size
1056
- end
1057
-
1058
- def test_pushing_association_updates_counter_cache
1059
- topic = Topic.order("id ASC").first
1060
- reply = Reply.create!
1061
-
1062
- assert_difference "topic.reload.replies_count", 1 do
1063
- topic.replies << reply
1064
- end
1065
- end
1066
-
1067
- def test_deleting_updates_counter_cache_without_dependent_option
1068
- post = posts(:welcome)
1069
-
1070
- assert_difference "post.reload.tags_count", -1 do
1071
- post.taggings.delete(post.taggings.first)
1072
- end
1073
- end
1074
-
1075
- def test_deleting_updates_counter_cache_with_dependent_delete_all
1076
- post = posts(:welcome)
1077
- post.update_columns(taggings_with_delete_all_count: post.tags_count)
1078
-
1079
- assert_difference "post.reload.taggings_with_delete_all_count", -1 do
1080
- post.taggings_with_delete_all.delete(post.taggings_with_delete_all.first)
1081
- end
1082
- end
1083
-
1084
- def test_deleting_updates_counter_cache_with_dependent_destroy
1085
- post = posts(:welcome)
1086
- post.update_columns(taggings_with_destroy_count: post.tags_count)
1087
-
1088
- assert_difference "post.reload.taggings_with_destroy_count", -1 do
1089
- post.taggings_with_destroy.delete(post.taggings_with_destroy.first)
1090
- end
1091
- end
1092
-
1093
- def test_calling_empty_with_counter_cache
1094
- post = posts(:welcome)
1095
- assert_queries(0) do
1096
- assert_not post.comments.empty?
1097
- end
1098
- end
1099
-
1100
- def test_custom_named_counter_cache
1101
- topic = topics(:first)
1102
-
1103
- assert_difference "topic.reload.replies_count", -1 do
1104
- topic.approved_replies.clear
1105
- end
1106
- end
1107
-
1108
- def test_calling_update_attributes_on_id_changes_the_counter_cache
1109
- topic = Topic.order("id ASC").first
1110
- original_count = topic.replies.to_a.size
1111
- assert_equal original_count, topic.replies_count
1112
-
1113
- first_reply = topic.replies.first
1114
- first_reply.update_attributes(:parent_id => nil)
1115
- assert_equal original_count - 1, topic.reload.replies_count
1116
-
1117
- first_reply.update_attributes(:parent_id => topic.id)
1118
- assert_equal original_count, topic.reload.replies_count
1119
- end
1120
-
1121
- def test_calling_update_attributes_changing_ids_doesnt_change_counter_cache
1122
- topic1 = Topic.find(1)
1123
- topic2 = Topic.find(3)
1124
- original_count1 = topic1.replies.to_a.size
1125
- original_count2 = topic2.replies.to_a.size
1126
-
1127
- reply1 = topic1.replies.first
1128
- reply2 = topic2.replies.first
1129
-
1130
- reply1.update_attributes(:parent_id => topic2.id)
1131
- assert_equal original_count1 - 1, topic1.reload.replies_count
1132
- assert_equal original_count2 + 1, topic2.reload.replies_count
1133
-
1134
- reply2.update_attributes(:parent_id => topic1.id)
1135
- assert_equal original_count1, topic1.reload.replies_count
1136
- assert_equal original_count2, topic2.reload.replies_count
1137
- end
1138
-
1139
- def test_deleting_a_collection
1140
- force_signal37_to_load_all_clients_of_firm
1141
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
1142
- assert_equal 3, companies(:first_firm).clients_of_firm.size
1143
- companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1], companies(:first_firm).clients_of_firm[2]])
1144
- assert_equal 0, companies(:first_firm).clients_of_firm.size
1145
- assert_equal 0, companies(:first_firm).clients_of_firm.reload.size
1146
- end
1147
-
1148
- def test_delete_all
1149
- force_signal37_to_load_all_clients_of_firm
1150
- companies(:first_firm).dependent_clients_of_firm.create("name" => "Another Client")
1151
- clients = companies(:first_firm).dependent_clients_of_firm.to_a
1152
- assert_equal 3, clients.count
1153
-
1154
- assert_difference "Client.count", -(clients.count) do
1155
- companies(:first_firm).dependent_clients_of_firm.delete_all
1156
- end
1157
- end
1158
-
1159
- def test_delete_all_with_not_yet_loaded_association_collection
1160
- force_signal37_to_load_all_clients_of_firm
1161
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
1162
- assert_equal 3, companies(:first_firm).clients_of_firm.size
1163
- companies(:first_firm).clients_of_firm.reset
1164
- companies(:first_firm).clients_of_firm.delete_all
1165
- assert_equal 0, companies(:first_firm).clients_of_firm.size
1166
- assert_equal 0, companies(:first_firm).clients_of_firm.reload.size
1167
- end
1168
-
1169
- def test_transaction_when_deleting_persisted
1170
- good = Client.new(:name => "Good")
1171
- bad = Client.new(:name => "Bad", :raise_on_destroy => true)
1172
-
1173
- companies(:first_firm).clients_of_firm = [good, bad]
1174
-
1175
- begin
1176
- companies(:first_firm).clients_of_firm.destroy(good, bad)
1177
- rescue Client::RaisedOnDestroy
1178
- end
1179
-
1180
- assert_equal [good, bad], companies(:first_firm).clients_of_firm.reload
1181
- end
1182
-
1183
- def test_transaction_when_deleting_new_record
1184
- assert_no_queries(ignore_none: false) do
1185
- firm = Firm.new
1186
- client = Client.new("name" => "New Client")
1187
- firm.clients_of_firm << client
1188
- firm.clients_of_firm.destroy(client)
1189
- end
1190
- end
1191
-
1192
- def test_clearing_an_association_collection
1193
- firm = companies(:first_firm)
1194
- client_id = firm.clients_of_firm.first.id
1195
- assert_equal 2, firm.clients_of_firm.size
1196
-
1197
- firm.clients_of_firm.clear
1198
-
1199
- assert_equal 0, firm.clients_of_firm.size
1200
- assert_equal 0, firm.clients_of_firm.reload.size
1201
- assert_equal [], Client.destroyed_client_ids[firm.id]
1202
-
1203
- # Should not be destroyed since the association is not dependent.
1204
- assert_nothing_raised do
1205
- assert_nil Client.find(client_id).firm
1206
- end
1207
- end
1208
-
1209
- def test_clearing_updates_counter_cache
1210
- topic = Topic.first
1211
-
1212
- assert_difference 'topic.reload.replies_count', -1 do
1213
- topic.replies.clear
1214
- end
1215
- end
1216
-
1217
- def test_clearing_updates_counter_cache_when_inverse_counter_cache_is_a_symbol_with_dependent_destroy
1218
- car = Car.first
1219
- car.engines.create!
1220
-
1221
- assert_difference 'car.reload.engines_count', -1 do
1222
- car.engines.clear
1223
- end
1224
- end
1225
-
1226
- def test_clearing_a_dependent_association_collection
1227
- firm = companies(:first_firm)
1228
- client_id = firm.dependent_clients_of_firm.first.id
1229
- assert_equal 2, firm.dependent_clients_of_firm.size
1230
- assert_equal 1, Client.find_by_id(client_id).client_of
1231
-
1232
- # :delete_all is called on each client since the dependent options is :destroy
1233
- firm.dependent_clients_of_firm.clear
1234
-
1235
- assert_equal 0, firm.dependent_clients_of_firm.size
1236
- assert_equal 0, firm.dependent_clients_of_firm.reload.size
1237
- assert_equal [], Client.destroyed_client_ids[firm.id]
1238
-
1239
- # Should be destroyed since the association is dependent.
1240
- assert_nil Client.find_by_id(client_id)
1241
- end
1242
-
1243
- def test_delete_all_with_option_delete_all
1244
- firm = companies(:first_firm)
1245
- client_id = firm.dependent_clients_of_firm.first.id
1246
- firm.dependent_clients_of_firm.delete_all(:delete_all)
1247
- assert_nil Client.find_by_id(client_id)
1248
- end
1249
-
1250
- def test_delete_all_accepts_limited_parameters
1251
- firm = companies(:first_firm)
1252
- assert_raise(ArgumentError) do
1253
- firm.dependent_clients_of_firm.delete_all(:destroy)
1254
- end
1255
- end
1256
-
1257
- def test_clearing_an_exclusively_dependent_association_collection
1258
- firm = companies(:first_firm)
1259
- client_id = firm.exclusively_dependent_clients_of_firm.first.id
1260
- assert_equal 2, firm.exclusively_dependent_clients_of_firm.size
1261
-
1262
- assert_equal [], Client.destroyed_client_ids[firm.id]
1263
-
1264
- # :exclusively_dependent means each client is deleted directly from
1265
- # the database without looping through them calling destroy.
1266
- firm.exclusively_dependent_clients_of_firm.clear
1267
-
1268
- assert_equal 0, firm.exclusively_dependent_clients_of_firm.size
1269
- assert_equal 0, firm.exclusively_dependent_clients_of_firm.reload.size
1270
- # no destroy-filters should have been called
1271
- assert_equal [], Client.destroyed_client_ids[firm.id]
1272
-
1273
- # Should be destroyed since the association is exclusively dependent.
1274
- assert_nil Client.find_by_id(client_id)
1275
- end
1276
-
1277
- def test_dependent_association_respects_optional_conditions_on_delete
1278
- firm = companies(:odegy)
1279
- Client.create(:client_of => firm.id, :name => "BigShot Inc.")
1280
- Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
1281
- # only one of two clients is included in the association due to the :conditions key
1282
- assert_equal 2, Client.where(client_of: firm.id).size
1283
- assert_equal 1, firm.dependent_conditional_clients_of_firm.size
1284
- firm.destroy
1285
- # only the correctly associated client should have been deleted
1286
- assert_equal 1, Client.where(client_of: firm.id).size
1287
- end
1288
-
1289
- def test_dependent_association_respects_optional_sanitized_conditions_on_delete
1290
- firm = companies(:odegy)
1291
- Client.create(:client_of => firm.id, :name => "BigShot Inc.")
1292
- Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
1293
- # only one of two clients is included in the association due to the :conditions key
1294
- assert_equal 2, Client.where(client_of: firm.id).size
1295
- assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
1296
- firm.destroy
1297
- # only the correctly associated client should have been deleted
1298
- assert_equal 1, Client.where(client_of: firm.id).size
1299
- end
1300
-
1301
- def test_dependent_association_respects_optional_hash_conditions_on_delete
1302
- firm = companies(:odegy)
1303
- Client.create(:client_of => firm.id, :name => "BigShot Inc.")
1304
- Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
1305
- # only one of two clients is included in the association due to the :conditions key
1306
- assert_equal 2, Client.where(client_of: firm.id).size
1307
- assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
1308
- firm.destroy
1309
- # only the correctly associated client should have been deleted
1310
- assert_equal 1, Client.where(client_of: firm.id).size
1311
- end
1312
-
1313
- def test_delete_all_association_with_primary_key_deletes_correct_records
1314
- firm = Firm.first
1315
- # break the vanilla firm_id foreign key
1316
- assert_equal 3, firm.clients.count
1317
- firm.clients.first.update_columns(firm_id: nil)
1318
- assert_equal 2, firm.clients.reload.count
1319
- assert_equal 2, firm.clients_using_primary_key_with_delete_all.count
1320
- old_record = firm.clients_using_primary_key_with_delete_all.first
1321
- firm = Firm.first
1322
- firm.destroy
1323
- assert_nil Client.find_by_id(old_record.id)
1324
- end
1325
-
1326
- def test_creation_respects_hash_condition
1327
- ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build
1328
-
1329
- assert ms_client.save
1330
- assert_equal 'Microsoft', ms_client.name
1331
-
1332
- another_ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.create
1333
-
1334
- assert another_ms_client.persisted?
1335
- assert_equal 'Microsoft', another_ms_client.name
1336
- end
1337
-
1338
- def test_clearing_without_initial_access
1339
- firm = companies(:first_firm)
1340
-
1341
- firm.clients_of_firm.clear
1342
-
1343
- assert_equal 0, firm.clients_of_firm.size
1344
- assert_equal 0, firm.clients_of_firm.reload.size
1345
- end
1346
-
1347
- def test_deleting_a_item_which_is_not_in_the_collection
1348
- force_signal37_to_load_all_clients_of_firm
1349
- summit = Client.find_by_name('Summit')
1350
- companies(:first_firm).clients_of_firm.delete(summit)
1351
- assert_equal 2, companies(:first_firm).clients_of_firm.size
1352
- assert_equal 2, companies(:first_firm).clients_of_firm.reload.size
1353
- assert_equal 2, summit.client_of
1354
- end
1355
-
1356
- def test_deleting_by_integer_id
1357
- david = Developer.find(1)
1358
-
1359
- assert_difference 'david.projects.count', -1 do
1360
- assert_equal 1, david.projects.delete(1).size
1361
- end
1362
-
1363
- assert_equal 1, david.projects.size
1364
- end
1365
-
1366
- def test_deleting_by_string_id
1367
- david = Developer.find(1)
1368
-
1369
- assert_difference 'david.projects.count', -1 do
1370
- assert_equal 1, david.projects.delete('1').size
1371
- end
1372
-
1373
- assert_equal 1, david.projects.size
1374
- end
1375
-
1376
- def test_deleting_self_type_mismatch
1377
- david = Developer.find(1)
1378
- david.projects.reload
1379
- assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
1380
- end
1381
-
1382
- def test_destroying
1383
- force_signal37_to_load_all_clients_of_firm
1384
-
1385
- assert_difference "Client.count", -1 do
1386
- companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first)
1387
- end
1388
-
1389
- assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
1390
- assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
1391
- end
1392
-
1393
- def test_destroying_by_integer_id
1394
- force_signal37_to_load_all_clients_of_firm
1395
-
1396
- assert_difference "Client.count", -1 do
1397
- companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id)
1398
- end
1399
-
1400
- assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
1401
- assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
1402
- end
1403
-
1404
- def test_destroying_by_string_id
1405
- force_signal37_to_load_all_clients_of_firm
1406
-
1407
- assert_difference "Client.count", -1 do
1408
- companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id.to_s)
1409
- end
1410
-
1411
- assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
1412
- assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
1413
- end
1414
-
1415
- def test_destroying_a_collection
1416
- force_signal37_to_load_all_clients_of_firm
1417
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
1418
- assert_equal 3, companies(:first_firm).clients_of_firm.size
1419
-
1420
- assert_difference "Client.count", -2 do
1421
- companies(:first_firm).clients_of_firm.destroy([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
1422
- end
1423
-
1424
- assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
1425
- assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
1426
- end
1427
-
1428
- def test_destroy_all
1429
- force_signal37_to_load_all_clients_of_firm
1430
- clients = companies(:first_firm).clients_of_firm.to_a
1431
- assert !clients.empty?, "37signals has clients after load"
1432
- destroyed = companies(:first_firm).clients_of_firm.destroy_all
1433
- assert_equal clients.sort_by(&:id), destroyed.sort_by(&:id)
1434
- assert destroyed.all?(&:frozen?), "destroyed clients should be frozen"
1435
- assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all"
1436
- assert companies(:first_firm).clients_of_firm.reload.empty?, "37signals has no clients after destroy all and refresh"
1437
- end
1438
-
1439
- def test_dependence
1440
- firm = companies(:first_firm)
1441
- assert_equal 3, firm.clients.size
1442
- firm.destroy
1443
- assert Client.all.merge!(:where => "firm_id=#{firm.id}").to_a.empty?
1444
- end
1445
-
1446
- def test_dependence_for_associations_with_hash_condition
1447
- david = authors(:david)
1448
- assert_difference('Post.count', -1) { assert david.destroy }
1449
- end
1450
-
1451
- def test_destroy_dependent_when_deleted_from_association
1452
- # sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
1453
- firm = Firm.all.merge!(:order => "id").first
1454
- assert_equal 3, firm.clients.size
1455
-
1456
- client = firm.clients.first
1457
- firm.clients.delete(client)
1458
-
1459
- assert_raise(ActiveRecord::RecordNotFound) { Client.find(client.id) }
1460
- assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(client.id) }
1461
- assert_equal 2, firm.clients.size
1462
- end
1463
-
1464
- def test_three_levels_of_dependence
1465
- topic = Topic.create "title" => "neat and simple"
1466
- reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it"
1467
- reply.replies.create "title" => "neat and simple", "content" => "ain't complaining"
1468
-
1469
- assert_nothing_raised { topic.destroy }
1470
- end
1471
-
1472
- def test_dependence_with_transaction_support_on_failure
1473
- firm = companies(:first_firm)
1474
- clients = firm.clients
1475
- assert_equal 3, clients.length
1476
- clients.last.instance_eval { def overwrite_to_raise() raise "Trigger rollback" end }
1477
-
1478
- firm.destroy rescue "do nothing"
1479
-
1480
- assert_equal 3, Client.all.merge!(:where => "firm_id=#{firm.id}").to_a.size
1481
- end
1482
-
1483
- def test_dependence_on_account
1484
- num_accounts = Account.count
1485
- companies(:first_firm).destroy
1486
- assert_equal num_accounts - 1, Account.count
1487
- end
1488
-
1489
- def test_depends_and_nullify
1490
- num_accounts = Account.count
1491
-
1492
- core = companies(:rails_core)
1493
- assert_equal accounts(:rails_core_account), core.account
1494
- assert_equal companies(:leetsoft, :jadedpixel), core.companies
1495
- core.destroy
1496
- assert_nil accounts(:rails_core_account).reload.firm_id
1497
- assert_nil companies(:leetsoft).reload.client_of
1498
- assert_nil companies(:jadedpixel).reload.client_of
1499
-
1500
- assert_equal num_accounts, Account.count
1501
- end
1502
-
1503
- def test_restrict_with_exception
1504
- firm = RestrictedWithExceptionFirm.create!(:name => 'restrict')
1505
- firm.companies.create(:name => 'child')
1506
-
1507
- assert !firm.companies.empty?
1508
- assert_raise(ActiveRecord::DeleteRestrictionError) { firm.destroy }
1509
- assert RestrictedWithExceptionFirm.exists?(:name => 'restrict')
1510
- assert firm.companies.exists?(:name => 'child')
1511
- end
1512
-
1513
- def test_restrict_with_error_is_deprecated_using_key_many
1514
- I18n.backend = I18n::Backend::Simple.new
1515
- I18n.backend.store_translations :en, activerecord: { errors: { messages: { restrict_dependent_destroy: { many: 'message for deprecated key' } } } }
1516
-
1517
- firm = RestrictedWithErrorFirm.create!(name: 'restrict')
1518
- firm.companies.create(name: 'child')
1519
-
1520
- assert !firm.companies.empty?
1521
-
1522
- assert_deprecated { firm.destroy }
1523
-
1524
- assert !firm.errors.empty?
1525
-
1526
- assert_equal 'message for deprecated key', firm.errors[:base].first
1527
- assert RestrictedWithErrorFirm.exists?(name: 'restrict')
1528
- assert firm.companies.exists?(name: 'child')
1529
- ensure
1530
- I18n.backend.reload!
1531
- end
1532
-
1533
- def test_restrict_with_error
1534
- firm = RestrictedWithErrorFirm.create!(:name => 'restrict')
1535
- firm.companies.create(:name => 'child')
1536
-
1537
- assert !firm.companies.empty?
1538
-
1539
- firm.destroy
1540
-
1541
- assert !firm.errors.empty?
1542
-
1543
- assert_equal "Cannot delete record because dependent companies exist", firm.errors[:base].first
1544
- assert RestrictedWithErrorFirm.exists?(:name => 'restrict')
1545
- assert firm.companies.exists?(:name => 'child')
1546
- end
1547
-
1548
- def test_restrict_with_error_with_locale
1549
- I18n.backend = I18n::Backend::Simple.new
1550
- I18n.backend.store_translations 'en', activerecord: {attributes: {restricted_with_error_firm: {companies: 'client companies'}}}
1551
- firm = RestrictedWithErrorFirm.create!(name: 'restrict')
1552
- firm.companies.create(name: 'child')
1553
-
1554
- assert !firm.companies.empty?
1555
-
1556
- firm.destroy
1557
-
1558
- assert !firm.errors.empty?
1559
-
1560
- assert_equal "Cannot delete record because dependent client companies exist", firm.errors[:base].first
1561
- assert RestrictedWithErrorFirm.exists?(name: 'restrict')
1562
- assert firm.companies.exists?(name: 'child')
1563
- ensure
1564
- I18n.backend.reload!
1565
- end
1566
-
1567
- def test_included_in_collection
1568
- assert_equal true, companies(:first_firm).clients.include?(Client.find(2))
1569
- end
1570
-
1571
- def test_included_in_collection_for_new_records
1572
- client = Client.create(:name => 'Persisted')
1573
- assert_nil client.client_of
1574
- assert_equal false, Firm.new.clients_of_firm.include?(client),
1575
- 'includes a client that does not belong to any firm'
1576
- end
1577
-
1578
- def test_adding_array_and_collection
1579
- assert_nothing_raised { Firm.first.clients + Firm.all.last.clients }
1580
- end
1581
-
1582
- def test_replace_with_less
1583
- firm = Firm.all.merge!(:order => "id").first
1584
- firm.clients = [companies(:first_client)]
1585
- assert firm.save, "Could not save firm"
1586
- firm.reload
1587
- assert_equal 1, firm.clients.length
1588
- end
1589
-
1590
- def test_replace_with_less_and_dependent_nullify
1591
- num_companies = Company.count
1592
- companies(:rails_core).companies = []
1593
- assert_equal num_companies, Company.count
1594
- end
1595
-
1596
- def test_replace_with_new
1597
- firm = Firm.all.merge!(:order => "id").first
1598
- firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
1599
- firm.save
1600
- firm.reload
1601
- assert_equal 2, firm.clients.length
1602
- assert_equal false, firm.clients.include?(:first_client)
1603
- end
1604
-
1605
- def test_replace_failure
1606
- firm = companies(:first_firm)
1607
- account = Account.new
1608
- orig_accounts = firm.accounts.to_a
1609
-
1610
- assert !account.valid?
1611
- assert !orig_accounts.empty?
1612
- error = assert_raise ActiveRecord::RecordNotSaved do
1613
- firm.accounts = [account]
1614
- end
1615
-
1616
- assert_equal orig_accounts, firm.accounts
1617
- assert_equal "Failed to replace accounts because one or more of the " \
1618
- "new records could not be saved.", error.message
1619
- end
1620
-
1621
- def test_replace_with_same_content
1622
- firm = Firm.first
1623
- firm.clients = []
1624
- firm.save
1625
-
1626
- assert_queries(0, ignore_none: true) do
1627
- firm.clients = []
1628
- end
1629
-
1630
- assert_equal [], firm.send('clients=', [])
1631
- end
1632
-
1633
- def test_transactions_when_replacing_on_persisted
1634
- good = Client.new(:name => "Good")
1635
- bad = Client.new(:name => "Bad", :raise_on_save => true)
1636
-
1637
- companies(:first_firm).clients_of_firm = [good]
1638
-
1639
- begin
1640
- companies(:first_firm).clients_of_firm = [bad]
1641
- rescue Client::RaisedOnSave
1642
- end
1643
-
1644
- assert_equal [good], companies(:first_firm).clients_of_firm.reload
1645
- end
1646
-
1647
- def test_transactions_when_replacing_on_new_record
1648
- assert_no_queries(ignore_none: false) do
1649
- firm = Firm.new
1650
- firm.clients_of_firm = [Client.new("name" => "New Client")]
1651
- end
1652
- end
1653
-
1654
- def test_get_ids
1655
- assert_equal [companies(:first_client).id, companies(:second_client).id, companies(:another_first_firm_client).id], companies(:first_firm).client_ids
1656
- end
1657
-
1658
- def test_get_ids_for_loaded_associations
1659
- company = companies(:first_firm)
1660
- company.clients.reload
1661
- assert_queries(0) do
1662
- company.client_ids
1663
- company.client_ids
1664
- end
1665
- end
1666
-
1667
- def test_get_ids_for_unloaded_associations_does_not_load_them
1668
- company = companies(:first_firm)
1669
- assert !company.clients.loaded?
1670
- assert_equal [companies(:first_client).id, companies(:second_client).id, companies(:another_first_firm_client).id], company.client_ids
1671
- assert !company.clients.loaded?
1672
- end
1673
-
1674
- def test_get_ids_ignores_include_option
1675
- assert_equal [readers(:michael_welcome).id], posts(:welcome).readers_with_person_ids
1676
- end
1677
-
1678
- def test_get_ids_for_ordered_association
1679
- assert_equal [companies(:another_first_firm_client).id, companies(:second_client).id, companies(:first_client).id], companies(:first_firm).clients_ordered_by_name_ids
1680
- end
1681
-
1682
- def test_get_ids_for_association_on_new_record_does_not_try_to_find_records
1683
- Company.columns # Load schema information so we don't query below
1684
- Contract.columns # if running just this test.
1685
-
1686
- company = Company.new
1687
- assert_queries(0) do
1688
- company.contract_ids
1689
- end
1690
-
1691
- assert_equal [], company.contract_ids
1692
- end
1693
-
1694
- def test_set_ids_for_association_on_new_record_applies_association_correctly
1695
- contract_a = Contract.create!
1696
- contract_b = Contract.create!
1697
- Contract.create! # another contract
1698
- company = Company.new(:name => "Some Company")
1699
-
1700
- company.contract_ids = [contract_a.id, contract_b.id]
1701
- assert_equal [contract_a.id, contract_b.id], company.contract_ids
1702
- assert_equal [contract_a, contract_b], company.contracts
1703
-
1704
- company.save!
1705
- assert_equal company, contract_a.reload.company
1706
- assert_equal company, contract_b.reload.company
1707
- end
1708
-
1709
- def test_assign_ids_ignoring_blanks
1710
- firm = Firm.create!(:name => 'Apple')
1711
- firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
1712
- firm.save!
1713
-
1714
- assert_equal 2, firm.clients.reload.size
1715
- assert_equal true, firm.clients.include?(companies(:second_client))
1716
- end
1717
-
1718
- def test_get_ids_for_through
1719
- assert_equal [comments(:eager_other_comment1).id], authors(:mary).comment_ids
1720
- end
1721
-
1722
- def test_modifying_a_through_a_has_many_should_raise
1723
- [
1724
- lambda { authors(:mary).comment_ids = [comments(:greetings).id, comments(:more_greetings).id] },
1725
- lambda { authors(:mary).comments = [comments(:greetings), comments(:more_greetings)] },
1726
- lambda { authors(:mary).comments << Comment.create!(:body => "Yay", :post_id => 424242) },
1727
- lambda { authors(:mary).comments.delete(authors(:mary).comments.first) },
1728
- ].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }
1729
- end
1730
-
1731
- def test_dynamic_find_should_respect_association_order_for_through
1732
- assert_equal Comment.find(10), authors(:david).comments_desc.where("comments.type = 'SpecialComment'").first
1733
- assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type('SpecialComment')
1734
- end
1735
-
1736
- def test_has_many_through_respects_hash_conditions
1737
- assert_equal authors(:david).hello_posts, authors(:david).hello_posts_with_hash_conditions
1738
- assert_equal authors(:david).hello_post_comments, authors(:david).hello_post_comments_with_hash_conditions
1739
- end
1740
-
1741
- def test_include_uses_array_include_after_loaded
1742
- firm = companies(:first_firm)
1743
- firm.clients.load_target
1744
-
1745
- client = firm.clients.first
1746
-
1747
- assert_no_queries do
1748
- assert firm.clients.loaded?
1749
- assert_equal true, firm.clients.include?(client)
1750
- end
1751
- end
1752
-
1753
- def test_include_checks_if_record_exists_if_target_not_loaded
1754
- firm = companies(:first_firm)
1755
- client = firm.clients.first
1756
-
1757
- firm.reload
1758
- assert ! firm.clients.loaded?
1759
- assert_queries(1) do
1760
- assert_equal true, firm.clients.include?(client)
1761
- end
1762
- assert ! firm.clients.loaded?
1763
- end
1764
-
1765
- def test_include_returns_false_for_non_matching_record_to_verify_scoping
1766
- firm = companies(:first_firm)
1767
- client = Client.create!(:name => 'Not Associated')
1768
-
1769
- assert ! firm.clients.loaded?
1770
- assert_equal false, firm.clients.include?(client)
1771
- end
1772
-
1773
- def test_calling_first_nth_or_last_on_association_should_not_load_association
1774
- firm = companies(:first_firm)
1775
- firm.clients.first
1776
- firm.clients.second
1777
- firm.clients.last
1778
- assert !firm.clients.loaded?
1779
- end
1780
-
1781
- def test_calling_first_or_last_on_loaded_association_should_not_fetch_with_query
1782
- firm = companies(:first_firm)
1783
- firm.clients.load_target
1784
- assert firm.clients.loaded?
1785
-
1786
- assert_no_queries(ignore_none: false) do
1787
- firm.clients.first
1788
- assert_equal 2, firm.clients.first(2).size
1789
- firm.clients.last
1790
- assert_equal 2, firm.clients.last(2).size
1791
- end
1792
- end
1793
-
1794
- def test_calling_first_or_last_on_existing_record_with_build_should_load_association
1795
- firm = companies(:first_firm)
1796
- firm.clients.build(:name => 'Foo')
1797
- assert !firm.clients.loaded?
1798
-
1799
- assert_queries 1 do
1800
- firm.clients.first
1801
- firm.clients.second
1802
- firm.clients.last
1803
- end
1804
-
1805
- assert firm.clients.loaded?
1806
- end
1807
-
1808
- def test_calling_first_nth_or_last_on_existing_record_with_create_should_not_load_association
1809
- firm = companies(:first_firm)
1810
- firm.clients.create(:name => 'Foo')
1811
- assert !firm.clients.loaded?
1812
-
1813
- assert_queries 3 do
1814
- firm.clients.first
1815
- firm.clients.second
1816
- firm.clients.last
1817
- end
1818
-
1819
- assert !firm.clients.loaded?
1820
- end
1821
-
1822
- def test_calling_first_nth_or_last_on_new_record_should_not_run_queries
1823
- firm = Firm.new
1824
-
1825
- assert_no_queries do
1826
- firm.clients.first
1827
- firm.clients.second
1828
- firm.clients.last
1829
- end
1830
- end
1831
-
1832
- def test_calling_first_or_last_with_integer_on_association_should_not_load_association
1833
- firm = companies(:first_firm)
1834
- firm.clients.create(:name => 'Foo')
1835
- assert !firm.clients.loaded?
1836
-
1837
- assert_queries 2 do
1838
- firm.clients.first(2)
1839
- firm.clients.last(2)
1840
- end
1841
-
1842
- assert !firm.clients.loaded?
1843
- end
1844
-
1845
- def test_calling_many_should_count_instead_of_loading_association
1846
- firm = companies(:first_firm)
1847
- assert_queries(1) do
1848
- firm.clients.many? # use count query
1849
- end
1850
- assert !firm.clients.loaded?
1851
- end
1852
-
1853
- def test_calling_many_on_loaded_association_should_not_use_query
1854
- firm = companies(:first_firm)
1855
- firm.clients.collect # force load
1856
- assert_no_queries { assert firm.clients.many? }
1857
- end
1858
-
1859
- def test_calling_many_should_defer_to_collection_if_using_a_block
1860
- firm = companies(:first_firm)
1861
- assert_queries(1) do
1862
- firm.clients.expects(:size).never
1863
- firm.clients.many? { true }
1864
- end
1865
- assert firm.clients.loaded?
1866
- end
1867
-
1868
- def test_calling_many_should_return_false_if_none_or_one
1869
- firm = companies(:another_firm)
1870
- assert !firm.clients_like_ms.many?
1871
- assert_equal 0, firm.clients_like_ms.size
1872
-
1873
- firm = companies(:first_firm)
1874
- assert !firm.limited_clients.many?
1875
- assert_equal 1, firm.limited_clients.size
1876
- end
1877
-
1878
- def test_calling_many_should_return_true_if_more_than_one
1879
- firm = companies(:first_firm)
1880
- assert firm.clients.many?
1881
- assert_equal 3, firm.clients.size
1882
- end
1883
-
1884
- def test_calling_none_should_count_instead_of_loading_association
1885
- firm = companies(:first_firm)
1886
- assert_queries(1) do
1887
- firm.clients.none? # use count query
1888
- end
1889
- assert !firm.clients.loaded?
1890
- end
1891
-
1892
- def test_calling_none_on_loaded_association_should_not_use_query
1893
- firm = companies(:first_firm)
1894
- firm.clients.collect # force load
1895
- assert_no_queries { assert ! firm.clients.none? }
1896
- end
1897
-
1898
- def test_calling_none_should_defer_to_collection_if_using_a_block
1899
- firm = companies(:first_firm)
1900
- assert_queries(1) do
1901
- firm.clients.expects(:size).never
1902
- firm.clients.none? { true }
1903
- end
1904
- assert firm.clients.loaded?
1905
- end
1906
-
1907
- def test_calling_none_should_return_true_if_none
1908
- firm = companies(:another_firm)
1909
- assert firm.clients_like_ms.none?
1910
- assert_equal 0, firm.clients_like_ms.size
1911
- end
1912
-
1913
- def test_calling_none_should_return_false_if_any
1914
- firm = companies(:first_firm)
1915
- assert !firm.limited_clients.none?
1916
- assert_equal 1, firm.limited_clients.size
1917
- end
1918
-
1919
- def test_calling_one_should_count_instead_of_loading_association
1920
- firm = companies(:first_firm)
1921
- assert_queries(1) do
1922
- firm.clients.one? # use count query
1923
- end
1924
- assert !firm.clients.loaded?
1925
- end
1926
-
1927
- def test_calling_one_on_loaded_association_should_not_use_query
1928
- firm = companies(:first_firm)
1929
- firm.clients.collect # force load
1930
- assert_no_queries { assert ! firm.clients.one? }
1931
- end
1932
-
1933
- def test_calling_one_should_defer_to_collection_if_using_a_block
1934
- firm = companies(:first_firm)
1935
- assert_queries(1) do
1936
- firm.clients.expects(:size).never
1937
- firm.clients.one? { true }
1938
- end
1939
- assert firm.clients.loaded?
1940
- end
1941
-
1942
- def test_calling_one_should_return_false_if_zero
1943
- firm = companies(:another_firm)
1944
- assert ! firm.clients_like_ms.one?
1945
- assert_equal 0, firm.clients_like_ms.size
1946
- end
1947
-
1948
- def test_calling_one_should_return_true_if_one
1949
- firm = companies(:first_firm)
1950
- assert firm.limited_clients.one?
1951
- assert_equal 1, firm.limited_clients.size
1952
- end
1953
-
1954
- def test_calling_one_should_return_false_if_more_than_one
1955
- firm = companies(:first_firm)
1956
- assert ! firm.clients.one?
1957
- assert_equal 3, firm.clients.size
1958
- end
1959
-
1960
- def test_joins_with_namespaced_model_should_use_correct_type
1961
- old = ActiveRecord::Base.store_full_sti_class
1962
- ActiveRecord::Base.store_full_sti_class = true
1963
-
1964
- firm = Namespaced::Firm.create({ :name => 'Some Company' })
1965
- firm.clients.create({ :name => 'Some Client' })
1966
-
1967
- stats = Namespaced::Firm.all.merge!(
1968
- :select => "#{Namespaced::Firm.table_name}.id, COUNT(#{Namespaced::Client.table_name}.id) AS num_clients",
1969
- :joins => :clients,
1970
- :group => "#{Namespaced::Firm.table_name}.id"
1971
- ).find firm.id
1972
- assert_equal 1, stats.num_clients.to_i
1973
- ensure
1974
- ActiveRecord::Base.store_full_sti_class = old
1975
- end
1976
-
1977
- def test_association_proxy_transaction_method_starts_transaction_in_association_class
1978
- Comment.expects(:transaction)
1979
- Post.first.comments.transaction do
1980
- # nothing
1981
- end
1982
- end
1983
-
1984
- def test_sending_new_to_association_proxy_should_have_same_effect_as_calling_new
1985
- client_association = companies(:first_firm).clients
1986
- assert_equal client_association.new.attributes, client_association.send(:new).attributes
1987
- end
1988
-
1989
- def test_respond_to_private_class_methods
1990
- client_association = companies(:first_firm).clients
1991
- assert !client_association.respond_to?(:private_method)
1992
- assert client_association.respond_to?(:private_method, true)
1993
- end
1994
-
1995
- def test_creating_using_primary_key
1996
- firm = Firm.all.merge!(:order => "id").first
1997
- client = firm.clients_using_primary_key.create!(:name => 'test')
1998
- assert_equal firm.name, client.firm_name
1999
- end
2000
-
2001
- def test_defining_has_many_association_with_delete_all_dependency_lazily_evaluates_target_class
2002
- ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
2003
- class_eval(<<-EOF, __FILE__, __LINE__ + 1)
2004
- class DeleteAllModel < ActiveRecord::Base
2005
- has_many :nonentities, :dependent => :delete_all
2006
- end
2007
- EOF
2008
- end
2009
-
2010
- def test_defining_has_many_association_with_nullify_dependency_lazily_evaluates_target_class
2011
- ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
2012
- class_eval(<<-EOF, __FILE__, __LINE__ + 1)
2013
- class NullifyModel < ActiveRecord::Base
2014
- has_many :nonentities, :dependent => :nullify
2015
- end
2016
- EOF
2017
- end
2018
-
2019
- def test_attributes_are_being_set_when_initialized_from_has_many_association_with_where_clause
2020
- new_comment = posts(:welcome).comments.where(:body => "Some content").build
2021
- assert_equal new_comment.body, "Some content"
2022
- end
2023
-
2024
- def test_attributes_are_being_set_when_initialized_from_has_many_association_with_multiple_where_clauses
2025
- new_comment = posts(:welcome).comments.where(:body => "Some content").where(:type => 'SpecialComment').build
2026
- assert_equal new_comment.body, "Some content"
2027
- assert_equal new_comment.type, "SpecialComment"
2028
- assert_equal new_comment.post_id, posts(:welcome).id
2029
- end
2030
-
2031
- def test_include_method_in_has_many_association_should_return_true_for_instance_added_with_build
2032
- post = Post.new
2033
- comment = post.comments.build
2034
- assert_equal true, post.comments.include?(comment)
2035
- end
2036
-
2037
- def test_load_target_respects_protected_attributes
2038
- topic = Topic.create!
2039
- reply = topic.replies.create(:title => "reply 1")
2040
- reply.approved = false
2041
- reply.save!
2042
-
2043
- # Save with a different object instance, so the instance that's still held
2044
- # in topic.relies doesn't know about the changed attribute.
2045
- reply2 = Reply.find(reply.id)
2046
- reply2.approved = true
2047
- reply2.save!
2048
-
2049
- # Force loading the collection from the db. This will merge the existing
2050
- # object (reply) with what gets loaded from the db (which includes the
2051
- # changed approved attribute). approved is a protected attribute, so if mass
2052
- # assignment is used, it won't get updated and will still be false.
2053
- first = topic.replies.to_a.first
2054
- assert_equal reply.id, first.id
2055
- assert_equal true, first.approved?
2056
- end
2057
-
2058
- def test_to_a_should_dup_target
2059
- ary = topics(:first).replies.to_a
2060
- target = topics(:first).replies.target
2061
-
2062
- assert_not_equal target.object_id, ary.object_id
2063
- end
2064
-
2065
- def test_merging_with_custom_attribute_writer
2066
- bulb = Bulb.new(:color => "red")
2067
- assert_equal "RED!", bulb.color
2068
-
2069
- car = Car.create!
2070
- car.bulbs << bulb
2071
-
2072
- assert_equal "RED!", car.bulbs.to_a.first.color
2073
- end
2074
-
2075
- def test_abstract_class_with_polymorphic_has_many
2076
- post = SubStiPost.create! :title => "fooo", :body => "baa"
2077
- tagging = Tagging.create! :taggable => post
2078
- assert_equal [tagging], post.taggings
2079
- end
2080
-
2081
- def test_with_polymorphic_has_many_with_custom_columns_name
2082
- post = Post.create! :title => 'foo', :body => 'bar'
2083
- image = Image.create!
2084
-
2085
- post.images << image
2086
-
2087
- assert_equal [image], post.images
2088
- end
2089
-
2090
- def test_build_with_polymorphic_has_many_does_not_allow_to_override_type_and_id
2091
- welcome = posts(:welcome)
2092
- tagging = welcome.taggings.build(:taggable_id => 99, :taggable_type => 'ShouldNotChange')
2093
-
2094
- assert_equal welcome.id, tagging.taggable_id
2095
- assert_equal 'Post', tagging.taggable_type
2096
- end
2097
-
2098
- def test_dont_call_save_callbacks_twice_on_has_many
2099
- firm = companies(:first_firm)
2100
- contract = firm.contracts.create!
2101
-
2102
- assert_equal 1, contract.hi_count
2103
- assert_equal 1, contract.bye_count
2104
- end
2105
-
2106
- def test_association_attributes_are_available_to_after_initialize
2107
- car = Car.create(:name => 'honda')
2108
- bulb = car.bulbs.build
2109
-
2110
- assert_equal car.id, bulb.attributes_after_initialize['car_id']
2111
- end
2112
-
2113
- def test_attributes_are_set_when_initialized_from_has_many_null_relationship
2114
- car = Car.new name: 'honda'
2115
- bulb = car.bulbs.where(name: 'headlight').first_or_initialize
2116
- assert_equal 'headlight', bulb.name
2117
- end
2118
-
2119
- def test_attributes_are_set_when_initialized_from_polymorphic_has_many_null_relationship
2120
- post = Post.new title: 'title', body: 'bar'
2121
- tag = Tag.create!(name: 'foo')
2122
-
2123
- tagging = post.taggings.where(tag: tag).first_or_initialize
2124
-
2125
- assert_equal tag.id, tagging.tag_id
2126
- assert_equal 'Post', tagging.taggable_type
2127
- end
2128
-
2129
- def test_replace
2130
- car = Car.create(:name => 'honda')
2131
- bulb1 = car.bulbs.create
2132
- bulb2 = Bulb.create
2133
-
2134
- assert_equal [bulb1], car.bulbs
2135
- car.bulbs.replace([bulb2])
2136
- assert_equal [bulb2], car.bulbs
2137
- assert_equal [bulb2], car.reload.bulbs
2138
- end
2139
-
2140
- def test_replace_returns_target
2141
- car = Car.create(:name => 'honda')
2142
- bulb1 = car.bulbs.create
2143
- bulb2 = car.bulbs.create
2144
- bulb3 = Bulb.create
2145
-
2146
- assert_equal [bulb1, bulb2], car.bulbs
2147
- result = car.bulbs.replace([bulb3, bulb1])
2148
- assert_equal [bulb1, bulb3], car.bulbs
2149
- assert_equal [bulb1, bulb3], result
2150
- end
2151
-
2152
- def test_collection_association_with_private_kernel_method
2153
- firm = companies(:first_firm)
2154
- assert_equal [accounts(:signals37)], firm.accounts.open
2155
- end
2156
-
2157
- test "first_or_initialize adds the record to the association" do
2158
- firm = Firm.create! name: 'omg'
2159
- client = firm.clients_of_firm.first_or_initialize
2160
- assert_equal [client], firm.clients_of_firm
2161
- end
2162
-
2163
- test "first_or_create adds the record to the association" do
2164
- firm = Firm.create! name: 'omg'
2165
- firm.clients_of_firm.load_target
2166
- client = firm.clients_of_firm.first_or_create name: 'lol'
2167
- assert_equal [client], firm.clients_of_firm
2168
- assert_equal [client], firm.reload.clients_of_firm
2169
- end
2170
-
2171
- test "delete_all, when not loaded, doesn't load the records" do
2172
- post = posts(:welcome)
2173
-
2174
- assert post.taggings_with_delete_all.count > 0
2175
- assert !post.taggings_with_delete_all.loaded?
2176
-
2177
- # 2 queries: one DELETE and another to update the counter cache
2178
- assert_queries(2) do
2179
- post.taggings_with_delete_all.delete_all
2180
- end
2181
- end
2182
-
2183
- test "has many associations on new records use null relations" do
2184
- post = Post.new
2185
-
2186
- assert_no_queries(ignore_none: false) do
2187
- assert_equal [], post.comments
2188
- assert_equal [], post.comments.where(body: 'omg')
2189
- assert_equal [], post.comments.pluck(:body)
2190
- assert_equal 0, post.comments.sum(:id)
2191
- assert_equal 0, post.comments.count
2192
- end
2193
- end
2194
-
2195
- test "collection proxy respects default scope" do
2196
- author = authors(:mary)
2197
- assert !author.first_posts.exists?
2198
- end
2199
-
2200
- test "association with extend option" do
2201
- post = posts(:welcome)
2202
- assert_equal "lifo", post.comments_with_extend.author
2203
- assert_equal "hello", post.comments_with_extend.greeting
2204
- end
2205
-
2206
- test "association with extend option with multiple extensions" do
2207
- post = posts(:welcome)
2208
- assert_equal "lifo", post.comments_with_extend_2.author
2209
- assert_equal "hullo", post.comments_with_extend_2.greeting
2210
- end
2211
-
2212
- test "extend option affects per association" do
2213
- post = posts(:welcome)
2214
- assert_equal "lifo", post.comments_with_extend.author
2215
- assert_equal "lifo", post.comments_with_extend_2.author
2216
- assert_equal "hello", post.comments_with_extend.greeting
2217
- assert_equal "hullo", post.comments_with_extend_2.greeting
2218
- end
2219
-
2220
- test "delete record with complex joins" do
2221
- david = authors(:david)
2222
-
2223
- post = david.posts.first
2224
- post.type = 'PostWithSpecialCategorization'
2225
- post.save
2226
-
2227
- categorization = post.categorizations.first
2228
- categorization.special = true
2229
- categorization.save
2230
-
2231
- assert_not_equal [], david.posts_with_special_categorizations
2232
- david.posts_with_special_categorizations = []
2233
- assert_equal [], david.posts_with_special_categorizations
2234
- end
2235
-
2236
- test "does not duplicate associations when used with natural primary keys" do
2237
- speedometer = Speedometer.create!(id: '4')
2238
- speedometer.minivans.create!(minivan_id: 'a-van-red' ,name: 'a van', color: 'red')
2239
-
2240
- assert_equal 1, speedometer.minivans.to_a.size, "Only one association should be present:\n#{speedometer.minivans.to_a}"
2241
- assert_equal 1, speedometer.reload.minivans.to_a.size
2242
- end
2243
-
2244
- test "can unscope the default scope of the associated model" do
2245
- car = Car.create!
2246
- bulb1 = Bulb.create! name: "defaulty", car: car
2247
- bulb2 = Bulb.create! name: "other", car: car
2248
-
2249
- assert_equal [bulb1], car.bulbs
2250
- assert_equal [bulb1, bulb2], car.all_bulbs.sort_by(&:id)
2251
- end
2252
-
2253
- test "can unscope and where the default scope of the associated model" do
2254
- Car.has_many :other_bulbs, -> { unscope(where: [:name]).where(name: 'other') }, class_name: "Bulb"
2255
- car = Car.create!
2256
- bulb1 = Bulb.create! name: "defaulty", car: car
2257
- bulb2 = Bulb.create! name: "other", car: car
2258
-
2259
- assert_equal [bulb1], car.bulbs
2260
- assert_equal [bulb2], car.other_bulbs
2261
- end
2262
-
2263
- test "can rewhere the default scope of the associated model" do
2264
- Car.has_many :old_bulbs, -> { rewhere(name: 'old') }, class_name: "Bulb"
2265
- car = Car.create!
2266
- bulb1 = Bulb.create! name: "defaulty", car: car
2267
- bulb2 = Bulb.create! name: "old", car: car
2268
-
2269
- assert_equal [bulb1], car.bulbs
2270
- assert_equal [bulb2], car.old_bulbs
2271
- end
2272
-
2273
- test 'unscopes the default scope of associated model when used with include' do
2274
- car = Car.create!
2275
- bulb = Bulb.create! name: "other", car: car
2276
-
2277
- assert_equal [bulb], Car.find(car.id).all_bulbs
2278
- assert_equal [bulb], Car.includes(:all_bulbs).find(car.id).all_bulbs
2279
- assert_equal [bulb], Car.eager_load(:all_bulbs).find(car.id).all_bulbs
2280
- end
2281
-
2282
- test "raises RecordNotDestroyed when replaced child can't be destroyed" do
2283
- car = Car.create!
2284
- original_child = FailedBulb.create!(car: car)
2285
-
2286
- error = assert_raise(ActiveRecord::RecordNotDestroyed) do
2287
- car.failed_bulbs = [FailedBulb.create!]
2288
- end
2289
-
2290
- assert_equal [original_child], car.reload.failed_bulbs
2291
- assert_equal "Failed to destroy the record", error.message
2292
- end
2293
-
2294
- test 'updates counter cache when default scope is given' do
2295
- topic = DefaultRejectedTopic.create approved: true
2296
-
2297
- assert_difference "topic.reload.replies_count", 1 do
2298
- topic.approved_replies.create!
2299
- end
2300
- end
2301
-
2302
- test 'dangerous association name raises ArgumentError' do
2303
- [:errors, 'errors', :save, 'save'].each do |name|
2304
- assert_raises(ArgumentError, "Association #{name} should not be allowed") do
2305
- Class.new(ActiveRecord::Base) do
2306
- has_many name
2307
- end
2308
- end
2309
- end
2310
- end
2311
-
2312
- test 'passes custom context validation to validate children' do
2313
- pirate = FamousPirate.new
2314
- pirate.famous_ships << ship = FamousShip.new
2315
-
2316
- assert pirate.valid?
2317
- assert_not pirate.valid?(:conference)
2318
- assert_equal "can't be blank", ship.errors[:name].first
2319
- end
2320
-
2321
- test 'association with instance dependent scope' do
2322
- bob = authors(:bob)
2323
- Post.create!(title: "signed post by bob", body: "stuff", author: authors(:bob))
2324
- Post.create!(title: "anonymous post", body: "more stuff", author: authors(:bob))
2325
- assert_equal ["misc post by bob", "other post by bob",
2326
- "signed post by bob"], bob.posts_with_signature.map(&:title).sort
2327
-
2328
- assert_equal [], authors(:david).posts_with_signature.map(&:title)
2329
- end
2330
-
2331
- test 'associations autosaves when object is already persisted' do
2332
- bulb = Bulb.create!
2333
- tyre = Tyre.create!
2334
-
2335
- car = Car.create! do |c|
2336
- c.bulbs << bulb
2337
- c.tyres << tyre
2338
- end
2339
-
2340
- assert_equal 1, car.bulbs.count
2341
- assert_equal 1, car.tyres.count
2342
- end
2343
-
2344
- test 'associations replace in memory when records have the same id' do
2345
- bulb = Bulb.create!
2346
- car = Car.create!(bulbs: [bulb])
2347
-
2348
- new_bulb = Bulb.find(bulb.id)
2349
- new_bulb.name = "foo"
2350
- car.bulbs = [new_bulb]
2351
-
2352
- assert_equal "foo", car.bulbs.first.name
2353
- end
2354
-
2355
- test 'in memory replacement executes no queries' do
2356
- bulb = Bulb.create!
2357
- car = Car.create!(bulbs: [bulb])
2358
-
2359
- new_bulb = Bulb.find(bulb.id)
2360
-
2361
- assert_no_queries do
2362
- car.bulbs = [new_bulb]
2363
- end
2364
- end
2365
-
2366
- test 'in memory replacements do not execute callbacks' do
2367
- raise_after_add = false
2368
- klass = Class.new(ActiveRecord::Base) do
2369
- self.table_name = :cars
2370
- has_many :bulbs, after_add: proc { raise if raise_after_add }
2371
-
2372
- def self.name
2373
- "Car"
2374
- end
2375
- end
2376
- bulb = Bulb.create!
2377
- car = klass.create!(bulbs: [bulb])
2378
-
2379
- new_bulb = Bulb.find(bulb.id)
2380
- raise_after_add = true
2381
-
2382
- assert_nothing_raised do
2383
- car.bulbs = [new_bulb]
2384
- end
2385
- end
2386
-
2387
- test 'in memory replacements sets inverse instance' do
2388
- bulb = Bulb.create!
2389
- car = Car.create!(bulbs: [bulb])
2390
-
2391
- new_bulb = Bulb.find(bulb.id)
2392
- car.bulbs = [new_bulb]
2393
-
2394
- assert_same car, new_bulb.car
2395
- end
2396
-
2397
- test 'in memory replacement maintains order' do
2398
- first_bulb = Bulb.create!
2399
- second_bulb = Bulb.create!
2400
- car = Car.create!(bulbs: [first_bulb, second_bulb])
2401
-
2402
- same_bulb = Bulb.find(first_bulb.id)
2403
- car.bulbs = [second_bulb, same_bulb]
2404
-
2405
- assert_equal [first_bulb, second_bulb], car.bulbs
2406
- end
2407
-
2408
- test "prevent double insertion of new object when the parent association loaded in the after save callback" do
2409
- reset_callbacks(:save, Bulb) do
2410
- Bulb.after_save { |record| record.car.bulbs.load }
2411
-
2412
- car = Car.create!
2413
- car.bulbs << Bulb.new
2414
-
2415
- assert_equal 1, car.bulbs.size
2416
- end
2417
- end
2418
-
2419
- test "prevent double firing the before save callback of new object when the parent association saved in the callback" do
2420
- reset_callbacks(:save, Bulb) do
2421
- count = 0
2422
- Bulb.before_save { |record| record.car.save && count += 1 }
2423
-
2424
- car = Car.create!
2425
- car.bulbs.create!
2426
-
2427
- assert_equal 1, count
2428
- end
2429
- end
2430
-
2431
- def test_association_force_reload_with_only_true_is_deprecated
2432
- company = Company.find(1)
2433
-
2434
- assert_deprecated { company.clients_of_firm(true) }
2435
- end
2436
-
2437
- class AuthorWithErrorDestroyingAssociation < ActiveRecord::Base
2438
- self.table_name = "authors"
2439
- has_many :posts_with_error_destroying,
2440
- class_name: "PostWithErrorDestroying",
2441
- foreign_key: :author_id,
2442
- dependent: :destroy
2443
- end
2444
-
2445
- class PostWithErrorDestroying < ActiveRecord::Base
2446
- self.table_name = "posts"
2447
- self.inheritance_column = nil
2448
- before_destroy -> { throw :abort }
2449
- end
2450
-
2451
- def test_destroy_does_not_raise_when_association_errors_on_destroy
2452
- assert_no_difference "AuthorWithErrorDestroyingAssociation.count" do
2453
- author = AuthorWithErrorDestroyingAssociation.first
2454
-
2455
- assert_not author.destroy
2456
- end
2457
- end
2458
-
2459
- def test_destroy_with_bang_bubbles_errors_from_associations
2460
- error = assert_raises ActiveRecord::RecordNotDestroyed do
2461
- AuthorWithErrorDestroyingAssociation.first.destroy!
2462
- end
2463
-
2464
- assert_instance_of PostWithErrorDestroying, error.record
2465
- end
2466
-
2467
- def test_ids_reader_memoization
2468
- car = Car.create!(name: 'Tofaş')
2469
- bulb = Bulb.create!(car: car)
2470
-
2471
- assert_equal [bulb.id], car.bulb_ids
2472
- assert_no_queries { car.bulb_ids }
2473
- end
2474
-
2475
- def test_loading_association_in_validate_callback_doesnt_affect_persistence
2476
- reset_callbacks(:validation, Bulb) do
2477
- Bulb.after_validation { |record| record.car.bulbs.load }
2478
-
2479
- car = Car.create!(name: "Car")
2480
- bulb = car.bulbs.create!
2481
-
2482
- assert_equal [bulb], car.bulbs
2483
- end
2484
- end
2485
-
2486
- private
2487
-
2488
- def reset_callbacks(kind, klass)
2489
- old_callbacks = {}
2490
- old_callbacks[klass] = klass.send("_#{kind}_callbacks").dup
2491
- klass.subclasses.each do |subclass|
2492
- old_callbacks[subclass] = subclass.send("_#{kind}_callbacks").dup
2493
- end
2494
- yield
2495
- ensure
2496
- klass.send("_#{kind}_callbacks=", old_callbacks[klass])
2497
- klass.subclasses.each do |subclass|
2498
- subclass.send("_#{kind}_callbacks=", old_callbacks[subclass])
2499
- end
2500
- end
2501
- end
1
+ require "cases/helper"
2
+ require 'models/developer'
3
+ require 'models/computer'
4
+ require 'models/project'
5
+ require 'models/company'
6
+ require 'models/contract'
7
+ require 'models/topic'
8
+ require 'models/reply'
9
+ require 'models/category'
10
+ require 'models/image'
11
+ require 'models/post'
12
+ require 'models/author'
13
+ require 'models/essay'
14
+ require 'models/comment'
15
+ require 'models/person'
16
+ require 'models/reader'
17
+ require 'models/tagging'
18
+ require 'models/tag'
19
+ require 'models/invoice'
20
+ require 'models/line_item'
21
+ require 'models/car'
22
+ require 'models/bulb'
23
+ require 'models/engine'
24
+ require 'models/categorization'
25
+ require 'models/minivan'
26
+ require 'models/speedometer'
27
+ require 'models/reference'
28
+ require 'models/job'
29
+ require 'models/college'
30
+ require 'models/student'
31
+ require 'models/pirate'
32
+ require 'models/ship'
33
+ require 'models/ship_part'
34
+ require 'models/treasure'
35
+ require 'models/parrot'
36
+ require 'models/tyre'
37
+ require 'models/subscriber'
38
+ require 'models/subscription'
39
+ require 'models/zine'
40
+ require 'models/interest'
41
+
42
+ class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
43
+ fixtures :authors, :posts, :comments
44
+
45
+ def test_should_generate_valid_sql
46
+ author = authors(:david)
47
+ # this can fail on adapters which require ORDER BY expressions to be included in the SELECT expression
48
+ # if the reorder clauses are not correctly handled
49
+ assert author.posts_with_comments_sorted_by_comment_id.where('comments.id > 0').reorder('posts.comments_count DESC', 'posts.tags_count DESC').last
50
+ end
51
+ end
52
+
53
+ class HasManyAssociationsTestPrimaryKeys < ActiveRecord::TestCase
54
+ fixtures :authors, :essays, :subscribers, :subscriptions, :people
55
+
56
+ def test_custom_primary_key_on_new_record_should_fetch_with_query
57
+ subscriber = Subscriber.new(nick: 'webster132')
58
+ assert !subscriber.subscriptions.loaded?
59
+
60
+ assert_queries 1 do
61
+ assert_equal 2, subscriber.subscriptions.size
62
+ end
63
+
64
+ assert_equal Subscription.where(subscriber_id: "webster132"), subscriber.subscriptions
65
+ end
66
+
67
+ def test_association_primary_key_on_new_record_should_fetch_with_query
68
+ author = Author.new(:name => "David")
69
+ assert !author.essays.loaded?
70
+
71
+ assert_queries 1 do
72
+ assert_equal 1, author.essays.size
73
+ end
74
+
75
+ assert_equal Essay.where(writer_id: "David"), author.essays
76
+ end
77
+
78
+ def test_has_many_custom_primary_key
79
+ david = authors(:david)
80
+ assert_equal Essay.where(writer_id: "David"), david.essays
81
+ end
82
+
83
+ def test_ids_on_unloaded_association_with_custom_primary_key
84
+ david = people(:david)
85
+ assert_equal Essay.where(writer_id: "David").pluck(:id), david.essay_ids
86
+ end
87
+
88
+ def test_ids_on_loaded_association_with_custom_primary_key
89
+ david = people(:david)
90
+ david.essays.load
91
+ assert_equal Essay.where(writer_id: "David").pluck(:id), david.essay_ids
92
+ end
93
+
94
+ def test_has_many_assignment_with_custom_primary_key
95
+ david = people(:david)
96
+
97
+ assert_equal ["A Modest Proposal"], david.essays.map(&:name)
98
+ david.essays = [Essay.create!(name: "Remote Work" )]
99
+ assert_equal ["Remote Work"], david.essays.map(&:name)
100
+ end
101
+
102
+ def test_blank_custom_primary_key_on_new_record_should_not_run_queries
103
+ author = Author.new
104
+ assert !author.essays.loaded?
105
+
106
+ assert_queries 0 do
107
+ assert_equal 0, author.essays.size
108
+ end
109
+ end
110
+ end
111
+
112
+ class HasManyAssociationsTest < ActiveRecord::TestCase
113
+ fixtures :accounts, :categories, :companies, :developers, :projects,
114
+ :developers_projects, :topics, :authors, :comments,
115
+ :posts, :readers, :taggings, :cars, :jobs, :tags,
116
+ :categorizations, :zines, :interests
117
+
118
+ def setup
119
+ Client.destroyed_client_ids.clear
120
+ end
121
+
122
+ def test_sti_subselect_count
123
+ tag = Tag.first
124
+ len = Post.tagged_with(tag.id).limit(10).size
125
+ assert_operator len, :>, 0
126
+ end
127
+
128
+ def test_anonymous_has_many
129
+ developer = Class.new(ActiveRecord::Base) {
130
+ self.table_name = 'developers'
131
+ dev = self
132
+
133
+ developer_project = Class.new(ActiveRecord::Base) {
134
+ self.table_name = 'developers_projects'
135
+ belongs_to :developer, :anonymous_class => dev
136
+ }
137
+ has_many :developer_projects, :anonymous_class => developer_project, :foreign_key => 'developer_id'
138
+ }
139
+ dev = developer.first
140
+ named = Developer.find(dev.id)
141
+ assert_operator dev.developer_projects.count, :>, 0
142
+ assert_equal named.projects.map(&:id).sort,
143
+ dev.developer_projects.map(&:project_id).sort
144
+ end
145
+
146
+ def test_default_scope_on_relations_is_not_cached
147
+ counter = 0
148
+ posts = Class.new(ActiveRecord::Base) {
149
+ self.table_name = 'posts'
150
+ self.inheritance_column = 'not_there'
151
+ post = self
152
+
153
+ comments = Class.new(ActiveRecord::Base) {
154
+ self.table_name = 'comments'
155
+ self.inheritance_column = 'not_there'
156
+ belongs_to :post, :anonymous_class => post
157
+ default_scope -> {
158
+ counter += 1
159
+ where("id = :inc", :inc => counter)
160
+ }
161
+ }
162
+ has_many :comments, :anonymous_class => comments, :foreign_key => 'post_id'
163
+ }
164
+ assert_equal 0, counter
165
+ post = posts.first
166
+ assert_equal 0, counter
167
+ sql = capture_sql { post.comments.to_a }
168
+ post.comments.reset
169
+ assert_not_equal sql, capture_sql { post.comments.to_a }
170
+ end
171
+
172
+ def test_has_many_build_with_options
173
+ college = College.create(name: 'UFMT')
174
+ Student.create(active: true, college_id: college.id, name: 'Sarah')
175
+
176
+ assert_equal college.students, Student.where(active: true, college_id: college.id)
177
+ end
178
+
179
+ def test_add_record_to_collection_should_change_its_updated_at
180
+ ship = Ship.create(name: 'dauntless')
181
+ part = ShipPart.create(name: 'cockpit')
182
+ updated_at = part.updated_at
183
+
184
+ travel(1.second) do
185
+ ship.parts << part
186
+ end
187
+
188
+ assert_equal part.ship, ship
189
+ assert_not_equal part.updated_at, updated_at
190
+ end
191
+
192
+ def test_clear_collection_should_not_change_updated_at
193
+ # GH#17161: .clear calls delete_all (and returns the association),
194
+ # which is intended to not touch associated objects's updated_at field
195
+ ship = Ship.create(name: 'dauntless')
196
+ part = ShipPart.create(name: 'cockpit', ship_id: ship.id)
197
+
198
+ ship.parts.clear
199
+ part.reload
200
+
201
+ assert_equal nil, part.ship
202
+ assert !part.updated_at_changed?
203
+ end
204
+
205
+ def test_create_from_association_should_respect_default_scope
206
+ car = Car.create(:name => 'honda')
207
+ assert_equal 'honda', car.name
208
+
209
+ bulb = Bulb.create
210
+ assert_equal 'defaulty', bulb.name
211
+
212
+ bulb = car.bulbs.build
213
+ assert_equal 'defaulty', bulb.name
214
+
215
+ bulb = car.bulbs.create
216
+ assert_equal 'defaulty', bulb.name
217
+ end
218
+
219
+ def test_build_and_create_from_association_should_respect_passed_attributes_over_default_scope
220
+ car = Car.create(name: 'honda')
221
+
222
+ bulb = car.bulbs.build(name: 'exotic')
223
+ assert_equal 'exotic', bulb.name
224
+
225
+ bulb = car.bulbs.create(name: 'exotic')
226
+ assert_equal 'exotic', bulb.name
227
+
228
+ bulb = car.awesome_bulbs.build(frickinawesome: false)
229
+ assert_equal false, bulb.frickinawesome
230
+
231
+ bulb = car.awesome_bulbs.create(frickinawesome: false)
232
+ assert_equal false, bulb.frickinawesome
233
+ end
234
+
235
+ def test_build_from_association_should_respect_scope
236
+ author = Author.new
237
+
238
+ post = author.thinking_posts.build
239
+ assert_equal 'So I was thinking', post.title
240
+ end
241
+
242
+ def test_create_from_association_with_nil_values_should_work
243
+ car = Car.create(:name => 'honda')
244
+
245
+ bulb = car.bulbs.new(nil)
246
+ assert_equal 'defaulty', bulb.name
247
+
248
+ bulb = car.bulbs.build(nil)
249
+ assert_equal 'defaulty', bulb.name
250
+
251
+ bulb = car.bulbs.create(nil)
252
+ assert_equal 'defaulty', bulb.name
253
+ end
254
+
255
+ def test_do_not_call_callbacks_for_delete_all
256
+ car = Car.create(:name => 'honda')
257
+ car.funky_bulbs.create!
258
+ assert_nothing_raised { car.reload.funky_bulbs.delete_all }
259
+ assert_equal 0, Bulb.count, "bulbs should have been deleted using :delete_all strategy"
260
+ end
261
+
262
+ def test_delete_all_on_association_is_the_same_as_not_loaded
263
+ author = authors :david
264
+ author.thinking_posts.create!(:body => "test")
265
+ author.reload
266
+ expected_sql = capture_sql { author.thinking_posts.delete_all }
267
+
268
+ author.thinking_posts.create!(:body => "test")
269
+ author.reload
270
+ author.thinking_posts.inspect
271
+ loaded_sql = capture_sql { author.thinking_posts.delete_all }
272
+ assert_equal(expected_sql, loaded_sql)
273
+ end
274
+
275
+ def test_delete_all_on_association_with_nil_dependency_is_the_same_as_not_loaded
276
+ author = authors :david
277
+ author.posts.create!(:title => "test", :body => "body")
278
+ author.reload
279
+ expected_sql = capture_sql { author.posts.delete_all }
280
+
281
+ author.posts.create!(:title => "test", :body => "body")
282
+ author.reload
283
+ author.posts.to_a
284
+ loaded_sql = capture_sql { author.posts.delete_all }
285
+ assert_equal(expected_sql, loaded_sql)
286
+ end
287
+
288
+ def test_building_the_associated_object_with_implicit_sti_base_class
289
+ firm = DependentFirm.new
290
+ company = firm.companies.build
291
+ assert_kind_of Company, company, "Expected #{company.class} to be a Company"
292
+ end
293
+
294
+ def test_building_the_associated_object_with_explicit_sti_base_class
295
+ firm = DependentFirm.new
296
+ company = firm.companies.build(:type => "Company")
297
+ assert_kind_of Company, company, "Expected #{company.class} to be a Company"
298
+ end
299
+
300
+ def test_building_the_associated_object_with_sti_subclass
301
+ firm = DependentFirm.new
302
+ company = firm.companies.build(:type => "Client")
303
+ assert_kind_of Client, company, "Expected #{company.class} to be a Client"
304
+ end
305
+
306
+ def test_building_the_associated_object_with_an_invalid_type
307
+ firm = DependentFirm.new
308
+ assert_raise(ActiveRecord::SubclassNotFound) { firm.companies.build(:type => "Invalid") }
309
+ end
310
+
311
+ def test_building_the_associated_object_with_an_unrelated_type
312
+ firm = DependentFirm.new
313
+ assert_raise(ActiveRecord::SubclassNotFound) { firm.companies.build(:type => "Account") }
314
+ end
315
+
316
+ test "building the association with an array" do
317
+ speedometer = Speedometer.new(speedometer_id: "a")
318
+ data = [{name: "first"}, {name: "second"}]
319
+ speedometer.minivans.build(data)
320
+
321
+ assert_equal 2, speedometer.minivans.size
322
+ assert speedometer.save
323
+ assert_equal ["first", "second"], speedometer.reload.minivans.map(&:name)
324
+ end
325
+
326
+ def test_association_keys_bypass_attribute_protection
327
+ car = Car.create(:name => 'honda')
328
+
329
+ bulb = car.bulbs.new
330
+ assert_equal car.id, bulb.car_id
331
+
332
+ bulb = car.bulbs.new :car_id => car.id + 1
333
+ assert_equal car.id, bulb.car_id
334
+
335
+ bulb = car.bulbs.build
336
+ assert_equal car.id, bulb.car_id
337
+
338
+ bulb = car.bulbs.build :car_id => car.id + 1
339
+ assert_equal car.id, bulb.car_id
340
+
341
+ bulb = car.bulbs.create
342
+ assert_equal car.id, bulb.car_id
343
+
344
+ bulb = car.bulbs.create :car_id => car.id + 1
345
+ assert_equal car.id, bulb.car_id
346
+ end
347
+
348
+ def test_association_protect_foreign_key
349
+ invoice = Invoice.create
350
+
351
+ line_item = invoice.line_items.new
352
+ assert_equal invoice.id, line_item.invoice_id
353
+
354
+ line_item = invoice.line_items.new :invoice_id => invoice.id + 1
355
+ assert_equal invoice.id, line_item.invoice_id
356
+
357
+ line_item = invoice.line_items.build
358
+ assert_equal invoice.id, line_item.invoice_id
359
+
360
+ line_item = invoice.line_items.build :invoice_id => invoice.id + 1
361
+ assert_equal invoice.id, line_item.invoice_id
362
+
363
+ line_item = invoice.line_items.create
364
+ assert_equal invoice.id, line_item.invoice_id
365
+
366
+ line_item = invoice.line_items.create :invoice_id => invoice.id + 1
367
+ assert_equal invoice.id, line_item.invoice_id
368
+ end
369
+
370
+ # When creating objects on the association, we must not do it within a scope (even though it
371
+ # would be convenient), because this would cause that scope to be applied to any callbacks etc.
372
+ def test_build_and_create_should_not_happen_within_scope
373
+ car = cars(:honda)
374
+ scope = car.foo_bulbs.where_values_hash
375
+
376
+ bulb = car.foo_bulbs.build
377
+ assert_not_equal scope, bulb.scope_after_initialize.where_values_hash
378
+
379
+ bulb = car.foo_bulbs.create
380
+ assert_not_equal scope, bulb.scope_after_initialize.where_values_hash
381
+
382
+ bulb = car.foo_bulbs.create!
383
+ assert_not_equal scope, bulb.scope_after_initialize.where_values_hash
384
+ end
385
+
386
+ def test_no_sql_should_be_fired_if_association_already_loaded
387
+ Car.create(:name => 'honda')
388
+ bulbs = Car.first.bulbs
389
+ bulbs.to_a # to load all instances of bulbs
390
+
391
+ assert_no_queries do
392
+ bulbs.first()
393
+ bulbs.first({})
394
+ end
395
+
396
+ assert_no_queries do
397
+ bulbs.second()
398
+ bulbs.second({})
399
+ end
400
+
401
+ assert_no_queries do
402
+ bulbs.third()
403
+ bulbs.third({})
404
+ end
405
+
406
+ assert_no_queries do
407
+ bulbs.fourth()
408
+ bulbs.fourth({})
409
+ end
410
+
411
+ assert_no_queries do
412
+ bulbs.fifth()
413
+ bulbs.fifth({})
414
+ end
415
+
416
+ assert_no_queries do
417
+ bulbs.forty_two()
418
+ bulbs.forty_two({})
419
+ end
420
+
421
+ assert_no_queries do
422
+ bulbs.third_to_last()
423
+ bulbs.third_to_last({})
424
+ end
425
+
426
+ assert_no_queries do
427
+ bulbs.second_to_last()
428
+ bulbs.second_to_last({})
429
+ end
430
+
431
+ assert_no_queries do
432
+ bulbs.last()
433
+ bulbs.last({})
434
+ end
435
+ end
436
+
437
+ def test_create_resets_cached_counters
438
+ person = Person.create!(:first_name => 'tenderlove')
439
+ post = Post.first
440
+
441
+ assert_equal [], person.readers
442
+ assert_nil person.readers.find_by_post_id(post.id)
443
+
444
+ person.readers.create(:post_id => post.id)
445
+
446
+ assert_equal 1, person.readers.count
447
+ assert_equal 1, person.readers.length
448
+ assert_equal post, person.readers.first.post
449
+ assert_equal person, person.readers.first.person
450
+ end
451
+
452
+ def test_update_all_respects_association_scope
453
+ person = Person.new
454
+ person.first_name = 'Naruto'
455
+ person.references << Reference.new
456
+ person.id = 10
457
+ person.references
458
+ person.save!
459
+ assert_equal 1, person.references.update_all(favourite: true)
460
+ end
461
+
462
+ def test_exists_respects_association_scope
463
+ person = Person.new
464
+ person.first_name = 'Sasuke'
465
+ person.references << Reference.new
466
+ person.id = 10
467
+ person.references
468
+ person.save!
469
+ assert_predicate person.references, :exists?
470
+ end
471
+
472
+ def force_signal37_to_load_all_clients_of_firm
473
+ companies(:first_firm).clients_of_firm.each {|f| }
474
+ end
475
+
476
+ # sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
477
+ def test_counting_with_counter_sql
478
+ assert_equal 3, Firm.all.merge!(:order => "id").first.clients.count
479
+ end
480
+
481
+ def test_counting
482
+ assert_equal 3, Firm.all.merge!(:order => "id").first.plain_clients.count
483
+ end
484
+
485
+ def test_counting_with_single_hash
486
+ assert_equal 1, Firm.all.merge!(:order => "id").first.plain_clients.where(:name => "Microsoft").count
487
+ end
488
+
489
+ def test_counting_with_column_name_and_hash
490
+ assert_equal 3, Firm.all.merge!(:order => "id").first.plain_clients.count(:name)
491
+ end
492
+
493
+ def test_counting_with_association_limit
494
+ firm = companies(:first_firm)
495
+ assert_equal firm.limited_clients.length, firm.limited_clients.size
496
+ assert_equal firm.limited_clients.length, firm.limited_clients.count
497
+ end
498
+
499
+ def test_finding
500
+ assert_equal 3, Firm.all.merge!(:order => "id").first.clients.length
501
+ end
502
+
503
+ def test_finding_array_compatibility
504
+ assert_equal 3, Firm.order(:id).find{|f| f.id > 0}.clients.length
505
+ end
506
+
507
+ def test_find_many_with_merged_options
508
+ assert_equal 1, companies(:first_firm).limited_clients.size
509
+ assert_equal 1, companies(:first_firm).limited_clients.to_a.size
510
+ assert_equal 3, companies(:first_firm).limited_clients.limit(nil).to_a.size
511
+ end
512
+
513
+ def test_find_should_append_to_association_order
514
+ ordered_clients = companies(:first_firm).clients_sorted_desc.order('companies.id')
515
+ assert_equal ['id DESC', 'companies.id'], ordered_clients.order_values
516
+ end
517
+
518
+ def test_dynamic_find_should_respect_association_order
519
+ assert_equal companies(:another_first_firm_client), companies(:first_firm).clients_sorted_desc.where("type = 'Client'").first
520
+ assert_equal companies(:another_first_firm_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client')
521
+ end
522
+
523
+ def test_taking
524
+ posts(:other_by_bob).destroy
525
+ assert_equal posts(:misc_by_bob), authors(:bob).posts.take
526
+ assert_equal posts(:misc_by_bob), authors(:bob).posts.take!
527
+ authors(:bob).posts.to_a
528
+ assert_equal posts(:misc_by_bob), authors(:bob).posts.take
529
+ assert_equal posts(:misc_by_bob), authors(:bob).posts.take!
530
+ end
531
+
532
+ def test_taking_not_found
533
+ authors(:bob).posts.delete_all
534
+ assert_raise(ActiveRecord::RecordNotFound) { authors(:bob).posts.take! }
535
+ authors(:bob).posts.to_a
536
+ assert_raise(ActiveRecord::RecordNotFound) { authors(:bob).posts.take! }
537
+ end
538
+
539
+ def test_taking_with_a_number
540
+ # taking from unloaded Relation
541
+ bob = Author.find(authors(:bob).id)
542
+ assert_equal [posts(:misc_by_bob)], bob.posts.take(1)
543
+ bob = Author.find(authors(:bob).id)
544
+ assert_equal [posts(:misc_by_bob), posts(:other_by_bob)], bob.posts.take(2)
545
+
546
+ # taking from loaded Relation
547
+ bob.posts.to_a
548
+ assert_equal [posts(:misc_by_bob)], authors(:bob).posts.take(1)
549
+ assert_equal [posts(:misc_by_bob), posts(:other_by_bob)], authors(:bob).posts.take(2)
550
+ end
551
+
552
+ def test_taking_with_inverse_of
553
+ interests(:woodsmanship).destroy
554
+ interests(:survival).destroy
555
+
556
+ zine = zines(:going_out)
557
+ interest = zine.interests.take
558
+ assert_equal interests(:hunting), interest
559
+ assert_same zine, interest.zine
560
+ end
561
+
562
+ def test_cant_save_has_many_readonly_association
563
+ authors(:david).readonly_comments.each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
564
+ authors(:david).readonly_comments.each { |c| assert c.readonly? }
565
+ end
566
+
567
+ def test_finding_default_orders
568
+ assert_equal "Summit", Firm.all.merge!(:order => "id").first.clients.first.name
569
+ end
570
+
571
+ def test_finding_with_different_class_name_and_order
572
+ assert_equal "Apex", Firm.all.merge!(:order => "id").first.clients_sorted_desc.first.name
573
+ end
574
+
575
+ def test_finding_with_foreign_key
576
+ assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_of_firm.first.name
577
+ end
578
+
579
+ def test_finding_with_condition
580
+ assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_like_ms.first.name
581
+ end
582
+
583
+ def test_finding_with_condition_hash
584
+ assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_like_ms_with_hash_conditions.first.name
585
+ end
586
+
587
+ def test_finding_using_primary_key
588
+ assert_equal "Summit", Firm.all.merge!(:order => "id").first.clients_using_primary_key.first.name
589
+ end
590
+
591
+ def test_update_all_on_association_accessed_before_save
592
+ firm = Firm.new(name: "Firm")
593
+ firm.clients << Client.first
594
+ firm.save!
595
+ assert_equal firm.clients.count, firm.clients.update_all(description: "Great!")
596
+ end
597
+
598
+ def test_update_all_on_association_accessed_before_save_with_explicit_foreign_key
599
+ firm = Firm.new(name: "Firm", id: 100)
600
+ firm.clients << Client.first
601
+ firm.save!
602
+ assert_equal firm.clients.count, firm.clients.update_all(description: "Great!")
603
+ end
604
+
605
+ def test_belongs_to_sanity
606
+ c = Client.new
607
+ assert_nil c.firm, "belongs_to failed sanity check on new object"
608
+ end
609
+
610
+ def test_find_ids
611
+ firm = Firm.all.merge!(:order => "id").first
612
+
613
+ assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find }
614
+
615
+ client = firm.clients.find(2)
616
+ assert_kind_of Client, client
617
+
618
+ client_ary = firm.clients.find([2])
619
+ assert_kind_of Array, client_ary
620
+ assert_equal client, client_ary.first
621
+
622
+ client_ary = firm.clients.find(2, 3)
623
+ assert_kind_of Array, client_ary
624
+ assert_equal 2, client_ary.size
625
+ assert_equal client, client_ary.first
626
+
627
+ assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
628
+ end
629
+
630
+ def test_find_one_message_on_primary_key
631
+ firm = Firm.all.merge!(order: "id").first
632
+
633
+ e = assert_raises(ActiveRecord::RecordNotFound) do
634
+ firm.clients.find(0)
635
+ end
636
+ assert_equal 0, e.id
637
+ assert_equal "id", e.primary_key
638
+ assert_equal "Client", e.model
639
+ assert_match (/\ACouldn't find Client with 'id'=0/), e.message
640
+ end
641
+
642
+ def test_find_ids_and_inverse_of
643
+ force_signal37_to_load_all_clients_of_firm
644
+
645
+ firm = companies(:first_firm)
646
+ client = firm.clients_of_firm.find(3)
647
+ assert_kind_of Client, client
648
+
649
+ client_ary = firm.clients_of_firm.find([3])
650
+ assert_kind_of Array, client_ary
651
+ assert_equal client, client_ary.first
652
+ end
653
+
654
+ def test_find_all
655
+ firm = Firm.all.merge!(:order => "id").first
656
+ assert_equal 3, firm.clients.where("#{QUOTED_TYPE} = 'Client'").to_a.length
657
+ assert_equal 1, firm.clients.where("name = 'Summit'").to_a.length
658
+ end
659
+
660
+ def test_find_each
661
+ firm = companies(:first_firm)
662
+
663
+ assert ! firm.clients.loaded?
664
+
665
+ assert_queries(4) do
666
+ firm.clients.find_each(:batch_size => 1) {|c| assert_equal firm.id, c.firm_id }
667
+ end
668
+
669
+ assert ! firm.clients.loaded?
670
+ end
671
+
672
+ def test_find_each_with_conditions
673
+ firm = companies(:first_firm)
674
+
675
+ assert_queries(2) do
676
+ firm.clients.where(name: 'Microsoft').find_each(batch_size: 1) do |c|
677
+ assert_equal firm.id, c.firm_id
678
+ assert_equal "Microsoft", c.name
679
+ end
680
+ end
681
+
682
+ assert ! firm.clients.loaded?
683
+ end
684
+
685
+ def test_find_in_batches
686
+ firm = companies(:first_firm)
687
+
688
+ assert ! firm.clients.loaded?
689
+
690
+ assert_queries(2) do
691
+ firm.clients.find_in_batches(:batch_size => 2) do |clients|
692
+ clients.each {|c| assert_equal firm.id, c.firm_id }
693
+ end
694
+ end
695
+
696
+ assert ! firm.clients.loaded?
697
+ end
698
+
699
+ def test_find_all_sanitized
700
+ # sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
701
+ firm = Firm.all.merge!(:order => "id").first
702
+ summit = firm.clients.where("name = 'Summit'").to_a
703
+ assert_equal summit, firm.clients.where("name = ?", "Summit").to_a
704
+ assert_equal summit, firm.clients.where("name = :name", { :name => "Summit" }).to_a
705
+ end
706
+
707
+ def test_find_first
708
+ firm = Firm.all.merge!(:order => "id").first
709
+ client2 = Client.find(2)
710
+ assert_equal firm.clients.first, firm.clients.order("id").first
711
+ assert_equal client2, firm.clients.where("#{QUOTED_TYPE} = 'Client'").order("id").first
712
+ end
713
+
714
+ def test_find_first_sanitized
715
+ firm = Firm.all.merge!(:order => "id").first
716
+ client2 = Client.find(2)
717
+ assert_equal client2, firm.clients.merge!(:where => ["#{QUOTED_TYPE} = ?", 'Client'], :order => "id").first
718
+ assert_equal client2, firm.clients.merge!(:where => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }], :order => "id").first
719
+ end
720
+
721
+ def test_find_all_with_include_and_conditions
722
+ assert_nothing_raised do
723
+ Developer.all.merge!(:joins => :audit_logs, :where => {'audit_logs.message' => nil, :name => 'Smith'}).to_a
724
+ end
725
+ end
726
+
727
+ def test_find_in_collection
728
+ assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name
729
+ assert_raise(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
730
+ end
731
+
732
+ def test_find_grouped
733
+ all_clients_of_firm1 = Client.all.merge!(:where => "firm_id = 1").to_a
734
+ grouped_clients_of_firm1 = Client.all.merge!(:where => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count').to_a
735
+ assert_equal 3, all_clients_of_firm1.size
736
+ assert_equal 1, grouped_clients_of_firm1.size
737
+ end
738
+
739
+ def test_find_scoped_grouped
740
+ assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.size
741
+ assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length
742
+ assert_equal 3, companies(:first_firm).clients_grouped_by_name.size
743
+ assert_equal 3, companies(:first_firm).clients_grouped_by_name.length
744
+ end
745
+
746
+ def test_find_scoped_grouped_having
747
+ assert_equal 1, authors(:david).popular_grouped_posts.length
748
+ assert_equal 0, authors(:mary).popular_grouped_posts.length
749
+ end
750
+
751
+ def test_default_select
752
+ assert_equal Comment.column_names.sort, posts(:welcome).comments.first.attributes.keys.sort
753
+ end
754
+
755
+ def test_select_query_method
756
+ assert_equal ['id', 'body'], posts(:welcome).comments.select(:id, :body).first.attributes.keys
757
+ end
758
+
759
+ def test_select_with_block
760
+ assert_equal [1], posts(:welcome).comments.select { |c| c.id == 1 }.map(&:id)
761
+ end
762
+
763
+ def test_select_without_foreign_key
764
+ assert_equal companies(:first_firm).accounts.first.credit_limit, companies(:first_firm).accounts.select(:credit_limit).first.credit_limit
765
+ end
766
+
767
+ def test_adding
768
+ force_signal37_to_load_all_clients_of_firm
769
+ natural = Client.new("name" => "Natural Company")
770
+ companies(:first_firm).clients_of_firm << natural
771
+ assert_equal 3, companies(:first_firm).clients_of_firm.size # checking via the collection
772
+ assert_equal 3, companies(:first_firm).clients_of_firm.reload.size # checking using the db
773
+ assert_equal natural, companies(:first_firm).clients_of_firm.last
774
+ end
775
+
776
+ def test_adding_using_create
777
+ first_firm = companies(:first_firm)
778
+ assert_equal 3, first_firm.plain_clients.size
779
+ first_firm.plain_clients.create(:name => "Natural Company")
780
+ assert_equal 4, first_firm.plain_clients.length
781
+ assert_equal 4, first_firm.plain_clients.size
782
+ end
783
+
784
+ def test_create_with_bang_on_has_many_when_parent_is_new_raises
785
+ error = assert_raise(ActiveRecord::RecordNotSaved) do
786
+ firm = Firm.new
787
+ firm.plain_clients.create! :name=>"Whoever"
788
+ end
789
+
790
+ assert_equal "You cannot call create unless the parent is saved", error.message
791
+ end
792
+
793
+ def test_regular_create_on_has_many_when_parent_is_new_raises
794
+ error = assert_raise(ActiveRecord::RecordNotSaved) do
795
+ firm = Firm.new
796
+ firm.plain_clients.create :name=>"Whoever"
797
+ end
798
+
799
+ assert_equal "You cannot call create unless the parent is saved", error.message
800
+ end
801
+
802
+ def test_create_with_bang_on_has_many_raises_when_record_not_saved
803
+ assert_raise(ActiveRecord::RecordInvalid) do
804
+ firm = Firm.all.merge!(:order => "id").first
805
+ firm.plain_clients.create!
806
+ end
807
+ end
808
+
809
+ def test_create_with_bang_on_habtm_when_parent_is_new_raises
810
+ error = assert_raise(ActiveRecord::RecordNotSaved) do
811
+ Developer.new("name" => "Aredridel").projects.create!
812
+ end
813
+
814
+ assert_equal "You cannot call create unless the parent is saved", error.message
815
+ end
816
+
817
+ def test_adding_a_mismatch_class
818
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
819
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
820
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
821
+ end
822
+
823
+ def test_adding_a_collection
824
+ force_signal37_to_load_all_clients_of_firm
825
+ companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")])
826
+ assert_equal 4, companies(:first_firm).clients_of_firm.size
827
+ assert_equal 4, companies(:first_firm).clients_of_firm.reload.size
828
+ end
829
+
830
+ def test_transactions_when_adding_to_persisted
831
+ good = Client.new(:name => "Good")
832
+ bad = Client.new(:name => "Bad", :raise_on_save => true)
833
+
834
+ begin
835
+ companies(:first_firm).clients_of_firm.concat(good, bad)
836
+ rescue Client::RaisedOnSave
837
+ end
838
+
839
+ assert !companies(:first_firm).clients_of_firm.reload.include?(good)
840
+ end
841
+
842
+ def test_transactions_when_adding_to_new_record
843
+ assert_no_queries(ignore_none: false) do
844
+ firm = Firm.new
845
+ firm.clients_of_firm.concat(Client.new("name" => "Natural Company"))
846
+ end
847
+ end
848
+
849
+ def test_inverse_on_before_validate
850
+ firm = companies(:first_firm)
851
+ assert_queries(1) do
852
+ firm.clients_of_firm << Client.new("name" => "Natural Company")
853
+ end
854
+ end
855
+
856
+ def test_new_aliased_to_build
857
+ company = companies(:first_firm)
858
+ new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.new("name" => "Another Client") }
859
+ assert !company.clients_of_firm.loaded?
860
+
861
+ assert_equal "Another Client", new_client.name
862
+ assert !new_client.persisted?
863
+ assert_equal new_client, company.clients_of_firm.last
864
+ end
865
+
866
+ def test_build
867
+ company = companies(:first_firm)
868
+ new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build("name" => "Another Client") }
869
+ assert !company.clients_of_firm.loaded?
870
+
871
+ assert_equal "Another Client", new_client.name
872
+ assert !new_client.persisted?
873
+ assert_equal new_client, company.clients_of_firm.last
874
+ end
875
+
876
+ def test_collection_size_after_building
877
+ company = companies(:first_firm) # company already has one client
878
+ company.clients_of_firm.build("name" => "Another Client")
879
+ company.clients_of_firm.build("name" => "Yet Another Client")
880
+ assert_equal 4, company.clients_of_firm.size
881
+ end
882
+
883
+ def test_collection_not_empty_after_building
884
+ company = companies(:first_firm)
885
+ assert_predicate company.contracts, :empty?
886
+ company.contracts.build
887
+ assert_not_predicate company.contracts, :empty?
888
+ end
889
+
890
+ def test_collection_size_twice_for_regressions
891
+ post = posts(:thinking)
892
+ assert_equal 0, post.readers.size
893
+ # This test needs a post that has no readers, we assert it to ensure it holds,
894
+ # but need to reload the post because the very call to #size hides the bug.
895
+ post.reload
896
+ post.readers.build
897
+ size1 = post.readers.size
898
+ size2 = post.readers.size
899
+ assert_equal size1, size2
900
+ end
901
+
902
+ def test_build_many
903
+ company = companies(:first_firm)
904
+ new_clients = assert_no_queries(ignore_none: false) { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }
905
+ assert_equal 2, new_clients.size
906
+ end
907
+
908
+ def test_build_followed_by_save_does_not_load_target
909
+ companies(:first_firm).clients_of_firm.build("name" => "Another Client")
910
+ assert companies(:first_firm).save
911
+ assert !companies(:first_firm).clients_of_firm.loaded?
912
+ end
913
+
914
+ def test_build_without_loading_association
915
+ first_topic = topics(:first)
916
+ Reply.column_names
917
+
918
+ assert_equal 1, first_topic.replies.length
919
+
920
+ assert_no_queries do
921
+ first_topic.replies.build(:title => "Not saved", :content => "Superstars")
922
+ assert_equal 2, first_topic.replies.size
923
+ end
924
+
925
+ assert_equal 2, first_topic.replies.to_ary.size
926
+ end
927
+
928
+ def test_build_via_block
929
+ company = companies(:first_firm)
930
+ new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build {|client| client.name = "Another Client" } }
931
+ assert !company.clients_of_firm.loaded?
932
+
933
+ assert_equal "Another Client", new_client.name
934
+ assert !new_client.persisted?
935
+ assert_equal new_client, company.clients_of_firm.last
936
+ end
937
+
938
+ def test_build_many_via_block
939
+ company = companies(:first_firm)
940
+ new_clients = assert_no_queries(ignore_none: false) do
941
+ company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) do |client|
942
+ client.name = "changed"
943
+ end
944
+ end
945
+
946
+ assert_equal 2, new_clients.size
947
+ assert_equal "changed", new_clients.first.name
948
+ assert_equal "changed", new_clients.last.name
949
+ end
950
+
951
+ def test_create_without_loading_association
952
+ first_firm = companies(:first_firm)
953
+ Firm.column_names
954
+ Client.column_names
955
+
956
+ assert_equal 2, first_firm.clients_of_firm.size
957
+ first_firm.clients_of_firm.reset
958
+
959
+ assert_queries(1) do
960
+ first_firm.clients_of_firm.create(:name => "Superstars")
961
+ end
962
+
963
+ assert_equal 3, first_firm.clients_of_firm.size
964
+ end
965
+
966
+ def test_create
967
+ force_signal37_to_load_all_clients_of_firm
968
+ new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
969
+ assert new_client.persisted?
970
+ assert_equal new_client, companies(:first_firm).clients_of_firm.last
971
+ assert_equal new_client, companies(:first_firm).clients_of_firm.reload.last
972
+ end
973
+
974
+ def test_create_many
975
+ companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
976
+ assert_equal 4, companies(:first_firm).clients_of_firm.reload.size
977
+ end
978
+
979
+ def test_create_followed_by_save_does_not_load_target
980
+ companies(:first_firm).clients_of_firm.create("name" => "Another Client")
981
+ assert companies(:first_firm).save
982
+ assert !companies(:first_firm).clients_of_firm.loaded?
983
+ end
984
+
985
+ def test_deleting
986
+ force_signal37_to_load_all_clients_of_firm
987
+ companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
988
+ assert_equal 1, companies(:first_firm).clients_of_firm.size
989
+ assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
990
+ end
991
+
992
+ def test_deleting_before_save
993
+ new_firm = Firm.new("name" => "A New Firm, Inc.")
994
+ new_client = new_firm.clients_of_firm.build("name" => "Another Client")
995
+ assert_equal 1, new_firm.clients_of_firm.size
996
+ new_firm.clients_of_firm.delete(new_client)
997
+ assert_equal 0, new_firm.clients_of_firm.size
998
+ end
999
+
1000
+ def test_has_many_without_counter_cache_option
1001
+ # Ship has a conventionally named `treasures_count` column, but the counter_cache
1002
+ # option is not given on the association.
1003
+ ship = Ship.create(name: 'Countless', treasures_count: 10)
1004
+
1005
+ assert_not Ship.reflect_on_association(:treasures).has_cached_counter?
1006
+
1007
+ # Count should come from sql count() of treasures rather than treasures_count attribute
1008
+ assert_equal ship.treasures.size, 0
1009
+
1010
+ assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed" do
1011
+ ship.treasures.create(name: 'Gold')
1012
+ end
1013
+
1014
+ assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed" do
1015
+ ship.treasures.destroy_all
1016
+ end
1017
+ end
1018
+
1019
+ def test_deleting_updates_counter_cache
1020
+ topic = Topic.order("id ASC").first
1021
+ assert_equal topic.replies.to_a.size, topic.replies_count
1022
+
1023
+ topic.replies.delete(topic.replies.first)
1024
+ topic.reload
1025
+ assert_equal topic.replies.to_a.size, topic.replies_count
1026
+ end
1027
+
1028
+ def test_counter_cache_updates_in_memory_after_concat
1029
+ topic = Topic.create title: "Zoom-zoom-zoom"
1030
+
1031
+ topic.replies << Reply.create(title: "re: zoom", content: "speedy quick!")
1032
+ assert_equal 1, topic.replies_count
1033
+ assert_equal 1, topic.replies.size
1034
+ assert_equal 1, topic.reload.replies.size
1035
+ end
1036
+
1037
+ def test_counter_cache_updates_in_memory_after_create
1038
+ topic = Topic.create title: "Zoom-zoom-zoom"
1039
+
1040
+ topic.replies.create!(title: "re: zoom", content: "speedy quick!")
1041
+ assert_equal 1, topic.replies_count
1042
+ assert_equal 1, topic.replies.size
1043
+ assert_equal 1, topic.reload.replies.size
1044
+ end
1045
+
1046
+ def test_counter_cache_updates_in_memory_after_create_with_array
1047
+ topic = Topic.create title: "Zoom-zoom-zoom"
1048
+
1049
+ topic.replies.create!([
1050
+ { title: "re: zoom", content: "speedy quick!" },
1051
+ { title: "re: zoom 2", content: "OMG lol!" },
1052
+ ])
1053
+ assert_equal 2, topic.replies_count
1054
+ assert_equal 2, topic.replies.size
1055
+ assert_equal 2, topic.reload.replies.size
1056
+ end
1057
+
1058
+ def test_pushing_association_updates_counter_cache
1059
+ topic = Topic.order("id ASC").first
1060
+ reply = Reply.create!
1061
+
1062
+ assert_difference "topic.reload.replies_count", 1 do
1063
+ topic.replies << reply
1064
+ end
1065
+ end
1066
+
1067
+ def test_deleting_updates_counter_cache_without_dependent_option
1068
+ post = posts(:welcome)
1069
+
1070
+ assert_difference "post.reload.tags_count", -1 do
1071
+ post.taggings.delete(post.taggings.first)
1072
+ end
1073
+ end
1074
+
1075
+ def test_deleting_updates_counter_cache_with_dependent_delete_all
1076
+ post = posts(:welcome)
1077
+ post.update_columns(taggings_with_delete_all_count: post.tags_count)
1078
+
1079
+ assert_difference "post.reload.taggings_with_delete_all_count", -1 do
1080
+ post.taggings_with_delete_all.delete(post.taggings_with_delete_all.first)
1081
+ end
1082
+ end
1083
+
1084
+ def test_deleting_updates_counter_cache_with_dependent_destroy
1085
+ post = posts(:welcome)
1086
+ post.update_columns(taggings_with_destroy_count: post.tags_count)
1087
+
1088
+ assert_difference "post.reload.taggings_with_destroy_count", -1 do
1089
+ post.taggings_with_destroy.delete(post.taggings_with_destroy.first)
1090
+ end
1091
+ end
1092
+
1093
+ def test_calling_empty_with_counter_cache
1094
+ post = posts(:welcome)
1095
+ assert_queries(0) do
1096
+ assert_not post.comments.empty?
1097
+ end
1098
+ end
1099
+
1100
+ def test_custom_named_counter_cache
1101
+ topic = topics(:first)
1102
+
1103
+ assert_difference "topic.reload.replies_count", -1 do
1104
+ topic.approved_replies.clear
1105
+ end
1106
+ end
1107
+
1108
+ def test_calling_update_attributes_on_id_changes_the_counter_cache
1109
+ topic = Topic.order("id ASC").first
1110
+ original_count = topic.replies.to_a.size
1111
+ assert_equal original_count, topic.replies_count
1112
+
1113
+ first_reply = topic.replies.first
1114
+ first_reply.update_attributes(:parent_id => nil)
1115
+ assert_equal original_count - 1, topic.reload.replies_count
1116
+
1117
+ first_reply.update_attributes(:parent_id => topic.id)
1118
+ assert_equal original_count, topic.reload.replies_count
1119
+ end
1120
+
1121
+ def test_calling_update_attributes_changing_ids_doesnt_change_counter_cache
1122
+ topic1 = Topic.find(1)
1123
+ topic2 = Topic.find(3)
1124
+ original_count1 = topic1.replies.to_a.size
1125
+ original_count2 = topic2.replies.to_a.size
1126
+
1127
+ reply1 = topic1.replies.first
1128
+ reply2 = topic2.replies.first
1129
+
1130
+ reply1.update_attributes(:parent_id => topic2.id)
1131
+ assert_equal original_count1 - 1, topic1.reload.replies_count
1132
+ assert_equal original_count2 + 1, topic2.reload.replies_count
1133
+
1134
+ reply2.update_attributes(:parent_id => topic1.id)
1135
+ assert_equal original_count1, topic1.reload.replies_count
1136
+ assert_equal original_count2, topic2.reload.replies_count
1137
+ end
1138
+
1139
+ def test_deleting_a_collection
1140
+ force_signal37_to_load_all_clients_of_firm
1141
+ companies(:first_firm).clients_of_firm.create("name" => "Another Client")
1142
+ assert_equal 3, companies(:first_firm).clients_of_firm.size
1143
+ companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1], companies(:first_firm).clients_of_firm[2]])
1144
+ assert_equal 0, companies(:first_firm).clients_of_firm.size
1145
+ assert_equal 0, companies(:first_firm).clients_of_firm.reload.size
1146
+ end
1147
+
1148
+ def test_delete_all
1149
+ force_signal37_to_load_all_clients_of_firm
1150
+ companies(:first_firm).dependent_clients_of_firm.create("name" => "Another Client")
1151
+ clients = companies(:first_firm).dependent_clients_of_firm.to_a
1152
+ assert_equal 3, clients.count
1153
+
1154
+ assert_difference "Client.count", -(clients.count) do
1155
+ companies(:first_firm).dependent_clients_of_firm.delete_all
1156
+ end
1157
+ end
1158
+
1159
+ def test_delete_all_with_not_yet_loaded_association_collection
1160
+ force_signal37_to_load_all_clients_of_firm
1161
+ companies(:first_firm).clients_of_firm.create("name" => "Another Client")
1162
+ assert_equal 3, companies(:first_firm).clients_of_firm.size
1163
+ companies(:first_firm).clients_of_firm.reset
1164
+ companies(:first_firm).clients_of_firm.delete_all
1165
+ assert_equal 0, companies(:first_firm).clients_of_firm.size
1166
+ assert_equal 0, companies(:first_firm).clients_of_firm.reload.size
1167
+ end
1168
+
1169
+ def test_transaction_when_deleting_persisted
1170
+ good = Client.new(:name => "Good")
1171
+ bad = Client.new(:name => "Bad", :raise_on_destroy => true)
1172
+
1173
+ companies(:first_firm).clients_of_firm = [good, bad]
1174
+
1175
+ begin
1176
+ companies(:first_firm).clients_of_firm.destroy(good, bad)
1177
+ rescue Client::RaisedOnDestroy
1178
+ end
1179
+
1180
+ assert_equal [good, bad], companies(:first_firm).clients_of_firm.reload
1181
+ end
1182
+
1183
+ def test_transaction_when_deleting_new_record
1184
+ assert_no_queries(ignore_none: false) do
1185
+ firm = Firm.new
1186
+ client = Client.new("name" => "New Client")
1187
+ firm.clients_of_firm << client
1188
+ firm.clients_of_firm.destroy(client)
1189
+ end
1190
+ end
1191
+
1192
+ def test_clearing_an_association_collection
1193
+ firm = companies(:first_firm)
1194
+ client_id = firm.clients_of_firm.first.id
1195
+ assert_equal 2, firm.clients_of_firm.size
1196
+
1197
+ firm.clients_of_firm.clear
1198
+
1199
+ assert_equal 0, firm.clients_of_firm.size
1200
+ assert_equal 0, firm.clients_of_firm.reload.size
1201
+ assert_equal [], Client.destroyed_client_ids[firm.id]
1202
+
1203
+ # Should not be destroyed since the association is not dependent.
1204
+ assert_nothing_raised do
1205
+ assert_nil Client.find(client_id).firm
1206
+ end
1207
+ end
1208
+
1209
+ def test_clearing_updates_counter_cache
1210
+ topic = Topic.first
1211
+
1212
+ assert_difference 'topic.reload.replies_count', -1 do
1213
+ topic.replies.clear
1214
+ end
1215
+ end
1216
+
1217
+ def test_clearing_updates_counter_cache_when_inverse_counter_cache_is_a_symbol_with_dependent_destroy
1218
+ car = Car.first
1219
+ car.engines.create!
1220
+
1221
+ assert_difference 'car.reload.engines_count', -1 do
1222
+ car.engines.clear
1223
+ end
1224
+ end
1225
+
1226
+ def test_clearing_a_dependent_association_collection
1227
+ firm = companies(:first_firm)
1228
+ client_id = firm.dependent_clients_of_firm.first.id
1229
+ assert_equal 2, firm.dependent_clients_of_firm.size
1230
+ assert_equal 1, Client.find_by_id(client_id).client_of
1231
+
1232
+ # :delete_all is called on each client since the dependent options is :destroy
1233
+ firm.dependent_clients_of_firm.clear
1234
+
1235
+ assert_equal 0, firm.dependent_clients_of_firm.size
1236
+ assert_equal 0, firm.dependent_clients_of_firm.reload.size
1237
+ assert_equal [], Client.destroyed_client_ids[firm.id]
1238
+
1239
+ # Should be destroyed since the association is dependent.
1240
+ assert_nil Client.find_by_id(client_id)
1241
+ end
1242
+
1243
+ def test_delete_all_with_option_delete_all
1244
+ firm = companies(:first_firm)
1245
+ client_id = firm.dependent_clients_of_firm.first.id
1246
+ firm.dependent_clients_of_firm.delete_all(:delete_all)
1247
+ assert_nil Client.find_by_id(client_id)
1248
+ end
1249
+
1250
+ def test_delete_all_accepts_limited_parameters
1251
+ firm = companies(:first_firm)
1252
+ assert_raise(ArgumentError) do
1253
+ firm.dependent_clients_of_firm.delete_all(:destroy)
1254
+ end
1255
+ end
1256
+
1257
+ def test_clearing_an_exclusively_dependent_association_collection
1258
+ firm = companies(:first_firm)
1259
+ client_id = firm.exclusively_dependent_clients_of_firm.first.id
1260
+ assert_equal 2, firm.exclusively_dependent_clients_of_firm.size
1261
+
1262
+ assert_equal [], Client.destroyed_client_ids[firm.id]
1263
+
1264
+ # :exclusively_dependent means each client is deleted directly from
1265
+ # the database without looping through them calling destroy.
1266
+ firm.exclusively_dependent_clients_of_firm.clear
1267
+
1268
+ assert_equal 0, firm.exclusively_dependent_clients_of_firm.size
1269
+ assert_equal 0, firm.exclusively_dependent_clients_of_firm.reload.size
1270
+ # no destroy-filters should have been called
1271
+ assert_equal [], Client.destroyed_client_ids[firm.id]
1272
+
1273
+ # Should be destroyed since the association is exclusively dependent.
1274
+ assert_nil Client.find_by_id(client_id)
1275
+ end
1276
+
1277
+ def test_dependent_association_respects_optional_conditions_on_delete
1278
+ firm = companies(:odegy)
1279
+ Client.create(:client_of => firm.id, :name => "BigShot Inc.")
1280
+ Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
1281
+ # only one of two clients is included in the association due to the :conditions key
1282
+ assert_equal 2, Client.where(client_of: firm.id).size
1283
+ assert_equal 1, firm.dependent_conditional_clients_of_firm.size
1284
+ firm.destroy
1285
+ # only the correctly associated client should have been deleted
1286
+ assert_equal 1, Client.where(client_of: firm.id).size
1287
+ end
1288
+
1289
+ def test_dependent_association_respects_optional_sanitized_conditions_on_delete
1290
+ firm = companies(:odegy)
1291
+ Client.create(:client_of => firm.id, :name => "BigShot Inc.")
1292
+ Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
1293
+ # only one of two clients is included in the association due to the :conditions key
1294
+ assert_equal 2, Client.where(client_of: firm.id).size
1295
+ assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
1296
+ firm.destroy
1297
+ # only the correctly associated client should have been deleted
1298
+ assert_equal 1, Client.where(client_of: firm.id).size
1299
+ end
1300
+
1301
+ def test_dependent_association_respects_optional_hash_conditions_on_delete
1302
+ firm = companies(:odegy)
1303
+ Client.create(:client_of => firm.id, :name => "BigShot Inc.")
1304
+ Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
1305
+ # only one of two clients is included in the association due to the :conditions key
1306
+ assert_equal 2, Client.where(client_of: firm.id).size
1307
+ assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
1308
+ firm.destroy
1309
+ # only the correctly associated client should have been deleted
1310
+ assert_equal 1, Client.where(client_of: firm.id).size
1311
+ end
1312
+
1313
+ def test_delete_all_association_with_primary_key_deletes_correct_records
1314
+ firm = Firm.first
1315
+ # break the vanilla firm_id foreign key
1316
+ assert_equal 3, firm.clients.count
1317
+ firm.clients.first.update_columns(firm_id: nil)
1318
+ assert_equal 2, firm.clients.reload.count
1319
+ assert_equal 2, firm.clients_using_primary_key_with_delete_all.count
1320
+ old_record = firm.clients_using_primary_key_with_delete_all.first
1321
+ firm = Firm.first
1322
+ firm.destroy
1323
+ assert_nil Client.find_by_id(old_record.id)
1324
+ end
1325
+
1326
+ def test_creation_respects_hash_condition
1327
+ ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build
1328
+
1329
+ assert ms_client.save
1330
+ assert_equal 'Microsoft', ms_client.name
1331
+
1332
+ another_ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.create
1333
+
1334
+ assert another_ms_client.persisted?
1335
+ assert_equal 'Microsoft', another_ms_client.name
1336
+ end
1337
+
1338
+ def test_clearing_without_initial_access
1339
+ firm = companies(:first_firm)
1340
+
1341
+ firm.clients_of_firm.clear
1342
+
1343
+ assert_equal 0, firm.clients_of_firm.size
1344
+ assert_equal 0, firm.clients_of_firm.reload.size
1345
+ end
1346
+
1347
+ def test_deleting_a_item_which_is_not_in_the_collection
1348
+ force_signal37_to_load_all_clients_of_firm
1349
+ summit = Client.find_by_name('Summit')
1350
+ companies(:first_firm).clients_of_firm.delete(summit)
1351
+ assert_equal 2, companies(:first_firm).clients_of_firm.size
1352
+ assert_equal 2, companies(:first_firm).clients_of_firm.reload.size
1353
+ assert_equal 2, summit.client_of
1354
+ end
1355
+
1356
+ def test_deleting_by_integer_id
1357
+ david = Developer.find(1)
1358
+
1359
+ assert_difference 'david.projects.count', -1 do
1360
+ assert_equal 1, david.projects.delete(1).size
1361
+ end
1362
+
1363
+ assert_equal 1, david.projects.size
1364
+ end
1365
+
1366
+ def test_deleting_by_string_id
1367
+ david = Developer.find(1)
1368
+
1369
+ assert_difference 'david.projects.count', -1 do
1370
+ assert_equal 1, david.projects.delete('1').size
1371
+ end
1372
+
1373
+ assert_equal 1, david.projects.size
1374
+ end
1375
+
1376
+ def test_deleting_self_type_mismatch
1377
+ david = Developer.find(1)
1378
+ david.projects.reload
1379
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
1380
+ end
1381
+
1382
+ def test_destroying
1383
+ force_signal37_to_load_all_clients_of_firm
1384
+
1385
+ assert_difference "Client.count", -1 do
1386
+ companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first)
1387
+ end
1388
+
1389
+ assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
1390
+ assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
1391
+ end
1392
+
1393
+ def test_destroying_by_integer_id
1394
+ force_signal37_to_load_all_clients_of_firm
1395
+
1396
+ assert_difference "Client.count", -1 do
1397
+ companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id)
1398
+ end
1399
+
1400
+ assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
1401
+ assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
1402
+ end
1403
+
1404
+ def test_destroying_by_string_id
1405
+ force_signal37_to_load_all_clients_of_firm
1406
+
1407
+ assert_difference "Client.count", -1 do
1408
+ companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id.to_s)
1409
+ end
1410
+
1411
+ assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
1412
+ assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
1413
+ end
1414
+
1415
+ def test_destroying_a_collection
1416
+ force_signal37_to_load_all_clients_of_firm
1417
+ companies(:first_firm).clients_of_firm.create("name" => "Another Client")
1418
+ assert_equal 3, companies(:first_firm).clients_of_firm.size
1419
+
1420
+ assert_difference "Client.count", -2 do
1421
+ companies(:first_firm).clients_of_firm.destroy([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
1422
+ end
1423
+
1424
+ assert_equal 1, companies(:first_firm).reload.clients_of_firm.size
1425
+ assert_equal 1, companies(:first_firm).clients_of_firm.reload.size
1426
+ end
1427
+
1428
+ def test_destroy_all
1429
+ force_signal37_to_load_all_clients_of_firm
1430
+ clients = companies(:first_firm).clients_of_firm.to_a
1431
+ assert !clients.empty?, "37signals has clients after load"
1432
+ destroyed = companies(:first_firm).clients_of_firm.destroy_all
1433
+ assert_equal clients.sort_by(&:id), destroyed.sort_by(&:id)
1434
+ assert destroyed.all?(&:frozen?), "destroyed clients should be frozen"
1435
+ assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all"
1436
+ assert companies(:first_firm).clients_of_firm.reload.empty?, "37signals has no clients after destroy all and refresh"
1437
+ end
1438
+
1439
+ def test_dependence
1440
+ firm = companies(:first_firm)
1441
+ assert_equal 3, firm.clients.size
1442
+ firm.destroy
1443
+ assert Client.all.merge!(:where => "firm_id=#{firm.id}").to_a.empty?
1444
+ end
1445
+
1446
+ def test_dependence_for_associations_with_hash_condition
1447
+ david = authors(:david)
1448
+ assert_difference('Post.count', -1) { assert david.destroy }
1449
+ end
1450
+
1451
+ def test_destroy_dependent_when_deleted_from_association
1452
+ # sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
1453
+ firm = Firm.all.merge!(:order => "id").first
1454
+ assert_equal 3, firm.clients.size
1455
+
1456
+ client = firm.clients.first
1457
+ firm.clients.delete(client)
1458
+
1459
+ assert_raise(ActiveRecord::RecordNotFound) { Client.find(client.id) }
1460
+ assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(client.id) }
1461
+ assert_equal 2, firm.clients.size
1462
+ end
1463
+
1464
+ def test_three_levels_of_dependence
1465
+ topic = Topic.create "title" => "neat and simple"
1466
+ reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it"
1467
+ reply.replies.create "title" => "neat and simple", "content" => "ain't complaining"
1468
+
1469
+ assert_nothing_raised { topic.destroy }
1470
+ end
1471
+
1472
+ def test_dependence_with_transaction_support_on_failure
1473
+ firm = companies(:first_firm)
1474
+ clients = firm.clients
1475
+ assert_equal 3, clients.length
1476
+ clients.last.instance_eval { def overwrite_to_raise() raise "Trigger rollback" end }
1477
+
1478
+ firm.destroy rescue "do nothing"
1479
+
1480
+ assert_equal 3, Client.all.merge!(:where => "firm_id=#{firm.id}").to_a.size
1481
+ end
1482
+
1483
+ def test_dependence_on_account
1484
+ num_accounts = Account.count
1485
+ companies(:first_firm).destroy
1486
+ assert_equal num_accounts - 1, Account.count
1487
+ end
1488
+
1489
+ def test_depends_and_nullify
1490
+ num_accounts = Account.count
1491
+
1492
+ core = companies(:rails_core)
1493
+ assert_equal accounts(:rails_core_account), core.account
1494
+ assert_equal companies(:leetsoft, :jadedpixel), core.companies
1495
+ core.destroy
1496
+ assert_nil accounts(:rails_core_account).reload.firm_id
1497
+ assert_nil companies(:leetsoft).reload.client_of
1498
+ assert_nil companies(:jadedpixel).reload.client_of
1499
+
1500
+ assert_equal num_accounts, Account.count
1501
+ end
1502
+
1503
+ def test_restrict_with_exception
1504
+ firm = RestrictedWithExceptionFirm.create!(:name => 'restrict')
1505
+ firm.companies.create(:name => 'child')
1506
+
1507
+ assert !firm.companies.empty?
1508
+ assert_raise(ActiveRecord::DeleteRestrictionError) { firm.destroy }
1509
+ assert RestrictedWithExceptionFirm.exists?(:name => 'restrict')
1510
+ assert firm.companies.exists?(:name => 'child')
1511
+ end
1512
+
1513
+ def test_restrict_with_error_is_deprecated_using_key_many
1514
+ I18n.backend = I18n::Backend::Simple.new
1515
+ I18n.backend.store_translations :en, activerecord: { errors: { messages: { restrict_dependent_destroy: { many: 'message for deprecated key' } } } }
1516
+
1517
+ firm = RestrictedWithErrorFirm.create!(name: 'restrict')
1518
+ firm.companies.create(name: 'child')
1519
+
1520
+ assert !firm.companies.empty?
1521
+
1522
+ assert_deprecated { firm.destroy }
1523
+
1524
+ assert !firm.errors.empty?
1525
+
1526
+ assert_equal 'message for deprecated key', firm.errors[:base].first
1527
+ assert RestrictedWithErrorFirm.exists?(name: 'restrict')
1528
+ assert firm.companies.exists?(name: 'child')
1529
+ ensure
1530
+ I18n.backend.reload!
1531
+ end
1532
+
1533
+ def test_restrict_with_error
1534
+ firm = RestrictedWithErrorFirm.create!(:name => 'restrict')
1535
+ firm.companies.create(:name => 'child')
1536
+
1537
+ assert !firm.companies.empty?
1538
+
1539
+ firm.destroy
1540
+
1541
+ assert !firm.errors.empty?
1542
+
1543
+ assert_equal "Cannot delete record because dependent companies exist", firm.errors[:base].first
1544
+ assert RestrictedWithErrorFirm.exists?(:name => 'restrict')
1545
+ assert firm.companies.exists?(:name => 'child')
1546
+ end
1547
+
1548
+ def test_restrict_with_error_with_locale
1549
+ I18n.backend = I18n::Backend::Simple.new
1550
+ I18n.backend.store_translations 'en', activerecord: {attributes: {restricted_with_error_firm: {companies: 'client companies'}}}
1551
+ firm = RestrictedWithErrorFirm.create!(name: 'restrict')
1552
+ firm.companies.create(name: 'child')
1553
+
1554
+ assert !firm.companies.empty?
1555
+
1556
+ firm.destroy
1557
+
1558
+ assert !firm.errors.empty?
1559
+
1560
+ assert_equal "Cannot delete record because dependent client companies exist", firm.errors[:base].first
1561
+ assert RestrictedWithErrorFirm.exists?(name: 'restrict')
1562
+ assert firm.companies.exists?(name: 'child')
1563
+ ensure
1564
+ I18n.backend.reload!
1565
+ end
1566
+
1567
+ def test_included_in_collection
1568
+ assert_equal true, companies(:first_firm).clients.include?(Client.find(2))
1569
+ end
1570
+
1571
+ def test_included_in_collection_for_new_records
1572
+ client = Client.create(:name => 'Persisted')
1573
+ assert_nil client.client_of
1574
+ assert_equal false, Firm.new.clients_of_firm.include?(client),
1575
+ 'includes a client that does not belong to any firm'
1576
+ end
1577
+
1578
+ def test_adding_array_and_collection
1579
+ assert_nothing_raised { Firm.first.clients + Firm.all.last.clients }
1580
+ end
1581
+
1582
+ def test_replace_with_less
1583
+ firm = Firm.all.merge!(:order => "id").first
1584
+ firm.clients = [companies(:first_client)]
1585
+ assert firm.save, "Could not save firm"
1586
+ firm.reload
1587
+ assert_equal 1, firm.clients.length
1588
+ end
1589
+
1590
+ def test_replace_with_less_and_dependent_nullify
1591
+ num_companies = Company.count
1592
+ companies(:rails_core).companies = []
1593
+ assert_equal num_companies, Company.count
1594
+ end
1595
+
1596
+ def test_replace_with_new
1597
+ firm = Firm.all.merge!(:order => "id").first
1598
+ firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
1599
+ firm.save
1600
+ firm.reload
1601
+ assert_equal 2, firm.clients.length
1602
+ assert_equal false, firm.clients.include?(:first_client)
1603
+ end
1604
+
1605
+ def test_replace_failure
1606
+ firm = companies(:first_firm)
1607
+ account = Account.new
1608
+ orig_accounts = firm.accounts.to_a
1609
+
1610
+ assert !account.valid?
1611
+ assert !orig_accounts.empty?
1612
+ error = assert_raise ActiveRecord::RecordNotSaved do
1613
+ firm.accounts = [account]
1614
+ end
1615
+
1616
+ assert_equal orig_accounts, firm.accounts
1617
+ assert_equal "Failed to replace accounts because one or more of the " \
1618
+ "new records could not be saved.", error.message
1619
+ end
1620
+
1621
+ def test_replace_with_same_content
1622
+ firm = Firm.first
1623
+ firm.clients = []
1624
+ firm.save
1625
+
1626
+ assert_queries(0, ignore_none: true) do
1627
+ firm.clients = []
1628
+ end
1629
+
1630
+ assert_equal [], firm.send('clients=', [])
1631
+ end
1632
+
1633
+ def test_transactions_when_replacing_on_persisted
1634
+ good = Client.new(:name => "Good")
1635
+ bad = Client.new(:name => "Bad", :raise_on_save => true)
1636
+
1637
+ companies(:first_firm).clients_of_firm = [good]
1638
+
1639
+ begin
1640
+ companies(:first_firm).clients_of_firm = [bad]
1641
+ rescue Client::RaisedOnSave
1642
+ end
1643
+
1644
+ assert_equal [good], companies(:first_firm).clients_of_firm.reload
1645
+ end
1646
+
1647
+ def test_transactions_when_replacing_on_new_record
1648
+ assert_no_queries(ignore_none: false) do
1649
+ firm = Firm.new
1650
+ firm.clients_of_firm = [Client.new("name" => "New Client")]
1651
+ end
1652
+ end
1653
+
1654
+ def test_get_ids
1655
+ assert_equal [companies(:first_client).id, companies(:second_client).id, companies(:another_first_firm_client).id], companies(:first_firm).client_ids
1656
+ end
1657
+
1658
+ def test_get_ids_for_loaded_associations
1659
+ company = companies(:first_firm)
1660
+ company.clients.reload
1661
+ assert_queries(0) do
1662
+ company.client_ids
1663
+ company.client_ids
1664
+ end
1665
+ end
1666
+
1667
+ def test_get_ids_for_unloaded_associations_does_not_load_them
1668
+ company = companies(:first_firm)
1669
+ assert !company.clients.loaded?
1670
+ assert_equal [companies(:first_client).id, companies(:second_client).id, companies(:another_first_firm_client).id], company.client_ids
1671
+ assert !company.clients.loaded?
1672
+ end
1673
+
1674
+ def test_get_ids_ignores_include_option
1675
+ assert_equal [readers(:michael_welcome).id], posts(:welcome).readers_with_person_ids
1676
+ end
1677
+
1678
+ def test_get_ids_for_ordered_association
1679
+ assert_equal [companies(:another_first_firm_client).id, companies(:second_client).id, companies(:first_client).id], companies(:first_firm).clients_ordered_by_name_ids
1680
+ end
1681
+
1682
+ def test_get_ids_for_association_on_new_record_does_not_try_to_find_records
1683
+ Company.columns # Load schema information so we don't query below
1684
+ Contract.columns # if running just this test.
1685
+
1686
+ company = Company.new
1687
+ assert_queries(0) do
1688
+ company.contract_ids
1689
+ end
1690
+
1691
+ assert_equal [], company.contract_ids
1692
+ end
1693
+
1694
+ def test_set_ids_for_association_on_new_record_applies_association_correctly
1695
+ contract_a = Contract.create!
1696
+ contract_b = Contract.create!
1697
+ Contract.create! # another contract
1698
+ company = Company.new(:name => "Some Company")
1699
+
1700
+ company.contract_ids = [contract_a.id, contract_b.id]
1701
+ assert_equal [contract_a.id, contract_b.id], company.contract_ids
1702
+ assert_equal [contract_a, contract_b], company.contracts
1703
+
1704
+ company.save!
1705
+ assert_equal company, contract_a.reload.company
1706
+ assert_equal company, contract_b.reload.company
1707
+ end
1708
+
1709
+ def test_assign_ids_ignoring_blanks
1710
+ firm = Firm.create!(:name => 'Apple')
1711
+ firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
1712
+ firm.save!
1713
+
1714
+ assert_equal 2, firm.clients.reload.size
1715
+ assert_equal true, firm.clients.include?(companies(:second_client))
1716
+ end
1717
+
1718
+ def test_get_ids_for_through
1719
+ assert_equal [comments(:eager_other_comment1).id], authors(:mary).comment_ids
1720
+ end
1721
+
1722
+ def test_modifying_a_through_a_has_many_should_raise
1723
+ [
1724
+ lambda { authors(:mary).comment_ids = [comments(:greetings).id, comments(:more_greetings).id] },
1725
+ lambda { authors(:mary).comments = [comments(:greetings), comments(:more_greetings)] },
1726
+ lambda { authors(:mary).comments << Comment.create!(:body => "Yay", :post_id => 424242) },
1727
+ lambda { authors(:mary).comments.delete(authors(:mary).comments.first) },
1728
+ ].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }
1729
+ end
1730
+
1731
+ def test_dynamic_find_should_respect_association_order_for_through
1732
+ assert_equal Comment.find(10), authors(:david).comments_desc.where("comments.type = 'SpecialComment'").first
1733
+ assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type('SpecialComment')
1734
+ end
1735
+
1736
+ def test_has_many_through_respects_hash_conditions
1737
+ assert_equal authors(:david).hello_posts, authors(:david).hello_posts_with_hash_conditions
1738
+ assert_equal authors(:david).hello_post_comments, authors(:david).hello_post_comments_with_hash_conditions
1739
+ end
1740
+
1741
+ def test_include_uses_array_include_after_loaded
1742
+ firm = companies(:first_firm)
1743
+ firm.clients.load_target
1744
+
1745
+ client = firm.clients.first
1746
+
1747
+ assert_no_queries do
1748
+ assert firm.clients.loaded?
1749
+ assert_equal true, firm.clients.include?(client)
1750
+ end
1751
+ end
1752
+
1753
+ def test_include_checks_if_record_exists_if_target_not_loaded
1754
+ firm = companies(:first_firm)
1755
+ client = firm.clients.first
1756
+
1757
+ firm.reload
1758
+ assert ! firm.clients.loaded?
1759
+ assert_queries(1) do
1760
+ assert_equal true, firm.clients.include?(client)
1761
+ end
1762
+ assert ! firm.clients.loaded?
1763
+ end
1764
+
1765
+ def test_include_returns_false_for_non_matching_record_to_verify_scoping
1766
+ firm = companies(:first_firm)
1767
+ client = Client.create!(:name => 'Not Associated')
1768
+
1769
+ assert ! firm.clients.loaded?
1770
+ assert_equal false, firm.clients.include?(client)
1771
+ end
1772
+
1773
+ def test_calling_first_nth_or_last_on_association_should_not_load_association
1774
+ firm = companies(:first_firm)
1775
+ firm.clients.first
1776
+ firm.clients.second
1777
+ firm.clients.last
1778
+ assert !firm.clients.loaded?
1779
+ end
1780
+
1781
+ def test_calling_first_or_last_on_loaded_association_should_not_fetch_with_query
1782
+ firm = companies(:first_firm)
1783
+ firm.clients.load_target
1784
+ assert firm.clients.loaded?
1785
+
1786
+ assert_no_queries(ignore_none: false) do
1787
+ firm.clients.first
1788
+ assert_equal 2, firm.clients.first(2).size
1789
+ firm.clients.last
1790
+ assert_equal 2, firm.clients.last(2).size
1791
+ end
1792
+ end
1793
+
1794
+ def test_calling_first_or_last_on_existing_record_with_build_should_load_association
1795
+ firm = companies(:first_firm)
1796
+ firm.clients.build(:name => 'Foo')
1797
+ assert !firm.clients.loaded?
1798
+
1799
+ assert_queries 1 do
1800
+ firm.clients.first
1801
+ firm.clients.second
1802
+ firm.clients.last
1803
+ end
1804
+
1805
+ assert firm.clients.loaded?
1806
+ end
1807
+
1808
+ def test_calling_first_nth_or_last_on_existing_record_with_create_should_not_load_association
1809
+ firm = companies(:first_firm)
1810
+ firm.clients.create(:name => 'Foo')
1811
+ assert !firm.clients.loaded?
1812
+
1813
+ assert_queries 3 do
1814
+ firm.clients.first
1815
+ firm.clients.second
1816
+ firm.clients.last
1817
+ end
1818
+
1819
+ assert !firm.clients.loaded?
1820
+ end
1821
+
1822
+ def test_calling_first_nth_or_last_on_new_record_should_not_run_queries
1823
+ firm = Firm.new
1824
+
1825
+ assert_no_queries do
1826
+ firm.clients.first
1827
+ firm.clients.second
1828
+ firm.clients.last
1829
+ end
1830
+ end
1831
+
1832
+ def test_calling_first_or_last_with_integer_on_association_should_not_load_association
1833
+ firm = companies(:first_firm)
1834
+ firm.clients.create(:name => 'Foo')
1835
+ assert !firm.clients.loaded?
1836
+
1837
+ assert_queries 2 do
1838
+ firm.clients.first(2)
1839
+ firm.clients.last(2)
1840
+ end
1841
+
1842
+ assert !firm.clients.loaded?
1843
+ end
1844
+
1845
+ def test_calling_many_should_count_instead_of_loading_association
1846
+ firm = companies(:first_firm)
1847
+ assert_queries(1) do
1848
+ firm.clients.many? # use count query
1849
+ end
1850
+ assert !firm.clients.loaded?
1851
+ end
1852
+
1853
+ def test_calling_many_on_loaded_association_should_not_use_query
1854
+ firm = companies(:first_firm)
1855
+ firm.clients.collect # force load
1856
+ assert_no_queries { assert firm.clients.many? }
1857
+ end
1858
+
1859
+ def test_calling_many_should_defer_to_collection_if_using_a_block
1860
+ firm = companies(:first_firm)
1861
+ assert_queries(1) do
1862
+ firm.clients.expects(:size).never
1863
+ firm.clients.many? { true }
1864
+ end
1865
+ assert firm.clients.loaded?
1866
+ end
1867
+
1868
+ def test_calling_many_should_return_false_if_none_or_one
1869
+ firm = companies(:another_firm)
1870
+ assert !firm.clients_like_ms.many?
1871
+ assert_equal 0, firm.clients_like_ms.size
1872
+
1873
+ firm = companies(:first_firm)
1874
+ assert !firm.limited_clients.many?
1875
+ assert_equal 1, firm.limited_clients.size
1876
+ end
1877
+
1878
+ def test_calling_many_should_return_true_if_more_than_one
1879
+ firm = companies(:first_firm)
1880
+ assert firm.clients.many?
1881
+ assert_equal 3, firm.clients.size
1882
+ end
1883
+
1884
+ def test_calling_none_should_count_instead_of_loading_association
1885
+ firm = companies(:first_firm)
1886
+ assert_queries(1) do
1887
+ firm.clients.none? # use count query
1888
+ end
1889
+ assert !firm.clients.loaded?
1890
+ end
1891
+
1892
+ def test_calling_none_on_loaded_association_should_not_use_query
1893
+ firm = companies(:first_firm)
1894
+ firm.clients.collect # force load
1895
+ assert_no_queries { assert ! firm.clients.none? }
1896
+ end
1897
+
1898
+ def test_calling_none_should_defer_to_collection_if_using_a_block
1899
+ firm = companies(:first_firm)
1900
+ assert_queries(1) do
1901
+ firm.clients.expects(:size).never
1902
+ firm.clients.none? { true }
1903
+ end
1904
+ assert firm.clients.loaded?
1905
+ end
1906
+
1907
+ def test_calling_none_should_return_true_if_none
1908
+ firm = companies(:another_firm)
1909
+ assert firm.clients_like_ms.none?
1910
+ assert_equal 0, firm.clients_like_ms.size
1911
+ end
1912
+
1913
+ def test_calling_none_should_return_false_if_any
1914
+ firm = companies(:first_firm)
1915
+ assert !firm.limited_clients.none?
1916
+ assert_equal 1, firm.limited_clients.size
1917
+ end
1918
+
1919
+ def test_calling_one_should_count_instead_of_loading_association
1920
+ firm = companies(:first_firm)
1921
+ assert_queries(1) do
1922
+ firm.clients.one? # use count query
1923
+ end
1924
+ assert !firm.clients.loaded?
1925
+ end
1926
+
1927
+ def test_calling_one_on_loaded_association_should_not_use_query
1928
+ firm = companies(:first_firm)
1929
+ firm.clients.collect # force load
1930
+ assert_no_queries { assert ! firm.clients.one? }
1931
+ end
1932
+
1933
+ def test_calling_one_should_defer_to_collection_if_using_a_block
1934
+ firm = companies(:first_firm)
1935
+ assert_queries(1) do
1936
+ firm.clients.expects(:size).never
1937
+ firm.clients.one? { true }
1938
+ end
1939
+ assert firm.clients.loaded?
1940
+ end
1941
+
1942
+ def test_calling_one_should_return_false_if_zero
1943
+ firm = companies(:another_firm)
1944
+ assert ! firm.clients_like_ms.one?
1945
+ assert_equal 0, firm.clients_like_ms.size
1946
+ end
1947
+
1948
+ def test_calling_one_should_return_true_if_one
1949
+ firm = companies(:first_firm)
1950
+ assert firm.limited_clients.one?
1951
+ assert_equal 1, firm.limited_clients.size
1952
+ end
1953
+
1954
+ def test_calling_one_should_return_false_if_more_than_one
1955
+ firm = companies(:first_firm)
1956
+ assert ! firm.clients.one?
1957
+ assert_equal 3, firm.clients.size
1958
+ end
1959
+
1960
+ def test_joins_with_namespaced_model_should_use_correct_type
1961
+ old = ActiveRecord::Base.store_full_sti_class
1962
+ ActiveRecord::Base.store_full_sti_class = true
1963
+
1964
+ firm = Namespaced::Firm.create({ :name => 'Some Company' })
1965
+ firm.clients.create({ :name => 'Some Client' })
1966
+
1967
+ stats = Namespaced::Firm.all.merge!(
1968
+ :select => "#{Namespaced::Firm.table_name}.id, COUNT(#{Namespaced::Client.table_name}.id) AS num_clients",
1969
+ :joins => :clients,
1970
+ :group => "#{Namespaced::Firm.table_name}.id"
1971
+ ).find firm.id
1972
+ assert_equal 1, stats.num_clients.to_i
1973
+ ensure
1974
+ ActiveRecord::Base.store_full_sti_class = old
1975
+ end
1976
+
1977
+ def test_association_proxy_transaction_method_starts_transaction_in_association_class
1978
+ Comment.expects(:transaction)
1979
+ Post.first.comments.transaction do
1980
+ # nothing
1981
+ end
1982
+ end
1983
+
1984
+ def test_sending_new_to_association_proxy_should_have_same_effect_as_calling_new
1985
+ client_association = companies(:first_firm).clients
1986
+ assert_equal client_association.new.attributes, client_association.send(:new).attributes
1987
+ end
1988
+
1989
+ def test_respond_to_private_class_methods
1990
+ client_association = companies(:first_firm).clients
1991
+ assert !client_association.respond_to?(:private_method)
1992
+ assert client_association.respond_to?(:private_method, true)
1993
+ end
1994
+
1995
+ def test_creating_using_primary_key
1996
+ firm = Firm.all.merge!(:order => "id").first
1997
+ client = firm.clients_using_primary_key.create!(:name => 'test')
1998
+ assert_equal firm.name, client.firm_name
1999
+ end
2000
+
2001
+ def test_defining_has_many_association_with_delete_all_dependency_lazily_evaluates_target_class
2002
+ ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
2003
+ class_eval(<<-EOF, __FILE__, __LINE__ + 1)
2004
+ class DeleteAllModel < ActiveRecord::Base
2005
+ has_many :nonentities, :dependent => :delete_all
2006
+ end
2007
+ EOF
2008
+ end
2009
+
2010
+ def test_defining_has_many_association_with_nullify_dependency_lazily_evaluates_target_class
2011
+ ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
2012
+ class_eval(<<-EOF, __FILE__, __LINE__ + 1)
2013
+ class NullifyModel < ActiveRecord::Base
2014
+ has_many :nonentities, :dependent => :nullify
2015
+ end
2016
+ EOF
2017
+ end
2018
+
2019
+ def test_attributes_are_being_set_when_initialized_from_has_many_association_with_where_clause
2020
+ new_comment = posts(:welcome).comments.where(:body => "Some content").build
2021
+ assert_equal new_comment.body, "Some content"
2022
+ end
2023
+
2024
+ def test_attributes_are_being_set_when_initialized_from_has_many_association_with_multiple_where_clauses
2025
+ new_comment = posts(:welcome).comments.where(:body => "Some content").where(:type => 'SpecialComment').build
2026
+ assert_equal new_comment.body, "Some content"
2027
+ assert_equal new_comment.type, "SpecialComment"
2028
+ assert_equal new_comment.post_id, posts(:welcome).id
2029
+ end
2030
+
2031
+ def test_include_method_in_has_many_association_should_return_true_for_instance_added_with_build
2032
+ post = Post.new
2033
+ comment = post.comments.build
2034
+ assert_equal true, post.comments.include?(comment)
2035
+ end
2036
+
2037
+ def test_load_target_respects_protected_attributes
2038
+ topic = Topic.create!
2039
+ reply = topic.replies.create(:title => "reply 1")
2040
+ reply.approved = false
2041
+ reply.save!
2042
+
2043
+ # Save with a different object instance, so the instance that's still held
2044
+ # in topic.relies doesn't know about the changed attribute.
2045
+ reply2 = Reply.find(reply.id)
2046
+ reply2.approved = true
2047
+ reply2.save!
2048
+
2049
+ # Force loading the collection from the db. This will merge the existing
2050
+ # object (reply) with what gets loaded from the db (which includes the
2051
+ # changed approved attribute). approved is a protected attribute, so if mass
2052
+ # assignment is used, it won't get updated and will still be false.
2053
+ first = topic.replies.to_a.first
2054
+ assert_equal reply.id, first.id
2055
+ assert_equal true, first.approved?
2056
+ end
2057
+
2058
+ def test_to_a_should_dup_target
2059
+ ary = topics(:first).replies.to_a
2060
+ target = topics(:first).replies.target
2061
+
2062
+ assert_not_equal target.object_id, ary.object_id
2063
+ end
2064
+
2065
+ def test_merging_with_custom_attribute_writer
2066
+ bulb = Bulb.new(:color => "red")
2067
+ assert_equal "RED!", bulb.color
2068
+
2069
+ car = Car.create!
2070
+ car.bulbs << bulb
2071
+
2072
+ assert_equal "RED!", car.bulbs.to_a.first.color
2073
+ end
2074
+
2075
+ def test_abstract_class_with_polymorphic_has_many
2076
+ post = SubStiPost.create! :title => "fooo", :body => "baa"
2077
+ tagging = Tagging.create! :taggable => post
2078
+ assert_equal [tagging], post.taggings
2079
+ end
2080
+
2081
+ def test_with_polymorphic_has_many_with_custom_columns_name
2082
+ post = Post.create! :title => 'foo', :body => 'bar'
2083
+ image = Image.create!
2084
+
2085
+ post.images << image
2086
+
2087
+ assert_equal [image], post.images
2088
+ end
2089
+
2090
+ def test_build_with_polymorphic_has_many_does_not_allow_to_override_type_and_id
2091
+ welcome = posts(:welcome)
2092
+ tagging = welcome.taggings.build(:taggable_id => 99, :taggable_type => 'ShouldNotChange')
2093
+
2094
+ assert_equal welcome.id, tagging.taggable_id
2095
+ assert_equal 'Post', tagging.taggable_type
2096
+ end
2097
+
2098
+ def test_dont_call_save_callbacks_twice_on_has_many
2099
+ firm = companies(:first_firm)
2100
+ contract = firm.contracts.create!
2101
+
2102
+ assert_equal 1, contract.hi_count
2103
+ assert_equal 1, contract.bye_count
2104
+ end
2105
+
2106
+ def test_association_attributes_are_available_to_after_initialize
2107
+ car = Car.create(:name => 'honda')
2108
+ bulb = car.bulbs.build
2109
+
2110
+ assert_equal car.id, bulb.attributes_after_initialize['car_id']
2111
+ end
2112
+
2113
+ def test_attributes_are_set_when_initialized_from_has_many_null_relationship
2114
+ car = Car.new name: 'honda'
2115
+ bulb = car.bulbs.where(name: 'headlight').first_or_initialize
2116
+ assert_equal 'headlight', bulb.name
2117
+ end
2118
+
2119
+ def test_attributes_are_set_when_initialized_from_polymorphic_has_many_null_relationship
2120
+ post = Post.new title: 'title', body: 'bar'
2121
+ tag = Tag.create!(name: 'foo')
2122
+
2123
+ tagging = post.taggings.where(tag: tag).first_or_initialize
2124
+
2125
+ assert_equal tag.id, tagging.tag_id
2126
+ assert_equal 'Post', tagging.taggable_type
2127
+ end
2128
+
2129
+ def test_replace
2130
+ car = Car.create(:name => 'honda')
2131
+ bulb1 = car.bulbs.create
2132
+ bulb2 = Bulb.create
2133
+
2134
+ assert_equal [bulb1], car.bulbs
2135
+ car.bulbs.replace([bulb2])
2136
+ assert_equal [bulb2], car.bulbs
2137
+ assert_equal [bulb2], car.reload.bulbs
2138
+ end
2139
+
2140
+ def test_replace_returns_target
2141
+ car = Car.create(:name => 'honda')
2142
+ bulb1 = car.bulbs.create
2143
+ bulb2 = car.bulbs.create
2144
+ bulb3 = Bulb.create
2145
+
2146
+ assert_equal [bulb1, bulb2], car.bulbs
2147
+ result = car.bulbs.replace([bulb3, bulb1])
2148
+ assert_equal [bulb1, bulb3], car.bulbs
2149
+ assert_equal [bulb1, bulb3], result
2150
+ end
2151
+
2152
+ def test_collection_association_with_private_kernel_method
2153
+ firm = companies(:first_firm)
2154
+ assert_equal [accounts(:signals37)], firm.accounts.open
2155
+ end
2156
+
2157
+ test "first_or_initialize adds the record to the association" do
2158
+ firm = Firm.create! name: 'omg'
2159
+ client = firm.clients_of_firm.first_or_initialize
2160
+ assert_equal [client], firm.clients_of_firm
2161
+ end
2162
+
2163
+ test "first_or_create adds the record to the association" do
2164
+ firm = Firm.create! name: 'omg'
2165
+ firm.clients_of_firm.load_target
2166
+ client = firm.clients_of_firm.first_or_create name: 'lol'
2167
+ assert_equal [client], firm.clients_of_firm
2168
+ assert_equal [client], firm.reload.clients_of_firm
2169
+ end
2170
+
2171
+ test "delete_all, when not loaded, doesn't load the records" do
2172
+ post = posts(:welcome)
2173
+
2174
+ assert post.taggings_with_delete_all.count > 0
2175
+ assert !post.taggings_with_delete_all.loaded?
2176
+
2177
+ # 2 queries: one DELETE and another to update the counter cache
2178
+ assert_queries(2) do
2179
+ post.taggings_with_delete_all.delete_all
2180
+ end
2181
+ end
2182
+
2183
+ test "has many associations on new records use null relations" do
2184
+ post = Post.new
2185
+
2186
+ assert_no_queries(ignore_none: false) do
2187
+ assert_equal [], post.comments
2188
+ assert_equal [], post.comments.where(body: 'omg')
2189
+ assert_equal [], post.comments.pluck(:body)
2190
+ assert_equal 0, post.comments.sum(:id)
2191
+ assert_equal 0, post.comments.count
2192
+ end
2193
+ end
2194
+
2195
+ test "collection proxy respects default scope" do
2196
+ author = authors(:mary)
2197
+ assert !author.first_posts.exists?
2198
+ end
2199
+
2200
+ test "association with extend option" do
2201
+ post = posts(:welcome)
2202
+ assert_equal "lifo", post.comments_with_extend.author
2203
+ assert_equal "hello", post.comments_with_extend.greeting
2204
+ end
2205
+
2206
+ test "association with extend option with multiple extensions" do
2207
+ post = posts(:welcome)
2208
+ assert_equal "lifo", post.comments_with_extend_2.author
2209
+ assert_equal "hullo", post.comments_with_extend_2.greeting
2210
+ end
2211
+
2212
+ test "extend option affects per association" do
2213
+ post = posts(:welcome)
2214
+ assert_equal "lifo", post.comments_with_extend.author
2215
+ assert_equal "lifo", post.comments_with_extend_2.author
2216
+ assert_equal "hello", post.comments_with_extend.greeting
2217
+ assert_equal "hullo", post.comments_with_extend_2.greeting
2218
+ end
2219
+
2220
+ test "delete record with complex joins" do
2221
+ david = authors(:david)
2222
+
2223
+ post = david.posts.first
2224
+ post.type = 'PostWithSpecialCategorization'
2225
+ post.save
2226
+
2227
+ categorization = post.categorizations.first
2228
+ categorization.special = true
2229
+ categorization.save
2230
+
2231
+ assert_not_equal [], david.posts_with_special_categorizations
2232
+ david.posts_with_special_categorizations = []
2233
+ assert_equal [], david.posts_with_special_categorizations
2234
+ end
2235
+
2236
+ test "does not duplicate associations when used with natural primary keys" do
2237
+ speedometer = Speedometer.create!(id: '4')
2238
+ speedometer.minivans.create!(minivan_id: 'a-van-red' ,name: 'a van', color: 'red')
2239
+
2240
+ assert_equal 1, speedometer.minivans.to_a.size, "Only one association should be present:\n#{speedometer.minivans.to_a}"
2241
+ assert_equal 1, speedometer.reload.minivans.to_a.size
2242
+ end
2243
+
2244
+ test "can unscope the default scope of the associated model" do
2245
+ car = Car.create!
2246
+ bulb1 = Bulb.create! name: "defaulty", car: car
2247
+ bulb2 = Bulb.create! name: "other", car: car
2248
+
2249
+ assert_equal [bulb1], car.bulbs
2250
+ assert_equal [bulb1, bulb2], car.all_bulbs.sort_by(&:id)
2251
+ end
2252
+
2253
+ test "can unscope and where the default scope of the associated model" do
2254
+ Car.has_many :other_bulbs, -> { unscope(where: [:name]).where(name: 'other') }, class_name: "Bulb"
2255
+ car = Car.create!
2256
+ bulb1 = Bulb.create! name: "defaulty", car: car
2257
+ bulb2 = Bulb.create! name: "other", car: car
2258
+
2259
+ assert_equal [bulb1], car.bulbs
2260
+ assert_equal [bulb2], car.other_bulbs
2261
+ end
2262
+
2263
+ test "can rewhere the default scope of the associated model" do
2264
+ Car.has_many :old_bulbs, -> { rewhere(name: 'old') }, class_name: "Bulb"
2265
+ car = Car.create!
2266
+ bulb1 = Bulb.create! name: "defaulty", car: car
2267
+ bulb2 = Bulb.create! name: "old", car: car
2268
+
2269
+ assert_equal [bulb1], car.bulbs
2270
+ assert_equal [bulb2], car.old_bulbs
2271
+ end
2272
+
2273
+ test 'unscopes the default scope of associated model when used with include' do
2274
+ car = Car.create!
2275
+ bulb = Bulb.create! name: "other", car: car
2276
+
2277
+ assert_equal [bulb], Car.find(car.id).all_bulbs
2278
+ assert_equal [bulb], Car.includes(:all_bulbs).find(car.id).all_bulbs
2279
+ assert_equal [bulb], Car.eager_load(:all_bulbs).find(car.id).all_bulbs
2280
+ end
2281
+
2282
+ test "raises RecordNotDestroyed when replaced child can't be destroyed" do
2283
+ car = Car.create!
2284
+ original_child = FailedBulb.create!(car: car)
2285
+
2286
+ error = assert_raise(ActiveRecord::RecordNotDestroyed) do
2287
+ car.failed_bulbs = [FailedBulb.create!]
2288
+ end
2289
+
2290
+ assert_equal [original_child], car.reload.failed_bulbs
2291
+ assert_equal "Failed to destroy the record", error.message
2292
+ end
2293
+
2294
+ test 'updates counter cache when default scope is given' do
2295
+ topic = DefaultRejectedTopic.create approved: true
2296
+
2297
+ assert_difference "topic.reload.replies_count", 1 do
2298
+ topic.approved_replies.create!
2299
+ end
2300
+ end
2301
+
2302
+ test 'dangerous association name raises ArgumentError' do
2303
+ [:errors, 'errors', :save, 'save'].each do |name|
2304
+ assert_raises(ArgumentError, "Association #{name} should not be allowed") do
2305
+ Class.new(ActiveRecord::Base) do
2306
+ has_many name
2307
+ end
2308
+ end
2309
+ end
2310
+ end
2311
+
2312
+ test 'passes custom context validation to validate children' do
2313
+ pirate = FamousPirate.new
2314
+ pirate.famous_ships << ship = FamousShip.new
2315
+
2316
+ assert pirate.valid?
2317
+ assert_not pirate.valid?(:conference)
2318
+ assert_equal "can't be blank", ship.errors[:name].first
2319
+ end
2320
+
2321
+ test 'association with instance dependent scope' do
2322
+ bob = authors(:bob)
2323
+ Post.create!(title: "signed post by bob", body: "stuff", author: authors(:bob))
2324
+ Post.create!(title: "anonymous post", body: "more stuff", author: authors(:bob))
2325
+ assert_equal ["misc post by bob", "other post by bob",
2326
+ "signed post by bob"], bob.posts_with_signature.map(&:title).sort
2327
+
2328
+ assert_equal [], authors(:david).posts_with_signature.map(&:title)
2329
+ end
2330
+
2331
+ test 'associations autosaves when object is already persisted' do
2332
+ bulb = Bulb.create!
2333
+ tyre = Tyre.create!
2334
+
2335
+ car = Car.create! do |c|
2336
+ c.bulbs << bulb
2337
+ c.tyres << tyre
2338
+ end
2339
+
2340
+ assert_equal 1, car.bulbs.count
2341
+ assert_equal 1, car.tyres.count
2342
+ end
2343
+
2344
+ test 'associations replace in memory when records have the same id' do
2345
+ bulb = Bulb.create!
2346
+ car = Car.create!(bulbs: [bulb])
2347
+
2348
+ new_bulb = Bulb.find(bulb.id)
2349
+ new_bulb.name = "foo"
2350
+ car.bulbs = [new_bulb]
2351
+
2352
+ assert_equal "foo", car.bulbs.first.name
2353
+ end
2354
+
2355
+ test 'in memory replacement executes no queries' do
2356
+ bulb = Bulb.create!
2357
+ car = Car.create!(bulbs: [bulb])
2358
+
2359
+ new_bulb = Bulb.find(bulb.id)
2360
+
2361
+ assert_no_queries do
2362
+ car.bulbs = [new_bulb]
2363
+ end
2364
+ end
2365
+
2366
+ test 'in memory replacements do not execute callbacks' do
2367
+ raise_after_add = false
2368
+ klass = Class.new(ActiveRecord::Base) do
2369
+ self.table_name = :cars
2370
+ has_many :bulbs, after_add: proc { raise if raise_after_add }
2371
+
2372
+ def self.name
2373
+ "Car"
2374
+ end
2375
+ end
2376
+ bulb = Bulb.create!
2377
+ car = klass.create!(bulbs: [bulb])
2378
+
2379
+ new_bulb = Bulb.find(bulb.id)
2380
+ raise_after_add = true
2381
+
2382
+ assert_nothing_raised do
2383
+ car.bulbs = [new_bulb]
2384
+ end
2385
+ end
2386
+
2387
+ test 'in memory replacements sets inverse instance' do
2388
+ bulb = Bulb.create!
2389
+ car = Car.create!(bulbs: [bulb])
2390
+
2391
+ new_bulb = Bulb.find(bulb.id)
2392
+ car.bulbs = [new_bulb]
2393
+
2394
+ assert_same car, new_bulb.car
2395
+ end
2396
+
2397
+ test 'in memory replacement maintains order' do
2398
+ first_bulb = Bulb.create!
2399
+ second_bulb = Bulb.create!
2400
+ car = Car.create!(bulbs: [first_bulb, second_bulb])
2401
+
2402
+ same_bulb = Bulb.find(first_bulb.id)
2403
+ car.bulbs = [second_bulb, same_bulb]
2404
+
2405
+ assert_equal [first_bulb, second_bulb], car.bulbs
2406
+ end
2407
+
2408
+ test "prevent double insertion of new object when the parent association loaded in the after save callback" do
2409
+ reset_callbacks(:save, Bulb) do
2410
+ Bulb.after_save { |record| record.car.bulbs.load }
2411
+
2412
+ car = Car.create!
2413
+ car.bulbs << Bulb.new
2414
+
2415
+ assert_equal 1, car.bulbs.size
2416
+ end
2417
+ end
2418
+
2419
+ test "prevent double firing the before save callback of new object when the parent association saved in the callback" do
2420
+ reset_callbacks(:save, Bulb) do
2421
+ count = 0
2422
+ Bulb.before_save { |record| record.car.save && count += 1 }
2423
+
2424
+ car = Car.create!
2425
+ car.bulbs.create!
2426
+
2427
+ assert_equal 1, count
2428
+ end
2429
+ end
2430
+
2431
+ def test_association_force_reload_with_only_true_is_deprecated
2432
+ company = Company.find(1)
2433
+
2434
+ assert_deprecated { company.clients_of_firm(true) }
2435
+ end
2436
+
2437
+ class AuthorWithErrorDestroyingAssociation < ActiveRecord::Base
2438
+ self.table_name = "authors"
2439
+ has_many :posts_with_error_destroying,
2440
+ class_name: "PostWithErrorDestroying",
2441
+ foreign_key: :author_id,
2442
+ dependent: :destroy
2443
+ end
2444
+
2445
+ class PostWithErrorDestroying < ActiveRecord::Base
2446
+ self.table_name = "posts"
2447
+ self.inheritance_column = nil
2448
+ before_destroy -> { throw :abort }
2449
+ end
2450
+
2451
+ def test_destroy_does_not_raise_when_association_errors_on_destroy
2452
+ assert_no_difference "AuthorWithErrorDestroyingAssociation.count" do
2453
+ author = AuthorWithErrorDestroyingAssociation.first
2454
+
2455
+ assert_not author.destroy
2456
+ end
2457
+ end
2458
+
2459
+ def test_destroy_with_bang_bubbles_errors_from_associations
2460
+ error = assert_raises ActiveRecord::RecordNotDestroyed do
2461
+ AuthorWithErrorDestroyingAssociation.first.destroy!
2462
+ end
2463
+
2464
+ assert_instance_of PostWithErrorDestroying, error.record
2465
+ end
2466
+
2467
+ def test_ids_reader_memoization
2468
+ car = Car.create!(name: 'Tofaş')
2469
+ bulb = Bulb.create!(car: car)
2470
+
2471
+ assert_equal [bulb.id], car.bulb_ids
2472
+ assert_no_queries { car.bulb_ids }
2473
+ end
2474
+
2475
+ def test_loading_association_in_validate_callback_doesnt_affect_persistence
2476
+ reset_callbacks(:validation, Bulb) do
2477
+ Bulb.after_validation { |record| record.car.bulbs.load }
2478
+
2479
+ car = Car.create!(name: "Car")
2480
+ bulb = car.bulbs.create!
2481
+
2482
+ assert_equal [bulb], car.bulbs
2483
+ end
2484
+ end
2485
+
2486
+ private
2487
+
2488
+ def reset_callbacks(kind, klass)
2489
+ old_callbacks = {}
2490
+ old_callbacks[klass] = klass.send("_#{kind}_callbacks").dup
2491
+ klass.subclasses.each do |subclass|
2492
+ old_callbacks[subclass] = subclass.send("_#{kind}_callbacks").dup
2493
+ end
2494
+ yield
2495
+ ensure
2496
+ klass.send("_#{kind}_callbacks=", old_callbacks[klass])
2497
+ klass.subclasses.each do |subclass|
2498
+ subclass.send("_#{kind}_callbacks=", old_callbacks[subclass])
2499
+ end
2500
+ end
2501
+ end