activerecord-nuodb-adapter 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (456) hide show
  1. data/.DS_Store +0 -0
  2. data/.travis.yml +26 -2
  3. data/{CONTRIBUTION.rdoc → CONTRIBUTION.md} +16 -15
  4. data/Gemfile +1 -1
  5. data/README.md +48 -0
  6. data/active_setup_linux.sh +97 -0
  7. data/activerecord-nuodb-adapter.gemspec +1 -1
  8. data/lib/active_record/connection_adapters/nuodb/version.rb +1 -1
  9. data/lib/active_record/connection_adapters/nuodb_adapter.rb +45 -9
  10. data/test/.DS_Store +0 -0
  11. data/test/.gitignore +1 -0
  12. data/test/active_record/connection_adapters/fake_adapter.rb +45 -0
  13. data/test/ar_schema_test.rb +70 -0
  14. data/test/assets/example.log +1 -0
  15. data/test/assets/flowers.jpg +0 -0
  16. data/test/assets/test.txt +1 -0
  17. data/test/base_test.rb +1481 -0
  18. data/test/cases/.DS_Store +0 -0
  19. data/test/cases/adapter_test.rb +211 -0
  20. data/test/cases/adapters/firebird/connection_test.rb +8 -0
  21. data/test/cases/adapters/firebird/default_test.rb +16 -0
  22. data/test/cases/adapters/firebird/migration_test.rb +124 -0
  23. data/test/cases/adapters/mysql/active_schema_test.rb +143 -0
  24. data/test/cases/adapters/mysql/case_sensitivity_test.rb +35 -0
  25. data/test/cases/adapters/mysql/connection_test.rb +172 -0
  26. data/test/cases/adapters/mysql/enum_test.rb +10 -0
  27. data/test/cases/adapters/mysql/mysql_adapter_test.rb +125 -0
  28. data/test/cases/adapters/mysql/quoting_test.rb +25 -0
  29. data/test/cases/adapters/mysql/reserved_word_test.rb +153 -0
  30. data/test/cases/adapters/mysql/schema_test.rb +62 -0
  31. data/test/cases/adapters/mysql/sp_test.rb +15 -0
  32. data/test/cases/adapters/mysql/sql_types_test.rb +14 -0
  33. data/test/cases/adapters/mysql/statement_pool_test.rb +23 -0
  34. data/test/cases/adapters/mysql2/active_schema_test.rb +143 -0
  35. data/test/cases/adapters/mysql2/bind_parameter_test.rb +50 -0
  36. data/test/cases/adapters/mysql2/case_sensitivity_test.rb +35 -0
  37. data/test/cases/adapters/mysql2/connection_test.rb +97 -0
  38. data/test/cases/adapters/mysql2/enum_test.rb +10 -0
  39. data/test/cases/adapters/mysql2/explain_test.rb +26 -0
  40. data/test/cases/adapters/mysql2/reserved_word_test.rb +152 -0
  41. data/test/cases/adapters/mysql2/schema_migrations_test.rb +26 -0
  42. data/test/cases/adapters/mysql2/schema_test.rb +70 -0
  43. data/test/cases/adapters/mysql2/sql_types_test.rb +14 -0
  44. data/test/cases/adapters/oracle/synonym_test.rb +17 -0
  45. data/test/cases/adapters/postgresql/active_schema_test.rb +58 -0
  46. data/test/cases/adapters/postgresql/array_test.rb +104 -0
  47. data/test/cases/adapters/postgresql/bytea_test.rb +104 -0
  48. data/test/cases/adapters/postgresql/connection_test.rb +163 -0
  49. data/test/cases/adapters/postgresql/datatype_test.rb +604 -0
  50. data/test/cases/adapters/postgresql/explain_test.rb +28 -0
  51. data/test/cases/adapters/postgresql/hstore_test.rb +200 -0
  52. data/test/cases/adapters/postgresql/json_test.rb +99 -0
  53. data/test/cases/adapters/postgresql/ltree_test.rb +41 -0
  54. data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +288 -0
  55. data/test/cases/adapters/postgresql/quoting_test.rb +58 -0
  56. data/test/cases/adapters/postgresql/schema_authorization_test.rb +114 -0
  57. data/test/cases/adapters/postgresql/schema_test.rb +376 -0
  58. data/test/cases/adapters/postgresql/sql_types_test.rb +18 -0
  59. data/test/cases/adapters/postgresql/statement_pool_test.rb +39 -0
  60. data/test/cases/adapters/postgresql/timestamp_test.rb +113 -0
  61. data/test/cases/adapters/postgresql/utils_test.rb +20 -0
  62. data/test/cases/adapters/postgresql/uuid_test.rb +95 -0
  63. data/test/cases/adapters/postgresql/view_test.rb +49 -0
  64. data/test/cases/adapters/sqlite3/copy_table_test.rb +99 -0
  65. data/test/cases/adapters/sqlite3/explain_test.rb +26 -0
  66. data/test/cases/adapters/sqlite3/quoting_test.rb +102 -0
  67. data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +366 -0
  68. data/test/cases/adapters/sqlite3/statement_pool_test.rb +24 -0
  69. data/test/cases/aggregations_test.rb +158 -0
  70. data/test/cases/ar_schema_test.rb +70 -0
  71. data/test/cases/associations/association_scope_test.rb +15 -0
  72. data/test/cases/associations/belongs_to_associations_test.rb +831 -0
  73. data/test/cases/associations/callbacks_test.rb +168 -0
  74. data/test/cases/associations/cascaded_eager_loading_test.rb +179 -0
  75. data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +36 -0
  76. data/test/cases/associations/eager_load_nested_include_test.rb +128 -0
  77. data/test/cases/associations/eager_singularization_test.rb +145 -0
  78. data/test/cases/associations/eager_test.rb +1179 -0
  79. data/test/cases/associations/extension_test.rb +80 -0
  80. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +779 -0
  81. data/test/cases/associations/has_many_associations_test.rb +1699 -0
  82. data/test/cases/associations/has_many_through_associations_test.rb +950 -0
  83. data/test/cases/associations/has_one_associations_test.rb +525 -0
  84. data/test/cases/associations/has_one_through_associations_test.rb +317 -0
  85. data/test/cases/associations/inner_join_association_test.rb +115 -0
  86. data/test/cases/associations/inverse_associations_test.rb +631 -0
  87. data/test/cases/associations/join_dependency_test.rb +8 -0
  88. data/test/cases/associations/join_model_test.rb +750 -0
  89. data/test/cases/associations/nested_through_associations_test.rb +575 -0
  90. data/test/cases/associations_test.rb +344 -0
  91. data/test/cases/attribute_methods/read_test.rb +58 -0
  92. data/test/cases/attribute_methods/serialization_test.rb +29 -0
  93. data/test/cases/attribute_methods_test.rb +819 -0
  94. data/test/cases/autosave_association_test.rb +1447 -0
  95. data/test/cases/base_test.rb +1481 -0
  96. data/test/cases/batches_test.rb +173 -0
  97. data/test/cases/binary_test.rb +49 -0
  98. data/test/cases/bind_parameter_test.rb +89 -0
  99. data/test/cases/calculations_test.rb +589 -0
  100. data/test/cases/callbacks_test.rb +535 -0
  101. data/test/cases/clone_test.rb +40 -0
  102. data/test/cases/coders/yaml_column_test.rb +63 -0
  103. data/test/cases/column_alias_test.rb +17 -0
  104. data/test/cases/column_definition_test.rb +143 -0
  105. data/test/cases/column_test.rb +115 -0
  106. data/test/cases/connection_adapters/abstract_adapter_test.rb +62 -0
  107. data/test/cases/connection_adapters/connection_handler_test.rb +53 -0
  108. data/test/cases/connection_adapters/connection_specification_test.rb +12 -0
  109. data/test/cases/connection_adapters/quoting_test.rb +13 -0
  110. data/test/cases/connection_adapters/schema_cache_test.rb +56 -0
  111. data/test/cases/connection_management_test.rb +112 -0
  112. data/test/cases/connection_pool_test.rb +344 -0
  113. data/test/cases/connection_specification/resolver_test.rb +63 -0
  114. data/test/cases/core_test.rb +33 -0
  115. data/test/cases/counter_cache_test.rb +163 -0
  116. data/test/cases/custom_locking_test.rb +17 -0
  117. data/test/cases/database_statements_test.rb +19 -0
  118. data/test/cases/date_time_test.rb +43 -0
  119. data/test/cases/defaults_test.rb +214 -0
  120. data/test/cases/dirty_test.rb +626 -0
  121. data/test/cases/disconnected_test.rb +27 -0
  122. data/test/cases/dup_test.rb +136 -0
  123. data/test/cases/explain_subscriber_test.rb +59 -0
  124. data/test/cases/explain_test.rb +72 -0
  125. data/test/cases/finder_respond_to_test.rb +50 -0
  126. data/test/cases/finder_test.rb +886 -0
  127. data/test/cases/fixture_set/file_test.rb +83 -0
  128. data/test/cases/fixtures_test.rb +808 -0
  129. data/test/cases/forbidden_attributes_protection_test.rb +64 -0
  130. data/test/cases/habtm_destroy_order_test.rb +61 -0
  131. data/test/cases/helper.rb +151 -0
  132. data/test/cases/hot_compatibility_test.rb +54 -0
  133. data/test/cases/i18n_test.rb +45 -0
  134. data/test/cases/inheritance_test.rb +355 -0
  135. data/test/cases/integration_test.rb +87 -0
  136. data/test/cases/invalid_connection_test.rb +22 -0
  137. data/test/cases/invalid_date_test.rb +32 -0
  138. data/test/cases/invertible_migration_test.rb +259 -0
  139. data/test/cases/json_serialization_test.rb +300 -0
  140. data/test/cases/locking_test.rb +453 -0
  141. data/test/cases/log_subscriber_test.rb +129 -0
  142. data/test/cases/migration/change_schema_test.rb +379 -0
  143. data/test/cases/migration/change_table_test.rb +204 -0
  144. data/test/cases/migration/column_attributes_test.rb +186 -0
  145. data/test/cases/migration/column_positioning_test.rb +60 -0
  146. data/test/cases/migration/columns_test.rb +279 -0
  147. data/test/cases/migration/command_recorder_test.rb +247 -0
  148. data/test/cases/migration/create_join_table_test.rb +125 -0
  149. data/test/cases/migration/helper.rb +43 -0
  150. data/test/cases/migration/index_test.rb +209 -0
  151. data/test/cases/migration/logger_test.rb +37 -0
  152. data/test/cases/migration/references_index_test.rb +102 -0
  153. data/test/cases/migration/references_statements_test.rb +111 -0
  154. data/test/cases/migration/rename_table_test.rb +90 -0
  155. data/test/cases/migration/table_and_index_test.rb +24 -0
  156. data/test/cases/migration_test.rb +861 -0
  157. data/test/cases/migrator_test.rb +378 -0
  158. data/test/cases/mixin_test.rb +96 -0
  159. data/test/cases/modules_test.rb +143 -0
  160. data/test/cases/multiparameter_attributes_test.rb +350 -0
  161. data/test/cases/multiple_db_test.rb +108 -0
  162. data/test/cases/nested_attributes_test.rb +1056 -0
  163. data/test/cases/persistence_test.rb +802 -0
  164. data/test/cases/pooled_connections_test.rb +51 -0
  165. data/test/cases/primary_keys_test.rb +218 -0
  166. data/test/cases/query_cache_test.rb +276 -0
  167. data/test/cases/quoting_test.rb +229 -0
  168. data/test/cases/readonly_test.rb +111 -0
  169. data/test/cases/reaper_test.rb +81 -0
  170. data/test/cases/reflection_test.rb +386 -0
  171. data/test/cases/relation/where_chain_test.rb +80 -0
  172. data/test/cases/relation/where_test.rb +120 -0
  173. data/test/cases/relation_test.rb +315 -0
  174. data/test/cases/relations_test.rb +1559 -0
  175. data/test/cases/reload_models_test.rb +22 -0
  176. data/test/cases/result_test.rb +32 -0
  177. data/test/cases/sanitize_test.rb +34 -0
  178. data/test/cases/schema_dumper_test.rb +391 -0
  179. data/test/cases/scoping/default_scoping_test.rb +374 -0
  180. data/test/cases/scoping/named_scoping_test.rb +451 -0
  181. data/test/cases/scoping/relation_scoping_test.rb +331 -0
  182. data/test/cases/serialization_test.rb +68 -0
  183. data/test/cases/serialized_attribute_test.rb +252 -0
  184. data/test/cases/statement_cache_test.rb +64 -0
  185. data/test/cases/store_test.rb +153 -0
  186. data/test/cases/tasks/database_tasks_test.rb +315 -0
  187. data/test/cases/tasks/mysql_rake_test.rb +301 -0
  188. data/test/cases/tasks/postgresql_rake_test.rb +236 -0
  189. data/test/cases/tasks/sqlite_rake_test.rb +191 -0
  190. data/test/cases/test_case.rb +95 -0
  191. data/test/cases/test_simple.rb +194 -0
  192. data/test/cases/timestamp_test.rb +308 -0
  193. data/test/cases/transaction_callbacks_test.rb +317 -0
  194. data/test/cases/transaction_isolation_test.rb +114 -0
  195. data/test/cases/transactions_test.rb +595 -0
  196. data/test/cases/unconnected_test.rb +33 -0
  197. data/test/cases/validations/association_validation_test.rb +121 -0
  198. data/test/cases/validations/i18n_generate_message_validation_test.rb +76 -0
  199. data/test/cases/validations/i18n_validation_test.rb +89 -0
  200. data/test/cases/validations/presence_validation_test.rb +51 -0
  201. data/test/cases/validations/uniqueness_validation_test.rb +379 -0
  202. data/test/cases/validations_repair_helper.rb +23 -0
  203. data/test/cases/validations_test.rb +124 -0
  204. data/test/cases/xml_serialization_test.rb +455 -0
  205. data/test/cases/yaml_serialization_test.rb +52 -0
  206. data/test/config.example.yml +154 -0
  207. data/test/config.rb +5 -0
  208. data/test/database_statements_test.rb +19 -0
  209. data/test/fixtures/.gitignore +1 -0
  210. data/test/fixtures/accounts.yml +29 -0
  211. data/test/fixtures/admin/accounts.yml +2 -0
  212. data/test/fixtures/admin/randomly_named_a9.yml +7 -0
  213. data/test/fixtures/admin/randomly_named_b0.yml +7 -0
  214. data/test/fixtures/admin/users.yml +10 -0
  215. data/test/fixtures/all/developers.yml +0 -0
  216. data/test/fixtures/all/people.yml +0 -0
  217. data/test/fixtures/all/tasks.yml +0 -0
  218. data/test/fixtures/author_addresses.yml +5 -0
  219. data/test/fixtures/author_favorites.yml +4 -0
  220. data/test/fixtures/authors.yml +15 -0
  221. data/test/fixtures/binaries.yml +133 -0
  222. data/test/fixtures/books.yml +9 -0
  223. data/test/fixtures/cars.yml +9 -0
  224. data/test/fixtures/categories.yml +19 -0
  225. data/test/fixtures/categories/special_categories.yml +9 -0
  226. data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
  227. data/test/fixtures/categories_ordered.yml +7 -0
  228. data/test/fixtures/categories_posts.yml +31 -0
  229. data/test/fixtures/categorizations.yml +23 -0
  230. data/test/fixtures/clubs.yml +8 -0
  231. data/test/fixtures/collections.yml +3 -0
  232. data/test/fixtures/colleges.yml +3 -0
  233. data/test/fixtures/comments.yml +65 -0
  234. data/test/fixtures/companies.yml +59 -0
  235. data/test/fixtures/computers.yml +4 -0
  236. data/test/fixtures/courses.yml +8 -0
  237. data/test/fixtures/customers.yml +26 -0
  238. data/test/fixtures/dashboards.yml +6 -0
  239. data/test/fixtures/developers.yml +21 -0
  240. data/test/fixtures/developers_projects.yml +17 -0
  241. data/test/fixtures/dog_lovers.yml +7 -0
  242. data/test/fixtures/dogs.yml +4 -0
  243. data/test/fixtures/edges.yml +5 -0
  244. data/test/fixtures/entrants.yml +14 -0
  245. data/test/fixtures/essays.yml +6 -0
  246. data/test/fixtures/faces.yml +11 -0
  247. data/test/fixtures/fk_test_has_fk.yml +3 -0
  248. data/test/fixtures/fk_test_has_pk.yml +2 -0
  249. data/test/fixtures/friendships.yml +4 -0
  250. data/test/fixtures/funny_jokes.yml +10 -0
  251. data/test/fixtures/interests.yml +33 -0
  252. data/test/fixtures/items.yml +3 -0
  253. data/test/fixtures/jobs.yml +7 -0
  254. data/test/fixtures/legacy_things.yml +3 -0
  255. data/test/fixtures/mateys.yml +4 -0
  256. data/test/fixtures/member_details.yml +8 -0
  257. data/test/fixtures/member_types.yml +6 -0
  258. data/test/fixtures/members.yml +11 -0
  259. data/test/fixtures/memberships.yml +34 -0
  260. data/test/fixtures/men.yml +5 -0
  261. data/test/fixtures/minimalistics.yml +2 -0
  262. data/test/fixtures/minivans.yml +5 -0
  263. data/test/fixtures/mixed_case_monkeys.yml +6 -0
  264. data/test/fixtures/mixins.yml +29 -0
  265. data/test/fixtures/movies.yml +7 -0
  266. data/test/fixtures/naked/csv/accounts.csv +1 -0
  267. data/test/fixtures/naked/yml/accounts.yml +1 -0
  268. data/test/fixtures/naked/yml/companies.yml +1 -0
  269. data/test/fixtures/naked/yml/courses.yml +1 -0
  270. data/test/fixtures/organizations.yml +5 -0
  271. data/test/fixtures/other_topics.yml +42 -0
  272. data/test/fixtures/owners.yml +8 -0
  273. data/test/fixtures/parrots.yml +27 -0
  274. data/test/fixtures/parrots_pirates.yml +7 -0
  275. data/test/fixtures/people.yml +24 -0
  276. data/test/fixtures/peoples_treasures.yml +3 -0
  277. data/test/fixtures/pets.yml +19 -0
  278. data/test/fixtures/pirates.yml +9 -0
  279. data/test/fixtures/posts.yml +82 -0
  280. data/test/fixtures/price_estimates.yml +7 -0
  281. data/test/fixtures/products.yml +4 -0
  282. data/test/fixtures/projects.yml +7 -0
  283. data/test/fixtures/randomly_named_a9.yml +7 -0
  284. data/test/fixtures/ratings.yml +14 -0
  285. data/test/fixtures/readers.yml +11 -0
  286. data/test/fixtures/references.yml +17 -0
  287. data/test/fixtures/reserved_words/distinct.yml +5 -0
  288. data/test/fixtures/reserved_words/distinct_select.yml +11 -0
  289. data/test/fixtures/reserved_words/group.yml +14 -0
  290. data/test/fixtures/reserved_words/select.yml +8 -0
  291. data/test/fixtures/reserved_words/values.yml +7 -0
  292. data/test/fixtures/ships.yml +6 -0
  293. data/test/fixtures/speedometers.yml +8 -0
  294. data/test/fixtures/sponsors.yml +12 -0
  295. data/test/fixtures/string_key_objects.yml +7 -0
  296. data/test/fixtures/subscribers.yml +11 -0
  297. data/test/fixtures/subscriptions.yml +12 -0
  298. data/test/fixtures/taggings.yml +78 -0
  299. data/test/fixtures/tags.yml +11 -0
  300. data/test/fixtures/tasks.yml +7 -0
  301. data/test/fixtures/teapots.yml +3 -0
  302. data/test/fixtures/topics.yml +42 -0
  303. data/test/fixtures/toys.yml +14 -0
  304. data/test/fixtures/traffic_lights.yml +10 -0
  305. data/test/fixtures/treasures.yml +10 -0
  306. data/test/fixtures/variants.yml +4 -0
  307. data/test/fixtures/vegetables.yml +20 -0
  308. data/test/fixtures/vertices.yml +4 -0
  309. data/test/fixtures/warehouse-things.yml +3 -0
  310. data/test/fixtures/zines.yml +5 -0
  311. data/test/migration_test.rb +861 -0
  312. data/test/migrations/10_urban/9_add_expressions.rb +11 -0
  313. data/test/migrations/decimal/1_give_me_big_numbers.rb +15 -0
  314. data/test/migrations/empty/.gitkeep +0 -0
  315. data/test/migrations/magic/1_currencies_have_symbols.rb +12 -0
  316. data/test/migrations/missing/1000_people_have_middle_names.rb +9 -0
  317. data/test/migrations/missing/1_people_have_last_names.rb +9 -0
  318. data/test/migrations/missing/3_we_need_reminders.rb +12 -0
  319. data/test/migrations/missing/4_innocent_jointable.rb +12 -0
  320. data/test/migrations/rename/1_we_need_things.rb +11 -0
  321. data/test/migrations/rename/2_rename_things.rb +9 -0
  322. data/test/migrations/to_copy/1_people_have_hobbies.rb +9 -0
  323. data/test/migrations/to_copy/2_people_have_descriptions.rb +9 -0
  324. data/test/migrations/to_copy2/1_create_articles.rb +7 -0
  325. data/test/migrations/to_copy2/2_create_comments.rb +7 -0
  326. data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +9 -0
  327. data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +9 -0
  328. data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +9 -0
  329. data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +7 -0
  330. data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +7 -0
  331. data/test/migrations/valid/1_valid_people_have_last_names.rb +9 -0
  332. data/test/migrations/valid/2_we_need_reminders.rb +12 -0
  333. data/test/migrations/valid/3_innocent_jointable.rb +12 -0
  334. data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +9 -0
  335. data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +12 -0
  336. data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +12 -0
  337. data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +9 -0
  338. data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +12 -0
  339. data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +12 -0
  340. data/test/models/admin.rb +5 -0
  341. data/test/models/admin/account.rb +3 -0
  342. data/test/models/admin/randomly_named_c1.rb +3 -0
  343. data/test/models/admin/user.rb +39 -0
  344. data/test/models/aircraft.rb +4 -0
  345. data/test/models/arunit2_model.rb +3 -0
  346. data/test/models/author.rb +196 -0
  347. data/test/models/auto_id.rb +4 -0
  348. data/test/models/autoloadable/extra_firm.rb +2 -0
  349. data/test/models/binary.rb +2 -0
  350. data/test/models/bird.rb +12 -0
  351. data/test/models/book.rb +9 -0
  352. data/test/models/boolean.rb +2 -0
  353. data/test/models/bulb.rb +45 -0
  354. data/test/models/car.rb +28 -0
  355. data/test/models/categorization.rb +19 -0
  356. data/test/models/category.rb +34 -0
  357. data/test/models/citation.rb +6 -0
  358. data/test/models/club.rb +15 -0
  359. data/test/models/college.rb +5 -0
  360. data/test/models/column_name.rb +3 -0
  361. data/test/models/comment.rb +38 -0
  362. data/test/models/company.rb +214 -0
  363. data/test/models/company_in_module.rb +80 -0
  364. data/test/models/computer.rb +3 -0
  365. data/test/models/contact.rb +40 -0
  366. data/test/models/contract.rb +19 -0
  367. data/test/models/country.rb +7 -0
  368. data/test/models/course.rb +6 -0
  369. data/test/models/customer.rb +77 -0
  370. data/test/models/dashboard.rb +3 -0
  371. data/test/models/default.rb +2 -0
  372. data/test/models/developer.rb +250 -0
  373. data/test/models/dog.rb +5 -0
  374. data/test/models/dog_lover.rb +5 -0
  375. data/test/models/edge.rb +5 -0
  376. data/test/models/electron.rb +3 -0
  377. data/test/models/engine.rb +4 -0
  378. data/test/models/entrant.rb +3 -0
  379. data/test/models/essay.rb +5 -0
  380. data/test/models/event.rb +3 -0
  381. data/test/models/eye.rb +37 -0
  382. data/test/models/face.rb +7 -0
  383. data/test/models/friendship.rb +6 -0
  384. data/test/models/guid.rb +2 -0
  385. data/test/models/interest.rb +5 -0
  386. data/test/models/invoice.rb +4 -0
  387. data/test/models/item.rb +7 -0
  388. data/test/models/job.rb +7 -0
  389. data/test/models/joke.rb +7 -0
  390. data/test/models/keyboard.rb +3 -0
  391. data/test/models/legacy_thing.rb +3 -0
  392. data/test/models/lesson.rb +11 -0
  393. data/test/models/line_item.rb +3 -0
  394. data/test/models/liquid.rb +4 -0
  395. data/test/models/man.rb +9 -0
  396. data/test/models/matey.rb +4 -0
  397. data/test/models/member.rb +36 -0
  398. data/test/models/member_detail.rb +7 -0
  399. data/test/models/member_type.rb +3 -0
  400. data/test/models/membership.rb +15 -0
  401. data/test/models/minimalistic.rb +2 -0
  402. data/test/models/minivan.rb +9 -0
  403. data/test/models/mixed_case_monkey.rb +3 -0
  404. data/test/models/molecule.rb +4 -0
  405. data/test/models/movie.rb +5 -0
  406. data/test/models/order.rb +4 -0
  407. data/test/models/organization.rb +12 -0
  408. data/test/models/owner.rb +5 -0
  409. data/test/models/parrot.rb +29 -0
  410. data/test/models/person.rb +128 -0
  411. data/test/models/pet.rb +15 -0
  412. data/test/models/pirate.rb +86 -0
  413. data/test/models/possession.rb +3 -0
  414. data/test/models/post.rb +194 -0
  415. data/test/models/price_estimate.rb +4 -0
  416. data/test/models/project.rb +30 -0
  417. data/test/models/randomly_named_c1.rb +3 -0
  418. data/test/models/rating.rb +4 -0
  419. data/test/models/reader.rb +21 -0
  420. data/test/models/reference.rb +22 -0
  421. data/test/models/reply.rb +61 -0
  422. data/test/models/ship.rb +19 -0
  423. data/test/models/ship_part.rb +7 -0
  424. data/test/models/shop.rb +12 -0
  425. data/test/models/speedometer.rb +6 -0
  426. data/test/models/sponsor.rb +7 -0
  427. data/test/models/string_key_object.rb +3 -0
  428. data/test/models/student.rb +3 -0
  429. data/test/models/subject.rb +16 -0
  430. data/test/models/subscriber.rb +8 -0
  431. data/test/models/subscription.rb +4 -0
  432. data/test/models/tag.rb +7 -0
  433. data/test/models/tagging.rb +13 -0
  434. data/test/models/task.rb +5 -0
  435. data/test/models/topic.rb +121 -0
  436. data/test/models/toy.rb +6 -0
  437. data/test/models/traffic_light.rb +4 -0
  438. data/test/models/treasure.rb +11 -0
  439. data/test/models/treaty.rb +7 -0
  440. data/test/models/tyre.rb +3 -0
  441. data/test/models/vegetables.rb +24 -0
  442. data/test/models/vertex.rb +9 -0
  443. data/test/models/warehouse_thing.rb +5 -0
  444. data/test/models/wheel.rb +3 -0
  445. data/test/models/without_table.rb +3 -0
  446. data/test/models/zine.rb +3 -0
  447. data/test/schema/mysql2_specific_schema.rb +58 -0
  448. data/test/schema/mysql_specific_schema.rb +70 -0
  449. data/test/schema/oracle_specific_schema.rb +46 -0
  450. data/test/schema/postgresql_specific_schema.rb +224 -0
  451. data/test/schema/schema.rb +809 -0
  452. data/test/schema/sqlite_specific_schema.rb +25 -0
  453. data/test/support/config.rb +43 -0
  454. data/test/support/connection.rb +21 -0
  455. metadata +898 -6
  456. data/README.rdoc +0 -42
