ibm_db 5.6.1-arm64-darwin-24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (753) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES +299 -0
  3. data/LICENSE +55 -0
  4. data/MANIFEST +14 -0
  5. data/ParameterizedQueries README +39 -0
  6. data/README +210 -0
  7. data/ext/Makefile +270 -0
  8. data/ext/Makefile.nt32 +181 -0
  9. data/ext/Makefile.nt32.191 +212 -0
  10. data/ext/extconf.rb +320 -0
  11. data/ext/gil_release_version.h +3 -0
  12. data/ext/ibm_db.bundle +0 -0
  13. data/ext/ibm_db.c +11865 -0
  14. data/ext/ibm_db.o +0 -0
  15. data/ext/mkmf.log +98 -0
  16. data/ext/ruby_ibm_db.h +241 -0
  17. data/ext/ruby_ibm_db_cli.c +867 -0
  18. data/ext/ruby_ibm_db_cli.h +508 -0
  19. data/ext/ruby_ibm_db_cli.o +0 -0
  20. data/ext/unicode_support_version.h +3 -0
  21. data/init.rb +42 -0
  22. data/lib/IBM_DB.rb +27 -0
  23. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +4407 -0
  24. data/lib/active_record/connection_adapters/ibm_db_pstmt.rb +1965 -0
  25. data/lib/active_record/connection_adapters/ibmdb_adapter.rb +5 -0
  26. data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -0
  27. data/lib/ibm_db.bundle +0 -0
  28. data/test/active_record/connection_adapters/fake_adapter.rb +52 -0
  29. data/test/activejob/destroy_association_async_test.rb +305 -0
  30. data/test/activejob/destroy_async_job_not_present_test.rb +31 -0
  31. data/test/activejob/helper.rb +15 -0
  32. data/test/assets/example.log +1 -0
  33. data/test/assets/flowers.jpg +0 -0
  34. data/test/assets/schema_dump_5_1.yml +345 -0
  35. data/test/assets/test.txt +1 -0
  36. data/test/cases/adapter_prevent_writes_test.rb +334 -0
  37. data/test/cases/adapter_test.rb +565 -0
  38. data/test/cases/adapters/mysql2/active_schema_test.rb +203 -0
  39. data/test/cases/adapters/mysql2/auto_increment_test.rb +34 -0
  40. data/test/cases/adapters/mysql2/bind_parameter_test.rb +52 -0
  41. data/test/cases/adapters/mysql2/boolean_test.rb +102 -0
  42. data/test/cases/adapters/mysql2/case_sensitivity_test.rb +65 -0
  43. data/test/cases/adapters/mysql2/charset_collation_test.rb +57 -0
  44. data/test/cases/adapters/mysql2/connection_test.rb +208 -0
  45. data/test/cases/adapters/mysql2/count_deleted_rows_with_lock_test.rb +28 -0
  46. data/test/cases/adapters/mysql2/datetime_precision_quoting_test.rb +49 -0
  47. data/test/cases/adapters/mysql2/enum_test.rb +47 -0
  48. data/test/cases/adapters/mysql2/explain_test.rb +23 -0
  49. data/test/cases/adapters/mysql2/json_test.rb +24 -0
  50. data/test/cases/adapters/mysql2/mysql2_adapter_prevent_writes_test.rb +208 -0
  51. data/test/cases/adapters/mysql2/mysql2_adapter_test.rb +238 -0
  52. data/test/cases/adapters/mysql2/nested_deadlock_test.rb +75 -0
  53. data/test/cases/adapters/mysql2/optimizer_hints_test.rb +69 -0
  54. data/test/cases/adapters/mysql2/reserved_word_test.rb +152 -0
  55. data/test/cases/adapters/mysql2/schema_migrations_test.rb +64 -0
  56. data/test/cases/adapters/mysql2/schema_test.rb +128 -0
  57. data/test/cases/adapters/mysql2/set_test.rb +32 -0
  58. data/test/cases/adapters/mysql2/sp_test.rb +38 -0
  59. data/test/cases/adapters/mysql2/sql_types_test.rb +16 -0
  60. data/test/cases/adapters/mysql2/table_options_test.rb +125 -0
  61. data/test/cases/adapters/mysql2/transaction_test.rb +151 -0
  62. data/test/cases/adapters/mysql2/unsigned_type_test.rb +68 -0
  63. data/test/cases/adapters/mysql2/virtual_column_test.rb +66 -0
  64. data/test/cases/adapters/postgresql/active_schema_test.rb +113 -0
  65. data/test/cases/adapters/postgresql/array_test.rb +394 -0
  66. data/test/cases/adapters/postgresql/bit_string_test.rb +84 -0
  67. data/test/cases/adapters/postgresql/bytea_test.rb +135 -0
  68. data/test/cases/adapters/postgresql/case_insensitive_test.rb +27 -0
  69. data/test/cases/adapters/postgresql/change_schema_test.rb +40 -0
  70. data/test/cases/adapters/postgresql/cidr_test.rb +27 -0
  71. data/test/cases/adapters/postgresql/citext_test.rb +78 -0
  72. data/test/cases/adapters/postgresql/collation_test.rb +55 -0
  73. data/test/cases/adapters/postgresql/composite_test.rb +134 -0
  74. data/test/cases/adapters/postgresql/connection_test.rb +245 -0
  75. data/test/cases/adapters/postgresql/create_unlogged_tables_test.rb +74 -0
  76. data/test/cases/adapters/postgresql/datatype_test.rb +89 -0
  77. data/test/cases/adapters/postgresql/date_test.rb +42 -0
  78. data/test/cases/adapters/postgresql/domain_test.rb +49 -0
  79. data/test/cases/adapters/postgresql/enum_test.rb +93 -0
  80. data/test/cases/adapters/postgresql/explain_test.rb +22 -0
  81. data/test/cases/adapters/postgresql/extension_migration_test.rb +64 -0
  82. data/test/cases/adapters/postgresql/foreign_table_test.rb +109 -0
  83. data/test/cases/adapters/postgresql/full_text_test.rb +46 -0
  84. data/test/cases/adapters/postgresql/geometric_test.rb +372 -0
  85. data/test/cases/adapters/postgresql/hstore_test.rb +390 -0
  86. data/test/cases/adapters/postgresql/infinity_test.rb +108 -0
  87. data/test/cases/adapters/postgresql/integer_test.rb +27 -0
  88. data/test/cases/adapters/postgresql/interval_test.rb +99 -0
  89. data/test/cases/adapters/postgresql/json_test.rb +52 -0
  90. data/test/cases/adapters/postgresql/ltree_test.rb +51 -0
  91. data/test/cases/adapters/postgresql/money_test.rb +127 -0
  92. data/test/cases/adapters/postgresql/network_test.rb +102 -0
  93. data/test/cases/adapters/postgresql/numbers_test.rb +51 -0
  94. data/test/cases/adapters/postgresql/optimizer_hints_test.rb +71 -0
  95. data/test/cases/adapters/postgresql/partitions_test.rb +22 -0
  96. data/test/cases/adapters/postgresql/postgresql_adapter_prevent_writes_test.rb +205 -0
  97. data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +447 -0
  98. data/test/cases/adapters/postgresql/prepared_statements_disabled_test.rb +27 -0
  99. data/test/cases/adapters/postgresql/prepared_statements_test.rb +22 -0
  100. data/test/cases/adapters/postgresql/quoting_test.rb +50 -0
  101. data/test/cases/adapters/postgresql/range_test.rb +457 -0
  102. data/test/cases/adapters/postgresql/referential_integrity_test.rb +112 -0
  103. data/test/cases/adapters/postgresql/rename_table_test.rb +35 -0
  104. data/test/cases/adapters/postgresql/schema_authorization_test.rb +110 -0
  105. data/test/cases/adapters/postgresql/schema_test.rb +713 -0
  106. data/test/cases/adapters/postgresql/serial_test.rb +156 -0
  107. data/test/cases/adapters/postgresql/statement_pool_test.rb +61 -0
  108. data/test/cases/adapters/postgresql/timestamp_test.rb +92 -0
  109. data/test/cases/adapters/postgresql/transaction_nested_test.rb +114 -0
  110. data/test/cases/adapters/postgresql/transaction_test.rb +189 -0
  111. data/test/cases/adapters/postgresql/type_lookup_test.rb +35 -0
  112. data/test/cases/adapters/postgresql/utils_test.rb +64 -0
  113. data/test/cases/adapters/postgresql/uuid_test.rb +411 -0
  114. data/test/cases/adapters/postgresql/xml_test.rb +50 -0
  115. data/test/cases/adapters/sqlite3/collation_test.rb +64 -0
  116. data/test/cases/adapters/sqlite3/copy_table_test.rb +101 -0
  117. data/test/cases/adapters/sqlite3/explain_test.rb +23 -0
  118. data/test/cases/adapters/sqlite3/json_test.rb +29 -0
  119. data/test/cases/adapters/sqlite3/quoting_test.rb +79 -0
  120. data/test/cases/adapters/sqlite3/sqlite3_adapter_prevent_writes_test.rb +186 -0
  121. data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +628 -0
  122. data/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb +24 -0
  123. data/test/cases/adapters/sqlite3/statement_pool_test.rb +21 -0
  124. data/test/cases/adapters/sqlite3/transaction_test.rb +123 -0
  125. data/test/cases/aggregations_test.rb +170 -0
  126. data/test/cases/annotate_test.rb +46 -0
  127. data/test/cases/ar_schema_test.rb +213 -0
  128. data/test/cases/arel/attributes/attribute_test.rb +1145 -0
  129. data/test/cases/arel/attributes/math_test.rb +83 -0
  130. data/test/cases/arel/attributes_test.rb +27 -0
  131. data/test/cases/arel/collectors/bind_test.rb +40 -0
  132. data/test/cases/arel/collectors/composite_test.rb +47 -0
  133. data/test/cases/arel/collectors/sql_string_test.rb +41 -0
  134. data/test/cases/arel/collectors/substitute_bind_collector_test.rb +48 -0
  135. data/test/cases/arel/crud_test.rb +65 -0
  136. data/test/cases/arel/delete_manager_test.rb +53 -0
  137. data/test/cases/arel/factory_methods_test.rb +46 -0
  138. data/test/cases/arel/helper.rb +45 -0
  139. data/test/cases/arel/insert_manager_test.rb +241 -0
  140. data/test/cases/arel/nodes/and_test.rb +30 -0
  141. data/test/cases/arel/nodes/as_test.rb +36 -0
  142. data/test/cases/arel/nodes/ascending_test.rb +46 -0
  143. data/test/cases/arel/nodes/bin_test.rb +35 -0
  144. data/test/cases/arel/nodes/binary_test.rb +29 -0
  145. data/test/cases/arel/nodes/bind_param_test.rb +22 -0
  146. data/test/cases/arel/nodes/case_test.rb +96 -0
  147. data/test/cases/arel/nodes/casted_test.rb +18 -0
  148. data/test/cases/arel/nodes/comment_test.rb +22 -0
  149. data/test/cases/arel/nodes/count_test.rb +35 -0
  150. data/test/cases/arel/nodes/delete_statement_test.rb +36 -0
  151. data/test/cases/arel/nodes/descending_test.rb +46 -0
  152. data/test/cases/arel/nodes/distinct_test.rb +21 -0
  153. data/test/cases/arel/nodes/equality_test.rb +62 -0
  154. data/test/cases/arel/nodes/extract_test.rb +43 -0
  155. data/test/cases/arel/nodes/false_test.rb +21 -0
  156. data/test/cases/arel/nodes/grouping_test.rb +26 -0
  157. data/test/cases/arel/nodes/infix_operation_test.rb +42 -0
  158. data/test/cases/arel/nodes/insert_statement_test.rb +44 -0
  159. data/test/cases/arel/nodes/named_function_test.rb +48 -0
  160. data/test/cases/arel/nodes/node_test.rb +22 -0
  161. data/test/cases/arel/nodes/not_test.rb +31 -0
  162. data/test/cases/arel/nodes/or_test.rb +36 -0
  163. data/test/cases/arel/nodes/over_test.rb +69 -0
  164. data/test/cases/arel/nodes/select_core_test.rb +79 -0
  165. data/test/cases/arel/nodes/select_statement_test.rb +51 -0
  166. data/test/cases/arel/nodes/sql_literal_test.rb +75 -0
  167. data/test/cases/arel/nodes/sum_test.rb +35 -0
  168. data/test/cases/arel/nodes/table_alias_test.rb +29 -0
  169. data/test/cases/arel/nodes/true_test.rb +21 -0
  170. data/test/cases/arel/nodes/unary_operation_test.rb +41 -0
  171. data/test/cases/arel/nodes/update_statement_test.rb +60 -0
  172. data/test/cases/arel/nodes/window_test.rb +81 -0
  173. data/test/cases/arel/nodes_test.rb +34 -0
  174. data/test/cases/arel/select_manager_test.rb +1238 -0
  175. data/test/cases/arel/support/fake_record.rb +135 -0
  176. data/test/cases/arel/table_test.rb +216 -0
  177. data/test/cases/arel/update_manager_test.rb +126 -0
  178. data/test/cases/arel/visitors/dispatch_contamination_test.rb +78 -0
  179. data/test/cases/arel/visitors/dot_test.rb +90 -0
  180. data/test/cases/arel/visitors/mysql_test.rb +157 -0
  181. data/test/cases/arel/visitors/postgres_test.rb +366 -0
  182. data/test/cases/arel/visitors/sqlite_test.rb +75 -0
  183. data/test/cases/arel/visitors/to_sql_test.rb +750 -0
  184. data/test/cases/associations/association_scope_test.rb +16 -0
  185. data/test/cases/associations/belongs_to_associations_test.rb +1493 -0
  186. data/test/cases/associations/bidirectional_destroy_dependencies_test.rb +43 -0
  187. data/test/cases/associations/callbacks_test.rb +208 -0
  188. data/test/cases/associations/cascaded_eager_loading_test.rb +245 -0
  189. data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +156 -0
  190. data/test/cases/associations/eager_load_nested_include_test.rb +127 -0
  191. data/test/cases/associations/eager_singularization_test.rb +148 -0
  192. data/test/cases/associations/eager_test.rb +1658 -0
  193. data/test/cases/associations/extension_test.rb +93 -0
  194. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +1025 -0
  195. data/test/cases/associations/has_many_associations_test.rb +3074 -0
  196. data/test/cases/associations/has_many_through_associations_test.rb +1580 -0
  197. data/test/cases/associations/has_one_associations_test.rb +872 -0
  198. data/test/cases/associations/has_one_through_associations_test.rb +429 -0
  199. data/test/cases/associations/inner_join_association_test.rb +215 -0
  200. data/test/cases/associations/inverse_associations_test.rb +941 -0
  201. data/test/cases/associations/join_model_test.rb +787 -0
  202. data/test/cases/associations/left_outer_join_association_test.rb +123 -0
  203. data/test/cases/associations/nested_through_associations_test.rb +636 -0
  204. data/test/cases/associations/required_test.rb +127 -0
  205. data/test/cases/associations_test.rb +516 -0
  206. data/test/cases/attribute_decorators_test.rb +126 -0
  207. data/test/cases/attribute_methods/read_test.rb +60 -0
  208. data/test/cases/attribute_methods_test.rb +1124 -0
  209. data/test/cases/attribute_set_test.rb +270 -0
  210. data/test/cases/attribute_test.rb +246 -0
  211. data/test/cases/attributes_test.rb +371 -0
  212. data/test/cases/autosave_association_test.rb +1953 -0
  213. data/test/cases/base_prevent_writes_test.rb +229 -0
  214. data/test/cases/base_test.rb +1770 -0
  215. data/test/cases/batches_test.rb +695 -0
  216. data/test/cases/binary_test.rb +39 -0
  217. data/test/cases/bind_parameter_test.rb +283 -0
  218. data/test/cases/boolean_test.rb +52 -0
  219. data/test/cases/cache_key_test.rb +131 -0
  220. data/test/cases/calculations_test.rb +1361 -0
  221. data/test/cases/callbacks_test.rb +503 -0
  222. data/test/cases/clone_test.rb +45 -0
  223. data/test/cases/coders/json_test.rb +17 -0
  224. data/test/cases/coders/yaml_column_test.rb +66 -0
  225. data/test/cases/collection_cache_key_test.rb +272 -0
  226. data/test/cases/column_alias_test.rb +19 -0
  227. data/test/cases/column_definition_test.rb +34 -0
  228. data/test/cases/comment_test.rb +204 -0
  229. data/test/cases/connection_adapters/adapter_leasing_test.rb +60 -0
  230. data/test/cases/connection_adapters/connection_handler_test.rb +467 -0
  231. data/test/cases/connection_adapters/connection_handlers_multi_db_test.rb +400 -0
  232. data/test/cases/connection_adapters/connection_handlers_multi_pool_config_test.rb +103 -0
  233. data/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb +499 -0
  234. data/test/cases/connection_adapters/connection_specification_test.rb +12 -0
  235. data/test/cases/connection_adapters/connection_swapping_nested_test.rb +457 -0
  236. data/test/cases/connection_adapters/legacy_connection_handlers_multi_db_test.rb +486 -0
  237. data/test/cases/connection_adapters/legacy_connection_handlers_sharding_db_test.rb +586 -0
  238. data/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb +436 -0
  239. data/test/cases/connection_adapters/mysql_type_lookup_test.rb +81 -0
  240. data/test/cases/connection_adapters/quoting_test.rb +13 -0
  241. data/test/cases/connection_adapters/schema_cache_test.rb +294 -0
  242. data/test/cases/connection_adapters/type_lookup_test.rb +119 -0
  243. data/test/cases/connection_management_test.rb +114 -0
  244. data/test/cases/connection_pool_test.rb +754 -0
  245. data/test/cases/connection_specification/resolver_test.rb +131 -0
  246. data/test/cases/core_test.rb +136 -0
  247. data/test/cases/counter_cache_test.rb +368 -0
  248. data/test/cases/custom_locking_test.rb +19 -0
  249. data/test/cases/database_configurations/hash_config_test.rb +74 -0
  250. data/test/cases/database_configurations/resolver_test.rb +150 -0
  251. data/test/cases/database_configurations_test.rb +145 -0
  252. data/test/cases/database_selector_test.rb +296 -0
  253. data/test/cases/database_statements_test.rb +36 -0
  254. data/test/cases/date_test.rb +36 -0
  255. data/test/cases/date_time_precision_test.rb +129 -0
  256. data/test/cases/date_time_test.rb +76 -0
  257. data/test/cases/defaults_test.rb +254 -0
  258. data/test/cases/delegated_type_test.rb +57 -0
  259. data/test/cases/dirty_test.rb +959 -0
  260. data/test/cases/disconnected_test.rb +30 -0
  261. data/test/cases/dup_test.rb +184 -0
  262. data/test/cases/enum_test.rb +823 -0
  263. data/test/cases/errors_test.rb +16 -0
  264. data/test/cases/explain_subscriber_test.rb +66 -0
  265. data/test/cases/explain_test.rb +79 -0
  266. data/test/cases/filter_attributes_test.rb +153 -0
  267. data/test/cases/finder_respond_to_test.rb +60 -0
  268. data/test/cases/finder_test.rb +1676 -0
  269. data/test/cases/fixture_set/file_test.rb +152 -0
  270. data/test/cases/fixtures_test.rb +1645 -0
  271. data/test/cases/forbidden_attributes_protection_test.rb +130 -0
  272. data/test/cases/habtm_destroy_order_test.rb +61 -0
  273. data/test/cases/helper.rb +233 -0
  274. data/test/cases/hot_compatibility_test.rb +143 -0
  275. data/test/cases/i18n_test.rb +46 -0
  276. data/test/cases/inheritance_test.rb +671 -0
  277. data/test/cases/insert_all_test.rb +489 -0
  278. data/test/cases/instrumentation_test.rb +101 -0
  279. data/test/cases/integration_test.rb +243 -0
  280. data/test/cases/invalid_connection_test.rb +26 -0
  281. data/test/cases/invertible_migration_test.rb +527 -0
  282. data/test/cases/json_attribute_test.rb +35 -0
  283. data/test/cases/json_serialization_test.rb +310 -0
  284. data/test/cases/json_shared_test_cases.rb +290 -0
  285. data/test/cases/locking_test.rb +787 -0
  286. data/test/cases/log_subscriber_test.rb +267 -0
  287. data/test/cases/marshal_serialization_test.rb +39 -0
  288. data/test/cases/migration/change_schema_test.rb +504 -0
  289. data/test/cases/migration/change_table_test.rb +364 -0
  290. data/test/cases/migration/check_constraint_test.rb +162 -0
  291. data/test/cases/migration/column_attributes_test.rb +186 -0
  292. data/test/cases/migration/column_positioning_test.rb +68 -0
  293. data/test/cases/migration/columns_test.rb +326 -0
  294. data/test/cases/migration/command_recorder_test.rb +437 -0
  295. data/test/cases/migration/compatibility_test.rb +673 -0
  296. data/test/cases/migration/create_join_table_test.rb +167 -0
  297. data/test/cases/migration/foreign_key_test.rb +581 -0
  298. data/test/cases/migration/helper.rb +40 -0
  299. data/test/cases/migration/index_test.rb +267 -0
  300. data/test/cases/migration/logger_test.rb +39 -0
  301. data/test/cases/migration/pending_migrations_test.rb +106 -0
  302. data/test/cases/migration/references_foreign_key_test.rb +235 -0
  303. data/test/cases/migration/references_index_test.rb +120 -0
  304. data/test/cases/migration/references_statements_test.rb +137 -0
  305. data/test/cases/migration/rename_table_test.rb +116 -0
  306. data/test/cases/migration_test.rb +1525 -0
  307. data/test/cases/migrator_test.rb +527 -0
  308. data/test/cases/mixin_test.rb +64 -0
  309. data/test/cases/modules_test.rb +174 -0
  310. data/test/cases/multi_db_migrator_test.rb +223 -0
  311. data/test/cases/multiparameter_attributes_test.rb +399 -0
  312. data/test/cases/multiple_db_test.rb +116 -0
  313. data/test/cases/nested_attributes_test.rb +1119 -0
  314. data/test/cases/nested_attributes_with_callbacks_test.rb +146 -0
  315. data/test/cases/null_relation_test.rb +84 -0
  316. data/test/cases/numeric_data_test.rb +93 -0
  317. data/test/cases/persistence_test.rb +1093 -0
  318. data/test/cases/pooled_connections_test.rb +73 -0
  319. data/test/cases/prepared_statement_status_test.rb +48 -0
  320. data/test/cases/primary_keys_test.rb +482 -0
  321. data/test/cases/query_cache_test.rb +915 -0
  322. data/test/cases/quoting_test.rb +303 -0
  323. data/test/cases/readonly_test.rb +120 -0
  324. data/test/cases/reaper_test.rb +199 -0
  325. data/test/cases/reflection_test.rb +520 -0
  326. data/test/cases/relation/delegation_test.rb +76 -0
  327. data/test/cases/relation/delete_all_test.rb +117 -0
  328. data/test/cases/relation/merging_test.rb +434 -0
  329. data/test/cases/relation/mutation_test.rb +145 -0
  330. data/test/cases/relation/or_test.rb +192 -0
  331. data/test/cases/relation/predicate_builder_test.rb +31 -0
  332. data/test/cases/relation/record_fetch_warning_test.rb +42 -0
  333. data/test/cases/relation/select_test.rb +67 -0
  334. data/test/cases/relation/update_all_test.rb +317 -0
  335. data/test/cases/relation/where_chain_test.rb +141 -0
  336. data/test/cases/relation/where_clause_test.rb +257 -0
  337. data/test/cases/relation/where_test.rb +429 -0
  338. data/test/cases/relation_test.rb +482 -0
  339. data/test/cases/relations_test.rb +2251 -0
  340. data/test/cases/reload_models_test.rb +26 -0
  341. data/test/cases/reserved_word_test.rb +141 -0
  342. data/test/cases/result_test.rb +141 -0
  343. data/test/cases/sanitize_test.rb +192 -0
  344. data/test/cases/schema_dumper_test.rb +550 -0
  345. data/test/cases/schema_loading_test.rb +53 -0
  346. data/test/cases/scoping/default_scoping_test.rb +569 -0
  347. data/test/cases/scoping/named_scoping_test.rb +649 -0
  348. data/test/cases/scoping/relation_scoping_test.rb +522 -0
  349. data/test/cases/secure_token_test.rb +47 -0
  350. data/test/cases/serialization_test.rb +106 -0
  351. data/test/cases/serialized_attribute_test.rb +455 -0
  352. data/test/cases/signed_id_test.rb +168 -0
  353. data/test/cases/statement_cache_test.rb +153 -0
  354. data/test/cases/statement_invalid_test.rb +42 -0
  355. data/test/cases/store_test.rb +320 -0
  356. data/test/cases/strict_loading_test.rb +473 -0
  357. data/test/cases/suppressor_test.rb +77 -0
  358. data/test/cases/tasks/database_tasks_test.rb +1526 -0
  359. data/test/cases/tasks/mysql_rake_test.rb +417 -0
  360. data/test/cases/tasks/postgresql_rake_test.rb +534 -0
  361. data/test/cases/tasks/sqlite_rake_test.rb +267 -0
  362. data/test/cases/test_case.rb +142 -0
  363. data/test/cases/test_databases_test.rb +79 -0
  364. data/test/cases/test_fixtures_test.rb +96 -0
  365. data/test/cases/time_precision_test.rb +125 -0
  366. data/test/cases/timestamp_test.rb +504 -0
  367. data/test/cases/touch_later_test.rb +123 -0
  368. data/test/cases/transaction_callbacks_test.rb +772 -0
  369. data/test/cases/transaction_isolation_test.rb +106 -0
  370. data/test/cases/transactions_test.rb +1285 -0
  371. data/test/cases/type/adapter_specific_registry_test.rb +145 -0
  372. data/test/cases/type/date_time_test.rb +16 -0
  373. data/test/cases/type/integer_test.rb +29 -0
  374. data/test/cases/type/string_test.rb +24 -0
  375. data/test/cases/type/time_test.rb +28 -0
  376. data/test/cases/type/type_map_test.rb +178 -0
  377. data/test/cases/type/unsigned_integer_test.rb +19 -0
  378. data/test/cases/type_test.rb +41 -0
  379. data/test/cases/types_test.rb +26 -0
  380. data/test/cases/unconnected_test.rb +46 -0
  381. data/test/cases/unsafe_raw_sql_test.rb +274 -0
  382. data/test/cases/validations/absence_validation_test.rb +75 -0
  383. data/test/cases/validations/association_validation_test.rb +99 -0
  384. data/test/cases/validations/i18n_generate_message_validation_test.rb +102 -0
  385. data/test/cases/validations/i18n_validation_test.rb +87 -0
  386. data/test/cases/validations/length_validation_test.rb +80 -0
  387. data/test/cases/validations/numericality_validation_test.rb +181 -0
  388. data/test/cases/validations/presence_validation_test.rb +105 -0
  389. data/test/cases/validations/uniqueness_validation_test.rb +618 -0
  390. data/test/cases/validations_repair_helper.rb +21 -0
  391. data/test/cases/validations_test.rb +229 -0
  392. data/test/cases/view_test.rb +222 -0
  393. data/test/cases/yaml_serialization_test.rb +166 -0
  394. data/test/config.example.yml +97 -0
  395. data/test/config.rb +7 -0
  396. data/test/config.yml +220 -0
  397. data/test/connections/native_ibm_db/connection.rb +44 -0
  398. data/test/fixtures/accounts.yml +29 -0
  399. data/test/fixtures/admin/accounts.yml +2 -0
  400. data/test/fixtures/admin/randomly_named_a9.yml +7 -0
  401. data/test/fixtures/admin/randomly_named_b0.yml +7 -0
  402. data/test/fixtures/admin/users.yml +10 -0
  403. data/test/fixtures/all/admin +1 -0
  404. data/test/fixtures/all/developers.yml +0 -0
  405. data/test/fixtures/all/namespaced/accounts.yml +2 -0
  406. data/test/fixtures/all/people.yml +0 -0
  407. data/test/fixtures/all/tasks.yml +0 -0
  408. data/test/fixtures/author_addresses.yml +11 -0
  409. data/test/fixtures/author_favorites.yml +4 -0
  410. data/test/fixtures/authors.yml +17 -0
  411. data/test/fixtures/bad_posts.yml +9 -0
  412. data/test/fixtures/binaries.yml +137 -0
  413. data/test/fixtures/books.yml +38 -0
  414. data/test/fixtures/bulbs.yml +5 -0
  415. data/test/fixtures/cars.yml +9 -0
  416. data/test/fixtures/categories/special_categories.yml +9 -0
  417. data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
  418. data/test/fixtures/categories.yml +19 -0
  419. data/test/fixtures/categories_ordered.yml +7 -0
  420. data/test/fixtures/categories_posts.yml +34 -0
  421. data/test/fixtures/categorizations.yml +23 -0
  422. data/test/fixtures/citations.yml +5 -0
  423. data/test/fixtures/clubs.yml +8 -0
  424. data/test/fixtures/collections.yml +3 -0
  425. data/test/fixtures/colleges.yml +3 -0
  426. data/test/fixtures/comments.yml +72 -0
  427. data/test/fixtures/companies.yml +72 -0
  428. data/test/fixtures/computers.yml +12 -0
  429. data/test/fixtures/content.yml +3 -0
  430. data/test/fixtures/content_positions.yml +3 -0
  431. data/test/fixtures/courses.yml +8 -0
  432. data/test/fixtures/customers.yml +35 -0
  433. data/test/fixtures/dashboards.yml +6 -0
  434. data/test/fixtures/dead_parrots.yml +5 -0
  435. data/test/fixtures/developers.yml +22 -0
  436. data/test/fixtures/developers_projects.yml +17 -0
  437. data/test/fixtures/dog_lovers.yml +7 -0
  438. data/test/fixtures/dogs.yml +4 -0
  439. data/test/fixtures/doubloons.yml +3 -0
  440. data/test/fixtures/edges.yml +5 -0
  441. data/test/fixtures/entrants.yml +14 -0
  442. data/test/fixtures/essays.yml +16 -0
  443. data/test/fixtures/faces.yml +11 -0
  444. data/test/fixtures/fk_test_has_fk.yml +3 -0
  445. data/test/fixtures/fk_test_has_pk.yml +2 -0
  446. data/test/fixtures/friendships.yml +4 -0
  447. data/test/fixtures/funny_jokes.yml +10 -0
  448. data/test/fixtures/humans.yml +5 -0
  449. data/test/fixtures/interests.yml +33 -0
  450. data/test/fixtures/items.yml +3 -0
  451. data/test/fixtures/jobs.yml +7 -0
  452. data/test/fixtures/legacy_things.yml +3 -0
  453. data/test/fixtures/live_parrots.yml +4 -0
  454. data/test/fixtures/mateys.yml +4 -0
  455. data/test/fixtures/member_details.yml +8 -0
  456. data/test/fixtures/member_types.yml +6 -0
  457. data/test/fixtures/members.yml +11 -0
  458. data/test/fixtures/memberships.yml +41 -0
  459. data/test/fixtures/men.yml +5 -0
  460. data/test/fixtures/minimalistics.yml +5 -0
  461. data/test/fixtures/minivans.yml +5 -0
  462. data/test/fixtures/mixed_case_monkeys.yml +6 -0
  463. data/test/fixtures/mixins.yml +29 -0
  464. data/test/fixtures/movies.yml +7 -0
  465. data/test/fixtures/naked/yml/accounts.yml +1 -0
  466. data/test/fixtures/naked/yml/companies.yml +1 -0
  467. data/test/fixtures/naked/yml/courses.yml +1 -0
  468. data/test/fixtures/naked/yml/courses_with_invalid_key.yml +3 -0
  469. data/test/fixtures/naked/yml/parrots.yml +3 -0
  470. data/test/fixtures/naked/yml/trees.yml +3 -0
  471. data/test/fixtures/nodes.yml +29 -0
  472. data/test/fixtures/organizations.yml +5 -0
  473. data/test/fixtures/other_books.yml +26 -0
  474. data/test/fixtures/other_comments.yml +6 -0
  475. data/test/fixtures/other_dogs.yml +2 -0
  476. data/test/fixtures/other_posts.yml +8 -0
  477. data/test/fixtures/other_topics.yml +42 -0
  478. data/test/fixtures/owners.yml +9 -0
  479. data/test/fixtures/parrots.yml +33 -0
  480. data/test/fixtures/parrots_pirates.yml +7 -0
  481. data/test/fixtures/people.yml +24 -0
  482. data/test/fixtures/peoples_treasures.yml +3 -0
  483. data/test/fixtures/pets.yml +19 -0
  484. data/test/fixtures/pirates.yml +15 -0
  485. data/test/fixtures/posts.yml +88 -0
  486. data/test/fixtures/price_estimates.yml +16 -0
  487. data/test/fixtures/products.yml +4 -0
  488. data/test/fixtures/projects.yml +7 -0
  489. data/test/fixtures/randomly_named_a9.yml +7 -0
  490. data/test/fixtures/ratings.yml +14 -0
  491. data/test/fixtures/readers.yml +17 -0
  492. data/test/fixtures/references.yml +17 -0
  493. data/test/fixtures/reserved_words/distinct.yml +5 -0
  494. data/test/fixtures/reserved_words/distinct_select.yml +11 -0
  495. data/test/fixtures/reserved_words/group.yml +14 -0
  496. data/test/fixtures/reserved_words/select.yml +8 -0
  497. data/test/fixtures/reserved_words/values.yml +7 -0
  498. data/test/fixtures/ships.yml +6 -0
  499. data/test/fixtures/speedometers.yml +8 -0
  500. data/test/fixtures/sponsors.yml +15 -0
  501. data/test/fixtures/strict_zines.yml +2 -0
  502. data/test/fixtures/string_key_objects.yml +7 -0
  503. data/test/fixtures/subscribers.yml +11 -0
  504. data/test/fixtures/subscriptions.yml +12 -0
  505. data/test/fixtures/taggings.yml +78 -0
  506. data/test/fixtures/tags.yml +11 -0
  507. data/test/fixtures/tasks.yml +7 -0
  508. data/test/fixtures/teapots.yml +3 -0
  509. data/test/fixtures/to_be_linked/accounts.yml +2 -0
  510. data/test/fixtures/to_be_linked/users.yml +10 -0
  511. data/test/fixtures/topics.yml +49 -0
  512. data/test/fixtures/toys.yml +14 -0
  513. data/test/fixtures/traffic_lights.yml +10 -0
  514. data/test/fixtures/treasures.yml +10 -0
  515. data/test/fixtures/trees.yml +3 -0
  516. data/test/fixtures/uuid_children.yml +3 -0
  517. data/test/fixtures/uuid_parents.yml +2 -0
  518. data/test/fixtures/variants.yml +4 -0
  519. data/test/fixtures/vegetables.yml +20 -0
  520. data/test/fixtures/vertices.yml +4 -0
  521. data/test/fixtures/warehouse-things.yml +3 -0
  522. data/test/fixtures/warehouse_things.yml +3 -0
  523. data/test/fixtures/zines.yml +5 -0
  524. data/test/ibm_db_test.rb +25 -0
  525. data/test/migrations/10_urban/9_add_expressions.rb +13 -0
  526. data/test/migrations/decimal/1_give_me_big_numbers.rb +17 -0
  527. data/test/migrations/magic/1_currencies_have_symbols.rb +13 -0
  528. data/test/migrations/missing/1000_people_have_middle_names.rb +11 -0
  529. data/test/migrations/missing/1_people_have_last_names.rb +11 -0
  530. data/test/migrations/missing/3_we_need_reminders.rb +14 -0
  531. data/test/migrations/missing/4_innocent_jointable.rb +14 -0
  532. data/test/migrations/rename/1_we_need_things.rb +13 -0
  533. data/test/migrations/rename/2_rename_things.rb +11 -0
  534. data/test/migrations/to_copy/1_people_have_hobbies.rb +11 -0
  535. data/test/migrations/to_copy/2_people_have_descriptions.rb +11 -0
  536. data/test/migrations/to_copy2/1_create_articles.rb +9 -0
  537. data/test/migrations/to_copy2/2_create_comments.rb +9 -0
  538. data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +11 -0
  539. data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +11 -0
  540. data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +11 -0
  541. data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +9 -0
  542. data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +9 -0
  543. data/test/migrations/valid/1_valid_people_have_last_names.rb +11 -0
  544. data/test/migrations/valid/2_we_need_reminders.rb +14 -0
  545. data/test/migrations/valid/3_innocent_jointable.rb +14 -0
  546. data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +11 -0
  547. data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +14 -0
  548. data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +14 -0
  549. data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +11 -0
  550. data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +14 -0
  551. data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +14 -0
  552. data/test/migrations/version_check/20131219224947_migration_version_check.rb +10 -0
  553. data/test/models/account.rb +46 -0
  554. data/test/models/admin/account.rb +5 -0
  555. data/test/models/admin/randomly_named_c1.rb +9 -0
  556. data/test/models/admin/user.rb +48 -0
  557. data/test/models/admin.rb +7 -0
  558. data/test/models/aircraft.rb +7 -0
  559. data/test/models/arunit2_model.rb +5 -0
  560. data/test/models/author.rb +260 -0
  561. data/test/models/auto_id.rb +6 -0
  562. data/test/models/autoloadable/extra_firm.rb +4 -0
  563. data/test/models/binary.rb +4 -0
  564. data/test/models/binary_field.rb +6 -0
  565. data/test/models/bird.rb +24 -0
  566. data/test/models/book.rb +33 -0
  567. data/test/models/book_destroy_async.rb +24 -0
  568. data/test/models/boolean.rb +7 -0
  569. data/test/models/bulb.rb +61 -0
  570. data/test/models/cake_designer.rb +5 -0
  571. data/test/models/car.rb +36 -0
  572. data/test/models/carrier.rb +4 -0
  573. data/test/models/cart.rb +5 -0
  574. data/test/models/cat.rb +12 -0
  575. data/test/models/categorization.rb +21 -0
  576. data/test/models/category.rb +47 -0
  577. data/test/models/chef.rb +10 -0
  578. data/test/models/citation.rb +7 -0
  579. data/test/models/club.rb +28 -0
  580. data/test/models/college.rb +12 -0
  581. data/test/models/column.rb +5 -0
  582. data/test/models/column_name.rb +5 -0
  583. data/test/models/comment.rb +98 -0
  584. data/test/models/company.rb +226 -0
  585. data/test/models/company_in_module.rb +99 -0
  586. data/test/models/computer.rb +5 -0
  587. data/test/models/contact.rb +43 -0
  588. data/test/models/content.rb +42 -0
  589. data/test/models/contract.rb +38 -0
  590. data/test/models/country.rb +5 -0
  591. data/test/models/course.rb +8 -0
  592. data/test/models/customer.rb +85 -0
  593. data/test/models/customer_carrier.rb +16 -0
  594. data/test/models/dashboard.rb +5 -0
  595. data/test/models/default.rb +4 -0
  596. data/test/models/department.rb +6 -0
  597. data/test/models/destroy_async_parent.rb +15 -0
  598. data/test/models/destroy_async_parent_soft_delete.rb +20 -0
  599. data/test/models/developer.rb +341 -0
  600. data/test/models/dl_keyed_belongs_to.rb +13 -0
  601. data/test/models/dl_keyed_belongs_to_soft_delete.rb +19 -0
  602. data/test/models/dl_keyed_has_many.rb +5 -0
  603. data/test/models/dl_keyed_has_many_through.rb +5 -0
  604. data/test/models/dl_keyed_has_one.rb +5 -0
  605. data/test/models/dl_keyed_join.rb +10 -0
  606. data/test/models/dog.rb +7 -0
  607. data/test/models/dog_lover.rb +7 -0
  608. data/test/models/doubloon.rb +14 -0
  609. data/test/models/drink_designer.rb +20 -0
  610. data/test/models/edge.rb +7 -0
  611. data/test/models/electron.rb +7 -0
  612. data/test/models/engine.rb +5 -0
  613. data/test/models/entrant.rb +5 -0
  614. data/test/models/entry.rb +5 -0
  615. data/test/models/essay.rb +8 -0
  616. data/test/models/essay_destroy_async.rb +12 -0
  617. data/test/models/event.rb +5 -0
  618. data/test/models/eye.rb +39 -0
  619. data/test/models/face.rb +17 -0
  620. data/test/models/family.rb +6 -0
  621. data/test/models/family_tree.rb +6 -0
  622. data/test/models/friendship.rb +8 -0
  623. data/test/models/frog.rb +8 -0
  624. data/test/models/guid.rb +4 -0
  625. data/test/models/guitar.rb +6 -0
  626. data/test/models/hotel.rb +13 -0
  627. data/test/models/human.rb +39 -0
  628. data/test/models/image.rb +5 -0
  629. data/test/models/interest.rb +16 -0
  630. data/test/models/invoice.rb +6 -0
  631. data/test/models/item.rb +9 -0
  632. data/test/models/job.rb +9 -0
  633. data/test/models/joke.rb +9 -0
  634. data/test/models/keyboard.rb +5 -0
  635. data/test/models/legacy_thing.rb +5 -0
  636. data/test/models/lesson.rb +13 -0
  637. data/test/models/line_item.rb +5 -0
  638. data/test/models/liquid.rb +6 -0
  639. data/test/models/man.rb +11 -0
  640. data/test/models/matey.rb +6 -0
  641. data/test/models/measurement.rb +4 -0
  642. data/test/models/member.rb +45 -0
  643. data/test/models/member_detail.rb +11 -0
  644. data/test/models/member_type.rb +5 -0
  645. data/test/models/membership.rb +38 -0
  646. data/test/models/mentor.rb +5 -0
  647. data/test/models/message.rb +5 -0
  648. data/test/models/minimalistic.rb +4 -0
  649. data/test/models/minivan.rb +10 -0
  650. data/test/models/mixed_case_monkey.rb +5 -0
  651. data/test/models/mocktail_designer.rb +2 -0
  652. data/test/models/molecule.rb +8 -0
  653. data/test/models/mouse.rb +6 -0
  654. data/test/models/movie.rb +7 -0
  655. data/test/models/node.rb +7 -0
  656. data/test/models/non_primary_key.rb +4 -0
  657. data/test/models/notification.rb +5 -0
  658. data/test/models/numeric_data.rb +12 -0
  659. data/test/models/order.rb +6 -0
  660. data/test/models/organization.rb +16 -0
  661. data/test/models/other_dog.rb +7 -0
  662. data/test/models/owner.rb +39 -0
  663. data/test/models/parrot.rb +36 -0
  664. data/test/models/person.rb +147 -0
  665. data/test/models/personal_legacy_thing.rb +6 -0
  666. data/test/models/pet.rb +20 -0
  667. data/test/models/pet_treasure.rb +8 -0
  668. data/test/models/pirate.rb +116 -0
  669. data/test/models/possession.rb +5 -0
  670. data/test/models/post.rb +371 -0
  671. data/test/models/price_estimate.rb +14 -0
  672. data/test/models/professor.rb +7 -0
  673. data/test/models/project.rb +42 -0
  674. data/test/models/publisher/article.rb +6 -0
  675. data/test/models/publisher/magazine.rb +5 -0
  676. data/test/models/publisher.rb +4 -0
  677. data/test/models/randomly_named_c1.rb +5 -0
  678. data/test/models/rating.rb +8 -0
  679. data/test/models/reader.rb +25 -0
  680. data/test/models/recipe.rb +5 -0
  681. data/test/models/record.rb +4 -0
  682. data/test/models/reference.rb +25 -0
  683. data/test/models/reply.rb +79 -0
  684. data/test/models/room.rb +6 -0
  685. data/test/models/section.rb +6 -0
  686. data/test/models/seminar.rb +6 -0
  687. data/test/models/session.rb +6 -0
  688. data/test/models/ship.rb +42 -0
  689. data/test/models/ship_part.rb +10 -0
  690. data/test/models/shop.rb +19 -0
  691. data/test/models/shop_account.rb +8 -0
  692. data/test/models/speedometer.rb +8 -0
  693. data/test/models/sponsor.rb +10 -0
  694. data/test/models/squeak.rb +6 -0
  695. data/test/models/strict_zine.rb +7 -0
  696. data/test/models/string_key_object.rb +5 -0
  697. data/test/models/student.rb +6 -0
  698. data/test/models/subject.rb +16 -0
  699. data/test/models/subscriber.rb +10 -0
  700. data/test/models/subscription.rb +8 -0
  701. data/test/models/tag.rb +16 -0
  702. data/test/models/tagging.rb +20 -0
  703. data/test/models/task.rb +7 -0
  704. data/test/models/topic.rb +153 -0
  705. data/test/models/toy.rb +10 -0
  706. data/test/models/traffic_light.rb +6 -0
  707. data/test/models/treasure.rb +16 -0
  708. data/test/models/treaty.rb +5 -0
  709. data/test/models/tree.rb +5 -0
  710. data/test/models/tuning_peg.rb +6 -0
  711. data/test/models/tyre.rb +13 -0
  712. data/test/models/user.rb +22 -0
  713. data/test/models/uuid_child.rb +5 -0
  714. data/test/models/uuid_item.rb +8 -0
  715. data/test/models/uuid_parent.rb +5 -0
  716. data/test/models/vegetables.rb +33 -0
  717. data/test/models/vehicle.rb +7 -0
  718. data/test/models/vertex.rb +11 -0
  719. data/test/models/warehouse_thing.rb +7 -0
  720. data/test/models/wheel.rb +5 -0
  721. data/test/models/without_table.rb +5 -0
  722. data/test/models/zine.rb +5 -0
  723. data/test/schema/i5/ibm_db_specific_schema.rb +137 -0
  724. data/test/schema/ids/ibm_db_specific_schema.rb +140 -0
  725. data/test/schema/luw/ibm_db_specific_schema.rb +137 -0
  726. data/test/schema/mysql2_specific_schema.rb +82 -0
  727. data/test/schema/oracle_specific_schema.rb +38 -0
  728. data/test/schema/postgresql_specific_schema.rb +125 -0
  729. data/test/schema/schema.rb +1237 -0
  730. data/test/schema/schema.rb.original +1057 -0
  731. data/test/schema/sqlite_specific_schema.rb +11 -0
  732. data/test/schema/zOS/ibm_db_specific_schema.rb +208 -0
  733. data/test/support/config.rb +43 -0
  734. data/test/support/connection.rb +29 -0
  735. data/test/support/connection_helper.rb +16 -0
  736. data/test/support/ddl_helper.rb +10 -0
  737. data/test/support/marshal_compatibility_fixtures/IBM_DB/rails_6_0_topic.dump +0 -0
  738. data/test/support/marshal_compatibility_fixtures/IBM_DB/rails_6_0_topic_associations.dump +0 -0
  739. data/test/support/marshal_compatibility_fixtures/Mysql2/rails_6_0_topic.dump +0 -0
  740. data/test/support/marshal_compatibility_fixtures/Mysql2/rails_6_0_topic_associations.dump +0 -0
  741. data/test/support/marshal_compatibility_fixtures/PostgreSQL/rails_6_0_topic.dump +0 -0
  742. data/test/support/marshal_compatibility_fixtures/PostgreSQL/rails_6_0_topic_associations.dump +0 -0
  743. data/test/support/marshal_compatibility_fixtures/SQLite/rails_6_0_topic.dump +0 -0
  744. data/test/support/marshal_compatibility_fixtures/SQLite/rails_6_0_topic_associations.dump +0 -0
  745. data/test/support/marshal_compatibility_fixtures/legacy_6_0_record_mysql.dump +0 -0
  746. data/test/support/marshal_compatibility_fixtures/legacy_relation.dump +0 -0
  747. data/test/support/schema_dumping_helper.rb +22 -0
  748. data/test/support/stubs/strong_parameters.rb +40 -0
  749. data/test/support/yaml_compatibility_fixtures/rails_4_1.yml +22 -0
  750. data/test/support/yaml_compatibility_fixtures/rails_4_2_0.yml +182 -0
  751. data/test/support/yaml_compatibility_fixtures/rails_v1_mysql.yml +206 -0
  752. data/test/support/yaml_compatibility_fixtures/rails_v2.yml +55 -0
  753. metadata +876 -0
