ibm_db 4.0.0-x86-mingw32 → 5.0.2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (570) hide show
  1. checksums.yaml +5 -5
  2. data/MANIFEST +14 -14
  3. data/README +208 -208
  4. data/ext/Makefile +269 -0
  5. data/ext/Makefile.nt32 +181 -181
  6. data/ext/Makefile.nt32.191 +212 -212
  7. data/ext/extconf.rb +322 -291
  8. data/ext/gil_release_version +3 -0
  9. data/ext/ibm_db.c +11879 -11887
  10. data/ext/mkmf.log +110 -0
  11. data/ext/ruby_ibm_db.h +241 -241
  12. data/ext/ruby_ibm_db_cli.c +866 -866
  13. data/ext/ruby_ibm_db_cli.h +500 -500
  14. data/ext/unicode_support_version +3 -0
  15. data/init.rb +41 -41
  16. data/lib/IBM_DB.rb +27 -27
  17. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +3533 -3452
  18. data/lib/active_record/connection_adapters/ibmdb_adapter.rb +5 -5
  19. data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -328
  20. data/lib/mswin32/ibm_db.rb +90 -90
  21. data/lib/mswin32/rb2x/i386/ibm_db.so +0 -0
  22. data/test/active_record/connection_adapters/fake_adapter.rb +49 -49
  23. data/test/assets/example.log +1 -1
  24. data/test/assets/test.txt +1 -1
  25. data/test/cases/adapter_test.rb +351 -351
  26. data/test/cases/adapters/mysql2/active_schema_test.rb +193 -193
  27. data/test/cases/adapters/mysql2/bind_parameter_test.rb +50 -50
  28. data/test/cases/adapters/mysql2/boolean_test.rb +100 -100
  29. data/test/cases/adapters/mysql2/case_sensitivity_test.rb +63 -63
  30. data/test/cases/adapters/mysql2/charset_collation_test.rb +54 -54
  31. data/test/cases/adapters/mysql2/connection_test.rb +210 -210
  32. data/test/cases/adapters/mysql2/datetime_precision_quoting_test.rb +45 -45
  33. data/test/cases/adapters/mysql2/enum_test.rb +26 -26
  34. data/test/cases/adapters/mysql2/explain_test.rb +21 -21
  35. data/test/cases/adapters/mysql2/json_test.rb +195 -195
  36. data/test/cases/adapters/mysql2/mysql2_adapter_test.rb +83 -83
  37. data/test/cases/adapters/mysql2/reserved_word_test.rb +152 -152
  38. data/test/cases/adapters/mysql2/schema_migrations_test.rb +59 -59
  39. data/test/cases/adapters/mysql2/schema_test.rb +126 -126
  40. data/test/cases/adapters/mysql2/sp_test.rb +36 -36
  41. data/test/cases/adapters/mysql2/sql_types_test.rb +14 -14
  42. data/test/cases/adapters/mysql2/table_options_test.rb +42 -42
  43. data/test/cases/adapters/mysql2/unsigned_type_test.rb +66 -66
  44. data/test/cases/adapters/postgresql/active_schema_test.rb +98 -98
  45. data/test/cases/adapters/postgresql/array_test.rb +339 -339
  46. data/test/cases/adapters/postgresql/bit_string_test.rb +82 -82
  47. data/test/cases/adapters/postgresql/bytea_test.rb +134 -134
  48. data/test/cases/adapters/postgresql/case_insensitive_test.rb +26 -26
  49. data/test/cases/adapters/postgresql/change_schema_test.rb +38 -38
  50. data/test/cases/adapters/postgresql/cidr_test.rb +25 -25
  51. data/test/cases/adapters/postgresql/citext_test.rb +78 -78
  52. data/test/cases/adapters/postgresql/collation_test.rb +53 -53
  53. data/test/cases/adapters/postgresql/composite_test.rb +132 -132
  54. data/test/cases/adapters/postgresql/connection_test.rb +257 -257
  55. data/test/cases/adapters/postgresql/datatype_test.rb +92 -92
  56. data/test/cases/adapters/postgresql/domain_test.rb +47 -47
  57. data/test/cases/adapters/postgresql/enum_test.rb +91 -91
  58. data/test/cases/adapters/postgresql/explain_test.rb +20 -20
  59. data/test/cases/adapters/postgresql/extension_migration_test.rb +63 -63
  60. data/test/cases/adapters/postgresql/full_text_test.rb +44 -44
  61. data/test/cases/adapters/postgresql/geometric_test.rb +378 -378
  62. data/test/cases/adapters/postgresql/hstore_test.rb +382 -382
  63. data/test/cases/adapters/postgresql/infinity_test.rb +69 -69
  64. data/test/cases/adapters/postgresql/integer_test.rb +25 -25
  65. data/test/cases/adapters/postgresql/json_test.rb +237 -237
  66. data/test/cases/adapters/postgresql/ltree_test.rb +53 -53
  67. data/test/cases/adapters/postgresql/money_test.rb +96 -96
  68. data/test/cases/adapters/postgresql/network_test.rb +94 -94
  69. data/test/cases/adapters/postgresql/numbers_test.rb +49 -49
  70. data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +405 -405
  71. data/test/cases/adapters/postgresql/prepared_statements_test.rb +22 -22
  72. data/test/cases/adapters/postgresql/quoting_test.rb +44 -44
  73. data/test/cases/adapters/postgresql/range_test.rb +343 -343
  74. data/test/cases/adapters/postgresql/referential_integrity_test.rb +111 -111
  75. data/test/cases/adapters/postgresql/rename_table_test.rb +34 -34
  76. data/test/cases/adapters/postgresql/schema_authorization_test.rb +119 -119
  77. data/test/cases/adapters/postgresql/schema_test.rb +597 -597
  78. data/test/cases/adapters/postgresql/serial_test.rb +154 -154
  79. data/test/cases/adapters/postgresql/statement_pool_test.rb +41 -41
  80. data/test/cases/adapters/postgresql/timestamp_test.rb +90 -90
  81. data/test/cases/adapters/postgresql/type_lookup_test.rb +33 -33
  82. data/test/cases/adapters/postgresql/utils_test.rb +62 -62
  83. data/test/cases/adapters/postgresql/uuid_test.rb +294 -294
  84. data/test/cases/adapters/postgresql/xml_test.rb +54 -54
  85. data/test/cases/adapters/sqlite3/collation_test.rb +53 -53
  86. data/test/cases/adapters/sqlite3/copy_table_test.rb +98 -98
  87. data/test/cases/adapters/sqlite3/explain_test.rb +21 -21
  88. data/test/cases/adapters/sqlite3/quoting_test.rb +101 -101
  89. data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +441 -441
  90. data/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb +24 -24
  91. data/test/cases/adapters/sqlite3/statement_pool_test.rb +20 -20
  92. data/test/cases/aggregations_test.rb +168 -168
  93. data/test/cases/ar_schema_test.rb +146 -146
  94. data/test/cases/associations/association_scope_test.rb +16 -16
  95. data/test/cases/associations/belongs_to_associations_test.rb +1141 -1141
  96. data/test/cases/associations/bidirectional_destroy_dependencies_test.rb +41 -41
  97. data/test/cases/associations/callbacks_test.rb +190 -190
  98. data/test/cases/associations/cascaded_eager_loading_test.rb +188 -188
  99. data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +36 -36
  100. data/test/cases/associations/eager_load_nested_include_test.rb +126 -126
  101. data/test/cases/associations/eager_singularization_test.rb +148 -148
  102. data/test/cases/associations/eager_test.rb +1514 -1514
  103. data/test/cases/associations/extension_test.rb +87 -87
  104. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +1004 -1004
  105. data/test/cases/associations/has_many_associations_test.rb +2501 -2501
  106. data/test/cases/associations/has_many_through_associations_test.rb +1271 -1271
  107. data/test/cases/associations/has_one_associations_test.rb +707 -707
  108. data/test/cases/associations/has_one_through_associations_test.rb +383 -383
  109. data/test/cases/associations/inner_join_association_test.rb +139 -139
  110. data/test/cases/associations/inverse_associations_test.rb +733 -733
  111. data/test/cases/associations/join_model_test.rb +777 -777
  112. data/test/cases/associations/left_outer_join_association_test.rb +88 -88
  113. data/test/cases/associations/nested_through_associations_test.rb +579 -579
  114. data/test/cases/associations/required_test.rb +102 -102
  115. data/test/cases/associations_test.rb +385 -385
  116. data/test/cases/attribute_decorators_test.rb +126 -125
  117. data/test/cases/attribute_methods/read_test.rb +60 -60
  118. data/test/cases/attribute_methods_test.rb +1009 -1009
  119. data/test/cases/attribute_set_test.rb +270 -270
  120. data/test/cases/attribute_test.rb +246 -246
  121. data/test/cases/attributes_test.rb +253 -253
  122. data/test/cases/autosave_association_test.rb +1708 -1708
  123. data/test/cases/base_test.rb +1713 -1713
  124. data/test/cases/batches_test.rb +489 -489
  125. data/test/cases/binary_test.rb +44 -44
  126. data/test/cases/bind_parameter_test.rb +110 -110
  127. data/test/cases/cache_key_test.rb +26 -25
  128. data/test/cases/calculations_test.rb +798 -798
  129. data/test/cases/callbacks_test.rb +636 -636
  130. data/test/cases/clone_test.rb +40 -40
  131. data/test/cases/coders/json_test.rb +15 -15
  132. data/test/cases/coders/yaml_column_test.rb +63 -63
  133. data/test/cases/collection_cache_key_test.rb +115 -115
  134. data/test/cases/column_alias_test.rb +17 -17
  135. data/test/cases/column_definition_test.rb +92 -92
  136. data/test/cases/comment_test.rb +145 -143
  137. data/test/cases/connection_adapters/adapter_leasing_test.rb +56 -56
  138. data/test/cases/connection_adapters/connection_handler_test.rb +160 -160
  139. data/test/cases/connection_adapters/connection_specification_test.rb +12 -12
  140. data/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb +255 -255
  141. data/test/cases/connection_adapters/mysql_type_lookup_test.rb +69 -69
  142. data/test/cases/connection_adapters/quoting_test.rb +13 -13
  143. data/test/cases/connection_adapters/schema_cache_test.rb +61 -61
  144. data/test/cases/connection_adapters/type_lookup_test.rb +118 -118
  145. data/test/cases/connection_management_test.rb +112 -112
  146. data/test/cases/connection_pool_test.rb +521 -521
  147. data/test/cases/connection_specification/resolver_test.rb +131 -131
  148. data/test/cases/core_test.rb +112 -112
  149. data/test/cases/counter_cache_test.rb +214 -214
  150. data/test/cases/custom_locking_test.rb +17 -17
  151. data/test/cases/database_statements_test.rb +34 -34
  152. data/test/cases/date_test.rb +44 -44
  153. data/test/cases/date_time_precision_test.rb +107 -106
  154. data/test/cases/date_time_test.rb +61 -61
  155. data/test/cases/defaults_test.rb +219 -218
  156. data/test/cases/dirty_test.rb +763 -763
  157. data/test/cases/disconnected_test.rb +30 -30
  158. data/test/cases/dup_test.rb +157 -157
  159. data/test/cases/enum_test.rb +444 -444
  160. data/test/cases/errors_test.rb +16 -16
  161. data/test/cases/explain_subscriber_test.rb +64 -64
  162. data/test/cases/explain_test.rb +87 -87
  163. data/test/cases/finder_respond_to_test.rb +60 -60
  164. data/test/cases/finder_test.rb +1294 -1294
  165. data/test/cases/fixture_set/file_test.rb +156 -156
  166. data/test/cases/fixtures_test.rb +988 -988
  167. data/test/cases/forbidden_attributes_protection_test.rb +165 -165
  168. data/test/cases/habtm_destroy_order_test.rb +61 -61
  169. data/test/cases/helper.rb +204 -204
  170. data/test/cases/hot_compatibility_test.rb +142 -142
  171. data/test/cases/i18n_test.rb +45 -45
  172. data/test/cases/inheritance_test.rb +606 -606
  173. data/test/cases/integration_test.rb +155 -155
  174. data/test/cases/invalid_connection_test.rb +24 -24
  175. data/test/cases/invertible_migration_test.rb +387 -387
  176. data/test/cases/json_serialization_test.rb +311 -311
  177. data/test/cases/locking_test.rb +493 -493
  178. data/test/cases/log_subscriber_test.rb +225 -225
  179. data/test/cases/migration/change_schema_test.rb +458 -458
  180. data/test/cases/migration/change_table_test.rb +256 -256
  181. data/test/cases/migration/column_attributes_test.rb +176 -176
  182. data/test/cases/migration/column_positioning_test.rb +56 -56
  183. data/test/cases/migration/columns_test.rb +310 -310
  184. data/test/cases/migration/command_recorder_test.rb +350 -350
  185. data/test/cases/migration/compatibility_test.rb +118 -118
  186. data/test/cases/migration/create_join_table_test.rb +157 -157
  187. data/test/cases/migration/foreign_key_test.rb +362 -360
  188. data/test/cases/migration/helper.rb +39 -39
  189. data/test/cases/migration/index_test.rb +218 -218
  190. data/test/cases/migration/logger_test.rb +36 -36
  191. data/test/cases/migration/pending_migrations_test.rb +52 -52
  192. data/test/cases/migration/references_foreign_key_test.rb +221 -216
  193. data/test/cases/migration/references_index_test.rb +101 -101
  194. data/test/cases/migration/references_statements_test.rb +136 -136
  195. data/test/cases/migration/rename_table_test.rb +93 -93
  196. data/test/cases/migration_test.rb +1157 -1157
  197. data/test/cases/migrator_test.rb +471 -470
  198. data/test/cases/mixin_test.rb +68 -68
  199. data/test/cases/modules_test.rb +172 -172
  200. data/test/cases/multiparameter_attributes_test.rb +372 -372
  201. data/test/cases/multiple_db_test.rb +122 -122
  202. data/test/cases/nested_attributes_test.rb +1098 -1098
  203. data/test/cases/nested_attributes_with_callbacks_test.rb +144 -144
  204. data/test/cases/persistence_test.rb +1001 -1001
  205. data/test/cases/pooled_connections_test.rb +81 -81
  206. data/test/cases/primary_keys_test.rb +376 -376
  207. data/test/cases/query_cache_test.rb +446 -446
  208. data/test/cases/quoting_test.rb +202 -202
  209. data/test/cases/readonly_test.rb +119 -119
  210. data/test/cases/reaper_test.rb +85 -85
  211. data/test/cases/reflection_test.rb +509 -509
  212. data/test/cases/relation/delegation_test.rb +63 -63
  213. data/test/cases/relation/merging_test.rb +157 -157
  214. data/test/cases/relation/mutation_test.rb +183 -183
  215. data/test/cases/relation/or_test.rb +92 -92
  216. data/test/cases/relation/predicate_builder_test.rb +16 -16
  217. data/test/cases/relation/record_fetch_warning_test.rb +40 -40
  218. data/test/cases/relation/where_chain_test.rb +105 -105
  219. data/test/cases/relation/where_clause_test.rb +182 -182
  220. data/test/cases/relation/where_test.rb +322 -322
  221. data/test/cases/relation_test.rb +328 -328
  222. data/test/cases/relations_test.rb +2026 -2026
  223. data/test/cases/reload_models_test.rb +22 -22
  224. data/test/cases/result_test.rb +90 -90
  225. data/test/cases/sanitize_test.rb +176 -176
  226. data/test/cases/schema_dumper_test.rb +457 -457
  227. data/test/cases/schema_loading_test.rb +52 -52
  228. data/test/cases/scoping/default_scoping_test.rb +528 -528
  229. data/test/cases/scoping/named_scoping_test.rb +561 -561
  230. data/test/cases/scoping/relation_scoping_test.rb +400 -400
  231. data/test/cases/secure_token_test.rb +32 -32
  232. data/test/cases/serialization_test.rb +104 -104
  233. data/test/cases/serialized_attribute_test.rb +364 -364
  234. data/test/cases/statement_cache_test.rb +136 -136
  235. data/test/cases/store_test.rb +195 -195
  236. data/test/cases/suppressor_test.rb +63 -63
  237. data/test/cases/tasks/database_tasks_test.rb +462 -462
  238. data/test/cases/tasks/mysql_rake_test.rb +345 -345
  239. data/test/cases/tasks/postgresql_rake_test.rb +304 -304
  240. data/test/cases/tasks/sqlite_rake_test.rb +220 -220
  241. data/test/cases/test_case.rb +131 -131
  242. data/test/cases/test_fixtures_test.rb +36 -36
  243. data/test/cases/time_precision_test.rb +103 -102
  244. data/test/cases/timestamp_test.rb +501 -501
  245. data/test/cases/touch_later_test.rb +121 -121
  246. data/test/cases/transaction_callbacks_test.rb +518 -518
  247. data/test/cases/transaction_isolation_test.rb +106 -106
  248. data/test/cases/transactions_test.rb +835 -834
  249. data/test/cases/type/adapter_specific_registry_test.rb +133 -133
  250. data/test/cases/type/date_time_test.rb +14 -14
  251. data/test/cases/type/integer_test.rb +27 -27
  252. data/test/cases/type/string_test.rb +22 -22
  253. data/test/cases/type/type_map_test.rb +177 -177
  254. data/test/cases/type_test.rb +39 -39
  255. data/test/cases/types_test.rb +24 -24
  256. data/test/cases/unconnected_test.rb +33 -33
  257. data/test/cases/validations/absence_validation_test.rb +73 -73
  258. data/test/cases/validations/association_validation_test.rb +97 -97
  259. data/test/cases/validations/i18n_generate_message_validation_test.rb +84 -84
  260. data/test/cases/validations/i18n_validation_test.rb +86 -86
  261. data/test/cases/validations/length_validation_test.rb +79 -79
  262. data/test/cases/validations/presence_validation_test.rb +103 -103
  263. data/test/cases/validations/uniqueness_validation_test.rb +548 -548
  264. data/test/cases/validations_repair_helper.rb +19 -19
  265. data/test/cases/validations_test.rb +194 -194
  266. data/test/cases/view_test.rb +216 -216
  267. data/test/cases/yaml_serialization_test.rb +121 -121
  268. data/test/config.example.yml +97 -97
  269. data/test/config.rb +5 -5
  270. data/test/connections/native_ibm_db/connection.rb +44 -0
  271. data/test/fixtures/accounts.yml +29 -29
  272. data/test/fixtures/admin/accounts.yml +2 -2
  273. data/test/fixtures/admin/users.yml +10 -10
  274. data/test/fixtures/author_addresses.yml +17 -17
  275. data/test/fixtures/author_favorites.yml +3 -3
  276. data/test/fixtures/authors.yml +23 -23
  277. data/test/fixtures/bad_posts.yml +9 -9
  278. data/test/fixtures/binaries.yml +133 -133
  279. data/test/fixtures/books.yml +31 -31
  280. data/test/fixtures/bulbs.yml +5 -5
  281. data/test/fixtures/cars.yml +9 -9
  282. data/test/fixtures/categories.yml +19 -19
  283. data/test/fixtures/categories/special_categories.yml +9 -9
  284. data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -4
  285. data/test/fixtures/categories_ordered.yml +7 -7
  286. data/test/fixtures/categories_posts.yml +31 -31
  287. data/test/fixtures/categorizations.yml +23 -23
  288. data/test/fixtures/clubs.yml +8 -8
  289. data/test/fixtures/collections.yml +3 -3
  290. data/test/fixtures/colleges.yml +3 -3
  291. data/test/fixtures/comments.yml +65 -65
  292. data/test/fixtures/companies.yml +67 -67
  293. data/test/fixtures/computers.yml +10 -10
  294. data/test/fixtures/content.yml +3 -3
  295. data/test/fixtures/content_positions.yml +3 -3
  296. data/test/fixtures/courses.yml +8 -8
  297. data/test/fixtures/customers.yml +25 -25
  298. data/test/fixtures/dashboards.yml +6 -6
  299. data/test/fixtures/dead_parrots.yml +5 -5
  300. data/test/fixtures/developers.yml +22 -22
  301. data/test/fixtures/developers_projects.yml +16 -16
  302. data/test/fixtures/dog_lovers.yml +7 -7
  303. data/test/fixtures/dogs.yml +4 -4
  304. data/test/fixtures/doubloons.yml +3 -3
  305. data/test/fixtures/edges.yml +5 -5
  306. data/test/fixtures/entrants.yml +14 -14
  307. data/test/fixtures/essays.yml +6 -6
  308. data/test/fixtures/faces.yml +11 -11
  309. data/test/fixtures/fk_test_has_fk.yml +3 -3
  310. data/test/fixtures/fk_test_has_pk.yml +1 -1
  311. data/test/fixtures/friendships.yml +4 -4
  312. data/test/fixtures/funny_jokes.yml +10 -10
  313. data/test/fixtures/interests.yml +33 -33
  314. data/test/fixtures/items.yml +3 -3
  315. data/test/fixtures/jobs.yml +7 -7
  316. data/test/fixtures/legacy_things.yml +3 -3
  317. data/test/fixtures/live_parrots.yml +4 -4
  318. data/test/fixtures/mateys.yml +4 -4
  319. data/test/fixtures/member_details.yml +8 -8
  320. data/test/fixtures/member_types.yml +6 -6
  321. data/test/fixtures/members.yml +11 -11
  322. data/test/fixtures/memberships.yml +34 -34
  323. data/test/fixtures/men.yml +5 -5
  324. data/test/fixtures/minimalistics.yml +2 -2
  325. data/test/fixtures/minivans.yml +5 -5
  326. data/test/fixtures/mixed_case_monkeys.yml +6 -6
  327. data/test/fixtures/mixins.yml +29 -29
  328. data/test/fixtures/movies.yml +7 -7
  329. data/test/fixtures/naked/yml/accounts.yml +1 -1
  330. data/test/fixtures/naked/yml/companies.yml +1 -1
  331. data/test/fixtures/naked/yml/courses.yml +1 -1
  332. data/test/fixtures/naked/yml/parrots.yml +2 -2
  333. data/test/fixtures/naked/yml/trees.yml +3 -3
  334. data/test/fixtures/nodes.yml +29 -29
  335. data/test/fixtures/organizations.yml +5 -5
  336. data/test/fixtures/other_comments.yml +6 -6
  337. data/test/fixtures/other_dogs.yml +2 -2
  338. data/test/fixtures/other_posts.yml +7 -7
  339. data/test/fixtures/other_topics.yml +42 -42
  340. data/test/fixtures/owners.yml +9 -9
  341. data/test/fixtures/parrots.yml +27 -27
  342. data/test/fixtures/parrots_pirates.yml +7 -7
  343. data/test/fixtures/people.yml +24 -24
  344. data/test/fixtures/peoples_treasures.yml +3 -3
  345. data/test/fixtures/pets.yml +19 -19
  346. data/test/fixtures/pirates.yml +12 -15
  347. data/test/fixtures/posts.yml +80 -80
  348. data/test/fixtures/price_estimates.yml +16 -16
  349. data/test/fixtures/products.yml +4 -4
  350. data/test/fixtures/projects.yml +7 -7
  351. data/test/fixtures/ratings.yml +14 -14
  352. data/test/fixtures/readers.yml +11 -11
  353. data/test/fixtures/references.yml +17 -17
  354. data/test/fixtures/reserved_words/distinct.yml +5 -5
  355. data/test/fixtures/reserved_words/distinct_select.yml +11 -11
  356. data/test/fixtures/reserved_words/group.yml +14 -14
  357. data/test/fixtures/reserved_words/select.yml +8 -8
  358. data/test/fixtures/reserved_words/values.yml +7 -7
  359. data/test/fixtures/ships.yml +6 -6
  360. data/test/fixtures/speedometers.yml +8 -8
  361. data/test/fixtures/sponsors.yml +12 -12
  362. data/test/fixtures/string_key_objects.yml +7 -7
  363. data/test/fixtures/subscribers.yml +10 -10
  364. data/test/fixtures/subscriptions.yml +12 -12
  365. data/test/fixtures/taggings.yml +78 -78
  366. data/test/fixtures/tags.yml +11 -11
  367. data/test/fixtures/tasks.yml +7 -7
  368. data/test/fixtures/teapots.yml +3 -3
  369. data/test/fixtures/to_be_linked/accounts.yml +2 -2
  370. data/test/fixtures/to_be_linked/users.yml +10 -10
  371. data/test/fixtures/topics.yml +49 -49
  372. data/test/fixtures/toys.yml +14 -14
  373. data/test/fixtures/traffic_lights.yml +9 -9
  374. data/test/fixtures/treasures.yml +10 -10
  375. data/test/fixtures/trees.yml +3 -3
  376. data/test/fixtures/uuid_children.yml +3 -3
  377. data/test/fixtures/uuid_parents.yml +2 -2
  378. data/test/fixtures/variants.yml +4 -4
  379. data/test/fixtures/vegetables.yml +19 -19
  380. data/test/fixtures/vertices.yml +3 -3
  381. data/test/fixtures/warehouse_things.yml +2 -2
  382. data/test/fixtures/zines.yml +5 -5
  383. data/test/migrations/10_urban/9_add_expressions.rb +11 -11
  384. data/test/migrations/decimal/1_give_me_big_numbers.rb +15 -15
  385. data/test/migrations/magic/1_currencies_have_symbols.rb +12 -12
  386. data/test/migrations/missing/1000_people_have_middle_names.rb +9 -9
  387. data/test/migrations/missing/1_people_have_last_names.rb +9 -9
  388. data/test/migrations/missing/3_we_need_reminders.rb +12 -12
  389. data/test/migrations/missing/4_innocent_jointable.rb +12 -12
  390. data/test/migrations/rename/1_we_need_things.rb +11 -11
  391. data/test/migrations/rename/2_rename_things.rb +9 -9
  392. data/test/migrations/to_copy/1_people_have_hobbies.rb +9 -9
  393. data/test/migrations/to_copy/2_people_have_descriptions.rb +9 -9
  394. data/test/migrations/to_copy2/1_create_articles.rb +7 -7
  395. data/test/migrations/to_copy2/2_create_comments.rb +7 -7
  396. data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +9 -9
  397. data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +9 -9
  398. data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +9 -9
  399. data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +7 -7
  400. data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +7 -7
  401. data/test/migrations/valid/1_valid_people_have_last_names.rb +9 -9
  402. data/test/migrations/valid/2_we_need_reminders.rb +12 -12
  403. data/test/migrations/valid/3_innocent_jointable.rb +12 -12
  404. data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +9 -9
  405. data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +12 -12
  406. data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +12 -12
  407. data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +9 -9
  408. data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +12 -12
  409. data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +12 -12
  410. data/test/migrations/version_check/20131219224947_migration_version_check.rb +8 -8
  411. data/test/models/admin.rb +5 -5
  412. data/test/models/admin/account.rb +3 -3
  413. data/test/models/admin/user.rb +40 -40
  414. data/test/models/aircraft.rb +5 -5
  415. data/test/models/arunit2_model.rb +3 -3
  416. data/test/models/author.rb +209 -209
  417. data/test/models/auto_id.rb +4 -4
  418. data/test/models/autoloadable/extra_firm.rb +2 -2
  419. data/test/models/binary.rb +2 -2
  420. data/test/models/bird.rb +12 -12
  421. data/test/models/book.rb +23 -23
  422. data/test/models/boolean.rb +2 -2
  423. data/test/models/bulb.rb +52 -52
  424. data/test/models/cake_designer.rb +3 -3
  425. data/test/models/car.rb +29 -29
  426. data/test/models/carrier.rb +2 -2
  427. data/test/models/cat.rb +10 -10
  428. data/test/models/categorization.rb +19 -19
  429. data/test/models/category.rb +35 -35
  430. data/test/models/chef.rb +8 -8
  431. data/test/models/citation.rb +3 -3
  432. data/test/models/club.rb +25 -25
  433. data/test/models/college.rb +10 -10
  434. data/test/models/column.rb +3 -3
  435. data/test/models/column_name.rb +3 -3
  436. data/test/models/comment.rb +76 -76
  437. data/test/models/company.rb +230 -230
  438. data/test/models/company_in_module.rb +98 -98
  439. data/test/models/computer.rb +3 -3
  440. data/test/models/contact.rb +41 -41
  441. data/test/models/content.rb +40 -40
  442. data/test/models/contract.rb +20 -20
  443. data/test/models/country.rb +7 -7
  444. data/test/models/course.rb +6 -6
  445. data/test/models/customer.rb +83 -83
  446. data/test/models/customer_carrier.rb +14 -14
  447. data/test/models/dashboard.rb +3 -3
  448. data/test/models/default.rb +2 -2
  449. data/test/models/department.rb +4 -4
  450. data/test/models/developer.rb +274 -274
  451. data/test/models/dog.rb +5 -5
  452. data/test/models/dog_lover.rb +5 -5
  453. data/test/models/doubloon.rb +12 -12
  454. data/test/models/drink_designer.rb +3 -3
  455. data/test/models/edge.rb +5 -5
  456. data/test/models/electron.rb +5 -5
  457. data/test/models/engine.rb +4 -4
  458. data/test/models/entrant.rb +3 -3
  459. data/test/models/essay.rb +5 -5
  460. data/test/models/event.rb +3 -3
  461. data/test/models/eye.rb +37 -37
  462. data/test/models/face.rb +9 -9
  463. data/test/models/friendship.rb +6 -6
  464. data/test/models/guid.rb +2 -2
  465. data/test/models/guitar.rb +4 -4
  466. data/test/models/hotel.rb +11 -11
  467. data/test/models/image.rb +3 -3
  468. data/test/models/interest.rb +5 -5
  469. data/test/models/invoice.rb +4 -4
  470. data/test/models/item.rb +7 -7
  471. data/test/models/job.rb +7 -7
  472. data/test/models/joke.rb +7 -7
  473. data/test/models/keyboard.rb +3 -3
  474. data/test/models/legacy_thing.rb +3 -3
  475. data/test/models/lesson.rb +11 -11
  476. data/test/models/line_item.rb +3 -3
  477. data/test/models/liquid.rb +4 -4
  478. data/test/models/man.rb +11 -11
  479. data/test/models/matey.rb +4 -4
  480. data/test/models/member.rb +42 -42
  481. data/test/models/member_detail.rb +8 -8
  482. data/test/models/member_type.rb +3 -3
  483. data/test/models/membership.rb +35 -35
  484. data/test/models/mentor.rb +2 -2
  485. data/test/models/minimalistic.rb +2 -2
  486. data/test/models/minivan.rb +9 -9
  487. data/test/models/mixed_case_monkey.rb +3 -3
  488. data/test/models/mocktail_designer.rb +2 -2
  489. data/test/models/molecule.rb +6 -6
  490. data/test/models/movie.rb +5 -5
  491. data/test/models/node.rb +5 -5
  492. data/test/models/non_primary_key.rb +2 -2
  493. data/test/models/notification.rb +3 -3
  494. data/test/models/order.rb +4 -4
  495. data/test/models/organization.rb +14 -14
  496. data/test/models/other_dog.rb +5 -5
  497. data/test/models/owner.rb +37 -37
  498. data/test/models/parrot.rb +28 -28
  499. data/test/models/person.rb +142 -142
  500. data/test/models/personal_legacy_thing.rb +4 -4
  501. data/test/models/pet.rb +18 -18
  502. data/test/models/pet_treasure.rb +6 -6
  503. data/test/models/pirate.rb +92 -92
  504. data/test/models/possession.rb +3 -3
  505. data/test/models/post.rb +273 -273
  506. data/test/models/price_estimate.rb +4 -4
  507. data/test/models/professor.rb +5 -5
  508. data/test/models/project.rb +40 -40
  509. data/test/models/publisher.rb +2 -2
  510. data/test/models/publisher/article.rb +4 -4
  511. data/test/models/publisher/magazine.rb +3 -3
  512. data/test/models/rating.rb +4 -4
  513. data/test/models/reader.rb +23 -23
  514. data/test/models/recipe.rb +3 -3
  515. data/test/models/record.rb +2 -2
  516. data/test/models/reference.rb +22 -22
  517. data/test/models/reply.rb +61 -61
  518. data/test/models/ship.rb +39 -39
  519. data/test/models/ship_part.rb +8 -8
  520. data/test/models/shop.rb +17 -17
  521. data/test/models/shop_account.rb +6 -6
  522. data/test/models/speedometer.rb +6 -6
  523. data/test/models/sponsor.rb +7 -7
  524. data/test/models/string_key_object.rb +3 -3
  525. data/test/models/student.rb +4 -4
  526. data/test/models/subject.rb +16 -16
  527. data/test/models/subscriber.rb +8 -8
  528. data/test/models/subscription.rb +4 -4
  529. data/test/models/tag.rb +13 -13
  530. data/test/models/tagging.rb +13 -13
  531. data/test/models/task.rb +5 -5
  532. data/test/models/topic.rb +118 -118
  533. data/test/models/toy.rb +6 -6
  534. data/test/models/traffic_light.rb +4 -4
  535. data/test/models/treasure.rb +14 -14
  536. data/test/models/treaty.rb +7 -7
  537. data/test/models/tree.rb +3 -3
  538. data/test/models/tuning_peg.rb +4 -4
  539. data/test/models/tyre.rb +11 -11
  540. data/test/models/user.rb +14 -14
  541. data/test/models/uuid_child.rb +3 -3
  542. data/test/models/uuid_item.rb +6 -6
  543. data/test/models/uuid_parent.rb +3 -3
  544. data/test/models/vegetables.rb +24 -24
  545. data/test/models/vehicle.rb +6 -6
  546. data/test/models/vertex.rb +9 -9
  547. data/test/models/warehouse_thing.rb +5 -5
  548. data/test/models/wheel.rb +3 -3
  549. data/test/models/without_table.rb +3 -3
  550. data/test/models/zine.rb +3 -3
  551. data/test/schema/i5/ibm_db_specific_schema.rb +137 -0
  552. data/test/schema/ids/ibm_db_specific_schema.rb +140 -0
  553. data/test/schema/luw/ibm_db_specific_schema.rb +137 -0
  554. data/test/schema/mysql2_specific_schema.rb +68 -68
  555. data/test/schema/oracle_specific_schema.rb +40 -40
  556. data/test/schema/postgresql_specific_schema.rb +114 -114
  557. data/test/schema/schema.rb +1057 -1057
  558. data/test/schema/schema.rb.original +1057 -1057
  559. data/test/schema/sqlite_specific_schema.rb +18 -18
  560. data/test/schema/zOS/ibm_db_specific_schema.rb +208 -0
  561. data/test/support/config.rb +43 -43
  562. data/test/support/connection.rb +23 -23
  563. data/test/support/connection_helper.rb +14 -14
  564. data/test/support/ddl_helper.rb +8 -8
  565. data/test/support/schema_dumping_helper.rb +20 -20
  566. data/test/support/yaml_compatibility_fixtures/rails_4_1.yml +22 -22
  567. data/test/support/yaml_compatibility_fixtures/rails_4_2_0.yml +182 -182
  568. metadata +24 -13
  569. data/test/fixtures/author_addresses.original +0 -11
  570. data/test/fixtures/authors.original +0 -17
