ibm_db 3.0.5-x86-mingw32 → 5.0.5-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGES +4 -0
- data/LICENSE +1 -1
- data/ParameterizedQueries README +6 -6
- data/README +38 -55
- data/ext/Makefile +269 -0
- data/ext/extconf.rb +34 -3
- data/ext/gil_release_version +3 -0
- data/ext/ibm_db-i386-mingw32.def +2 -0
- data/ext/ibm_db.c +100 -108
- data/ext/ibm_db.o +0 -0
- data/ext/ibm_db.so +0 -0
- data/ext/mkmf.log +110 -0
- data/ext/ruby_ibm_db_cli.o +0 -0
- data/ext/unicode_support_version +3 -0
- data/lib/active_record/connection_adapters/ibm_db_adapter.rb +923 -527
- data/lib/active_record/connection_adapters/ibmdb_adapter.rb +4 -1
- data/lib/mswin32/ibm_db.rb +7 -39
- data/lib/mswin32/rb2x/i386/ibm_db.so +0 -0
- data/test/active_record/connection_adapters/fake_adapter.rb +8 -5
- data/test/cases/adapter_test.rb +133 -58
- data/test/cases/adapters/mysql2/active_schema_test.rb +193 -0
- data/test/cases/adapters/mysql2/bind_parameter_test.rb +50 -0
- data/test/cases/adapters/mysql2/boolean_test.rb +100 -0
- data/test/cases/adapters/mysql2/case_sensitivity_test.rb +63 -0
- data/test/cases/adapters/mysql2/charset_collation_test.rb +54 -0
- data/test/cases/adapters/mysql2/connection_test.rb +210 -0
- data/test/cases/adapters/mysql2/datetime_precision_quoting_test.rb +45 -0
- data/test/cases/adapters/mysql2/enum_test.rb +26 -0
- data/test/cases/adapters/mysql2/explain_test.rb +21 -0
- data/test/cases/adapters/mysql2/json_test.rb +195 -0
- data/test/cases/adapters/mysql2/mysql2_adapter_test.rb +83 -0
- data/test/cases/adapters/mysql2/reserved_word_test.rb +152 -0
- data/test/cases/adapters/mysql2/schema_migrations_test.rb +59 -0
- data/test/cases/adapters/mysql2/schema_test.rb +126 -0
- data/test/cases/adapters/mysql2/sp_test.rb +36 -0
- data/test/cases/adapters/mysql2/sql_types_test.rb +14 -0
- data/test/cases/adapters/mysql2/table_options_test.rb +42 -0
- data/test/cases/adapters/mysql2/unsigned_type_test.rb +66 -0
- data/test/cases/adapters/postgresql/active_schema_test.rb +98 -0
- data/test/cases/adapters/postgresql/array_test.rb +339 -0
- data/test/cases/adapters/postgresql/bit_string_test.rb +82 -0
- data/test/cases/adapters/postgresql/bytea_test.rb +134 -0
- data/test/cases/adapters/postgresql/case_insensitive_test.rb +26 -0
- data/test/cases/adapters/postgresql/change_schema_test.rb +38 -0
- data/test/cases/adapters/postgresql/cidr_test.rb +25 -0
- data/test/cases/adapters/postgresql/citext_test.rb +78 -0
- data/test/cases/adapters/postgresql/collation_test.rb +53 -0
- data/test/cases/adapters/postgresql/composite_test.rb +132 -0
- data/test/cases/adapters/postgresql/connection_test.rb +257 -0
- data/test/cases/adapters/postgresql/datatype_test.rb +92 -0
- data/test/cases/adapters/postgresql/domain_test.rb +47 -0
- data/test/cases/adapters/postgresql/enum_test.rb +91 -0
- data/test/cases/adapters/postgresql/explain_test.rb +20 -0
- data/test/cases/adapters/postgresql/extension_migration_test.rb +63 -0
- data/test/cases/adapters/postgresql/full_text_test.rb +44 -0
- data/test/cases/adapters/postgresql/geometric_test.rb +378 -0
- data/test/cases/adapters/postgresql/hstore_test.rb +382 -0
- data/test/cases/adapters/postgresql/infinity_test.rb +69 -0
- data/test/cases/adapters/postgresql/integer_test.rb +25 -0
- data/test/cases/adapters/postgresql/json_test.rb +237 -0
- data/test/cases/adapters/postgresql/ltree_test.rb +53 -0
- data/test/cases/adapters/postgresql/money_test.rb +96 -0
- data/test/cases/adapters/postgresql/network_test.rb +94 -0
- data/test/cases/adapters/postgresql/numbers_test.rb +49 -0
- data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +405 -0
- data/test/cases/adapters/postgresql/prepared_statements_test.rb +22 -0
- data/test/cases/adapters/postgresql/quoting_test.rb +44 -0
- data/test/cases/adapters/postgresql/range_test.rb +343 -0
- data/test/cases/adapters/postgresql/referential_integrity_test.rb +111 -0
- data/test/cases/adapters/postgresql/rename_table_test.rb +34 -0
- data/test/cases/adapters/postgresql/schema_authorization_test.rb +119 -0
- data/test/cases/adapters/postgresql/schema_test.rb +597 -0
- data/test/cases/adapters/postgresql/serial_test.rb +154 -0
- data/test/cases/adapters/postgresql/statement_pool_test.rb +41 -0
- data/test/cases/adapters/postgresql/timestamp_test.rb +90 -0
- data/test/cases/adapters/postgresql/type_lookup_test.rb +33 -0
- data/test/cases/adapters/postgresql/utils_test.rb +62 -0
- data/test/cases/adapters/postgresql/uuid_test.rb +294 -0
- data/test/cases/adapters/postgresql/xml_test.rb +54 -0
- data/test/cases/adapters/sqlite3/collation_test.rb +53 -0
- data/test/cases/adapters/sqlite3/copy_table_test.rb +98 -0
- data/test/cases/adapters/sqlite3/explain_test.rb +21 -0
- data/test/cases/adapters/sqlite3/quoting_test.rb +101 -0
- data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +441 -0
- data/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb +24 -0
- data/test/cases/adapters/sqlite3/statement_pool_test.rb +20 -0
- data/test/cases/aggregations_test.rb +11 -1
- data/test/cases/ar_schema_test.rb +35 -50
- data/test/cases/associations/association_scope_test.rb +1 -6
- data/test/cases/associations/belongs_to_associations_test.rb +122 -10
- data/test/cases/associations/bidirectional_destroy_dependencies_test.rb +41 -0
- data/test/cases/associations/callbacks_test.rb +5 -7
- data/test/cases/associations/cascaded_eager_loading_test.rb +1 -1
- data/test/cases/associations/eager_load_nested_include_test.rb +1 -3
- data/test/cases/associations/eager_test.rb +158 -73
- data/test/cases/associations/extension_test.rb +7 -2
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +64 -32
- data/test/cases/associations/has_many_associations_test.rb +362 -43
- data/test/cases/associations/has_many_through_associations_test.rb +108 -41
- data/test/cases/associations/has_one_associations_test.rb +105 -8
- data/test/cases/associations/has_one_through_associations_test.rb +6 -3
- data/test/cases/associations/inner_join_association_test.rb +3 -3
- data/test/cases/associations/inverse_associations_test.rb +38 -11
- data/test/cases/associations/join_model_test.rb +59 -36
- data/test/cases/associations/left_outer_join_association_test.rb +88 -0
- data/test/cases/associations/nested_through_associations_test.rb +2 -2
- data/test/cases/associations/required_test.rb +25 -5
- data/test/cases/associations_test.rb +39 -34
- data/test/cases/attribute_decorators_test.rb +9 -8
- data/test/cases/attribute_methods/read_test.rb +5 -5
- data/test/cases/attribute_methods_test.rb +97 -40
- data/test/cases/attribute_set_test.rb +64 -4
- data/test/cases/attribute_test.rb +84 -18
- data/test/cases/attributes_test.rb +151 -34
- data/test/cases/autosave_association_test.rb +149 -36
- data/test/cases/base_test.rb +290 -241
- data/test/cases/batches_test.rb +299 -22
- data/test/cases/binary_test.rb +2 -10
- data/test/cases/bind_parameter_test.rb +76 -66
- data/test/cases/cache_key_test.rb +26 -0
- data/test/cases/calculations_test.rb +167 -15
- data/test/cases/callbacks_test.rb +161 -68
- data/test/cases/coders/json_test.rb +15 -0
- data/test/cases/collection_cache_key_test.rb +115 -0
- data/test/cases/column_definition_test.rb +26 -57
- data/test/cases/comment_test.rb +145 -0
- data/test/cases/connection_adapters/adapter_leasing_test.rb +5 -3
- data/test/cases/connection_adapters/connection_handler_test.rb +128 -21
- data/test/cases/connection_adapters/connection_specification_test.rb +1 -1
- data/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb +0 -38
- data/test/cases/connection_adapters/mysql_type_lookup_test.rb +5 -1
- data/test/cases/connection_adapters/schema_cache_test.rb +8 -3
- data/test/cases/connection_adapters/type_lookup_test.rb +15 -7
- data/test/cases/connection_management_test.rb +46 -56
- data/test/cases/connection_pool_test.rb +195 -20
- data/test/cases/connection_specification/resolver_test.rb +15 -0
- data/test/cases/counter_cache_test.rb +10 -5
- data/test/cases/custom_locking_test.rb +1 -1
- data/test/cases/database_statements_test.rb +18 -3
- data/test/cases/{invalid_date_test.rb → date_test.rb} +13 -1
- data/test/cases/date_time_precision_test.rb +107 -0
- data/test/cases/defaults_test.rb +85 -89
- data/test/cases/dirty_test.rb +30 -52
- data/test/cases/disconnected_test.rb +4 -2
- data/test/cases/enum_test.rb +178 -24
- data/test/cases/errors_test.rb +16 -0
- data/test/cases/explain_test.rb +32 -21
- data/test/cases/finder_test.rb +273 -148
- data/test/cases/fixture_set/file_test.rb +18 -0
- data/test/cases/fixtures_test.rb +112 -32
- data/test/cases/forbidden_attributes_protection_test.rb +69 -3
- data/test/cases/helper.rb +10 -16
- data/test/cases/hot_compatibility_test.rb +89 -1
- data/test/cases/inheritance_test.rb +284 -53
- data/test/cases/integration_test.rb +23 -7
- data/test/cases/invalid_connection_test.rb +4 -2
- data/test/cases/invertible_migration_test.rb +124 -32
- data/test/cases/json_serialization_test.rb +11 -2
- data/test/cases/locking_test.rb +22 -6
- data/test/cases/log_subscriber_test.rb +106 -17
- data/test/cases/migration/change_schema_test.rb +60 -114
- data/test/cases/migration/change_table_test.rb +34 -2
- data/test/cases/migration/column_attributes_test.rb +7 -23
- data/test/cases/migration/column_positioning_test.rb +8 -8
- data/test/cases/migration/columns_test.rb +17 -11
- data/test/cases/migration/command_recorder_test.rb +47 -2
- data/test/cases/migration/compatibility_test.rb +118 -0
- data/test/cases/migration/create_join_table_test.rb +21 -12
- data/test/cases/migration/foreign_key_test.rb +52 -18
- data/test/cases/migration/index_test.rb +14 -12
- data/test/cases/migration/logger_test.rb +1 -1
- data/test/cases/migration/pending_migrations_test.rb +0 -1
- data/test/cases/migration/references_foreign_key_test.rb +59 -7
- data/test/cases/migration/references_index_test.rb +4 -4
- data/test/cases/migration/references_statements_test.rb +26 -6
- data/test/cases/migration/rename_table_test.rb +25 -25
- data/test/cases/migration_test.rb +279 -81
- data/test/cases/migrator_test.rb +91 -8
- data/test/cases/mixin_test.rb +0 -2
- data/test/cases/modules_test.rb +3 -4
- data/test/cases/multiparameter_attributes_test.rb +24 -2
- data/test/cases/multiple_db_test.rb +11 -4
- data/test/cases/nested_attributes_test.rb +61 -33
- data/test/cases/persistence_test.rb +102 -10
- data/test/cases/pooled_connections_test.rb +3 -3
- data/test/cases/primary_keys_test.rb +170 -31
- data/test/cases/query_cache_test.rb +216 -96
- data/test/cases/quoting_test.rb +65 -19
- data/test/cases/readonly_test.rb +2 -1
- data/test/cases/reflection_test.rb +68 -22
- data/test/cases/relation/delegation_test.rb +3 -8
- data/test/cases/relation/merging_test.rb +10 -14
- data/test/cases/relation/mutation_test.rb +42 -24
- data/test/cases/relation/or_test.rb +92 -0
- data/test/cases/relation/predicate_builder_test.rb +4 -2
- data/test/cases/relation/record_fetch_warning_test.rb +40 -0
- data/test/cases/relation/where_chain_test.rb +23 -99
- data/test/cases/relation/where_clause_test.rb +182 -0
- data/test/cases/relation/where_test.rb +45 -23
- data/test/cases/relation_test.rb +67 -58
- data/test/cases/relations_test.rb +249 -38
- data/test/cases/result_test.rb +10 -0
- data/test/cases/sanitize_test.rb +108 -15
- data/test/cases/schema_dumper_test.rb +119 -125
- data/test/cases/schema_loading_test.rb +52 -0
- data/test/cases/scoping/default_scoping_test.rb +113 -39
- data/test/cases/scoping/named_scoping_test.rb +46 -9
- data/test/cases/scoping/relation_scoping_test.rb +47 -4
- data/test/cases/secure_token_test.rb +32 -0
- data/test/cases/serialization_test.rb +1 -1
- data/test/cases/serialized_attribute_test.rb +93 -6
- data/test/cases/statement_cache_test.rb +38 -0
- data/test/cases/store_test.rb +2 -1
- data/test/cases/suppressor_test.rb +63 -0
- data/test/cases/tasks/database_tasks_test.rb +73 -9
- data/test/cases/tasks/mysql_rake_test.rb +139 -118
- data/test/cases/tasks/postgresql_rake_test.rb +60 -6
- data/test/cases/tasks/sqlite_rake_test.rb +30 -3
- data/test/cases/test_case.rb +28 -20
- data/test/cases/test_fixtures_test.rb +36 -0
- data/test/cases/time_precision_test.rb +103 -0
- data/test/cases/timestamp_test.rb +44 -10
- data/test/cases/touch_later_test.rb +121 -0
- data/test/cases/transaction_callbacks_test.rb +128 -62
- data/test/cases/transaction_isolation_test.rb +2 -2
- data/test/cases/transactions_test.rb +61 -43
- data/test/cases/type/adapter_specific_registry_test.rb +133 -0
- data/test/cases/type/date_time_test.rb +14 -0
- data/test/cases/type/integer_test.rb +2 -96
- data/test/cases/type/string_test.rb +0 -14
- data/test/cases/type_test.rb +39 -0
- data/test/cases/types_test.rb +1 -118
- data/test/cases/unconnected_test.rb +1 -1
- data/test/cases/validations/absence_validation_test.rb +73 -0
- data/test/cases/validations/association_validation_test.rb +13 -2
- data/test/cases/validations/i18n_validation_test.rb +6 -10
- data/test/cases/validations/length_validation_test.rb +62 -30
- data/test/cases/validations/presence_validation_test.rb +36 -1
- data/test/cases/validations/uniqueness_validation_test.rb +128 -37
- data/test/cases/validations_repair_helper.rb +2 -6
- data/test/cases/validations_test.rb +36 -7
- data/test/cases/view_test.rb +102 -5
- data/test/cases/yaml_serialization_test.rb +21 -26
- data/test/config.example.yml +97 -0
- data/test/fixtures/bad_posts.yml +9 -0
- data/test/fixtures/books.yml +20 -0
- data/test/fixtures/content.yml +3 -0
- data/test/fixtures/content_positions.yml +3 -0
- data/test/fixtures/dead_parrots.yml +5 -0
- data/test/fixtures/live_parrots.yml +4 -0
- data/test/fixtures/naked/yml/parrots.yml +2 -0
- data/test/fixtures/naked/yml/trees.yml +3 -0
- data/test/fixtures/nodes.yml +29 -0
- data/test/fixtures/other_comments.yml +6 -0
- data/test/fixtures/other_dogs.yml +2 -0
- data/test/fixtures/other_posts.yml +7 -0
- data/test/fixtures/price_estimates.yml +10 -1
- data/test/fixtures/trees.yml +3 -0
- data/test/migrations/10_urban/9_add_expressions.rb +1 -1
- data/test/migrations/decimal/1_give_me_big_numbers.rb +1 -1
- data/test/migrations/magic/1_currencies_have_symbols.rb +1 -1
- data/test/migrations/missing/1000_people_have_middle_names.rb +2 -2
- data/test/migrations/missing/1_people_have_last_names.rb +2 -2
- data/test/migrations/missing/3_we_need_reminders.rb +2 -2
- data/test/migrations/missing/4_innocent_jointable.rb +2 -2
- data/test/migrations/rename/1_we_need_things.rb +2 -2
- data/test/migrations/rename/2_rename_things.rb +2 -2
- data/test/migrations/to_copy/1_people_have_hobbies.rb +1 -1
- data/test/migrations/to_copy/2_people_have_descriptions.rb +1 -1
- data/test/migrations/to_copy2/1_create_articles.rb +1 -1
- data/test/migrations/to_copy2/2_create_comments.rb +1 -1
- data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +1 -1
- data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +1 -1
- data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +1 -1
- data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +1 -1
- data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +1 -1
- data/test/migrations/valid/1_valid_people_have_last_names.rb +1 -1
- data/test/migrations/valid/2_we_need_reminders.rb +2 -2
- data/test/migrations/valid/3_innocent_jointable.rb +2 -2
- data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +1 -1
- data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +2 -2
- data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +2 -2
- data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +1 -1
- data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +1 -1
- data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +1 -1
- data/test/migrations/version_check/20131219224947_migration_version_check.rb +1 -1
- data/test/models/admin/randomly_named_c1.rb +6 -2
- data/test/models/aircraft.rb +1 -0
- data/test/models/author.rb +4 -7
- data/test/models/bird.rb +1 -1
- data/test/models/book.rb +5 -0
- data/test/models/bulb.rb +2 -1
- data/test/models/car.rb +3 -0
- data/test/models/cat.rb +10 -0
- data/test/models/chef.rb +1 -0
- data/test/models/club.rb +2 -0
- data/test/models/comment.rb +17 -5
- data/test/models/company.rb +4 -2
- data/test/models/company_in_module.rb +1 -1
- data/test/models/contact.rb +1 -1
- data/test/models/content.rb +40 -0
- data/test/models/customer.rb +8 -2
- data/test/models/developer.rb +19 -0
- data/test/models/face.rb +1 -1
- data/test/models/guitar.rb +4 -0
- data/test/models/hotel.rb +2 -0
- data/test/models/member.rb +1 -0
- data/test/models/member_detail.rb +4 -3
- data/test/models/mentor.rb +3 -0
- data/test/models/mocktail_designer.rb +2 -0
- data/test/models/node.rb +5 -0
- data/test/models/non_primary_key.rb +2 -0
- data/test/models/notification.rb +3 -0
- data/test/models/other_dog.rb +5 -0
- data/test/models/owner.rb +4 -1
- data/test/models/parrot.rb +6 -7
- data/test/models/person.rb +0 -1
- data/test/models/pet.rb +3 -0
- data/test/models/pet_treasure.rb +6 -0
- data/test/models/pirate.rb +3 -3
- data/test/models/post.rb +18 -9
- data/test/models/project.rb +9 -0
- data/test/models/randomly_named_c1.rb +1 -1
- data/test/models/recipe.rb +3 -0
- data/test/models/ship.rb +8 -2
- data/test/models/tag.rb +6 -0
- data/test/models/topic.rb +2 -8
- data/test/models/tree.rb +3 -0
- data/test/models/tuning_peg.rb +4 -0
- data/test/models/user.rb +14 -0
- data/test/models/uuid_item.rb +6 -0
- data/test/schema/mysql2_specific_schema.rb +33 -23
- data/test/schema/oracle_specific_schema.rb +1 -4
- data/test/schema/postgresql_specific_schema.rb +36 -124
- data/test/schema/schema.rb +170 -65
- data/test/schema/schema.rb.original +1057 -0
- data/test/schema/sqlite_specific_schema.rb +1 -5
- data/test/support/connection.rb +1 -0
- data/test/support/schema_dumping_helper.rb +1 -1
- data/test/support/yaml_compatibility_fixtures/rails_4_1.yml +22 -0
- data/test/support/yaml_compatibility_fixtures/rails_4_2_0.yml +182 -0
- metadata +146 -30
- data/lib/mswin32/rb19x/ibm_db.so +0 -0
- data/lib/mswin32/rb21x/i386/ibm_db.so +0 -0
- data/lib/mswin32/rb22x/i386/ibm_db.so +0 -0
- data/lib/mswin32/rb23x/i386/ibm_db.so +0 -0
- data/test/cases/associations/deprecated_counter_cache_on_has_many_through_test.rb +0 -26
- data/test/cases/attribute_methods/serialization_test.rb +0 -29
- data/test/cases/migration/change_schema_test - Copy.rb +0 -448
- data/test/cases/migration/foreign_key_test - Changed.rb +0 -325
- data/test/cases/migration/table_and_index_test.rb +0 -24
- data/test/cases/relation/where_test2.rb +0 -36
- data/test/cases/type/decimal_test.rb +0 -56
- data/test/cases/type/unsigned_integer_test.rb +0 -18
- data/test/cases/xml_serialization_test.rb +0 -457
- data/test/fixtures/naked/csv/accounts.csv +0 -1
- data/test/schema/mysql_specific_schema.rb +0 -70
@@ -0,0 +1,26 @@
|
|
1
|
+
require "cases/helper"
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class CacheKeyTest < ActiveRecord::TestCase
|
5
|
+
self.use_transactional_tests = false
|
6
|
+
|
7
|
+
class CacheMe < ActiveRecord::Base; end
|
8
|
+
|
9
|
+
setup do
|
10
|
+
@connection = ActiveRecord::Base.connection
|
11
|
+
@connection.create_table(:cache_mes) { |t| t.timestamps }
|
12
|
+
end
|
13
|
+
|
14
|
+
teardown do
|
15
|
+
@connection.drop_table :cache_mes
|
16
|
+
#, if_exists: true
|
17
|
+
end
|
18
|
+
|
19
|
+
test "test_cache_key_format_is_not_too_precise" do
|
20
|
+
record = CacheMe.create
|
21
|
+
key = record.cache_key
|
22
|
+
|
23
|
+
assert_equal key, record.reload.cache_key
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require "cases/helper"
|
2
|
+
require "models/book"
|
2
3
|
require 'models/club'
|
3
4
|
require 'models/company'
|
4
5
|
require "models/contract"
|
@@ -19,29 +20,37 @@ require 'models/post'
|
|
19
20
|
class NumericData < ActiveRecord::Base
|
20
21
|
self.table_name = 'numeric_data'
|
21
22
|
|
22
|
-
attribute :world_population,
|
23
|
-
attribute :my_house_population,
|
24
|
-
attribute :atoms_in_universe,
|
23
|
+
attribute :world_population, :integer
|
24
|
+
attribute :my_house_population, :integer
|
25
|
+
attribute :atoms_in_universe, :integer
|
25
26
|
end
|
26
27
|
|
27
28
|
class CalculationsTest < ActiveRecord::TestCase
|
28
|
-
fixtures :companies, :accounts, :topics, :speedometers, :minivans
|
29
|
+
fixtures :companies, :accounts, :topics, :speedometers, :minivans, :books
|
29
30
|
|
30
31
|
def test_should_sum_field
|
31
32
|
assert_equal 318, Account.sum(:credit_limit)
|
32
33
|
end
|
33
34
|
|
35
|
+
def test_should_sum_arel_attribute
|
36
|
+
assert_equal 318, Account.sum(Account.arel_table[:credit_limit])
|
37
|
+
end
|
38
|
+
|
34
39
|
def test_should_average_field
|
35
40
|
value = Account.average(:credit_limit)
|
36
41
|
assert_equal 53.0, value
|
37
42
|
end
|
38
43
|
|
44
|
+
def test_should_average_arel_attribute
|
45
|
+
value = Account.average(Account.arel_table[:credit_limit])
|
46
|
+
assert_equal 53.0, value
|
47
|
+
end
|
48
|
+
|
39
49
|
def test_should_resolve_aliased_attributes
|
40
50
|
assert_equal 318, Account.sum(:available_credit)
|
41
51
|
end
|
42
52
|
|
43
53
|
def test_should_return_decimal_average_of_integer_field
|
44
|
-
return if current_adapter?(:IBM_DBAdapter) #average cannot be a decimal value when applied on integer field
|
45
54
|
value = Account.average(:id)
|
46
55
|
assert_equal 3.5, value
|
47
56
|
end
|
@@ -61,14 +70,26 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
61
70
|
assert_equal 60, Account.maximum(:credit_limit)
|
62
71
|
end
|
63
72
|
|
73
|
+
def test_should_get_maximum_of_arel_attribute
|
74
|
+
assert_equal 60, Account.maximum(Account.arel_table[:credit_limit])
|
75
|
+
end
|
76
|
+
|
64
77
|
def test_should_get_maximum_of_field_with_include
|
65
78
|
assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).includes(:firm).maximum(:credit_limit)
|
66
79
|
end
|
67
80
|
|
81
|
+
def test_should_get_maximum_of_arel_attribute_with_include
|
82
|
+
assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).includes(:firm).maximum(Account.arel_table[:credit_limit])
|
83
|
+
end
|
84
|
+
|
68
85
|
def test_should_get_minimum_of_field
|
69
86
|
assert_equal 50, Account.minimum(:credit_limit)
|
70
87
|
end
|
71
88
|
|
89
|
+
def test_should_get_minimum_of_arel_attribute
|
90
|
+
assert_equal 50, Account.minimum(Account.arel_table[:credit_limit])
|
91
|
+
end
|
92
|
+
|
72
93
|
def test_should_group_by_field
|
73
94
|
c = Account.group(:firm_id).sum(:credit_limit)
|
74
95
|
[1,6,2].each do |firm_id|
|
@@ -103,6 +124,25 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
103
124
|
assert_equal 60, c[2]
|
104
125
|
end
|
105
126
|
|
127
|
+
def test_should_generate_valid_sql_with_joins_and_group
|
128
|
+
assert_nothing_raised do
|
129
|
+
AuditLog.joins(:developer).group(:id).count
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_should_calculate_against_given_relation
|
134
|
+
developer = Developer.create!(name: "developer")
|
135
|
+
developer.audit_logs.create!(message: "first log")
|
136
|
+
developer.audit_logs.create!(message: "second log")
|
137
|
+
|
138
|
+
c = developer.audit_logs.joins(:developer).group(:id).count
|
139
|
+
|
140
|
+
assert_equal developer.audit_logs.count, c.size
|
141
|
+
developer.audit_logs.each do |log|
|
142
|
+
assert_equal 1, c[log.id]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
106
146
|
def test_should_order_by_grouped_field
|
107
147
|
c = Account.group(:firm_id).order("firm_id").sum(:credit_limit)
|
108
148
|
assert_equal [1, 2, 6, 9], c.keys.compact
|
@@ -132,6 +172,14 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
132
172
|
assert_equal 3, accounts.select(:firm_id).count
|
133
173
|
end
|
134
174
|
|
175
|
+
def test_limit_should_apply_before_count_arel_attribute
|
176
|
+
accounts = Account.limit(3).where('firm_id IS NOT NULL')
|
177
|
+
|
178
|
+
firm_id_attribute = Account.arel_table[:firm_id]
|
179
|
+
assert_equal 3, accounts.count(firm_id_attribute)
|
180
|
+
assert_equal 3, accounts.select(firm_id_attribute).count
|
181
|
+
end
|
182
|
+
|
135
183
|
def test_count_should_shortcut_with_limit_zero
|
136
184
|
accounts = Account.limit(0)
|
137
185
|
|
@@ -139,7 +187,7 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
139
187
|
end
|
140
188
|
|
141
189
|
def test_limit_is_kept
|
142
|
-
return if current_adapter?(:OracleAdapter)
|
190
|
+
return if current_adapter?(:OracleAdapter)
|
143
191
|
|
144
192
|
queries = assert_sql { Account.limit(1).count }
|
145
193
|
assert_equal 1, queries.length
|
@@ -147,7 +195,7 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
147
195
|
end
|
148
196
|
|
149
197
|
def test_offset_is_kept
|
150
|
-
return if current_adapter?(:OracleAdapter)
|
198
|
+
return if current_adapter?(:OracleAdapter)
|
151
199
|
|
152
200
|
queries = assert_sql { Account.offset(1).count }
|
153
201
|
assert_equal 1, queries.length
|
@@ -155,7 +203,7 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
155
203
|
end
|
156
204
|
|
157
205
|
def test_limit_with_offset_is_kept
|
158
|
-
return if current_adapter?(:OracleAdapter)
|
206
|
+
return if current_adapter?(:OracleAdapter)
|
159
207
|
|
160
208
|
queries = assert_sql { Account.limit(1).offset(1).count }
|
161
209
|
assert_equal 1, queries.length
|
@@ -187,7 +235,8 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
187
235
|
end
|
188
236
|
|
189
237
|
def test_should_group_by_summed_field_having_condition_from_select
|
190
|
-
|
238
|
+
skip unless current_adapter?(:Mysql2Adapter, :SQLite3Adapter)
|
239
|
+
c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit)
|
191
240
|
assert_nil c[1]
|
192
241
|
assert_equal 60, c[2]
|
193
242
|
assert_equal 53, c[9]
|
@@ -354,13 +403,29 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
354
403
|
assert_equal 6, Account.select("DISTINCT accounts.id").includes(:firm).count
|
355
404
|
end
|
356
405
|
|
406
|
+
def test_count_selected_arel_attribute
|
407
|
+
assert_equal 5, Account.select(Account.arel_table[:firm_id]).count
|
408
|
+
assert_equal 4, Account.distinct.select(Account.arel_table[:firm_id]).count
|
409
|
+
end
|
410
|
+
|
357
411
|
def test_count_with_column_parameter
|
358
412
|
assert_equal 5, Account.count(:firm_id)
|
359
413
|
end
|
360
414
|
|
415
|
+
def test_count_with_arel_attribute
|
416
|
+
assert_equal 5, Account.count(Account.arel_table[:firm_id])
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_count_with_arel_star
|
420
|
+
assert_equal 6, Account.count(Arel.star)
|
421
|
+
end
|
422
|
+
|
361
423
|
def test_count_with_distinct
|
362
424
|
assert_equal 4, Account.select(:credit_limit).distinct.count
|
363
|
-
|
425
|
+
|
426
|
+
assert_deprecated do
|
427
|
+
assert_equal 4, Account.select(:credit_limit).uniq.count
|
428
|
+
end
|
364
429
|
end
|
365
430
|
|
366
431
|
def test_count_with_aliased_attribute
|
@@ -376,12 +441,27 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
376
441
|
assert_equal 4, Account.joins(:firm).distinct.count('companies.id')
|
377
442
|
end
|
378
443
|
|
444
|
+
def test_count_arel_attribute_in_joined_table_with
|
445
|
+
assert_equal 5, Account.joins(:firm).count(Company.arel_table[:id])
|
446
|
+
assert_equal 4, Account.joins(:firm).distinct.count(Company.arel_table[:id])
|
447
|
+
end
|
448
|
+
|
449
|
+
def test_count_selected_arel_attribute_in_joined_table
|
450
|
+
assert_equal 5, Account.joins(:firm).select(Company.arel_table[:id]).count
|
451
|
+
assert_equal 4, Account.joins(:firm).distinct.select(Company.arel_table[:id]).count
|
452
|
+
end
|
453
|
+
|
379
454
|
def test_should_count_field_in_joined_table_with_group_by
|
380
455
|
c = Account.group('accounts.firm_id').joins(:firm).count('companies.id')
|
381
456
|
|
382
457
|
[1,6,2,9].each { |firm_id| assert c.keys.include?(firm_id) }
|
383
458
|
end
|
384
459
|
|
460
|
+
def test_should_count_field_of_root_table_with_conflicting_group_by_column
|
461
|
+
assert_equal({ 1 => 1 }, Firm.joins(:accounts).group(:firm_id).count)
|
462
|
+
assert_equal({ 1 => 1 }, Firm.joins(:accounts).group('accounts.firm_id').count)
|
463
|
+
end
|
464
|
+
|
385
465
|
def test_count_with_no_parameters_isnt_deprecated
|
386
466
|
assert_not_deprecated { Account.count }
|
387
467
|
end
|
@@ -467,8 +547,8 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
467
547
|
assert_equal 7, Company.includes(:contracts).sum(:developer_id)
|
468
548
|
end
|
469
549
|
|
470
|
-
|
471
|
-
|
550
|
+
if current_adapter?(:Mysql2Adapter)
|
551
|
+
def test_from_option_with_specified_index
|
472
552
|
assert_equal Edge.count(:all), Edge.from('edges USE INDEX(unique_edge_index)').count(:all)
|
473
553
|
assert_equal Edge.where('sink_id < 5').count(:all),
|
474
554
|
Edge.from('edges USE INDEX(unique_edge_index)').where('sink_id < 5').count(:all)
|
@@ -505,8 +585,8 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
505
585
|
assert_equal [ topic.written_on ], relation.pluck(:written_on)
|
506
586
|
end
|
507
587
|
|
508
|
-
def
|
509
|
-
assert_equal [50, 53, 55, 60], Account.order(:credit_limit).
|
588
|
+
def test_pluck_and_distinct
|
589
|
+
assert_equal [50, 53, 55, 60], Account.order(:credit_limit).distinct.pluck(:credit_limit)
|
510
590
|
end
|
511
591
|
|
512
592
|
def test_pluck_in_relation
|
@@ -630,6 +710,27 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
630
710
|
assert_equal [part.id], ShipPart.joins(:trinkets).pluck(:id)
|
631
711
|
end
|
632
712
|
|
713
|
+
def test_pluck_loaded_relation
|
714
|
+
companies = Company.order(:id).limit(3).load
|
715
|
+
assert_no_queries do
|
716
|
+
assert_equal ['37signals', 'Summit', 'Microsoft'], companies.pluck(:name)
|
717
|
+
end
|
718
|
+
end
|
719
|
+
|
720
|
+
def test_pluck_loaded_relation_multiple_columns
|
721
|
+
companies = Company.order(:id).limit(3).load
|
722
|
+
assert_no_queries do
|
723
|
+
assert_equal [[1, '37signals'], [2, 'Summit'], [3, 'Microsoft']], companies.pluck(:id, :name)
|
724
|
+
end
|
725
|
+
end
|
726
|
+
|
727
|
+
def test_pluck_loaded_relation_sql_fragment
|
728
|
+
companies = Company.order(:name).limit(3).load
|
729
|
+
assert_queries 1 do
|
730
|
+
assert_equal ['37signals', 'Apex', 'Ex Nihilo'], companies.pluck('DISTINCT name')
|
731
|
+
end
|
732
|
+
end
|
733
|
+
|
633
734
|
def test_grouped_calculation_with_polymorphic_relation
|
634
735
|
part = ShipPart.create!(name: "has trinket")
|
635
736
|
part.trinkets.create!
|
@@ -637,10 +738,61 @@ class CalculationsTest < ActiveRecord::TestCase
|
|
637
738
|
assert_equal({ "has trinket" => part.id }, ShipPart.joins(:trinkets).group("ship_parts.name").sum(:id))
|
638
739
|
end
|
639
740
|
|
741
|
+
def test_calculation_grouped_by_association_doesnt_error_when_no_records_have_association
|
742
|
+
Client.update_all(client_of: nil)
|
743
|
+
assert_equal({ nil => Client.count }, Client.group(:firm).count)
|
744
|
+
end
|
745
|
+
|
640
746
|
def test_should_reference_correct_aliases_while_joining_tables_of_has_many_through_association
|
641
|
-
assert_nothing_raised
|
747
|
+
assert_nothing_raised do
|
642
748
|
developer = Developer.create!(name: 'developer')
|
643
749
|
developer.ratings.includes(comment: :post).where(posts: { id: 1 }).count
|
644
750
|
end
|
645
751
|
end
|
752
|
+
|
753
|
+
def test_sum_uses_enumerable_version_when_block_is_given
|
754
|
+
block_called = false
|
755
|
+
relation = Client.all.load
|
756
|
+
|
757
|
+
assert_no_queries do
|
758
|
+
assert_equal 0, relation.sum { block_called = true; 0 }
|
759
|
+
end
|
760
|
+
assert block_called
|
761
|
+
end
|
762
|
+
|
763
|
+
def test_having_with_strong_parameters
|
764
|
+
protected_params = Class.new do
|
765
|
+
attr_reader :permitted
|
766
|
+
alias :permitted? :permitted
|
767
|
+
|
768
|
+
def initialize(parameters)
|
769
|
+
@parameters = parameters
|
770
|
+
@permitted = false
|
771
|
+
end
|
772
|
+
|
773
|
+
def to_h
|
774
|
+
@parameters
|
775
|
+
end
|
776
|
+
|
777
|
+
def permit!
|
778
|
+
@permitted = true
|
779
|
+
self
|
780
|
+
end
|
781
|
+
end
|
782
|
+
|
783
|
+
params = protected_params.new(credit_limit: '50')
|
784
|
+
|
785
|
+
assert_raises(ActiveModel::ForbiddenAttributesError) do
|
786
|
+
Account.group(:id).having(params)
|
787
|
+
end
|
788
|
+
|
789
|
+
result = Account.group(:id).having(params.permit!)
|
790
|
+
assert_equal 50, result[0].credit_limit
|
791
|
+
assert_equal 50, result[1].credit_limit
|
792
|
+
assert_equal 50, result[2].credit_limit
|
793
|
+
end
|
794
|
+
|
795
|
+
def test_group_by_attribute_with_custom_type
|
796
|
+
assert_equal({ "proposed" => 2, "published" => 2 }, Book.group(:status).count)
|
797
|
+
end
|
646
798
|
end
|
@@ -33,7 +33,7 @@ class CallbackDeveloper < ActiveRecord::Base
|
|
33
33
|
ActiveRecord::Callbacks::CALLBACKS.each do |callback_method|
|
34
34
|
next if callback_method.to_s =~ /^around_/
|
35
35
|
define_callback_method(callback_method)
|
36
|
-
send(callback_method, callback_string(callback_method))
|
36
|
+
ActiveSupport::Deprecation.silence { send(callback_method, callback_string(callback_method)) }
|
37
37
|
send(callback_method, callback_proc(callback_method))
|
38
38
|
send(callback_method, callback_object(callback_method))
|
39
39
|
send(callback_method) { |model| model.history << [callback_method, :block] }
|
@@ -49,6 +49,11 @@ class CallbackDeveloperWithFalseValidation < CallbackDeveloper
|
|
49
49
|
before_validation proc { |model| model.history << [:before_validation, :should_never_get_here] }
|
50
50
|
end
|
51
51
|
|
52
|
+
class CallbackDeveloperWithHaltedValidation < CallbackDeveloper
|
53
|
+
before_validation proc { |model| model.history << [:before_validation, :throwing_abort]; throw(:abort) }
|
54
|
+
before_validation proc { |model| model.history << [:before_validation, :should_never_get_here] }
|
55
|
+
end
|
56
|
+
|
52
57
|
class ParentDeveloper < ActiveRecord::Base
|
53
58
|
self.table_name = 'developers'
|
54
59
|
attr_accessor :after_save_called
|
@@ -59,27 +64,6 @@ class ChildDeveloper < ParentDeveloper
|
|
59
64
|
|
60
65
|
end
|
61
66
|
|
62
|
-
class RecursiveCallbackDeveloper < ActiveRecord::Base
|
63
|
-
self.table_name = 'developers'
|
64
|
-
|
65
|
-
before_save :on_before_save
|
66
|
-
after_save :on_after_save
|
67
|
-
|
68
|
-
attr_reader :on_before_save_called, :on_after_save_called
|
69
|
-
|
70
|
-
def on_before_save
|
71
|
-
@on_before_save_called ||= 0
|
72
|
-
@on_before_save_called += 1
|
73
|
-
save unless @on_before_save_called > 1
|
74
|
-
end
|
75
|
-
|
76
|
-
def on_after_save
|
77
|
-
@on_after_save_called ||= 0
|
78
|
-
@on_after_save_called += 1
|
79
|
-
save unless @on_after_save_called > 1
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
67
|
class ImmutableDeveloper < ActiveRecord::Base
|
84
68
|
self.table_name = 'developers'
|
85
69
|
|
@@ -88,35 +72,24 @@ class ImmutableDeveloper < ActiveRecord::Base
|
|
88
72
|
before_save :cancel
|
89
73
|
before_destroy :cancel
|
90
74
|
|
91
|
-
def cancelled?
|
92
|
-
@cancelled == true
|
93
|
-
end
|
94
|
-
|
95
75
|
private
|
96
76
|
def cancel
|
97
|
-
@cancelled = true
|
98
77
|
false
|
99
78
|
end
|
100
79
|
end
|
101
80
|
|
102
|
-
class
|
81
|
+
class DeveloperWithCanceledCallbacks < ActiveRecord::Base
|
103
82
|
self.table_name = 'developers'
|
104
83
|
|
105
|
-
validates_inclusion_of :salary, :
|
106
|
-
|
107
|
-
def cancelled?
|
108
|
-
@cancelled == true
|
109
|
-
end
|
84
|
+
validates_inclusion_of :salary, in: 50000..200000
|
110
85
|
|
111
|
-
before_save
|
112
|
-
|
113
|
-
false
|
114
|
-
end
|
86
|
+
before_save :cancel
|
87
|
+
before_destroy :cancel
|
115
88
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
89
|
+
private
|
90
|
+
def cancel
|
91
|
+
throw(:abort)
|
92
|
+
end
|
120
93
|
end
|
121
94
|
|
122
95
|
class OnCallbacksDeveloper < ActiveRecord::Base
|
@@ -182,6 +155,23 @@ class CallbackCancellationDeveloper < ActiveRecord::Base
|
|
182
155
|
after_destroy { @after_destroy_called = true }
|
183
156
|
end
|
184
157
|
|
158
|
+
class CallbackHaltedDeveloper < ActiveRecord::Base
|
159
|
+
self.table_name = 'developers'
|
160
|
+
|
161
|
+
attr_reader :after_save_called, :after_create_called, :after_update_called, :after_destroy_called
|
162
|
+
attr_accessor :cancel_before_save, :cancel_before_create, :cancel_before_update, :cancel_before_destroy
|
163
|
+
|
164
|
+
before_save { throw(:abort) if defined?(@cancel_before_save) }
|
165
|
+
before_create { throw(:abort) if @cancel_before_create }
|
166
|
+
before_update { throw(:abort) if @cancel_before_update }
|
167
|
+
before_destroy { throw(:abort) if @cancel_before_destroy }
|
168
|
+
|
169
|
+
after_save { @after_save_called = true }
|
170
|
+
after_update { @after_update_called = true }
|
171
|
+
after_create { @after_create_called = true }
|
172
|
+
after_destroy { @after_destroy_called = true }
|
173
|
+
end
|
174
|
+
|
185
175
|
class CallbacksTest < ActiveRecord::TestCase
|
186
176
|
fixtures :developers
|
187
177
|
|
@@ -298,7 +288,12 @@ class CallbacksTest < ActiveRecord::TestCase
|
|
298
288
|
[ :after_save, :string ],
|
299
289
|
[ :after_save, :proc ],
|
300
290
|
[ :after_save, :object ],
|
301
|
-
[ :after_save, :block ]
|
291
|
+
[ :after_save, :block ],
|
292
|
+
[ :after_commit, :block ],
|
293
|
+
[ :after_commit, :object ],
|
294
|
+
[ :after_commit, :proc ],
|
295
|
+
[ :after_commit, :string ],
|
296
|
+
[ :after_commit, :method ]
|
302
297
|
], david.history
|
303
298
|
end
|
304
299
|
|
@@ -367,7 +362,12 @@ class CallbacksTest < ActiveRecord::TestCase
|
|
367
362
|
[ :after_save, :string ],
|
368
363
|
[ :after_save, :proc ],
|
369
364
|
[ :after_save, :object ],
|
370
|
-
[ :after_save, :block ]
|
365
|
+
[ :after_save, :block ],
|
366
|
+
[ :after_commit, :block ],
|
367
|
+
[ :after_commit, :object ],
|
368
|
+
[ :after_commit, :proc ],
|
369
|
+
[ :after_commit, :string ],
|
370
|
+
[ :after_commit, :method ]
|
371
371
|
], david.history
|
372
372
|
end
|
373
373
|
|
@@ -418,7 +418,12 @@ class CallbacksTest < ActiveRecord::TestCase
|
|
418
418
|
[ :after_destroy, :string ],
|
419
419
|
[ :after_destroy, :proc ],
|
420
420
|
[ :after_destroy, :object ],
|
421
|
-
[ :after_destroy, :block ]
|
421
|
+
[ :after_destroy, :block ],
|
422
|
+
[ :after_commit, :block ],
|
423
|
+
[ :after_commit, :object ],
|
424
|
+
[ :after_commit, :proc ],
|
425
|
+
[ :after_commit, :string ],
|
426
|
+
[ :after_commit, :method ]
|
422
427
|
], david.history
|
423
428
|
end
|
424
429
|
|
@@ -439,14 +444,15 @@ class CallbacksTest < ActiveRecord::TestCase
|
|
439
444
|
], david.history
|
440
445
|
end
|
441
446
|
|
442
|
-
def
|
447
|
+
def test_deprecated_before_save_returning_false
|
443
448
|
david = ImmutableDeveloper.find(1)
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
449
|
+
assert_deprecated do
|
450
|
+
assert david.valid?
|
451
|
+
assert !david.save
|
452
|
+
exc = assert_raise(ActiveRecord::RecordNotSaved) { david.save! }
|
453
|
+
assert_equal exc.record, david
|
454
|
+
assert_equal "Failed to save the record", exc.message
|
455
|
+
end
|
450
456
|
|
451
457
|
david = ImmutableDeveloper.find(1)
|
452
458
|
david.salary = 10_000_000
|
@@ -456,40 +462,49 @@ class CallbacksTest < ActiveRecord::TestCase
|
|
456
462
|
|
457
463
|
someone = CallbackCancellationDeveloper.find(1)
|
458
464
|
someone.cancel_before_save = true
|
459
|
-
|
460
|
-
|
465
|
+
assert_deprecated do
|
466
|
+
assert someone.valid?
|
467
|
+
assert !someone.save
|
468
|
+
end
|
461
469
|
assert_save_callbacks_not_called(someone)
|
462
470
|
end
|
463
471
|
|
464
|
-
def
|
472
|
+
def test_deprecated_before_create_returning_false
|
465
473
|
someone = CallbackCancellationDeveloper.new
|
466
474
|
someone.cancel_before_create = true
|
467
|
-
|
468
|
-
|
475
|
+
assert_deprecated do
|
476
|
+
assert someone.valid?
|
477
|
+
assert !someone.save
|
478
|
+
end
|
469
479
|
assert_save_callbacks_not_called(someone)
|
470
480
|
end
|
471
481
|
|
472
|
-
def
|
482
|
+
def test_deprecated_before_update_returning_false
|
473
483
|
someone = CallbackCancellationDeveloper.find(1)
|
474
484
|
someone.cancel_before_update = true
|
475
|
-
|
476
|
-
|
485
|
+
assert_deprecated do
|
486
|
+
assert someone.valid?
|
487
|
+
assert !someone.save
|
488
|
+
end
|
477
489
|
assert_save_callbacks_not_called(someone)
|
478
490
|
end
|
479
491
|
|
480
|
-
def
|
492
|
+
def test_deprecated_before_destroy_returning_false
|
481
493
|
david = ImmutableDeveloper.find(1)
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
494
|
+
assert_deprecated do
|
495
|
+
assert !david.destroy
|
496
|
+
exc = assert_raise(ActiveRecord::RecordNotDestroyed) { david.destroy! }
|
497
|
+
assert_equal exc.record, david
|
498
|
+
assert_equal "Failed to destroy the record", exc.message
|
499
|
+
end
|
487
500
|
assert_not_nil ImmutableDeveloper.find_by_id(1)
|
488
501
|
|
489
502
|
someone = CallbackCancellationDeveloper.find(1)
|
490
503
|
someone.cancel_before_destroy = true
|
491
|
-
|
492
|
-
|
504
|
+
assert_deprecated do
|
505
|
+
assert !someone.destroy
|
506
|
+
assert_raise(ActiveRecord::RecordNotDestroyed) { someone.destroy! }
|
507
|
+
end
|
493
508
|
assert !someone.after_destroy_called
|
494
509
|
end
|
495
510
|
|
@@ -500,9 +515,59 @@ class CallbacksTest < ActiveRecord::TestCase
|
|
500
515
|
end
|
501
516
|
private :assert_save_callbacks_not_called
|
502
517
|
|
518
|
+
def test_before_create_throwing_abort
|
519
|
+
someone = CallbackHaltedDeveloper.new
|
520
|
+
someone.cancel_before_create = true
|
521
|
+
assert someone.valid?
|
522
|
+
assert !someone.save
|
523
|
+
assert_save_callbacks_not_called(someone)
|
524
|
+
end
|
525
|
+
|
526
|
+
def test_before_save_throwing_abort
|
527
|
+
david = DeveloperWithCanceledCallbacks.find(1)
|
528
|
+
assert david.valid?
|
529
|
+
assert !david.save
|
530
|
+
exc = assert_raise(ActiveRecord::RecordNotSaved) { david.save! }
|
531
|
+
assert_equal exc.record, david
|
532
|
+
|
533
|
+
david = DeveloperWithCanceledCallbacks.find(1)
|
534
|
+
david.salary = 10_000_000
|
535
|
+
assert !david.valid?
|
536
|
+
assert !david.save
|
537
|
+
assert_raise(ActiveRecord::RecordInvalid) { david.save! }
|
538
|
+
|
539
|
+
someone = CallbackHaltedDeveloper.find(1)
|
540
|
+
someone.cancel_before_save = true
|
541
|
+
assert someone.valid?
|
542
|
+
assert !someone.save
|
543
|
+
assert_save_callbacks_not_called(someone)
|
544
|
+
end
|
545
|
+
|
546
|
+
def test_before_update_throwing_abort
|
547
|
+
someone = CallbackHaltedDeveloper.find(1)
|
548
|
+
someone.cancel_before_update = true
|
549
|
+
assert someone.valid?
|
550
|
+
assert !someone.save
|
551
|
+
assert_save_callbacks_not_called(someone)
|
552
|
+
end
|
553
|
+
|
554
|
+
def test_before_destroy_throwing_abort
|
555
|
+
david = DeveloperWithCanceledCallbacks.find(1)
|
556
|
+
assert !david.destroy
|
557
|
+
exc = assert_raise(ActiveRecord::RecordNotDestroyed) { david.destroy! }
|
558
|
+
assert_equal exc.record, david
|
559
|
+
assert_not_nil ImmutableDeveloper.find_by_id(1)
|
560
|
+
|
561
|
+
someone = CallbackHaltedDeveloper.find(1)
|
562
|
+
someone.cancel_before_destroy = true
|
563
|
+
assert !someone.destroy
|
564
|
+
assert_raise(ActiveRecord::RecordNotDestroyed) { someone.destroy! }
|
565
|
+
assert !someone.after_destroy_called
|
566
|
+
end
|
567
|
+
|
503
568
|
def test_callback_returning_false
|
504
569
|
david = CallbackDeveloperWithFalseValidation.find(1)
|
505
|
-
david.save
|
570
|
+
assert_deprecated { david.save }
|
506
571
|
assert_equal [
|
507
572
|
[ :after_find, :method ],
|
508
573
|
[ :after_find, :string ],
|
@@ -528,6 +593,34 @@ class CallbacksTest < ActiveRecord::TestCase
|
|
528
593
|
], david.history
|
529
594
|
end
|
530
595
|
|
596
|
+
def test_callback_throwing_abort
|
597
|
+
david = CallbackDeveloperWithHaltedValidation.find(1)
|
598
|
+
david.save
|
599
|
+
assert_equal [
|
600
|
+
[ :after_find, :method ],
|
601
|
+
[ :after_find, :string ],
|
602
|
+
[ :after_find, :proc ],
|
603
|
+
[ :after_find, :object ],
|
604
|
+
[ :after_find, :block ],
|
605
|
+
[ :after_initialize, :method ],
|
606
|
+
[ :after_initialize, :string ],
|
607
|
+
[ :after_initialize, :proc ],
|
608
|
+
[ :after_initialize, :object ],
|
609
|
+
[ :after_initialize, :block ],
|
610
|
+
[ :before_validation, :method ],
|
611
|
+
[ :before_validation, :string ],
|
612
|
+
[ :before_validation, :proc ],
|
613
|
+
[ :before_validation, :object ],
|
614
|
+
[ :before_validation, :block ],
|
615
|
+
[ :before_validation, :throwing_abort ],
|
616
|
+
[ :after_rollback, :block ],
|
617
|
+
[ :after_rollback, :object ],
|
618
|
+
[ :after_rollback, :proc ],
|
619
|
+
[ :after_rollback, :string ],
|
620
|
+
[ :after_rollback, :method ],
|
621
|
+
], david.history
|
622
|
+
end
|
623
|
+
|
531
624
|
def test_inheritance_of_callbacks
|
532
625
|
parent = ParentDeveloper.new
|
533
626
|
assert !parent.after_save_called
|