@@ -0,0 +1,1361 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cases/helper"
4
+ require "models/book"
5
+ require "models/club"
6
+ require "models/company"
7
+ require "models/contract"
8
+ require "models/edge"
9
+ require "models/organization"
10
+ require "models/possession"
11
+ require "models/author"
12
+ require "models/topic"
13
+ require "models/reply"
14
+ require "models/numeric_data"
15
+ require "models/minivan"
16
+ require "models/speedometer"
17
+ require "models/ship_part"
18
+ require "models/treasure"
19
+ require "models/developer"
20
+ require "models/post"
21
+ require "models/comment"
22
+ require "models/rating"
23
+ require "support/stubs/strong_parameters"
24
+
25
+ class CalculationsTest < ActiveRecord::TestCase
26
+ fixtures :companies, :accounts, :authors, :author_addresses, :topics, :speedometers, :minivans, :books, :posts, :comments
27
+
28
+ def test_should_sum_field
29
+ assert_equal 318, Account.sum(:credit_limit)
30
+ end
31
+
32
+ def test_should_sum_arel_attribute
33
+ assert_equal 318, Account.sum(Account.arel_table[:credit_limit])
34
+ end
35
+
36
+ def test_should_average_field
37
+ value = Account.average(:credit_limit)
38
+ assert_equal 53.0, value
39
+ end
40
+
41
+ def test_should_average_arel_attribute
42
+ value = Account.average(Account.arel_table[:credit_limit])
43
+ assert_equal 53.0, value
44
+ end
45
+
46
+ def test_should_resolve_aliased_attributes
47
+ assert_equal 318, Account.sum(:available_credit)
48
+ end
49
+
50
+ def test_should_return_decimal_average_of_integer_field
51
+ value = Account.average(:id)
52
+ assert_equal 3.5, value
53
+ end
54
+
55
+ def test_should_return_integer_average_if_db_returns_such
56
+ ShipPart.delete_all
57
+ ShipPart.create!(id: 3, name: "foo")
58
+ value = ShipPart.average(:id)
59
+ assert_equal 3, value
60
+ end
61
+
62
+ def test_should_return_nil_as_average
63
+ assert_nil NumericData.average(:bank_balance)
64
+ end
65
+
66
+ def test_should_get_maximum_of_field
67
+ assert_equal 60, Account.maximum(:credit_limit)
68
+ end
69
+
70
+ def test_should_get_maximum_of_arel_attribute
71
+ assert_equal 60, Account.maximum(Account.arel_table[:credit_limit])
72
+ end
73
+
74
+ def test_should_get_maximum_of_field_with_include
75
+ assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).includes(:firm).maximum(:credit_limit)
76
+ end
77
+
78
+ def test_should_get_maximum_of_arel_attribute_with_include
79
+ assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).includes(:firm).maximum(Account.arel_table[:credit_limit])
80
+ end
81
+
82
+ def test_should_get_minimum_of_field
83
+ assert_equal 50, Account.minimum(:credit_limit)
84
+ end
85
+
86
+ def test_should_get_minimum_of_arel_attribute
87
+ assert_equal 50, Account.minimum(Account.arel_table[:credit_limit])
88
+ end
89
+
90
+ def test_should_group_by_field
91
+ c = Account.group(:firm_id).sum(:credit_limit)
92
+ [1, 6, 2].each do |firm_id|
93
+ assert_includes c.keys, firm_id, "Group #{c.inspect} does not contain firm_id #{firm_id}"
94
+ end
95
+ end
96
+
97
+ def test_should_group_by_arel_attribute
98
+ c = Account.group(Account.arel_table[:firm_id]).sum(:credit_limit)
99
+ [1, 6, 2].each do |firm_id|
100
+ assert_includes c.keys, firm_id, "Group #{c.inspect} does not contain firm_id #{firm_id}"
101
+ end
102
+ end
103
+
104
+ def test_should_group_by_multiple_fields
105
+ c = Account.group("firm_id", :credit_limit).count(:all)
106
+ [ [nil, 50], [1, 50], [6, 50], [6, 55], [9, 53], [2, 60] ].each { |firm_and_limit| assert_includes c.keys, firm_and_limit }
107
+ end
108
+
109
+ def test_should_group_by_multiple_fields_having_functions
110
+ c = Topic.group(:author_name, "COALESCE(type, title)").count(:all)
111
+ assert_equal 1, c[["Carl", "The Third Topic of the day"]]
112
+ assert_equal 1, c[["Mary", "Reply"]]
113
+ assert_equal 1, c[["David", "The First Topic"]]
114
+ assert_equal 1, c[["Carl", "Reply"]]
115
+ end
116
+
117
+ def test_should_group_by_summed_field
118
+ expected = { nil => 50, 1 => 50, 2 => 60, 6 => 105, 9 => 53 }
119
+ assert_equal expected, Account.group(:firm_id).sum(:credit_limit)
120
+ end
121
+
122
+ def test_group_by_multiple_same_field
123
+ accounts = Account.group(:firm_id)
124
+
125
+ expected = {
126
+ nil => 50,
127
+ 1 => 50,
128
+ 2 => 60,
129
+ 6 => 105,
130
+ 9 => 53
131
+ }
132
+ assert_equal expected, accounts.sum(:credit_limit)
133
+ assert_equal expected, accounts.merge!(accounts).uniq!(:group).sum(:credit_limit)
134
+
135
+ expected = {
136
+ [nil, nil] => 50,
137
+ [1, 1] => 50,
138
+ [2, 2] => 60,
139
+ [6, 6] => 55,
140
+ [9, 9] => 53
141
+ }
142
+ message = <<-MSG.squish
143
+ `maximum` with group by duplicated fields does no longer affect to result in Rails 7.0.
144
+ To migrate to Rails 7.0's behavior, use `uniq!(:group)` to deduplicate group fields
145
+ (`accounts.uniq!(:group).maximum(:credit_limit)`).
146
+ MSG
147
+ assert_deprecated(message) do
148
+ assert_equal expected, accounts.merge!(accounts).maximum(:credit_limit)
149
+ end
150
+
151
+ expected = {
152
+ [nil, nil, nil, nil] => 50,
153
+ [1, 1, 1, 1] => 50,
154
+ [2, 2, 2, 2] => 60,
155
+ [6, 6, 6, 6] => 50,
156
+ [9, 9, 9, 9] => 53
157
+ }
158
+ message = <<-MSG.squish
159
+ `minimum` with group by duplicated fields does no longer affect to result in Rails 7.0.
160
+ To migrate to Rails 7.0's behavior, use `uniq!(:group)` to deduplicate group fields
161
+ (`accounts.uniq!(:group).minimum(:credit_limit)`).
162
+ MSG
163
+ assert_deprecated(message) do
164
+ assert_equal expected, accounts.merge!(accounts).minimum(:credit_limit)
165
+ end
166
+ end
167
+
168
+ def test_should_generate_valid_sql_with_joins_and_group
169
+ assert_nothing_raised do
170
+ AuditLog.joins(:developer).group(:id).count
171
+ end
172
+ end
173
+
174
+ def test_should_calculate_against_given_relation
175
+ developer = Developer.create!(name: "developer")
176
+ developer.audit_logs.create!(message: "first log")
177
+ developer.audit_logs.create!(message: "second log")
178
+
179
+ c = developer.audit_logs.joins(:developer).group(:id).count
180
+
181
+ assert_equal developer.audit_logs.count, c.size
182
+ developer.audit_logs.each do |log|
183
+ assert_equal 1, c[log.id]
184
+ end
185
+ end
186
+
187
+ def test_should_not_use_alias_for_grouped_field
188
+ assert_sql(/GROUP BY #{Regexp.escape(Account.connection.quote_table_name("accounts.firm_id"))}/i) do
189
+ c = Account.group(:firm_id).order("accounts_firm_id").sum(:credit_limit)
190
+ assert_equal [1, 2, 6, 9], c.keys.compact
191
+ end
192
+ end
193
+
194
+ def test_should_order_by_grouped_field
195
+ c = Account.group(:firm_id).order("firm_id").sum(:credit_limit)
196
+ assert_equal [1, 2, 6, 9], c.keys.compact
197
+ end
198
+
199
+ def test_should_order_by_calculation
200
+ c = Account.group(:firm_id).order("sum_credit_limit desc, firm_id").sum(:credit_limit)
201
+ assert_equal [105, 60, 53, 50, 50], c.keys.collect { |k| c[k] }
202
+ assert_equal [6, 2, 9, 1], c.keys.compact
203
+ end
204
+
205
+ def test_should_limit_calculation
206
+ c = Account.where("firm_id IS NOT NULL").group(:firm_id).order("firm_id").limit(2).sum(:credit_limit)
207
+ assert_equal [1, 2], c.keys.compact
208
+ end
209
+
210
+ def test_should_limit_calculation_with_offset
211
+ c = Account.where("firm_id IS NOT NULL").group(:firm_id).order("firm_id").
212
+ limit(2).offset(1).sum(:credit_limit)
213
+ assert_equal [2, 6], c.keys.compact
214
+ end
215
+
216
+ def test_limit_should_apply_before_count
217
+ accounts = Account.order(:id).limit(4)
218
+
219
+ assert_equal 3, accounts.count(:firm_id)
220
+ assert_equal 3, accounts.select(:firm_id).count
221
+ end
222
+
223
+ def test_limit_should_apply_before_count_arel_attribute
224
+ accounts = Account.order(:id).limit(4)
225
+
226
+ firm_id_attribute = Account.arel_table[:firm_id]
227
+ assert_equal 3, accounts.count(firm_id_attribute)
228
+ assert_equal 3, accounts.select(firm_id_attribute).count
229
+ end
230
+
231
+ def test_count_should_shortcut_with_limit_zero
232
+ accounts = Account.limit(0)
233
+
234
+ assert_no_queries { assert_equal 0, accounts.count }
235
+ end
236
+
237
+ def test_limit_is_kept
238
+ return if current_adapter?(:OracleAdapter)
239
+
240
+ queries = capture_sql { Account.limit(1).count }
241
+ assert_equal 1, queries.length
242
+ assert_match(/FETCH FIRST/, queries.first)
243
+ end
244
+
245
+ def test_offset_is_kept
246
+ return if current_adapter?(:OracleAdapter)
247
+
248
+ queries = capture_sql { Account.offset(1).count }
249
+ assert_equal 1, queries.length
250
+ assert_match(/OFFSET/, queries.first)
251
+ end
252
+
253
+ def test_limit_with_offset_is_kept
254
+ return if current_adapter?(:OracleAdapter)
255
+
256
+ queries = capture_sql { Account.limit(1).offset(1).count }
257
+ assert_equal 1, queries.length
258
+ assert_match(/FETCH FIRST/, queries.first)
259
+ assert_match(/OFFSET/, queries.first)
260
+ end
261
+
262
+ def test_no_limit_no_offset
263
+ queries = capture_sql { Account.count }
264
+ assert_equal 1, queries.length
265
+ assert_no_match(/FETCH FIRST/, queries.first)
266
+ assert_no_match(/OFFSET/, queries.first)
267
+ end
268
+
269
+ def test_count_on_invalid_columns_raises
270
+ e = assert_raises(ActiveRecord::StatementInvalid) {
271
+ Account.select("credit_limit, firm_name").count
272
+ }
273
+
274
+ assert_match %r{accounts}i, e.sql
275
+ assert_match "credit_limit, firm_name", e.sql
276
+ end
277
+
278
+ def test_apply_distinct_in_count
279
+ queries = capture_sql do
280
+ Account.distinct.count
281
+ Account.group(:firm_id).distinct.count
282
+ end
283
+
284
+ queries.each do |query|
285
+ assert_match %r{\ASELECT(?! DISTINCT) COUNT\(DISTINCT\b}, query
286
+ end
287
+ end
288
+
289
+ def test_count_with_eager_loading_and_custom_order
290
+ posts = Post.includes(:comments).order("comments.id")
291
+ assert_queries(1) { assert_equal 11, posts.count }
292
+ assert_queries(1) { assert_equal 11, posts.count(:all) }
293
+ end
294
+
295
+ def test_count_with_eager_loading_and_custom_select_and_order
296
+ posts = Post.includes(:comments).order("comments.id").select(:type)
297
+ assert_queries(1) { assert_equal 11, posts.count }
298
+ assert_queries(1) { assert_equal 11, posts.count(:all) }
299
+ end
300
+
301
+ def test_count_with_eager_loading_and_custom_order_and_distinct
302
+ posts = Post.includes(:comments).order("comments.id").distinct
303
+ assert_queries(1) { assert_equal 11, posts.count }
304
+ assert_queries(1) { assert_equal 11, posts.count(:all) }
305
+ end
306
+
307
+ def test_distinct_count_all_with_custom_select_and_order
308
+ accounts = Account.distinct.select("credit_limit % 10").order(Arel.sql("credit_limit % 10"))
309
+ assert_queries(1) { assert_equal 3, accounts.count(:all) }
310
+ assert_queries(1) { assert_equal 3, accounts.load.size }
311
+ end
312
+
313
+ def test_distinct_count_with_order_and_limit
314
+ assert_equal 4, Account.distinct.order(:firm_id).limit(4).count
315
+ end
316
+
317
+ def test_distinct_count_with_order_and_offset
318
+ assert_equal 4, Account.distinct.order(:firm_id).offset(2).count
319
+ end
320
+
321
+ def test_distinct_count_with_order_and_limit_and_offset
322
+ assert_equal 4, Account.distinct.order(:firm_id).limit(4).offset(2).count
323
+ end
324
+
325
+ def test_distinct_joins_count_with_order_and_limit
326
+ assert_equal 3, Account.joins(:firm).distinct.order(:firm_id).limit(3).count
327
+ end
328
+
329
+ def test_distinct_joins_count_with_order_and_offset
330
+ assert_equal 3, Account.joins(:firm).distinct.order(:firm_id).offset(2).count
331
+ end
332
+
333
+ def test_distinct_joins_count_with_order_and_limit_and_offset
334
+ assert_equal 3, Account.joins(:firm).distinct.order(:firm_id).limit(3).offset(2).count
335
+ end
336
+
337
+ def test_distinct_joins_count_with_group_by
338
+ expected = { nil => 4, 1 => 1, 2 => 1, 4 => 1, 5 => 1, 7 => 1 }
339
+ assert_equal expected, Post.left_joins(:comments).group(:post_id).distinct.count(:author_id)
340
+ assert_equal expected, Post.left_joins(:comments).group(:post_id).distinct.select(:author_id).count
341
+ assert_equal expected, Post.left_joins(:comments).group(:post_id).count("DISTINCT posts.author_id")
342
+ assert_equal expected, Post.left_joins(:comments).group(:post_id).select("DISTINCT posts.author_id").count
343
+
344
+ expected = { nil => 6, 1 => 1, 2 => 1, 4 => 1, 5 => 1, 7 => 1 }
345
+ assert_equal expected, Post.left_joins(:comments).group(:post_id).distinct.count(:all)
346
+ assert_equal expected, Post.left_joins(:comments).group(:post_id).distinct.select(:author_id).count(:all)
347
+ end
348
+
349
+ def test_distinct_count_with_group_by_and_order_and_limit
350
+ assert_equal({ 6 => 2 }, Account.group(:firm_id).distinct.order("1 DESC").limit(1).count)
351
+ end
352
+
353
+ def test_should_group_by_summed_field_having_condition
354
+ c = Account.group(:firm_id).having("sum(credit_limit) > 50").sum(:credit_limit)
355
+ assert_nil c[1]
356
+ assert_equal 105, c[6]
357
+ assert_equal 60, c[2]
358
+ end
359
+
360
+ def test_should_group_by_summed_field_having_condition_from_select
361
+ skip unless current_adapter?(:Mysql2Adapter, :SQLite3Adapter)
362
+ c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit)
363
+ assert_nil c[1]
364
+ assert_equal 60, c[2]
365
+ assert_equal 53, c[9]
366
+ end
367
+
368
+ def test_should_group_by_summed_association
369
+ c = Account.group(:firm).sum(:credit_limit)
370
+ assert_equal 50, c[companies(:first_firm)]
371
+ assert_equal 105, c[companies(:rails_core)]
372
+ assert_equal 60, c[companies(:first_client)]
373
+ end
374
+
375
+ def test_should_sum_field_with_conditions
376
+ assert_equal 105, Account.where("firm_id = 6").sum(:credit_limit)
377
+ end
378
+
379
+ def test_should_return_zero_if_sum_conditions_return_nothing
380
+ assert_equal 0, Account.where("1 = 2").sum(:credit_limit)
381
+ assert_equal 0, companies(:rails_core).companies.where("1 = 2").sum(:id)
382
+ end
383
+
384
+ def test_sum_should_return_valid_values_for_decimals
385
+ NumericData.create(bank_balance: 19.83)
386
+ assert_equal 19.83, NumericData.sum(:bank_balance)
387
+ end
388
+
389
+ def test_should_return_type_casted_values_with_group_and_expression
390
+ assert_equal 0.5, Account.group(:firm_name).sum("0.01 * credit_limit")["37signals"]
391
+ end
392
+
393
+ def test_should_group_by_summed_field_with_conditions
394
+ c = Account.where("firm_id > 1").group(:firm_id).sum(:credit_limit)
395
+ assert_nil c[1]
396
+ assert_equal 105, c[6]
397
+ assert_equal 60, c[2]
398
+ end
399
+
400
+ def test_should_group_by_summed_field_with_conditions_and_having
401
+ c = Account.where("firm_id > 1").group(:firm_id).
402
+ having("sum(credit_limit) > 60").sum(:credit_limit)
403
+ assert_nil c[1]
404
+ assert_equal 105, c[6]
405
+ assert_nil c[2]
406
+ end
407
+
408
+ def test_should_group_by_fields_with_table_alias
409
+ c = Account.group("accounts.firm_id").sum(:credit_limit)
410
+ assert_equal 50, c[1]
411
+ assert_equal 105, c[6]
412
+ assert_equal 60, c[2]
413
+ end
414
+
415
+ def test_should_calculate_grouped_with_longer_field
416
+ field = "a" * Account.connection.max_identifier_length
417
+
418
+ Account.update_all("#{field} = credit_limit")
419
+
420
+ c = Account.group(:firm_id).sum(field)
421
+ assert_equal 50, c[1]
422
+ assert_equal 105, c[6]
423
+ assert_equal 60, c[2]
424
+ end
425
+
426
+ def test_should_calculate_with_invalid_field
427
+ assert_equal 6, Account.calculate(:count, "*")
428
+ assert_equal 6, Account.calculate(:count, :all)
429
+ end
430
+
431
+ def test_should_calculate_grouped_with_invalid_field
432
+ c = Account.group("accounts.firm_id").count(:all)
433
+ assert_equal 1, c[1]
434
+ assert_equal 2, c[6]
435
+ assert_equal 1, c[2]
436
+ end
437
+
438
+ def test_should_calculate_grouped_association_with_invalid_field
439
+ c = Account.group(:firm).count(:all)
440
+ assert_equal 1, c[companies(:first_firm)]
441
+ assert_equal 2, c[companies(:rails_core)]
442
+ assert_equal 1, c[companies(:first_client)]
443
+ end
444
+
445
+ def test_should_group_by_association_with_non_numeric_foreign_key
446
+ Speedometer.create! id: "ABC"
447
+ Minivan.create! id: "OMG", speedometer_id: "ABC"
448
+
449
+ c = Minivan.group(:speedometer).count(:all)
450
+ first_key = c.keys.first
451
+ assert_equal Speedometer, first_key.class
452
+ assert_equal 1, c[first_key]
453
+ end
454
+
455
+ def test_should_calculate_grouped_association_with_foreign_key_option
456
+ Account.belongs_to :another_firm, class_name: "Firm", foreign_key: "firm_id"
457
+ c = Account.group(:another_firm).count(:all)
458
+ assert_equal 1, c[companies(:first_firm)]
459
+ assert_equal 2, c[companies(:rails_core)]
460
+ assert_equal 1, c[companies(:first_client)]
461
+ end
462
+
463
+ def test_should_calculate_grouped_by_function
464
+ c = Company.group("UPPER(#{QUOTED_TYPE})").count(:all)
465
+ assert_equal 2, c[nil]
466
+ assert_equal 1, c["DEPENDENTFIRM"]
467
+ assert_equal 5, c["CLIENT"]
468
+ assert_equal 3, c["FIRM"]
469
+ end
470
+
471
+ def test_should_calculate_grouped_by_function_with_table_alias
472
+ c = Company.group("UPPER(companies.#{QUOTED_TYPE})").count(:all)
473
+ assert_equal 2, c[nil]
474
+ assert_equal 1, c["DEPENDENTFIRM"]
475
+ assert_equal 5, c["CLIENT"]
476
+ assert_equal 3, c["FIRM"]
477
+ end
478
+
479
+ def test_should_not_overshadow_enumerable_sum
480
+ assert_equal 6, [1, 2, 3].sum(&:abs)
481
+ end
482
+
483
+ def test_should_sum_scoped_field
484
+ assert_equal 15, companies(:rails_core).companies.sum(:id)
485
+ end
486
+
487
+ def test_should_sum_scoped_field_with_from
488
+ assert_equal Club.count, Organization.clubs.count
489
+ end
490
+
491
+ def test_should_sum_scoped_field_with_conditions
492
+ assert_equal 8, companies(:rails_core).companies.where("id > 7").sum(:id)
493
+ end
494
+
495
+ def test_should_group_by_scoped_field
496
+ c = companies(:rails_core).companies.group(:name).sum(:id)
497
+ assert_equal 7, c["Leetsoft"]
498
+ assert_equal 8, c["Jadedpixel"]
499
+ end
500
+
501
+ def test_should_group_by_summed_field_through_association_and_having
502
+ c = companies(:rails_core).companies.group(:name).having("sum(id) > 7").sum(:id)
503
+ assert_nil c["Leetsoft"]
504
+ assert_equal 8, c["Jadedpixel"]
505
+ end
506
+
507
+ def test_should_count_selected_field_with_include
508
+ assert_equal 6, Account.includes(:firm).distinct.count
509
+ assert_equal 4, Account.includes(:firm).distinct.select(:credit_limit).count
510
+ assert_equal 4, Account.includes(:firm).distinct.count("DISTINCT credit_limit")
511
+ assert_equal 4, Account.includes(:firm).distinct.count("DISTINCT(credit_limit)")
512
+ end
513
+
514
+ def test_should_not_perform_joined_include_by_default
515
+ assert_equal Account.count, Account.includes(:firm).count
516
+ queries = capture_sql { Account.includes(:firm).count }
517
+ assert_no_match(/join/i, queries.last)
518
+ end
519
+
520
+ def test_should_perform_joined_include_when_referencing_included_tables
521
+ joined_count = Account.includes(:firm).where(companies: { name: "37signals" }).count
522
+ assert_equal 1, joined_count
523
+ end
524
+
525
+ def test_should_count_scoped_select
526
+ Account.update_all("credit_limit = NULL")
527
+ assert_equal 0, Account.select("credit_limit").count
528
+ end
529
+
530
+ def test_should_count_scoped_select_with_options
531
+ Account.update_all("credit_limit = NULL")
532
+ Account.last.update_columns("credit_limit" => 49)
533
+ Account.first.update_columns("credit_limit" => 51)
534
+
535
+ assert_equal 1, Account.select("credit_limit").where("credit_limit >= 50").count
536
+ end
537
+
538
+ def test_should_count_manual_select_with_include
539
+ assert_equal 6, Account.select("DISTINCT accounts.id").includes(:firm).count
540
+ end
541
+
542
+ def test_should_count_manual_select_with_count_all
543
+ assert_equal 5, Account.select("DISTINCT accounts.firm_id").count(:all)
544
+ end
545
+
546
+ def test_should_count_with_manual_distinct_select_and_distinct
547
+ assert_equal 4, Account.select("DISTINCT accounts.firm_id").distinct(true).count
548
+ end
549
+
550
+ def test_should_count_manual_select_with_group_with_count_all
551
+ expected = { nil => 1, 1 => 1, 2 => 1, 6 => 2, 9 => 1 }
552
+ actual = Account.select("DISTINCT accounts.firm_id").group("accounts.firm_id").count(:all)
553
+ assert_equal expected, actual
554
+ end
555
+
556
+ def test_should_count_manual_with_count_all
557
+ assert_equal 6, Account.count(:all)
558
+ end
559
+
560
+ def test_count_selected_arel_attribute
561
+ assert_equal 5, Account.select(Account.arel_table[:firm_id]).count
562
+ assert_equal 4, Account.distinct.select(Account.arel_table[:firm_id]).count
563
+ end
564
+
565
+ def test_count_with_column_parameter
566
+ assert_equal 5, Account.count(:firm_id)
567
+ end
568
+
569
+ def test_count_with_arel_attribute
570
+ assert_equal 5, Account.count(Account.arel_table[:firm_id])
571
+ end
572
+
573
+ def test_count_with_arel_star
574
+ assert_equal 6, Account.count(Arel.star)
575
+ end
576
+
577
+ def test_count_with_distinct
578
+ assert_equal 4, Account.select(:credit_limit).distinct.count
579
+ end
580
+
581
+ def test_count_with_aliased_attribute
582
+ assert_equal 6, Account.count(:available_credit)
583
+ end
584
+
585
+ def test_count_with_column_and_options_parameter
586
+ assert_equal 2, Account.where("credit_limit = 50 AND firm_id IS NOT NULL").count(:firm_id)
587
+ end
588
+
589
+ def test_should_count_field_in_joined_table
590
+ assert_equal 5, Account.joins(:firm).count("companies.id")
591
+ assert_equal 4, Account.joins(:firm).distinct.count("companies.id")
592
+ end
593
+
594
+ def test_count_arel_attribute_in_joined_table_with
595
+ assert_equal 5, Account.joins(:firm).count(Company.arel_table[:id])
596
+ assert_equal 4, Account.joins(:firm).distinct.count(Company.arel_table[:id])
597
+ end
598
+
599
+ def test_count_selected_arel_attribute_in_joined_table
600
+ assert_equal 5, Account.joins(:firm).select(Company.arel_table[:id]).count
601
+ assert_equal 4, Account.joins(:firm).distinct.select(Company.arel_table[:id]).count
602
+ end
603
+
604
+ def test_should_count_field_in_joined_table_with_group_by
605
+ c = Account.group("accounts.firm_id").joins(:firm).count("companies.id")
606
+
607
+ [1, 6, 2, 9].each { |firm_id| assert_includes c.keys, firm_id }
608
+ end
609
+
610
+ def test_should_count_field_of_root_table_with_conflicting_group_by_column
611
+ expected = { 1 => 2, 2 => 1, 4 => 5, 5 => 3, 7 => 1 }
612
+ assert_equal expected, Post.joins(:comments).group(:post_id).count
613
+ assert_equal expected, Post.joins(:comments).group("comments.post_id").count
614
+ assert_equal expected, Post.joins(:comments).group(:post_id).select("DISTINCT posts.author_id").count(:all)
615
+ end
616
+
617
+ def test_count_with_no_parameters_isnt_deprecated
618
+ assert_not_deprecated { Account.count }
619
+ end
620
+
621
+ def test_count_with_too_many_parameters_raises
622
+ assert_raise(ArgumentError) { Account.count(1, 2, 3) }
623
+ end
624
+
625
+ def test_count_with_order
626
+ assert_equal 6, Account.order(:credit_limit).count
627
+ end
628
+
629
+ def test_count_with_reverse_order
630
+ assert_equal 6, Account.order(:credit_limit).reverse_order.count
631
+ end
632
+
633
+ def test_count_with_where_and_order
634
+ assert_equal 1, Account.where(firm_name: "37signals").count
635
+ assert_equal 1, Account.where(firm_name: "37signals").order(:firm_name).count
636
+ assert_equal 1, Account.where(firm_name: "37signals").order(:firm_name).reverse_order.count
637
+ end
638
+
639
+ def test_count_with_block
640
+ assert_equal 4, Account.count { |account| account.credit_limit.modulo(10).zero? }
641
+ end
642
+
643
+ def test_should_sum_expression
644
+ assert_equal 636, Account.sum("2 * credit_limit")
645
+ end
646
+
647
+ def test_sum_expression_returns_zero_when_no_records_to_sum
648
+ assert_equal 0, Account.where("1 = 2").sum("2 * credit_limit")
649
+ end
650
+
651
+ def test_count_with_from_option
652
+ assert_equal Company.count(:all), Company.from("companies").count(:all)
653
+ assert_equal Account.where("credit_limit = 50").count(:all),
654
+ Account.from("accounts").where("credit_limit = 50").count(:all)
655
+ assert_equal Company.where(type: "Firm").count(:type),
656
+ Company.where(type: "Firm").from("companies").count(:type)
657
+ end
658
+
659
+ def test_sum_with_from_option
660
+ assert_equal Account.sum(:credit_limit), Account.from("accounts").sum(:credit_limit)
661
+ assert_equal Account.where("credit_limit > 50").sum(:credit_limit),
662
+ Account.where("credit_limit > 50").from("accounts").sum(:credit_limit)
663
+ end
664
+
665
+ def test_average_with_from_option
666
+ assert_equal Account.average(:credit_limit), Account.from("accounts").average(:credit_limit)
667
+ assert_equal Account.where("credit_limit > 50").average(:credit_limit),
668
+ Account.where("credit_limit > 50").from("accounts").average(:credit_limit)
669
+ end
670
+
671
+ def test_minimum_with_from_option
672
+ assert_equal Account.minimum(:credit_limit), Account.from("accounts").minimum(:credit_limit)
673
+ assert_equal Account.where("credit_limit > 50").minimum(:credit_limit),
674
+ Account.where("credit_limit > 50").from("accounts").minimum(:credit_limit)
675
+ end
676
+
677
+ def test_maximum_with_from_option
678
+ assert_equal Account.maximum(:credit_limit), Account.from("accounts").maximum(:credit_limit)
679
+ assert_equal Account.where("credit_limit > 50").maximum(:credit_limit),
680
+ Account.where("credit_limit > 50").from("accounts").maximum(:credit_limit)
681
+ end
682
+
683
+ def test_maximum_with_not_auto_table_name_prefix_if_column_included
684
+ Company.create!(name: "test", contracts: [Contract.new(developer_id: 7)])
685
+
686
+ assert_equal 7, Company.includes(:contracts).maximum(:developer_id)
687
+ end
688
+
689
+ def test_minimum_with_not_auto_table_name_prefix_if_column_included
690
+ Company.create!(name: "test", contracts: [Contract.new(developer_id: 7)])
691
+
692
+ assert_equal 7, Company.includes(:contracts).minimum(:developer_id)
693
+ end
694
+
695
+ def test_sum_with_not_auto_table_name_prefix_if_column_included
696
+ Company.create!(name: "test", contracts: [Contract.new(developer_id: 7)])
697
+
698
+ assert_equal 7, Company.includes(:contracts).sum(:developer_id)
699
+ end
700
+
701
+ def test_from_option_with_specified_index
702
+ edges = Edge.from("edges /*! USE INDEX(unique_edge_index) */")
703
+ assert_equal Edge.count(:all), edges.count(:all)
704
+ assert_equal Edge.where("sink_id < 5").count(:all), edges.where("sink_id < 5").count(:all)
705
+ end
706
+
707
+ def test_from_option_with_table_different_than_class
708
+ assert_equal Account.count(:all), Company.from("accounts").count(:all)
709
+ end
710
+
711
+ def test_distinct_is_honored_when_used_with_count_operation_after_group
712
+ # Count the number of authors for approved topics
713
+ approved_topics_count = Topic.group(:approved).count(:author_name)[true]
714
+ assert_equal approved_topics_count, 4
715
+ # Count the number of distinct authors for approved Topics
716
+ distinct_authors_for_approved_count = Topic.group(:approved).distinct.count(:author_name)[true]
717
+ assert_equal distinct_authors_for_approved_count, 3
718
+ end
719
+
720
+ def test_pluck
721
+ assert_equal [1, 2, 3, 4, 5], Topic.order(:id).pluck(:id)
722
+ end
723
+
724
+ def test_pluck_with_empty_in
725
+ assert_queries(0) do
726
+ assert_equal [], Topic.where(id: []).pluck(:id)
727
+ end
728
+ end
729
+
730
+ def test_pluck_without_column_names
731
+ if current_adapter?(:OracleAdapter)
732
+ assert_equal [[1, "Firm", 1, nil, "37signals", nil, 1, nil, nil]], Company.order(:id).limit(1).pluck
733
+ else
734
+ assert_equal [[1, "Firm", 1, nil, "37signals", nil, 1, nil, ""]], Company.order(:id).limit(1).pluck
735
+ end
736
+ end
737
+
738
+ def test_pluck_type_cast
739
+ topic = topics(:first)
740
+ relation = Topic.where(id: topic.id)
741
+ assert_equal [ topic.approved ], relation.pluck(:approved)
742
+ assert_equal [ topic.last_read ], relation.pluck(:last_read)
743
+ assert_equal [ topic.written_on ], relation.pluck(:written_on)
744
+ end
745
+
746
+ def test_pluck_type_cast_with_conflict_column_names
747
+ expected = [
748
+ [Date.new(2004, 4, 15), "unread"],
749
+ [Date.new(2004, 4, 15), "reading"],
750
+ [Date.new(2004, 4, 15), "read"],
751
+ ]
752
+ actual = AuthorAddress.joins(author: [:topics, :books]).order(:"books.last_read")
753
+ .where("books.last_read": [:unread, :reading, :read])
754
+ .pluck(:"topics.last_read", :"books.last_read")
755
+
756
+ assert_equal expected, actual
757
+ end
758
+
759
+ def test_pluck_type_cast_with_joins_without_table_name_qualified_column
760
+ assert_pluck_type_cast_without_table_name_qualified_column(AuthorAddress.joins(author: :books))
761
+ end
762
+
763
+ def test_pluck_type_cast_with_left_joins_without_table_name_qualified_column
764
+ assert_pluck_type_cast_without_table_name_qualified_column(AuthorAddress.left_joins(author: :books))
765
+ end
766
+
767
+ def test_pluck_type_cast_with_eager_load_without_table_name_qualified_column
768
+ assert_pluck_type_cast_without_table_name_qualified_column(AuthorAddress.eager_load(author: :books))
769
+ end
770
+
771
+ def assert_pluck_type_cast_without_table_name_qualified_column(author_addresses)
772
+ expected = [
773
+ [nil, "unread"],
774
+ ["ebook", "reading"],
775
+ ["paperback", "read"],
776
+ ]
777
+ actual = author_addresses.order(:last_read)
778
+ .where("books.last_read": [:unread, :reading, :read])
779
+ .pluck(:format, :last_read)
780
+
781
+ assert_equal expected, actual
782
+ end
783
+ private :assert_pluck_type_cast_without_table_name_qualified_column
784
+
785
+ def test_pluck_with_type_cast_does_not_corrupt_the_query_cache
786
+ topic = topics(:first)
787
+ relation = Topic.where(id: topic.id)
788
+ assert_queries 1 do
789
+ Topic.cache do
790
+ kind = relation.select(:written_on).load.first.read_attribute_before_type_cast(:written_on).class
791
+ relation.pluck(:written_on)
792
+ assert_kind_of kind, relation.select(:written_on).load.first.read_attribute_before_type_cast(:written_on)
793
+ end
794
+ end
795
+ end
796
+
797
+ def test_pluck_and_distinct
798
+ assert_equal [50, 53, 55, 60], Account.order(:credit_limit).distinct.pluck(:credit_limit)
799
+ end
800
+
801
+ def test_pluck_in_relation
802
+ company = Company.first
803
+ contract = company.contracts.create!
804
+ assert_equal [contract.id], company.contracts.pluck(:id)
805
+ end
806
+
807
+ def test_pluck_on_aliased_attribute
808
+ assert_equal "The First Topic", Topic.order(:id).pluck(:heading).first
809
+ end
810
+
811
+ def test_pluck_with_serialization
812
+ t = Topic.create!(content: { foo: :bar })
813
+ assert_equal [{ foo: :bar }], Topic.where(id: t.id).pluck(:content)
814
+ end
815
+
816
+ def test_pluck_with_qualified_column_name
817
+ assert_equal [1, 2, 3, 4, 5], Topic.order(:id).pluck("topics.id")
818
+ end
819
+
820
+ def test_pluck_auto_table_name_prefix
821
+ c = Company.create!(name: "test", contracts: [Contract.new])
822
+ assert_equal [c.id], Company.joins(:contracts).pluck(:id)
823
+ end
824
+
825
+ def test_pluck_if_table_included
826
+ c = Company.create!(name: "test", contracts: [Contract.new(developer_id: 7)])
827
+ assert_equal [c.id], Company.includes(:contracts).where("contracts.id" => c.contracts.first).pluck(:id)
828
+ end
829
+
830
+ def test_pluck_not_auto_table_name_prefix_if_column_joined
831
+ company = Company.create!(name: "test", contracts: [Contract.new(developer_id: 7)])
832
+ metadata = company.contracts.first.metadata
833
+ assert_equal [metadata], Company.joins(:contracts).pluck(:metadata)
834
+ end
835
+
836
+ def test_pluck_with_selection_clause
837
+ assert_equal [50, 53, 55, 60], Account.pluck(Arel.sql("DISTINCT credit_limit")).sort
838
+ assert_equal [50, 53, 55, 60], Account.pluck(Arel.sql("DISTINCT accounts.credit_limit")).sort
839
+ assert_equal [50, 53, 55, 60], Account.pluck(Arel.sql("DISTINCT(credit_limit)")).sort
840
+ assert_equal [50 + 53 + 55 + 60], Account.pluck(Arel.sql("SUM(DISTINCT(credit_limit))"))
841
+ end
842
+
843
+ def test_plucks_with_ids
844
+ assert_equal Company.all.map(&:id).sort, Company.ids.sort
845
+ end
846
+
847
+ def test_pluck_with_includes_limit_and_empty_result
848
+ assert_equal [], Topic.includes(:replies).limit(0).pluck(:id)
849
+ assert_equal [], Topic.includes(:replies).limit(1).where("0 = 1").pluck(:id)
850
+ end
851
+
852
+ def test_pluck_with_includes_offset
853
+ assert_equal [5], Topic.includes(:replies).order(:id).offset(4).pluck(:id)
854
+ assert_equal [], Topic.includes(:replies).order(:id).offset(5).pluck(:id)
855
+ end
856
+
857
+ def test_pluck_with_join
858
+ assert_equal [[2, 2], [4, 4]], Reply.includes(:topic).order(:id).pluck(:id, :"topics.id")
859
+ end
860
+
861
+ def test_group_by_with_order_by_virtual_count_attribute
862
+ expected = { "SpecialPost" => 1, "StiPost" => 2 }
863
+ actual = Post.group(:type).order(:count).limit(2).maximum(:comments_count)
864
+ assert_equal expected, actual
865
+ end if current_adapter?(:PostgreSQLAdapter)
866
+
867
+ def test_group_by_with_limit
868
+ expected = { "StiPost" => 3, "SpecialPost" => 1 }
869
+ actual = Post.includes(:comments).group(:type).order(type: :desc).limit(2).count("comments.id")
870
+ assert_equal expected, actual
871
+ end
872
+
873
+ def test_group_by_with_offset
874
+ expected = { "SpecialPost" => 1, "Post" => 8 }
875
+ actual = Post.includes(:comments).group(:type).order(type: :desc).offset(1).count("comments.id")
876
+ assert_equal expected, actual
877
+ end
878
+
879
+ def test_group_by_with_limit_and_offset
880
+ expected = { "SpecialPost" => 1 }
881
+ actual = Post.includes(:comments).group(:type).order(type: :desc).offset(1).limit(1).count("comments.id")
882
+ assert_equal expected, actual
883
+ end
884
+
885
+ def test_group_by_with_quoted_count_and_order_by_alias
886
+ quoted_posts_id = Post.connection.quote_table_name("posts.id")
887
+ expected = { "SpecialPost" => 1, "StiPost" => 1, "Post" => 9 }
888
+ actual = Post.group(:type).order("count_posts_id").count(quoted_posts_id)
889
+ assert_equal expected, actual
890
+ end
891
+
892
+ def test_pluck_not_auto_table_name_prefix_if_column_included
893
+ Company.create!(name: "test", contracts: [Contract.new(developer_id: 7)])
894
+ ids = Company.includes(:contracts).pluck(:developer_id)
895
+ assert_equal Company.count, ids.length
896
+ assert_equal [7], ids.compact
897
+ end
898
+
899
+ def test_pluck_multiple_columns
900
+ assert_equal [
901
+ [1, "The First Topic"], [2, "The Second Topic of the day"],
902
+ [3, "The Third Topic of the day"], [4, "The Fourth Topic of the day"],
903
+ [5, "The Fifth Topic of the day"]
904
+ ], Topic.order(:id).pluck(:id, :title)
905
+ assert_equal [
906
+ [1, "The First Topic", "David"], [2, "The Second Topic of the day", "Mary"],
907
+ [3, "The Third Topic of the day", "Carl"], [4, "The Fourth Topic of the day", "Carl"],
908
+ [5, "The Fifth Topic of the day", "Jason"]
909
+ ], Topic.order(:id).pluck(:id, :title, :author_name)
910
+ end
911
+
912
+ def test_pluck_with_multiple_columns_and_selection_clause
913
+ assert_equal [[1, 50], [2, 50], [3, 50], [4, 60], [5, 55], [6, 53]],
914
+ Account.order(:id).pluck("id, credit_limit")
915
+ end
916
+
917
+ def test_pluck_with_multiple_columns_and_includes
918
+ Company.create!(name: "test", contracts: [Contract.new(developer_id: 7)])
919
+ companies_and_developers = Company.order("companies.id").includes(:contracts).pluck(:name, :developer_id)
920
+
921
+ assert_equal Company.count, companies_and_developers.length
922
+ assert_equal ["37signals", nil], companies_and_developers.first
923
+ assert_equal ["test", 7], companies_and_developers.last
924
+ end
925
+
926
+ def test_pluck_with_reserved_words
927
+ Possession.create!(where: "Over There")
928
+
929
+ assert_equal ["Over There"], Possession.pluck(:where)
930
+ end
931
+
932
+ def test_pluck_replaces_select_clause
933
+ taks_relation = Topic.select(:approved, :id).order(:id)
934
+ assert_equal [1, 2, 3, 4, 5], taks_relation.pluck(:id)
935
+ assert_equal [false, true, true, true, true], taks_relation.pluck(:approved)
936
+ end
937
+
938
+ def test_pluck_columns_with_same_name
939
+ expected = [["The First Topic", "The Second Topic of the day"], ["The Third Topic of the day", "The Fourth Topic of the day"]]
940
+ actual = Topic.joins(:replies).order(:id)
941
+ .pluck("topics.title", "replies_topics.title")
942
+ assert_equal expected, actual
943
+ end
944
+
945
+ def test_pluck_functions_with_alias
946
+ assert_equal [
947
+ [1, "The First Topic"], [2, "The Second Topic of the day"],
948
+ [3, "The Third Topic of the day"], [4, "The Fourth Topic of the day"],
949
+ [5, "The Fifth Topic of the day"]
950
+ ], Topic.order(:id).pluck(
951
+ Arel.sql("COALESCE(id, 0) id"),
952
+ Arel.sql("COALESCE(title, 'untitled') title")
953
+ )
954
+ end
955
+
956
+ def test_pluck_functions_without_alias
957
+ assert_equal [
958
+ [1, "The First Topic"], [2, "The Second Topic of the day"],
959
+ [3, "The Third Topic of the day"], [4, "The Fourth Topic of the day"],
960
+ [5, "The Fifth Topic of the day"]
961
+ ], Topic.order(:id).pluck(
962
+ Arel.sql("COALESCE(id, 0)"),
963
+ Arel.sql("COALESCE(title, 'untitled')")
964
+ )
965
+ end
966
+
967
+ def test_calculation_with_polymorphic_relation
968
+ part = ShipPart.create!(name: "has trinket")
969
+ part.trinkets.create!
970
+
971
+ assert_equal part.id, ShipPart.joins(:trinkets).sum(:id)
972
+ end
973
+
974
+ def test_pluck_joined_with_polymorphic_relation
975
+ part = ShipPart.create!(name: "has trinket")
976
+ part.trinkets.create!
977
+
978
+ assert_equal [part.id], ShipPart.joins(:trinkets).pluck(:id)
979
+ end
980
+
981
+ def test_pluck_loaded_relation
982
+ companies = Company.order(:id).limit(3).load
983
+
984
+ assert_queries(0) do
985
+ assert_equal ["37signals", "Summit", "Microsoft"], companies.pluck(:name)
986
+ end
987
+ end
988
+
989
+ def test_pluck_loaded_relation_multiple_columns
990
+ companies = Company.order(:id).limit(3).load
991
+
992
+ assert_queries(0) do
993
+ assert_equal [[1, "37signals"], [2, "Summit"], [3, "Microsoft"]], companies.pluck(:id, :name)
994
+ end
995
+ end
996
+
997
+ def test_pluck_loaded_relation_sql_fragment
998
+ companies = Company.order(:name).limit(3).load
999
+
1000
+ assert_queries(1) do
1001
+ assert_equal ["37signals", "Apex", "Ex Nihilo"], companies.pluck(Arel.sql("DISTINCT name"))
1002
+ end
1003
+ end
1004
+
1005
+ def test_pluck_loaded_relation_aliased_attribute
1006
+ companies = Company.order(:id).limit(3).load
1007
+
1008
+ assert_queries(0) do
1009
+ assert_equal ["37signals", "Summit", "Microsoft"], companies.pluck(:new_name)
1010
+ end
1011
+ end
1012
+
1013
+ def test_pick_one
1014
+ assert_equal "The First Topic", Topic.order(:id).pick(:heading)
1015
+ assert_no_queries do
1016
+ assert_nil Topic.none.pick(:heading)
1017
+ assert_nil Topic.where(id: 9999999999999999999).pick(:heading)
1018
+ end
1019
+ end
1020
+
1021
+ def test_pick_two
1022
+ assert_equal ["David", "david@loudthinking.com"], Topic.order(:id).pick(:author_name, :author_email_address)
1023
+ assert_no_queries do
1024
+ assert_nil Topic.none.pick(:author_name, :author_email_address)
1025
+ assert_nil Topic.where(id: 9999999999999999999).pick(:author_name, :author_email_address)
1026
+ end
1027
+ end
1028
+
1029
+ def test_pick_delegate_to_all
1030
+ cool_first = minivans(:cool_first)
1031
+ assert_equal cool_first.color, Minivan.pick(:color)
1032
+ end
1033
+
1034
+ def test_pick_loaded_relation
1035
+ companies = Company.order(:id).limit(3).load
1036
+
1037
+ assert_no_queries do
1038
+ assert_equal "37signals", companies.pick(:name)
1039
+ end
1040
+ end
1041
+
1042
+ def test_pick_loaded_relation_multiple_columns
1043
+ companies = Company.order(:id).limit(3).load
1044
+
1045
+ assert_no_queries do
1046
+ assert_equal [1, "37signals"], companies.pick(:id, :name)
1047
+ end
1048
+ end
1049
+
1050
+ def test_pick_loaded_relation_sql_fragment
1051
+ companies = Company.order(:name).limit(3).load
1052
+
1053
+ assert_queries 1 do
1054
+ assert_equal "37signals", companies.pick(Arel.sql("DISTINCT name"))
1055
+ end
1056
+ end
1057
+
1058
+ def test_pick_loaded_relation_aliased_attribute
1059
+ companies = Company.order(:id).limit(3).load
1060
+
1061
+ assert_no_queries do
1062
+ assert_equal "37signals", companies.pick(:new_name)
1063
+ end
1064
+ end
1065
+
1066
+ def test_grouped_calculation_with_polymorphic_relation
1067
+ part = ShipPart.create!(name: "has trinket")
1068
+ part.trinkets.create!
1069
+
1070
+ assert_equal({ "has trinket" => part.id }, ShipPart.joins(:trinkets).group("ship_parts.name").sum(:id))
1071
+ end
1072
+
1073
+ def test_calculation_grouped_by_association_doesnt_error_when_no_records_have_association
1074
+ Client.update_all(client_of: nil)
1075
+ assert_equal({ nil => Client.count }, Client.group(:firm).count)
1076
+ end
1077
+
1078
+ def test_should_reference_correct_aliases_while_joining_tables_of_has_many_through_association
1079
+ assert_nothing_raised do
1080
+ developer = Developer.create!(name: "developer")
1081
+ developer.ratings.includes(comment: :post).where(posts: { id: 1 }).count
1082
+ end
1083
+ end
1084
+
1085
+ def test_sum_uses_enumerable_version_when_block_is_given
1086
+ block_called = false
1087
+ relation = Client.all.load
1088
+
1089
+ assert_no_queries do
1090
+ assert_equal 0, relation.sum { block_called = true; 0 }
1091
+ end
1092
+ assert block_called
1093
+ end
1094
+
1095
+ def test_having_with_strong_parameters
1096
+ params = ProtectedParams.new(credit_limit: "50")
1097
+
1098
+ assert_raises(ActiveModel::ForbiddenAttributesError) do
1099
+ Account.group(:id).having(params)
1100
+ end
1101
+
1102
+ result = Account.group(:id, :firm_id, :firm_name, :credit_limit, :AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA).having(params.permit!)
1103
+ assert_equal 50, result[0].credit_limit
1104
+ assert_equal 50, result[1].credit_limit
1105
+ assert_equal 50, result[2].credit_limit
1106
+ end
1107
+
1108
+ def test_count_takes_attribute_type_precedence_over_database_type
1109
+ assert_called(
1110
+ Account.connection, :select_all,
1111
+ returns: ActiveRecord::Result.new(["count"], [["10"]])
1112
+ ) do
1113
+ result = Account.count
1114
+ assert_equal 10, result
1115
+ assert_instance_of Integer, result
1116
+ end
1117
+ end
1118
+
1119
+ def test_sum_takes_attribute_type_precedence_over_database_type
1120
+ assert_called(
1121
+ Account.connection, :select_all,
1122
+ returns: ActiveRecord::Result.new(["sum"], [[10.to_d]])
1123
+ ) do
1124
+ result = Account.sum(:credit_limit)
1125
+ assert_equal 10, result
1126
+ assert_instance_of Integer, result
1127
+ end
1128
+ end
1129
+
1130
+ def test_group_by_attribute_with_custom_type
1131
+ assert_equal({ "proposed" => 2, "published" => 2 }, Book.group(:status).count)
1132
+ end
1133
+
1134
+ def test_aggregate_attribute_on_enum_type
1135
+ assert_equal 4, Book.sum(:status)
1136
+ assert_equal 1, Book.sum(:difficulty)
1137
+ assert_equal 0, Book.minimum(:difficulty)
1138
+ assert_equal 1, Book.maximum(:difficulty)
1139
+ assert_equal({ "proposed" => 0, "published" => 4 }, Book.group(:status).sum(:status))
1140
+ assert_equal({ "proposed" => 0, "published" => 1 }, Book.group(:status).sum(:difficulty))
1141
+ assert_equal({ "proposed" => 0, "published" => 0 }, Book.group(:status).minimum(:difficulty))
1142
+ assert_equal({ "proposed" => 0, "published" => 1 }, Book.group(:status).maximum(:difficulty))
1143
+ end
1144
+
1145
+ def test_minimum_and_maximum_on_non_numeric_type
1146
+ assert_equal Date.new(2004, 4, 15), Topic.minimum(:last_read)
1147
+ assert_equal Date.new(2004, 4, 15), Topic.maximum(:last_read)
1148
+ assert_equal({ false => Date.new(2004, 4, 15), true => nil }, Topic.group(:approved).minimum(:last_read))
1149
+ assert_equal({ false => Date.new(2004, 4, 15), true => nil }, Topic.group(:approved).maximum(:last_read))
1150
+ end
1151
+
1152
+ def test_minimum_and_maximum_on_time_attributes
1153
+ assert_minimum_and_maximum_on_time_attributes(Time)
1154
+ end
1155
+
1156
+ def test_minimum_and_maximum_on_tz_aware_attributes
1157
+ with_timezone_config aware_attributes: true, zone: "Pacific Time (US & Canada)" do
1158
+ Topic.reset_column_information
1159
+ assert_minimum_and_maximum_on_time_attributes(ActiveSupport::TimeWithZone)
1160
+ end
1161
+ ensure
1162
+ Topic.reset_column_information
1163
+ end
1164
+
1165
+ def assert_minimum_and_maximum_on_time_attributes(time_class)
1166
+ actual = Topic.minimum(:written_on)
1167
+ assert_equal Time.utc(2003, 7, 16, 14, 28, 11, 223300), actual
1168
+ assert_instance_of time_class, actual
1169
+
1170
+ actual = Topic.maximum(:written_on)
1171
+ assert_equal Time.utc(2013, 7, 13, 11, 11, 0, 9900), actual
1172
+ assert_instance_of time_class, actual
1173
+
1174
+ expected = {
1175
+ false => Time.utc(2003, 7, 16, 14, 28, 11, 223300),
1176
+ true => Time.utc(2004, 7, 15, 14, 28, 0, 9900),
1177
+ }
1178
+ actual = Topic.group(:approved).minimum(:written_on)
1179
+ assert_equal expected, actual
1180
+ assert_instance_of time_class, actual[true]
1181
+ assert_instance_of time_class, actual[true]
1182
+
1183
+ expected = {
1184
+ false => Time.utc(2003, 7, 16, 14, 28, 11, 223300),
1185
+ true => Time.utc(2013, 7, 13, 11, 11, 0, 9900),
1186
+ }
1187
+ actual = Topic.group(:approved).maximum(:written_on)
1188
+ assert_equal expected, actual
1189
+ assert_instance_of time_class, actual[true]
1190
+ assert_instance_of time_class, actual[true]
1191
+
1192
+ assert_minimum_and_maximum_on_time_attributes_joins_with_column(time_class, :"topics.written_on")
1193
+ assert_minimum_and_maximum_on_time_attributes_joins_with_column(time_class, :written_on)
1194
+ end
1195
+ private :assert_minimum_and_maximum_on_time_attributes
1196
+
1197
+ def assert_minimum_and_maximum_on_time_attributes_joins_with_column(time_class, column)
1198
+ actual = Author.joins(:topics).maximum(column)
1199
+ assert_equal Time.utc(2004, 7, 15, 14, 28, 0, 9900), actual
1200
+ assert_instance_of time_class, actual
1201
+
1202
+ actual = Author.joins(:topics).minimum(column)
1203
+ assert_equal Time.utc(2003, 7, 16, 14, 28, 11, 223300), actual
1204
+ assert_instance_of time_class, actual
1205
+
1206
+ expected = {
1207
+ 1 => Time.utc(2003, 7, 16, 14, 28, 11, 223300),
1208
+ 2 => Time.utc(2004, 7, 15, 14, 28, 0, 9900),
1209
+ }
1210
+
1211
+ actual = Author.joins(:topics).group(:id).maximum(column)
1212
+ assert_equal expected, actual
1213
+ assert_instance_of time_class, actual[1]
1214
+ assert_instance_of time_class, actual[2]
1215
+
1216
+ actual = Author.joins(:topics).group(:id).minimum(column)
1217
+ assert_equal expected, actual
1218
+ assert_instance_of time_class, actual[1]
1219
+ assert_instance_of time_class, actual[2]
1220
+ end
1221
+ private :assert_minimum_and_maximum_on_time_attributes_joins_with_column
1222
+
1223
+ def test_select_avg_with_group_by_as_virtual_attribute_with_sql
1224
+ rails_core = companies(:rails_core)
1225
+
1226
+ sql = <<~SQL
1227
+ SELECT firm_id, AVG(credit_limit) AS avg_credit_limit
1228
+ FROM accounts
1229
+ WHERE firm_id = ?
1230
+ GROUP BY firm_id
1231
+ LIMIT 1
1232
+ SQL
1233
+
1234
+ account = Account.find_by_sql([sql, rails_core]).first
1235
+
1236
+ # id was not selected, so it should be nil
1237
+ # (cannot select id because it wasn't used in the GROUP BY clause)
1238
+ assert_nil account.id
1239
+
1240
+ # firm_id was explicitly selected, so it should be present
1241
+ assert_equal(rails_core, account.firm)
1242
+
1243
+ # avg_credit_limit should be present as a virtual attribute
1244
+ assert_equal(52.5, account.avg_credit_limit)
1245
+ end
1246
+
1247
+ def test_select_avg_with_group_by_as_virtual_attribute_with_ar
1248
+ rails_core = companies(:rails_core)
1249
+
1250
+ account = Account
1251
+ .select(:firm_id, "AVG(credit_limit) AS avg_credit_limit")
1252
+ .where(firm: rails_core)
1253
+ .group(:firm_id)
1254
+ .take!
1255
+
1256
+ # id was not selected, so it should be nil
1257
+ # (cannot select id because it wasn't used in the GROUP BY clause)
1258
+ assert_nil account.id
1259
+
1260
+ # firm_id was explicitly selected, so it should be present
1261
+ assert_equal(rails_core, account.firm)
1262
+
1263
+ # avg_credit_limit should be present as a virtual attribute
1264
+ assert_equal(52.5, account.avg_credit_limit)
1265
+ end
1266
+
1267
+ def test_select_avg_with_joins_and_group_by_as_virtual_attribute_with_sql
1268
+ rails_core = companies(:rails_core)
1269
+
1270
+ sql = <<~SQL
1271
+ SELECT companies.*, AVG(accounts.credit_limit) AS avg_credit_limit
1272
+ FROM companies
1273
+ INNER JOIN accounts ON companies.id = accounts.firm_id
1274
+ WHERE companies.id = ?
1275
+ GROUP BY companies.id, companies.type, companies.firm_id, companies.firm_name, companies.name, companies.client_of, companies.rating, companies.account_id,
1276
+ companies.DESCRIPTION
1277
+ LIMIT 1
1278
+ SQL
1279
+
1280
+ firm = DependentFirm.find_by_sql([sql, rails_core]).first
1281
+
1282
+ # all the DependentFirm attributes should be present
1283
+ assert_equal rails_core, firm
1284
+ assert_equal rails_core.name, firm.name
1285
+
1286
+ # avg_credit_limit should be present as a virtual attribute
1287
+ assert_equal(52.5, firm.avg_credit_limit)
1288
+ end
1289
+
1290
+ def test_select_avg_with_joins_and_group_by_as_virtual_attribute_with_ar
1291
+ rails_core = companies(:rails_core)
1292
+
1293
+ firm = DependentFirm
1294
+ .select("companies.*", "AVG(accounts.credit_limit) AS avg_credit_limit")
1295
+ .where(id: rails_core)
1296
+ .joins(:account)
1297
+ .group(:id, :type, :firm_id, :firm_name, :name, :client_of, :rating, :account_id, :description)
1298
+ .take!
1299
+
1300
+ # all the DependentFirm attributes should be present
1301
+ assert_equal rails_core, firm
1302
+ assert_equal rails_core.name, firm.name
1303
+
1304
+ # avg_credit_limit should be present as a virtual attribute
1305
+ assert_equal(52.5, firm.avg_credit_limit)
1306
+ end
1307
+
1308
+ def test_count_with_block_and_column_name_raises_an_error
1309
+ assert_raises(ArgumentError) do
1310
+ Account.count(:firm_id) { true }
1311
+ end
1312
+ end
1313
+
1314
+ def test_sum_with_block_and_column_name_raises_an_error
1315
+ assert_raises(ArgumentError) do
1316
+ Account.sum(:firm_id) { 1 }
1317
+ end
1318
+ end
1319
+
1320
+ test "#skip_query_cache! for #pluck" do
1321
+ Account.cache do
1322
+ assert_queries(1) do
1323
+ Account.pluck(:credit_limit)
1324
+ Account.pluck(:credit_limit)
1325
+ end
1326
+
1327
+ assert_queries(2) do
1328
+ Account.all.skip_query_cache!.pluck(:credit_limit)
1329
+ Account.all.skip_query_cache!.pluck(:credit_limit)
1330
+ end
1331
+ end
1332
+ end
1333
+
1334
+ test "#skip_query_cache! for a simple calculation" do
1335
+ Account.cache do
1336
+ assert_queries(1) do
1337
+ Account.calculate(:sum, :credit_limit)
1338
+ Account.calculate(:sum, :credit_limit)
1339
+ end
1340
+
1341
+ assert_queries(2) do
1342
+ Account.all.skip_query_cache!.calculate(:sum, :credit_limit)
1343
+ Account.all.skip_query_cache!.calculate(:sum, :credit_limit)
1344
+ end
1345
+ end
1346
+ end
1347
+
1348
+ test "#skip_query_cache! for a grouped calculation" do
1349
+ Account.cache do
1350
+ assert_queries(1) do
1351
+ Account.group(:firm_id).calculate(:sum, :credit_limit)
1352
+ Account.group(:firm_id).calculate(:sum, :credit_limit)
1353
+ end
1354
+
1355
+ assert_queries(2) do
1356
+ Account.all.skip_query_cache!.group(:firm_id).calculate(:sum, :credit_limit)
1357
+ Account.all.skip_query_cache!.group(:firm_id).calculate(:sum, :credit_limit)
1358
+ end
1359
+ end
1360
+ end
1361
+ end