@@ -1,1294 +1,1294 @@
1
- require "cases/helper"
2
- require 'models/post'
3
- require 'models/author'
4
- require 'models/categorization'
5
- require 'models/comment'
6
- require 'models/company'
7
- require 'models/tagging'
8
- require 'models/topic'
9
- require 'models/reply'
10
- require 'models/entrant'
11
- require 'models/project'
12
- require 'models/developer'
13
- require 'models/computer'
14
- require 'models/customer'
15
- require 'models/toy'
16
- require 'models/matey'
17
- require 'models/dog'
18
- require 'models/car'
19
- require 'models/tyre'
20
-
21
- class FinderTest < ActiveRecord::TestCase
22
- fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :author_addresses, :customers, :categories, :categorizations, :cars
23
-
24
- def test_find_by_id_with_hash
25
- assert_raises(ActiveRecord::StatementInvalid) do
26
- Post.find_by_id(:limit => 1)
27
- end
28
- end
29
-
30
- def test_find_by_title_and_id_with_hash
31
- assert_raises(ActiveRecord::StatementInvalid) do
32
- Post.find_by_title_and_id('foo', :limit => 1)
33
- end
34
- end
35
-
36
- def test_find
37
- assert_equal(topics(:first).title, Topic.find(1).title)
38
- end
39
-
40
- def test_find_with_proc_parameter_and_block
41
- exception = assert_raises(RuntimeError) do
42
- Topic.all.find(-> { raise "should happen" }) { |e| e.title == "non-existing-title" }
43
- end
44
- assert_equal "should happen", exception.message
45
-
46
- assert_nothing_raised do
47
- Topic.all.find(-> { raise "should not happen" }) { |e| e.title == topics(:first).title }
48
- end
49
- end
50
-
51
- def test_find_with_ids_returning_ordered
52
- puts "finder_test.test_find_with_ids_returning_ordered"
53
- records = Topic.find([4,2,5])
54
- assert_equal 'The Fourth Topic of the day', records[0].title
55
- assert_equal 'The Second Topic of the day', records[1].title
56
- assert_equal 'The Fifth Topic of the day', records[2].title
57
-
58
- records = Topic.find(4,2,5)
59
- assert_equal 'The Fourth Topic of the day', records[0].title
60
- assert_equal 'The Second Topic of the day', records[1].title
61
- assert_equal 'The Fifth Topic of the day', records[2].title
62
-
63
- records = Topic.find(['4','2','5'])
64
- assert_equal 'The Fourth Topic of the day', records[0].title
65
- assert_equal 'The Second Topic of the day', records[1].title
66
- assert_equal 'The Fifth Topic of the day', records[2].title
67
-
68
- records = Topic.find('4','2','5')
69
- assert_equal 'The Fourth Topic of the day', records[0].title
70
- assert_equal 'The Second Topic of the day', records[1].title
71
- assert_equal 'The Fifth Topic of the day', records[2].title
72
- end
73
-
74
- def test_find_with_ids_and_order_clause
75
- # The order clause takes precedence over the informed ids
76
- records = Topic.order(:author_name).find([5,3,1])
77
- assert_equal 'The Third Topic of the day', records[0].title
78
- assert_equal 'The First Topic', records[1].title
79
- assert_equal 'The Fifth Topic of the day', records[2].title
80
-
81
- records = Topic.order(:id).find([5,3,1])
82
- assert_equal 'The First Topic', records[0].title
83
- assert_equal 'The Third Topic of the day', records[1].title
84
- assert_equal 'The Fifth Topic of the day', records[2].title
85
- end
86
-
87
- def test_find_with_ids_with_limit_and_order_clause
88
- puts "finder_test.test_find_with_ids_with_limit_and_order_clause"
89
- # The order clause takes precedence over the informed ids
90
- records = Topic.limit(2).order(:id).find([5,3,1])
91
- assert_equal 2, records.size
92
- assert_equal 'The First Topic', records[0].title
93
- assert_equal 'The Third Topic of the day', records[1].title
94
- end
95
-
96
- def test_find_with_ids_and_limit
97
- records = Topic.limit(3).find([3,2,5,1,4])
98
- assert_equal 3, records.size
99
- assert_equal 'The Third Topic of the day', records[0].title
100
- assert_equal 'The Second Topic of the day', records[1].title
101
- assert_equal 'The Fifth Topic of the day', records[2].title
102
- end
103
-
104
- def test_find_with_ids_where_and_limit
105
- # Please note that Topic 1 is the only not approved so
106
- # if it were among the first 3 it would raise an ActiveRecord::RecordNotFound
107
- records = Topic.where(approved: true).limit(3).find([3,2,5,1,4])
108
- assert_equal 3, records.size
109
- assert_equal 'The Third Topic of the day', records[0].title
110
- assert_equal 'The Second Topic of the day', records[1].title
111
- assert_equal 'The Fifth Topic of the day', records[2].title
112
- end
113
-
114
- def test_find_with_ids_and_offset
115
- records = Topic.offset(2).find([3,2,5,1,4])
116
- assert_equal 3, records.size
117
- assert_equal 'The Fifth Topic of the day', records[0].title
118
- assert_equal 'The First Topic', records[1].title
119
- assert_equal 'The Fourth Topic of the day', records[2].title
120
- end
121
-
122
- def test_find_passing_active_record_object_is_deprecated
123
- assert_deprecated do
124
- Topic.find(Topic.last)
125
- end
126
- end
127
-
128
- def test_symbols_table_ref
129
- gc_disabled = GC.disable
130
- Post.where("author_id" => nil) # warm up
131
- x = Symbol.all_symbols.count
132
- Post.where("title" => {"xxxqqqq" => "bar"})
133
- assert_equal x, Symbol.all_symbols.count
134
- ensure
135
- GC.enable if gc_disabled == false
136
- end
137
-
138
- # find should handle strings that come from URLs
139
- # (example: Category.find(params[:id]))
140
- def test_find_with_string
141
- assert_equal(Topic.find(1).title,Topic.find("1").title)
142
- end
143
-
144
- def test_exists
145
- assert_equal true, Topic.exists?(1)
146
- assert_equal true, Topic.exists?("1")
147
- assert_equal true, Topic.exists?(title: "The First Topic")
148
- assert_equal true, Topic.exists?(heading: "The First Topic")
149
- assert_equal true, Topic.exists?(:author_name => "Mary", :approved => true)
150
- assert_equal true, Topic.exists?(["parent_id = ?", 1])
151
- assert_equal true, Topic.exists?(id: [1, 9999])
152
-
153
- assert_equal false, Topic.exists?(45)
154
- assert_equal false, Topic.exists?(Topic.new.id)
155
-
156
- assert_raise(NoMethodError) { Topic.exists?([1,2]) }
157
- end
158
-
159
- def test_exists_with_polymorphic_relation
160
- puts "finder_test.test_exists_with_polymorphic_relation"
161
- post = Post.create!(title: 'Post', body: 'default', taggings: [Tagging.new(comment: 'tagging comment')])
162
- relation = Post.tagged_with_comment('tagging comment')
163
-
164
- assert_equal true, relation.exists?(title: ['Post'])
165
- assert_equal true, relation.exists?(['title LIKE ?', 'Post%'])
166
- assert_equal true, relation.exists?
167
- assert_equal true, relation.exists?(post.id)
168
- assert_equal true, relation.exists?(post.id.to_s)
169
-
170
- assert_equal false, relation.exists?(false)
171
- end
172
-
173
- def test_exists_passing_active_record_object_is_deprecated
174
- assert_deprecated do
175
- Topic.exists?(Topic.new)
176
- end
177
- end
178
-
179
- def test_exists_fails_when_parameter_has_invalid_type
180
- puts "finder_test.test_exists_fails_when_parameter_has_invalid_type"
181
- assert_raises(ActiveModel::RangeError) do
182
- assert_equal false, Topic.exists?(("9"*53).to_i) # number that's bigger than int
183
- end
184
- assert_equal false, Topic.exists?("foo")
185
- end
186
-
187
- def test_exists_does_not_select_columns_without_alias
188
- puts "finder_test.test_exists_does_not_select_columns_without_alias"
189
- assert_sql(/SELECT\W+1 AS one FROM ["`]topics["`]/i) do
190
- Topic.exists?
191
- end
192
- end
193
-
194
- def test_exists_returns_true_with_one_record_and_no_args
195
- assert_equal true, Topic.exists?
196
- end
197
-
198
- def test_exists_returns_false_with_false_arg
199
- assert_equal false, Topic.exists?(false)
200
- end
201
-
202
- # exists? should handle nil for id's that come from URLs and always return false
203
- # (example: Topic.exists?(params[:id])) where params[:id] is nil
204
- def test_exists_with_nil_arg
205
- assert_equal false, Topic.exists?(nil)
206
- assert_equal true, Topic.exists?
207
-
208
- assert_equal false, Topic.first.replies.exists?(nil)
209
- assert_equal true, Topic.first.replies.exists?
210
- end
211
-
212
- # ensures +exists?+ runs valid SQL by excluding order value
213
- def test_exists_with_order
214
- assert_equal true, Topic.order(:id).distinct.exists?
215
- end
216
-
217
- def test_exists_with_includes_limit_and_empty_result
218
- puts "finder_test.test_exists_with_includes_limit_and_empty_result"
219
- assert_equal false, Topic.includes(:replies).limit(0).exists?
220
- assert_equal false, Topic.includes(:replies).limit(1).where('0 = 1').exists?
221
- end
222
-
223
- def test_exists_with_distinct_association_includes_and_limit
224
- puts "finder_test.test_exists_with_distinct_association_includes_and_limit"
225
- author = Author.first
226
- assert_equal false, author.unique_categorized_posts.includes(:special_comments).limit(0).exists?
227
- assert_equal true, author.unique_categorized_posts.includes(:special_comments).limit(1).exists?
228
- end
229
-
230
- def test_exists_with_distinct_association_includes_limit_and_order
231
- puts "finder_test.test_exists_with_distinct_association_includes_limit_and_order"
232
- author = Author.first
233
- assert_equal false, author.unique_categorized_posts.includes(:special_comments).order('comments.tags_count DESC').limit(0).exists?
234
- assert_equal true, author.unique_categorized_posts.includes(:special_comments).order('comments.tags_count DESC').limit(1).exists?
235
- end
236
-
237
- def test_exists_with_empty_table_and_no_args_given
238
- Topic.delete_all
239
- assert_equal false, Topic.exists?
240
- end
241
-
242
- def test_exists_with_aggregate_having_three_mappings
243
- existing_address = customers(:david).address
244
- assert_equal true, Customer.exists?(:address => existing_address)
245
- end
246
-
247
- def test_exists_with_aggregate_having_three_mappings_with_one_difference
248
- existing_address = customers(:david).address
249
- assert_equal false, Customer.exists?(:address =>
250
- Address.new(existing_address.street, existing_address.city, existing_address.country + "1"))
251
- assert_equal false, Customer.exists?(:address =>
252
- Address.new(existing_address.street, existing_address.city + "1", existing_address.country))
253
- assert_equal false, Customer.exists?(:address =>
254
- Address.new(existing_address.street + "1", existing_address.city, existing_address.country))
255
- end
256
-
257
- def test_exists_does_not_instantiate_records
258
- assert_not_called(Developer, :instantiate) do
259
- Developer.exists?
260
- end
261
- end
262
-
263
- def test_find_by_array_of_one_id
264
- assert_kind_of(Array, Topic.find([ 1 ]))
265
- assert_equal(1, Topic.find([ 1 ]).length)
266
- end
267
-
268
- def test_find_by_ids
269
- assert_equal 2, Topic.find(1, 2).size
270
- assert_equal topics(:second).title, Topic.find([2]).first.title
271
- end
272
-
273
- def test_find_by_ids_with_limit_and_offset
274
- assert_equal 2, Entrant.limit(2).find([1,3,2]).size
275
- entrants = Entrant.limit(3).offset(2).find([1,3,2])
276
- assert_equal 1, entrants.size
277
- assert_equal 'Ruby Guru', entrants.first.name
278
-
279
- # Also test an edge case: If you have 11 results, and you set a
280
- # limit of 3 and offset of 9, then you should find that there
281
- # will be only 2 results, regardless of the limit.
282
- devs = Developer.all
283
- last_devs = Developer.limit(3).offset(9).find devs.map(&:id)
284
- assert_equal 2, last_devs.size
285
- assert_equal 'fixture_10', last_devs[0].name
286
- assert_equal 'Jamis', last_devs[1].name
287
- end
288
-
289
- unless current_adapter?(:IBM_DBAdapter)
290
- def test_find_with_large_number
291
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find('9999999999999999999999999999999') }
292
- end
293
-
294
- def test_find_by_with_large_number
295
- assert_nil Topic.find_by(id: '9999999999999999999999999999999')
296
- end
297
-
298
- def test_find_by_id_with_large_number
299
- assert_nil Topic.find_by_id('9999999999999999999999999999999')
300
- end
301
-
302
- def test_find_on_relation_with_large_number
303
- assert_nil Topic.where('1=1').find_by(id: 9999999999999999999999999999999)
304
- end
305
-
306
- def test_find_by_bang_on_relation_with_large_number
307
- assert_raises(ActiveRecord::RecordNotFound) do
308
- Topic.where('1=1').find_by!(id: 9999999999999999999999999999999)
309
- end
310
- end
311
- end
312
-
313
- def test_find_an_empty_array
314
- assert_equal [], Topic.find([])
315
- end
316
-
317
- def test_find_doesnt_have_implicit_ordering
318
- assert_sql(/^((?!ORDER).)*$/) { Topic.find(1) }
319
- end
320
-
321
- def test_find_by_ids_missing_one
322
- assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, 2, 45) }
323
- end
324
-
325
- def test_find_with_group_and_sanitized_having_method
326
- developers = Developer.group(:salary).having("sum(salary) > ?", 10000).select('salary').to_a
327
- assert_equal 3, developers.size
328
- assert_equal 3, developers.map(&:salary).uniq.size
329
- assert developers.all? { |developer| developer.salary > 10000 }
330
- end
331
-
332
- def test_find_with_entire_select_statement
333
- topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
334
-
335
- assert_equal(1, topics.size)
336
- assert_equal(topics(:second).title, topics.first.title)
337
- end
338
-
339
- def test_find_with_prepared_select_statement
340
- topics = Topic.find_by_sql ["SELECT * FROM topics WHERE author_name = ?", "Mary"]
341
-
342
- assert_equal(1, topics.size)
343
- assert_equal(topics(:second).title, topics.first.title)
344
- end
345
-
346
- def test_find_by_sql_with_sti_on_joined_table
347
- accounts = Account.find_by_sql("SELECT * FROM accounts INNER JOIN companies ON companies.id = accounts.firm_id")
348
- assert_equal [Account], accounts.collect(&:class).uniq
349
- end
350
-
351
- def test_find_by_association_subquery
352
- author = authors(:david)
353
- assert_equal author.post, Post.find_by(author: Author.where(id: author))
354
- assert_equal author.post, Post.find_by(author_id: Author.where(id: author))
355
- end
356
-
357
- def test_find_by_and_where_consistency_with_active_record_instance
358
- author = authors(:david)
359
- assert_equal Post.where(author_id: author).take, Post.find_by(author_id: author)
360
- end
361
-
362
- def test_take
363
- assert_equal topics(:first), Topic.take
364
- end
365
-
366
- def test_take_failing
367
- assert_nil Topic.where("title = 'This title does not exist'").take
368
- end
369
-
370
- def test_take_bang_present
371
- assert_nothing_raised do
372
- assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").take!
373
- end
374
- end
375
-
376
- def test_take_bang_missing
377
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
378
- Topic.where("title = 'This title does not exist'").take!
379
- end
380
- end
381
-
382
- def test_first
383
- assert_equal topics(:second).title, Topic.where("title = 'The Second Topic of the day'").first.title
384
- end
385
-
386
- def test_first_failing
387
- assert_nil Topic.where("title = 'The Second Topic of the day!'").first
388
- end
389
-
390
- def test_first_bang_present
391
- assert_nothing_raised do
392
- assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").first!
393
- end
394
- end
395
-
396
- def test_first_bang_missing
397
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
398
- Topic.where("title = 'This title does not exist'").first!
399
- end
400
- end
401
-
402
- def test_first_have_primary_key_order_by_default
403
- expected = topics(:first)
404
- expected.touch # PostgreSQL changes the default order if no order clause is used
405
- assert_equal expected, Topic.first
406
- end
407
-
408
- def test_model_class_responds_to_first_bang
409
- assert Topic.first!
410
- Topic.delete_all
411
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
412
- Topic.first!
413
- end
414
- end
415
-
416
- def test_second
417
- puts "finder_test.test_second"
418
- assert_equal topics(:second).title, Topic.second.title
419
- end
420
-
421
- def test_second_with_offset
422
- puts "finder_test.test_second_with_offset"
423
- assert_equal topics(:fifth), Topic.offset(3).second
424
- end
425
-
426
- def test_second_have_primary_key_order_by_default
427
- puts "finder_test.test_second_have_primary_key_order_by_default"
428
- expected = topics(:second)
429
- expected.touch # PostgreSQL changes the default order if no order clause is used
430
- assert_equal expected, Topic.second
431
- end
432
-
433
- def test_model_class_responds_to_second_bang
434
- assert Topic.second!
435
- Topic.delete_all
436
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
437
- Topic.second!
438
- end
439
- end
440
-
441
- def test_third
442
- puts "finder_test.test_third"
443
- assert_equal topics(:third).title, Topic.third.title
444
- end
445
-
446
- def test_third_with_offset
447
- puts "finder_test.test_third_with_offset"
448
- assert_equal topics(:fifth), Topic.offset(2).third
449
- end
450
-
451
- def test_third_have_primary_key_order_by_default
452
- puts "finder_test.test_third_have_primary_key_order_by_default"
453
- expected = topics(:third)
454
- expected.touch # PostgreSQL changes the default order if no order clause is used
455
- assert_equal expected, Topic.third
456
- end
457
-
458
- def test_model_class_responds_to_third_bang
459
- assert Topic.third!
460
- Topic.delete_all
461
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
462
- Topic.third!
463
- end
464
- end
465
-
466
- def test_fourth
467
- puts "finder_test.test_fourth"
468
- assert_equal topics(:fourth).title, Topic.fourth.title
469
- end
470
-
471
- def test_fourth_with_offset
472
- puts "finder_test.test_fourth_with_offset"
473
- assert_equal topics(:fifth), Topic.offset(1).fourth
474
- end
475
-
476
- def test_fourth_have_primary_key_order_by_default
477
- puts "finder_test.test_fourth_have_primary_key_order_by_default"
478
- expected = topics(:fourth)
479
- expected.touch # PostgreSQL changes the default order if no order clause is used
480
- assert_equal expected, Topic.fourth
481
- end
482
-
483
- def test_model_class_responds_to_fourth_bang
484
- assert Topic.fourth!
485
- Topic.delete_all
486
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
487
- Topic.fourth!
488
- end
489
- end
490
-
491
- def test_fifth
492
- assert_equal topics(:fifth).title, Topic.fifth.title
493
- end
494
-
495
- def test_fifth_with_offset
496
- puts "finder_test.test_fifth_with_offset"
497
- assert_equal topics(:fifth), Topic.offset(0).fifth
498
- end
499
-
500
- def test_fifth_have_primary_key_order_by_default
501
- puts "finder_test.test_fifth_have_primary_key_order_by_default"
502
- expected = topics(:fifth)
503
- expected.touch # PostgreSQL changes the default order if no order clause is used
504
- assert_equal expected, Topic.fifth
505
- end
506
-
507
- def test_model_class_responds_to_fifth_bang
508
- assert Topic.fifth!
509
- Topic.delete_all
510
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
511
- Topic.fifth!
512
- end
513
- end
514
-
515
- def test_second_to_last
516
- puts "finder_test.test_second_to_last"
517
- assert_equal topics(:fourth).title, Topic.second_to_last.title
518
-
519
- # test with offset
520
- assert_equal topics(:fourth), Topic.offset(1).second_to_last
521
- assert_equal topics(:fourth), Topic.offset(2).second_to_last
522
- assert_equal topics(:fourth), Topic.offset(3).second_to_last
523
- assert_equal nil, Topic.offset(4).second_to_last
524
- assert_equal nil, Topic.offset(5).second_to_last
525
-
526
- #test with limit
527
- # assert_equal nil, Topic.limit(1).second # TODO: currently failing
528
- assert_equal nil, Topic.limit(1).second_to_last
529
- end
530
-
531
- def test_second_to_last_have_primary_key_order_by_default
532
- expected = topics(:fourth)
533
- expected.touch # PostgreSQL changes the default order if no order clause is used
534
- assert_equal expected, Topic.second_to_last
535
- end
536
-
537
- def test_model_class_responds_to_second_to_last_bang
538
- assert Topic.second_to_last!
539
- Topic.delete_all
540
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
541
- Topic.second_to_last!
542
- end
543
- end
544
-
545
- def test_third_to_last
546
- assert_equal topics(:third).title, Topic.third_to_last.title
547
-
548
- # test with offset
549
- assert_equal topics(:third), Topic.offset(1).third_to_last
550
- assert_equal topics(:third), Topic.offset(2).third_to_last
551
- assert_equal nil, Topic.offset(3).third_to_last
552
- assert_equal nil, Topic.offset(4).third_to_last
553
- assert_equal nil, Topic.offset(5).third_to_last
554
-
555
- # test with limit
556
- # assert_equal nil, Topic.limit(1).third # TODO: currently failing
557
- assert_equal nil, Topic.limit(1).third_to_last
558
- # assert_equal nil, Topic.limit(2).third # TODO: currently failing
559
- assert_equal nil, Topic.limit(2).third_to_last
560
- end
561
-
562
- def test_third_to_last_have_primary_key_order_by_default
563
- expected = topics(:third)
564
- expected.touch # PostgreSQL changes the default order if no order clause is used
565
- assert_equal expected, Topic.third_to_last
566
- end
567
-
568
- def test_model_class_responds_to_third_to_last_bang
569
- assert Topic.third_to_last!
570
- Topic.delete_all
571
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
572
- Topic.third_to_last!
573
- end
574
- end
575
-
576
- def test_last_bang_present
577
- assert_nothing_raised do
578
- assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").last!
579
- end
580
- end
581
-
582
- def test_last_bang_missing
583
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
584
- Topic.where("title = 'This title does not exist'").last!
585
- end
586
- end
587
-
588
- def test_model_class_responds_to_last_bang
589
- assert_equal topics(:fifth), Topic.last!
590
- assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
591
- Topic.delete_all
592
- Topic.last!
593
- end
594
- end
595
-
596
- def test_take_and_first_and_last_with_integer_should_use_sql_limit
597
- puts "finder_test.test_take_and_first_and_last_with_integer_should_use_sql_limit"
598
- assert_sql(/LIMIT|ROWNUM <=|FETCH FIRST/) { Topic.take(3).entries }
599
- assert_sql(/LIMIT|ROWNUM <=|FETCH FIRST/) { Topic.first(2).entries }
600
- assert_sql(/LIMIT|ROWNUM <=|FETCH FIRST/) { Topic.last(5).entries }
601
- end
602
-
603
- def test_last_with_integer_and_order_should_keep_the_order
604
- puts "finder_test.test_last_with_integer_and_order_should_keep_the_order"
605
- assert_equal Topic.order("title").to_a.last(2), Topic.order("title").last(2)
606
- end
607
-
608
- def test_last_with_integer_and_order_should_use_sql_limit
609
- relation = Topic.order("title")
610
- assert_queries(1) { relation.last(5) }
611
- assert !relation.loaded?
612
- end
613
-
614
- def test_last_with_integer_and_reorder_should_use_sql_limit
615
- relation = Topic.reorder("title")
616
- assert_queries(1) { relation.last(5) }
617
- assert !relation.loaded?
618
- end
619
-
620
- def test_last_on_loaded_relation_should_not_use_sql
621
- relation = Topic.limit(10).load
622
- assert_no_queries do
623
- relation.last
624
- relation.last(2)
625
- end
626
- end
627
-
628
- def test_last_with_irreversible_order
629
- assert_deprecated do
630
- Topic.order("coalesce(author_name, title)").last
631
- end
632
- end
633
-
634
- def test_last_on_relation_with_limit_and_offset
635
- post = posts('sti_comments')
636
-
637
- comments = post.comments.order(id: :asc)
638
- assert_equal comments.limit(2).to_a.last, comments.limit(2).last
639
- assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2)
640
- assert_equal comments.limit(2).to_a.last(3), comments.limit(2).last(3)
641
-
642
- comments = comments.offset(1)
643
- assert_equal comments.limit(2).to_a.last, comments.limit(2).last
644
- assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2)
645
- assert_equal comments.limit(2).to_a.last(3), comments.limit(2).last(3)
646
- end
647
-
648
- def test_take_and_first_and_last_with_integer_should_return_an_array
649
- assert_kind_of Array, Topic.take(5)
650
- assert_kind_of Array, Topic.first(5)
651
- assert_kind_of Array, Topic.last(5)
652
- end
653
-
654
- def test_unexisting_record_exception_handling
655
- assert_raise(ActiveRecord::RecordNotFound) {
656
- Topic.find(1).parent
657
- }
658
-
659
- Topic.find(2).topic
660
- end
661
-
662
- def test_find_only_some_columns
663
- topic = Topic.select("author_name").find(1)
664
- assert_raise(ActiveModel::MissingAttributeError) {topic.title}
665
- assert_raise(ActiveModel::MissingAttributeError) {topic.title?}
666
- assert_nil topic.read_attribute("title")
667
- assert_equal "David", topic.author_name
668
- assert !topic.attribute_present?("title")
669
- assert !topic.attribute_present?(:title)
670
- assert topic.attribute_present?("author_name")
671
- assert_respond_to topic, "author_name"
672
- end
673
-
674
- def test_find_on_array_conditions
675
- assert Topic.where(["approved = ?", false]).find(1)
676
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where(["approved = ?", true]).find(1) }
677
- end
678
-
679
- def test_find_on_hash_conditions
680
- assert Topic.where(approved: false).find(1)
681
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where(approved: true).find(1) }
682
- end
683
-
684
- def test_find_on_hash_conditions_with_qualified_attribute_dot_notation_string
685
- assert Topic.where('topics.approved' => false).find(1)
686
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where('topics.approved' => true).find(1) }
687
- end
688
-
689
- def test_find_on_hash_conditions_with_qualified_attribute_dot_notation_symbol
690
- assert Topic.where('topics.approved': false).find(1)
691
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where('topics.approved': true).find(1) }
692
- end
693
-
694
- def test_find_on_hash_conditions_with_hashed_table_name
695
- assert Topic.where(topics: { approved: false }).find(1)
696
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where(topics: { approved: true }).find(1) }
697
- end
698
-
699
- def test_find_on_combined_explicit_and_hashed_table_names
700
- assert Topic.where('topics.approved' => false, topics: { author_name: "David" }).find(1)
701
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where('topics.approved' => true, topics: { author_name: "David" }).find(1) }
702
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where('topics.approved' => false, topics: { author_name: "Melanie" }).find(1) }
703
- end
704
-
705
- def test_find_with_hash_conditions_on_joined_table
706
- firms = Firm.joins(:account).where(:accounts => { :credit_limit => 50 })
707
- assert_equal 1, firms.size
708
- assert_equal companies(:first_firm), firms.first
709
- end
710
-
711
- def test_find_with_hash_conditions_on_joined_table_and_with_range
712
- firms = DependentFirm.joins(:account).where(name: 'RailsCore', accounts: { credit_limit: 55..60 })
713
- assert_equal 1, firms.size
714
- assert_equal companies(:rails_core), firms.first
715
- end
716
-
717
- def test_find_on_hash_conditions_with_explicit_table_name_and_aggregate
718
- david = customers(:david)
719
- assert Customer.where('customers.name' => david.name, :address => david.address).find(david.id)
720
- assert_raise(ActiveRecord::RecordNotFound) {
721
- Customer.where('customers.name' => david.name + "1", :address => david.address).find(david.id)
722
- }
723
- end
724
-
725
- def test_find_on_association_proxy_conditions
726
- assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10, 12], Comment.where(post_id: authors(:david).posts).map(&:id).sort
727
- end
728
-
729
- def test_find_on_hash_conditions_with_range
730
- assert_equal [1,2], Topic.where(id: 1..2).to_a.map(&:id).sort
731
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where(id: 2..3).find(1) }
732
- end
733
-
734
- def test_find_on_hash_conditions_with_end_exclusive_range
735
- assert_equal [1,2,3], Topic.where(id: 1..3).to_a.map(&:id).sort
736
- assert_equal [1,2], Topic.where(id: 1...3).to_a.map(&:id).sort
737
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where(id: 2...3).find(3) }
738
- end
739
-
740
- def test_find_on_hash_conditions_with_multiple_ranges
741
- assert_equal [1,2,3], Comment.where(id: 1..3, post_id: 1..2).to_a.map(&:id).sort
742
- assert_equal [1], Comment.where(id: 1..1, post_id: 1..10).to_a.map(&:id).sort
743
- end
744
-
745
- def test_find_on_hash_conditions_with_array_of_integers_and_ranges
746
- assert_equal [1,2,3,5,6,7,8,9], Comment.where(id: [1..2, 3, 5, 6..8, 9]).to_a.map(&:id).sort
747
- end
748
-
749
- def test_find_on_hash_conditions_with_array_of_ranges
750
- assert_equal [1,2,6,7,8], Comment.where(id: [1..2, 6..8]).to_a.map(&:id).sort
751
- end
752
-
753
- def test_find_on_multiple_hash_conditions
754
- assert Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: false).find(1)
755
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: true).find(1) }
756
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where(author_name: "David", title: "HHC", replies_count: 1, approved: false).find(1) }
757
- assert_raise(ActiveRecord::RecordNotFound) { Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: true).find(1) }
758
- end
759
-
760
- def test_condition_interpolation
761
- puts "finder_test.test_condition_interpolation"
762
- assert_kind_of Firm, Company.where("name = '%s'", "37signals").first
763
- assert_nil Company.where(["name = '%s'", "37signals!"]).first
764
- assert_nil Company.where(["name = '%s'", "37signals!' OR 1=1"]).first
765
- assert_kind_of Time, Topic.where(["id = %d", 1]).first.written_on
766
- end
767
-
768
- def test_condition_array_interpolation
769
- puts "finder_test.test_condition_array_interpolation"
770
- assert_kind_of Firm, Company.where(["name = '%s'", "37signals"]).first
771
- assert_nil Company.where(["name = '%s'", "37signals!"]).first
772
- assert_nil Company.where(["name = '%s'", "37signals!' OR 1=1"]).first
773
- assert_kind_of Time, Topic.where(["id = %d", 1]).first.written_on
774
- end
775
-
776
- def test_condition_hash_interpolation
777
- assert_kind_of Firm, Company.where(name: "37signals").first
778
- assert_nil Company.where(name: "37signals!").first
779
- assert_kind_of Time, Topic.where(id: 1).first.written_on
780
- end
781
-
782
- def test_hash_condition_find_malformed
783
- assert_raise(ActiveRecord::StatementInvalid) {
784
- Company.where(id: 2, dhh: true).first
785
- }
786
- end
787
-
788
- def test_hash_condition_find_with_escaped_characters
789
- puts "finder_test.test_hash_condition_find_with_escaped_characters"
790
- Company.create("name" => "Ain't noth'n like' \#stuff")
791
- assert Company.where(name: "Ain't noth'n like' \#stuff").first
792
- end
793
-
794
- def test_hash_condition_find_with_array
795
- puts "finder_test.test_hash_condition_find_with_array"
796
- p1, p2 = Post.limit(2).order('id asc').to_a
797
- assert_equal [p1, p2], Post.where(id: [p1, p2]).order('id asc').to_a
798
- assert_equal [p1, p2], Post.where(id: [p1, p2.id]).order('id asc').to_a
799
- end
800
-
801
- def test_hash_condition_find_with_nil
802
- topic = Topic.where(last_read: nil).first
803
- assert_not_nil topic
804
- assert_nil topic.last_read
805
- end
806
-
807
- def test_hash_condition_find_with_aggregate_having_one_mapping
808
- balance = customers(:david).balance
809
- assert_kind_of Money, balance
810
- found_customer = Customer.where(:balance => balance).first
811
- assert_equal customers(:david), found_customer
812
- end
813
-
814
- def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_aggregate
815
- gps_location = customers(:david).gps_location
816
- assert_kind_of GpsLocation, gps_location
817
- found_customer = Customer.where(:gps_location => gps_location).first
818
- assert_equal customers(:david), found_customer
819
- end
820
-
821
- def test_hash_condition_find_with_aggregate_having_one_mapping_and_key_value_being_attribute_value
822
- balance = customers(:david).balance
823
- assert_kind_of Money, balance
824
- found_customer = Customer.where(:balance => balance.amount).first
825
- assert_equal customers(:david), found_customer
826
- end
827
-
828
- def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_attribute_value
829
- gps_location = customers(:david).gps_location
830
- assert_kind_of GpsLocation, gps_location
831
- found_customer = Customer.where(:gps_location => gps_location.gps_location).first
832
- assert_equal customers(:david), found_customer
833
- end
834
-
835
- def test_hash_condition_find_with_aggregate_having_three_mappings
836
- address = customers(:david).address
837
- assert_kind_of Address, address
838
- found_customer = Customer.where(:address => address).first
839
- assert_equal customers(:david), found_customer
840
- end
841
-
842
- def test_hash_condition_find_with_one_condition_being_aggregate_and_another_not
843
- address = customers(:david).address
844
- assert_kind_of Address, address
845
- found_customer = Customer.where(:address => address, :name => customers(:david).name).first
846
- assert_equal customers(:david), found_customer
847
- end
848
-
849
- def test_condition_utc_time_interpolation_with_default_timezone_local
850
- with_env_tz 'America/New_York' do
851
- with_timezone_config default: :local do
852
- topic = Topic.first
853
- assert_equal topic, Topic.where(['written_on = ?', topic.written_on.getutc]).first
854
- end
855
- end
856
- end
857
-
858
- def test_hash_condition_utc_time_interpolation_with_default_timezone_local
859
- puts "finder_test.test_hash_condition_utc_time_interpolation_with_default_timezone_local"
860
- with_env_tz 'America/New_York' do
861
- with_timezone_config default: :local do
862
- topic = Topic.first
863
- assert_equal topic, Topic.where(written_on: topic.written_on.getutc).first
864
- end
865
- end
866
- end
867
-
868
- def test_condition_local_time_interpolation_with_default_timezone_utc
869
- puts "finder_test.test_condition_local_time_interpolation_with_default_timezone_utc"
870
- with_env_tz 'America/New_York' do
871
- with_timezone_config default: :utc do
872
- topic = Topic.first
873
- assert_equal topic, Topic.where(['written_on = ?', topic.written_on.getlocal]).first
874
- end
875
- end
876
- end
877
-
878
- def test_hash_condition_local_time_interpolation_with_default_timezone_utc
879
- puts "finder_test.test_hash_condition_local_time_interpolation_with_default_timezone_utc"
880
- with_env_tz 'America/New_York' do
881
- with_timezone_config default: :utc do
882
- topic = Topic.first
883
- assert_equal topic, Topic.where(written_on: topic.written_on.getlocal).first
884
- end
885
- end
886
- end
887
-
888
- def test_bind_variables
889
- assert_kind_of Firm, Company.where(["name = ?", "37signals"]).first
890
- assert_nil Company.where(["name = ?", "37signals!"]).first
891
- assert_nil Company.where(["name = ?", "37signals!' OR 1=1"]).first
892
- assert_kind_of Time, Topic.where(["id = ?", 1]).first.written_on
893
- assert_raise(ActiveRecord::PreparedStatementInvalid) {
894
- Company.where(["id=? AND name = ?", 2]).first
895
- }
896
- assert_raise(ActiveRecord::PreparedStatementInvalid) {
897
- Company.where(["id=?", 2, 3, 4]).first
898
- }
899
- end
900
-
901
- def test_bind_variables_with_quotes
902
- puts "finder_test.test_bind_variables_with_quotes"
903
- Company.create("name" => "37signals' go'es agains")
904
- assert Company.where(["name = ?", "37signals' go'es agains"]).first
905
- end
906
-
907
- def test_named_bind_variables_with_quotes
908
- puts "finder_test.test_named_bind_variables_with_quotes"
909
- Company.create("name" => "37signals' go'es agains")
910
- assert Company.where(["name = :name", {name: "37signals' go'es agains"}]).first
911
- end
912
-
913
- def test_named_bind_variables
914
- puts "finder_test.test_named_bind_variables"
915
- assert_kind_of Firm, Company.where(["name = :name", { name: "37signals" }]).first
916
- assert_nil Company.where(["name = :name", { name: "37signals!" }]).first
917
- assert_nil Company.where(["name = :name", { name: "37signals!' OR 1=1" }]).first
918
- assert_kind_of Time, Topic.where(["id = :id", { id: 1 }]).first.written_on
919
- end
920
-
921
- def test_string_sanitation
922
- assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
923
- assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table")
924
- end
925
-
926
- def test_count_by_sql
927
- assert_equal(0, Entrant.count_by_sql("SELECT COUNT(*) FROM entrants WHERE id > 3"))
928
- assert_equal(1, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 2]))
929
- assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
930
- end
931
-
932
- def test_find_by_one_attribute
933
- assert_equal topics(:first), Topic.find_by_title("The First Topic")
934
- assert_nil Topic.find_by_title("The First Topic!")
935
- end
936
-
937
- def test_find_by_one_attribute_bang
938
- assert_equal topics(:first), Topic.find_by_title!("The First Topic")
939
- assert_raises_with_message(ActiveRecord::RecordNotFound, "Couldn't find Topic") do
940
- Topic.find_by_title!("The First Topic!")
941
- end
942
- end
943
-
944
- def test_find_by_on_attribute_that_is_a_reserved_word
945
- puts "finder_test.test_find_by_on_attribute_that_is_a_reserved_word"
946
- dog_alias = 'Dog'
947
- dog = Dog.create(alias: dog_alias)
948
-
949
- assert_equal dog, Dog.find_by_alias(dog_alias)
950
- end
951
-
952
- def test_find_by_one_attribute_that_is_an_alias
953
- assert_equal topics(:first), Topic.find_by_heading("The First Topic")
954
- assert_nil Topic.find_by_heading("The First Topic!")
955
- end
956
-
957
- def test_find_by_one_attribute_bang_with_blank_defined
958
- puts "finder_test.test_find_by_one_attribute_bang_with_blank_defined"
959
- blank_topic = BlankTopic.create(title: "The Blank One")
960
- assert_equal blank_topic, BlankTopic.find_by_title!("The Blank One")
961
- end
962
-
963
- def test_find_by_one_attribute_with_conditions
964
- assert_equal accounts(:rails_core_account), Account.where('firm_id = ?', 6).find_by_credit_limit(50)
965
- end
966
-
967
- def test_find_by_one_attribute_that_is_an_aggregate
968
- address = customers(:david).address
969
- assert_kind_of Address, address
970
- found_customer = Customer.find_by_address(address)
971
- assert_equal customers(:david), found_customer
972
- end
973
-
974
- def test_find_by_one_attribute_that_is_an_aggregate_with_one_attribute_difference
975
- address = customers(:david).address
976
- assert_kind_of Address, address
977
- missing_address = Address.new(address.street, address.city, address.country + "1")
978
- assert_nil Customer.find_by_address(missing_address)
979
- missing_address = Address.new(address.street, address.city + "1", address.country)
980
- assert_nil Customer.find_by_address(missing_address)
981
- missing_address = Address.new(address.street + "1", address.city, address.country)
982
- assert_nil Customer.find_by_address(missing_address)
983
- end
984
-
985
- def test_find_by_two_attributes_that_are_both_aggregates
986
- balance = customers(:david).balance
987
- address = customers(:david).address
988
- assert_kind_of Money, balance
989
- assert_kind_of Address, address
990
- found_customer = Customer.find_by_balance_and_address(balance, address)
991
- assert_equal customers(:david), found_customer
992
- end
993
-
994
- def test_find_by_two_attributes_with_one_being_an_aggregate
995
- balance = customers(:david).balance
996
- assert_kind_of Money, balance
997
- found_customer = Customer.find_by_balance_and_name(balance, customers(:david).name)
998
- assert_equal customers(:david), found_customer
999
- end
1000
-
1001
- def test_dynamic_finder_on_one_attribute_with_conditions_returns_same_results_after_caching
1002
- # ensure this test can run independently of order
1003
- class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.include?(:find_by_credit_limit)
1004
- a = Account.where('firm_id = ?', 6).find_by_credit_limit(50)
1005
- assert_equal a, Account.where('firm_id = ?', 6).find_by_credit_limit(50) # find_by_credit_limit has been cached
1006
- end
1007
-
1008
- def test_find_by_one_attribute_with_several_options
1009
- assert_equal accounts(:unknown), Account.order('id DESC').where('id != ?', 3).find_by_credit_limit(50)
1010
- end
1011
-
1012
- def test_find_by_one_missing_attribute
1013
- assert_raise(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
1014
- end
1015
-
1016
- def test_find_by_invalid_method_syntax
1017
- assert_raise(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
1018
- assert_raise(NoMethodError) { Topic.find_by_title?("The First Topic") }
1019
- assert_raise(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
1020
- assert_raise(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
1021
- end
1022
-
1023
- def test_find_by_two_attributes
1024
- assert_equal topics(:first), Topic.find_by_title_and_author_name("The First Topic", "David")
1025
- assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")
1026
- end
1027
-
1028
- def test_find_by_two_attributes_but_passing_only_one
1029
- assert_raise(ArgumentError) { Topic.find_by_title_and_author_name("The First Topic") }
1030
- end
1031
-
1032
- def test_find_last_with_offset
1033
- puts "finder_test.test_find_last_with_offset"
1034
- devs = Developer.order('id')
1035
-
1036
- assert_equal devs[2], Developer.offset(2).first
1037
- assert_equal devs[-3], Developer.offset(2).last
1038
- assert_equal devs[-3], Developer.offset(2).last
1039
- assert_equal devs[-3], Developer.offset(2).order('id DESC').first
1040
- end
1041
-
1042
- def test_find_by_nil_attribute
1043
- topic = Topic.find_by_last_read nil
1044
- assert_not_nil topic
1045
- assert_nil topic.last_read
1046
- end
1047
-
1048
- def test_find_by_nil_and_not_nil_attributes
1049
- topic = Topic.find_by_last_read_and_author_name nil, "Mary"
1050
- assert_equal "Mary", topic.author_name
1051
- end
1052
-
1053
- def test_find_with_bad_sql
1054
- assert_raise(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
1055
- end
1056
-
1057
- def test_find_all_with_join
1058
- developers_on_project_one = Developer.
1059
- joins('LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id').
1060
- where('project_id=1').to_a
1061
- assert_equal 3, developers_on_project_one.length
1062
- developer_names = developers_on_project_one.map(&:name)
1063
- assert developer_names.include?('David')
1064
- assert developer_names.include?('Jamis')
1065
- end
1066
-
1067
- def test_joins_dont_clobber_id
1068
- first = Firm.
1069
- joins('INNER JOIN companies clients ON clients.firm_id = companies.id').
1070
- where('companies.id = 1').first
1071
- assert_equal 1, first.id
1072
- end
1073
-
1074
- def test_joins_with_string_array
1075
- person_with_reader_and_post = Post.
1076
- joins(["INNER JOIN categorizations ON categorizations.post_id = posts.id",
1077
- "INNER JOIN categories ON categories.id = categorizations.category_id AND categories.type = 'SpecialCategory'"
1078
- ])
1079
- assert_equal 1, person_with_reader_and_post.size
1080
- end
1081
-
1082
- def test_find_by_id_with_conditions_with_or
1083
- assert_nothing_raised do
1084
- Post.where("posts.id <= 3 OR posts.#{QUOTED_TYPE} = 'Post'").find([1,2,3])
1085
- end
1086
- end
1087
-
1088
- def test_find_ignores_previously_inserted_record
1089
- puts "finder_test.test_find_ignores_previously_inserted_record"
1090
- Post.create!(:title => 'test', :body => 'it out')
1091
- assert_equal [], Post.where(id: nil)
1092
- end
1093
-
1094
- def test_find_by_empty_ids
1095
- assert_equal [], Post.find([])
1096
- end
1097
-
1098
- def test_find_by_empty_in_condition
1099
- assert_equal [], Post.where('id in (?)', [])
1100
- end
1101
-
1102
- def test_find_by_records
1103
- puts "finder_test.test_find_by_records"
1104
- p1, p2 = Post.limit(2).order('id asc').to_a
1105
- assert_equal [p1, p2], Post.where(['id in (?)', [p1, p2]]).order('id asc')
1106
- assert_equal [p1, p2], Post.where(['id in (?)', [p1, p2.id]]).order('id asc')
1107
- end
1108
-
1109
- def test_select_value
1110
- assert_equal "37signals", Company.connection.select_value("SELECT name FROM companies WHERE id = 1")
1111
- assert_nil Company.connection.select_value("SELECT name FROM companies WHERE id = -1")
1112
- # make sure we didn't break count...
1113
- assert_equal 0, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = 'Halliburton'")
1114
- assert_equal 1, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = '37signals'")
1115
- end
1116
-
1117
- def test_select_values
1118
- assert_equal ["1","2","3","4","5","6","7","8","9", "10", "11"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map!(&:to_s)
1119
- assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy", "Ex Nihilo Part Deux", "Apex"], Company.connection.select_values("SELECT name FROM companies ORDER BY id")
1120
- end
1121
-
1122
- def test_select_rows
1123
- assert_equal(
1124
- [["1", "1", nil, "37signals"],
1125
- ["2", "1", "2", "Summit"],
1126
- ["3", "1", "1", "Microsoft"]],
1127
- Company.connection.select_rows("SELECT id, firm_id, client_of, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}})
1128
- assert_equal [["1", "37signals"], ["2", "Summit"], ["3", "Microsoft"]],
1129
- Company.connection.select_rows("SELECT id, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}}
1130
- end
1131
-
1132
- def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
1133
- puts "finder_test.test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct"
1134
- assert_equal 2, Post.includes(authors: :author_address).
1135
- where.not(author_addresses: { id: nil }).
1136
- order('author_addresses.id DESC').limit(2).to_a.size
1137
-
1138
- assert_equal 3, Post.includes(author: :author_address, authors: :author_address).
1139
- where.not(author_addresses_authors: { id: nil }).
1140
- order('author_addresses_authors.id DESC').limit(3).to_a.size
1141
- end
1142
-
1143
- def test_find_with_nil_inside_set_passed_for_one_attribute
1144
- client_of = Company.
1145
- where(client_of: [2, 1, nil],
1146
- name: ['37signals', 'Summit', 'Microsoft']).
1147
- order('client_of DESC').
1148
- map(&:client_of)
1149
-
1150
- assert client_of.include?(nil)
1151
- assert_equal [2, 1].sort, client_of.compact.sort
1152
- end
1153
-
1154
- def test_find_with_nil_inside_set_passed_for_attribute
1155
- client_of = Company.
1156
- where(client_of: [nil]).
1157
- order('client_of DESC').
1158
- map(&:client_of)
1159
-
1160
- assert_equal [], client_of.compact
1161
- end
1162
-
1163
- def test_with_limiting_with_custom_select
1164
- puts "finder_test.test_with_limiting_with_custom_select"
1165
- posts = Post.references(:authors).merge(
1166
- :includes => :author, :select => 'posts.*, authors.id as "author_id"',
1167
- :limit => 3, :order => 'posts.id'
1168
- ).to_a
1169
- assert_equal 3, posts.size
1170
- assert_equal [0, 1, 1], posts.map(&:author_id).sort
1171
- end
1172
-
1173
- def test_find_one_message_on_primary_key
1174
- e = assert_raises(ActiveRecord::RecordNotFound) do
1175
- Car.find(0)
1176
- end
1177
- assert_equal 0, e.id
1178
- assert_equal "id", e.primary_key
1179
- assert_equal "Car", e.model
1180
- assert_equal "Couldn't find Car with 'id'=0", e.message
1181
- end
1182
-
1183
- def test_find_one_message_with_custom_primary_key
1184
- table_with_custom_primary_key do |model|
1185
- model.primary_key = :name
1186
- e = assert_raises(ActiveRecord::RecordNotFound) do
1187
- model.find 'Hello World!'
1188
- end
1189
- assert_equal %Q{Couldn't find MercedesCar with 'name'=Hello World!}, e.message
1190
- end
1191
- end
1192
-
1193
- def test_find_some_message_with_custom_primary_key
1194
- table_with_custom_primary_key do |model|
1195
- model.primary_key = :name
1196
- e = assert_raises(ActiveRecord::RecordNotFound) do
1197
- model.find 'Hello', 'World!'
1198
- end
1199
- assert_equal %Q{Couldn't find all MercedesCars with 'name': (Hello, World!) (found 0 results, but was looking for 2)}, e.message
1200
- end
1201
- end
1202
-
1203
- def test_find_without_primary_key
1204
- assert_raises(ActiveRecord::UnknownPrimaryKey) do
1205
- Matey.find(1)
1206
- end
1207
- end
1208
-
1209
- def test_finder_with_offset_string
1210
- assert_nothing_raised { Topic.offset("3").to_a }
1211
- end
1212
-
1213
- test "find_by with hash conditions returns the first matching record" do
1214
- assert_equal posts(:eager_other), Post.find_by(id: posts(:eager_other).id)
1215
- end
1216
-
1217
- test "find_by with non-hash conditions returns the first matching record" do
1218
- assert_equal posts(:eager_other), Post.find_by("id = #{posts(:eager_other).id}")
1219
- end
1220
-
1221
- test "find_by with multi-arg conditions returns the first matching record" do
1222
- assert_equal posts(:eager_other), Post.find_by('id = ?', posts(:eager_other).id)
1223
- end
1224
-
1225
- test "find_by returns nil if the record is missing" do
1226
- assert_equal nil, Post.find_by("1 = 0")
1227
- end
1228
-
1229
- test "find_by with associations" do
1230
- assert_equal authors(:david), Post.find_by(author: authors(:david)).author
1231
- assert_equal authors(:mary) , Post.find_by(author: authors(:mary) ).author
1232
- end
1233
-
1234
- test "find_by doesn't have implicit ordering" do
1235
- assert_sql(/^((?!ORDER).)*$/) { Post.find_by(id: posts(:eager_other).id) }
1236
- end
1237
-
1238
- test "find_by! with hash conditions returns the first matching record" do
1239
- assert_equal posts(:eager_other), Post.find_by!(id: posts(:eager_other).id)
1240
- end
1241
-
1242
- test "find_by! with non-hash conditions returns the first matching record" do
1243
- assert_equal posts(:eager_other), Post.find_by!("id = #{posts(:eager_other).id}")
1244
- end
1245
-
1246
- test "find_by! with multi-arg conditions returns the first matching record" do
1247
- assert_equal posts(:eager_other), Post.find_by!('id = ?', posts(:eager_other).id)
1248
- end
1249
-
1250
- test "find_by! doesn't have implicit ordering" do
1251
- assert_sql(/^((?!ORDER).)*$/) { Post.find_by!(id: posts(:eager_other).id) }
1252
- end
1253
-
1254
- test "find_by! raises RecordNotFound if the record is missing" do
1255
- assert_raises(ActiveRecord::RecordNotFound) do
1256
- Post.find_by!("1 = 0")
1257
- end
1258
- end
1259
-
1260
- test "find on a scope does not perform statement caching" do
1261
- honda = cars(:honda)
1262
- zyke = cars(:zyke)
1263
- tyre = honda.tyres.create!
1264
- tyre2 = zyke.tyres.create!
1265
-
1266
- assert_equal tyre, honda.tyres.custom_find(tyre.id)
1267
- assert_equal tyre2, zyke.tyres.custom_find(tyre2.id)
1268
- end
1269
-
1270
- test "find_by on a scope does not perform statement caching" do
1271
- honda = cars(:honda)
1272
- zyke = cars(:zyke)
1273
- tyre = honda.tyres.create!
1274
- tyre2 = zyke.tyres.create!
1275
-
1276
- assert_equal tyre, honda.tyres.custom_find_by(id: tyre.id)
1277
- assert_equal tyre2, zyke.tyres.custom_find_by(id: tyre2.id)
1278
- end
1279
-
1280
- protected
1281
- def table_with_custom_primary_key
1282
- yield(Class.new(Toy) do
1283
- def self.name
1284
- 'MercedesCar'
1285
- end
1286
- end)
1287
- end
1288
-
1289
- def assert_raises_with_message(exception_class, message, &block)
1290
- err = assert_raises(exception_class) { block.call }
1291
- assert_match message, err.message
1292
- end
1293
-
1294
- end
1
+ require "cases/helper"
2
+ require 'models/post'
3
+ require 'models/author'
4
+ require 'models/categorization'
5
+ require 'models/comment'
6
+ require 'models/company'
7
+ require 'models/tagging'
8
+ require 'models/topic'
9
+ require 'models/reply'
10
+ require 'models/entrant'
11
+ require 'models/project'
12
+ require 'models/developer'
13
+ require 'models/computer'
14
+ require 'models/customer'
15
+ require 'models/toy'
16
+ require 'models/matey'
17
+ require 'models/dog'
18
+ require 'models/car'
19
+ require 'models/tyre'
20
+
21
+ class FinderTest < ActiveRecord::TestCase
22
+ fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :author_addresses, :customers, :categories, :categorizations, :cars
23
+
24
+ def test_find_by_id_with_hash
25
+ assert_raises(ActiveRecord::StatementInvalid) do
26
+ Post.find_by_id(:limit => 1)
27
+ end
28
+ end
29
+
30
+ def test_find_by_title_and_id_with_hash
31
+ assert_raises(ActiveRecord::StatementInvalid) do
32
+ Post.find_by_title_and_id('foo', :limit => 1)
33
+ end
34
+ end
35
+
36
+ def test_find
37
+ assert_equal(topics(:first).title, Topic.find(1).title)
38
+ end
39
+
40
+ def test_find_with_proc_parameter_and_block
41
+ exception = assert_raises(RuntimeError) do
42
+ Topic.all.find(-> { raise "should happen" }) { |e| e.title == "non-existing-title" }
43
+ end
44
+ assert_equal "should happen", exception.message
45
+
46
+ assert_nothing_raised do
47
+ Topic.all.find(-> { raise "should not happen" }) { |e| e.title == topics(:first).title }
48
+ end
49
+ end
50
+
51
+ def test_find_with_ids_returning_ordered
52
+ puts "finder_test.test_find_with_ids_returning_ordered"
53
+ records = Topic.find([4,2,5])
54
+ assert_equal 'The Fourth Topic of the day', records[0].title
55
+ assert_equal 'The Second Topic of the day', records[1].title
56
+ assert_equal 'The Fifth Topic of the day', records[2].title
57
+
58
+ records = Topic.find(4,2,5)
59
+ assert_equal 'The Fourth Topic of the day', records[0].title
60
+ assert_equal 'The Second Topic of the day', records[1].title
61
+ assert_equal 'The Fifth Topic of the day', records[2].title
62
+
63
+ records = Topic.find(['4','2','5'])
64
+ assert_equal 'The Fourth Topic of the day', records[0].title
65
+ assert_equal 'The Second Topic of the day', records[1].title
66
+ assert_equal 'The Fifth Topic of the day', records[2].title
67
+
68
+ records = Topic.find('4','2','5')
69
+ assert_equal 'The Fourth Topic of the day', records[0].title
70
+ assert_equal 'The Second Topic of the day', records[1].title
71
+ assert_equal 'The Fifth Topic of the day', records[2].title
72
+ end
73
+
74
+ def test_find_with_ids_and_order_clause
75
+ # The order clause takes precedence over the informed ids
76
+ records = Topic.order(:author_name).find([5,3,1])
77
+ assert_equal 'The Third Topic of the day', records[0].title
78
+ assert_equal 'The First Topic', records[1].title
79
+ assert_equal 'The Fifth Topic of the day', records[2].title
80
+
81
+ records = Topic.order(:id).find([5,3,1])
82
+ assert_equal 'The First Topic', records[0].title
83
+ assert_equal 'The Third Topic of the day', records[1].title
84
+ assert_equal 'The Fifth Topic of the day', records[2].title
85
+ end
86
+
87
+ def test_find_with_ids_with_limit_and_order_clause
88
+ puts "finder_test.test_find_with_ids_with_limit_and_order_clause"
89
+ # The order clause takes precedence over the informed ids
90
+ records = Topic.limit(2).order(:id).find([5,3,1])
91
+ assert_equal 2, records.size
92
+ assert_equal 'The First Topic', records[0].title
93
+ assert_equal 'The Third Topic of the day', records[1].title
94
+ end
95
+
96
+ def test_find_with_ids_and_limit
97
+ records = Topic.limit(3).find([3,2,5,1,4])
98
+ assert_equal 3, records.size
99
+ assert_equal 'The Third Topic of the day', records[0].title
100
+ assert_equal 'The Second Topic of the day', records[1].title
101
+ assert_equal 'The Fifth Topic of the day', records[2].title
102
+ end
103
+
104
+ def test_find_with_ids_where_and_limit
105
+ # Please note that Topic 1 is the only not approved so
106
+ # if it were among the first 3 it would raise an ActiveRecord::RecordNotFound
107
+ records = Topic.where(approved: true).limit(3).find([3,2,5,1,4])
108
+ assert_equal 3, records.size
109
+ assert_equal 'The Third Topic of the day', records[0].title
110
+ assert_equal 'The Second Topic of the day', records[1].title
111
+ assert_equal 'The Fifth Topic of the day', records[2].title
112
+ end
113
+
114
+ def test_find_with_ids_and_offset
115
+ records = Topic.offset(2).find([3,2,5,1,4])
116
+ assert_equal 3, records.size
117
+ assert_equal 'The Fifth Topic of the day', records[0].title
118
+ assert_equal 'The First Topic', records[1].title
119
+ assert_equal 'The Fourth Topic of the day', records[2].title
120
+ end
121
+
122
+ def test_find_passing_active_record_object_is_deprecated
123
+ assert_deprecated do
124
+ Topic.find(Topic.last)
125
+ end
126
+ end
127
+
128
+ def test_symbols_table_ref
129
+ gc_disabled = GC.disable
130
+ Post.where("author_id" => nil) # warm up
131
+ x = Symbol.all_symbols.count
132
+ Post.where("title" => {"xxxqqqq" => "bar"})
133
+ assert_equal x, Symbol.all_symbols.count
134
+ ensure
135
+ GC.enable if gc_disabled == false
136
+ end
137
+
138
+ # find should handle strings that come from URLs
139
+ # (example: Category.find(params[:id]))
140
+ def test_find_with_string
141
+ assert_equal(Topic.find(1).title,Topic.find("1").title)
142
+ end
143
+
144
+ def test_exists
145
+ assert_equal true, Topic.exists?(1)
146
+ assert_equal true, Topic.exists?("1")
147
+ assert_equal true, Topic.exists?(title: "The First Topic")
148
+ assert_equal true, Topic.exists?(heading: "The First Topic")
149
+ assert_equal true, Topic.exists?(:author_name => "Mary", :approved => true)
150
+ assert_equal true, Topic.exists?(["parent_id = ?", 1])
151
+ assert_equal true, Topic.exists?(id: [1, 9999])
152
+
153
+ assert_equal false, Topic.exists?(45)
154
+ assert_equal false, Topic.exists?(Topic.new.id)
155
+
156
+ assert_raise(NoMethodError) { Topic.exists?([1,2]) }
157
+ end
158
+
159
+ def test_exists_with_polymorphic_relation
160
+ puts "finder_test.test_exists_with_polymorphic_relation"
161
+ post = Post.create!(title: 'Post', body: 'default', taggings: [Tagging.new(comment: 'tagging comment')])
162
+ relation = Post.tagged_with_comment('tagging comment')
163
+
164
+ assert_equal true, relation.exists?(title: ['Post'])
165
+ assert_equal true, relation.exists?(['title LIKE ?', 'Post%'])
166
+ assert_equal true, relation.exists?
167
+ assert_equal true, relation.exists?(post.id)
168
+ assert_equal true, relation.exists?(post.id.to_s)
169
+
170
+ assert_equal false, relation.exists?(false)
171
+ end
172
+
173
+ def test_exists_passing_active_record_object_is_deprecated
174
+ assert_deprecated do
175
+ Topic.exists?(Topic.new)
176
+ end
177
+ end
178
+
179
+ def test_exists_fails_when_parameter_has_invalid_type
180
+ puts "finder_test.test_exists_fails_when_parameter_has_invalid_type"
181
+ assert_raises(ActiveModel::RangeError) do
182
+ assert_equal false, Topic.exists?(("9"*53).to_i) # number that's bigger than int
183
+ end
184
+ assert_equal false, Topic.exists?("foo")
185
+ end
186
+
187
+ def test_exists_does_not_select_columns_without_alias
188
+ puts "finder_test.test_exists_does_not_select_columns_without_alias"
189
+ assert_sql(/SELECT\W+1 AS one FROM ["`]topics["`]/i) do
190
+ Topic.exists?
191
+ end
192
+ end
193
+
194
+ def test_exists_returns_true_with_one_record_and_no_args
195
+ assert_equal true, Topic.exists?
196
+ end
197
+
198
+ def test_exists_returns_false_with_false_arg
199
+ assert_equal false, Topic.exists?(false)
200
+ end
201
+
202
+ # exists? should handle nil for id's that come from URLs and always return false
203
+ # (example: Topic.exists?(params[:id])) where params[:id] is nil
204
+ def test_exists_with_nil_arg
205
+ assert_equal false, Topic.exists?(nil)
206
+ assert_equal true, Topic.exists?
207
+
208
+ assert_equal false, Topic.first.replies.exists?(nil)
209
+ assert_equal true, Topic.first.replies.exists?
210
+ end
211
+
212
+ # ensures +exists?+ runs valid SQL by excluding order value
213
+ def test_exists_with_order
214
+ assert_equal true, Topic.order(:id).distinct.exists?
215
+ end
216
+
217
+ def test_exists_with_includes_limit_and_empty_result
218
+ puts "finder_test.test_exists_with_includes_limit_and_empty_result"
219
+ assert_equal false, Topic.includes(:replies).limit(0).exists?
220
+ assert_equal false, Topic.includes(:replies).limit(1).where('0 = 1').exists?
221
+ end
222
+
223
+ def test_exists_with_distinct_association_includes_and_limit
224
+ puts "finder_test.test_exists_with_distinct_association_includes_and_limit"
225
+ author = Author.first
226
+ assert_equal false, author.unique_categorized_posts.includes(:special_comments).limit(0).exists?
227
+ assert_equal true, author.unique_categorized_posts.includes(:special_comments).limit(1).exists?
228
+ end
229
+
230
+ def test_exists_with_distinct_association_includes_limit_and_order
231
+ puts "finder_test.test_exists_with_distinct_association_includes_limit_and_order"
232
+ author = Author.first
233
+ assert_equal false, author.unique_categorized_posts.includes(:special_comments).order('comments.tags_count DESC').limit(0).exists?
234
+ assert_equal true, author.unique_categorized_posts.includes(:special_comments).order('comments.tags_count DESC').limit(1).exists?
235
+ end
236
+
237
+ def test_exists_with_empty_table_and_no_args_given
238
+ Topic.delete_all
239
+ assert_equal false, Topic.exists?
240
+ end
241
+
242
+ def test_exists_with_aggregate_having_three_mappings
243
+ existing_address = customers(:david).address
244
+ assert_equal true, Customer.exists?(:address => existing_address)
245
+ end
246
+
247
+ def test_exists_with_aggregate_having_three_mappings_with_one_difference
248
+ existing_address = customers(:david).address
249
+ assert_equal false, Customer.exists?(:address =>
250
+ Address.new(existing_address.street, existing_address.city, existing_address.country + "1"))
251
+ assert_equal false, Customer.exists?(:address =>
252
+ Address.new(existing_address.street, existing_address.city + "1", existing_address.country))
253
+ assert_equal false, Customer.exists?(:address =>
254
+ Address.new(existing_address.street + "1", existing_address.city, existing_address.country))
255
+ end
256
+
257
+ def test_exists_does_not_instantiate_records
258
+ assert_not_called(Developer, :instantiate) do
259
+ Developer.exists?
260
+ end
261
+ end
262
+
263
+ def test_find_by_array_of_one_id
264
+ assert_kind_of(Array, Topic.find([ 1 ]))
265
+ assert_equal(1, Topic.find([ 1 ]).length)
266
+ end
267
+
268
+ def test_find_by_ids
269
+ assert_equal 2, Topic.find(1, 2).size
270
+ assert_equal topics(:second).title, Topic.find([2]).first.title
271
+ end
272
+
273
+ def test_find_by_ids_with_limit_and_offset
274
+ assert_equal 2, Entrant.limit(2).find([1,3,2]).size
275
+ entrants = Entrant.limit(3).offset(2).find([1,3,2])
276
+ assert_equal 1, entrants.size
277
+ assert_equal 'Ruby Guru', entrants.first.name
278
+
279
+ # Also test an edge case: If you have 11 results, and you set a
280
+ # limit of 3 and offset of 9, then you should find that there
281
+ # will be only 2 results, regardless of the limit.
282
+ devs = Developer.all
283
+ last_devs = Developer.limit(3).offset(9).find devs.map(&:id)
284
+ assert_equal 2, last_devs.size
285
+ assert_equal 'fixture_10', last_devs[0].name
286
+ assert_equal 'Jamis', last_devs[1].name
287
+ end
288
+
289
+ unless current_adapter?(:IBM_DBAdapter)
290
+ def test_find_with_large_number
291
+ assert_raises(ActiveRecord::RecordNotFound) { Topic.find('9999999999999999999999999999999') }
292
+ end
293
+
294
+ def test_find_by_with_large_number
295
+ assert_nil Topic.find_by(id: '9999999999999999999999999999999')
296
+ end
297
+
298
+ def test_find_by_id_with_large_number
299
+ assert_nil Topic.find_by_id('9999999999999999999999999999999')
300
+ end
301
+
302
+ def test_find_on_relation_with_large_number
303
+ assert_nil Topic.where('1=1').find_by(id: 9999999999999999999999999999999)
304
+ end
305
+
306
+ def test_find_by_bang_on_relation_with_large_number
307
+ assert_raises(ActiveRecord::RecordNotFound) do
308
+ Topic.where('1=1').find_by!(id: 9999999999999999999999999999999)
309
+ end
310
+ end
311
+ end
312
+
313
+ def test_find_an_empty_array
314
+ assert_equal [], Topic.find([])
315
+ end
316
+
317
+ def test_find_doesnt_have_implicit_ordering
318
+ assert_sql(/^((?!ORDER).)*$/) { Topic.find(1) }
319
+ end
320
+
321
+ def test_find_by_ids_missing_one
322
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, 2, 45) }
323
+ end
324
+
325
+ def test_find_with_group_and_sanitized_having_method
326
+ developers = Developer.group(:salary).having("sum(salary) > ?", 10000).select('salary').to_a
327
+ assert_equal 3, developers.size
328
+ assert_equal 3, developers.map(&:salary).uniq.size
329
+ assert developers.all? { |developer| developer.salary > 10000 }
330
+ end
331
+
332
+ def test_find_with_entire_select_statement
333
+ topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
334
+
335
+ assert_equal(1, topics.size)
336
+ assert_equal(topics(:second).title, topics.first.title)
337
+ end
338
+
339
+ def test_find_with_prepared_select_statement
340
+ topics = Topic.find_by_sql ["SELECT * FROM topics WHERE author_name = ?", "Mary"]
341
+
342
+ assert_equal(1, topics.size)
343
+ assert_equal(topics(:second).title, topics.first.title)
344
+ end
345
+
346
+ def test_find_by_sql_with_sti_on_joined_table
347
+ accounts = Account.find_by_sql("SELECT * FROM accounts INNER JOIN companies ON companies.id = accounts.firm_id")
348
+ assert_equal [Account], accounts.collect(&:class).uniq
349
+ end
350
+
351
+ def test_find_by_association_subquery
352
+ author = authors(:david)
353
+ assert_equal author.post, Post.find_by(author: Author.where(id: author))
354
+ assert_equal author.post, Post.find_by(author_id: Author.where(id: author))
355
+ end
356
+
357
+ def test_find_by_and_where_consistency_with_active_record_instance
358
+ author = authors(:david)
359
+ assert_equal Post.where(author_id: author).take, Post.find_by(author_id: author)
360
+ end
361
+
362
+ def test_take
363
+ assert_equal topics(:first), Topic.take
364
+ end
365
+
366
+ def test_take_failing
367
+ assert_nil Topic.where("title = 'This title does not exist'").take
368
+ end
369
+
370
+ def test_take_bang_present
371
+ assert_nothing_raised do
372
+ assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").take!
373
+ end
374
+ end
375
+
376
+ def test_take_bang_missing
377
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
378
+ Topic.where("title = 'This title does not exist'").take!
379
+ end
380
+ end
381
+
382
+ def test_first
383
+ assert_equal topics(:second).title, Topic.where("title = 'The Second Topic of the day'").first.title
384
+ end
385
+
386
+ def test_first_failing
387
+ assert_nil Topic.where("title = 'The Second Topic of the day!'").first
388
+ end
389
+
390
+ def test_first_bang_present
391
+ assert_nothing_raised do
392
+ assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").first!
393
+ end
394
+ end
395
+
396
+ def test_first_bang_missing
397
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
398
+ Topic.where("title = 'This title does not exist'").first!
399
+ end
400
+ end
401
+
402
+ def test_first_have_primary_key_order_by_default
403
+ expected = topics(:first)
404
+ expected.touch # PostgreSQL changes the default order if no order clause is used
405
+ assert_equal expected, Topic.first
406
+ end
407
+
408
+ def test_model_class_responds_to_first_bang
409
+ assert Topic.first!
410
+ Topic.delete_all
411
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
412
+ Topic.first!
413
+ end
414
+ end
415
+
416
+ def test_second
417
+ puts "finder_test.test_second"
418
+ assert_equal topics(:second).title, Topic.second.title
419
+ end
420
+
421
+ def test_second_with_offset
422
+ puts "finder_test.test_second_with_offset"
423
+ assert_equal topics(:fifth), Topic.offset(3).second
424
+ end
425
+
426
+ def test_second_have_primary_key_order_by_default
427
+ puts "finder_test.test_second_have_primary_key_order_by_default"
428
+ expected = topics(:second)
429
+ expected.touch # PostgreSQL changes the default order if no order clause is used
430
+ assert_equal expected, Topic.second
431
+ end
432
+
433
+ def test_model_class_responds_to_second_bang
434
+ assert Topic.second!
435
+ Topic.delete_all
436
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
437
+ Topic.second!
438
+ end
439
+ end
440
+
441
+ def test_third
442
+ puts "finder_test.test_third"
443
+ assert_equal topics(:third).title, Topic.third.title
444
+ end
445
+
446
+ def test_third_with_offset
447
+ puts "finder_test.test_third_with_offset"
448
+ assert_equal topics(:fifth), Topic.offset(2).third
449
+ end
450
+
451
+ def test_third_have_primary_key_order_by_default
452
+ puts "finder_test.test_third_have_primary_key_order_by_default"
453
+ expected = topics(:third)
454
+ expected.touch # PostgreSQL changes the default order if no order clause is used
455
+ assert_equal expected, Topic.third
456
+ end
457
+
458
+ def test_model_class_responds_to_third_bang
459
+ assert Topic.third!
460
+ Topic.delete_all
461
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
462
+ Topic.third!
463
+ end
464
+ end
465
+
466
+ def test_fourth
467
+ puts "finder_test.test_fourth"
468
+ assert_equal topics(:fourth).title, Topic.fourth.title
469
+ end
470
+
471
+ def test_fourth_with_offset
472
+ puts "finder_test.test_fourth_with_offset"
473
+ assert_equal topics(:fifth), Topic.offset(1).fourth
474
+ end
475
+
476
+ def test_fourth_have_primary_key_order_by_default
477
+ puts "finder_test.test_fourth_have_primary_key_order_by_default"
478
+ expected = topics(:fourth)
479
+ expected.touch # PostgreSQL changes the default order if no order clause is used
480
+ assert_equal expected, Topic.fourth
481
+ end
482
+
483
+ def test_model_class_responds_to_fourth_bang
484
+ assert Topic.fourth!
485
+ Topic.delete_all
486
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
487
+ Topic.fourth!
488
+ end
489
+ end
490
+
491
+ def test_fifth
492
+ assert_equal topics(:fifth).title, Topic.fifth.title
493
+ end
494
+
495
+ def test_fifth_with_offset
496
+ puts "finder_test.test_fifth_with_offset"
497
+ assert_equal topics(:fifth), Topic.offset(0).fifth
498
+ end
499
+
500
+ def test_fifth_have_primary_key_order_by_default
501
+ puts "finder_test.test_fifth_have_primary_key_order_by_default"
502
+ expected = topics(:fifth)
503
+ expected.touch # PostgreSQL changes the default order if no order clause is used
504
+ assert_equal expected, Topic.fifth
505
+ end
506
+
507
+ def test_model_class_responds_to_fifth_bang
508
+ assert Topic.fifth!
509
+ Topic.delete_all
510
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
511
+ Topic.fifth!
512
+ end
513
+ end
514
+
515
+ def test_second_to_last
516
+ puts "finder_test.test_second_to_last"
517
+ assert_equal topics(:fourth).title, Topic.second_to_last.title
518
+
519
+ # test with offset
520
+ assert_equal topics(:fourth), Topic.offset(1).second_to_last
521
+ assert_equal topics(:fourth), Topic.offset(2).second_to_last
522
+ assert_equal topics(:fourth), Topic.offset(3).second_to_last
523
+ assert_equal nil, Topic.offset(4).second_to_last
524
+ assert_equal nil, Topic.offset(5).second_to_last
525
+
526
+ #test with limit
527
+ # assert_equal nil, Topic.limit(1).second # TODO: currently failing
528
+ assert_equal nil, Topic.limit(1).second_to_last
529
+ end
530
+
531
+ def test_second_to_last_have_primary_key_order_by_default
532
+ expected = topics(:fourth)
533
+ expected.touch # PostgreSQL changes the default order if no order clause is used
534
+ assert_equal expected, Topic.second_to_last
535
+ end
536
+
537
+ def test_model_class_responds_to_second_to_last_bang
538
+ assert Topic.second_to_last!
539
+ Topic.delete_all
540
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
541
+ Topic.second_to_last!
542
+ end
543
+ end
544
+
545
+ def test_third_to_last
546
+ assert_equal topics(:third).title, Topic.third_to_last.title
547
+
548
+ # test with offset
549
+ assert_equal topics(:third), Topic.offset(1).third_to_last
550
+ assert_equal topics(:third), Topic.offset(2).third_to_last
551
+ assert_equal nil, Topic.offset(3).third_to_last
552
+ assert_equal nil, Topic.offset(4).third_to_last
553
+ assert_equal nil, Topic.offset(5).third_to_last
554
+
555
+ # test with limit
556
+ # assert_equal nil, Topic.limit(1).third # TODO: currently failing
557
+ assert_equal nil, Topic.limit(1).third_to_last
558
+ # assert_equal nil, Topic.limit(2).third # TODO: currently failing
559
+ assert_equal nil, Topic.limit(2).third_to_last
560
+ end
561
+
562
+ def test_third_to_last_have_primary_key_order_by_default
563
+ expected = topics(:third)
564
+ expected.touch # PostgreSQL changes the default order if no order clause is used
565
+ assert_equal expected, Topic.third_to_last
566
+ end
567
+
568
+ def test_model_class_responds_to_third_to_last_bang
569
+ assert Topic.third_to_last!
570
+ Topic.delete_all
571
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
572
+ Topic.third_to_last!
573
+ end
574
+ end
575
+
576
+ def test_last_bang_present
577
+ assert_nothing_raised do
578
+ assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").last!
579
+ end
580
+ end
581
+
582
+ def test_last_bang_missing
583
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
584
+ Topic.where("title = 'This title does not exist'").last!
585
+ end
586
+ end
587
+
588
+ def test_model_class_responds_to_last_bang
589
+ assert_equal topics(:fifth), Topic.last!
590
+ assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
591
+ Topic.delete_all
592
+ Topic.last!
593
+ end
594
+ end
595
+
596
+ def test_take_and_first_and_last_with_integer_should_use_sql_limit
597
+ puts "finder_test.test_take_and_first_and_last_with_integer_should_use_sql_limit"
598
+ assert_sql(/LIMIT|ROWNUM <=|FETCH FIRST/) { Topic.take(3).entries }
599
+ assert_sql(/LIMIT|ROWNUM <=|FETCH FIRST/) { Topic.first(2).entries }
600
+ assert_sql(/LIMIT|ROWNUM <=|FETCH FIRST/) { Topic.last(5).entries }
601
+ end
602
+
603
+ def test_last_with_integer_and_order_should_keep_the_order
604
+ puts "finder_test.test_last_with_integer_and_order_should_keep_the_order"
605
+ assert_equal Topic.order("title").to_a.last(2), Topic.order("title").last(2)
606
+ end
607
+
608
+ def test_last_with_integer_and_order_should_use_sql_limit
609
+ relation = Topic.order("title")
610
+ assert_queries(1) { relation.last(5) }
611
+ assert !relation.loaded?
612
+ end
613
+
614
+ def test_last_with_integer_and_reorder_should_use_sql_limit
615
+ relation = Topic.reorder("title")
616
+ assert_queries(1) { relation.last(5) }
617
+ assert !relation.loaded?
618
+ end
619
+
620
+ def test_last_on_loaded_relation_should_not_use_sql
621
+ relation = Topic.limit(10).load
622
+ assert_no_queries do
623
+ relation.last
624
+ relation.last(2)
625
+ end
626
+ end
627
+
628
+ def test_last_with_irreversible_order
629
+ assert_deprecated do
630
+ Topic.order("coalesce(author_name, title)").last
631
+ end
632
+ end
633
+
634
+ def test_last_on_relation_with_limit_and_offset
635
+ post = posts('sti_comments')
636
+
637
+ comments = post.comments.order(id: :asc)
638
+ assert_equal comments.limit(2).to_a.last, comments.limit(2).last
639
+ assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2)
640
+ assert_equal comments.limit(2).to_a.last(3), comments.limit(2).last(3)
641
+
642
+ comments = comments.offset(1)
643
+ assert_equal comments.limit(2).to_a.last, comments.limit(2).last
644
+ assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2)
645
+ assert_equal comments.limit(2).to_a.last(3), comments.limit(2).last(3)
646
+ end
647
+
648
+ def test_take_and_first_and_last_with_integer_should_return_an_array
649
+ assert_kind_of Array, Topic.take(5)
650
+ assert_kind_of Array, Topic.first(5)
651
+ assert_kind_of Array, Topic.last(5)
652
+ end
653
+
654
+ def test_unexisting_record_exception_handling
655
+ assert_raise(ActiveRecord::RecordNotFound) {
656
+ Topic.find(1).parent
657
+ }
658
+
659
+ Topic.find(2).topic
660
+ end
661
+
662
+ def test_find_only_some_columns
663
+ topic = Topic.select("author_name").find(1)
664
+ assert_raise(ActiveModel::MissingAttributeError) {topic.title}
665
+ assert_raise(ActiveModel::MissingAttributeError) {topic.title?}
666
+ assert_nil topic.read_attribute("title")
667
+ assert_equal "David", topic.author_name
668
+ assert !topic.attribute_present?("title")
669
+ assert !topic.attribute_present?(:title)
670
+ assert topic.attribute_present?("author_name")
671
+ assert_respond_to topic, "author_name"
672
+ end
673
+
674
+ def test_find_on_array_conditions
675
+ assert Topic.where(["approved = ?", false]).find(1)
676
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where(["approved = ?", true]).find(1) }
677
+ end
678
+
679
+ def test_find_on_hash_conditions
680
+ assert Topic.where(approved: false).find(1)
681
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where(approved: true).find(1) }
682
+ end
683
+
684
+ def test_find_on_hash_conditions_with_qualified_attribute_dot_notation_string
685
+ assert Topic.where('topics.approved' => false).find(1)
686
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where('topics.approved' => true).find(1) }
687
+ end
688
+
689
+ def test_find_on_hash_conditions_with_qualified_attribute_dot_notation_symbol
690
+ assert Topic.where('topics.approved': false).find(1)
691
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where('topics.approved': true).find(1) }
692
+ end
693
+
694
+ def test_find_on_hash_conditions_with_hashed_table_name
695
+ assert Topic.where(topics: { approved: false }).find(1)
696
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where(topics: { approved: true }).find(1) }
697
+ end
698
+
699
+ def test_find_on_combined_explicit_and_hashed_table_names
700
+ assert Topic.where('topics.approved' => false, topics: { author_name: "David" }).find(1)
701
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where('topics.approved' => true, topics: { author_name: "David" }).find(1) }
702
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where('topics.approved' => false, topics: { author_name: "Melanie" }).find(1) }
703
+ end
704
+
705
+ def test_find_with_hash_conditions_on_joined_table
706
+ firms = Firm.joins(:account).where(:accounts => { :credit_limit => 50 })
707
+ assert_equal 1, firms.size
708
+ assert_equal companies(:first_firm), firms.first
709
+ end
710
+
711
+ def test_find_with_hash_conditions_on_joined_table_and_with_range
712
+ firms = DependentFirm.joins(:account).where(name: 'RailsCore', accounts: { credit_limit: 55..60 })
713
+ assert_equal 1, firms.size
714
+ assert_equal companies(:rails_core), firms.first
715
+ end
716
+
717
+ def test_find_on_hash_conditions_with_explicit_table_name_and_aggregate
718
+ david = customers(:david)
719
+ assert Customer.where('customers.name' => david.name, :address => david.address).find(david.id)
720
+ assert_raise(ActiveRecord::RecordNotFound) {
721
+ Customer.where('customers.name' => david.name + "1", :address => david.address).find(david.id)
722
+ }
723
+ end
724
+
725
+ def test_find_on_association_proxy_conditions
726
+ assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10, 12], Comment.where(post_id: authors(:david).posts).map(&:id).sort
727
+ end
728
+
729
+ def test_find_on_hash_conditions_with_range
730
+ assert_equal [1,2], Topic.where(id: 1..2).to_a.map(&:id).sort
731
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where(id: 2..3).find(1) }
732
+ end
733
+
734
+ def test_find_on_hash_conditions_with_end_exclusive_range
735
+ assert_equal [1,2,3], Topic.where(id: 1..3).to_a.map(&:id).sort
736
+ assert_equal [1,2], Topic.where(id: 1...3).to_a.map(&:id).sort
737
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where(id: 2...3).find(3) }
738
+ end
739
+
740
+ def test_find_on_hash_conditions_with_multiple_ranges
741
+ assert_equal [1,2,3], Comment.where(id: 1..3, post_id: 1..2).to_a.map(&:id).sort
742
+ assert_equal [1], Comment.where(id: 1..1, post_id: 1..10).to_a.map(&:id).sort
743
+ end
744
+
745
+ def test_find_on_hash_conditions_with_array_of_integers_and_ranges
746
+ assert_equal [1,2,3,5,6,7,8,9], Comment.where(id: [1..2, 3, 5, 6..8, 9]).to_a.map(&:id).sort
747
+ end
748
+
749
+ def test_find_on_hash_conditions_with_array_of_ranges
750
+ assert_equal [1,2,6,7,8], Comment.where(id: [1..2, 6..8]).to_a.map(&:id).sort
751
+ end
752
+
753
+ def test_find_on_multiple_hash_conditions
754
+ assert Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: false).find(1)
755
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: true).find(1) }
756
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where(author_name: "David", title: "HHC", replies_count: 1, approved: false).find(1) }
757
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: true).find(1) }
758
+ end
759
+
760
+ def test_condition_interpolation
761
+ puts "finder_test.test_condition_interpolation"
762
+ assert_kind_of Firm, Company.where("name = '%s'", "37signals").first
763
+ assert_nil Company.where(["name = '%s'", "37signals!"]).first
764
+ assert_nil Company.where(["name = '%s'", "37signals!' OR 1=1"]).first
765
+ assert_kind_of Time, Topic.where(["id = %d", 1]).first.written_on
766
+ end
767
+
768
+ def test_condition_array_interpolation
769
+ puts "finder_test.test_condition_array_interpolation"
770
+ assert_kind_of Firm, Company.where(["name = '%s'", "37signals"]).first
771
+ assert_nil Company.where(["name = '%s'", "37signals!"]).first
772
+ assert_nil Company.where(["name = '%s'", "37signals!' OR 1=1"]).first
773
+ assert_kind_of Time, Topic.where(["id = %d", 1]).first.written_on
774
+ end
775
+
776
+ def test_condition_hash_interpolation
777
+ assert_kind_of Firm, Company.where(name: "37signals").first
778
+ assert_nil Company.where(name: "37signals!").first
779
+ assert_kind_of Time, Topic.where(id: 1).first.written_on
780
+ end
781
+
782
+ def test_hash_condition_find_malformed
783
+ assert_raise(ActiveRecord::StatementInvalid) {
784
+ Company.where(id: 2, dhh: true).first
785
+ }
786
+ end
787
+
788
+ def test_hash_condition_find_with_escaped_characters
789
+ puts "finder_test.test_hash_condition_find_with_escaped_characters"
790
+ Company.create("name" => "Ain't noth'n like' \#stuff")
791
+ assert Company.where(name: "Ain't noth'n like' \#stuff").first
792
+ end
793
+
794
+ def test_hash_condition_find_with_array
795
+ puts "finder_test.test_hash_condition_find_with_array"
796
+ p1, p2 = Post.limit(2).order('id asc').to_a
797
+ assert_equal [p1, p2], Post.where(id: [p1, p2]).order('id asc').to_a
798
+ assert_equal [p1, p2], Post.where(id: [p1, p2.id]).order('id asc').to_a
799
+ end
800
+
801
+ def test_hash_condition_find_with_nil
802
+ topic = Topic.where(last_read: nil).first
803
+ assert_not_nil topic
804
+ assert_nil topic.last_read
805
+ end
806
+
807
+ def test_hash_condition_find_with_aggregate_having_one_mapping
808
+ balance = customers(:david).balance
809
+ assert_kind_of Money, balance
810
+ found_customer = Customer.where(:balance => balance).first
811
+ assert_equal customers(:david), found_customer
812
+ end
813
+
814
+ def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_aggregate
815
+ gps_location = customers(:david).gps_location
816
+ assert_kind_of GpsLocation, gps_location
817
+ found_customer = Customer.where(:gps_location => gps_location).first
818
+ assert_equal customers(:david), found_customer
819
+ end
820
+
821
+ def test_hash_condition_find_with_aggregate_having_one_mapping_and_key_value_being_attribute_value
822
+ balance = customers(:david).balance
823
+ assert_kind_of Money, balance
824
+ found_customer = Customer.where(:balance => balance.amount).first
825
+ assert_equal customers(:david), found_customer
826
+ end
827
+
828
+ def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_attribute_value
829
+ gps_location = customers(:david).gps_location
830
+ assert_kind_of GpsLocation, gps_location
831
+ found_customer = Customer.where(:gps_location => gps_location.gps_location).first
832
+ assert_equal customers(:david), found_customer
833
+ end
834
+
835
+ def test_hash_condition_find_with_aggregate_having_three_mappings
836
+ address = customers(:david).address
837
+ assert_kind_of Address, address
838
+ found_customer = Customer.where(:address => address).first
839
+ assert_equal customers(:david), found_customer
840
+ end
841
+
842
+ def test_hash_condition_find_with_one_condition_being_aggregate_and_another_not
843
+ address = customers(:david).address
844
+ assert_kind_of Address, address
845
+ found_customer = Customer.where(:address => address, :name => customers(:david).name).first
846
+ assert_equal customers(:david), found_customer
847
+ end
848
+
849
+ def test_condition_utc_time_interpolation_with_default_timezone_local
850
+ with_env_tz 'America/New_York' do
851
+ with_timezone_config default: :local do
852
+ topic = Topic.first
853
+ assert_equal topic, Topic.where(['written_on = ?', topic.written_on.getutc]).first
854
+ end
855
+ end
856
+ end
857
+
858
+ def test_hash_condition_utc_time_interpolation_with_default_timezone_local
859
+ puts "finder_test.test_hash_condition_utc_time_interpolation_with_default_timezone_local"
860
+ with_env_tz 'America/New_York' do
861
+ with_timezone_config default: :local do
862
+ topic = Topic.first
863
+ assert_equal topic, Topic.where(written_on: topic.written_on.getutc).first
864
+ end
865
+ end
866
+ end
867
+
868
+ def test_condition_local_time_interpolation_with_default_timezone_utc
869
+ puts "finder_test.test_condition_local_time_interpolation_with_default_timezone_utc"
870
+ with_env_tz 'America/New_York' do
871
+ with_timezone_config default: :utc do
872
+ topic = Topic.first
873
+ assert_equal topic, Topic.where(['written_on = ?', topic.written_on.getlocal]).first
874
+ end
875
+ end
876
+ end
877
+
878
+ def test_hash_condition_local_time_interpolation_with_default_timezone_utc
879
+ puts "finder_test.test_hash_condition_local_time_interpolation_with_default_timezone_utc"
880
+ with_env_tz 'America/New_York' do
881
+ with_timezone_config default: :utc do
882
+ topic = Topic.first
883
+ assert_equal topic, Topic.where(written_on: topic.written_on.getlocal).first
884
+ end
885
+ end
886
+ end
887
+
888
+ def test_bind_variables
889
+ assert_kind_of Firm, Company.where(["name = ?", "37signals"]).first
890
+ assert_nil Company.where(["name = ?", "37signals!"]).first
891
+ assert_nil Company.where(["name = ?", "37signals!' OR 1=1"]).first
892
+ assert_kind_of Time, Topic.where(["id = ?", 1]).first.written_on
893
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
894
+ Company.where(["id=? AND name = ?", 2]).first
895
+ }
896
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
897
+ Company.where(["id=?", 2, 3, 4]).first
898
+ }
899
+ end
900
+
901
+ def test_bind_variables_with_quotes
902
+ puts "finder_test.test_bind_variables_with_quotes"
903
+ Company.create("name" => "37signals' go'es agains")
904
+ assert Company.where(["name = ?", "37signals' go'es agains"]).first
905
+ end
906
+
907
+ def test_named_bind_variables_with_quotes
908
+ puts "finder_test.test_named_bind_variables_with_quotes"
909
+ Company.create("name" => "37signals' go'es agains")
910
+ assert Company.where(["name = :name", {name: "37signals' go'es agains"}]).first
911
+ end
912
+
913
+ def test_named_bind_variables
914
+ puts "finder_test.test_named_bind_variables"
915
+ assert_kind_of Firm, Company.where(["name = :name", { name: "37signals" }]).first
916
+ assert_nil Company.where(["name = :name", { name: "37signals!" }]).first
917
+ assert_nil Company.where(["name = :name", { name: "37signals!' OR 1=1" }]).first
918
+ assert_kind_of Time, Topic.where(["id = :id", { id: 1 }]).first.written_on
919
+ end
920
+
921
+ def test_string_sanitation
922
+ assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
923
+ assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table")
924
+ end
925
+
926
+ def test_count_by_sql
927
+ assert_equal(0, Entrant.count_by_sql("SELECT COUNT(*) FROM entrants WHERE id > 3"))
928
+ assert_equal(1, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 2]))
929
+ assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
930
+ end
931
+
932
+ def test_find_by_one_attribute
933
+ assert_equal topics(:first), Topic.find_by_title("The First Topic")
934
+ assert_nil Topic.find_by_title("The First Topic!")
935
+ end
936
+
937
+ def test_find_by_one_attribute_bang
938
+ assert_equal topics(:first), Topic.find_by_title!("The First Topic")
939
+ assert_raises_with_message(ActiveRecord::RecordNotFound, "Couldn't find Topic") do
940
+ Topic.find_by_title!("The First Topic!")
941
+ end
942
+ end
943
+
944
+ def test_find_by_on_attribute_that_is_a_reserved_word
945
+ puts "finder_test.test_find_by_on_attribute_that_is_a_reserved_word"
946
+ dog_alias = 'Dog'
947
+ dog = Dog.create(alias: dog_alias)
948
+
949
+ assert_equal dog, Dog.find_by_alias(dog_alias)
950
+ end
951
+
952
+ def test_find_by_one_attribute_that_is_an_alias
953
+ assert_equal topics(:first), Topic.find_by_heading("The First Topic")
954
+ assert_nil Topic.find_by_heading("The First Topic!")
955
+ end
956
+
957
+ def test_find_by_one_attribute_bang_with_blank_defined
958
+ puts "finder_test.test_find_by_one_attribute_bang_with_blank_defined"
959
+ blank_topic = BlankTopic.create(title: "The Blank One")
960
+ assert_equal blank_topic, BlankTopic.find_by_title!("The Blank One")
961
+ end
962
+
963
+ def test_find_by_one_attribute_with_conditions
964
+ assert_equal accounts(:rails_core_account), Account.where('firm_id = ?', 6).find_by_credit_limit(50)
965
+ end
966
+
967
+ def test_find_by_one_attribute_that_is_an_aggregate
968
+ address = customers(:david).address
969
+ assert_kind_of Address, address
970
+ found_customer = Customer.find_by_address(address)
971
+ assert_equal customers(:david), found_customer
972
+ end
973
+
974
+ def test_find_by_one_attribute_that_is_an_aggregate_with_one_attribute_difference
975
+ address = customers(:david).address
976
+ assert_kind_of Address, address
977
+ missing_address = Address.new(address.street, address.city, address.country + "1")
978
+ assert_nil Customer.find_by_address(missing_address)
979
+ missing_address = Address.new(address.street, address.city + "1", address.country)
980
+ assert_nil Customer.find_by_address(missing_address)
981
+ missing_address = Address.new(address.street + "1", address.city, address.country)
982
+ assert_nil Customer.find_by_address(missing_address)
983
+ end
984
+
985
+ def test_find_by_two_attributes_that_are_both_aggregates
986
+ balance = customers(:david).balance
987
+ address = customers(:david).address
988
+ assert_kind_of Money, balance
989
+ assert_kind_of Address, address
990
+ found_customer = Customer.find_by_balance_and_address(balance, address)
991
+ assert_equal customers(:david), found_customer
992
+ end
993
+
994
+ def test_find_by_two_attributes_with_one_being_an_aggregate
995
+ balance = customers(:david).balance
996
+ assert_kind_of Money, balance
997
+ found_customer = Customer.find_by_balance_and_name(balance, customers(:david).name)
998
+ assert_equal customers(:david), found_customer
999
+ end
1000
+
1001
+ def test_dynamic_finder_on_one_attribute_with_conditions_returns_same_results_after_caching
1002
+ # ensure this test can run independently of order
1003
+ class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.include?(:find_by_credit_limit)
1004
+ a = Account.where('firm_id = ?', 6).find_by_credit_limit(50)
1005
+ assert_equal a, Account.where('firm_id = ?', 6).find_by_credit_limit(50) # find_by_credit_limit has been cached
1006
+ end
1007
+
1008
+ def test_find_by_one_attribute_with_several_options
1009
+ assert_equal accounts(:unknown), Account.order('id DESC').where('id != ?', 3).find_by_credit_limit(50)
1010
+ end
1011
+
1012
+ def test_find_by_one_missing_attribute
1013
+ assert_raise(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
1014
+ end
1015
+
1016
+ def test_find_by_invalid_method_syntax
1017
+ assert_raise(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
1018
+ assert_raise(NoMethodError) { Topic.find_by_title?("The First Topic") }
1019
+ assert_raise(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
1020
+ assert_raise(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
1021
+ end
1022
+
1023
+ def test_find_by_two_attributes
1024
+ assert_equal topics(:first), Topic.find_by_title_and_author_name("The First Topic", "David")
1025
+ assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")
1026
+ end
1027
+
1028
+ def test_find_by_two_attributes_but_passing_only_one
1029
+ assert_raise(ArgumentError) { Topic.find_by_title_and_author_name("The First Topic") }
1030
+ end
1031
+
1032
+ def test_find_last_with_offset
1033
+ puts "finder_test.test_find_last_with_offset"
1034
+ devs = Developer.order('id')
1035
+
1036
+ assert_equal devs[2], Developer.offset(2).first
1037
+ assert_equal devs[-3], Developer.offset(2).last
1038
+ assert_equal devs[-3], Developer.offset(2).last
1039
+ assert_equal devs[-3], Developer.offset(2).order('id DESC').first
1040
+ end
1041
+
1042
+ def test_find_by_nil_attribute
1043
+ topic = Topic.find_by_last_read nil
1044
+ assert_not_nil topic
1045
+ assert_nil topic.last_read
1046
+ end
1047
+
1048
+ def test_find_by_nil_and_not_nil_attributes
1049
+ topic = Topic.find_by_last_read_and_author_name nil, "Mary"
1050
+ assert_equal "Mary", topic.author_name
1051
+ end
1052
+
1053
+ def test_find_with_bad_sql
1054
+ assert_raise(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
1055
+ end
1056
+
1057
+ def test_find_all_with_join
1058
+ developers_on_project_one = Developer.
1059
+ joins('LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id').
1060
+ where('project_id=1').to_a
1061
+ assert_equal 3, developers_on_project_one.length
1062
+ developer_names = developers_on_project_one.map(&:name)
1063
+ assert developer_names.include?('David')
1064
+ assert developer_names.include?('Jamis')
1065
+ end
1066
+
1067
+ def test_joins_dont_clobber_id
1068
+ first = Firm.
1069
+ joins('INNER JOIN companies clients ON clients.firm_id = companies.id').
1070
+ where('companies.id = 1').first
1071
+ assert_equal 1, first.id
1072
+ end
1073
+
1074
+ def test_joins_with_string_array
1075
+ person_with_reader_and_post = Post.
1076
+ joins(["INNER JOIN categorizations ON categorizations.post_id = posts.id",
1077
+ "INNER JOIN categories ON categories.id = categorizations.category_id AND categories.type = 'SpecialCategory'"
1078
+ ])
1079
+ assert_equal 1, person_with_reader_and_post.size
1080
+ end
1081
+
1082
+ def test_find_by_id_with_conditions_with_or
1083
+ assert_nothing_raised do
1084
+ Post.where("posts.id <= 3 OR posts.#{QUOTED_TYPE} = 'Post'").find([1,2,3])
1085
+ end
1086
+ end
1087
+
1088
+ def test_find_ignores_previously_inserted_record
1089
+ puts "finder_test.test_find_ignores_previously_inserted_record"
1090
+ Post.create!(:title => 'test', :body => 'it out')
1091
+ assert_equal [], Post.where(id: nil)
1092
+ end
1093
+
1094
+ def test_find_by_empty_ids
1095
+ assert_equal [], Post.find([])
1096
+ end
1097
+
1098
+ def test_find_by_empty_in_condition
1099
+ assert_equal [], Post.where('id in (?)', [])
1100
+ end
1101
+
1102
+ def test_find_by_records
1103
+ puts "finder_test.test_find_by_records"
1104
+ p1, p2 = Post.limit(2).order('id asc').to_a
1105
+ assert_equal [p1, p2], Post.where(['id in (?)', [p1, p2]]).order('id asc')
1106
+ assert_equal [p1, p2], Post.where(['id in (?)', [p1, p2.id]]).order('id asc')
1107
+ end
1108
+
1109
+ def test_select_value
1110
+ assert_equal "37signals", Company.connection.select_value("SELECT name FROM companies WHERE id = 1")
1111
+ assert_nil Company.connection.select_value("SELECT name FROM companies WHERE id = -1")
1112
+ # make sure we didn't break count...
1113
+ assert_equal 0, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = 'Halliburton'")
1114
+ assert_equal 1, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = '37signals'")
1115
+ end
1116
+
1117
+ def test_select_values
1118
+ assert_equal ["1","2","3","4","5","6","7","8","9", "10", "11"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map!(&:to_s)
1119
+ assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy", "Ex Nihilo Part Deux", "Apex"], Company.connection.select_values("SELECT name FROM companies ORDER BY id")
1120
+ end
1121
+
1122
+ def test_select_rows
1123
+ assert_equal(
1124
+ [["1", "1", nil, "37signals"],
1125
+ ["2", "1", "2", "Summit"],
1126
+ ["3", "1", "1", "Microsoft"]],
1127
+ Company.connection.select_rows("SELECT id, firm_id, client_of, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}})
1128
+ assert_equal [["1", "37signals"], ["2", "Summit"], ["3", "Microsoft"]],
1129
+ Company.connection.select_rows("SELECT id, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}}
1130
+ end
1131
+
1132
+ def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
1133
+ puts "finder_test.test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct"
1134
+ assert_equal 2, Post.includes(authors: :author_address).
1135
+ where.not(author_addresses: { id: nil }).
1136
+ order('author_addresses.id DESC').limit(2).to_a.size
1137
+
1138
+ assert_equal 3, Post.includes(author: :author_address, authors: :author_address).
1139
+ where.not(author_addresses_authors: { id: nil }).
1140
+ order('author_addresses_authors.id DESC').limit(3).to_a.size
1141
+ end
1142
+
1143
+ def test_find_with_nil_inside_set_passed_for_one_attribute
1144
+ client_of = Company.
1145
+ where(client_of: [2, 1, nil],
1146
+ name: ['37signals', 'Summit', 'Microsoft']).
1147
+ order('client_of DESC').
1148
+ map(&:client_of)
1149
+
1150
+ assert client_of.include?(nil)
1151
+ assert_equal [2, 1].sort, client_of.compact.sort
1152
+ end
1153
+
1154
+ def test_find_with_nil_inside_set_passed_for_attribute
1155
+ client_of = Company.
1156
+ where(client_of: [nil]).
1157
+ order('client_of DESC').
1158
+ map(&:client_of)
1159
+
1160
+ assert_equal [], client_of.compact
1161
+ end
1162
+
1163
+ def test_with_limiting_with_custom_select
1164
+ puts "finder_test.test_with_limiting_with_custom_select"
1165
+ posts = Post.references(:authors).merge(
1166
+ :includes => :author, :select => 'posts.*, authors.id as "author_id"',
1167
+ :limit => 3, :order => 'posts.id'
1168
+ ).to_a
1169
+ assert_equal 3, posts.size
1170
+ assert_equal [0, 1, 1], posts.map(&:author_id).sort
1171
+ end
1172
+
1173
+ def test_find_one_message_on_primary_key
1174
+ e = assert_raises(ActiveRecord::RecordNotFound) do
1175
+ Car.find(0)
1176
+ end
1177
+ assert_equal 0, e.id
1178
+ assert_equal "id", e.primary_key
1179
+ assert_equal "Car", e.model
1180
+ assert_equal "Couldn't find Car with 'id'=0", e.message
1181
+ end
1182
+
1183
+ def test_find_one_message_with_custom_primary_key
1184
+ table_with_custom_primary_key do |model|
1185
+ model.primary_key = :name
1186
+ e = assert_raises(ActiveRecord::RecordNotFound) do
1187
+ model.find 'Hello World!'
1188
+ end
1189
+ assert_equal %Q{Couldn't find MercedesCar with 'name'=Hello World!}, e.message
1190
+ end
1191
+ end
1192
+
1193
+ def test_find_some_message_with_custom_primary_key
1194
+ table_with_custom_primary_key do |model|
1195
+ model.primary_key = :name
1196
+ e = assert_raises(ActiveRecord::RecordNotFound) do
1197
+ model.find 'Hello', 'World!'
1198
+ end
1199
+ assert_equal %Q{Couldn't find all MercedesCars with 'name': (Hello, World!) (found 0 results, but was looking for 2)}, e.message
1200
+ end
1201
+ end
1202
+
1203
+ def test_find_without_primary_key
1204
+ assert_raises(ActiveRecord::UnknownPrimaryKey) do
1205
+ Matey.find(1)
1206
+ end
1207
+ end
1208
+
1209
+ def test_finder_with_offset_string
1210
+ assert_nothing_raised { Topic.offset("3").to_a }
1211
+ end
1212
+
1213
+ test "find_by with hash conditions returns the first matching record" do
1214
+ assert_equal posts(:eager_other), Post.find_by(id: posts(:eager_other).id)
1215
+ end
1216
+
1217
+ test "find_by with non-hash conditions returns the first matching record" do
1218
+ assert_equal posts(:eager_other), Post.find_by("id = #{posts(:eager_other).id}")
1219
+ end
1220
+
1221
+ test "find_by with multi-arg conditions returns the first matching record" do
1222
+ assert_equal posts(:eager_other), Post.find_by('id = ?', posts(:eager_other).id)
1223
+ end
1224
+
1225
+ test "find_by returns nil if the record is missing" do
1226
+ assert_equal nil, Post.find_by("1 = 0")
1227
+ end
1228
+
1229
+ test "find_by with associations" do
1230
+ assert_equal authors(:david), Post.find_by(author: authors(:david)).author
1231
+ assert_equal authors(:mary) , Post.find_by(author: authors(:mary) ).author
1232
+ end
1233
+
1234
+ test "find_by doesn't have implicit ordering" do
1235
+ assert_sql(/^((?!ORDER).)*$/) { Post.find_by(id: posts(:eager_other).id) }
1236
+ end
1237
+
1238
+ test "find_by! with hash conditions returns the first matching record" do
1239
+ assert_equal posts(:eager_other), Post.find_by!(id: posts(:eager_other).id)
1240
+ end
1241
+
1242
+ test "find_by! with non-hash conditions returns the first matching record" do
1243
+ assert_equal posts(:eager_other), Post.find_by!("id = #{posts(:eager_other).id}")
1244
+ end
1245
+
1246
+ test "find_by! with multi-arg conditions returns the first matching record" do
1247
+ assert_equal posts(:eager_other), Post.find_by!('id = ?', posts(:eager_other).id)
1248
+ end
1249
+
1250
+ test "find_by! doesn't have implicit ordering" do
1251
+ assert_sql(/^((?!ORDER).)*$/) { Post.find_by!(id: posts(:eager_other).id) }
1252
+ end
1253
+
1254
+ test "find_by! raises RecordNotFound if the record is missing" do
1255
+ assert_raises(ActiveRecord::RecordNotFound) do
1256
+ Post.find_by!("1 = 0")
1257
+ end
1258
+ end
1259
+
1260
+ test "find on a scope does not perform statement caching" do
1261
+ honda = cars(:honda)
1262
+ zyke = cars(:zyke)
1263
+ tyre = honda.tyres.create!
1264
+ tyre2 = zyke.tyres.create!
1265
+
1266
+ assert_equal tyre, honda.tyres.custom_find(tyre.id)
1267
+ assert_equal tyre2, zyke.tyres.custom_find(tyre2.id)
1268
+ end
1269
+
1270
+ test "find_by on a scope does not perform statement caching" do
1271
+ honda = cars(:honda)
1272
+ zyke = cars(:zyke)
1273
+ tyre = honda.tyres.create!
1274
+ tyre2 = zyke.tyres.create!
1275
+
1276
+ assert_equal tyre, honda.tyres.custom_find_by(id: tyre.id)
1277
+ assert_equal tyre2, zyke.tyres.custom_find_by(id: tyre2.id)
1278
+ end
1279
+
1280
+ protected
1281
+ def table_with_custom_primary_key
1282
+ yield(Class.new(Toy) do
1283
+ def self.name
1284
+ 'MercedesCar'
1285
+ end
1286
+ end)
1287
+ end
1288
+
1289
+ def assert_raises_with_message(exception_class, message, &block)
1290
+ err = assert_raises(exception_class) { block.call }
1291
+ assert_match message, err.message
1292
+ end
1293
+
1294
+ end