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,802 @@
1
+ require "cases/helper"
2
+ require 'models/post'
3
+ require 'models/comment'
4
+ require 'models/author'
5
+ require 'models/topic'
6
+ require 'models/reply'
7
+ require 'models/category'
8
+ require 'models/company'
9
+ require 'models/developer'
10
+ require 'models/project'
11
+ require 'models/minimalistic'
12
+ require 'models/warehouse_thing'
13
+ require 'models/parrot'
14
+ require 'models/minivan'
15
+ require 'models/owner'
16
+ require 'models/person'
17
+ require 'models/pet'
18
+ require 'models/toy'
19
+ require 'rexml/document'
20
+
21
+ class PersistenceTest < ActiveRecord::TestCase
22
+ fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts, :minivans, :pets, :toys
23
+
24
+ # Oracle UPDATE does not support ORDER BY
25
+ unless current_adapter?(:OracleAdapter)
26
+ def test_update_all_ignores_order_without_limit_from_association
27
+ author = authors(:david)
28
+ assert_nothing_raised do
29
+ assert_equal author.posts_with_comments_and_categories.length, author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ])
30
+ end
31
+ end
32
+
33
+ def test_update_all_doesnt_ignore_order
34
+ assert_equal authors(:david).id + 1, authors(:mary).id # make sure there is going to be a duplicate PK error
35
+ test_update_with_order_succeeds = lambda do |order|
36
+ begin
37
+ Author.order(order).update_all('id = id + 1')
38
+ rescue ActiveRecord::ActiveRecordError
39
+ false
40
+ end
41
+ end
42
+
43
+ if test_update_with_order_succeeds.call('id DESC')
44
+ assert !test_update_with_order_succeeds.call('id ASC') # test that this wasn't a fluke and using an incorrect order results in an exception
45
+ else
46
+ # test that we're failing because the current Arel's engine doesn't support UPDATE ORDER BY queries is using subselects instead
47
+ assert_sql(/\AUPDATE .+ \(SELECT .* ORDER BY id DESC\)\Z/i) do
48
+ test_update_with_order_succeeds.call('id DESC')
49
+ end
50
+ end
51
+ end
52
+
53
+ def test_update_all_with_order_and_limit_updates_subset_only
54
+ author = authors(:david)
55
+ assert_nothing_raised do
56
+ assert_equal 1, author.posts_sorted_by_id_limited.size
57
+ assert_equal 2, author.posts_sorted_by_id_limited.limit(2).to_a.size
58
+ assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ])
59
+ assert_equal "bulk update!", posts(:welcome).body
60
+ assert_not_equal "bulk update!", posts(:thinking).body
61
+ end
62
+ end
63
+ end
64
+
65
+ def test_update_many
66
+ topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
67
+ updated = Topic.update(topic_data.keys, topic_data.values)
68
+
69
+ assert_equal 2, updated.size
70
+ assert_equal "1 updated", Topic.find(1).content
71
+ assert_equal "2 updated", Topic.find(2).content
72
+ end
73
+
74
+ def test_delete_all
75
+ assert Topic.count > 0
76
+
77
+ assert_equal Topic.count, Topic.delete_all
78
+ end
79
+
80
+ def test_delete_all_with_joins_and_where_part_is_hash
81
+ where_args = {:toys => {:name => 'Bone'}}
82
+ count = Pet.joins(:toys).where(where_args).count
83
+
84
+ assert_equal count, 1
85
+ assert_equal count, Pet.joins(:toys).where(where_args).delete_all
86
+ end
87
+
88
+ def test_delete_all_with_joins_and_where_part_is_not_hash
89
+ where_args = ['toys.name = ?', 'Bone']
90
+ count = Pet.joins(:toys).where(where_args).count
91
+
92
+ assert_equal count, 1
93
+ assert_equal count, Pet.joins(:toys).where(where_args).delete_all
94
+ end
95
+
96
+ def test_increment_attribute
97
+ assert_equal 50, accounts(:signals37).credit_limit
98
+ accounts(:signals37).increment! :credit_limit
99
+ assert_equal 51, accounts(:signals37, :reload).credit_limit
100
+
101
+ accounts(:signals37).increment(:credit_limit).increment!(:credit_limit)
102
+ assert_equal 53, accounts(:signals37, :reload).credit_limit
103
+ end
104
+
105
+ def test_increment_nil_attribute
106
+ assert_nil topics(:first).parent_id
107
+ topics(:first).increment! :parent_id
108
+ assert_equal 1, topics(:first).parent_id
109
+ end
110
+
111
+ def test_increment_attribute_by
112
+ assert_equal 50, accounts(:signals37).credit_limit
113
+ accounts(:signals37).increment! :credit_limit, 5
114
+ assert_equal 55, accounts(:signals37, :reload).credit_limit
115
+
116
+ accounts(:signals37).increment(:credit_limit, 1).increment!(:credit_limit, 3)
117
+ assert_equal 59, accounts(:signals37, :reload).credit_limit
118
+ end
119
+
120
+ def test_destroy_all
121
+ conditions = "author_name = 'Mary'"
122
+ topics_by_mary = Topic.all.merge!(:where => conditions, :order => 'id').to_a
123
+ assert ! topics_by_mary.empty?
124
+
125
+ assert_difference('Topic.count', -topics_by_mary.size) do
126
+ destroyed = Topic.destroy_all(conditions).sort_by(&:id)
127
+ assert_equal topics_by_mary, destroyed
128
+ assert destroyed.all? { |topic| topic.frozen? }, "destroyed topics should be frozen"
129
+ end
130
+ end
131
+
132
+ def test_destroy_many
133
+ clients = Client.all.merge!(:order => 'id').find([2, 3])
134
+
135
+ assert_difference('Client.count', -2) do
136
+ destroyed = Client.destroy([2, 3]).sort_by(&:id)
137
+ assert_equal clients, destroyed
138
+ assert destroyed.all? { |client| client.frozen? }, "destroyed clients should be frozen"
139
+ end
140
+ end
141
+
142
+ def test_becomes
143
+ assert_kind_of Reply, topics(:first).becomes(Reply)
144
+ assert_equal "The First Topic", topics(:first).becomes(Reply).title
145
+ end
146
+
147
+ def test_becomes_includes_errors
148
+ company = Company.new(:name => nil)
149
+ assert !company.valid?
150
+ original_errors = company.errors
151
+ client = company.becomes(Client)
152
+ assert_equal original_errors, client.errors
153
+ end
154
+
155
+ def test_delete_many
156
+ original_count = Topic.count
157
+ Topic.delete(deleting = [1, 2])
158
+ assert_equal original_count - deleting.size, Topic.count
159
+ end
160
+
161
+ def test_decrement_attribute
162
+ assert_equal 50, accounts(:signals37).credit_limit
163
+
164
+ accounts(:signals37).decrement!(:credit_limit)
165
+ assert_equal 49, accounts(:signals37, :reload).credit_limit
166
+
167
+ accounts(:signals37).decrement(:credit_limit).decrement!(:credit_limit)
168
+ assert_equal 47, accounts(:signals37, :reload).credit_limit
169
+ end
170
+
171
+ def test_decrement_attribute_by
172
+ assert_equal 50, accounts(:signals37).credit_limit
173
+ accounts(:signals37).decrement! :credit_limit, 5
174
+ assert_equal 45, accounts(:signals37, :reload).credit_limit
175
+
176
+ accounts(:signals37).decrement(:credit_limit, 1).decrement!(:credit_limit, 3)
177
+ assert_equal 41, accounts(:signals37, :reload).credit_limit
178
+ end
179
+
180
+ def test_create
181
+ topic = Topic.new
182
+ topic.title = "New Topic"
183
+ topic.save
184
+ topic_reloaded = Topic.find(topic.id)
185
+ assert_equal("New Topic", topic_reloaded.title)
186
+ end
187
+
188
+ def test_save!
189
+ topic = Topic.new(:title => "New Topic")
190
+ assert topic.save!
191
+
192
+ reply = WrongReply.new
193
+ assert_raise(ActiveRecord::RecordInvalid) { reply.save! }
194
+ end
195
+
196
+ def test_save_null_string_attributes
197
+ topic = Topic.find(1)
198
+ topic.attributes = { "title" => "null", "author_name" => "null" }
199
+ topic.save!
200
+ topic.reload
201
+ assert_equal("null", topic.title)
202
+ assert_equal("null", topic.author_name)
203
+ end
204
+
205
+ def test_save_nil_string_attributes
206
+ topic = Topic.find(1)
207
+ topic.title = nil
208
+ topic.save!
209
+ topic.reload
210
+ assert_nil topic.title
211
+ end
212
+
213
+ def test_save_for_record_with_only_primary_key
214
+ minimalistic = Minimalistic.new
215
+ assert_nothing_raised { minimalistic.save }
216
+ end
217
+
218
+ def test_save_for_record_with_only_primary_key_that_is_provided
219
+ assert_nothing_raised { Minimalistic.create!(:id => 2) }
220
+ end
221
+
222
+ def test_create_many
223
+ topics = Topic.create([ { "title" => "first" }, { "title" => "second" }])
224
+ assert_equal 2, topics.size
225
+ assert_equal "first", topics.first.title
226
+ end
227
+
228
+ def test_create_columns_not_equal_attributes
229
+ topic = Topic.allocate.init_with(
230
+ 'attributes' => {
231
+ 'title' => 'Another New Topic',
232
+ 'does_not_exist' => 'test'
233
+ }
234
+ )
235
+ assert_nothing_raised { topic.save }
236
+ end
237
+
238
+ def test_create_through_factory_with_block
239
+ topic = Topic.create("title" => "New Topic") do |t|
240
+ t.author_name = "David"
241
+ end
242
+ assert_equal("New Topic", topic.title)
243
+ assert_equal("David", topic.author_name)
244
+ end
245
+
246
+ def test_create_many_through_factory_with_block
247
+ topics = Topic.create([ { "title" => "first" }, { "title" => "second" }]) do |t|
248
+ t.author_name = "David"
249
+ end
250
+ assert_equal 2, topics.size
251
+ topic1, topic2 = Topic.find(topics[0].id), Topic.find(topics[1].id)
252
+ assert_equal "first", topic1.title
253
+ assert_equal "David", topic1.author_name
254
+ assert_equal "second", topic2.title
255
+ assert_equal "David", topic2.author_name
256
+ end
257
+
258
+ def test_update_object
259
+ topic = Topic.new
260
+ topic.title = "Another New Topic"
261
+ topic.written_on = "2003-12-12 23:23:00"
262
+ topic.save
263
+ topic_reloaded = Topic.find(topic.id)
264
+ assert_equal("Another New Topic", topic_reloaded.title)
265
+
266
+ topic_reloaded.title = "Updated topic"
267
+ topic_reloaded.save
268
+
269
+ topic_reloaded_again = Topic.find(topic.id)
270
+
271
+ assert_equal("Updated topic", topic_reloaded_again.title)
272
+ end
273
+
274
+ def test_update_columns_not_equal_attributes
275
+ topic = Topic.new
276
+ topic.title = "Still another topic"
277
+ topic.save
278
+
279
+ topic_reloaded = Topic.allocate
280
+ topic_reloaded.init_with(
281
+ 'attributes' => topic.attributes.merge('does_not_exist' => 'test')
282
+ )
283
+ topic_reloaded.title = 'A New Topic'
284
+ assert_nothing_raised { topic_reloaded.save }
285
+ end
286
+
287
+ def test_update_for_record_with_only_primary_key
288
+ minimalistic = minimalistics(:first)
289
+ assert_nothing_raised { minimalistic.save }
290
+ end
291
+
292
+ def test_update_sti_type
293
+ assert_instance_of Reply, topics(:second)
294
+
295
+ topic = topics(:second).becomes!(Topic)
296
+ assert_instance_of Topic, topic
297
+ topic.save!
298
+ assert_instance_of Topic, Topic.find(topic.id)
299
+ end
300
+
301
+ def test_preserve_original_sti_type
302
+ reply = topics(:second)
303
+ assert_equal "Reply", reply.type
304
+
305
+ topic = reply.becomes(Topic)
306
+ assert_equal "Reply", reply.type
307
+
308
+ assert_instance_of Topic, topic
309
+ assert_equal "Reply", topic.type
310
+ end
311
+
312
+ def test_update_after_create
313
+ klass = Class.new(Topic) do
314
+ def self.name; 'Topic'; end
315
+ after_create do
316
+ update_attribute("author_name", "David")
317
+ end
318
+ end
319
+ topic = klass.new
320
+ topic.title = "Another New Topic"
321
+ topic.save
322
+
323
+ topic_reloaded = Topic.find(topic.id)
324
+ assert_equal("Another New Topic", topic_reloaded.title)
325
+ assert_equal("David", topic_reloaded.author_name)
326
+ end
327
+
328
+ def test_delete
329
+ topic = Topic.find(1)
330
+ assert_equal topic, topic.delete, 'topic.delete did not return self'
331
+ assert topic.frozen?, 'topic not frozen after delete'
332
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
333
+ end
334
+
335
+ def test_delete_doesnt_run_callbacks
336
+ Topic.find(1).delete
337
+ assert_not_nil Topic.find(2)
338
+ end
339
+
340
+ def test_destroy
341
+ topic = Topic.find(1)
342
+ assert_equal topic, topic.destroy, 'topic.destroy did not return self'
343
+ assert topic.frozen?, 'topic not frozen after destroy'
344
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
345
+ end
346
+
347
+ def test_destroy!
348
+ topic = Topic.find(1)
349
+ assert_equal topic, topic.destroy!, 'topic.destroy! did not return self'
350
+ assert topic.frozen?, 'topic not frozen after destroy!'
351
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
352
+ end
353
+
354
+ def test_record_not_found_exception
355
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(99999) }
356
+ end
357
+
358
+ def test_update_all
359
+ assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
360
+ assert_equal "bulk updated!", Topic.find(1).content
361
+ assert_equal "bulk updated!", Topic.find(2).content
362
+
363
+ assert_equal Topic.count, Topic.update_all(['content = ?', 'bulk updated again!'])
364
+ assert_equal "bulk updated again!", Topic.find(1).content
365
+ assert_equal "bulk updated again!", Topic.find(2).content
366
+
367
+ assert_equal Topic.count, Topic.update_all(['content = ?', nil])
368
+ assert_nil Topic.find(1).content
369
+ end
370
+
371
+ def test_update_all_with_hash
372
+ assert_not_nil Topic.find(1).last_read
373
+ assert_equal Topic.count, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil)
374
+ assert_equal "bulk updated with hash!", Topic.find(1).content
375
+ assert_equal "bulk updated with hash!", Topic.find(2).content
376
+ assert_nil Topic.find(1).last_read
377
+ assert_nil Topic.find(2).last_read
378
+ end
379
+
380
+ def test_update_all_with_non_standard_table_name
381
+ assert_equal 1, WarehouseThing.where(id: 1).update_all(['value = ?', 0])
382
+ assert_equal 0, WarehouseThing.find(1).value
383
+ end
384
+
385
+ def test_delete_new_record
386
+ client = Client.new
387
+ client.delete
388
+ assert client.frozen?
389
+ end
390
+
391
+ def test_delete_record_with_associations
392
+ client = Client.find(3)
393
+ client.delete
394
+ assert client.frozen?
395
+ assert_kind_of Firm, client.firm
396
+ assert_raise(RuntimeError) { client.name = "something else" }
397
+ end
398
+
399
+ def test_destroy_new_record
400
+ client = Client.new
401
+ client.destroy
402
+ assert client.frozen?
403
+ end
404
+
405
+ def test_destroy_record_with_associations
406
+ client = Client.find(3)
407
+ client.destroy
408
+ assert client.frozen?
409
+ assert_kind_of Firm, client.firm
410
+ assert_raise(RuntimeError) { client.name = "something else" }
411
+ end
412
+
413
+ def test_update_attribute
414
+ assert !Topic.find(1).approved?
415
+ Topic.find(1).update_attribute("approved", true)
416
+ assert Topic.find(1).approved?
417
+
418
+ Topic.find(1).update_attribute(:approved, false)
419
+ assert !Topic.find(1).approved?
420
+ end
421
+
422
+ def test_update_attribute_does_not_choke_on_nil
423
+ assert Topic.find(1).update(nil)
424
+ end
425
+
426
+ def test_update_attribute_for_readonly_attribute
427
+ minivan = Minivan.find('m1')
428
+ assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
429
+ end
430
+
431
+ def test_update_attribute_with_one_updated
432
+ t = Topic.first
433
+ t.update_attribute(:title, 'super_title')
434
+ assert_equal 'super_title', t.title
435
+ assert !t.changed?, "topic should not have changed"
436
+ assert !t.title_changed?, "title should not have changed"
437
+ assert_nil t.title_change, 'title change should be nil'
438
+
439
+ t.reload
440
+ assert_equal 'super_title', t.title
441
+ end
442
+
443
+ def test_update_attribute_for_updated_at_on
444
+ developer = Developer.find(1)
445
+ prev_month = Time.now.prev_month
446
+
447
+ developer.update_attribute(:updated_at, prev_month)
448
+ assert_equal prev_month, developer.updated_at
449
+
450
+ developer.update_attribute(:salary, 80001)
451
+ assert_not_equal prev_month, developer.updated_at
452
+
453
+ developer.reload
454
+ assert_not_equal prev_month, developer.updated_at
455
+ end
456
+
457
+ def test_update_column
458
+ topic = Topic.find(1)
459
+ topic.update_column("approved", true)
460
+ assert topic.approved?
461
+ topic.reload
462
+ assert topic.approved?
463
+
464
+ topic.update_column(:approved, false)
465
+ assert !topic.approved?
466
+ topic.reload
467
+ assert !topic.approved?
468
+ end
469
+
470
+ def test_update_column_should_not_use_setter_method
471
+ dev = Developer.find(1)
472
+ dev.instance_eval { def salary=(value); write_attribute(:salary, value * 2); end }
473
+
474
+ dev.update_column(:salary, 80000)
475
+ assert_equal 80000, dev.salary
476
+
477
+ dev.reload
478
+ assert_equal 80000, dev.salary
479
+ end
480
+
481
+ def test_update_column_should_raise_exception_if_new_record
482
+ topic = Topic.new
483
+ assert_raises(ActiveRecord::ActiveRecordError) { topic.update_column("approved", false) }
484
+ end
485
+
486
+ def test_update_column_should_not_leave_the_object_dirty
487
+ topic = Topic.find(1)
488
+ topic.update_column("content", "Have a nice day")
489
+
490
+ topic.reload
491
+ topic.update_column(:content, "You too")
492
+ assert_equal [], topic.changed
493
+
494
+ topic.reload
495
+ topic.update_column("content", "Have a nice day")
496
+ assert_equal [], topic.changed
497
+ end
498
+
499
+ def test_update_column_with_model_having_primary_key_other_than_id
500
+ minivan = Minivan.find('m1')
501
+ new_name = 'sebavan'
502
+
503
+ minivan.update_column(:name, new_name)
504
+ assert_equal new_name, minivan.name
505
+ end
506
+
507
+ def test_update_column_for_readonly_attribute
508
+ minivan = Minivan.find('m1')
509
+ prev_color = minivan.color
510
+ assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_column(:color, 'black') }
511
+ assert_equal prev_color, minivan.color
512
+ end
513
+
514
+ def test_update_column_should_not_modify_updated_at
515
+ developer = Developer.find(1)
516
+ prev_month = Time.now.prev_month
517
+
518
+ developer.update_column(:updated_at, prev_month)
519
+ assert_equal prev_month, developer.updated_at
520
+
521
+ developer.update_column(:salary, 80001)
522
+ assert_equal prev_month, developer.updated_at
523
+
524
+ developer.reload
525
+ assert_equal prev_month.to_i, developer.updated_at.to_i
526
+ end
527
+
528
+ def test_update_column_with_one_changed_and_one_updated
529
+ t = Topic.order('id').limit(1).first
530
+ author_name = t.author_name
531
+ t.author_name = 'John'
532
+ t.update_column(:title, 'super_title')
533
+ assert_equal 'John', t.author_name
534
+ assert_equal 'super_title', t.title
535
+ assert t.changed?, "topic should have changed"
536
+ assert t.author_name_changed?, "author_name should have changed"
537
+
538
+ t.reload
539
+ assert_equal author_name, t.author_name
540
+ assert_equal 'super_title', t.title
541
+ end
542
+
543
+ def test_update_column_with_default_scope
544
+ developer = DeveloperCalledDavid.first
545
+ developer.name = 'John'
546
+ developer.save!
547
+
548
+ assert developer.update_column(:name, 'Will'), 'did not update record due to default scope'
549
+ end
550
+
551
+ def test_update_columns
552
+ topic = Topic.find(1)
553
+ topic.update_columns({ "approved" => true, title: "Sebastian Topic" })
554
+ assert topic.approved?
555
+ assert_equal "Sebastian Topic", topic.title
556
+ topic.reload
557
+ assert topic.approved?
558
+ assert_equal "Sebastian Topic", topic.title
559
+ end
560
+
561
+ def test_update_columns_should_not_use_setter_method
562
+ dev = Developer.find(1)
563
+ dev.instance_eval { def salary=(value); write_attribute(:salary, value * 2); end }
564
+
565
+ dev.update_columns(salary: 80000)
566
+ assert_equal 80000, dev.salary
567
+
568
+ dev.reload
569
+ assert_equal 80000, dev.salary
570
+ end
571
+
572
+ def test_update_columns_should_raise_exception_if_new_record
573
+ topic = Topic.new
574
+ assert_raises(ActiveRecord::ActiveRecordError) { topic.update_columns({ approved: false }) }
575
+ end
576
+
577
+ def test_update_columns_should_not_leave_the_object_dirty
578
+ topic = Topic.find(1)
579
+ topic.update({ "content" => "Have a nice day", :author_name => "Jose" })
580
+
581
+ topic.reload
582
+ topic.update_columns({ content: "You too", "author_name" => "Sebastian" })
583
+ assert_equal [], topic.changed
584
+
585
+ topic.reload
586
+ topic.update_columns({ content: "Have a nice day", author_name: "Jose" })
587
+ assert_equal [], topic.changed
588
+ end
589
+
590
+ def test_update_columns_with_model_having_primary_key_other_than_id
591
+ minivan = Minivan.find('m1')
592
+ new_name = 'sebavan'
593
+
594
+ minivan.update_columns(name: new_name)
595
+ assert_equal new_name, minivan.name
596
+ end
597
+
598
+ def test_update_columns_with_one_readonly_attribute
599
+ minivan = Minivan.find('m1')
600
+ prev_color = minivan.color
601
+ prev_name = minivan.name
602
+ assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_columns({ name: "My old minivan", color: 'black' }) }
603
+ assert_equal prev_color, minivan.color
604
+ assert_equal prev_name, minivan.name
605
+
606
+ minivan.reload
607
+ assert_equal prev_color, minivan.color
608
+ assert_equal prev_name, minivan.name
609
+ end
610
+
611
+ def test_update_columns_should_not_modify_updated_at
612
+ developer = Developer.find(1)
613
+ prev_month = Time.now.prev_month
614
+
615
+ developer.update_columns(updated_at: prev_month)
616
+ assert_equal prev_month, developer.updated_at
617
+
618
+ developer.update_columns(salary: 80000)
619
+ assert_equal prev_month, developer.updated_at
620
+ assert_equal 80000, developer.salary
621
+
622
+ developer.reload
623
+ assert_equal prev_month.to_i, developer.updated_at.to_i
624
+ assert_equal 80000, developer.salary
625
+ end
626
+
627
+ def test_update_columns_with_one_changed_and_one_updated
628
+ t = Topic.order('id').limit(1).first
629
+ author_name = t.author_name
630
+ t.author_name = 'John'
631
+ t.update_columns(title: 'super_title')
632
+ assert_equal 'John', t.author_name
633
+ assert_equal 'super_title', t.title
634
+ assert t.changed?, "topic should have changed"
635
+ assert t.author_name_changed?, "author_name should have changed"
636
+
637
+ t.reload
638
+ assert_equal author_name, t.author_name
639
+ assert_equal 'super_title', t.title
640
+ end
641
+
642
+ def test_update_columns_changing_id
643
+ topic = Topic.find(1)
644
+ topic.update_columns(id: 123)
645
+ assert_equal 123, topic.id
646
+ topic.reload
647
+ assert_equal 123, topic.id
648
+ end
649
+
650
+ def test_update_columns_returns_boolean
651
+ topic = Topic.find(1)
652
+ assert_equal true, topic.update_columns(title: "New title")
653
+ end
654
+
655
+ def test_update_columns_with_default_scope
656
+ developer = DeveloperCalledDavid.first
657
+ developer.name = 'John'
658
+ developer.save!
659
+
660
+ assert developer.update_columns(name: 'Will'), 'did not update record due to default scope'
661
+ end
662
+
663
+ def test_update
664
+ topic = Topic.find(1)
665
+ assert !topic.approved?
666
+ assert_equal "The First Topic", topic.title
667
+
668
+ topic.update("approved" => true, "title" => "The First Topic Updated")
669
+ topic.reload
670
+ assert topic.approved?
671
+ assert_equal "The First Topic Updated", topic.title
672
+
673
+ topic.update(approved: false, title: "The First Topic")
674
+ topic.reload
675
+ assert !topic.approved?
676
+ assert_equal "The First Topic", topic.title
677
+ end
678
+
679
+ def test_update_attributes
680
+ topic = Topic.find(1)
681
+ assert !topic.approved?
682
+ assert_equal "The First Topic", topic.title
683
+
684
+ topic.update_attributes("approved" => true, "title" => "The First Topic Updated")
685
+ topic.reload
686
+ assert topic.approved?
687
+ assert_equal "The First Topic Updated", topic.title
688
+
689
+ topic.update_attributes(approved: false, title: "The First Topic")
690
+ topic.reload
691
+ assert !topic.approved?
692
+ assert_equal "The First Topic", topic.title
693
+
694
+ assert_raise(ActiveRecord::RecordNotUnique, ActiveRecord::StatementInvalid) do
695
+ topic.update_attributes(id: 3, title: "Hm is it possible?")
696
+ end
697
+ assert_not_equal "Hm is it possible?", Topic.find(3).title
698
+
699
+ topic.update_attributes(id: 1234)
700
+ assert_nothing_raised { topic.reload }
701
+ assert_equal topic.title, Topic.find(1234).title
702
+ end
703
+
704
+ def test_update!
705
+ Reply.validates_presence_of(:title)
706
+ reply = Reply.find(2)
707
+ assert_equal "The Second Topic of the day", reply.title
708
+ assert_equal "Have a nice day", reply.content
709
+
710
+ reply.update!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening")
711
+ reply.reload
712
+ assert_equal "The Second Topic of the day updated", reply.title
713
+ assert_equal "Have a nice evening", reply.content
714
+
715
+ reply.update!(title: "The Second Topic of the day", content: "Have a nice day")
716
+ reply.reload
717
+ assert_equal "The Second Topic of the day", reply.title
718
+ assert_equal "Have a nice day", reply.content
719
+
720
+ assert_raise(ActiveRecord::RecordInvalid) { reply.update!(title: nil, content: "Have a nice evening") }
721
+ ensure
722
+ Reply.reset_callbacks(:validate)
723
+ end
724
+
725
+ def test_update_attributes!
726
+ Reply.validates_presence_of(:title)
727
+ reply = Reply.find(2)
728
+ assert_equal "The Second Topic of the day", reply.title
729
+ assert_equal "Have a nice day", reply.content
730
+
731
+ reply.update_attributes!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening")
732
+ reply.reload
733
+ assert_equal "The Second Topic of the day updated", reply.title
734
+ assert_equal "Have a nice evening", reply.content
735
+
736
+ reply.update_attributes!(title: "The Second Topic of the day", content: "Have a nice day")
737
+ reply.reload
738
+ assert_equal "The Second Topic of the day", reply.title
739
+ assert_equal "Have a nice day", reply.content
740
+
741
+ assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(title: nil, content: "Have a nice evening") }
742
+ ensure
743
+ Reply.reset_callbacks(:validate)
744
+ end
745
+
746
+ def test_destroyed_returns_boolean
747
+ developer = Developer.first
748
+ assert_equal false, developer.destroyed?
749
+ developer.destroy
750
+ assert_equal true, developer.destroyed?
751
+
752
+ developer = Developer.last
753
+ assert_equal false, developer.destroyed?
754
+ developer.delete
755
+ assert_equal true, developer.destroyed?
756
+ end
757
+
758
+ def test_persisted_returns_boolean
759
+ developer = Developer.new(:name => "Jose")
760
+ assert_equal false, developer.persisted?
761
+ developer.save!
762
+ assert_equal true, developer.persisted?
763
+
764
+ developer = Developer.first
765
+ assert_equal true, developer.persisted?
766
+ developer.destroy
767
+ assert_equal false, developer.persisted?
768
+
769
+ developer = Developer.last
770
+ assert_equal true, developer.persisted?
771
+ developer.delete
772
+ assert_equal false, developer.persisted?
773
+ end
774
+
775
+ def test_class_level_destroy
776
+ should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
777
+ Topic.find(1).replies << should_be_destroyed_reply
778
+
779
+ Topic.destroy(1)
780
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
781
+ assert_raise(ActiveRecord::RecordNotFound) { Reply.find(should_be_destroyed_reply.id) }
782
+ end
783
+
784
+ def test_class_level_delete
785
+ should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
786
+ Topic.find(1).replies << should_be_destroyed_reply
787
+
788
+ Topic.delete(1)
789
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
790
+ assert_nothing_raised { Reply.find(should_be_destroyed_reply.id) }
791
+ end
792
+
793
+ def test_create_with_custom_timestamps
794
+ custom_datetime = 1.hour.ago.beginning_of_day
795
+
796
+ %w(created_at created_on updated_at updated_on).each do |attribute|
797
+ parrot = LiveParrot.create(:name => "colombian", attribute => custom_datetime)
798
+ assert_equal custom_datetime, parrot[attribute]
799
+ end
800
+ end
801
+
802
+ end