@@ -0,0 +1,27 @@
1
+ require "cases/helper"
2
+
3
+ class TestRecord < ActiveRecord::Base
4
+ end
5
+
6
+ class TestDisconnectedAdapter < ActiveRecord::TestCase
7
+ self.use_transactional_fixtures = false
8
+
9
+ def setup
10
+ skip "in-memory database mustn't disconnect" if in_memory_db?
11
+ @connection = ActiveRecord::Base.connection
12
+ end
13
+
14
+ def teardown
15
+ return if in_memory_db?
16
+ spec = ActiveRecord::Base.connection_config
17
+ ActiveRecord::Base.establish_connection(spec)
18
+ end
19
+
20
+ test "can't execute statements while disconnected" do
21
+ @connection.execute "SELECT count(*) from products"
22
+ @connection.disconnect!
23
+ assert_raises(ActiveRecord::StatementInvalid) do
24
+ @connection.execute "SELECT count(*) from products"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,136 @@
1
+ require "cases/helper"
2
+ require 'models/topic'
3
+
4
+ module ActiveRecord
5
+ class DupTest < ActiveRecord::TestCase
6
+ fixtures :topics
7
+
8
+ def test_dup
9
+ assert !Topic.new.freeze.dup.frozen?
10
+ end
11
+
12
+ def test_not_readonly
13
+ topic = Topic.first
14
+
15
+ duped = topic.dup
16
+ assert !duped.readonly?, 'should not be readonly'
17
+ end
18
+
19
+ def test_is_readonly
20
+ topic = Topic.first
21
+ topic.readonly!
22
+
23
+ duped = topic.dup
24
+ assert duped.readonly?, 'should be readonly'
25
+ end
26
+
27
+ def test_dup_not_persisted
28
+ topic = Topic.first
29
+ duped = topic.dup
30
+
31
+ assert !duped.persisted?, 'topic not persisted'
32
+ assert duped.new_record?, 'topic is new'
33
+ end
34
+
35
+ def test_dup_has_no_id
36
+ topic = Topic.first
37
+ duped = topic.dup
38
+ assert_nil duped.id
39
+ end
40
+
41
+ def test_dup_with_modified_attributes
42
+ topic = Topic.first
43
+ topic.author_name = 'Aaron'
44
+ duped = topic.dup
45
+ assert_equal 'Aaron', duped.author_name
46
+ end
47
+
48
+ def test_dup_with_changes
49
+ dbtopic = Topic.first
50
+ topic = Topic.new
51
+
52
+ topic.attributes = dbtopic.attributes.except("id")
53
+
54
+ #duped has no timestamp values
55
+ duped = dbtopic.dup
56
+
57
+ #clear topic timestamp values
58
+ topic.send(:clear_timestamp_attributes)
59
+
60
+ assert_equal topic.changes, duped.changes
61
+ end
62
+
63
+ def test_dup_topics_are_independent
64
+ topic = Topic.first
65
+ topic.author_name = 'Aaron'
66
+ duped = topic.dup
67
+
68
+ duped.author_name = 'meow'
69
+
70
+ assert_not_equal topic.changes, duped.changes
71
+ end
72
+
73
+ def test_dup_attributes_are_independent
74
+ topic = Topic.first
75
+ duped = topic.dup
76
+
77
+ duped.author_name = 'meow'
78
+ topic.author_name = 'Aaron'
79
+
80
+ assert_equal 'Aaron', topic.author_name
81
+ assert_equal 'meow', duped.author_name
82
+ end
83
+
84
+ def test_dup_timestamps_are_cleared
85
+ topic = Topic.first
86
+ assert_not_nil topic.updated_at
87
+ assert_not_nil topic.created_at
88
+
89
+ # temporary change to the topic object
90
+ topic.updated_at -= 3.days
91
+
92
+ #dup should not preserve the timestamps if present
93
+ new_topic = topic.dup
94
+ assert_nil new_topic.updated_at
95
+ assert_nil new_topic.created_at
96
+
97
+ new_topic.save
98
+ assert_not_nil new_topic.updated_at
99
+ assert_not_nil new_topic.created_at
100
+ end
101
+
102
+ def test_dup_after_initialize_callbacks
103
+ topic = Topic.new
104
+ assert Topic.after_initialize_called
105
+ Topic.after_initialize_called = false
106
+ topic.dup
107
+ assert Topic.after_initialize_called
108
+ end
109
+
110
+ def test_dup_validity_is_independent
111
+ repair_validations(Topic) do
112
+ Topic.validates_presence_of :title
113
+ topic = Topic.new("title" => "Literature")
114
+ topic.valid?
115
+
116
+ duped = topic.dup
117
+ duped.title = nil
118
+ assert duped.invalid?
119
+
120
+ topic.title = nil
121
+ duped.title = 'Mathematics'
122
+ assert topic.invalid?
123
+ assert duped.valid?
124
+ end
125
+ end
126
+
127
+ def test_dup_with_default_scope
128
+ prev_default_scopes = Topic.default_scopes
129
+ Topic.default_scopes = [Topic.where(:approved => true)]
130
+ topic = Topic.new(:approved => false)
131
+ assert !topic.dup.approved?, "should not be overridden by default scopes"
132
+ ensure
133
+ Topic.default_scopes = prev_default_scopes
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,59 @@
1
+ require 'cases/helper'
2
+ require 'active_record/explain_subscriber'
3
+ require 'active_record/explain_registry'
4
+
5
+ if ActiveRecord::Base.connection.supports_explain?
6
+ class ExplainSubscriberTest < ActiveRecord::TestCase
7
+ SUBSCRIBER = ActiveRecord::ExplainSubscriber.new
8
+
9
+ def setup
10
+ ActiveRecord::ExplainRegistry.reset
11
+ ActiveRecord::ExplainRegistry.collect = true
12
+ end
13
+
14
+ def test_collects_nothing_if_the_payload_has_an_exception
15
+ SUBSCRIBER.finish(nil, nil, exception: Exception.new)
16
+ assert queries.empty?
17
+ end
18
+
19
+ def test_collects_nothing_for_ignored_payloads
20
+ ActiveRecord::ExplainSubscriber::IGNORED_PAYLOADS.each do |ip|
21
+ SUBSCRIBER.finish(nil, nil, name: ip)
22
+ end
23
+ assert queries.empty?
24
+ end
25
+
26
+ def test_collects_nothing_if_collect_is_false
27
+ ActiveRecord::ExplainRegistry.collect = false
28
+ SUBSCRIBER.finish(nil, nil, name: 'SQL', sql: 'select 1 from users', binds: [1, 2])
29
+ assert queries.empty?
30
+ end
31
+
32
+ def test_collects_pairs_of_queries_and_binds
33
+ sql = 'select 1 from users'
34
+ binds = [1, 2]
35
+ SUBSCRIBER.finish(nil, nil, name: 'SQL', sql: sql, binds: binds)
36
+ assert_equal 1, queries.size
37
+ assert_equal sql, queries[0][0]
38
+ assert_equal binds, queries[0][1]
39
+ end
40
+
41
+ def test_collects_nothing_if_the_statement_is_not_whitelisted
42
+ SUBSCRIBER.finish(nil, nil, name: 'SQL', sql: 'SHOW max_identifier_length')
43
+ assert queries.empty?
44
+ end
45
+
46
+ def test_collects_nothing_if_the_statement_is_only_partially_matched
47
+ SUBSCRIBER.finish(nil, nil, name: 'SQL', sql: 'select_db yo_mama')
48
+ assert queries.empty?
49
+ end
50
+
51
+ def teardown
52
+ ActiveRecord::ExplainRegistry.reset
53
+ end
54
+
55
+ def queries
56
+ ActiveRecord::ExplainRegistry.queries
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,72 @@
1
+ require 'cases/helper'
2
+ require 'models/car'
3
+ require 'active_support/core_ext/string/strip'
4
+
5
+ if ActiveRecord::Base.connection.supports_explain?
6
+ class ExplainTest < ActiveRecord::TestCase
7
+ fixtures :cars
8
+
9
+ def base
10
+ ActiveRecord::Base
11
+ end
12
+
13
+ def connection
14
+ base.connection
15
+ end
16
+
17
+ def test_relation_explain
18
+ message = Car.where(:name => 'honda').explain
19
+ assert_match(/^EXPLAIN for:/, message)
20
+ end
21
+
22
+ def test_collecting_queries_for_explain
23
+ queries = ActiveRecord::Base.collecting_queries_for_explain do
24
+ Car.where(:name => 'honda').to_a
25
+ end
26
+
27
+ sql, binds = queries[0]
28
+ assert_match "SELECT", sql
29
+ assert_match "honda", sql
30
+ assert_equal [], binds
31
+ end
32
+
33
+ def test_exec_explain_with_no_binds
34
+ sqls = %w(foo bar)
35
+ binds = [[], []]
36
+ queries = sqls.zip(binds)
37
+
38
+ connection.stubs(:explain).returns('query plan foo', 'query plan bar')
39
+ expected = sqls.map {|sql| "EXPLAIN for: #{sql}\nquery plan #{sql}"}.join("\n")
40
+ assert_equal expected, base.exec_explain(queries)
41
+ end
42
+
43
+ def test_exec_explain_with_binds
44
+ cols = [Object.new, Object.new]
45
+ cols[0].expects(:name).returns('wadus')
46
+ cols[1].expects(:name).returns('chaflan')
47
+
48
+ sqls = %w(foo bar)
49
+ binds = [[[cols[0], 1]], [[cols[1], 2]]]
50
+ queries = sqls.zip(binds)
51
+
52
+ connection.stubs(:explain).returns("query plan foo\n", "query plan bar\n")
53
+ expected = <<-SQL.strip_heredoc
54
+ EXPLAIN for: #{sqls[0]} [["wadus", 1]]
55
+ query plan foo
56
+
57
+ EXPLAIN for: #{sqls[1]} [["chaflan", 2]]
58
+ query plan bar
59
+ SQL
60
+ assert_equal expected, base.exec_explain(queries)
61
+ end
62
+
63
+ def test_unsupported_connection_adapter
64
+ connection.stubs(:supports_explain?).returns(false)
65
+
66
+ base.logger.expects(:warn).never
67
+
68
+ Car.where(:name => 'honda').to_a
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,50 @@
1
+ require "cases/helper"
2
+ require 'models/topic'
3
+
4
+ class FinderRespondToTest < ActiveRecord::TestCase
5
+
6
+ fixtures :topics
7
+
8
+ def test_should_preserve_normal_respond_to_behaviour_and_respond_to_newly_added_method
9
+ class << Topic; self; end.send(:define_method, :method_added_for_finder_respond_to_test) { }
10
+ assert_respond_to Topic, :method_added_for_finder_respond_to_test
11
+ ensure
12
+ class << Topic; self; end.send(:remove_method, :method_added_for_finder_respond_to_test)
13
+ end
14
+
15
+ def test_should_preserve_normal_respond_to_behaviour_and_respond_to_standard_object_method
16
+ assert_respond_to Topic, :to_s
17
+ end
18
+
19
+ def test_should_respond_to_find_by_one_attribute_before_caching
20
+ ensure_topic_method_is_not_cached(:find_by_title)
21
+ assert_respond_to Topic, :find_by_title
22
+ end
23
+
24
+ def test_should_respond_to_find_by_two_attributes
25
+ ensure_topic_method_is_not_cached(:find_by_title_and_author_name)
26
+ assert_respond_to Topic, :find_by_title_and_author_name
27
+ end
28
+
29
+ def test_should_respond_to_find_all_by_an_aliased_attribute
30
+ ensure_topic_method_is_not_cached(:find_by_heading)
31
+ assert_respond_to Topic, :find_by_heading
32
+ end
33
+
34
+ def test_should_not_respond_to_find_by_one_missing_attribute
35
+ assert !Topic.respond_to?(:find_by_undertitle)
36
+ end
37
+
38
+ def test_should_not_respond_to_find_by_invalid_method_syntax
39
+ assert !Topic.respond_to?(:fail_to_find_by_title)
40
+ assert !Topic.respond_to?(:find_by_title?)
41
+ assert !Topic.respond_to?(:fail_to_find_or_create_by_title)
42
+ assert !Topic.respond_to?(:find_or_create_by_title?)
43
+ end
44
+
45
+ private
46
+
47
+ def ensure_topic_method_is_not_cached(method_id)
48
+ class << Topic; self; end.send(:remove_method, method_id) if Topic.public_methods.include? method_id
49
+ end
50
+ end
@@ -0,0 +1,886 @@
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/topic'
8
+ require 'models/reply'
9
+ require 'models/entrant'
10
+ require 'models/project'
11
+ require 'models/developer'
12
+ require 'models/customer'
13
+ require 'models/toy'
14
+
15
+ class FinderTest < ActiveRecord::TestCase
16
+ fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers, :categories, :categorizations
17
+
18
+ def test_find_by_id_with_hash
19
+ assert_raises(ActiveRecord::StatementInvalid) do
20
+ Post.find_by_id(:limit => 1)
21
+ end
22
+ end
23
+
24
+ def test_find_by_title_and_id_with_hash
25
+ assert_raises(ActiveRecord::StatementInvalid) do
26
+ Post.find_by_title_and_id('foo', :limit => 1)
27
+ end
28
+ end
29
+
30
+ def test_find
31
+ assert_equal(topics(:first).title, Topic.find(1).title)
32
+ end
33
+
34
+ def test_symbols_table_ref
35
+ Post.first # warm up
36
+ x = Symbol.all_symbols.count
37
+ Post.where("title" => {"xxxqqqq" => "bar"})
38
+ assert_equal x, Symbol.all_symbols.count
39
+ end
40
+
41
+ # find should handle strings that come from URLs
42
+ # (example: Category.find(params[:id]))
43
+ def test_find_with_string
44
+ assert_equal(Topic.find(1).title,Topic.find("1").title)
45
+ end
46
+
47
+ def test_exists
48
+ assert Topic.exists?(1)
49
+ assert Topic.exists?("1")
50
+ assert Topic.exists?(title: "The First Topic")
51
+ assert Topic.exists?(heading: "The First Topic")
52
+ assert Topic.exists?(:author_name => "Mary", :approved => true)
53
+ assert Topic.exists?(["parent_id = ?", 1])
54
+ assert !Topic.exists?(45)
55
+ assert !Topic.exists?(Topic.new)
56
+
57
+ begin
58
+ assert !Topic.exists?("foo")
59
+ rescue ActiveRecord::StatementInvalid
60
+ # PostgreSQL complains about string comparison with integer field
61
+ rescue Exception
62
+ flunk
63
+ end
64
+
65
+ assert_raise(NoMethodError) { Topic.exists?([1,2]) }
66
+ end
67
+
68
+ def test_exists_does_not_select_columns_without_alias
69
+ assert_sql(/SELECT\W+1 AS one FROM ["`]topics["`]/i) do
70
+ Topic.exists?
71
+ end
72
+ end
73
+
74
+ def test_exists_returns_true_with_one_record_and_no_args
75
+ assert Topic.exists?
76
+ end
77
+
78
+ def test_exists_returns_false_with_false_arg
79
+ assert !Topic.exists?(false)
80
+ end
81
+
82
+ # exists? should handle nil for id's that come from URLs and always return false
83
+ # (example: Topic.exists?(params[:id])) where params[:id] is nil
84
+ def test_exists_with_nil_arg
85
+ assert !Topic.exists?(nil)
86
+ assert Topic.exists?
87
+ assert !Topic.first.replies.exists?(nil)
88
+ assert Topic.first.replies.exists?
89
+ end
90
+
91
+ # ensures +exists?+ runs valid SQL by excluding order value
92
+ def test_exists_with_order
93
+ assert Topic.order(:id).distinct.exists?
94
+ end
95
+
96
+ def test_exists_with_includes_limit_and_empty_result
97
+ assert !Topic.includes(:replies).limit(0).exists?
98
+ assert !Topic.includes(:replies).limit(1).where('0 = 1').exists?
99
+ end
100
+
101
+ def test_exists_with_distinct_association_includes_and_limit
102
+ author = Author.first
103
+ assert !author.unique_categorized_posts.includes(:special_comments).limit(0).exists?
104
+ assert author.unique_categorized_posts.includes(:special_comments).limit(1).exists?
105
+ end
106
+
107
+ def test_exists_with_distinct_association_includes_limit_and_order
108
+ author = Author.first
109
+ assert !author.unique_categorized_posts.includes(:special_comments).order('comments.taggings_count DESC').limit(0).exists?
110
+ assert author.unique_categorized_posts.includes(:special_comments).order('comments.taggings_count DESC').limit(1).exists?
111
+ end
112
+
113
+ def test_exists_with_empty_table_and_no_args_given
114
+ Topic.delete_all
115
+ assert !Topic.exists?
116
+ end
117
+
118
+ def test_exists_with_aggregate_having_three_mappings
119
+ existing_address = customers(:david).address
120
+ assert Customer.exists?(:address => existing_address)
121
+ end
122
+
123
+ def test_exists_with_aggregate_having_three_mappings_with_one_difference
124
+ existing_address = customers(:david).address
125
+ assert !Customer.exists?(:address =>
126
+ Address.new(existing_address.street, existing_address.city, existing_address.country + "1"))
127
+ assert !Customer.exists?(:address =>
128
+ Address.new(existing_address.street, existing_address.city + "1", existing_address.country))
129
+ assert !Customer.exists?(:address =>
130
+ Address.new(existing_address.street + "1", existing_address.city, existing_address.country))
131
+ end
132
+
133
+ def test_exists_does_not_instantiate_records
134
+ Developer.expects(:instantiate).never
135
+ Developer.exists?
136
+ end
137
+
138
+ def test_find_by_array_of_one_id
139
+ assert_kind_of(Array, Topic.find([ 1 ]))
140
+ assert_equal(1, Topic.find([ 1 ]).length)
141
+ end
142
+
143
+ def test_find_by_ids
144
+ assert_equal 2, Topic.find(1, 2).size
145
+ assert_equal topics(:second).title, Topic.find([2]).first.title
146
+ end
147
+
148
+ def test_find_by_ids_with_limit_and_offset
149
+ assert_equal 2, Entrant.all.merge!(:limit => 2).find([1,3,2]).size
150
+ assert_equal 1, Entrant.all.merge!(:limit => 3, :offset => 2).find([1,3,2]).size
151
+
152
+ # Also test an edge case: If you have 11 results, and you set a
153
+ # limit of 3 and offset of 9, then you should find that there
154
+ # will be only 2 results, regardless of the limit.
155
+ devs = Developer.all
156
+ last_devs = Developer.all.merge!(:limit => 3, :offset => 9).find devs.map(&:id)
157
+ assert_equal 2, last_devs.size
158
+ end
159
+
160
+ def test_find_an_empty_array
161
+ assert_equal [], Topic.find([])
162
+ end
163
+
164
+ def test_find_doesnt_have_implicit_ordering
165
+ assert_sql(/^((?!ORDER).)*$/) { Topic.find(1) }
166
+ end
167
+
168
+ def test_find_by_ids_missing_one
169
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, 2, 45) }
170
+ end
171
+
172
+ def test_find_with_group_and_sanitized_having_method
173
+ developers = Developer.group(:salary).having("sum(salary) > ?", 10000).select('salary').to_a
174
+ assert_equal 3, developers.size
175
+ assert_equal 3, developers.map(&:salary).uniq.size
176
+ assert developers.all? { |developer| developer.salary > 10000 }
177
+ end
178
+
179
+ def test_find_with_entire_select_statement
180
+ topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
181
+
182
+ assert_equal(1, topics.size)
183
+ assert_equal(topics(:second).title, topics.first.title)
184
+ end
185
+
186
+ def test_find_with_prepared_select_statement
187
+ topics = Topic.find_by_sql ["SELECT * FROM topics WHERE author_name = ?", "Mary"]
188
+
189
+ assert_equal(1, topics.size)
190
+ assert_equal(topics(:second).title, topics.first.title)
191
+ end
192
+
193
+ def test_find_by_sql_with_sti_on_joined_table
194
+ accounts = Account.find_by_sql("SELECT * FROM accounts INNER JOIN companies ON companies.id = accounts.firm_id")
195
+ assert_equal [Account], accounts.collect(&:class).uniq
196
+ end
197
+
198
+ def test_take
199
+ assert_equal topics(:first), Topic.take
200
+ end
201
+
202
+ def test_take_failing
203
+ assert_nil Topic.where("title = 'This title does not exist'").take
204
+ end
205
+
206
+ def test_take_bang_present
207
+ assert_nothing_raised do
208
+ assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").take!
209
+ end
210
+ end
211
+
212
+ def test_take_bang_missing
213
+ assert_raises ActiveRecord::RecordNotFound do
214
+ Topic.where("title = 'This title does not exist'").take!
215
+ end
216
+ end
217
+
218
+ def test_first
219
+ assert_equal topics(:second).title, Topic.where("title = 'The Second Topic of the day'").first.title
220
+ end
221
+
222
+ def test_first_failing
223
+ assert_nil Topic.where("title = 'The Second Topic of the day!'").first
224
+ end
225
+
226
+ def test_first_bang_present
227
+ assert_nothing_raised do
228
+ assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").first!
229
+ end
230
+ end
231
+
232
+ def test_first_bang_missing
233
+ assert_raises ActiveRecord::RecordNotFound do
234
+ Topic.where("title = 'This title does not exist'").first!
235
+ end
236
+ end
237
+
238
+ def test_first_have_primary_key_order_by_default
239
+ expected = topics(:first)
240
+ expected.touch # PostgreSQL changes the default order if no order clause is used
241
+ assert_equal expected, Topic.first
242
+ end
243
+
244
+ def test_model_class_responds_to_first_bang
245
+ assert Topic.first!
246
+ Topic.delete_all
247
+ assert_raises ActiveRecord::RecordNotFound do
248
+ Topic.first!
249
+ end
250
+ end
251
+
252
+ def test_last_bang_present
253
+ assert_nothing_raised do
254
+ assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").last!
255
+ end
256
+ end
257
+
258
+ def test_last_bang_missing
259
+ assert_raises ActiveRecord::RecordNotFound do
260
+ Topic.where("title = 'This title does not exist'").last!
261
+ end
262
+ end
263
+
264
+ def test_model_class_responds_to_last_bang
265
+ assert_equal topics(:fourth), Topic.last!
266
+ assert_raises ActiveRecord::RecordNotFound do
267
+ Topic.delete_all
268
+ Topic.last!
269
+ end
270
+ end
271
+
272
+ def test_take_and_first_and_last_with_integer_should_use_sql_limit
273
+ assert_sql(/LIMIT 3|ROWNUM <= 3/) { Topic.take(3).entries }
274
+ assert_sql(/LIMIT 2|ROWNUM <= 2/) { Topic.first(2).entries }
275
+ assert_sql(/LIMIT 5|ROWNUM <= 5/) { Topic.last(5).entries }
276
+ end
277
+
278
+ def test_last_with_integer_and_order_should_keep_the_order
279
+ assert_equal Topic.order("title").to_a.last(2), Topic.order("title").last(2)
280
+ end
281
+
282
+ def test_last_with_integer_and_order_should_not_use_sql_limit
283
+ query = assert_sql { Topic.order("title").last(5).entries }
284
+ assert_equal 1, query.length
285
+ assert_no_match(/LIMIT/, query.first)
286
+ end
287
+
288
+ def test_last_with_integer_and_reorder_should_not_use_sql_limit
289
+ query = assert_sql { Topic.reorder("title").last(5).entries }
290
+ assert_equal 1, query.length
291
+ assert_no_match(/LIMIT/, query.first)
292
+ end
293
+
294
+ def test_take_and_first_and_last_with_integer_should_return_an_array
295
+ assert_kind_of Array, Topic.take(5)
296
+ assert_kind_of Array, Topic.first(5)
297
+ assert_kind_of Array, Topic.last(5)
298
+ end
299
+
300
+ def test_unexisting_record_exception_handling
301
+ assert_raise(ActiveRecord::RecordNotFound) {
302
+ Topic.find(1).parent
303
+ }
304
+
305
+ Topic.find(2).topic
306
+ end
307
+
308
+ def test_find_only_some_columns
309
+ topic = Topic.all.merge!(:select => "author_name").find(1)
310
+ assert_raise(ActiveModel::MissingAttributeError) {topic.title}
311
+ assert_raise(ActiveModel::MissingAttributeError) {topic.title?}
312
+ assert_nil topic.read_attribute("title")
313
+ assert_equal "David", topic.author_name
314
+ assert !topic.attribute_present?("title")
315
+ assert !topic.attribute_present?(:title)
316
+ assert topic.attribute_present?("author_name")
317
+ assert_respond_to topic, "author_name"
318
+ end
319
+
320
+ def test_find_on_array_conditions
321
+ assert Topic.all.merge!(:where => ["approved = ?", false]).find(1)
322
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.all.merge!(:where => ["approved = ?", true]).find(1) }
323
+ end
324
+
325
+ def test_find_on_hash_conditions
326
+ assert Topic.all.merge!(:where => { :approved => false }).find(1)
327
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.all.merge!(:where => { :approved => true }).find(1) }
328
+ end
329
+
330
+ def test_find_on_hash_conditions_with_explicit_table_name
331
+ assert Topic.all.merge!(:where => { 'topics.approved' => false }).find(1)
332
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.all.merge!(:where => { 'topics.approved' => true }).find(1) }
333
+ end
334
+
335
+ def test_find_on_hash_conditions_with_hashed_table_name
336
+ assert Topic.all.merge!(:where => {:topics => { :approved => false }}).find(1)
337
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.all.merge!(:where => {:topics => { :approved => true }}).find(1) }
338
+ end
339
+
340
+ def test_find_with_hash_conditions_on_joined_table
341
+ firms = Firm.joins(:account).where(:accounts => { :credit_limit => 50 })
342
+ assert_equal 1, firms.size
343
+ assert_equal companies(:first_firm), firms.first
344
+ end
345
+
346
+ def test_find_with_hash_conditions_on_joined_table_and_with_range
347
+ firms = DependentFirm.all.merge!(:joins => :account, :where => {:name => 'RailsCore', :accounts => { :credit_limit => 55..60 }})
348
+ assert_equal 1, firms.size
349
+ assert_equal companies(:rails_core), firms.first
350
+ end
351
+
352
+ def test_find_on_hash_conditions_with_explicit_table_name_and_aggregate
353
+ david = customers(:david)
354
+ assert Customer.where('customers.name' => david.name, :address => david.address).find(david.id)
355
+ assert_raise(ActiveRecord::RecordNotFound) {
356
+ Customer.where('customers.name' => david.name + "1", :address => david.address).find(david.id)
357
+ }
358
+ end
359
+
360
+ def test_find_on_association_proxy_conditions
361
+ assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10, 12], Comment.where(post_id: authors(:david).posts).map(&:id).sort
362
+ end
363
+
364
+ def test_find_on_hash_conditions_with_range
365
+ assert_equal [1,2], Topic.all.merge!(:where => { :id => 1..2 }).to_a.map(&:id).sort
366
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.all.merge!(:where => { :id => 2..3 }).find(1) }
367
+ end
368
+
369
+ def test_find_on_hash_conditions_with_end_exclusive_range
370
+ assert_equal [1,2,3], Topic.all.merge!(:where => { :id => 1..3 }).to_a.map(&:id).sort
371
+ assert_equal [1,2], Topic.all.merge!(:where => { :id => 1...3 }).to_a.map(&:id).sort
372
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.all.merge!(:where => { :id => 2...3 }).find(3) }
373
+ end
374
+
375
+ def test_find_on_hash_conditions_with_multiple_ranges
376
+ assert_equal [1,2,3], Comment.all.merge!(:where => { :id => 1..3, :post_id => 1..2 }).to_a.map(&:id).sort
377
+ assert_equal [1], Comment.all.merge!(:where => { :id => 1..1, :post_id => 1..10 }).to_a.map(&:id).sort
378
+ end
379
+
380
+ def test_find_on_hash_conditions_with_array_of_integers_and_ranges
381
+ assert_equal [1,2,3,5,6,7,8,9], Comment.all.merge!(:where => {:id => [1..2, 3, 5, 6..8, 9]}).to_a.map(&:id).sort
382
+ end
383
+
384
+ def test_find_on_multiple_hash_conditions
385
+ assert Topic.all.merge!(:where => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => false }).find(1)
386
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.all.merge!(:where => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }).find(1) }
387
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.all.merge!(:where => { :author_name => "David", :title => "HHC", :replies_count => 1, :approved => false }).find(1) }
388
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.all.merge!(:where => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }).find(1) }
389
+ end
390
+
391
+ def test_condition_interpolation
392
+ assert_kind_of Firm, Company.where("name = '%s'", "37signals").first
393
+ assert_nil Company.all.merge!(:where => ["name = '%s'", "37signals!"]).first
394
+ assert_nil Company.all.merge!(:where => ["name = '%s'", "37signals!' OR 1=1"]).first
395
+ assert_kind_of Time, Topic.all.merge!(:where => ["id = %d", 1]).first.written_on
396
+ end
397
+
398
+ def test_condition_array_interpolation
399
+ assert_kind_of Firm, Company.all.merge!(:where => ["name = '%s'", "37signals"]).first
400
+ assert_nil Company.all.merge!(:where => ["name = '%s'", "37signals!"]).first
401
+ assert_nil Company.all.merge!(:where => ["name = '%s'", "37signals!' OR 1=1"]).first
402
+ assert_kind_of Time, Topic.all.merge!(:where => ["id = %d", 1]).first.written_on
403
+ end
404
+
405
+ def test_condition_hash_interpolation
406
+ assert_kind_of Firm, Company.all.merge!(:where => { :name => "37signals"}).first
407
+ assert_nil Company.all.merge!(:where => { :name => "37signals!"}).first
408
+ assert_kind_of Time, Topic.all.merge!(:where => {:id => 1}).first.written_on
409
+ end
410
+
411
+ def test_hash_condition_find_malformed
412
+ assert_raise(ActiveRecord::StatementInvalid) {
413
+ Company.all.merge!(:where => { :id => 2, :dhh => true }).first
414
+ }
415
+ end
416
+
417
+ def test_hash_condition_find_with_escaped_characters
418
+ Company.create("name" => "Ain't noth'n like' \#stuff")
419
+ assert Company.all.merge!(:where => { :name => "Ain't noth'n like' \#stuff" }).first
420
+ end
421
+
422
+ def test_hash_condition_find_with_array
423
+ p1, p2 = Post.all.merge!(:limit => 2, :order => 'id asc').to_a
424
+ assert_equal [p1, p2], Post.all.merge!(:where => { :id => [p1, p2] }, :order => 'id asc').to_a
425
+ assert_equal [p1, p2], Post.all.merge!(:where => { :id => [p1, p2.id] }, :order => 'id asc').to_a
426
+ end
427
+
428
+ def test_hash_condition_find_with_nil
429
+ topic = Topic.all.merge!(:where => { :last_read => nil } ).first
430
+ assert_not_nil topic
431
+ assert_nil topic.last_read
432
+ end
433
+
434
+ def test_hash_condition_find_with_aggregate_having_one_mapping
435
+ balance = customers(:david).balance
436
+ assert_kind_of Money, balance
437
+ found_customer = Customer.where(:balance => balance).first
438
+ assert_equal customers(:david), found_customer
439
+ end
440
+
441
+ def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_aggregate
442
+ gps_location = customers(:david).gps_location
443
+ assert_kind_of GpsLocation, gps_location
444
+ found_customer = Customer.where(:gps_location => gps_location).first
445
+ assert_equal customers(:david), found_customer
446
+ end
447
+
448
+ def test_hash_condition_find_with_aggregate_having_one_mapping_and_key_value_being_attribute_value
449
+ balance = customers(:david).balance
450
+ assert_kind_of Money, balance
451
+ found_customer = Customer.where(:balance => balance.amount).first
452
+ assert_equal customers(:david), found_customer
453
+ end
454
+
455
+ def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_attribute_value
456
+ gps_location = customers(:david).gps_location
457
+ assert_kind_of GpsLocation, gps_location
458
+ found_customer = Customer.where(:gps_location => gps_location.gps_location).first
459
+ assert_equal customers(:david), found_customer
460
+ end
461
+
462
+ def test_hash_condition_find_with_aggregate_having_three_mappings
463
+ address = customers(:david).address
464
+ assert_kind_of Address, address
465
+ found_customer = Customer.where(:address => address).first
466
+ assert_equal customers(:david), found_customer
467
+ end
468
+
469
+ def test_hash_condition_find_with_one_condition_being_aggregate_and_another_not
470
+ address = customers(:david).address
471
+ assert_kind_of Address, address
472
+ found_customer = Customer.where(:address => address, :name => customers(:david).name).first
473
+ assert_equal customers(:david), found_customer
474
+ end
475
+
476
+ def test_condition_utc_time_interpolation_with_default_timezone_local
477
+ with_env_tz 'America/New_York' do
478
+ with_active_record_default_timezone :local do
479
+ topic = Topic.first
480
+ assert_equal topic, Topic.all.merge!(:where => ['written_on = ?', topic.written_on.getutc]).first
481
+ end
482
+ end
483
+ end
484
+
485
+ def test_hash_condition_utc_time_interpolation_with_default_timezone_local
486
+ with_env_tz 'America/New_York' do
487
+ with_active_record_default_timezone :local do
488
+ topic = Topic.first
489
+ assert_equal topic, Topic.all.merge!(:where => {:written_on => topic.written_on.getutc}).first
490
+ end
491
+ end
492
+ end
493
+
494
+ def test_condition_local_time_interpolation_with_default_timezone_utc
495
+ with_env_tz 'America/New_York' do
496
+ with_active_record_default_timezone :utc do
497
+ topic = Topic.first
498
+ assert_equal topic, Topic.all.merge!(:where => ['written_on = ?', topic.written_on.getlocal]).first
499
+ end
500
+ end
501
+ end
502
+
503
+ def test_hash_condition_local_time_interpolation_with_default_timezone_utc
504
+ with_env_tz 'America/New_York' do
505
+ with_active_record_default_timezone :utc do
506
+ topic = Topic.first
507
+ assert_equal topic, Topic.all.merge!(:where => {:written_on => topic.written_on.getlocal}).first
508
+ end
509
+ end
510
+ end
511
+
512
+ def test_bind_variables
513
+ assert_kind_of Firm, Company.all.merge!(:where => ["name = ?", "37signals"]).first
514
+ assert_nil Company.all.merge!(:where => ["name = ?", "37signals!"]).first
515
+ assert_nil Company.all.merge!(:where => ["name = ?", "37signals!' OR 1=1"]).first
516
+ assert_kind_of Time, Topic.all.merge!(:where => ["id = ?", 1]).first.written_on
517
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
518
+ Company.all.merge!(:where => ["id=? AND name = ?", 2]).first
519
+ }
520
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
521
+ Company.all.merge!(:where => ["id=?", 2, 3, 4]).first
522
+ }
523
+ end
524
+
525
+ def test_bind_variables_with_quotes
526
+ Company.create("name" => "37signals' go'es agains")
527
+ assert Company.all.merge!(:where => ["name = ?", "37signals' go'es agains"]).first
528
+ end
529
+
530
+ def test_named_bind_variables_with_quotes
531
+ Company.create("name" => "37signals' go'es agains")
532
+ assert Company.all.merge!(:where => ["name = :name", {:name => "37signals' go'es agains"}]).first
533
+ end
534
+
535
+ def test_bind_arity
536
+ assert_nothing_raised { bind '' }
537
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '', 1 }
538
+
539
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '?' }
540
+ assert_nothing_raised { bind '?', 1 }
541
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 }
542
+ end
543
+
544
+ def test_named_bind_variables
545
+ assert_equal '1', bind(':a', :a => 1) # ' ruby-mode
546
+ assert_equal '1 1', bind(':a :a', :a => 1) # ' ruby-mode
547
+
548
+ assert_nothing_raised { bind("'+00:00'", :foo => "bar") }
549
+
550
+ assert_kind_of Firm, Company.all.merge!(:where => ["name = :name", { :name => "37signals" }]).first
551
+ assert_nil Company.all.merge!(:where => ["name = :name", { :name => "37signals!" }]).first
552
+ assert_nil Company.all.merge!(:where => ["name = :name", { :name => "37signals!' OR 1=1" }]).first
553
+ assert_kind_of Time, Topic.all.merge!(:where => ["id = :id", { :id => 1 }]).first.written_on
554
+ end
555
+
556
+ class SimpleEnumerable
557
+ include Enumerable
558
+
559
+ def initialize(ary)
560
+ @ary = ary
561
+ end
562
+
563
+ def each(&b)
564
+ @ary.each(&b)
565
+ end
566
+ end
567
+
568
+ def test_bind_enumerable
569
+ quoted_abc = %(#{ActiveRecord::Base.connection.quote('a')},#{ActiveRecord::Base.connection.quote('b')},#{ActiveRecord::Base.connection.quote('c')})
570
+
571
+ assert_equal '1,2,3', bind('?', [1, 2, 3])
572
+ assert_equal quoted_abc, bind('?', %w(a b c))
573
+
574
+ assert_equal '1,2,3', bind(':a', :a => [1, 2, 3])
575
+ assert_equal quoted_abc, bind(':a', :a => %w(a b c)) # '
576
+
577
+ assert_equal '1,2,3', bind('?', SimpleEnumerable.new([1, 2, 3]))
578
+ assert_equal quoted_abc, bind('?', SimpleEnumerable.new(%w(a b c)))
579
+
580
+ assert_equal '1,2,3', bind(':a', :a => SimpleEnumerable.new([1, 2, 3]))
581
+ assert_equal quoted_abc, bind(':a', :a => SimpleEnumerable.new(%w(a b c))) # '
582
+ end
583
+
584
+ def test_bind_empty_enumerable
585
+ quoted_nil = ActiveRecord::Base.connection.quote(nil)
586
+ assert_equal quoted_nil, bind('?', [])
587
+ assert_equal " in (#{quoted_nil})", bind(' in (?)', [])
588
+ assert_equal "foo in (#{quoted_nil})", bind('foo in (?)', [])
589
+ end
590
+
591
+ def test_bind_empty_string
592
+ quoted_empty = ActiveRecord::Base.connection.quote('')
593
+ assert_equal quoted_empty, bind('?', '')
594
+ end
595
+
596
+ def test_bind_chars
597
+ quoted_bambi = ActiveRecord::Base.connection.quote("Bambi")
598
+ quoted_bambi_and_thumper = ActiveRecord::Base.connection.quote("Bambi\nand\nThumper")
599
+ assert_equal "name=#{quoted_bambi}", bind('name=?', "Bambi")
600
+ assert_equal "name=#{quoted_bambi_and_thumper}", bind('name=?', "Bambi\nand\nThumper")
601
+ assert_equal "name=#{quoted_bambi}", bind('name=?', "Bambi".mb_chars)
602
+ assert_equal "name=#{quoted_bambi_and_thumper}", bind('name=?', "Bambi\nand\nThumper".mb_chars)
603
+ end
604
+
605
+ def test_bind_record
606
+ o = Struct.new(:quoted_id).new(1)
607
+ assert_equal '1', bind('?', o)
608
+
609
+ os = [o] * 3
610
+ assert_equal '1,1,1', bind('?', os)
611
+ end
612
+
613
+ def test_named_bind_with_postgresql_type_casts
614
+ l = Proc.new { bind(":a::integer '2009-01-01'::date", :a => '10') }
615
+ assert_nothing_raised(&l)
616
+ assert_equal "#{ActiveRecord::Base.quote_value('10')}::integer '2009-01-01'::date", l.call
617
+ end
618
+
619
+ def test_string_sanitation
620
+ assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
621
+ assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table")
622
+ end
623
+
624
+ def test_count_by_sql
625
+ assert_equal(0, Entrant.count_by_sql("SELECT COUNT(*) FROM entrants WHERE id > 3"))
626
+ assert_equal(1, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 2]))
627
+ assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
628
+ end
629
+
630
+ def test_find_by_one_attribute
631
+ assert_equal topics(:first), Topic.find_by_title("The First Topic")
632
+ assert_nil Topic.find_by_title("The First Topic!")
633
+ end
634
+
635
+ def test_find_by_one_attribute_bang
636
+ assert_equal topics(:first), Topic.find_by_title!("The First Topic")
637
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
638
+ end
639
+
640
+ def test_find_by_one_attribute_that_is_an_alias
641
+ assert_equal topics(:first), Topic.find_by_heading("The First Topic")
642
+ assert_nil Topic.find_by_heading("The First Topic!")
643
+ end
644
+
645
+ def test_find_by_one_attribute_bang_with_blank_defined
646
+ blank_topic = BlankTopic.create(title: "The Blank One")
647
+ assert_equal blank_topic, BlankTopic.find_by_title!("The Blank One")
648
+ end
649
+
650
+ def test_find_by_one_attribute_with_conditions
651
+ assert_equal accounts(:rails_core_account), Account.where('firm_id = ?', 6).find_by_credit_limit(50)
652
+ end
653
+
654
+ def test_find_by_one_attribute_that_is_an_aggregate
655
+ address = customers(:david).address
656
+ assert_kind_of Address, address
657
+ found_customer = Customer.find_by_address(address)
658
+ assert_equal customers(:david), found_customer
659
+ end
660
+
661
+ def test_find_by_one_attribute_that_is_an_aggregate_with_one_attribute_difference
662
+ address = customers(:david).address
663
+ assert_kind_of Address, address
664
+ missing_address = Address.new(address.street, address.city, address.country + "1")
665
+ assert_nil Customer.find_by_address(missing_address)
666
+ missing_address = Address.new(address.street, address.city + "1", address.country)
667
+ assert_nil Customer.find_by_address(missing_address)
668
+ missing_address = Address.new(address.street + "1", address.city, address.country)
669
+ assert_nil Customer.find_by_address(missing_address)
670
+ end
671
+
672
+ def test_find_by_two_attributes_that_are_both_aggregates
673
+ balance = customers(:david).balance
674
+ address = customers(:david).address
675
+ assert_kind_of Money, balance
676
+ assert_kind_of Address, address
677
+ found_customer = Customer.find_by_balance_and_address(balance, address)
678
+ assert_equal customers(:david), found_customer
679
+ end
680
+
681
+ def test_find_by_two_attributes_with_one_being_an_aggregate
682
+ balance = customers(:david).balance
683
+ assert_kind_of Money, balance
684
+ found_customer = Customer.find_by_balance_and_name(balance, customers(:david).name)
685
+ assert_equal customers(:david), found_customer
686
+ end
687
+
688
+ def test_dynamic_finder_on_one_attribute_with_conditions_returns_same_results_after_caching
689
+ # ensure this test can run independently of order
690
+ class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.include?(:find_by_credit_limit)
691
+ a = Account.where('firm_id = ?', 6).find_by_credit_limit(50)
692
+ assert_equal a, Account.where('firm_id = ?', 6).find_by_credit_limit(50) # find_by_credit_limit has been cached
693
+ end
694
+
695
+ def test_find_by_one_attribute_with_several_options
696
+ assert_equal accounts(:unknown), Account.order('id DESC').where('id != ?', 3).find_by_credit_limit(50)
697
+ end
698
+
699
+ def test_find_by_one_missing_attribute
700
+ assert_raise(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
701
+ end
702
+
703
+ def test_find_by_invalid_method_syntax
704
+ assert_raise(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
705
+ assert_raise(NoMethodError) { Topic.find_by_title?("The First Topic") }
706
+ assert_raise(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
707
+ assert_raise(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
708
+ end
709
+
710
+ def test_find_by_two_attributes
711
+ assert_equal topics(:first), Topic.find_by_title_and_author_name("The First Topic", "David")
712
+ assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")
713
+ end
714
+
715
+ def test_find_by_two_attributes_but_passing_only_one
716
+ assert_raise(ArgumentError) { Topic.find_by_title_and_author_name("The First Topic") }
717
+ end
718
+
719
+ def test_find_by_nil_attribute
720
+ topic = Topic.find_by_last_read nil
721
+ assert_not_nil topic
722
+ assert_nil topic.last_read
723
+ end
724
+
725
+ def test_find_by_nil_and_not_nil_attributes
726
+ topic = Topic.find_by_last_read_and_author_name nil, "Mary"
727
+ assert_equal "Mary", topic.author_name
728
+ end
729
+
730
+ def test_find_with_bad_sql
731
+ assert_raise(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
732
+ end
733
+
734
+ def test_find_all_with_join
735
+ developers_on_project_one = Developer.all.merge!(
736
+ :joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
737
+ :where => 'project_id=1'
738
+ ).to_a
739
+ assert_equal 3, developers_on_project_one.length
740
+ developer_names = developers_on_project_one.map { |d| d.name }
741
+ assert developer_names.include?('David')
742
+ assert developer_names.include?('Jamis')
743
+ end
744
+
745
+ def test_joins_dont_clobber_id
746
+ first = Firm.all.merge!(
747
+ :joins => 'INNER JOIN companies clients ON clients.firm_id = companies.id',
748
+ :where => 'companies.id = 1'
749
+ ).first
750
+ assert_equal 1, first.id
751
+ end
752
+
753
+ def test_joins_with_string_array
754
+ person_with_reader_and_post = Post.all.merge!(
755
+ :joins => [
756
+ "INNER JOIN categorizations ON categorizations.post_id = posts.id",
757
+ "INNER JOIN categories ON categories.id = categorizations.category_id AND categories.type = 'SpecialCategory'"
758
+ ]
759
+ )
760
+ assert_equal 1, person_with_reader_and_post.size
761
+ end
762
+
763
+ def test_find_by_id_with_conditions_with_or
764
+ assert_nothing_raised do
765
+ Post.where("posts.id <= 3 OR posts.#{QUOTED_TYPE} = 'Post'").find([1,2,3])
766
+ end
767
+ end
768
+
769
+ # http://dev.rubyonrails.org/ticket/6778
770
+ def test_find_ignores_previously_inserted_record
771
+ Post.create!(:title => 'test', :body => 'it out')
772
+ assert_equal [], Post.where(id: nil)
773
+ end
774
+
775
+ def test_find_by_empty_ids
776
+ assert_equal [], Post.find([])
777
+ end
778
+
779
+ def test_find_by_empty_in_condition
780
+ assert_equal [], Post.where('id in (?)', [])
781
+ end
782
+
783
+ def test_find_by_records
784
+ p1, p2 = Post.all.merge!(:limit => 2, :order => 'id asc').to_a
785
+ assert_equal [p1, p2], Post.all.merge!(:where => ['id in (?)', [p1, p2]], :order => 'id asc')
786
+ assert_equal [p1, p2], Post.all.merge!(:where => ['id in (?)', [p1, p2.id]], :order => 'id asc')
787
+ end
788
+
789
+ def test_select_value
790
+ assert_equal "37signals", Company.connection.select_value("SELECT name FROM companies WHERE id = 1")
791
+ assert_nil Company.connection.select_value("SELECT name FROM companies WHERE id = -1")
792
+ # make sure we didn't break count...
793
+ assert_equal 0, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = 'Halliburton'")
794
+ assert_equal 1, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = '37signals'")
795
+ end
796
+
797
+ def test_select_values
798
+ assert_equal ["1","2","3","4","5","6","7","8","9", "10"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s }
799
+ assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy", "Ex Nihilo Part Deux"], Company.connection.select_values("SELECT name FROM companies ORDER BY id")
800
+ end
801
+
802
+ def test_select_rows
803
+ assert_equal(
804
+ [["1", "1", nil, "37signals"],
805
+ ["2", "1", "2", "Summit"],
806
+ ["3", "1", "1", "Microsoft"]],
807
+ 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?}})
808
+ assert_equal [["1", "37signals"], ["2", "Summit"], ["3", "Microsoft"]],
809
+ 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?}}
810
+ end
811
+
812
+ def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
813
+ assert_equal 2, Post.all.merge!(:includes => { :authors => :author_address }, :order => 'author_addresses.id DESC ', :limit => 2).to_a.size
814
+
815
+ assert_equal 3, Post.all.merge!(:includes => { :author => :author_address, :authors => :author_address},
816
+ :order => 'author_addresses_authors.id DESC ', :limit => 3).to_a.size
817
+ end
818
+
819
+ def test_find_with_nil_inside_set_passed_for_one_attribute
820
+ client_of = Company.all.merge!(
821
+ :where => {
822
+ :client_of => [2, 1, nil],
823
+ :name => ['37signals', 'Summit', 'Microsoft'] },
824
+ :order => 'client_of DESC'
825
+ ).map { |x| x.client_of }
826
+
827
+ assert client_of.include?(nil)
828
+ assert_equal [2, 1].sort, client_of.compact.sort
829
+ end
830
+
831
+ def test_find_with_nil_inside_set_passed_for_attribute
832
+ client_of = Company.all.merge!(
833
+ :where => { :client_of => [nil] },
834
+ :order => 'client_of DESC'
835
+ ).map { |x| x.client_of }
836
+
837
+ assert_equal [], client_of.compact
838
+ end
839
+
840
+ def test_with_limiting_with_custom_select
841
+ posts = Post.references(:authors).merge(
842
+ :includes => :author, :select => ' posts.*, authors.id as "author_id"',
843
+ :limit => 3, :order => 'posts.id'
844
+ ).to_a
845
+ assert_equal 3, posts.size
846
+ assert_equal [0, 1, 1], posts.map(&:author_id).sort
847
+ end
848
+
849
+ def test_find_one_message_with_custom_primary_key
850
+ Toy.primary_key = :name
851
+ begin
852
+ Toy.find 'Hello World!'
853
+ rescue ActiveRecord::RecordNotFound => e
854
+ assert_equal 'Couldn\'t find Toy with name=Hello World!', e.message
855
+ end
856
+ ensure
857
+ Toy.reset_primary_key
858
+ end
859
+
860
+ def test_finder_with_offset_string
861
+ assert_nothing_raised(ActiveRecord::StatementInvalid) { Topic.all.merge!(:offset => "3").to_a }
862
+ end
863
+
864
+ protected
865
+ def bind(statement, *vars)
866
+ if vars.first.is_a?(Hash)
867
+ ActiveRecord::Base.send(:replace_named_bind_variables, statement, vars.first)
868
+ else
869
+ ActiveRecord::Base.send(:replace_bind_variables, statement, vars)
870
+ end
871
+ end
872
+
873
+ def with_env_tz(new_tz = 'US/Eastern')
874
+ old_tz, ENV['TZ'] = ENV['TZ'], new_tz
875
+ yield
876
+ ensure
877
+ old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
878
+ end
879
+
880
+ def with_active_record_default_timezone(zone)
881
+ old_zone, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, zone
882
+ yield
883
+ ensure
884
+ ActiveRecord::Base.default_timezone = old_zone
885
+ end
886
+ end