sequel 5.29.0 → 5.30.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +8 -1922
- data/doc/release_notes/5.30.0.txt +20 -0
- data/lib/sequel/adapters/shared/sqlite.rb +7 -1
- data/lib/sequel/database/transactions.rb +5 -9
- data/lib/sequel/version.rb +1 -1
- metadata +4 -368
- data/Rakefile +0 -151
- data/doc/release_notes/4.0.0.txt +0 -262
- data/doc/release_notes/4.1.0.txt +0 -85
- data/doc/release_notes/4.10.0.txt +0 -226
- data/doc/release_notes/4.11.0.txt +0 -147
- data/doc/release_notes/4.12.0.txt +0 -105
- data/doc/release_notes/4.13.0.txt +0 -169
- data/doc/release_notes/4.14.0.txt +0 -68
- data/doc/release_notes/4.15.0.txt +0 -56
- data/doc/release_notes/4.16.0.txt +0 -36
- data/doc/release_notes/4.17.0.txt +0 -38
- data/doc/release_notes/4.18.0.txt +0 -36
- data/doc/release_notes/4.19.0.txt +0 -45
- data/doc/release_notes/4.2.0.txt +0 -129
- data/doc/release_notes/4.20.0.txt +0 -79
- data/doc/release_notes/4.21.0.txt +0 -94
- data/doc/release_notes/4.22.0.txt +0 -72
- data/doc/release_notes/4.23.0.txt +0 -65
- data/doc/release_notes/4.24.0.txt +0 -99
- data/doc/release_notes/4.25.0.txt +0 -181
- data/doc/release_notes/4.26.0.txt +0 -44
- data/doc/release_notes/4.27.0.txt +0 -78
- data/doc/release_notes/4.28.0.txt +0 -57
- data/doc/release_notes/4.29.0.txt +0 -41
- data/doc/release_notes/4.3.0.txt +0 -40
- data/doc/release_notes/4.30.0.txt +0 -37
- data/doc/release_notes/4.31.0.txt +0 -57
- data/doc/release_notes/4.32.0.txt +0 -132
- data/doc/release_notes/4.33.0.txt +0 -88
- data/doc/release_notes/4.34.0.txt +0 -86
- data/doc/release_notes/4.35.0.txt +0 -130
- data/doc/release_notes/4.36.0.txt +0 -116
- data/doc/release_notes/4.37.0.txt +0 -50
- data/doc/release_notes/4.38.0.txt +0 -67
- data/doc/release_notes/4.39.0.txt +0 -127
- data/doc/release_notes/4.4.0.txt +0 -92
- data/doc/release_notes/4.40.0.txt +0 -179
- data/doc/release_notes/4.41.0.txt +0 -77
- data/doc/release_notes/4.42.0.txt +0 -221
- data/doc/release_notes/4.43.0.txt +0 -87
- data/doc/release_notes/4.44.0.txt +0 -125
- data/doc/release_notes/4.45.0.txt +0 -370
- data/doc/release_notes/4.46.0.txt +0 -404
- data/doc/release_notes/4.47.0.txt +0 -56
- data/doc/release_notes/4.48.0.txt +0 -293
- data/doc/release_notes/4.49.0.txt +0 -222
- data/doc/release_notes/4.5.0.txt +0 -34
- data/doc/release_notes/4.6.0.txt +0 -30
- data/doc/release_notes/4.7.0.txt +0 -103
- data/doc/release_notes/4.8.0.txt +0 -175
- data/doc/release_notes/4.9.0.txt +0 -190
- data/spec/adapter_spec.rb +0 -4
- data/spec/adapters/db2_spec.rb +0 -170
- data/spec/adapters/mssql_spec.rb +0 -828
- data/spec/adapters/mysql_spec.rb +0 -1060
- data/spec/adapters/oracle_spec.rb +0 -371
- data/spec/adapters/postgres_spec.rb +0 -4476
- data/spec/adapters/spec_helper.rb +0 -44
- data/spec/adapters/sqlanywhere_spec.rb +0 -97
- data/spec/adapters/sqlite_spec.rb +0 -652
- data/spec/bin_spec.rb +0 -278
- data/spec/core/connection_pool_spec.rb +0 -1250
- data/spec/core/database_spec.rb +0 -2915
- data/spec/core/dataset_spec.rb +0 -5544
- data/spec/core/deprecated_spec.rb +0 -70
- data/spec/core/expression_filters_spec.rb +0 -1498
- data/spec/core/mock_adapter_spec.rb +0 -722
- data/spec/core/object_graph_spec.rb +0 -336
- data/spec/core/placeholder_literalizer_spec.rb +0 -166
- data/spec/core/schema_generator_spec.rb +0 -214
- data/spec/core/schema_spec.rb +0 -1844
- data/spec/core/spec_helper.rb +0 -24
- data/spec/core/version_spec.rb +0 -14
- data/spec/core_extensions_spec.rb +0 -763
- data/spec/core_model_spec.rb +0 -2
- data/spec/core_spec.rb +0 -1
- data/spec/deprecation_helper.rb +0 -30
- data/spec/extensions/accessed_columns_spec.rb +0 -51
- data/spec/extensions/active_model_spec.rb +0 -99
- data/spec/extensions/after_initialize_spec.rb +0 -28
- data/spec/extensions/any_not_empty_spec.rb +0 -23
- data/spec/extensions/arbitrary_servers_spec.rb +0 -109
- data/spec/extensions/association_dependencies_spec.rb +0 -125
- data/spec/extensions/association_multi_add_remove_spec.rb +0 -1041
- data/spec/extensions/association_pks_spec.rb +0 -423
- data/spec/extensions/association_proxies_spec.rb +0 -100
- data/spec/extensions/auto_literal_strings_spec.rb +0 -205
- data/spec/extensions/auto_validations_spec.rb +0 -229
- data/spec/extensions/blacklist_security_spec.rb +0 -95
- data/spec/extensions/blank_spec.rb +0 -69
- data/spec/extensions/boolean_readers_spec.rb +0 -93
- data/spec/extensions/boolean_subsets_spec.rb +0 -47
- data/spec/extensions/caching_spec.rb +0 -273
- data/spec/extensions/caller_logging_spec.rb +0 -52
- data/spec/extensions/class_table_inheritance_spec.rb +0 -750
- data/spec/extensions/column_conflicts_spec.rb +0 -75
- data/spec/extensions/column_select_spec.rb +0 -129
- data/spec/extensions/columns_introspection_spec.rb +0 -90
- data/spec/extensions/columns_updated_spec.rb +0 -35
- data/spec/extensions/composition_spec.rb +0 -248
- data/spec/extensions/connection_expiration_spec.rb +0 -151
- data/spec/extensions/connection_validator_spec.rb +0 -144
- data/spec/extensions/constant_sql_override_spec.rb +0 -24
- data/spec/extensions/constraint_validations_plugin_spec.rb +0 -300
- data/spec/extensions/constraint_validations_spec.rb +0 -439
- data/spec/extensions/core_refinements_spec.rb +0 -528
- data/spec/extensions/csv_serializer_spec.rb +0 -183
- data/spec/extensions/current_datetime_timestamp_spec.rb +0 -27
- data/spec/extensions/dataset_associations_spec.rb +0 -365
- data/spec/extensions/dataset_source_alias_spec.rb +0 -51
- data/spec/extensions/date_arithmetic_spec.rb +0 -181
- data/spec/extensions/datetime_parse_to_time_spec.rb +0 -169
- data/spec/extensions/def_dataset_method_spec.rb +0 -100
- data/spec/extensions/defaults_setter_spec.rb +0 -150
- data/spec/extensions/delay_add_association_spec.rb +0 -73
- data/spec/extensions/dirty_spec.rb +0 -222
- data/spec/extensions/duplicate_columns_handler_spec.rb +0 -104
- data/spec/extensions/eager_each_spec.rb +0 -62
- data/spec/extensions/eager_graph_eager_spec.rb +0 -100
- data/spec/extensions/empty_array_consider_nulls_spec.rb +0 -24
- data/spec/extensions/empty_failure_backtraces_spec.rb +0 -60
- data/spec/extensions/error_splitter_spec.rb +0 -18
- data/spec/extensions/error_sql_spec.rb +0 -20
- data/spec/extensions/escaped_like_spec.rb +0 -40
- data/spec/extensions/eval_inspect_spec.rb +0 -81
- data/spec/extensions/exclude_or_null_spec.rb +0 -15
- data/spec/extensions/finder_spec.rb +0 -260
- data/spec/extensions/force_encoding_spec.rb +0 -126
- data/spec/extensions/freeze_datasets_spec.rb +0 -31
- data/spec/extensions/graph_each_spec.rb +0 -113
- data/spec/extensions/hook_class_methods_spec.rb +0 -402
- data/spec/extensions/identifier_mangling_spec.rb +0 -201
- data/spec/extensions/implicit_subquery_spec.rb +0 -58
- data/spec/extensions/index_caching_spec.rb +0 -66
- data/spec/extensions/inflector_spec.rb +0 -183
- data/spec/extensions/input_transformer_spec.rb +0 -69
- data/spec/extensions/insert_conflict_spec.rb +0 -103
- data/spec/extensions/insert_returning_select_spec.rb +0 -72
- data/spec/extensions/instance_filters_spec.rb +0 -79
- data/spec/extensions/instance_hooks_spec.rb +0 -246
- data/spec/extensions/integer64_spec.rb +0 -22
- data/spec/extensions/inverted_subsets_spec.rb +0 -33
- data/spec/extensions/json_serializer_spec.rb +0 -346
- data/spec/extensions/lazy_attributes_spec.rb +0 -183
- data/spec/extensions/list_spec.rb +0 -291
- data/spec/extensions/looser_typecasting_spec.rb +0 -43
- data/spec/extensions/many_through_many_spec.rb +0 -2177
- data/spec/extensions/migration_spec.rb +0 -864
- data/spec/extensions/modification_detection_spec.rb +0 -93
- data/spec/extensions/mssql_optimistic_locking_spec.rb +0 -92
- data/spec/extensions/named_timezones_spec.rb +0 -218
- data/spec/extensions/nested_attributes_spec.rb +0 -815
- data/spec/extensions/null_dataset_spec.rb +0 -85
- data/spec/extensions/optimistic_locking_spec.rb +0 -127
- data/spec/extensions/pagination_spec.rb +0 -116
- data/spec/extensions/pg_array_associations_spec.rb +0 -802
- data/spec/extensions/pg_array_ops_spec.rb +0 -144
- data/spec/extensions/pg_array_spec.rb +0 -398
- data/spec/extensions/pg_auto_constraint_validations_spec.rb +0 -209
- data/spec/extensions/pg_enum_spec.rb +0 -118
- data/spec/extensions/pg_extended_date_support_spec.rb +0 -126
- data/spec/extensions/pg_hstore_ops_spec.rb +0 -238
- data/spec/extensions/pg_hstore_spec.rb +0 -219
- data/spec/extensions/pg_inet_ops_spec.rb +0 -102
- data/spec/extensions/pg_inet_spec.rb +0 -72
- data/spec/extensions/pg_interval_spec.rb +0 -103
- data/spec/extensions/pg_json_ops_spec.rb +0 -356
- data/spec/extensions/pg_json_spec.rb +0 -451
- data/spec/extensions/pg_loose_count_spec.rb +0 -23
- data/spec/extensions/pg_range_ops_spec.rb +0 -60
- data/spec/extensions/pg_range_spec.rb +0 -600
- data/spec/extensions/pg_row_ops_spec.rb +0 -61
- data/spec/extensions/pg_row_plugin_spec.rb +0 -60
- data/spec/extensions/pg_row_spec.rb +0 -363
- data/spec/extensions/pg_static_cache_updater_spec.rb +0 -93
- data/spec/extensions/pg_timestamptz_spec.rb +0 -17
- data/spec/extensions/prepared_statements_safe_spec.rb +0 -66
- data/spec/extensions/prepared_statements_spec.rb +0 -177
- data/spec/extensions/pretty_table_spec.rb +0 -123
- data/spec/extensions/query_spec.rb +0 -94
- data/spec/extensions/rcte_tree_spec.rb +0 -387
- data/spec/extensions/round_timestamps_spec.rb +0 -39
- data/spec/extensions/s_spec.rb +0 -60
- data/spec/extensions/schema_caching_spec.rb +0 -64
- data/spec/extensions/schema_dumper_spec.rb +0 -870
- data/spec/extensions/select_remove_spec.rb +0 -38
- data/spec/extensions/sequel_4_dataset_methods_spec.rb +0 -121
- data/spec/extensions/serialization_modification_detection_spec.rb +0 -98
- data/spec/extensions/serialization_spec.rb +0 -365
- data/spec/extensions/server_block_spec.rb +0 -135
- data/spec/extensions/server_logging_spec.rb +0 -45
- data/spec/extensions/sharding_spec.rb +0 -197
- data/spec/extensions/shared_caching_spec.rb +0 -151
- data/spec/extensions/single_table_inheritance_spec.rb +0 -347
- data/spec/extensions/singular_table_names_spec.rb +0 -22
- data/spec/extensions/skip_create_refresh_spec.rb +0 -18
- data/spec/extensions/spec_helper.rb +0 -70
- data/spec/extensions/split_array_nil_spec.rb +0 -24
- data/spec/extensions/split_values_spec.rb +0 -57
- data/spec/extensions/sql_comments_spec.rb +0 -33
- data/spec/extensions/sql_expr_spec.rb +0 -59
- data/spec/extensions/static_cache_cache_spec.rb +0 -35
- data/spec/extensions/static_cache_spec.rb +0 -471
- data/spec/extensions/string_agg_spec.rb +0 -90
- data/spec/extensions/string_date_time_spec.rb +0 -95
- data/spec/extensions/string_stripper_spec.rb +0 -68
- data/spec/extensions/subclasses_spec.rb +0 -79
- data/spec/extensions/subset_conditions_spec.rb +0 -38
- data/spec/extensions/symbol_aref_refinement_spec.rb +0 -28
- data/spec/extensions/symbol_as_refinement_spec.rb +0 -21
- data/spec/extensions/synchronize_sql_spec.rb +0 -124
- data/spec/extensions/table_select_spec.rb +0 -83
- data/spec/extensions/tactical_eager_loading_spec.rb +0 -402
- data/spec/extensions/thread_local_timezones_spec.rb +0 -67
- data/spec/extensions/throw_failures_spec.rb +0 -74
- data/spec/extensions/timestamps_spec.rb +0 -209
- data/spec/extensions/to_dot_spec.rb +0 -153
- data/spec/extensions/touch_spec.rb +0 -226
- data/spec/extensions/tree_spec.rb +0 -334
- data/spec/extensions/typecast_on_load_spec.rb +0 -86
- data/spec/extensions/unlimited_update_spec.rb +0 -21
- data/spec/extensions/update_or_create_spec.rb +0 -83
- data/spec/extensions/update_primary_key_spec.rb +0 -105
- data/spec/extensions/update_refresh_spec.rb +0 -59
- data/spec/extensions/uuid_spec.rb +0 -101
- data/spec/extensions/validate_associated_spec.rb +0 -52
- data/spec/extensions/validation_class_methods_spec.rb +0 -1040
- data/spec/extensions/validation_contexts_spec.rb +0 -31
- data/spec/extensions/validation_helpers_spec.rb +0 -525
- data/spec/extensions/whitelist_security_spec.rb +0 -157
- data/spec/extensions/xml_serializer_spec.rb +0 -213
- data/spec/files/bad_down_migration/001_create_alt_basic.rb +0 -4
- data/spec/files/bad_down_migration/002_create_alt_advanced.rb +0 -4
- data/spec/files/bad_timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/bad_timestamped_migrations/1273253851_create_nodes.rb +0 -9
- data/spec/files/bad_timestamped_migrations/1273253853_3_create_users.rb +0 -3
- data/spec/files/bad_up_migration/001_create_alt_basic.rb +0 -4
- data/spec/files/bad_up_migration/002_create_alt_advanced.rb +0 -3
- data/spec/files/convert_to_timestamp_migrations/001_create_sessions.rb +0 -9
- data/spec/files/convert_to_timestamp_migrations/002_create_nodes.rb +0 -9
- data/spec/files/convert_to_timestamp_migrations/003_3_create_users.rb +0 -4
- data/spec/files/convert_to_timestamp_migrations/1273253850_create_artists.rb +0 -9
- data/spec/files/convert_to_timestamp_migrations/1273253852_create_albums.rb +0 -9
- data/spec/files/double_migration/001_create_sessions.rb +0 -9
- data/spec/files/double_migration/002_create_nodes.rb +0 -19
- data/spec/files/double_migration/003_3_create_users.rb +0 -4
- data/spec/files/duplicate_integer_migrations/001_create_alt_advanced.rb +0 -4
- data/spec/files/duplicate_integer_migrations/001_create_alt_basic.rb +0 -4
- data/spec/files/duplicate_timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/duplicate_timestamped_migrations/1273253853_create_nodes.rb +0 -9
- data/spec/files/duplicate_timestamped_migrations/1273253853_create_users.rb +0 -4
- data/spec/files/empty_migration/001_create_sessions.rb +0 -9
- data/spec/files/empty_migration/002_create_nodes.rb +0 -0
- data/spec/files/empty_migration/003_3_create_users.rb +0 -4
- data/spec/files/integer_migrations/001_create_sessions.rb +0 -9
- data/spec/files/integer_migrations/002_create_nodes.rb +0 -9
- data/spec/files/integer_migrations/003_3_create_users.rb +0 -4
- data/spec/files/interleaved_timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/interleaved_timestamped_migrations/1273253850_create_artists.rb +0 -9
- data/spec/files/interleaved_timestamped_migrations/1273253851_create_nodes.rb +0 -9
- data/spec/files/interleaved_timestamped_migrations/1273253852_create_albums.rb +0 -9
- data/spec/files/interleaved_timestamped_migrations/1273253853_3_create_users.rb +0 -4
- data/spec/files/missing_integer_migrations/001_create_alt_basic.rb +0 -4
- data/spec/files/missing_integer_migrations/003_create_alt_advanced.rb +0 -4
- data/spec/files/missing_timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/missing_timestamped_migrations/1273253853_3_create_users.rb +0 -4
- data/spec/files/reversible_migrations/001_reversible.rb +0 -5
- data/spec/files/reversible_migrations/002_reversible.rb +0 -5
- data/spec/files/reversible_migrations/003_reversible.rb +0 -5
- data/spec/files/reversible_migrations/004_reversible.rb +0 -5
- data/spec/files/reversible_migrations/005_reversible.rb +0 -10
- data/spec/files/reversible_migrations/006_reversible.rb +0 -10
- data/spec/files/reversible_migrations/007_reversible.rb +0 -10
- data/spec/files/timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/timestamped_migrations/1273253851_create_nodes.rb +0 -9
- data/spec/files/timestamped_migrations/1273253853_3_create_users.rb +0 -4
- data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +0 -4
- data/spec/files/transaction_specified_migrations/002_create_basic.rb +0 -4
- data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +0 -3
- data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +0 -3
- data/spec/files/uppercase_timestamped_migrations/1273253849_CREATE_SESSIONS.RB +0 -9
- data/spec/files/uppercase_timestamped_migrations/1273253851_CREATE_NODES.RB +0 -9
- data/spec/files/uppercase_timestamped_migrations/1273253853_3_CREATE_USERS.RB +0 -4
- data/spec/guards_helper.rb +0 -59
- data/spec/integration/associations_test.rb +0 -2597
- data/spec/integration/database_test.rb +0 -113
- data/spec/integration/dataset_test.rb +0 -2037
- data/spec/integration/eager_loader_test.rb +0 -687
- data/spec/integration/migrator_test.rb +0 -262
- data/spec/integration/model_test.rb +0 -203
- data/spec/integration/plugin_test.rb +0 -2423
- data/spec/integration/prepared_statement_test.rb +0 -405
- data/spec/integration/schema_test.rb +0 -903
- data/spec/integration/spec_helper.rb +0 -71
- data/spec/integration/timezone_test.rb +0 -86
- data/spec/integration/transaction_test.rb +0 -603
- data/spec/integration/type_test.rb +0 -127
- data/spec/model/association_reflection_spec.rb +0 -803
- data/spec/model/associations_spec.rb +0 -4738
- data/spec/model/base_spec.rb +0 -875
- data/spec/model/class_dataset_methods_spec.rb +0 -146
- data/spec/model/dataset_methods_spec.rb +0 -198
- data/spec/model/eager_loading_spec.rb +0 -2377
- data/spec/model/hooks_spec.rb +0 -370
- data/spec/model/inflector_spec.rb +0 -26
- data/spec/model/model_spec.rb +0 -956
- data/spec/model/plugins_spec.rb +0 -429
- data/spec/model/record_spec.rb +0 -2118
- data/spec/model/spec_helper.rb +0 -46
- data/spec/model/validations_spec.rb +0 -220
- data/spec/model_no_assoc_spec.rb +0 -1
- data/spec/model_spec.rb +0 -1
- data/spec/plugin_spec.rb +0 -1
- data/spec/sequel_coverage.rb +0 -15
- data/spec/sequel_warning.rb +0 -5
- data/spec/spec_config.rb +0 -12
data/spec/model/hooks_spec.rb
DELETED
@@ -1,370 +0,0 @@
|
|
1
|
-
require_relative "spec_helper"
|
2
|
-
|
3
|
-
describe "Model#before_create && Model#after_create" do
|
4
|
-
before do
|
5
|
-
@c = Class.new(Sequel::Model(:items)) do
|
6
|
-
columns :x
|
7
|
-
set_primary_key :x
|
8
|
-
unrestrict_primary_key
|
9
|
-
|
10
|
-
def after_create
|
11
|
-
DB << "BLAH after"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
DB.reset
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should be called around new record creation" do
|
18
|
-
@c.send(:define_method, :before_create){DB << "BLAH before"}
|
19
|
-
@c.create(:x => 2)
|
20
|
-
DB.sqls.must_equal ['BLAH before', 'INSERT INTO items (x) VALUES (2)', 'SELECT * FROM items WHERE x = 2', 'BLAH after']
|
21
|
-
end
|
22
|
-
|
23
|
-
it ".create should cancel the save and raise an error if before_create calls cancel_action and raise_on_save_failure is true" do
|
24
|
-
@c.send(:define_method, :before_create){cancel_action 'not good'}
|
25
|
-
proc{@c.create(:x => 2)}.must_raise(Sequel::HookFailed, 'not good')
|
26
|
-
DB.sqls.must_equal []
|
27
|
-
@c.load(:id => 2233).save
|
28
|
-
end
|
29
|
-
|
30
|
-
it ".create should cancel the save and return nil if before_create calls cancel_action and raise_on_save_failure is false" do
|
31
|
-
@c.send(:define_method, :before_create){cancel_action}
|
32
|
-
@c.raise_on_save_failure = false
|
33
|
-
@c.create(:x => 2).must_be_nil
|
34
|
-
DB.sqls.must_equal []
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe "Model#before_update && Model#after_update" do
|
39
|
-
before do
|
40
|
-
@c = Class.new(Sequel::Model(:items)) do
|
41
|
-
columns :id, :x
|
42
|
-
def after_update
|
43
|
-
DB << "BLAH after"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
DB.reset
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should be called around record update" do
|
50
|
-
@c.send(:define_method, :before_update){DB << "BLAH before"}
|
51
|
-
m = @c.load(:id => 2233, :x=>123)
|
52
|
-
m.save
|
53
|
-
DB.sqls.must_equal ['BLAH before', 'UPDATE items SET x = 123 WHERE (id = 2233)', 'BLAH after']
|
54
|
-
end
|
55
|
-
|
56
|
-
it "#save should cancel the save and raise an error if before_update calls cancel_action and raise_on_save_failure is true" do
|
57
|
-
@c.send(:define_method, :before_update){cancel_action}
|
58
|
-
proc{@c.load(:id => 2233).save}.must_raise(Sequel::HookFailed)
|
59
|
-
DB.sqls.must_equal []
|
60
|
-
end
|
61
|
-
|
62
|
-
it "#save should cancel the save and return nil if before_update calls cancel_action and raise_on_save_failure is false" do
|
63
|
-
@c.send(:define_method, :before_update){cancel_action}
|
64
|
-
@c.raise_on_save_failure = false
|
65
|
-
@c.load(:id => 2233).save.must_be_nil
|
66
|
-
DB.sqls.must_equal []
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe "Model#before_save && Model#after_save" do
|
71
|
-
before do
|
72
|
-
@c = Class.new(Sequel::Model(:items)) do
|
73
|
-
columns :x
|
74
|
-
def after_save
|
75
|
-
DB << "BLAH after"
|
76
|
-
end
|
77
|
-
end
|
78
|
-
DB.reset
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should be called around record update" do
|
82
|
-
@c.send(:define_method, :before_save){DB << "BLAH before"}
|
83
|
-
m = @c.load(:id => 2233, :x=>123)
|
84
|
-
m.save
|
85
|
-
DB.sqls.must_equal ['BLAH before', 'UPDATE items SET x = 123 WHERE (id = 2233)', 'BLAH after']
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should be called around record creation" do
|
89
|
-
@c.send(:define_method, :before_save){DB << "BLAH before"}
|
90
|
-
@c.set_primary_key :x
|
91
|
-
@c.unrestrict_primary_key
|
92
|
-
@c.create(:x => 2)
|
93
|
-
DB.sqls.must_equal ['BLAH before', 'INSERT INTO items (x) VALUES (2)', 'SELECT * FROM items WHERE x = 2', 'BLAH after']
|
94
|
-
end
|
95
|
-
|
96
|
-
it "#save should cancel the save and raise an error if before_save calls cancel_action and raise_on_failure option is true" do
|
97
|
-
@c.send(:define_method, :before_save){cancel_action}
|
98
|
-
@c.raise_on_save_failure = false
|
99
|
-
proc{@c.load(:id => 2233).save(:raise_on_failure => true)}.must_raise(Sequel::HookFailed)
|
100
|
-
DB.sqls.must_equal []
|
101
|
-
end
|
102
|
-
|
103
|
-
it "#save should cancel the save and return nil if before_save calls cancel_action and raise_on_save_failure is false" do
|
104
|
-
@c.send(:define_method, :before_save){cancel_action}
|
105
|
-
@c.raise_on_save_failure = false
|
106
|
-
@c.load(:id => 2233).save.must_be_nil
|
107
|
-
DB.sqls.must_equal []
|
108
|
-
end
|
109
|
-
|
110
|
-
it "#save should have a raised exception reference the model instance" do
|
111
|
-
@c.send(:define_method, :before_save){cancel_action}
|
112
|
-
proc{@c.create(:x => 2233)}.must_raise(Sequel::HookFailed){|e| e.model.must_equal @c.load(:x=>2233)}
|
113
|
-
DB.sqls.must_equal []
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
describe "Model#before_destroy && Model#after_destroy" do
|
118
|
-
before do
|
119
|
-
@c = Class.new(Sequel::Model(:items)) do
|
120
|
-
def after_destroy
|
121
|
-
DB << "BLAH after"
|
122
|
-
end
|
123
|
-
end
|
124
|
-
DB.reset
|
125
|
-
end
|
126
|
-
|
127
|
-
it "should be called around record destruction" do
|
128
|
-
@c.send(:define_method, :before_destroy){DB << "BLAH before"}
|
129
|
-
m = @c.load(:id => 2233)
|
130
|
-
m.destroy
|
131
|
-
DB.sqls.must_equal ['BLAH before', 'DELETE FROM items WHERE id = 2233', 'BLAH after']
|
132
|
-
end
|
133
|
-
|
134
|
-
it "#destroy should cancel the destroy and raise an error if before_destroy calls cancel_action and raise_on_save_failure is true" do
|
135
|
-
@c.send(:define_method, :before_destroy){cancel_action; true}
|
136
|
-
proc{@c.load(:id => 2233).destroy}.must_raise(Sequel::HookFailed)
|
137
|
-
DB.sqls.must_equal []
|
138
|
-
end
|
139
|
-
|
140
|
-
it "#destroy should cancel the destroy and return nil if before_destroy calls cancel_action and raise_on_save_failure is false" do
|
141
|
-
@c.send(:define_method, :before_destroy){cancel_action; true}
|
142
|
-
@c.raise_on_save_failure = false
|
143
|
-
@c.load(:id => 2233).destroy.must_be_nil
|
144
|
-
DB.sqls.must_equal []
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
describe "Model#before_validation && Model#after_validation" do
|
149
|
-
before do
|
150
|
-
@c = Class.new(Sequel::Model(:items)) do
|
151
|
-
columns :id
|
152
|
-
def after_validation
|
153
|
-
DB << "BLAH after"
|
154
|
-
end
|
155
|
-
|
156
|
-
def validate
|
157
|
-
errors.add(:id, 'not valid') unless id == 2233
|
158
|
-
end
|
159
|
-
end
|
160
|
-
DB.reset
|
161
|
-
end
|
162
|
-
|
163
|
-
it "should be called around validation" do
|
164
|
-
@c.send(:define_method, :before_validation){DB << "BLAH before"}
|
165
|
-
m = @c.load(:id => 2233)
|
166
|
-
m.must_be :valid?
|
167
|
-
DB.sqls.must_equal ['BLAH before', 'BLAH after']
|
168
|
-
|
169
|
-
m = @c.load(:id => 22)
|
170
|
-
m.wont_be :valid?
|
171
|
-
DB.sqls.must_equal ['BLAH before', 'BLAH after']
|
172
|
-
end
|
173
|
-
|
174
|
-
it "should be called when calling save" do
|
175
|
-
@c.send(:define_method, :before_validation){DB << "BLAH before"}
|
176
|
-
m = @c.load(:id => 2233, :x=>123)
|
177
|
-
m.save.must_equal m
|
178
|
-
DB.sqls.must_equal ['BLAH before', 'BLAH after', 'UPDATE items SET x = 123 WHERE (id = 2233)']
|
179
|
-
|
180
|
-
m = @c.load(:id => 22)
|
181
|
-
m.raise_on_save_failure = false
|
182
|
-
m.save.must_be_nil
|
183
|
-
DB.sqls.must_equal ['BLAH before', 'BLAH after']
|
184
|
-
end
|
185
|
-
|
186
|
-
it "#save should cancel the save and raise an error if before_validation calls cancel_action and raise_on_save_failure is true" do
|
187
|
-
@c.send(:define_method, :before_validation){cancel_action}
|
188
|
-
proc{@c.load(:id => 2233).save}.must_raise(Sequel::HookFailed)
|
189
|
-
DB.sqls.must_equal []
|
190
|
-
end
|
191
|
-
|
192
|
-
it "#save should cancel the save and return nil if before_validation calls cancel_action and raise_on_save_failure is false" do
|
193
|
-
@c.send(:define_method, :before_validation){cancel_action}
|
194
|
-
@c.raise_on_save_failure = false
|
195
|
-
@c.load(:id => 2233).save.must_be_nil
|
196
|
-
DB.sqls.must_equal []
|
197
|
-
end
|
198
|
-
|
199
|
-
it "#valid? should return false if before_validation calls cancel_action" do
|
200
|
-
@c.send(:define_method, :before_validation){cancel_action}
|
201
|
-
@c.load(:id => 2233).valid?.must_equal false
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
describe "Model around filters" do
|
206
|
-
before do
|
207
|
-
@c = Class.new(Sequel::Model(:items)) do
|
208
|
-
columns :id, :x
|
209
|
-
end
|
210
|
-
DB.reset
|
211
|
-
end
|
212
|
-
|
213
|
-
it "around_create should be called around new record creation" do
|
214
|
-
@c.class_eval do
|
215
|
-
def around_create
|
216
|
-
DB << 'ac_before'
|
217
|
-
super
|
218
|
-
DB << 'ac_after'
|
219
|
-
end
|
220
|
-
end
|
221
|
-
@c.create(:x => 2)
|
222
|
-
DB.sqls.must_equal ['ac_before', 'INSERT INTO items (x) VALUES (2)', "SELECT * FROM items WHERE id = 10", 'ac_after']
|
223
|
-
end
|
224
|
-
|
225
|
-
it "around_delete should be called around record destruction" do
|
226
|
-
@c.class_eval do
|
227
|
-
def around_destroy
|
228
|
-
DB << 'ad_before'
|
229
|
-
super
|
230
|
-
DB << 'ad_after'
|
231
|
-
end
|
232
|
-
end
|
233
|
-
@c.load(:id=>1, :x => 2).destroy
|
234
|
-
DB.sqls.must_equal ['ad_before', 'DELETE FROM items WHERE id = 1', 'ad_after']
|
235
|
-
end
|
236
|
-
|
237
|
-
it "around_update should be called around updating existing records" do
|
238
|
-
@c.class_eval do
|
239
|
-
def around_update
|
240
|
-
DB << 'au_before'
|
241
|
-
super
|
242
|
-
DB << 'au_after'
|
243
|
-
end
|
244
|
-
end
|
245
|
-
@c.load(:id=>1, :x => 2).save
|
246
|
-
DB.sqls.must_equal ['au_before', 'UPDATE items SET x = 2 WHERE (id = 1)', 'au_after']
|
247
|
-
end
|
248
|
-
|
249
|
-
it "around_save should be called around saving both new and existing records, around either after_create and after_update" do
|
250
|
-
@c.class_eval do
|
251
|
-
def around_update
|
252
|
-
DB << 'au_before'
|
253
|
-
super
|
254
|
-
DB << 'au_after'
|
255
|
-
end
|
256
|
-
def around_create
|
257
|
-
DB << 'ac_before'
|
258
|
-
super
|
259
|
-
DB << 'ac_after'
|
260
|
-
end
|
261
|
-
def around_save
|
262
|
-
DB << 'as_before'
|
263
|
-
super
|
264
|
-
DB << 'as_after'
|
265
|
-
end
|
266
|
-
end
|
267
|
-
@c.create(:x => 2)
|
268
|
-
DB.sqls.must_equal ['as_before', 'ac_before', 'INSERT INTO items (x) VALUES (2)', "SELECT * FROM items WHERE id = 10", 'ac_after', 'as_after']
|
269
|
-
@c.load(:id=>1, :x => 2).save
|
270
|
-
DB.sqls.must_equal ['as_before', 'au_before', 'UPDATE items SET x = 2 WHERE (id = 1)', 'au_after', 'as_after']
|
271
|
-
end
|
272
|
-
|
273
|
-
it "around_validation should be called around validating records" do
|
274
|
-
@c.class_eval do
|
275
|
-
def around_validation
|
276
|
-
DB << 'av_before'
|
277
|
-
super
|
278
|
-
DB << 'av_after'
|
279
|
-
end
|
280
|
-
def validate
|
281
|
-
DB << 'validate'
|
282
|
-
end
|
283
|
-
end
|
284
|
-
@c.new(:x => 2).valid?.must_equal true
|
285
|
-
DB.sqls.must_equal [ 'av_before', 'validate', 'av_after' ]
|
286
|
-
end
|
287
|
-
|
288
|
-
it "around_validation should handle cancel_action" do
|
289
|
-
@c.class_eval do
|
290
|
-
def around_validation
|
291
|
-
DB << 'av_before'
|
292
|
-
cancel_action
|
293
|
-
super
|
294
|
-
DB << 'av_after'
|
295
|
-
end
|
296
|
-
def validate
|
297
|
-
DB << 'validate'
|
298
|
-
end
|
299
|
-
end
|
300
|
-
@c.new(:x => 2).valid?.must_equal false
|
301
|
-
DB.sqls.must_equal [ 'av_before' ]
|
302
|
-
end
|
303
|
-
|
304
|
-
it "around_validation should be able to catch validation errors and modify them" do
|
305
|
-
@c.class_eval do
|
306
|
-
def validate
|
307
|
-
errors.add(:x, 'foo')
|
308
|
-
end
|
309
|
-
end
|
310
|
-
@c.new(:x => 2).valid?.must_equal false
|
311
|
-
@c.class_eval do
|
312
|
-
def around_validation
|
313
|
-
super
|
314
|
-
errors.clear
|
315
|
-
end
|
316
|
-
end
|
317
|
-
@c.new(:x => 2).valid?.must_equal true
|
318
|
-
end
|
319
|
-
|
320
|
-
it "around_create that doesn't call super should raise a HookFailed" do
|
321
|
-
@c.send(:define_method, :around_create){}
|
322
|
-
proc{@c.create(:x => 2)}.must_raise(Sequel::HookFailed)
|
323
|
-
end
|
324
|
-
|
325
|
-
it "around_update that doesn't call super should raise a HookFailed" do
|
326
|
-
@c.send(:define_method, :around_update){}
|
327
|
-
proc{@c.load(:x => 2).save}.must_raise(Sequel::HookFailed)
|
328
|
-
end
|
329
|
-
|
330
|
-
it "around_save that doesn't call super should raise a HookFailed" do
|
331
|
-
@c.send(:define_method, :around_save){}
|
332
|
-
proc{@c.create(:x => 2)}.must_raise(Sequel::HookFailed)
|
333
|
-
proc{@c.load(:x => 2).save}.must_raise(Sequel::HookFailed)
|
334
|
-
end
|
335
|
-
|
336
|
-
it "around_destroy that doesn't call super should raise a HookFailed" do
|
337
|
-
@c.send(:define_method, :around_destroy){}
|
338
|
-
proc{@c.load(:x => 2).destroy}.must_raise(Sequel::HookFailed)
|
339
|
-
end
|
340
|
-
|
341
|
-
it "around_validation that doesn't call super should raise a HookFailed" do
|
342
|
-
@c.send(:define_method, :around_validation){}
|
343
|
-
proc{@c.new.save}.must_raise(Sequel::HookFailed)
|
344
|
-
end
|
345
|
-
|
346
|
-
it "around_validation that doesn't call super should have valid? return false" do
|
347
|
-
@c.send(:define_method, :around_validation){}
|
348
|
-
@c.new.valid?.must_equal false
|
349
|
-
end
|
350
|
-
|
351
|
-
it "around_* that doesn't call super should return nil if raise_on_save_failure is false" do
|
352
|
-
@c.raise_on_save_failure = false
|
353
|
-
|
354
|
-
o = @c.load(:id => 1)
|
355
|
-
def o.around_save() end
|
356
|
-
o.save.must_be_nil
|
357
|
-
|
358
|
-
o = @c.load(:id => 1)
|
359
|
-
def o.around_update() end
|
360
|
-
o.save.must_be_nil
|
361
|
-
|
362
|
-
o = @c.new
|
363
|
-
def o.around_create() end
|
364
|
-
o.save.must_be_nil
|
365
|
-
|
366
|
-
o = @c.new
|
367
|
-
def o.around_validation() end
|
368
|
-
o.save.must_be_nil
|
369
|
-
end
|
370
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require_relative "spec_helper"
|
2
|
-
|
3
|
-
describe Sequel::Inflections do
|
4
|
-
before do
|
5
|
-
@plurals, @singulars, @uncountables = Sequel.inflections.plurals.dup, Sequel.inflections.singulars.dup, Sequel.inflections.uncountables.dup
|
6
|
-
end
|
7
|
-
after do
|
8
|
-
Sequel.inflections.plurals.replace(@plurals)
|
9
|
-
Sequel.inflections.singulars.replace(@singulars)
|
10
|
-
Sequel.inflections.uncountables.replace(@uncountables)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should be possible to clear the list of singulars, plurals, and uncountables" do
|
14
|
-
Sequel.inflections.clear(:plurals)
|
15
|
-
Sequel.inflections.plurals.must_equal []
|
16
|
-
Sequel.inflections.plural('blah', 'blahs')
|
17
|
-
Sequel.inflections.clear
|
18
|
-
Sequel.inflections.plurals.must_equal []
|
19
|
-
Sequel.inflections.singulars.must_equal []
|
20
|
-
Sequel.inflections.uncountables.must_equal []
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should be yielded and returned by Sequel.inflections" do
|
24
|
-
Sequel.inflections{|i| i.must_equal Sequel::Inflections}.must_equal Sequel::Inflections
|
25
|
-
end
|
26
|
-
end
|
data/spec/model/model_spec.rb
DELETED
@@ -1,956 +0,0 @@
|
|
1
|
-
require_relative "spec_helper"
|
2
|
-
|
3
|
-
describe "Sequel::Model()" do
|
4
|
-
before do
|
5
|
-
@db = Sequel::Model.db
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should return a model subclass with the given dataset if given a dataset" do
|
9
|
-
ds = @db[:blah]
|
10
|
-
c = Sequel::Model(ds)
|
11
|
-
c.superclass.must_equal Sequel::Model
|
12
|
-
c.dataset.row_proc.must_equal c
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should return a model subclass with a dataset with the default database and given table name if given a Symbol" do
|
16
|
-
c = Sequel::Model(:blah)
|
17
|
-
c.superclass.must_equal Sequel::Model
|
18
|
-
c.db.must_equal @db
|
19
|
-
c.table_name.must_equal :blah
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should return a model subclass with a dataset with the default database and given table name if given a LiteralString" do
|
23
|
-
c = Sequel::Model(Sequel.lit('blah'))
|
24
|
-
c.superclass.must_equal Sequel::Model
|
25
|
-
c.db.must_equal @db
|
26
|
-
c.table_name.must_equal Sequel.lit('blah')
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should return a model subclass with a dataset with the default database and given table name if given an SQL::Identifier" do
|
30
|
-
c = Sequel::Model(Sequel.identifier(:blah))
|
31
|
-
c.superclass.must_equal Sequel::Model
|
32
|
-
c.db.must_equal @db
|
33
|
-
c.table_name.must_equal Sequel.identifier(:blah)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should return a model subclass with a dataset with the default database and given table name if given an SQL::QualifiedIdentifier" do
|
37
|
-
c = Sequel::Model(Sequel.qualify(:boo, :blah))
|
38
|
-
c.superclass.must_equal Sequel::Model
|
39
|
-
c.db.must_equal @db
|
40
|
-
c.table_name.must_equal Sequel.qualify(:boo, :blah)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should return a model subclass with a dataset with the default database and given table name if given an SQL::AliasedExpression" do
|
44
|
-
c = Sequel::Model(Sequel.as(:blah, :boo))
|
45
|
-
c.superclass.must_equal Sequel::Model
|
46
|
-
c.db.must_equal @db
|
47
|
-
c.table_name.must_equal :boo
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should return a model subclass with the given dataset if given a dataset using an SQL::Identifier" do
|
51
|
-
ds = @db[Sequel.identifier(:blah)]
|
52
|
-
c = Sequel::Model(ds)
|
53
|
-
c.superclass.must_equal Sequel::Model
|
54
|
-
c.dataset.row_proc.must_equal c
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should be callable on Sequel::Model" do
|
58
|
-
ds = @db[:blah]
|
59
|
-
c = Sequel::Model::Model(ds)
|
60
|
-
c.superclass.must_equal Sequel::Model
|
61
|
-
c.dataset.row_proc.must_equal c
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should be callable on subclasses of Sequel::Model" do
|
65
|
-
ds = @db[:blah]
|
66
|
-
c = Class.new(Sequel::Model)
|
67
|
-
sc = c::Model(ds)
|
68
|
-
sc.superclass.must_equal c
|
69
|
-
sc.dataset.row_proc.must_equal sc
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should be callable on other modules if def_Model is used" do
|
73
|
-
m = Module.new
|
74
|
-
Sequel::Model.def_Model(m)
|
75
|
-
ds = @db[:blah]
|
76
|
-
c = m::Model(ds)
|
77
|
-
c.superclass.must_equal Sequel::Model
|
78
|
-
c.dataset.row_proc.must_equal c
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should be callable using model subclasses on other modules if def_Model is used" do
|
82
|
-
m = Module.new
|
83
|
-
c = Class.new(Sequel::Model)
|
84
|
-
c.def_Model(m)
|
85
|
-
ds = @db[:blah]
|
86
|
-
sc = m::Model(ds)
|
87
|
-
sc.superclass.must_equal c
|
88
|
-
sc.dataset.row_proc.must_equal sc
|
89
|
-
end
|
90
|
-
|
91
|
-
it "should return a model subclass associated to the given database if given a database" do
|
92
|
-
db = Sequel.mock
|
93
|
-
c = Sequel::Model(db)
|
94
|
-
c.superclass.must_equal Sequel::Model
|
95
|
-
c.db.must_equal db
|
96
|
-
proc{c.dataset}.must_raise(Sequel::Error)
|
97
|
-
class SmBlahTest < c
|
98
|
-
end
|
99
|
-
SmBlahTest.db.must_equal db
|
100
|
-
SmBlahTest.table_name.must_equal :sm_blah_tests
|
101
|
-
end
|
102
|
-
|
103
|
-
describe "reloading" do
|
104
|
-
before do
|
105
|
-
Sequel::Model.cache_anonymous_models = true
|
106
|
-
end
|
107
|
-
after do
|
108
|
-
Sequel::Model.cache_anonymous_models = false
|
109
|
-
Object.send(:remove_const, :Album) if defined?(::Album)
|
110
|
-
end
|
111
|
-
|
112
|
-
it "should work without raising an exception with a symbol" do
|
113
|
-
class ::Album < Sequel::Model(:table); end
|
114
|
-
class ::Album < Sequel::Model(:table); end
|
115
|
-
end
|
116
|
-
|
117
|
-
it "should work without raising an exception with an SQL::Identifier " do
|
118
|
-
class ::Album < Sequel::Model(Sequel.identifier(:table)); end
|
119
|
-
class ::Album < Sequel::Model(Sequel.identifier(:table)); end
|
120
|
-
end
|
121
|
-
|
122
|
-
it "should work without raising an exception with an SQL::QualifiedIdentifier " do
|
123
|
-
class ::Album < Sequel::Model(Sequel.qualify(:schema, :table)); end
|
124
|
-
class ::Album < Sequel::Model(Sequel.qualify(:schema, :table)); end
|
125
|
-
end
|
126
|
-
|
127
|
-
it "should work without raising an exception with an SQL::AliasedExpression" do
|
128
|
-
class ::Album < Sequel::Model(Sequel.as(:table, :alias)); end
|
129
|
-
class ::Album < Sequel::Model(Sequel.as(:table, :alias)); end
|
130
|
-
end
|
131
|
-
|
132
|
-
it "should work without raising an exception with an LiteralString" do
|
133
|
-
class ::Album < Sequel::Model(Sequel.lit('table')); end
|
134
|
-
class ::Album < Sequel::Model(Sequel.lit('table')); end
|
135
|
-
end
|
136
|
-
|
137
|
-
it "should work without raising an exception with a database" do
|
138
|
-
class ::Album < Sequel::Model(@db); end
|
139
|
-
class ::Album < Sequel::Model(@db); end
|
140
|
-
end
|
141
|
-
|
142
|
-
it "should work without raising an exception with a dataset" do
|
143
|
-
class ::Album < Sequel::Model(@db[:table]); end
|
144
|
-
class ::Album < Sequel::Model(@db[:table]); end
|
145
|
-
end
|
146
|
-
|
147
|
-
it "should work without raising an exception with a dataset with an SQL::Identifier" do
|
148
|
-
class ::Album < Sequel::Model(@db[Sequel.identifier(:table)]); end
|
149
|
-
class ::Album < Sequel::Model(@db[Sequel.identifier(:table)]); end
|
150
|
-
end
|
151
|
-
|
152
|
-
it "should raise an exception if anonymous model caching is disabled" do
|
153
|
-
Sequel::Model.cache_anonymous_models = false
|
154
|
-
proc do
|
155
|
-
class ::Album < Sequel::Model(@db[Sequel.identifier(:table)]); end
|
156
|
-
class ::Album < Sequel::Model(@db[Sequel.identifier(:table)]); end
|
157
|
-
end.must_raise TypeError
|
158
|
-
end
|
159
|
-
|
160
|
-
it "should use separate anonymous cache for subclasses" do
|
161
|
-
c = Class.new(Sequel::Model)
|
162
|
-
c.cache_anonymous_models.must_equal true
|
163
|
-
class ::Album < c::Model(:table); end
|
164
|
-
class ::Album < c::Model(:table); end
|
165
|
-
|
166
|
-
c1 = c::Model(:t1)
|
167
|
-
c1.must_equal c::Model(:t1)
|
168
|
-
c1.wont_equal Sequel::Model(:t1)
|
169
|
-
|
170
|
-
c.cache_anonymous_models = false
|
171
|
-
Sequel::Model.cache_anonymous_models.must_equal true
|
172
|
-
c1.wont_equal c::Model(:t1)
|
173
|
-
end
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
describe "Sequel::Model.freeze" do
|
178
|
-
it "should freeze the model class and not allow any changes" do
|
179
|
-
model = Class.new(Sequel::Model(:items))
|
180
|
-
model.freeze
|
181
|
-
|
182
|
-
model.frozen?.must_equal true
|
183
|
-
model.dataset.frozen?.must_equal true
|
184
|
-
model.db_schema.frozen?.must_equal true
|
185
|
-
model.db_schema[:id].frozen?.must_equal true
|
186
|
-
model.columns.frozen?.must_equal true
|
187
|
-
model.setter_methods.frozen?.must_equal true
|
188
|
-
model.send(:overridable_methods_module).frozen?.must_equal true
|
189
|
-
model.default_set_fields_options.frozen?.must_equal true
|
190
|
-
|
191
|
-
proc{model.dataset_module{}}.must_raise RuntimeError
|
192
|
-
end
|
193
|
-
|
194
|
-
it "should work if the model is already frozen" do
|
195
|
-
model = Class.new(Sequel::Model(:items))
|
196
|
-
model.freeze.freeze
|
197
|
-
end
|
198
|
-
|
199
|
-
it "should freeze a model class without a dataset without breaking" do
|
200
|
-
model = Class.new(Sequel::Model)
|
201
|
-
model.freeze
|
202
|
-
model.frozen?.must_equal true
|
203
|
-
proc{model.dataset}.must_raise Sequel::Error
|
204
|
-
model.db_schema.must_be_nil
|
205
|
-
model.columns.must_be_nil
|
206
|
-
model.setter_methods.must_equal []
|
207
|
-
model.send(:overridable_methods_module).frozen?.must_equal true
|
208
|
-
model.default_set_fields_options.frozen?.must_equal true
|
209
|
-
|
210
|
-
proc{model.dataset_module{}}.must_raise RuntimeError
|
211
|
-
end
|
212
|
-
|
213
|
-
it "should allow subclasses of frozen model classes to work correctly" do
|
214
|
-
model = Class.new(Sequel::Model(:items))
|
215
|
-
model.freeze
|
216
|
-
model = Class.new(model)
|
217
|
-
model.dataset = :items2
|
218
|
-
|
219
|
-
model.dataset_module{}
|
220
|
-
model.plugin Module.new
|
221
|
-
|
222
|
-
model.frozen?.must_equal false
|
223
|
-
model.db_schema.frozen?.must_equal false
|
224
|
-
model.db_schema[:id].frozen?.must_equal false
|
225
|
-
model.setter_methods.frozen?.must_equal false
|
226
|
-
model.dataset_module{}.frozen?.must_equal false
|
227
|
-
model.send(:overridable_methods_module).frozen?.must_equal false
|
228
|
-
model.default_set_fields_options.frozen?.must_equal false
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
describe Sequel::Model do
|
233
|
-
it "should have class method aliased as model" do
|
234
|
-
model_a = Class.new(Sequel::Model(:items))
|
235
|
-
model_a.new.model.must_be_same_as model_a
|
236
|
-
end
|
237
|
-
|
238
|
-
it "should be associated with a dataset" do
|
239
|
-
model_a = Class.new(Sequel::Model) { set_dataset DB[:as] }
|
240
|
-
|
241
|
-
model_a.dataset.must_be_kind_of(Sequel::Mock::Dataset)
|
242
|
-
model_a.dataset.opts[:from].must_equal [:as]
|
243
|
-
|
244
|
-
model_b = Class.new(Sequel::Model) { set_dataset DB[:bs] }
|
245
|
-
|
246
|
-
model_b.dataset.must_be_kind_of(Sequel::Mock::Dataset)
|
247
|
-
model_b.dataset.opts[:from].must_equal [:bs]
|
248
|
-
|
249
|
-
model_a.dataset.opts[:from].must_equal [:as]
|
250
|
-
end
|
251
|
-
end
|
252
|
-
|
253
|
-
describe Sequel::Model do
|
254
|
-
before do
|
255
|
-
@model = Class.new(Sequel::Model(:items))
|
256
|
-
DB.reset
|
257
|
-
end
|
258
|
-
|
259
|
-
it "should not allow dup/clone" do
|
260
|
-
proc{@model.dup}.must_raise NoMethodError
|
261
|
-
proc{@model.clone}.must_raise NoMethodError
|
262
|
-
end
|
263
|
-
|
264
|
-
it "has table_name return name of table" do
|
265
|
-
@model.table_name.must_equal :items
|
266
|
-
end
|
267
|
-
|
268
|
-
it "defaults to primary key of id" do
|
269
|
-
@model.primary_key.must_equal :id
|
270
|
-
end
|
271
|
-
|
272
|
-
it "allow primary key change" do
|
273
|
-
@model.set_primary_key :ssn
|
274
|
-
@model.primary_key.must_equal :ssn
|
275
|
-
end
|
276
|
-
|
277
|
-
it "allows dataset change" do
|
278
|
-
@model.set_dataset(DB[:foo])
|
279
|
-
@model.table_name.must_equal :foo
|
280
|
-
end
|
281
|
-
|
282
|
-
it "allows frozen dataset" do
|
283
|
-
@model.set_dataset(DB[:foo].freeze)
|
284
|
-
@model.table_name.must_equal :foo
|
285
|
-
@model.dataset.sql.must_equal 'SELECT * FROM foo'
|
286
|
-
end
|
287
|
-
|
288
|
-
|
289
|
-
it "table_name should respect table aliases" do
|
290
|
-
@model.set_dataset(Sequel[:foo].as(:x))
|
291
|
-
@model.table_name.must_equal :x
|
292
|
-
end
|
293
|
-
|
294
|
-
with_symbol_splitting "table_name should respect table alias symbols" do
|
295
|
-
@model.set_dataset(:foo___x)
|
296
|
-
@model.table_name.must_equal :x
|
297
|
-
end
|
298
|
-
|
299
|
-
it "set_dataset should raise an error unless given a Symbol or Dataset" do
|
300
|
-
proc{@model.set_dataset(Object.new)}.must_raise(Sequel::Error)
|
301
|
-
end
|
302
|
-
|
303
|
-
it "set_dataset should use a subquery for joined datasets" do
|
304
|
-
@model.set_dataset(DB.from(:foo, :bar))
|
305
|
-
@model.dataset.sql.must_equal 'SELECT * FROM (SELECT * FROM foo, bar) AS foo'
|
306
|
-
@model.set_dataset(DB[:foo].cross_join(:bar))
|
307
|
-
@model.dataset.sql.must_equal 'SELECT * FROM (SELECT * FROM foo CROSS JOIN bar) AS foo'
|
308
|
-
end
|
309
|
-
|
310
|
-
it "set_dataset should add the destroy method to the dataset that destroys each object" do
|
311
|
-
ds = DB[:foo]
|
312
|
-
ds.wont_respond_to(:destroy)
|
313
|
-
ds = @model.set_dataset(ds).dataset
|
314
|
-
ds.must_respond_to(:destroy)
|
315
|
-
DB.sqls
|
316
|
-
ds.with_fetch([{:id=>1}, {:id=>2}]).destroy.must_equal 2
|
317
|
-
DB.sqls.must_equal ["SELECT * FROM foo", "DELETE FROM foo WHERE id = 1", "DELETE FROM foo WHERE id = 2"]
|
318
|
-
end
|
319
|
-
|
320
|
-
it "set_dataset should add the destroy method that respects sharding with transactions" do
|
321
|
-
db = Sequel.mock(:servers=>{:s1=>{}})
|
322
|
-
ds = db[:foo].server(:s1)
|
323
|
-
@model.use_transactions = true
|
324
|
-
ds = @model.set_dataset(ds).dataset
|
325
|
-
db.sqls
|
326
|
-
ds.destroy.must_equal 0
|
327
|
-
db.sqls.must_equal ["BEGIN -- s1", "SELECT * FROM foo -- s1", "COMMIT -- s1"]
|
328
|
-
end
|
329
|
-
|
330
|
-
it "should raise an error on set_dataset if there is an error connecting to the database" do
|
331
|
-
def @model.columns() raise Sequel::DatabaseConnectionError end
|
332
|
-
proc{@model.set_dataset(Sequel::Database.new[:foo].join(:blah).from_self)}.must_raise Sequel::DatabaseConnectionError
|
333
|
-
end
|
334
|
-
|
335
|
-
it "should not raise an error if there is a problem getting the columns for a dataset" do
|
336
|
-
def @model.columns() raise Sequel::Error end
|
337
|
-
@model.set_dataset(DB[:foo].join(:blah).from_self)
|
338
|
-
end
|
339
|
-
|
340
|
-
it "doesn't raise an error on set_dataset if there is an error raised getting the schema" do
|
341
|
-
db = Sequel.mock
|
342
|
-
def db.schema(*) raise Sequel::Error; end
|
343
|
-
@model.set_dataset(db[:foo])
|
344
|
-
end
|
345
|
-
|
346
|
-
it "reload_db_schema? should be false by default" do
|
347
|
-
c = Class.new
|
348
|
-
c.extend Sequel::Model::ClassMethods
|
349
|
-
c.send(:reload_db_schema?).must_equal false
|
350
|
-
end
|
351
|
-
|
352
|
-
it "doesn't raise an error on inherited if there is an error setting the dataset" do
|
353
|
-
db = Sequel.mock
|
354
|
-
def db.schema(*) raise Sequel::Error; end
|
355
|
-
@model.dataset = db[:foo]
|
356
|
-
Class.new(@model)
|
357
|
-
end
|
358
|
-
|
359
|
-
it "uses a savepoint if inside a transaction when getting the columns" do
|
360
|
-
db = Sequel.mock
|
361
|
-
def db.supports_savepoints?; true end
|
362
|
-
Sequel::Model(db[:table])
|
363
|
-
db.sqls.must_equal ["SELECT * FROM table LIMIT 1"]
|
364
|
-
db.transaction{Sequel::Model(db[:table])}
|
365
|
-
db.sqls.must_equal ["BEGIN", "SAVEPOINT autopoint_1", "SELECT * FROM table LIMIT 1", "RELEASE SAVEPOINT autopoint_1", "COMMIT"]
|
366
|
-
end
|
367
|
-
|
368
|
-
it "should raise if bad inherited instance variable value is used" do
|
369
|
-
def @model.inherited_instance_variables() super.merge(:@a=>:foo) end
|
370
|
-
@model.instance_eval{@a=1}
|
371
|
-
proc{Class.new(@model)}.must_raise(Sequel::Error)
|
372
|
-
end
|
373
|
-
|
374
|
-
it "copy inherited instance variables into subclass if set" do
|
375
|
-
def @model.inherited_instance_variables() super.merge(:@a=>nil, :@b=>:dup, :@c=>:hash_dup, :@d=>proc{|v| v * 2}) end
|
376
|
-
@model.instance_eval{@a=1; @b=[2]; @c={3=>[4]}; @d=10}
|
377
|
-
m = Class.new(@model)
|
378
|
-
@model.instance_eval{@a=5; @b << 6; @c[3] << 7; @c[8] = [9]; @d=40}
|
379
|
-
m.instance_eval do
|
380
|
-
@a.must_equal 1
|
381
|
-
@b.must_equal [2]
|
382
|
-
@c.must_equal(3=>[4])
|
383
|
-
@d.must_equal 20
|
384
|
-
end
|
385
|
-
end
|
386
|
-
end
|
387
|
-
|
388
|
-
describe Sequel::Model do
|
389
|
-
before do
|
390
|
-
@model = Class.new(Sequel::Model)
|
391
|
-
DB.reset
|
392
|
-
end
|
393
|
-
|
394
|
-
it "allows set_dataset to accept a Symbol" do
|
395
|
-
@model.set_dataset(:foo)
|
396
|
-
@model.table_name.must_equal :foo
|
397
|
-
end
|
398
|
-
|
399
|
-
it "allows set_dataset to accept a LiteralString" do
|
400
|
-
@model.set_dataset(Sequel.lit('foo'))
|
401
|
-
@model.table_name.must_equal Sequel.lit('foo')
|
402
|
-
end
|
403
|
-
|
404
|
-
it "allows set_dataset to acceptan SQL::Identifier" do
|
405
|
-
@model.set_dataset(Sequel.identifier(:foo))
|
406
|
-
@model.table_name.must_equal Sequel.identifier(:foo)
|
407
|
-
end
|
408
|
-
|
409
|
-
it "allows set_dataset to acceptan SQL::QualifiedIdentifier" do
|
410
|
-
@model.set_dataset(Sequel.qualify(:bar, :foo))
|
411
|
-
@model.table_name.must_equal Sequel.qualify(:bar, :foo)
|
412
|
-
end
|
413
|
-
|
414
|
-
it "allows set_dataset to acceptan SQL::AliasedExpression" do
|
415
|
-
@model.set_dataset(Sequel.as(:foo, :bar))
|
416
|
-
@model.table_name.must_equal :bar
|
417
|
-
end
|
418
|
-
end
|
419
|
-
|
420
|
-
describe Sequel::Model, ".require_valid_table = true" do
|
421
|
-
before do
|
422
|
-
@db = Sequel.mock
|
423
|
-
@db.columns = proc do |sql|
|
424
|
-
raise Sequel::Error if sql =~ /foos/
|
425
|
-
[:id]
|
426
|
-
end
|
427
|
-
def @db.supports_schema_parsing?; true end
|
428
|
-
def @db.schema(t, *) t.first_source == :foos ? (raise Sequel::Error) : [[:id, {}]] end
|
429
|
-
Sequel::Model.db = @db
|
430
|
-
Sequel::Model.require_valid_table = true
|
431
|
-
end
|
432
|
-
after do
|
433
|
-
Sequel::Model.require_valid_table = false
|
434
|
-
Sequel::Model.db = DB
|
435
|
-
if Object.const_defined?(:Bar)
|
436
|
-
Object.send(:remove_const, :Bar)
|
437
|
-
end
|
438
|
-
if Object.const_defined?(:Foo)
|
439
|
-
Object.send(:remove_const, :Foo)
|
440
|
-
end
|
441
|
-
end
|
442
|
-
|
443
|
-
it "should raise an exception when creating a model with an invalid implicit table" do
|
444
|
-
proc{class ::Foo < Sequel::Model; end}.must_raise Sequel::Error
|
445
|
-
end
|
446
|
-
|
447
|
-
it "should not raise an exception when creating a model with a valid implicit table" do
|
448
|
-
class ::Bar < Sequel::Model; end
|
449
|
-
Bar.columns.must_equal [:id]
|
450
|
-
end
|
451
|
-
|
452
|
-
it "should raise an exception when creating a model with an invalid explicit table" do
|
453
|
-
proc{Sequel::Model(@db[:foos])}.must_raise Sequel::Error
|
454
|
-
end
|
455
|
-
|
456
|
-
it "should not raise an exception when creating a model with a valid explicit table" do
|
457
|
-
c = Sequel::Model(@db[:bars])
|
458
|
-
c.columns.must_equal [:id]
|
459
|
-
end
|
460
|
-
|
461
|
-
it "should raise an exception when calling set_dataset with an invalid table" do
|
462
|
-
c = Class.new(Sequel::Model)
|
463
|
-
proc{c.set_dataset @db[:foos]}.must_raise Sequel::Error
|
464
|
-
end
|
465
|
-
|
466
|
-
it "should not raise an exception when calling set_dataset with an valid table" do
|
467
|
-
c = Class.new(Sequel::Model)
|
468
|
-
c.set_dataset @db[:bars]
|
469
|
-
c.columns.must_equal [:id]
|
470
|
-
end
|
471
|
-
|
472
|
-
it "should assume nil value is the same as false" do
|
473
|
-
c = Class.new(Sequel::Model)
|
474
|
-
c.require_valid_table = nil
|
475
|
-
ds = @db.dataset.with_extend{def columns; raise Sequel::Error; end}
|
476
|
-
c.set_dataset(ds)
|
477
|
-
end
|
478
|
-
end
|
479
|
-
|
480
|
-
describe Sequel::Model, "constructors" do
|
481
|
-
before do
|
482
|
-
@m = Class.new(Sequel::Model)
|
483
|
-
@m.columns :a, :b
|
484
|
-
end
|
485
|
-
|
486
|
-
it "should accept a hash" do
|
487
|
-
m = @m.new(:a => 1, :b => 2)
|
488
|
-
m.values.must_equal(:a => 1, :b => 2)
|
489
|
-
m.must_be :new?
|
490
|
-
end
|
491
|
-
|
492
|
-
it "should accept a block and yield itself to the block" do
|
493
|
-
block_called = false
|
494
|
-
m = @m.new {|i| block_called = true; i.must_be_kind_of(@m); i.values[:a] = 1}
|
495
|
-
|
496
|
-
block_called.must_equal true
|
497
|
-
m.values[:a].must_equal 1
|
498
|
-
end
|
499
|
-
|
500
|
-
it "should have dataset row_proc create an existing object" do
|
501
|
-
@m.dataset = Sequel.mock.dataset
|
502
|
-
o = @m.dataset.row_proc.call(:a=>1)
|
503
|
-
o.must_be_kind_of(@m)
|
504
|
-
o.values.must_equal(:a=>1)
|
505
|
-
o.new?.must_equal false
|
506
|
-
end
|
507
|
-
|
508
|
-
it "should have .call create an existing object" do
|
509
|
-
o = @m.call(:a=>1)
|
510
|
-
o.must_be_kind_of(@m)
|
511
|
-
o.values.must_equal(:a=>1)
|
512
|
-
o.new?.must_equal false
|
513
|
-
end
|
514
|
-
|
515
|
-
it "should have .load create an existing object" do
|
516
|
-
o = @m.load(:a=>1)
|
517
|
-
o.must_be_kind_of(@m)
|
518
|
-
o.values.must_equal(:a=>1)
|
519
|
-
o.new?.must_equal false
|
520
|
-
end
|
521
|
-
end
|
522
|
-
|
523
|
-
describe Sequel::Model, "new" do
|
524
|
-
before do
|
525
|
-
@m = Class.new(Sequel::Model) do
|
526
|
-
set_dataset DB[:items]
|
527
|
-
columns :x, :id
|
528
|
-
end
|
529
|
-
end
|
530
|
-
|
531
|
-
it "should be marked as new?" do
|
532
|
-
o = @m.new
|
533
|
-
o.must_be :new?
|
534
|
-
end
|
535
|
-
|
536
|
-
it "should not be marked as new? once it is saved" do
|
537
|
-
o = @m.new(:x => 1)
|
538
|
-
o.must_be :new?
|
539
|
-
o.save
|
540
|
-
o.wont_be :new?
|
541
|
-
end
|
542
|
-
|
543
|
-
it "should use the last inserted id as primary key if not in values" do
|
544
|
-
@m.dataset = @m.dataset.with_fetch(:x => 1, :id => 1234).with_autoid(1234)
|
545
|
-
|
546
|
-
o = @m.new(:x => 1)
|
547
|
-
o.save
|
548
|
-
o.id.must_equal 1234
|
549
|
-
|
550
|
-
o = @m.load(:x => 1, :id => 333)
|
551
|
-
o.save
|
552
|
-
o.id.must_equal 333
|
553
|
-
end
|
554
|
-
end
|
555
|
-
|
556
|
-
describe Sequel::Model, ".find" do
|
557
|
-
before do
|
558
|
-
@c = Class.new(Sequel::Model(:items))
|
559
|
-
@c.dataset = @c.dataset.with_fetch(:name => 'sharon', :id => 1)
|
560
|
-
DB.reset
|
561
|
-
end
|
562
|
-
|
563
|
-
it "should return the first record matching the given filter" do
|
564
|
-
@c.find(:name => 'sharon').must_be_kind_of(@c)
|
565
|
-
DB.sqls.must_equal ["SELECT * FROM items WHERE (name = 'sharon') LIMIT 1"]
|
566
|
-
|
567
|
-
@c.find(Sequel.expr(:name).like('abc%')).must_be_kind_of(@c)
|
568
|
-
DB.sqls.must_equal ["SELECT * FROM items WHERE (name LIKE 'abc%' ESCAPE '\\') LIMIT 1"]
|
569
|
-
end
|
570
|
-
|
571
|
-
it "should accept filter blocks" do
|
572
|
-
@c.find{id > 1}.must_be_kind_of(@c)
|
573
|
-
DB.sqls.must_equal ["SELECT * FROM items WHERE (id > 1) LIMIT 1"]
|
574
|
-
|
575
|
-
@c.find{(x > 1) & (y < 2)}.must_be_kind_of(@c)
|
576
|
-
DB.sqls.must_equal ["SELECT * FROM items WHERE ((x > 1) AND (y < 2)) LIMIT 1"]
|
577
|
-
end
|
578
|
-
end
|
579
|
-
|
580
|
-
describe Sequel::Model, ".fetch" do
|
581
|
-
before do
|
582
|
-
DB.reset
|
583
|
-
@c = Class.new(Sequel::Model(:items))
|
584
|
-
end
|
585
|
-
|
586
|
-
it "should return instances of Model" do
|
587
|
-
@c.fetch("SELECT * FROM items").first.must_be_kind_of(@c)
|
588
|
-
end
|
589
|
-
|
590
|
-
it "should return true for .empty? and not raise an error on empty selection" do
|
591
|
-
@c.dataset = @c.dataset.with_extend do
|
592
|
-
def fetch_rows(sql)
|
593
|
-
yield({:count => 0})
|
594
|
-
end
|
595
|
-
end
|
596
|
-
@c.fetch("SELECT * FROM items WHERE FALSE").empty?
|
597
|
-
end
|
598
|
-
end
|
599
|
-
|
600
|
-
describe Sequel::Model, ".find_or_create" do
|
601
|
-
before do
|
602
|
-
@db = Sequel.mock
|
603
|
-
@c = Class.new(Sequel::Model(@db[:items])) do
|
604
|
-
set_primary_key :id
|
605
|
-
columns :x
|
606
|
-
end
|
607
|
-
@db.sqls
|
608
|
-
end
|
609
|
-
|
610
|
-
it "should find the record" do
|
611
|
-
@db.fetch = [{:x=>1, :id=>1}]
|
612
|
-
@db.autoid = 1
|
613
|
-
@c.find_or_create(:x => 1).must_equal @c.load(:x=>1, :id=>1)
|
614
|
-
@db.sqls.must_equal ["SELECT * FROM items WHERE (x = 1) LIMIT 1"]
|
615
|
-
end
|
616
|
-
|
617
|
-
it "should create the record if not found" do
|
618
|
-
@db.fetch = [[], {:x=>1, :id=>1}]
|
619
|
-
@db.autoid = 1
|
620
|
-
@c.find_or_create(:x => 1).must_equal @c.load(:x=>1, :id=>1)
|
621
|
-
@db.sqls.must_equal ["SELECT * FROM items WHERE (x = 1) LIMIT 1",
|
622
|
-
"INSERT INTO items (x) VALUES (1)",
|
623
|
-
"SELECT * FROM items WHERE id = 1"]
|
624
|
-
end
|
625
|
-
|
626
|
-
it "should pass the new record to be created to the block if no record is found" do
|
627
|
-
@db.fetch = [[], {:x=>1, :id=>1}]
|
628
|
-
@db.autoid = 1
|
629
|
-
@c.find_or_create(:x => 1){|x| x[:y] = 2}.must_equal @c.load(:x=>1, :id=>1)
|
630
|
-
@db.sqls.must_equal ["SELECT * FROM items WHERE (x = 1) LIMIT 1",
|
631
|
-
"INSERT INTO items (x, y) VALUES (1, 2)",
|
632
|
-
"SELECT * FROM items WHERE id = 1"]
|
633
|
-
end
|
634
|
-
end
|
635
|
-
|
636
|
-
describe Sequel::Model, ".all" do
|
637
|
-
it "should return all records in the dataset" do
|
638
|
-
c = Class.new(Sequel::Model(:items))
|
639
|
-
c.all.must_equal [c.load(:x=>1, :id=>1)]
|
640
|
-
end
|
641
|
-
end
|
642
|
-
|
643
|
-
describe Sequel::Model, "A model class without a primary key" do
|
644
|
-
before do
|
645
|
-
@c = Class.new(Sequel::Model(:items)) do
|
646
|
-
columns :x
|
647
|
-
no_primary_key
|
648
|
-
end
|
649
|
-
DB.reset
|
650
|
-
end
|
651
|
-
|
652
|
-
it "should be able to insert records without selecting them back" do
|
653
|
-
i = nil
|
654
|
-
i = @c.create(:x => 1)
|
655
|
-
i.class.wont_be_nil
|
656
|
-
i.values.to_hash.must_equal(:x => 1)
|
657
|
-
|
658
|
-
DB.sqls.must_equal ['INSERT INTO items (x) VALUES (1)']
|
659
|
-
end
|
660
|
-
|
661
|
-
it "should raise when deleting" do
|
662
|
-
proc{@c.load(:x=>1).delete}.must_raise Sequel::Error
|
663
|
-
end
|
664
|
-
|
665
|
-
it "should raise when updating" do
|
666
|
-
proc{@c.load(:x=>1).update(:x=>2)}.must_raise Sequel::Error
|
667
|
-
end
|
668
|
-
|
669
|
-
it "should insert a record when saving" do
|
670
|
-
o = @c.new(:x => 2)
|
671
|
-
o.must_be :new?
|
672
|
-
o.save
|
673
|
-
DB.sqls.must_equal ['INSERT INTO items (x) VALUES (2)']
|
674
|
-
end
|
675
|
-
end
|
676
|
-
|
677
|
-
describe Sequel::Model, "attribute accessors" do
|
678
|
-
before do
|
679
|
-
db = Sequel.mock
|
680
|
-
def db.supports_schema_parsing?() true end
|
681
|
-
def db.schema(*)
|
682
|
-
[[:x, {:type=>:integer}], [:z, {:type=>:integer}]]
|
683
|
-
end
|
684
|
-
@dataset = db[:items].columns(:x, :z)
|
685
|
-
@c = Class.new(Sequel::Model)
|
686
|
-
DB.reset
|
687
|
-
end
|
688
|
-
|
689
|
-
it "should be created on set_dataset" do
|
690
|
-
a = [:x, :z, :x= ,:z=]
|
691
|
-
(a - @c.instance_methods).must_equal a
|
692
|
-
@c.set_dataset(@dataset)
|
693
|
-
(a - @c.instance_methods).must_equal []
|
694
|
-
o = @c.new
|
695
|
-
(a - o.methods).must_equal []
|
696
|
-
|
697
|
-
o.x.must_be_nil
|
698
|
-
o.x = 34
|
699
|
-
o.x.must_equal 34
|
700
|
-
end
|
701
|
-
|
702
|
-
it "should be only accept one argument for the write accessor" do
|
703
|
-
@c.set_dataset(@dataset)
|
704
|
-
o = @c.new
|
705
|
-
|
706
|
-
o.x = 34
|
707
|
-
o.x.must_equal 34
|
708
|
-
proc{o.send(:x=)}.must_raise ArgumentError
|
709
|
-
proc{o.send(:x=, 3, 4)}.must_raise ArgumentError
|
710
|
-
end
|
711
|
-
|
712
|
-
it "should have a working typecasting setter even if the column is not selected" do
|
713
|
-
@c.set_dataset(@dataset.select(:z).columns(:z))
|
714
|
-
o = @c.new
|
715
|
-
|
716
|
-
o.x = '34'
|
717
|
-
o.x.must_equal 34
|
718
|
-
end
|
719
|
-
|
720
|
-
it "should typecast if the new value is the same as the existing but has a different class" do
|
721
|
-
@c.set_dataset(@dataset.select(:z).columns(:z))
|
722
|
-
o = @c.new
|
723
|
-
|
724
|
-
o.x = 34
|
725
|
-
o.x = 34.0
|
726
|
-
o.x.must_equal 34.0
|
727
|
-
o.x = 34
|
728
|
-
o.x.must_equal 34
|
729
|
-
end
|
730
|
-
end
|
731
|
-
|
732
|
-
describe Sequel::Model, ".[]" do
|
733
|
-
before do
|
734
|
-
@c = Class.new(Sequel::Model(:items))
|
735
|
-
@c.dataset = @c.dataset.with_fetch(:name => 'sharon', :id => 1)
|
736
|
-
DB.reset
|
737
|
-
end
|
738
|
-
|
739
|
-
it "should return the first record for the given pk" do
|
740
|
-
@c[1].must_equal @c.load(:name => 'sharon', :id => 1)
|
741
|
-
DB.sqls.must_equal ["SELECT * FROM items WHERE id = 1"]
|
742
|
-
@c[9999].must_equal @c.load(:name => 'sharon', :id => 1)
|
743
|
-
DB.sqls.must_equal ["SELECT * FROM items WHERE id = 9999"]
|
744
|
-
end
|
745
|
-
|
746
|
-
it "should have #[] return nil if no rows match" do
|
747
|
-
@c.dataset = @c.dataset.with_fetch([])
|
748
|
-
@c[1].must_be_nil
|
749
|
-
DB.sqls.must_equal ["SELECT * FROM items WHERE id = 1"]
|
750
|
-
end
|
751
|
-
|
752
|
-
it "should work correctly for custom primary key" do
|
753
|
-
@c.set_primary_key :name
|
754
|
-
@c['sharon'].must_equal @c.load(:name => 'sharon', :id => 1)
|
755
|
-
DB.sqls.must_equal ["SELECT * FROM items WHERE name = 'sharon'"]
|
756
|
-
end
|
757
|
-
|
758
|
-
it "should handle a dataset that uses a subquery" do
|
759
|
-
@c.dataset = @c.dataset.cross_join(:a).from_self(:alias=>:b)
|
760
|
-
@c[1].must_equal @c.load(:name => 'sharon', :id => 1)
|
761
|
-
DB.sqls.must_equal ["SELECT * FROM (SELECT * FROM items CROSS JOIN a) AS b WHERE (id = 1) LIMIT 1"]
|
762
|
-
end
|
763
|
-
|
764
|
-
it "should work correctly for composite primary key specified as array" do
|
765
|
-
@c.set_primary_key [:node_id, :kind]
|
766
|
-
@c[3921, 201].must_be_kind_of(@c)
|
767
|
-
DB.sqls.must_equal ['SELECT * FROM items WHERE ((node_id = 3921) AND (kind = 201)) LIMIT 1']
|
768
|
-
end
|
769
|
-
end
|
770
|
-
|
771
|
-
describe "Model#inspect" do
|
772
|
-
it "should include the class name and the values" do
|
773
|
-
Sequel::Model.load(:x => 333).inspect.must_equal '#<Sequel::Model @values={:x=>333}>'
|
774
|
-
end
|
775
|
-
end
|
776
|
-
|
777
|
-
describe "Model.db_schema" do
|
778
|
-
before do
|
779
|
-
@c = Class.new(Sequel::Model(:items)) do
|
780
|
-
def self.columns; orig_columns; end
|
781
|
-
end
|
782
|
-
@db = Sequel.mock
|
783
|
-
def @db.supports_schema_parsing?() true end
|
784
|
-
@dataset = @db[:items]
|
785
|
-
end
|
786
|
-
|
787
|
-
it "should not call database's schema if it isn't supported" do
|
788
|
-
def @db.supports_schema_parsing?() false end
|
789
|
-
def @db.schema(table, opts = {})
|
790
|
-
raise Sequel::Error
|
791
|
-
end
|
792
|
-
@dataset = @dataset.with_extend do
|
793
|
-
def columns
|
794
|
-
[:x, :y]
|
795
|
-
end
|
796
|
-
end
|
797
|
-
|
798
|
-
@c.dataset = @dataset
|
799
|
-
@c.db_schema.must_equal(:x=>{}, :y=>{})
|
800
|
-
@c.columns.must_equal [:x, :y]
|
801
|
-
|
802
|
-
@c.instance_eval{@db_schema = nil}
|
803
|
-
@c.db_schema.must_equal(:x=>{}, :y=>{})
|
804
|
-
@c.columns.must_equal [:x, :y]
|
805
|
-
end
|
806
|
-
|
807
|
-
it "should use the database's schema and set the columns and dataset columns" do
|
808
|
-
def @db.schema(table, opts = {})
|
809
|
-
[[:x, {:type=>:integer}], [:y, {:type=>:string}]]
|
810
|
-
end
|
811
|
-
@c.dataset = @dataset
|
812
|
-
@c.db_schema.must_equal(:x=>{:type=>:integer}, :y=>{:type=>:string})
|
813
|
-
@c.columns.must_equal [:x, :y]
|
814
|
-
@c.dataset.columns.must_equal [:x, :y]
|
815
|
-
end
|
816
|
-
|
817
|
-
it "should not restrict the schema for datasets with a :select option" do
|
818
|
-
def @c.columns; [:x, :z]; end
|
819
|
-
def @db.schema(table, opts = {})
|
820
|
-
[[:x, {:type=>:integer}], [:y, {:type=>:string}]]
|
821
|
-
end
|
822
|
-
@c.dataset = @dataset.select(:x, :y___z)
|
823
|
-
@c.db_schema.must_equal(:x=>{:type=>:integer}, :z=>{}, :y=>{:type=>:string})
|
824
|
-
end
|
825
|
-
|
826
|
-
it "should not raise error if setting dataset where getting schema and columns raises an error and require_valid_table is false" do
|
827
|
-
@c.require_valid_table = false
|
828
|
-
def @db.schema(table, opts={})
|
829
|
-
raise Sequel::Error
|
830
|
-
end
|
831
|
-
@c.dataset = @dataset.join(:x, :id).from_self.columns(:id, :x)
|
832
|
-
@c.db_schema.must_equal(:x=>{}, :id=>{})
|
833
|
-
end
|
834
|
-
|
835
|
-
it "should raise error if setting dataset where getting schema and columns raises an error and require_valid_table is true" do
|
836
|
-
@c.require_valid_table = true
|
837
|
-
def @db.schema(table, opts={})
|
838
|
-
raise Sequel::Error
|
839
|
-
end
|
840
|
-
@c.dataset = @dataset.join(:x, :id).from_self.columns(:id, :x)
|
841
|
-
@c.db_schema.must_equal(:x=>{}, :id=>{})
|
842
|
-
end
|
843
|
-
|
844
|
-
it "should use dataset columns if getting schema raises an error and require_valid_table is false" do
|
845
|
-
@c.require_valid_table = false
|
846
|
-
def @db.schema(table, opts={})
|
847
|
-
raise Sequel::Error
|
848
|
-
end
|
849
|
-
@c.dataset = @dataset.join(:x, :id).from_self.columns(:id, :x)
|
850
|
-
@c.db_schema.must_equal(:x=>{}, :id=>{})
|
851
|
-
end
|
852
|
-
|
853
|
-
it "should use dataset columns if getting schema raises an error and require_valid_table is true" do
|
854
|
-
@c.require_valid_table = true
|
855
|
-
def @db.schema(table, opts={})
|
856
|
-
raise Sequel::Error
|
857
|
-
end
|
858
|
-
@c.dataset = @dataset.join(:x, :id).from_self.columns(:id, :x)
|
859
|
-
@c.db_schema.must_equal(:x=>{}, :id=>{})
|
860
|
-
end
|
861
|
-
|
862
|
-
it "should automatically set a singular primary key based on the schema" do
|
863
|
-
ds = @dataset
|
864
|
-
d = ds.db
|
865
|
-
def d.schema(table, *opts) [[:x, {:primary_key=>true}]] end
|
866
|
-
@c.primary_key.must_equal :id
|
867
|
-
@c.dataset = ds
|
868
|
-
@c.db_schema.must_equal(:x=>{:primary_key=>true})
|
869
|
-
@c.primary_key.must_equal :x
|
870
|
-
end
|
871
|
-
|
872
|
-
it "should automatically set a singular primary key even if there are specific columns selected" do
|
873
|
-
ds = @dataset.select(:a, :b, :x)
|
874
|
-
d = ds.db
|
875
|
-
def d.schema(table, *opts) [[:a, {:primary_key=>false}], [:b, {:primary_key=>false}], [:x, {:primary_key=>true}]] end
|
876
|
-
@c.primary_key.must_equal :id
|
877
|
-
@c.dataset = ds
|
878
|
-
@c.db_schema.must_equal(:a=>{:primary_key=>false}, :b=>{:primary_key=>false}, :x=>{:primary_key=>true})
|
879
|
-
@c.primary_key.must_equal :x
|
880
|
-
end
|
881
|
-
|
882
|
-
it "should automatically set the composite primary key based on the schema" do
|
883
|
-
ds = @dataset
|
884
|
-
d = ds.db
|
885
|
-
def d.schema(table, *opts) [[:x, {:primary_key=>true}], [:y, {:primary_key=>true}]] end
|
886
|
-
@c.primary_key.must_equal :id
|
887
|
-
@c.dataset = ds
|
888
|
-
@c.db_schema.must_equal(:x=>{:primary_key=>true}, :y=>{:primary_key=>true})
|
889
|
-
@c.primary_key.must_equal [:x, :y]
|
890
|
-
end
|
891
|
-
|
892
|
-
it "should set an immutable composite primary key based on the schema" do
|
893
|
-
ds = @dataset
|
894
|
-
d = ds.db
|
895
|
-
def d.schema(table, *opts) [[:x, {:primary_key=>true}], [:y, {:primary_key=>true}]] end
|
896
|
-
@c.dataset = ds
|
897
|
-
@c.primary_key.must_equal [:x, :y]
|
898
|
-
proc{@c.primary_key.pop}.must_raise
|
899
|
-
end
|
900
|
-
|
901
|
-
it "should automatically set no primary key based on the schema" do
|
902
|
-
ds = @dataset
|
903
|
-
d = ds.db
|
904
|
-
def d.schema(table, *opts) [[:x, {:primary_key=>false}], [:y, {:primary_key=>false}]] end
|
905
|
-
@c.primary_key.must_equal :id
|
906
|
-
@c.dataset = ds
|
907
|
-
@c.db_schema.must_equal(:x=>{:primary_key=>false}, :y=>{:primary_key=>false})
|
908
|
-
@c.primary_key.must_be_nil
|
909
|
-
end
|
910
|
-
|
911
|
-
it "should automatically set primary key for dataset selecting table.*" do
|
912
|
-
ds = @dataset.select_all(:items)
|
913
|
-
d = ds.db
|
914
|
-
def d.schema(table, *opts) [[:x, {:primary_key=>true}]] end
|
915
|
-
@c.primary_key.must_equal :id
|
916
|
-
@c.dataset = ds
|
917
|
-
@c.db_schema.must_equal(:x=>{:primary_key=>true})
|
918
|
-
@c.primary_key.must_equal :x
|
919
|
-
end
|
920
|
-
|
921
|
-
it "should not modify the primary key unless all column schema hashes have a :primary_key entry" do
|
922
|
-
ds = @dataset
|
923
|
-
d = ds.db
|
924
|
-
def d.schema(table, *opts) [[:x, {:primary_key=>false}], [:y, {}]] end
|
925
|
-
@c.primary_key.must_equal :id
|
926
|
-
@c.dataset = ds
|
927
|
-
@c.db_schema.must_equal(:x=>{:primary_key=>false}, :y=>{})
|
928
|
-
@c.primary_key.must_equal :id
|
929
|
-
end
|
930
|
-
end
|
931
|
-
|
932
|
-
describe "Model#use_transactions" do
|
933
|
-
before do
|
934
|
-
@c = Class.new(Sequel::Model(:items))
|
935
|
-
end
|
936
|
-
|
937
|
-
it "should return class value by default" do
|
938
|
-
@c.use_transactions = true
|
939
|
-
@c.new.use_transactions.must_equal true
|
940
|
-
@c.use_transactions = false
|
941
|
-
@c.new.use_transactions.must_equal false
|
942
|
-
end
|
943
|
-
|
944
|
-
it "should return set value if manually set" do
|
945
|
-
instance = @c.new
|
946
|
-
instance.use_transactions = false
|
947
|
-
instance.use_transactions.must_equal false
|
948
|
-
@c.use_transactions = true
|
949
|
-
instance.use_transactions.must_equal false
|
950
|
-
|
951
|
-
instance.use_transactions = true
|
952
|
-
instance.use_transactions.must_equal true
|
953
|
-
@c.use_transactions = false
|
954
|
-
instance.use_transactions.must_equal true
|
955
|
-
end
|
956
|
-
end
|