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.
- data/.DS_Store +0 -0
- data/.travis.yml +26 -2
- data/{CONTRIBUTION.rdoc → CONTRIBUTION.md} +16 -15
- data/Gemfile +1 -1
- data/README.md +48 -0
- data/active_setup_linux.sh +97 -0
- data/activerecord-nuodb-adapter.gemspec +1 -1
- data/lib/active_record/connection_adapters/nuodb/version.rb +1 -1
- data/lib/active_record/connection_adapters/nuodb_adapter.rb +45 -9
- data/test/.DS_Store +0 -0
- data/test/.gitignore +1 -0
- data/test/active_record/connection_adapters/fake_adapter.rb +45 -0
- data/test/ar_schema_test.rb +70 -0
- data/test/assets/example.log +1 -0
- data/test/assets/flowers.jpg +0 -0
- data/test/assets/test.txt +1 -0
- data/test/base_test.rb +1481 -0
- data/test/cases/.DS_Store +0 -0
- data/test/cases/adapter_test.rb +211 -0
- data/test/cases/adapters/firebird/connection_test.rb +8 -0
- data/test/cases/adapters/firebird/default_test.rb +16 -0
- data/test/cases/adapters/firebird/migration_test.rb +124 -0
- data/test/cases/adapters/mysql/active_schema_test.rb +143 -0
- data/test/cases/adapters/mysql/case_sensitivity_test.rb +35 -0
- data/test/cases/adapters/mysql/connection_test.rb +172 -0
- data/test/cases/adapters/mysql/enum_test.rb +10 -0
- data/test/cases/adapters/mysql/mysql_adapter_test.rb +125 -0
- data/test/cases/adapters/mysql/quoting_test.rb +25 -0
- data/test/cases/adapters/mysql/reserved_word_test.rb +153 -0
- data/test/cases/adapters/mysql/schema_test.rb +62 -0
- data/test/cases/adapters/mysql/sp_test.rb +15 -0
- data/test/cases/adapters/mysql/sql_types_test.rb +14 -0
- data/test/cases/adapters/mysql/statement_pool_test.rb +23 -0
- data/test/cases/adapters/mysql2/active_schema_test.rb +143 -0
- data/test/cases/adapters/mysql2/bind_parameter_test.rb +50 -0
- data/test/cases/adapters/mysql2/case_sensitivity_test.rb +35 -0
- data/test/cases/adapters/mysql2/connection_test.rb +97 -0
- data/test/cases/adapters/mysql2/enum_test.rb +10 -0
- data/test/cases/adapters/mysql2/explain_test.rb +26 -0
- data/test/cases/adapters/mysql2/reserved_word_test.rb +152 -0
- data/test/cases/adapters/mysql2/schema_migrations_test.rb +26 -0
- data/test/cases/adapters/mysql2/schema_test.rb +70 -0
- data/test/cases/adapters/mysql2/sql_types_test.rb +14 -0
- data/test/cases/adapters/oracle/synonym_test.rb +17 -0
- data/test/cases/adapters/postgresql/active_schema_test.rb +58 -0
- data/test/cases/adapters/postgresql/array_test.rb +104 -0
- data/test/cases/adapters/postgresql/bytea_test.rb +104 -0
- data/test/cases/adapters/postgresql/connection_test.rb +163 -0
- data/test/cases/adapters/postgresql/datatype_test.rb +604 -0
- data/test/cases/adapters/postgresql/explain_test.rb +28 -0
- data/test/cases/adapters/postgresql/hstore_test.rb +200 -0
- data/test/cases/adapters/postgresql/json_test.rb +99 -0
- data/test/cases/adapters/postgresql/ltree_test.rb +41 -0
- data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +288 -0
- data/test/cases/adapters/postgresql/quoting_test.rb +58 -0
- data/test/cases/adapters/postgresql/schema_authorization_test.rb +114 -0
- data/test/cases/adapters/postgresql/schema_test.rb +376 -0
- data/test/cases/adapters/postgresql/sql_types_test.rb +18 -0
- data/test/cases/adapters/postgresql/statement_pool_test.rb +39 -0
- data/test/cases/adapters/postgresql/timestamp_test.rb +113 -0
- data/test/cases/adapters/postgresql/utils_test.rb +20 -0
- data/test/cases/adapters/postgresql/uuid_test.rb +95 -0
- data/test/cases/adapters/postgresql/view_test.rb +49 -0
- data/test/cases/adapters/sqlite3/copy_table_test.rb +99 -0
- data/test/cases/adapters/sqlite3/explain_test.rb +26 -0
- data/test/cases/adapters/sqlite3/quoting_test.rb +102 -0
- data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +366 -0
- data/test/cases/adapters/sqlite3/statement_pool_test.rb +24 -0
- data/test/cases/aggregations_test.rb +158 -0
- data/test/cases/ar_schema_test.rb +70 -0
- data/test/cases/associations/association_scope_test.rb +15 -0
- data/test/cases/associations/belongs_to_associations_test.rb +831 -0
- data/test/cases/associations/callbacks_test.rb +168 -0
- data/test/cases/associations/cascaded_eager_loading_test.rb +179 -0
- data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +36 -0
- data/test/cases/associations/eager_load_nested_include_test.rb +128 -0
- data/test/cases/associations/eager_singularization_test.rb +145 -0
- data/test/cases/associations/eager_test.rb +1179 -0
- data/test/cases/associations/extension_test.rb +80 -0
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +779 -0
- data/test/cases/associations/has_many_associations_test.rb +1699 -0
- data/test/cases/associations/has_many_through_associations_test.rb +950 -0
- data/test/cases/associations/has_one_associations_test.rb +525 -0
- data/test/cases/associations/has_one_through_associations_test.rb +317 -0
- data/test/cases/associations/inner_join_association_test.rb +115 -0
- data/test/cases/associations/inverse_associations_test.rb +631 -0
- data/test/cases/associations/join_dependency_test.rb +8 -0
- data/test/cases/associations/join_model_test.rb +750 -0
- data/test/cases/associations/nested_through_associations_test.rb +575 -0
- data/test/cases/associations_test.rb +344 -0
- data/test/cases/attribute_methods/read_test.rb +58 -0
- data/test/cases/attribute_methods/serialization_test.rb +29 -0
- data/test/cases/attribute_methods_test.rb +819 -0
- data/test/cases/autosave_association_test.rb +1447 -0
- data/test/cases/base_test.rb +1481 -0
- data/test/cases/batches_test.rb +173 -0
- data/test/cases/binary_test.rb +49 -0
- data/test/cases/bind_parameter_test.rb +89 -0
- data/test/cases/calculations_test.rb +589 -0
- data/test/cases/callbacks_test.rb +535 -0
- data/test/cases/clone_test.rb +40 -0
- data/test/cases/coders/yaml_column_test.rb +63 -0
- data/test/cases/column_alias_test.rb +17 -0
- data/test/cases/column_definition_test.rb +143 -0
- data/test/cases/column_test.rb +115 -0
- data/test/cases/connection_adapters/abstract_adapter_test.rb +62 -0
- data/test/cases/connection_adapters/connection_handler_test.rb +53 -0
- data/test/cases/connection_adapters/connection_specification_test.rb +12 -0
- data/test/cases/connection_adapters/quoting_test.rb +13 -0
- data/test/cases/connection_adapters/schema_cache_test.rb +56 -0
- data/test/cases/connection_management_test.rb +112 -0
- data/test/cases/connection_pool_test.rb +344 -0
- data/test/cases/connection_specification/resolver_test.rb +63 -0
- data/test/cases/core_test.rb +33 -0
- data/test/cases/counter_cache_test.rb +163 -0
- data/test/cases/custom_locking_test.rb +17 -0
- data/test/cases/database_statements_test.rb +19 -0
- data/test/cases/date_time_test.rb +43 -0
- data/test/cases/defaults_test.rb +214 -0
- data/test/cases/dirty_test.rb +626 -0
- data/test/cases/disconnected_test.rb +27 -0
- data/test/cases/dup_test.rb +136 -0
- data/test/cases/explain_subscriber_test.rb +59 -0
- data/test/cases/explain_test.rb +72 -0
- data/test/cases/finder_respond_to_test.rb +50 -0
- data/test/cases/finder_test.rb +886 -0
- data/test/cases/fixture_set/file_test.rb +83 -0
- data/test/cases/fixtures_test.rb +808 -0
- data/test/cases/forbidden_attributes_protection_test.rb +64 -0
- data/test/cases/habtm_destroy_order_test.rb +61 -0
- data/test/cases/helper.rb +151 -0
- data/test/cases/hot_compatibility_test.rb +54 -0
- data/test/cases/i18n_test.rb +45 -0
- data/test/cases/inheritance_test.rb +355 -0
- data/test/cases/integration_test.rb +87 -0
- data/test/cases/invalid_connection_test.rb +22 -0
- data/test/cases/invalid_date_test.rb +32 -0
- data/test/cases/invertible_migration_test.rb +259 -0
- data/test/cases/json_serialization_test.rb +300 -0
- data/test/cases/locking_test.rb +453 -0
- data/test/cases/log_subscriber_test.rb +129 -0
- data/test/cases/migration/change_schema_test.rb +379 -0
- data/test/cases/migration/change_table_test.rb +204 -0
- data/test/cases/migration/column_attributes_test.rb +186 -0
- data/test/cases/migration/column_positioning_test.rb +60 -0
- data/test/cases/migration/columns_test.rb +279 -0
- data/test/cases/migration/command_recorder_test.rb +247 -0
- data/test/cases/migration/create_join_table_test.rb +125 -0
- data/test/cases/migration/helper.rb +43 -0
- data/test/cases/migration/index_test.rb +209 -0
- data/test/cases/migration/logger_test.rb +37 -0
- data/test/cases/migration/references_index_test.rb +102 -0
- data/test/cases/migration/references_statements_test.rb +111 -0
- data/test/cases/migration/rename_table_test.rb +90 -0
- data/test/cases/migration/table_and_index_test.rb +24 -0
- data/test/cases/migration_test.rb +861 -0
- data/test/cases/migrator_test.rb +378 -0
- data/test/cases/mixin_test.rb +96 -0
- data/test/cases/modules_test.rb +143 -0
- data/test/cases/multiparameter_attributes_test.rb +350 -0
- data/test/cases/multiple_db_test.rb +108 -0
- data/test/cases/nested_attributes_test.rb +1056 -0
- data/test/cases/persistence_test.rb +802 -0
- data/test/cases/pooled_connections_test.rb +51 -0
- data/test/cases/primary_keys_test.rb +218 -0
- data/test/cases/query_cache_test.rb +276 -0
- data/test/cases/quoting_test.rb +229 -0
- data/test/cases/readonly_test.rb +111 -0
- data/test/cases/reaper_test.rb +81 -0
- data/test/cases/reflection_test.rb +386 -0
- data/test/cases/relation/where_chain_test.rb +80 -0
- data/test/cases/relation/where_test.rb +120 -0
- data/test/cases/relation_test.rb +315 -0
- data/test/cases/relations_test.rb +1559 -0
- data/test/cases/reload_models_test.rb +22 -0
- data/test/cases/result_test.rb +32 -0
- data/test/cases/sanitize_test.rb +34 -0
- data/test/cases/schema_dumper_test.rb +391 -0
- data/test/cases/scoping/default_scoping_test.rb +374 -0
- data/test/cases/scoping/named_scoping_test.rb +451 -0
- data/test/cases/scoping/relation_scoping_test.rb +331 -0
- data/test/cases/serialization_test.rb +68 -0
- data/test/cases/serialized_attribute_test.rb +252 -0
- data/test/cases/statement_cache_test.rb +64 -0
- data/test/cases/store_test.rb +153 -0
- data/test/cases/tasks/database_tasks_test.rb +315 -0
- data/test/cases/tasks/mysql_rake_test.rb +301 -0
- data/test/cases/tasks/postgresql_rake_test.rb +236 -0
- data/test/cases/tasks/sqlite_rake_test.rb +191 -0
- data/test/cases/test_case.rb +95 -0
- data/test/cases/test_simple.rb +194 -0
- data/test/cases/timestamp_test.rb +308 -0
- data/test/cases/transaction_callbacks_test.rb +317 -0
- data/test/cases/transaction_isolation_test.rb +114 -0
- data/test/cases/transactions_test.rb +595 -0
- data/test/cases/unconnected_test.rb +33 -0
- data/test/cases/validations/association_validation_test.rb +121 -0
- data/test/cases/validations/i18n_generate_message_validation_test.rb +76 -0
- data/test/cases/validations/i18n_validation_test.rb +89 -0
- data/test/cases/validations/presence_validation_test.rb +51 -0
- data/test/cases/validations/uniqueness_validation_test.rb +379 -0
- data/test/cases/validations_repair_helper.rb +23 -0
- data/test/cases/validations_test.rb +124 -0
- data/test/cases/xml_serialization_test.rb +455 -0
- data/test/cases/yaml_serialization_test.rb +52 -0
- data/test/config.example.yml +154 -0
- data/test/config.rb +5 -0
- data/test/database_statements_test.rb +19 -0
- data/test/fixtures/.gitignore +1 -0
- data/test/fixtures/accounts.yml +29 -0
- data/test/fixtures/admin/accounts.yml +2 -0
- data/test/fixtures/admin/randomly_named_a9.yml +7 -0
- data/test/fixtures/admin/randomly_named_b0.yml +7 -0
- data/test/fixtures/admin/users.yml +10 -0
- data/test/fixtures/all/developers.yml +0 -0
- data/test/fixtures/all/people.yml +0 -0
- data/test/fixtures/all/tasks.yml +0 -0
- data/test/fixtures/author_addresses.yml +5 -0
- data/test/fixtures/author_favorites.yml +4 -0
- data/test/fixtures/authors.yml +15 -0
- data/test/fixtures/binaries.yml +133 -0
- data/test/fixtures/books.yml +9 -0
- data/test/fixtures/cars.yml +9 -0
- data/test/fixtures/categories.yml +19 -0
- data/test/fixtures/categories/special_categories.yml +9 -0
- data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
- data/test/fixtures/categories_ordered.yml +7 -0
- data/test/fixtures/categories_posts.yml +31 -0
- data/test/fixtures/categorizations.yml +23 -0
- data/test/fixtures/clubs.yml +8 -0
- data/test/fixtures/collections.yml +3 -0
- data/test/fixtures/colleges.yml +3 -0
- data/test/fixtures/comments.yml +65 -0
- data/test/fixtures/companies.yml +59 -0
- data/test/fixtures/computers.yml +4 -0
- data/test/fixtures/courses.yml +8 -0
- data/test/fixtures/customers.yml +26 -0
- data/test/fixtures/dashboards.yml +6 -0
- data/test/fixtures/developers.yml +21 -0
- data/test/fixtures/developers_projects.yml +17 -0
- data/test/fixtures/dog_lovers.yml +7 -0
- data/test/fixtures/dogs.yml +4 -0
- data/test/fixtures/edges.yml +5 -0
- data/test/fixtures/entrants.yml +14 -0
- data/test/fixtures/essays.yml +6 -0
- data/test/fixtures/faces.yml +11 -0
- data/test/fixtures/fk_test_has_fk.yml +3 -0
- data/test/fixtures/fk_test_has_pk.yml +2 -0
- data/test/fixtures/friendships.yml +4 -0
- data/test/fixtures/funny_jokes.yml +10 -0
- data/test/fixtures/interests.yml +33 -0
- data/test/fixtures/items.yml +3 -0
- data/test/fixtures/jobs.yml +7 -0
- data/test/fixtures/legacy_things.yml +3 -0
- data/test/fixtures/mateys.yml +4 -0
- data/test/fixtures/member_details.yml +8 -0
- data/test/fixtures/member_types.yml +6 -0
- data/test/fixtures/members.yml +11 -0
- data/test/fixtures/memberships.yml +34 -0
- data/test/fixtures/men.yml +5 -0
- data/test/fixtures/minimalistics.yml +2 -0
- data/test/fixtures/minivans.yml +5 -0
- data/test/fixtures/mixed_case_monkeys.yml +6 -0
- data/test/fixtures/mixins.yml +29 -0
- data/test/fixtures/movies.yml +7 -0
- data/test/fixtures/naked/csv/accounts.csv +1 -0
- data/test/fixtures/naked/yml/accounts.yml +1 -0
- data/test/fixtures/naked/yml/companies.yml +1 -0
- data/test/fixtures/naked/yml/courses.yml +1 -0
- data/test/fixtures/organizations.yml +5 -0
- data/test/fixtures/other_topics.yml +42 -0
- data/test/fixtures/owners.yml +8 -0
- data/test/fixtures/parrots.yml +27 -0
- data/test/fixtures/parrots_pirates.yml +7 -0
- data/test/fixtures/people.yml +24 -0
- data/test/fixtures/peoples_treasures.yml +3 -0
- data/test/fixtures/pets.yml +19 -0
- data/test/fixtures/pirates.yml +9 -0
- data/test/fixtures/posts.yml +82 -0
- data/test/fixtures/price_estimates.yml +7 -0
- data/test/fixtures/products.yml +4 -0
- data/test/fixtures/projects.yml +7 -0
- data/test/fixtures/randomly_named_a9.yml +7 -0
- data/test/fixtures/ratings.yml +14 -0
- data/test/fixtures/readers.yml +11 -0
- data/test/fixtures/references.yml +17 -0
- data/test/fixtures/reserved_words/distinct.yml +5 -0
- data/test/fixtures/reserved_words/distinct_select.yml +11 -0
- data/test/fixtures/reserved_words/group.yml +14 -0
- data/test/fixtures/reserved_words/select.yml +8 -0
- data/test/fixtures/reserved_words/values.yml +7 -0
- data/test/fixtures/ships.yml +6 -0
- data/test/fixtures/speedometers.yml +8 -0
- data/test/fixtures/sponsors.yml +12 -0
- data/test/fixtures/string_key_objects.yml +7 -0
- data/test/fixtures/subscribers.yml +11 -0
- data/test/fixtures/subscriptions.yml +12 -0
- data/test/fixtures/taggings.yml +78 -0
- data/test/fixtures/tags.yml +11 -0
- data/test/fixtures/tasks.yml +7 -0
- data/test/fixtures/teapots.yml +3 -0
- data/test/fixtures/topics.yml +42 -0
- data/test/fixtures/toys.yml +14 -0
- data/test/fixtures/traffic_lights.yml +10 -0
- data/test/fixtures/treasures.yml +10 -0
- data/test/fixtures/variants.yml +4 -0
- data/test/fixtures/vegetables.yml +20 -0
- data/test/fixtures/vertices.yml +4 -0
- data/test/fixtures/warehouse-things.yml +3 -0
- data/test/fixtures/zines.yml +5 -0
- data/test/migration_test.rb +861 -0
- data/test/migrations/10_urban/9_add_expressions.rb +11 -0
- data/test/migrations/decimal/1_give_me_big_numbers.rb +15 -0
- data/test/migrations/empty/.gitkeep +0 -0
- data/test/migrations/magic/1_currencies_have_symbols.rb +12 -0
- data/test/migrations/missing/1000_people_have_middle_names.rb +9 -0
- data/test/migrations/missing/1_people_have_last_names.rb +9 -0
- data/test/migrations/missing/3_we_need_reminders.rb +12 -0
- data/test/migrations/missing/4_innocent_jointable.rb +12 -0
- data/test/migrations/rename/1_we_need_things.rb +11 -0
- data/test/migrations/rename/2_rename_things.rb +9 -0
- data/test/migrations/to_copy/1_people_have_hobbies.rb +9 -0
- data/test/migrations/to_copy/2_people_have_descriptions.rb +9 -0
- data/test/migrations/to_copy2/1_create_articles.rb +7 -0
- data/test/migrations/to_copy2/2_create_comments.rb +7 -0
- data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +9 -0
- data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +9 -0
- data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +9 -0
- data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +7 -0
- data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +7 -0
- data/test/migrations/valid/1_valid_people_have_last_names.rb +9 -0
- data/test/migrations/valid/2_we_need_reminders.rb +12 -0
- data/test/migrations/valid/3_innocent_jointable.rb +12 -0
- data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +9 -0
- data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +12 -0
- data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +12 -0
- data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +9 -0
- data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +12 -0
- data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +12 -0
- data/test/models/admin.rb +5 -0
- data/test/models/admin/account.rb +3 -0
- data/test/models/admin/randomly_named_c1.rb +3 -0
- data/test/models/admin/user.rb +39 -0
- data/test/models/aircraft.rb +4 -0
- data/test/models/arunit2_model.rb +3 -0
- data/test/models/author.rb +196 -0
- data/test/models/auto_id.rb +4 -0
- data/test/models/autoloadable/extra_firm.rb +2 -0
- data/test/models/binary.rb +2 -0
- data/test/models/bird.rb +12 -0
- data/test/models/book.rb +9 -0
- data/test/models/boolean.rb +2 -0
- data/test/models/bulb.rb +45 -0
- data/test/models/car.rb +28 -0
- data/test/models/categorization.rb +19 -0
- data/test/models/category.rb +34 -0
- data/test/models/citation.rb +6 -0
- data/test/models/club.rb +15 -0
- data/test/models/college.rb +5 -0
- data/test/models/column_name.rb +3 -0
- data/test/models/comment.rb +38 -0
- data/test/models/company.rb +214 -0
- data/test/models/company_in_module.rb +80 -0
- data/test/models/computer.rb +3 -0
- data/test/models/contact.rb +40 -0
- data/test/models/contract.rb +19 -0
- data/test/models/country.rb +7 -0
- data/test/models/course.rb +6 -0
- data/test/models/customer.rb +77 -0
- data/test/models/dashboard.rb +3 -0
- data/test/models/default.rb +2 -0
- data/test/models/developer.rb +250 -0
- data/test/models/dog.rb +5 -0
- data/test/models/dog_lover.rb +5 -0
- data/test/models/edge.rb +5 -0
- data/test/models/electron.rb +3 -0
- data/test/models/engine.rb +4 -0
- data/test/models/entrant.rb +3 -0
- data/test/models/essay.rb +5 -0
- data/test/models/event.rb +3 -0
- data/test/models/eye.rb +37 -0
- data/test/models/face.rb +7 -0
- data/test/models/friendship.rb +6 -0
- data/test/models/guid.rb +2 -0
- data/test/models/interest.rb +5 -0
- data/test/models/invoice.rb +4 -0
- data/test/models/item.rb +7 -0
- data/test/models/job.rb +7 -0
- data/test/models/joke.rb +7 -0
- data/test/models/keyboard.rb +3 -0
- data/test/models/legacy_thing.rb +3 -0
- data/test/models/lesson.rb +11 -0
- data/test/models/line_item.rb +3 -0
- data/test/models/liquid.rb +4 -0
- data/test/models/man.rb +9 -0
- data/test/models/matey.rb +4 -0
- data/test/models/member.rb +36 -0
- data/test/models/member_detail.rb +7 -0
- data/test/models/member_type.rb +3 -0
- data/test/models/membership.rb +15 -0
- data/test/models/minimalistic.rb +2 -0
- data/test/models/minivan.rb +9 -0
- data/test/models/mixed_case_monkey.rb +3 -0
- data/test/models/molecule.rb +4 -0
- data/test/models/movie.rb +5 -0
- data/test/models/order.rb +4 -0
- data/test/models/organization.rb +12 -0
- data/test/models/owner.rb +5 -0
- data/test/models/parrot.rb +29 -0
- data/test/models/person.rb +128 -0
- data/test/models/pet.rb +15 -0
- data/test/models/pirate.rb +86 -0
- data/test/models/possession.rb +3 -0
- data/test/models/post.rb +194 -0
- data/test/models/price_estimate.rb +4 -0
- data/test/models/project.rb +30 -0
- data/test/models/randomly_named_c1.rb +3 -0
- data/test/models/rating.rb +4 -0
- data/test/models/reader.rb +21 -0
- data/test/models/reference.rb +22 -0
- data/test/models/reply.rb +61 -0
- data/test/models/ship.rb +19 -0
- data/test/models/ship_part.rb +7 -0
- data/test/models/shop.rb +12 -0
- data/test/models/speedometer.rb +6 -0
- data/test/models/sponsor.rb +7 -0
- data/test/models/string_key_object.rb +3 -0
- data/test/models/student.rb +3 -0
- data/test/models/subject.rb +16 -0
- data/test/models/subscriber.rb +8 -0
- data/test/models/subscription.rb +4 -0
- data/test/models/tag.rb +7 -0
- data/test/models/tagging.rb +13 -0
- data/test/models/task.rb +5 -0
- data/test/models/topic.rb +121 -0
- data/test/models/toy.rb +6 -0
- data/test/models/traffic_light.rb +4 -0
- data/test/models/treasure.rb +11 -0
- data/test/models/treaty.rb +7 -0
- data/test/models/tyre.rb +3 -0
- data/test/models/vegetables.rb +24 -0
- data/test/models/vertex.rb +9 -0
- data/test/models/warehouse_thing.rb +5 -0
- data/test/models/wheel.rb +3 -0
- data/test/models/without_table.rb +3 -0
- data/test/models/zine.rb +3 -0
- data/test/schema/mysql2_specific_schema.rb +58 -0
- data/test/schema/mysql_specific_schema.rb +70 -0
- data/test/schema/oracle_specific_schema.rb +46 -0
- data/test/schema/postgresql_specific_schema.rb +224 -0
- data/test/schema/schema.rb +809 -0
- data/test/schema/sqlite_specific_schema.rb +25 -0
- data/test/support/config.rb +43 -0
- data/test/support/connection.rb +21 -0
- metadata +898 -6
- data/README.rdoc +0 -42
@@ -0,0 +1,1699 @@
|
|
1
|
+
require "cases/helper"
|
2
|
+
require 'models/developer'
|
3
|
+
require 'models/project'
|
4
|
+
require 'models/company'
|
5
|
+
require 'models/contract'
|
6
|
+
require 'models/topic'
|
7
|
+
require 'models/reply'
|
8
|
+
require 'models/category'
|
9
|
+
require 'models/post'
|
10
|
+
require 'models/author'
|
11
|
+
require 'models/essay'
|
12
|
+
require 'models/comment'
|
13
|
+
require 'models/person'
|
14
|
+
require 'models/reader'
|
15
|
+
require 'models/tagging'
|
16
|
+
require 'models/tag'
|
17
|
+
require 'models/invoice'
|
18
|
+
require 'models/line_item'
|
19
|
+
require 'models/car'
|
20
|
+
require 'models/bulb'
|
21
|
+
require 'models/engine'
|
22
|
+
require 'models/categorization'
|
23
|
+
require 'models/minivan'
|
24
|
+
require 'models/speedometer'
|
25
|
+
|
26
|
+
class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
|
27
|
+
fixtures :authors, :posts, :comments
|
28
|
+
|
29
|
+
def test_should_generate_valid_sql
|
30
|
+
author = authors(:david)
|
31
|
+
# this can fail on adapters which require ORDER BY expressions to be included in the SELECT expression
|
32
|
+
# if the reorder clauses are not correctly handled
|
33
|
+
assert author.posts_with_comments_sorted_by_comment_id.where('comments.id > 0').reorder('posts.comments_count DESC', 'posts.taggings_count DESC').last
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
class HasManyAssociationsTest < ActiveRecord::TestCase
|
39
|
+
fixtures :accounts, :categories, :companies, :developers, :projects,
|
40
|
+
:developers_projects, :topics, :authors, :comments,
|
41
|
+
:people, :posts, :readers, :taggings, :cars, :essays,
|
42
|
+
:categorizations
|
43
|
+
|
44
|
+
def setup
|
45
|
+
Client.destroyed_client_ids.clear
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_create_from_association_should_respect_default_scope
|
49
|
+
car = Car.create(:name => 'honda')
|
50
|
+
assert_equal 'honda', car.name
|
51
|
+
|
52
|
+
bulb = Bulb.create
|
53
|
+
assert_equal 'defaulty', bulb.name
|
54
|
+
|
55
|
+
bulb = car.bulbs.build
|
56
|
+
assert_equal 'defaulty', bulb.name
|
57
|
+
|
58
|
+
bulb = car.bulbs.create
|
59
|
+
assert_equal 'defaulty', bulb.name
|
60
|
+
|
61
|
+
bulb = car.bulbs.create(:name => 'exotic')
|
62
|
+
assert_equal 'exotic', bulb.name
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_create_from_association_with_nil_values_should_work
|
66
|
+
car = Car.create(:name => 'honda')
|
67
|
+
|
68
|
+
bulb = car.bulbs.new(nil)
|
69
|
+
assert_equal 'defaulty', bulb.name
|
70
|
+
|
71
|
+
bulb = car.bulbs.build(nil)
|
72
|
+
assert_equal 'defaulty', bulb.name
|
73
|
+
|
74
|
+
bulb = car.bulbs.create(nil)
|
75
|
+
assert_equal 'defaulty', bulb.name
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_do_not_call_callbacks_for_delete_all
|
79
|
+
bulb_count = Bulb.count
|
80
|
+
car = Car.create(:name => 'honda')
|
81
|
+
car.funky_bulbs.create!
|
82
|
+
assert_nothing_raised { car.reload.funky_bulbs.delete_all }
|
83
|
+
assert_equal bulb_count + 1, Bulb.count, "bulbs should have been deleted using :nullify strategey"
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_building_the_associated_object_with_implicit_sti_base_class
|
87
|
+
firm = DependentFirm.new
|
88
|
+
company = firm.companies.build
|
89
|
+
assert_kind_of Company, company, "Expected #{company.class} to be a Company"
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_building_the_associated_object_with_explicit_sti_base_class
|
93
|
+
firm = DependentFirm.new
|
94
|
+
company = firm.companies.build(:type => "Company")
|
95
|
+
assert_kind_of Company, company, "Expected #{company.class} to be a Company"
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_building_the_associated_object_with_sti_subclass
|
99
|
+
firm = DependentFirm.new
|
100
|
+
company = firm.companies.build(:type => "Client")
|
101
|
+
assert_kind_of Client, company, "Expected #{company.class} to be a Client"
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_building_the_associated_object_with_an_invalid_type
|
105
|
+
firm = DependentFirm.new
|
106
|
+
assert_raise(ActiveRecord::SubclassNotFound) { firm.companies.build(:type => "Invalid") }
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_building_the_associated_object_with_an_unrelated_type
|
110
|
+
firm = DependentFirm.new
|
111
|
+
assert_raise(ActiveRecord::SubclassNotFound) { firm.companies.build(:type => "Account") }
|
112
|
+
end
|
113
|
+
|
114
|
+
test "building the association with an array" do
|
115
|
+
speedometer = Speedometer.new(speedometer_id: "a")
|
116
|
+
data = [{name: "first"}, {name: "second"}]
|
117
|
+
speedometer.minivans.build(data)
|
118
|
+
|
119
|
+
assert_equal 2, speedometer.minivans.size
|
120
|
+
assert speedometer.save
|
121
|
+
assert_equal ["first", "second"], speedometer.reload.minivans.map(&:name)
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_association_keys_bypass_attribute_protection
|
125
|
+
car = Car.create(:name => 'honda')
|
126
|
+
|
127
|
+
bulb = car.bulbs.new
|
128
|
+
assert_equal car.id, bulb.car_id
|
129
|
+
|
130
|
+
bulb = car.bulbs.new :car_id => car.id + 1
|
131
|
+
assert_equal car.id, bulb.car_id
|
132
|
+
|
133
|
+
bulb = car.bulbs.build
|
134
|
+
assert_equal car.id, bulb.car_id
|
135
|
+
|
136
|
+
bulb = car.bulbs.build :car_id => car.id + 1
|
137
|
+
assert_equal car.id, bulb.car_id
|
138
|
+
|
139
|
+
bulb = car.bulbs.create
|
140
|
+
assert_equal car.id, bulb.car_id
|
141
|
+
|
142
|
+
bulb = car.bulbs.create :car_id => car.id + 1
|
143
|
+
assert_equal car.id, bulb.car_id
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_association_protect_foreign_key
|
147
|
+
invoice = Invoice.create
|
148
|
+
|
149
|
+
line_item = invoice.line_items.new
|
150
|
+
assert_equal invoice.id, line_item.invoice_id
|
151
|
+
|
152
|
+
line_item = invoice.line_items.new :invoice_id => invoice.id + 1
|
153
|
+
assert_equal invoice.id, line_item.invoice_id
|
154
|
+
|
155
|
+
line_item = invoice.line_items.build
|
156
|
+
assert_equal invoice.id, line_item.invoice_id
|
157
|
+
|
158
|
+
line_item = invoice.line_items.build :invoice_id => invoice.id + 1
|
159
|
+
assert_equal invoice.id, line_item.invoice_id
|
160
|
+
|
161
|
+
line_item = invoice.line_items.create
|
162
|
+
assert_equal invoice.id, line_item.invoice_id
|
163
|
+
|
164
|
+
line_item = invoice.line_items.create :invoice_id => invoice.id + 1
|
165
|
+
assert_equal invoice.id, line_item.invoice_id
|
166
|
+
end
|
167
|
+
|
168
|
+
# When creating objects on the association, we must not do it within a scope (even though it
|
169
|
+
# would be convenient), because this would cause that scope to be applied to any callbacks etc.
|
170
|
+
def test_build_and_create_should_not_happen_within_scope
|
171
|
+
car = cars(:honda)
|
172
|
+
scoped_count = car.foo_bulbs.where_values.count
|
173
|
+
|
174
|
+
bulb = car.foo_bulbs.build
|
175
|
+
assert_not_equal scoped_count, bulb.scope_after_initialize.where_values.count
|
176
|
+
|
177
|
+
bulb = car.foo_bulbs.create
|
178
|
+
assert_not_equal scoped_count, bulb.scope_after_initialize.where_values.count
|
179
|
+
|
180
|
+
bulb = car.foo_bulbs.create!
|
181
|
+
assert_not_equal scoped_count, bulb.scope_after_initialize.where_values.count
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_no_sql_should_be_fired_if_association_already_loaded
|
185
|
+
Car.create(:name => 'honda')
|
186
|
+
bulbs = Car.first.bulbs
|
187
|
+
bulbs.to_a # to load all instances of bulbs
|
188
|
+
|
189
|
+
assert_no_queries do
|
190
|
+
bulbs.first()
|
191
|
+
bulbs.first({})
|
192
|
+
end
|
193
|
+
|
194
|
+
assert_no_queries do
|
195
|
+
bulbs.last()
|
196
|
+
bulbs.last({})
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_create_resets_cached_counters
|
201
|
+
person = Person.create!(:first_name => 'tenderlove')
|
202
|
+
post = Post.first
|
203
|
+
|
204
|
+
assert_equal [], person.readers
|
205
|
+
assert_nil person.readers.find_by_post_id(post.id)
|
206
|
+
|
207
|
+
person.readers.create(:post_id => post.id)
|
208
|
+
|
209
|
+
assert_equal 1, person.readers.count
|
210
|
+
assert_equal 1, person.readers.length
|
211
|
+
assert_equal post, person.readers.first.post
|
212
|
+
assert_equal person, person.readers.first.person
|
213
|
+
end
|
214
|
+
|
215
|
+
def force_signal37_to_load_all_clients_of_firm
|
216
|
+
companies(:first_firm).clients_of_firm.each {|f| }
|
217
|
+
end
|
218
|
+
|
219
|
+
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
|
220
|
+
def test_counting_with_counter_sql
|
221
|
+
assert_equal 2, Firm.all.merge!(:order => "id").first.clients.count
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_counting
|
225
|
+
assert_equal 2, Firm.all.merge!(:order => "id").first.plain_clients.count
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_counting_with_single_hash
|
229
|
+
assert_equal 1, Firm.all.merge!(:order => "id").first.plain_clients.where(:name => "Microsoft").count
|
230
|
+
end
|
231
|
+
|
232
|
+
def test_counting_with_column_name_and_hash
|
233
|
+
assert_equal 2, Firm.all.merge!(:order => "id").first.plain_clients.count(:name)
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_counting_with_association_limit
|
237
|
+
firm = companies(:first_firm)
|
238
|
+
assert_equal firm.limited_clients.length, firm.limited_clients.size
|
239
|
+
assert_equal firm.limited_clients.length, firm.limited_clients.count
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_finding
|
243
|
+
assert_equal 2, Firm.all.merge!(:order => "id").first.clients.length
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_finding_array_compatibility
|
247
|
+
assert_equal 2, Firm.order(:id).find{|f| f.id > 0}.clients.length
|
248
|
+
end
|
249
|
+
|
250
|
+
def test_find_many_with_merged_options
|
251
|
+
assert_equal 1, companies(:first_firm).limited_clients.size
|
252
|
+
assert_equal 1, companies(:first_firm).limited_clients.to_a.size
|
253
|
+
assert_equal 2, companies(:first_firm).limited_clients.limit(nil).to_a.size
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_find_should_prepend_to_association_order
|
257
|
+
ordered_clients = companies(:first_firm).clients_sorted_desc.order('companies.id')
|
258
|
+
assert_equal ['companies.id', 'id DESC'], ordered_clients.order_values
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_dynamic_find_should_respect_association_order
|
262
|
+
assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.where("type = 'Client'").first
|
263
|
+
assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client')
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_cant_save_has_many_readonly_association
|
267
|
+
authors(:david).readonly_comments.each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
|
268
|
+
authors(:david).readonly_comments.each { |c| assert c.readonly? }
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_finding_default_orders
|
272
|
+
assert_equal "Summit", Firm.all.merge!(:order => "id").first.clients.first.name
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_finding_with_different_class_name_and_order
|
276
|
+
assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_sorted_desc.first.name
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_finding_with_foreign_key
|
280
|
+
assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_of_firm.first.name
|
281
|
+
end
|
282
|
+
|
283
|
+
def test_finding_with_condition
|
284
|
+
assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_like_ms.first.name
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_finding_with_condition_hash
|
288
|
+
assert_equal "Microsoft", Firm.all.merge!(:order => "id").first.clients_like_ms_with_hash_conditions.first.name
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_finding_using_primary_key
|
292
|
+
assert_equal "Summit", Firm.all.merge!(:order => "id").first.clients_using_primary_key.first.name
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_belongs_to_sanity
|
296
|
+
c = Client.new
|
297
|
+
assert_nil c.firm
|
298
|
+
|
299
|
+
flunk "belongs_to failed if check" if c.firm
|
300
|
+
end
|
301
|
+
|
302
|
+
def test_find_ids
|
303
|
+
firm = Firm.all.merge!(:order => "id").first
|
304
|
+
|
305
|
+
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find }
|
306
|
+
|
307
|
+
client = firm.clients.find(2)
|
308
|
+
assert_kind_of Client, client
|
309
|
+
|
310
|
+
client_ary = firm.clients.find([2])
|
311
|
+
assert_kind_of Array, client_ary
|
312
|
+
assert_equal client, client_ary.first
|
313
|
+
|
314
|
+
client_ary = firm.clients.find(2, 3)
|
315
|
+
assert_kind_of Array, client_ary
|
316
|
+
assert_equal 2, client_ary.size
|
317
|
+
assert_equal client, client_ary.first
|
318
|
+
|
319
|
+
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
|
320
|
+
end
|
321
|
+
|
322
|
+
def test_find_all
|
323
|
+
firm = Firm.all.merge!(:order => "id").first
|
324
|
+
assert_equal 2, firm.clients.where("#{QUOTED_TYPE} = 'Client'").to_a.length
|
325
|
+
assert_equal 1, firm.clients.where("name = 'Summit'").to_a.length
|
326
|
+
end
|
327
|
+
|
328
|
+
def test_find_each
|
329
|
+
firm = companies(:first_firm)
|
330
|
+
|
331
|
+
assert ! firm.clients.loaded?
|
332
|
+
|
333
|
+
assert_queries(3) do
|
334
|
+
firm.clients.find_each(:batch_size => 1) {|c| assert_equal firm.id, c.firm_id }
|
335
|
+
end
|
336
|
+
|
337
|
+
assert ! firm.clients.loaded?
|
338
|
+
end
|
339
|
+
|
340
|
+
def test_find_each_with_conditions
|
341
|
+
firm = companies(:first_firm)
|
342
|
+
|
343
|
+
assert_queries(2) do
|
344
|
+
firm.clients.where(name: 'Microsoft').find_each(batch_size: 1) do |c|
|
345
|
+
assert_equal firm.id, c.firm_id
|
346
|
+
assert_equal "Microsoft", c.name
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
assert ! firm.clients.loaded?
|
351
|
+
end
|
352
|
+
|
353
|
+
def test_find_in_batches
|
354
|
+
firm = companies(:first_firm)
|
355
|
+
|
356
|
+
assert ! firm.clients.loaded?
|
357
|
+
|
358
|
+
assert_queries(2) do
|
359
|
+
firm.clients.find_in_batches(:batch_size => 2) do |clients|
|
360
|
+
clients.each {|c| assert_equal firm.id, c.firm_id }
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
assert ! firm.clients.loaded?
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_find_all_sanitized
|
368
|
+
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
|
369
|
+
firm = Firm.all.merge!(:order => "id").first
|
370
|
+
summit = firm.clients.where("name = 'Summit'").to_a
|
371
|
+
assert_equal summit, firm.clients.where("name = ?", "Summit").to_a
|
372
|
+
assert_equal summit, firm.clients.where("name = :name", { :name => "Summit" }).to_a
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_find_first
|
376
|
+
firm = Firm.all.merge!(:order => "id").first
|
377
|
+
client2 = Client.find(2)
|
378
|
+
assert_equal firm.clients.first, firm.clients.order("id").first
|
379
|
+
assert_equal client2, firm.clients.where("#{QUOTED_TYPE} = 'Client'").order("id").first
|
380
|
+
end
|
381
|
+
|
382
|
+
def test_find_first_sanitized
|
383
|
+
firm = Firm.all.merge!(:order => "id").first
|
384
|
+
client2 = Client.find(2)
|
385
|
+
assert_equal client2, firm.clients.merge!(:where => ["#{QUOTED_TYPE} = ?", 'Client'], :order => "id").first
|
386
|
+
assert_equal client2, firm.clients.merge!(:where => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }], :order => "id").first
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_find_all_with_include_and_conditions
|
390
|
+
assert_nothing_raised do
|
391
|
+
Developer.all.merge!(:joins => :audit_logs, :where => {'audit_logs.message' => nil, :name => 'Smith'}).to_a
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
def test_find_in_collection
|
396
|
+
assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name
|
397
|
+
assert_raise(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_find_grouped
|
401
|
+
all_clients_of_firm1 = Client.all.merge!(:where => "firm_id = 1").to_a
|
402
|
+
grouped_clients_of_firm1 = Client.all.merge!(:where => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count').to_a
|
403
|
+
assert_equal 2, all_clients_of_firm1.size
|
404
|
+
assert_equal 1, grouped_clients_of_firm1.size
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_find_scoped_grouped
|
408
|
+
assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.size
|
409
|
+
assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length
|
410
|
+
assert_equal 2, companies(:first_firm).clients_grouped_by_name.size
|
411
|
+
assert_equal 2, companies(:first_firm).clients_grouped_by_name.length
|
412
|
+
end
|
413
|
+
|
414
|
+
def test_find_scoped_grouped_having
|
415
|
+
assert_equal 1, authors(:david).popular_grouped_posts.length
|
416
|
+
assert_equal 0, authors(:mary).popular_grouped_posts.length
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_default_select
|
420
|
+
assert_equal Comment.column_names.sort, posts(:welcome).comments.first.attributes.keys.sort
|
421
|
+
end
|
422
|
+
|
423
|
+
def test_select_query_method
|
424
|
+
assert_equal ['id'], posts(:welcome).comments.select(:id).first.attributes.keys
|
425
|
+
end
|
426
|
+
|
427
|
+
def test_adding
|
428
|
+
force_signal37_to_load_all_clients_of_firm
|
429
|
+
natural = Client.new("name" => "Natural Company")
|
430
|
+
companies(:first_firm).clients_of_firm << natural
|
431
|
+
assert_equal 2, companies(:first_firm).clients_of_firm.size # checking via the collection
|
432
|
+
assert_equal 2, companies(:first_firm).clients_of_firm(true).size # checking using the db
|
433
|
+
assert_equal natural, companies(:first_firm).clients_of_firm.last
|
434
|
+
end
|
435
|
+
|
436
|
+
def test_adding_using_create
|
437
|
+
first_firm = companies(:first_firm)
|
438
|
+
assert_equal 2, first_firm.plain_clients.size
|
439
|
+
first_firm.plain_clients.create(:name => "Natural Company")
|
440
|
+
assert_equal 3, first_firm.plain_clients.length
|
441
|
+
assert_equal 3, first_firm.plain_clients.size
|
442
|
+
end
|
443
|
+
|
444
|
+
def test_create_with_bang_on_has_many_when_parent_is_new_raises
|
445
|
+
assert_raise(ActiveRecord::RecordNotSaved) do
|
446
|
+
firm = Firm.new
|
447
|
+
firm.plain_clients.create! :name=>"Whoever"
|
448
|
+
end
|
449
|
+
end
|
450
|
+
|
451
|
+
def test_regular_create_on_has_many_when_parent_is_new_raises
|
452
|
+
assert_raise(ActiveRecord::RecordNotSaved) do
|
453
|
+
firm = Firm.new
|
454
|
+
firm.plain_clients.create :name=>"Whoever"
|
455
|
+
end
|
456
|
+
end
|
457
|
+
|
458
|
+
def test_create_with_bang_on_has_many_raises_when_record_not_saved
|
459
|
+
assert_raise(ActiveRecord::RecordInvalid) do
|
460
|
+
firm = Firm.all.merge!(:order => "id").first
|
461
|
+
firm.plain_clients.create!
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
def test_create_with_bang_on_habtm_when_parent_is_new_raises
|
466
|
+
assert_raise(ActiveRecord::RecordNotSaved) do
|
467
|
+
Developer.new("name" => "Aredridel").projects.create!
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_adding_a_mismatch_class
|
472
|
+
assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
|
473
|
+
assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
|
474
|
+
assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
|
475
|
+
end
|
476
|
+
|
477
|
+
def test_adding_a_collection
|
478
|
+
force_signal37_to_load_all_clients_of_firm
|
479
|
+
companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")])
|
480
|
+
assert_equal 3, companies(:first_firm).clients_of_firm.size
|
481
|
+
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
|
482
|
+
end
|
483
|
+
|
484
|
+
def test_transactions_when_adding_to_persisted
|
485
|
+
good = Client.new(:name => "Good")
|
486
|
+
bad = Client.new(:name => "Bad", :raise_on_save => true)
|
487
|
+
|
488
|
+
begin
|
489
|
+
companies(:first_firm).clients_of_firm.concat(good, bad)
|
490
|
+
rescue Client::RaisedOnSave
|
491
|
+
end
|
492
|
+
|
493
|
+
assert !companies(:first_firm).clients_of_firm(true).include?(good)
|
494
|
+
end
|
495
|
+
|
496
|
+
def test_transactions_when_adding_to_new_record
|
497
|
+
assert_no_queries do
|
498
|
+
firm = Firm.new
|
499
|
+
firm.clients_of_firm.concat(Client.new("name" => "Natural Company"))
|
500
|
+
end
|
501
|
+
end
|
502
|
+
|
503
|
+
def test_new_aliased_to_build
|
504
|
+
company = companies(:first_firm)
|
505
|
+
new_client = assert_no_queries { company.clients_of_firm.new("name" => "Another Client") }
|
506
|
+
assert !company.clients_of_firm.loaded?
|
507
|
+
|
508
|
+
assert_equal "Another Client", new_client.name
|
509
|
+
assert !new_client.persisted?
|
510
|
+
assert_equal new_client, company.clients_of_firm.last
|
511
|
+
end
|
512
|
+
|
513
|
+
def test_build
|
514
|
+
company = companies(:first_firm)
|
515
|
+
new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") }
|
516
|
+
assert !company.clients_of_firm.loaded?
|
517
|
+
|
518
|
+
assert_equal "Another Client", new_client.name
|
519
|
+
assert !new_client.persisted?
|
520
|
+
assert_equal new_client, company.clients_of_firm.last
|
521
|
+
end
|
522
|
+
|
523
|
+
def test_collection_size_after_building
|
524
|
+
company = companies(:first_firm) # company already has one client
|
525
|
+
company.clients_of_firm.build("name" => "Another Client")
|
526
|
+
company.clients_of_firm.build("name" => "Yet Another Client")
|
527
|
+
assert_equal 3, company.clients_of_firm.size
|
528
|
+
end
|
529
|
+
|
530
|
+
def test_collection_not_empty_after_building
|
531
|
+
company = companies(:first_firm)
|
532
|
+
assert_predicate company.contracts, :empty?
|
533
|
+
company.contracts.build
|
534
|
+
assert_not_predicate company.contracts, :empty?
|
535
|
+
end
|
536
|
+
|
537
|
+
def test_collection_size_twice_for_regressions
|
538
|
+
post = posts(:thinking)
|
539
|
+
assert_equal 0, post.readers.size
|
540
|
+
# This test needs a post that has no readers, we assert it to ensure it holds,
|
541
|
+
# but need to reload the post because the very call to #size hides the bug.
|
542
|
+
post.reload
|
543
|
+
post.readers.build
|
544
|
+
size1 = post.readers.size
|
545
|
+
size2 = post.readers.size
|
546
|
+
assert_equal size1, size2
|
547
|
+
end
|
548
|
+
|
549
|
+
def test_build_many
|
550
|
+
company = companies(:first_firm)
|
551
|
+
new_clients = assert_no_queries { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }
|
552
|
+
assert_equal 2, new_clients.size
|
553
|
+
end
|
554
|
+
|
555
|
+
def test_build_followed_by_save_does_not_load_target
|
556
|
+
companies(:first_firm).clients_of_firm.build("name" => "Another Client")
|
557
|
+
assert companies(:first_firm).save
|
558
|
+
assert !companies(:first_firm).clients_of_firm.loaded?
|
559
|
+
end
|
560
|
+
|
561
|
+
def test_build_without_loading_association
|
562
|
+
first_topic = topics(:first)
|
563
|
+
Reply.column_names
|
564
|
+
|
565
|
+
assert_equal 1, first_topic.replies.length
|
566
|
+
|
567
|
+
assert_no_queries do
|
568
|
+
first_topic.replies.build(:title => "Not saved", :content => "Superstars")
|
569
|
+
assert_equal 2, first_topic.replies.size
|
570
|
+
end
|
571
|
+
|
572
|
+
assert_equal 2, first_topic.replies.to_ary.size
|
573
|
+
end
|
574
|
+
|
575
|
+
def test_build_via_block
|
576
|
+
company = companies(:first_firm)
|
577
|
+
new_client = assert_no_queries { company.clients_of_firm.build {|client| client.name = "Another Client" } }
|
578
|
+
assert !company.clients_of_firm.loaded?
|
579
|
+
|
580
|
+
assert_equal "Another Client", new_client.name
|
581
|
+
assert !new_client.persisted?
|
582
|
+
assert_equal new_client, company.clients_of_firm.last
|
583
|
+
end
|
584
|
+
|
585
|
+
def test_build_many_via_block
|
586
|
+
company = companies(:first_firm)
|
587
|
+
new_clients = assert_no_queries do
|
588
|
+
company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) do |client|
|
589
|
+
client.name = "changed"
|
590
|
+
end
|
591
|
+
end
|
592
|
+
|
593
|
+
assert_equal 2, new_clients.size
|
594
|
+
assert_equal "changed", new_clients.first.name
|
595
|
+
assert_equal "changed", new_clients.last.name
|
596
|
+
end
|
597
|
+
|
598
|
+
def test_create_without_loading_association
|
599
|
+
first_firm = companies(:first_firm)
|
600
|
+
Firm.column_names
|
601
|
+
Client.column_names
|
602
|
+
|
603
|
+
assert_equal 1, first_firm.clients_of_firm.size
|
604
|
+
first_firm.clients_of_firm.reset
|
605
|
+
|
606
|
+
assert_queries(1) do
|
607
|
+
first_firm.clients_of_firm.create(:name => "Superstars")
|
608
|
+
end
|
609
|
+
|
610
|
+
assert_equal 2, first_firm.clients_of_firm.size
|
611
|
+
end
|
612
|
+
|
613
|
+
def test_create
|
614
|
+
force_signal37_to_load_all_clients_of_firm
|
615
|
+
new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
|
616
|
+
assert new_client.persisted?
|
617
|
+
assert_equal new_client, companies(:first_firm).clients_of_firm.last
|
618
|
+
assert_equal new_client, companies(:first_firm).clients_of_firm(true).last
|
619
|
+
end
|
620
|
+
|
621
|
+
def test_create_many
|
622
|
+
companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
|
623
|
+
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
|
624
|
+
end
|
625
|
+
|
626
|
+
def test_create_followed_by_save_does_not_load_target
|
627
|
+
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
|
628
|
+
assert companies(:first_firm).save
|
629
|
+
assert !companies(:first_firm).clients_of_firm.loaded?
|
630
|
+
end
|
631
|
+
|
632
|
+
def test_deleting
|
633
|
+
force_signal37_to_load_all_clients_of_firm
|
634
|
+
companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
|
635
|
+
assert_equal 0, companies(:first_firm).clients_of_firm.size
|
636
|
+
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
|
637
|
+
end
|
638
|
+
|
639
|
+
def test_deleting_before_save
|
640
|
+
new_firm = Firm.new("name" => "A New Firm, Inc.")
|
641
|
+
new_client = new_firm.clients_of_firm.build("name" => "Another Client")
|
642
|
+
assert_equal 1, new_firm.clients_of_firm.size
|
643
|
+
new_firm.clients_of_firm.delete(new_client)
|
644
|
+
assert_equal 0, new_firm.clients_of_firm.size
|
645
|
+
end
|
646
|
+
|
647
|
+
def test_deleting_updates_counter_cache
|
648
|
+
topic = Topic.order("id ASC").first
|
649
|
+
assert_equal topic.replies.to_a.size, topic.replies_count
|
650
|
+
|
651
|
+
topic.replies.delete(topic.replies.first)
|
652
|
+
topic.reload
|
653
|
+
assert_equal topic.replies.to_a.size, topic.replies_count
|
654
|
+
end
|
655
|
+
|
656
|
+
def test_pushing_association_updates_counter_cache
|
657
|
+
topic = Topic.order("id ASC").first
|
658
|
+
reply = Reply.create!
|
659
|
+
|
660
|
+
assert_difference "topic.reload.replies_count", 1 do
|
661
|
+
topic.replies << reply
|
662
|
+
end
|
663
|
+
end
|
664
|
+
|
665
|
+
def test_deleting_updates_counter_cache_without_dependent_option
|
666
|
+
post = posts(:welcome)
|
667
|
+
|
668
|
+
assert_difference "post.reload.taggings_count", -1 do
|
669
|
+
post.taggings.delete(post.taggings.first)
|
670
|
+
end
|
671
|
+
end
|
672
|
+
|
673
|
+
def test_deleting_updates_counter_cache_with_dependent_delete_all
|
674
|
+
post = posts(:welcome)
|
675
|
+
post.update_columns(taggings_with_delete_all_count: post.taggings_count)
|
676
|
+
|
677
|
+
assert_difference "post.reload.taggings_with_delete_all_count", -1 do
|
678
|
+
post.taggings_with_delete_all.delete(post.taggings_with_delete_all.first)
|
679
|
+
end
|
680
|
+
end
|
681
|
+
|
682
|
+
def test_deleting_updates_counter_cache_with_dependent_destroy
|
683
|
+
post = posts(:welcome)
|
684
|
+
post.update_columns(taggings_with_destroy_count: post.taggings_count)
|
685
|
+
|
686
|
+
assert_difference "post.reload.taggings_with_destroy_count", -1 do
|
687
|
+
post.taggings_with_destroy.delete(post.taggings_with_destroy.first)
|
688
|
+
end
|
689
|
+
end
|
690
|
+
|
691
|
+
def test_custom_named_counter_cache
|
692
|
+
topic = topics(:first)
|
693
|
+
|
694
|
+
assert_difference "topic.reload.replies_count", -1 do
|
695
|
+
topic.approved_replies.clear
|
696
|
+
end
|
697
|
+
end
|
698
|
+
|
699
|
+
def test_calling_update_attributes_on_id_changes_the_counter_cache
|
700
|
+
topic = Topic.order("id ASC").first
|
701
|
+
original_count = topic.replies.to_a.size
|
702
|
+
assert_equal original_count, topic.replies_count
|
703
|
+
|
704
|
+
first_reply = topic.replies.first
|
705
|
+
first_reply.update_attributes(:parent_id => nil)
|
706
|
+
assert_equal original_count - 1, topic.reload.replies_count
|
707
|
+
|
708
|
+
first_reply.update_attributes(:parent_id => topic.id)
|
709
|
+
assert_equal original_count, topic.reload.replies_count
|
710
|
+
end
|
711
|
+
|
712
|
+
def test_calling_update_attributes_changing_ids_doesnt_change_counter_cache
|
713
|
+
topic1 = Topic.find(1)
|
714
|
+
topic2 = Topic.find(3)
|
715
|
+
original_count1 = topic1.replies.to_a.size
|
716
|
+
original_count2 = topic2.replies.to_a.size
|
717
|
+
|
718
|
+
reply1 = topic1.replies.first
|
719
|
+
reply2 = topic2.replies.first
|
720
|
+
|
721
|
+
reply1.update_attributes(:parent_id => topic2.id)
|
722
|
+
assert_equal original_count1 - 1, topic1.reload.replies_count
|
723
|
+
assert_equal original_count2 + 1, topic2.reload.replies_count
|
724
|
+
|
725
|
+
reply2.update_attributes(:parent_id => topic1.id)
|
726
|
+
assert_equal original_count1, topic1.reload.replies_count
|
727
|
+
assert_equal original_count2, topic2.reload.replies_count
|
728
|
+
end
|
729
|
+
|
730
|
+
def test_deleting_a_collection
|
731
|
+
force_signal37_to_load_all_clients_of_firm
|
732
|
+
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
|
733
|
+
assert_equal 2, companies(:first_firm).clients_of_firm.size
|
734
|
+
companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
|
735
|
+
assert_equal 0, companies(:first_firm).clients_of_firm.size
|
736
|
+
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
|
737
|
+
end
|
738
|
+
|
739
|
+
def test_delete_all
|
740
|
+
force_signal37_to_load_all_clients_of_firm
|
741
|
+
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
|
742
|
+
clients = companies(:first_firm).clients_of_firm.to_a
|
743
|
+
assert_equal 2, clients.count
|
744
|
+
deleted = companies(:first_firm).clients_of_firm.delete_all
|
745
|
+
assert_equal clients.sort_by(&:id), deleted.sort_by(&:id)
|
746
|
+
assert_equal 0, companies(:first_firm).clients_of_firm.size
|
747
|
+
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
|
748
|
+
end
|
749
|
+
|
750
|
+
def test_delete_all_with_not_yet_loaded_association_collection
|
751
|
+
force_signal37_to_load_all_clients_of_firm
|
752
|
+
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
|
753
|
+
assert_equal 2, companies(:first_firm).clients_of_firm.size
|
754
|
+
companies(:first_firm).clients_of_firm.reset
|
755
|
+
companies(:first_firm).clients_of_firm.delete_all
|
756
|
+
assert_equal 0, companies(:first_firm).clients_of_firm.size
|
757
|
+
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
|
758
|
+
end
|
759
|
+
|
760
|
+
def test_transaction_when_deleting_persisted
|
761
|
+
good = Client.new(:name => "Good")
|
762
|
+
bad = Client.new(:name => "Bad", :raise_on_destroy => true)
|
763
|
+
|
764
|
+
companies(:first_firm).clients_of_firm = [good, bad]
|
765
|
+
|
766
|
+
begin
|
767
|
+
companies(:first_firm).clients_of_firm.destroy(good, bad)
|
768
|
+
rescue Client::RaisedOnDestroy
|
769
|
+
end
|
770
|
+
|
771
|
+
assert_equal [good, bad], companies(:first_firm).clients_of_firm(true)
|
772
|
+
end
|
773
|
+
|
774
|
+
def test_transaction_when_deleting_new_record
|
775
|
+
assert_no_queries do
|
776
|
+
firm = Firm.new
|
777
|
+
client = Client.new("name" => "New Client")
|
778
|
+
firm.clients_of_firm << client
|
779
|
+
firm.clients_of_firm.destroy(client)
|
780
|
+
end
|
781
|
+
end
|
782
|
+
|
783
|
+
def test_clearing_an_association_collection
|
784
|
+
firm = companies(:first_firm)
|
785
|
+
client_id = firm.clients_of_firm.first.id
|
786
|
+
assert_equal 1, firm.clients_of_firm.size
|
787
|
+
|
788
|
+
firm.clients_of_firm.clear
|
789
|
+
|
790
|
+
assert_equal 0, firm.clients_of_firm.size
|
791
|
+
assert_equal 0, firm.clients_of_firm(true).size
|
792
|
+
assert_equal [], Client.destroyed_client_ids[firm.id]
|
793
|
+
|
794
|
+
# Should not be destroyed since the association is not dependent.
|
795
|
+
assert_nothing_raised do
|
796
|
+
assert_nil Client.find(client_id).firm
|
797
|
+
end
|
798
|
+
end
|
799
|
+
|
800
|
+
def test_clearing_updates_counter_cache
|
801
|
+
topic = Topic.first
|
802
|
+
|
803
|
+
assert_difference 'topic.reload.replies_count', -1 do
|
804
|
+
topic.replies.clear
|
805
|
+
end
|
806
|
+
end
|
807
|
+
|
808
|
+
def test_clearing_updates_counter_cache_when_inverse_counter_cache_is_a_symbol_with_dependent_destroy
|
809
|
+
car = Car.first
|
810
|
+
car.engines.create!
|
811
|
+
|
812
|
+
assert_difference 'car.reload.engines_count', -1 do
|
813
|
+
car.engines.clear
|
814
|
+
end
|
815
|
+
end
|
816
|
+
|
817
|
+
def test_clearing_a_dependent_association_collection
|
818
|
+
firm = companies(:first_firm)
|
819
|
+
client_id = firm.dependent_clients_of_firm.first.id
|
820
|
+
assert_equal 1, firm.dependent_clients_of_firm.size
|
821
|
+
assert_equal 1, Client.find_by_id(client_id).client_of
|
822
|
+
|
823
|
+
# :nullify is called on each client
|
824
|
+
firm.dependent_clients_of_firm.clear
|
825
|
+
|
826
|
+
assert_equal 0, firm.dependent_clients_of_firm.size
|
827
|
+
assert_equal 0, firm.dependent_clients_of_firm(true).size
|
828
|
+
assert_equal [], Client.destroyed_client_ids[firm.id]
|
829
|
+
|
830
|
+
# Should be destroyed since the association is dependent.
|
831
|
+
assert_nil Client.find_by_id(client_id).client_of
|
832
|
+
end
|
833
|
+
|
834
|
+
def test_delete_all_with_option_delete_all
|
835
|
+
firm = companies(:first_firm)
|
836
|
+
client_id = firm.dependent_clients_of_firm.first.id
|
837
|
+
firm.dependent_clients_of_firm.delete_all(:delete_all)
|
838
|
+
assert_nil Client.find_by_id(client_id)
|
839
|
+
end
|
840
|
+
|
841
|
+
def test_delete_all_accepts_limited_parameters
|
842
|
+
firm = companies(:first_firm)
|
843
|
+
assert_raise(ArgumentError) do
|
844
|
+
firm.dependent_clients_of_firm.delete_all(:destroy)
|
845
|
+
end
|
846
|
+
end
|
847
|
+
|
848
|
+
def test_clearing_an_exclusively_dependent_association_collection
|
849
|
+
firm = companies(:first_firm)
|
850
|
+
client_id = firm.exclusively_dependent_clients_of_firm.first.id
|
851
|
+
assert_equal 1, firm.exclusively_dependent_clients_of_firm.size
|
852
|
+
|
853
|
+
assert_equal [], Client.destroyed_client_ids[firm.id]
|
854
|
+
|
855
|
+
# :exclusively_dependent means each client is deleted directly from
|
856
|
+
# the database without looping through them calling destroy.
|
857
|
+
firm.exclusively_dependent_clients_of_firm.clear
|
858
|
+
|
859
|
+
assert_equal 0, firm.exclusively_dependent_clients_of_firm.size
|
860
|
+
assert_equal 0, firm.exclusively_dependent_clients_of_firm(true).size
|
861
|
+
# no destroy-filters should have been called
|
862
|
+
assert_equal [], Client.destroyed_client_ids[firm.id]
|
863
|
+
|
864
|
+
# Should be destroyed since the association is exclusively dependent.
|
865
|
+
assert_nil Client.find_by_id(client_id)
|
866
|
+
end
|
867
|
+
|
868
|
+
def test_dependent_association_respects_optional_conditions_on_delete
|
869
|
+
firm = companies(:odegy)
|
870
|
+
Client.create(:client_of => firm.id, :name => "BigShot Inc.")
|
871
|
+
Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
|
872
|
+
# only one of two clients is included in the association due to the :conditions key
|
873
|
+
assert_equal 2, Client.where(client_of: firm.id).size
|
874
|
+
assert_equal 1, firm.dependent_conditional_clients_of_firm.size
|
875
|
+
firm.destroy
|
876
|
+
# only the correctly associated client should have been deleted
|
877
|
+
assert_equal 1, Client.where(client_of: firm.id).size
|
878
|
+
end
|
879
|
+
|
880
|
+
def test_dependent_association_respects_optional_sanitized_conditions_on_delete
|
881
|
+
firm = companies(:odegy)
|
882
|
+
Client.create(:client_of => firm.id, :name => "BigShot Inc.")
|
883
|
+
Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
|
884
|
+
# only one of two clients is included in the association due to the :conditions key
|
885
|
+
assert_equal 2, Client.where(client_of: firm.id).size
|
886
|
+
assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
|
887
|
+
firm.destroy
|
888
|
+
# only the correctly associated client should have been deleted
|
889
|
+
assert_equal 1, Client.where(client_of: firm.id).size
|
890
|
+
end
|
891
|
+
|
892
|
+
def test_dependent_association_respects_optional_hash_conditions_on_delete
|
893
|
+
firm = companies(:odegy)
|
894
|
+
Client.create(:client_of => firm.id, :name => "BigShot Inc.")
|
895
|
+
Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
|
896
|
+
# only one of two clients is included in the association due to the :conditions key
|
897
|
+
assert_equal 2, Client.where(client_of: firm.id).size
|
898
|
+
assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
|
899
|
+
firm.destroy
|
900
|
+
# only the correctly associated client should have been deleted
|
901
|
+
assert_equal 1, Client.where(client_of: firm.id).size
|
902
|
+
end
|
903
|
+
|
904
|
+
def test_delete_all_association_with_primary_key_deletes_correct_records
|
905
|
+
firm = Firm.first
|
906
|
+
# break the vanilla firm_id foreign key
|
907
|
+
assert_equal 2, firm.clients.count
|
908
|
+
firm.clients.first.update_columns(firm_id: nil)
|
909
|
+
assert_equal 1, firm.clients(true).count
|
910
|
+
assert_equal 1, firm.clients_using_primary_key_with_delete_all.count
|
911
|
+
old_record = firm.clients_using_primary_key_with_delete_all.first
|
912
|
+
firm = Firm.first
|
913
|
+
firm.destroy
|
914
|
+
assert_nil Client.find_by_id(old_record.id)
|
915
|
+
end
|
916
|
+
|
917
|
+
def test_creation_respects_hash_condition
|
918
|
+
ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build
|
919
|
+
|
920
|
+
assert ms_client.save
|
921
|
+
assert_equal 'Microsoft', ms_client.name
|
922
|
+
|
923
|
+
another_ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.create
|
924
|
+
|
925
|
+
assert another_ms_client.persisted?
|
926
|
+
assert_equal 'Microsoft', another_ms_client.name
|
927
|
+
end
|
928
|
+
|
929
|
+
def test_clearing_without_initial_access
|
930
|
+
firm = companies(:first_firm)
|
931
|
+
|
932
|
+
firm.clients_of_firm.clear
|
933
|
+
|
934
|
+
assert_equal 0, firm.clients_of_firm.size
|
935
|
+
assert_equal 0, firm.clients_of_firm(true).size
|
936
|
+
end
|
937
|
+
|
938
|
+
def test_deleting_a_item_which_is_not_in_the_collection
|
939
|
+
force_signal37_to_load_all_clients_of_firm
|
940
|
+
summit = Client.find_by_name('Summit')
|
941
|
+
companies(:first_firm).clients_of_firm.delete(summit)
|
942
|
+
assert_equal 1, companies(:first_firm).clients_of_firm.size
|
943
|
+
assert_equal 1, companies(:first_firm).clients_of_firm(true).size
|
944
|
+
assert_equal 2, summit.client_of
|
945
|
+
end
|
946
|
+
|
947
|
+
def test_deleting_by_fixnum_id
|
948
|
+
david = Developer.find(1)
|
949
|
+
|
950
|
+
assert_difference 'david.projects.count', -1 do
|
951
|
+
assert_equal 1, david.projects.delete(1).size
|
952
|
+
end
|
953
|
+
|
954
|
+
assert_equal 1, david.projects.size
|
955
|
+
end
|
956
|
+
|
957
|
+
def test_deleting_by_string_id
|
958
|
+
david = Developer.find(1)
|
959
|
+
|
960
|
+
assert_difference 'david.projects.count', -1 do
|
961
|
+
assert_equal 1, david.projects.delete('1').size
|
962
|
+
end
|
963
|
+
|
964
|
+
assert_equal 1, david.projects.size
|
965
|
+
end
|
966
|
+
|
967
|
+
def test_deleting_self_type_mismatch
|
968
|
+
david = Developer.find(1)
|
969
|
+
david.projects.reload
|
970
|
+
assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
|
971
|
+
end
|
972
|
+
|
973
|
+
def test_destroying
|
974
|
+
force_signal37_to_load_all_clients_of_firm
|
975
|
+
|
976
|
+
assert_difference "Client.count", -1 do
|
977
|
+
companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first)
|
978
|
+
end
|
979
|
+
|
980
|
+
assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
|
981
|
+
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
|
982
|
+
end
|
983
|
+
|
984
|
+
def test_destroying_by_fixnum_id
|
985
|
+
force_signal37_to_load_all_clients_of_firm
|
986
|
+
|
987
|
+
assert_difference "Client.count", -1 do
|
988
|
+
companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id)
|
989
|
+
end
|
990
|
+
|
991
|
+
assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
|
992
|
+
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
|
993
|
+
end
|
994
|
+
|
995
|
+
def test_destroying_by_string_id
|
996
|
+
force_signal37_to_load_all_clients_of_firm
|
997
|
+
|
998
|
+
assert_difference "Client.count", -1 do
|
999
|
+
companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id.to_s)
|
1000
|
+
end
|
1001
|
+
|
1002
|
+
assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
|
1003
|
+
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
|
1004
|
+
end
|
1005
|
+
|
1006
|
+
def test_destroying_a_collection
|
1007
|
+
force_signal37_to_load_all_clients_of_firm
|
1008
|
+
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
|
1009
|
+
assert_equal 2, companies(:first_firm).clients_of_firm.size
|
1010
|
+
|
1011
|
+
assert_difference "Client.count", -2 do
|
1012
|
+
companies(:first_firm).clients_of_firm.destroy([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
|
1013
|
+
end
|
1014
|
+
|
1015
|
+
assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
|
1016
|
+
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
|
1017
|
+
end
|
1018
|
+
|
1019
|
+
def test_destroy_all
|
1020
|
+
force_signal37_to_load_all_clients_of_firm
|
1021
|
+
clients = companies(:first_firm).clients_of_firm.to_a
|
1022
|
+
assert !clients.empty?, "37signals has clients after load"
|
1023
|
+
destroyed = companies(:first_firm).clients_of_firm.destroy_all
|
1024
|
+
assert_equal clients.sort_by(&:id), destroyed.sort_by(&:id)
|
1025
|
+
assert destroyed.all? { |client| client.frozen? }, "destroyed clients should be frozen"
|
1026
|
+
assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all"
|
1027
|
+
assert companies(:first_firm).clients_of_firm(true).empty?, "37signals has no clients after destroy all and refresh"
|
1028
|
+
end
|
1029
|
+
|
1030
|
+
def test_dependence
|
1031
|
+
firm = companies(:first_firm)
|
1032
|
+
assert_equal 2, firm.clients.size
|
1033
|
+
firm.destroy
|
1034
|
+
assert Client.all.merge!(:where => "firm_id=#{firm.id}").to_a.empty?
|
1035
|
+
end
|
1036
|
+
|
1037
|
+
def test_dependence_for_associations_with_hash_condition
|
1038
|
+
david = authors(:david)
|
1039
|
+
assert_difference('Post.count', -1) { assert david.destroy }
|
1040
|
+
end
|
1041
|
+
|
1042
|
+
def test_destroy_dependent_when_deleted_from_association
|
1043
|
+
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
|
1044
|
+
firm = Firm.all.merge!(:order => "id").first
|
1045
|
+
assert_equal 2, firm.clients.size
|
1046
|
+
|
1047
|
+
client = firm.clients.first
|
1048
|
+
firm.clients.delete(client)
|
1049
|
+
|
1050
|
+
assert_raise(ActiveRecord::RecordNotFound) { Client.find(client.id) }
|
1051
|
+
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(client.id) }
|
1052
|
+
assert_equal 1, firm.clients.size
|
1053
|
+
end
|
1054
|
+
|
1055
|
+
def test_three_levels_of_dependence
|
1056
|
+
topic = Topic.create "title" => "neat and simple"
|
1057
|
+
reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it"
|
1058
|
+
reply.replies.create "title" => "neat and simple", "content" => "ain't complaining"
|
1059
|
+
|
1060
|
+
assert_nothing_raised { topic.destroy }
|
1061
|
+
end
|
1062
|
+
|
1063
|
+
uses_transaction :test_dependence_with_transaction_support_on_failure
|
1064
|
+
def test_dependence_with_transaction_support_on_failure
|
1065
|
+
firm = companies(:first_firm)
|
1066
|
+
clients = firm.clients
|
1067
|
+
assert_equal 2, clients.length
|
1068
|
+
clients.last.instance_eval { def overwrite_to_raise() raise "Trigger rollback" end }
|
1069
|
+
|
1070
|
+
firm.destroy rescue "do nothing"
|
1071
|
+
|
1072
|
+
assert_equal 2, Client.all.merge!(:where => "firm_id=#{firm.id}").to_a.size
|
1073
|
+
end
|
1074
|
+
|
1075
|
+
def test_dependence_on_account
|
1076
|
+
num_accounts = Account.count
|
1077
|
+
companies(:first_firm).destroy
|
1078
|
+
assert_equal num_accounts - 1, Account.count
|
1079
|
+
end
|
1080
|
+
|
1081
|
+
def test_depends_and_nullify
|
1082
|
+
num_accounts = Account.count
|
1083
|
+
|
1084
|
+
core = companies(:rails_core)
|
1085
|
+
assert_equal accounts(:rails_core_account), core.account
|
1086
|
+
assert_equal companies(:leetsoft, :jadedpixel), core.companies
|
1087
|
+
core.destroy
|
1088
|
+
assert_nil accounts(:rails_core_account).reload.firm_id
|
1089
|
+
assert_nil companies(:leetsoft).reload.client_of
|
1090
|
+
assert_nil companies(:jadedpixel).reload.client_of
|
1091
|
+
|
1092
|
+
assert_equal num_accounts, Account.count
|
1093
|
+
end
|
1094
|
+
|
1095
|
+
def test_restrict_with_exception
|
1096
|
+
firm = RestrictedWithExceptionFirm.create!(:name => 'restrict')
|
1097
|
+
firm.companies.create(:name => 'child')
|
1098
|
+
|
1099
|
+
assert !firm.companies.empty?
|
1100
|
+
assert_raise(ActiveRecord::DeleteRestrictionError) { firm.destroy }
|
1101
|
+
assert RestrictedWithExceptionFirm.exists?(:name => 'restrict')
|
1102
|
+
assert firm.companies.exists?(:name => 'child')
|
1103
|
+
end
|
1104
|
+
|
1105
|
+
def test_restrict_with_error
|
1106
|
+
firm = RestrictedWithErrorFirm.create!(:name => 'restrict')
|
1107
|
+
firm.companies.create(:name => 'child')
|
1108
|
+
|
1109
|
+
assert !firm.companies.empty?
|
1110
|
+
|
1111
|
+
firm.destroy
|
1112
|
+
|
1113
|
+
assert !firm.errors.empty?
|
1114
|
+
|
1115
|
+
assert_equal "Cannot delete record because dependent companies exist", firm.errors[:base].first
|
1116
|
+
assert RestrictedWithErrorFirm.exists?(:name => 'restrict')
|
1117
|
+
assert firm.companies.exists?(:name => 'child')
|
1118
|
+
end
|
1119
|
+
|
1120
|
+
def test_included_in_collection
|
1121
|
+
assert_equal true, companies(:first_firm).clients.include?(Client.find(2))
|
1122
|
+
end
|
1123
|
+
|
1124
|
+
def test_included_in_collection_for_new_records
|
1125
|
+
client = Client.create(:name => 'Persisted')
|
1126
|
+
assert_nil client.client_of
|
1127
|
+
assert_equal false, Firm.new.clients_of_firm.include?(client),
|
1128
|
+
'includes a client that does not belong to any firm'
|
1129
|
+
end
|
1130
|
+
|
1131
|
+
def test_adding_array_and_collection
|
1132
|
+
assert_nothing_raised { Firm.first.clients + Firm.all.last.clients }
|
1133
|
+
end
|
1134
|
+
|
1135
|
+
def test_replace_with_less
|
1136
|
+
firm = Firm.all.merge!(:order => "id").first
|
1137
|
+
firm.clients = [companies(:first_client)]
|
1138
|
+
assert firm.save, "Could not save firm"
|
1139
|
+
firm.reload
|
1140
|
+
assert_equal 1, firm.clients.length
|
1141
|
+
end
|
1142
|
+
|
1143
|
+
def test_replace_with_less_and_dependent_nullify
|
1144
|
+
num_companies = Company.count
|
1145
|
+
companies(:rails_core).companies = []
|
1146
|
+
assert_equal num_companies, Company.count
|
1147
|
+
end
|
1148
|
+
|
1149
|
+
def test_replace_with_new
|
1150
|
+
firm = Firm.all.merge!(:order => "id").first
|
1151
|
+
firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
|
1152
|
+
firm.save
|
1153
|
+
firm.reload
|
1154
|
+
assert_equal 2, firm.clients.length
|
1155
|
+
assert_equal false, firm.clients.include?(:first_client)
|
1156
|
+
end
|
1157
|
+
|
1158
|
+
def test_replace_failure
|
1159
|
+
firm = companies(:first_firm)
|
1160
|
+
account = Account.new
|
1161
|
+
orig_accounts = firm.accounts.to_a
|
1162
|
+
|
1163
|
+
assert !account.valid?
|
1164
|
+
assert !orig_accounts.empty?
|
1165
|
+
assert_raise ActiveRecord::RecordNotSaved do
|
1166
|
+
firm.accounts = [account]
|
1167
|
+
end
|
1168
|
+
assert_equal orig_accounts, firm.accounts
|
1169
|
+
end
|
1170
|
+
|
1171
|
+
def test_transactions_when_replacing_on_persisted
|
1172
|
+
good = Client.new(:name => "Good")
|
1173
|
+
bad = Client.new(:name => "Bad", :raise_on_save => true)
|
1174
|
+
|
1175
|
+
companies(:first_firm).clients_of_firm = [good]
|
1176
|
+
|
1177
|
+
begin
|
1178
|
+
companies(:first_firm).clients_of_firm = [bad]
|
1179
|
+
rescue Client::RaisedOnSave
|
1180
|
+
end
|
1181
|
+
|
1182
|
+
assert_equal [good], companies(:first_firm).clients_of_firm(true)
|
1183
|
+
end
|
1184
|
+
|
1185
|
+
def test_transactions_when_replacing_on_new_record
|
1186
|
+
assert_no_queries do
|
1187
|
+
firm = Firm.new
|
1188
|
+
firm.clients_of_firm = [Client.new("name" => "New Client")]
|
1189
|
+
end
|
1190
|
+
end
|
1191
|
+
|
1192
|
+
def test_get_ids
|
1193
|
+
assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids
|
1194
|
+
end
|
1195
|
+
|
1196
|
+
def test_get_ids_for_loaded_associations
|
1197
|
+
company = companies(:first_firm)
|
1198
|
+
company.clients(true)
|
1199
|
+
assert_queries(0) do
|
1200
|
+
company.client_ids
|
1201
|
+
company.client_ids
|
1202
|
+
end
|
1203
|
+
end
|
1204
|
+
|
1205
|
+
def test_get_ids_for_unloaded_associations_does_not_load_them
|
1206
|
+
company = companies(:first_firm)
|
1207
|
+
assert !company.clients.loaded?
|
1208
|
+
assert_equal [companies(:first_client).id, companies(:second_client).id], company.client_ids
|
1209
|
+
assert !company.clients.loaded?
|
1210
|
+
end
|
1211
|
+
|
1212
|
+
def test_get_ids_ignores_include_option
|
1213
|
+
assert_equal [readers(:michael_welcome).id], posts(:welcome).readers_with_person_ids
|
1214
|
+
end
|
1215
|
+
|
1216
|
+
def test_get_ids_for_ordered_association
|
1217
|
+
assert_equal [companies(:second_client).id, companies(:first_client).id], companies(:first_firm).clients_ordered_by_name_ids
|
1218
|
+
end
|
1219
|
+
|
1220
|
+
def test_get_ids_for_association_on_new_record_does_not_try_to_find_records
|
1221
|
+
Company.columns # Load schema information so we don't query below
|
1222
|
+
Contract.columns # if running just this test.
|
1223
|
+
|
1224
|
+
company = Company.new
|
1225
|
+
assert_queries(0) do
|
1226
|
+
company.contract_ids
|
1227
|
+
end
|
1228
|
+
|
1229
|
+
assert_equal [], company.contract_ids
|
1230
|
+
end
|
1231
|
+
|
1232
|
+
def test_set_ids_for_association_on_new_record_applies_association_correctly
|
1233
|
+
contract_a = Contract.create!
|
1234
|
+
contract_b = Contract.create!
|
1235
|
+
Contract.create! # another contract
|
1236
|
+
company = Company.new(:name => "Some Company")
|
1237
|
+
|
1238
|
+
company.contract_ids = [contract_a.id, contract_b.id]
|
1239
|
+
assert_equal [contract_a.id, contract_b.id], company.contract_ids
|
1240
|
+
assert_equal [contract_a, contract_b], company.contracts
|
1241
|
+
|
1242
|
+
company.save!
|
1243
|
+
assert_equal company, contract_a.reload.company
|
1244
|
+
assert_equal company, contract_b.reload.company
|
1245
|
+
end
|
1246
|
+
|
1247
|
+
def test_assign_ids_ignoring_blanks
|
1248
|
+
firm = Firm.create!(:name => 'Apple')
|
1249
|
+
firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
|
1250
|
+
firm.save!
|
1251
|
+
|
1252
|
+
assert_equal 2, firm.clients(true).size
|
1253
|
+
assert_equal true, firm.clients.include?(companies(:second_client))
|
1254
|
+
end
|
1255
|
+
|
1256
|
+
def test_get_ids_for_through
|
1257
|
+
assert_equal [comments(:eager_other_comment1).id], authors(:mary).comment_ids
|
1258
|
+
end
|
1259
|
+
|
1260
|
+
def test_modifying_a_through_a_has_many_should_raise
|
1261
|
+
[
|
1262
|
+
lambda { authors(:mary).comment_ids = [comments(:greetings).id, comments(:more_greetings).id] },
|
1263
|
+
lambda { authors(:mary).comments = [comments(:greetings), comments(:more_greetings)] },
|
1264
|
+
lambda { authors(:mary).comments << Comment.create!(:body => "Yay", :post_id => 424242) },
|
1265
|
+
lambda { authors(:mary).comments.delete(authors(:mary).comments.first) },
|
1266
|
+
].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }
|
1267
|
+
end
|
1268
|
+
|
1269
|
+
def test_dynamic_find_should_respect_association_order_for_through
|
1270
|
+
assert_equal Comment.find(10), authors(:david).comments_desc.where("comments.type = 'SpecialComment'").first
|
1271
|
+
assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type('SpecialComment')
|
1272
|
+
end
|
1273
|
+
|
1274
|
+
def test_has_many_through_respects_hash_conditions
|
1275
|
+
assert_equal authors(:david).hello_posts, authors(:david).hello_posts_with_hash_conditions
|
1276
|
+
assert_equal authors(:david).hello_post_comments, authors(:david).hello_post_comments_with_hash_conditions
|
1277
|
+
end
|
1278
|
+
|
1279
|
+
def test_include_uses_array_include_after_loaded
|
1280
|
+
firm = companies(:first_firm)
|
1281
|
+
firm.clients.load_target
|
1282
|
+
|
1283
|
+
client = firm.clients.first
|
1284
|
+
|
1285
|
+
assert_no_queries do
|
1286
|
+
assert firm.clients.loaded?
|
1287
|
+
assert_equal true, firm.clients.include?(client)
|
1288
|
+
end
|
1289
|
+
end
|
1290
|
+
|
1291
|
+
def test_include_checks_if_record_exists_if_target_not_loaded
|
1292
|
+
firm = companies(:first_firm)
|
1293
|
+
client = firm.clients.first
|
1294
|
+
|
1295
|
+
firm.reload
|
1296
|
+
assert ! firm.clients.loaded?
|
1297
|
+
assert_queries(1) do
|
1298
|
+
assert_equal true, firm.clients.include?(client)
|
1299
|
+
end
|
1300
|
+
assert ! firm.clients.loaded?
|
1301
|
+
end
|
1302
|
+
|
1303
|
+
def test_include_returns_false_for_non_matching_record_to_verify_scoping
|
1304
|
+
firm = companies(:first_firm)
|
1305
|
+
client = Client.create!(:name => 'Not Associated')
|
1306
|
+
|
1307
|
+
assert ! firm.clients.loaded?
|
1308
|
+
assert_equal false, firm.clients.include?(client)
|
1309
|
+
end
|
1310
|
+
|
1311
|
+
def test_calling_first_or_last_on_association_should_not_load_association
|
1312
|
+
firm = companies(:first_firm)
|
1313
|
+
firm.clients.first
|
1314
|
+
firm.clients.last
|
1315
|
+
assert !firm.clients.loaded?
|
1316
|
+
end
|
1317
|
+
|
1318
|
+
def test_calling_first_or_last_on_loaded_association_should_not_fetch_with_query
|
1319
|
+
firm = companies(:first_firm)
|
1320
|
+
firm.clients.load_target
|
1321
|
+
assert firm.clients.loaded?
|
1322
|
+
|
1323
|
+
assert_no_queries do
|
1324
|
+
firm.clients.first
|
1325
|
+
assert_equal 2, firm.clients.first(2).size
|
1326
|
+
firm.clients.last
|
1327
|
+
assert_equal 2, firm.clients.last(2).size
|
1328
|
+
end
|
1329
|
+
end
|
1330
|
+
|
1331
|
+
def test_calling_first_or_last_on_existing_record_with_build_should_load_association
|
1332
|
+
firm = companies(:first_firm)
|
1333
|
+
firm.clients.build(:name => 'Foo')
|
1334
|
+
assert !firm.clients.loaded?
|
1335
|
+
|
1336
|
+
assert_queries 1 do
|
1337
|
+
firm.clients.first
|
1338
|
+
firm.clients.last
|
1339
|
+
end
|
1340
|
+
|
1341
|
+
assert firm.clients.loaded?
|
1342
|
+
end
|
1343
|
+
|
1344
|
+
def test_calling_first_or_last_on_existing_record_with_create_should_not_load_association
|
1345
|
+
firm = companies(:first_firm)
|
1346
|
+
firm.clients.create(:name => 'Foo')
|
1347
|
+
assert !firm.clients.loaded?
|
1348
|
+
|
1349
|
+
assert_queries 2 do
|
1350
|
+
firm.clients.first
|
1351
|
+
firm.clients.last
|
1352
|
+
end
|
1353
|
+
|
1354
|
+
assert !firm.clients.loaded?
|
1355
|
+
end
|
1356
|
+
|
1357
|
+
def test_calling_first_or_last_on_new_record_should_not_run_queries
|
1358
|
+
firm = Firm.new
|
1359
|
+
|
1360
|
+
assert_no_queries do
|
1361
|
+
firm.clients.first
|
1362
|
+
firm.clients.last
|
1363
|
+
end
|
1364
|
+
end
|
1365
|
+
|
1366
|
+
def test_custom_primary_key_on_new_record_should_fetch_with_query
|
1367
|
+
author = Author.new(:name => "David")
|
1368
|
+
assert !author.essays.loaded?
|
1369
|
+
|
1370
|
+
assert_queries 1 do
|
1371
|
+
assert_equal 1, author.essays.size
|
1372
|
+
end
|
1373
|
+
|
1374
|
+
assert_equal author.essays, Essay.where(writer_id: "David")
|
1375
|
+
end
|
1376
|
+
|
1377
|
+
def test_has_many_custom_primary_key
|
1378
|
+
david = authors(:david)
|
1379
|
+
assert_equal david.essays, Essay.where(writer_id: "David")
|
1380
|
+
end
|
1381
|
+
|
1382
|
+
def test_has_many_assignment_with_custom_primary_key
|
1383
|
+
david = people(:david)
|
1384
|
+
|
1385
|
+
assert_equal ["A Modest Proposal"], david.essays.map(&:name)
|
1386
|
+
david.essays = [Essay.create!(name: "Remote Work" )]
|
1387
|
+
assert_equal ["Remote Work"], david.essays.map(&:name)
|
1388
|
+
end
|
1389
|
+
|
1390
|
+
def test_blank_custom_primary_key_on_new_record_should_not_run_queries
|
1391
|
+
author = Author.new
|
1392
|
+
assert !author.essays.loaded?
|
1393
|
+
|
1394
|
+
assert_queries 0 do
|
1395
|
+
assert_equal 0, author.essays.size
|
1396
|
+
end
|
1397
|
+
end
|
1398
|
+
|
1399
|
+
def test_calling_first_or_last_with_integer_on_association_should_load_association
|
1400
|
+
firm = companies(:first_firm)
|
1401
|
+
|
1402
|
+
assert_queries 1 do
|
1403
|
+
firm.clients.first(2)
|
1404
|
+
firm.clients.last(2)
|
1405
|
+
end
|
1406
|
+
|
1407
|
+
assert firm.clients.loaded?
|
1408
|
+
end
|
1409
|
+
|
1410
|
+
def test_calling_many_should_count_instead_of_loading_association
|
1411
|
+
firm = companies(:first_firm)
|
1412
|
+
assert_queries(1) do
|
1413
|
+
firm.clients.many? # use count query
|
1414
|
+
end
|
1415
|
+
assert !firm.clients.loaded?
|
1416
|
+
end
|
1417
|
+
|
1418
|
+
def test_calling_many_on_loaded_association_should_not_use_query
|
1419
|
+
firm = companies(:first_firm)
|
1420
|
+
firm.clients.collect # force load
|
1421
|
+
assert_no_queries { assert firm.clients.many? }
|
1422
|
+
end
|
1423
|
+
|
1424
|
+
def test_calling_many_should_defer_to_collection_if_using_a_block
|
1425
|
+
firm = companies(:first_firm)
|
1426
|
+
assert_queries(1) do
|
1427
|
+
firm.clients.expects(:size).never
|
1428
|
+
firm.clients.many? { true }
|
1429
|
+
end
|
1430
|
+
assert firm.clients.loaded?
|
1431
|
+
end
|
1432
|
+
|
1433
|
+
def test_calling_many_should_return_false_if_none_or_one
|
1434
|
+
firm = companies(:another_firm)
|
1435
|
+
assert !firm.clients_like_ms.many?
|
1436
|
+
assert_equal 0, firm.clients_like_ms.size
|
1437
|
+
|
1438
|
+
firm = companies(:first_firm)
|
1439
|
+
assert !firm.limited_clients.many?
|
1440
|
+
assert_equal 1, firm.limited_clients.size
|
1441
|
+
end
|
1442
|
+
|
1443
|
+
def test_calling_many_should_return_true_if_more_than_one
|
1444
|
+
firm = companies(:first_firm)
|
1445
|
+
assert firm.clients.many?
|
1446
|
+
assert_equal 2, firm.clients.size
|
1447
|
+
end
|
1448
|
+
|
1449
|
+
def test_joins_with_namespaced_model_should_use_correct_type
|
1450
|
+
old = ActiveRecord::Base.store_full_sti_class
|
1451
|
+
ActiveRecord::Base.store_full_sti_class = true
|
1452
|
+
|
1453
|
+
firm = Namespaced::Firm.create({ :name => 'Some Company' })
|
1454
|
+
firm.clients.create({ :name => 'Some Client' })
|
1455
|
+
|
1456
|
+
stats = Namespaced::Firm.all.merge!(
|
1457
|
+
:select => "#{Namespaced::Firm.table_name}.id, COUNT(#{Namespaced::Client.table_name}.id) AS num_clients",
|
1458
|
+
:joins => :clients,
|
1459
|
+
:group => "#{Namespaced::Firm.table_name}.id"
|
1460
|
+
).find firm.id
|
1461
|
+
assert_equal 1, stats.num_clients.to_i
|
1462
|
+
ensure
|
1463
|
+
ActiveRecord::Base.store_full_sti_class = old
|
1464
|
+
end
|
1465
|
+
|
1466
|
+
def test_association_proxy_transaction_method_starts_transaction_in_association_class
|
1467
|
+
Comment.expects(:transaction)
|
1468
|
+
Post.first.comments.transaction do
|
1469
|
+
# nothing
|
1470
|
+
end
|
1471
|
+
end
|
1472
|
+
|
1473
|
+
def test_sending_new_to_association_proxy_should_have_same_effect_as_calling_new
|
1474
|
+
client_association = companies(:first_firm).clients
|
1475
|
+
assert_equal client_association.new.attributes, client_association.send(:new).attributes
|
1476
|
+
end
|
1477
|
+
|
1478
|
+
def test_respond_to_private_class_methods
|
1479
|
+
client_association = companies(:first_firm).clients
|
1480
|
+
assert !client_association.respond_to?(:private_method)
|
1481
|
+
assert client_association.respond_to?(:private_method, true)
|
1482
|
+
end
|
1483
|
+
|
1484
|
+
def test_creating_using_primary_key
|
1485
|
+
firm = Firm.all.merge!(:order => "id").first
|
1486
|
+
client = firm.clients_using_primary_key.create!(:name => 'test')
|
1487
|
+
assert_equal firm.name, client.firm_name
|
1488
|
+
end
|
1489
|
+
|
1490
|
+
def test_defining_has_many_association_with_delete_all_dependency_lazily_evaluates_target_class
|
1491
|
+
ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
|
1492
|
+
class_eval(<<-EOF, __FILE__, __LINE__ + 1)
|
1493
|
+
class DeleteAllModel < ActiveRecord::Base
|
1494
|
+
has_many :nonentities, :dependent => :delete_all
|
1495
|
+
end
|
1496
|
+
EOF
|
1497
|
+
end
|
1498
|
+
|
1499
|
+
def test_defining_has_many_association_with_nullify_dependency_lazily_evaluates_target_class
|
1500
|
+
ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
|
1501
|
+
class_eval(<<-EOF, __FILE__, __LINE__ + 1)
|
1502
|
+
class NullifyModel < ActiveRecord::Base
|
1503
|
+
has_many :nonentities, :dependent => :nullify
|
1504
|
+
end
|
1505
|
+
EOF
|
1506
|
+
end
|
1507
|
+
|
1508
|
+
def test_attributes_are_being_set_when_initialized_from_has_many_association_with_where_clause
|
1509
|
+
new_comment = posts(:welcome).comments.where(:body => "Some content").build
|
1510
|
+
assert_equal new_comment.body, "Some content"
|
1511
|
+
end
|
1512
|
+
|
1513
|
+
def test_attributes_are_being_set_when_initialized_from_has_many_association_with_multiple_where_clauses
|
1514
|
+
new_comment = posts(:welcome).comments.where(:body => "Some content").where(:type => 'SpecialComment').build
|
1515
|
+
assert_equal new_comment.body, "Some content"
|
1516
|
+
assert_equal new_comment.type, "SpecialComment"
|
1517
|
+
assert_equal new_comment.post_id, posts(:welcome).id
|
1518
|
+
end
|
1519
|
+
|
1520
|
+
def test_include_method_in_has_many_association_should_return_true_for_instance_added_with_build
|
1521
|
+
post = Post.new
|
1522
|
+
comment = post.comments.build
|
1523
|
+
assert_equal true, post.comments.include?(comment)
|
1524
|
+
end
|
1525
|
+
|
1526
|
+
def test_load_target_respects_protected_attributes
|
1527
|
+
topic = Topic.create!
|
1528
|
+
reply = topic.replies.create(:title => "reply 1")
|
1529
|
+
reply.approved = false
|
1530
|
+
reply.save!
|
1531
|
+
|
1532
|
+
# Save with a different object instance, so the instance that's still held
|
1533
|
+
# in topic.relies doesn't know about the changed attribute.
|
1534
|
+
reply2 = Reply.find(reply.id)
|
1535
|
+
reply2.approved = true
|
1536
|
+
reply2.save!
|
1537
|
+
|
1538
|
+
# Force loading the collection from the db. This will merge the existing
|
1539
|
+
# object (reply) with what gets loaded from the db (which includes the
|
1540
|
+
# changed approved attribute). approved is a protected attribute, so if mass
|
1541
|
+
# assignment is used, it won't get updated and will still be false.
|
1542
|
+
first = topic.replies.to_a.first
|
1543
|
+
assert_equal reply.id, first.id
|
1544
|
+
assert_equal true, first.approved?
|
1545
|
+
end
|
1546
|
+
|
1547
|
+
def test_to_a_should_dup_target
|
1548
|
+
ary = topics(:first).replies.to_a
|
1549
|
+
target = topics(:first).replies.target
|
1550
|
+
|
1551
|
+
assert_not_equal target.object_id, ary.object_id
|
1552
|
+
end
|
1553
|
+
|
1554
|
+
def test_merging_with_custom_attribute_writer
|
1555
|
+
bulb = Bulb.new(:color => "red")
|
1556
|
+
assert_equal "RED!", bulb.color
|
1557
|
+
|
1558
|
+
car = Car.create!
|
1559
|
+
car.bulbs << bulb
|
1560
|
+
|
1561
|
+
assert_equal "RED!", car.bulbs.to_a.first.color
|
1562
|
+
end
|
1563
|
+
|
1564
|
+
def test_abstract_class_with_polymorphic_has_many
|
1565
|
+
post = SubStiPost.create! :title => "fooo", :body => "baa"
|
1566
|
+
tagging = Tagging.create! :taggable => post
|
1567
|
+
assert_equal [tagging], post.taggings
|
1568
|
+
end
|
1569
|
+
|
1570
|
+
def test_build_with_polymorphic_has_many_does_not_allow_to_override_type_and_id
|
1571
|
+
welcome = posts(:welcome)
|
1572
|
+
tagging = welcome.taggings.build(:taggable_id => 99, :taggable_type => 'ShouldNotChange')
|
1573
|
+
|
1574
|
+
assert_equal welcome.id, tagging.taggable_id
|
1575
|
+
assert_equal 'Post', tagging.taggable_type
|
1576
|
+
end
|
1577
|
+
|
1578
|
+
def test_dont_call_save_callbacks_twice_on_has_many
|
1579
|
+
firm = companies(:first_firm)
|
1580
|
+
contract = firm.contracts.create!
|
1581
|
+
|
1582
|
+
assert_equal 1, contract.hi_count
|
1583
|
+
assert_equal 1, contract.bye_count
|
1584
|
+
end
|
1585
|
+
|
1586
|
+
def test_association_attributes_are_available_to_after_initialize
|
1587
|
+
car = Car.create(:name => 'honda')
|
1588
|
+
bulb = car.bulbs.build
|
1589
|
+
|
1590
|
+
assert_equal car.id, bulb.attributes_after_initialize['car_id']
|
1591
|
+
end
|
1592
|
+
|
1593
|
+
def test_replace
|
1594
|
+
car = Car.create(:name => 'honda')
|
1595
|
+
bulb1 = car.bulbs.create
|
1596
|
+
bulb2 = Bulb.create
|
1597
|
+
|
1598
|
+
assert_equal [bulb1], car.bulbs
|
1599
|
+
car.bulbs.replace([bulb2])
|
1600
|
+
assert_equal [bulb2], car.bulbs
|
1601
|
+
assert_equal [bulb2], car.reload.bulbs
|
1602
|
+
end
|
1603
|
+
|
1604
|
+
def test_replace_returns_target
|
1605
|
+
car = Car.create(:name => 'honda')
|
1606
|
+
bulb1 = car.bulbs.create
|
1607
|
+
bulb2 = car.bulbs.create
|
1608
|
+
bulb3 = Bulb.create
|
1609
|
+
|
1610
|
+
assert_equal [bulb1, bulb2], car.bulbs
|
1611
|
+
result = car.bulbs.replace([bulb3, bulb1])
|
1612
|
+
assert_equal [bulb1, bulb3], car.bulbs
|
1613
|
+
assert_equal [bulb1, bulb3], result
|
1614
|
+
end
|
1615
|
+
|
1616
|
+
def test_collection_association_with_private_kernel_method
|
1617
|
+
firm = companies(:first_firm)
|
1618
|
+
assert_equal [accounts(:signals37)], firm.accounts.open
|
1619
|
+
end
|
1620
|
+
|
1621
|
+
test "first_or_initialize adds the record to the association" do
|
1622
|
+
firm = Firm.create! name: 'omg'
|
1623
|
+
client = firm.clients_of_firm.first_or_initialize
|
1624
|
+
assert_equal [client], firm.clients_of_firm
|
1625
|
+
end
|
1626
|
+
|
1627
|
+
test "first_or_create adds the record to the association" do
|
1628
|
+
firm = Firm.create! name: 'omg'
|
1629
|
+
firm.clients_of_firm.load_target
|
1630
|
+
client = firm.clients_of_firm.first_or_create name: 'lol'
|
1631
|
+
assert_equal [client], firm.clients_of_firm
|
1632
|
+
assert_equal [client], firm.reload.clients_of_firm
|
1633
|
+
end
|
1634
|
+
|
1635
|
+
test "delete_all, when not loaded, doesn't load the records" do
|
1636
|
+
post = posts(:welcome)
|
1637
|
+
|
1638
|
+
assert post.taggings_with_delete_all.count > 0
|
1639
|
+
assert !post.taggings_with_delete_all.loaded?
|
1640
|
+
|
1641
|
+
# 2 queries: one DELETE and another to update the counter cache
|
1642
|
+
assert_queries(2) do
|
1643
|
+
post.taggings_with_delete_all.delete_all
|
1644
|
+
end
|
1645
|
+
end
|
1646
|
+
|
1647
|
+
test "has many associations on new records use null relations" do
|
1648
|
+
post = Post.new
|
1649
|
+
|
1650
|
+
assert_no_queries do
|
1651
|
+
assert_equal [], post.comments
|
1652
|
+
assert_equal [], post.comments.where(body: 'omg')
|
1653
|
+
assert_equal [], post.comments.pluck(:body)
|
1654
|
+
assert_equal 0, post.comments.sum(:id)
|
1655
|
+
assert_equal 0, post.comments.count
|
1656
|
+
end
|
1657
|
+
end
|
1658
|
+
|
1659
|
+
test "collection proxy respects default scope" do
|
1660
|
+
author = authors(:mary)
|
1661
|
+
assert !author.first_posts.exists?
|
1662
|
+
end
|
1663
|
+
|
1664
|
+
test "association with extend option" do
|
1665
|
+
post = posts(:welcome)
|
1666
|
+
assert_equal "lifo", post.comments_with_extend.author
|
1667
|
+
assert_equal "hello", post.comments_with_extend.greeting
|
1668
|
+
end
|
1669
|
+
|
1670
|
+
test "association with extend option with multiple extensions" do
|
1671
|
+
post = posts(:welcome)
|
1672
|
+
assert_equal "lifo", post.comments_with_extend_2.author
|
1673
|
+
assert_equal "hello", post.comments_with_extend_2.greeting
|
1674
|
+
end
|
1675
|
+
|
1676
|
+
test "delete record with complex joins" do
|
1677
|
+
david = authors(:david)
|
1678
|
+
|
1679
|
+
post = david.posts.first
|
1680
|
+
post.type = 'PostWithSpecialCategorization'
|
1681
|
+
post.save
|
1682
|
+
|
1683
|
+
categorization = post.categorizations.first
|
1684
|
+
categorization.special = true
|
1685
|
+
categorization.save
|
1686
|
+
|
1687
|
+
assert_not_equal [], david.posts_with_special_categorizations
|
1688
|
+
david.posts_with_special_categorizations = []
|
1689
|
+
assert_equal [], david.posts_with_special_categorizations
|
1690
|
+
end
|
1691
|
+
|
1692
|
+
test "does not duplicate associations when used with natural primary keys" do
|
1693
|
+
speedometer = Speedometer.create!(id: '4')
|
1694
|
+
speedometer.minivans.create!(minivan_id: 'a-van-red' ,name: 'a van', color: 'red')
|
1695
|
+
|
1696
|
+
assert_equal 1, speedometer.minivans.to_a.size, "Only one association should be present:\n#{speedometer.minivans.to_a}"
|
1697
|
+
assert_equal 1, speedometer.reload.minivans.to_a.size
|
1698
|
+
end
|
1699
|
+
end
|