sequel 4.22.0 → 4.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG +22 -0
- data/README.rdoc +6 -0
- data/Rakefile +59 -81
- data/doc/migration.rdoc +2 -0
- data/doc/release_notes/4.23.0.txt +65 -0
- data/doc/sharding.rdoc +16 -14
- data/doc/testing.rdoc +61 -77
- data/lib/sequel/adapters/jdbc.rb +1 -0
- data/lib/sequel/adapters/mock.rb +0 -1
- data/lib/sequel/adapters/postgres.rb +1 -0
- data/lib/sequel/adapters/postgresql.rb +1 -0
- data/lib/sequel/adapters/shared/postgres.rb +3 -3
- data/lib/sequel/connection_pool/sharded_threaded.rb +5 -0
- data/lib/sequel/connection_pool/threaded.rb +9 -1
- data/lib/sequel/database/connecting.rb +1 -1
- data/lib/sequel/database/transactions.rb +2 -1
- data/lib/sequel/dataset/prepared_statements.rb +1 -1
- data/lib/sequel/extensions/constraint_validations.rb +12 -12
- data/lib/sequel/extensions/date_arithmetic.rb +0 -4
- data/lib/sequel/extensions/pagination.rb +14 -2
- data/lib/sequel/extensions/pg_enum.rb +2 -2
- data/lib/sequel/extensions/pg_hstore.rb +1 -1
- data/lib/sequel/extensions/pg_json_ops.rb +2 -2
- data/lib/sequel/plugins/csv_serializer.rb +2 -0
- data/lib/sequel/plugins/delay_add_association.rb +50 -0
- data/lib/sequel/plugins/list.rb +2 -2
- data/lib/sequel/plugins/nested_attributes.rb +8 -28
- data/lib/sequel/plugins/update_refresh.rb +50 -0
- data/lib/sequel/plugins/validate_associated.rb +55 -0
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +29 -29
- data/spec/adapters/firebird_spec.rb +97 -103
- data/spec/adapters/informix_spec.rb +25 -25
- data/spec/adapters/mssql_spec.rb +156 -172
- data/spec/adapters/mysql_spec.rb +334 -359
- data/spec/adapters/oracle_spec.rb +67 -69
- data/spec/adapters/postgres_spec.rb +1298 -1249
- data/spec/adapters/spec_helper.rb +2 -35
- data/spec/adapters/sqlanywhere_spec.rb +39 -39
- data/spec/adapters/sqlite_spec.rb +203 -200
- data/spec/bin_spec.rb +57 -59
- data/spec/core/connection_pool_spec.rb +402 -401
- data/spec/core/database_spec.rb +953 -944
- data/spec/core/dataset_spec.rb +2178 -2168
- data/spec/core/deprecated_spec.rb +19 -19
- data/spec/core/expression_filters_spec.rb +415 -415
- data/spec/core/mock_adapter_spec.rb +212 -212
- data/spec/core/object_graph_spec.rb +73 -73
- data/spec/core/placeholder_literalizer_spec.rb +71 -71
- data/spec/core/schema_generator_spec.rb +44 -44
- data/spec/core/schema_spec.rb +470 -472
- data/spec/core/spec_helper.rb +5 -20
- data/spec/core/version_spec.rb +2 -2
- data/spec/core_extensions_spec.rb +320 -320
- data/spec/extensions/accessed_columns_spec.rb +12 -12
- data/spec/extensions/active_model_spec.rb +3 -3
- data/spec/extensions/after_initialize_spec.rb +2 -2
- data/spec/extensions/arbitrary_servers_spec.rb +23 -23
- data/spec/extensions/association_dependencies_spec.rb +34 -34
- data/spec/extensions/association_pks_spec.rb +98 -98
- data/spec/extensions/association_proxies_spec.rb +33 -33
- data/spec/extensions/auto_validations_spec.rb +46 -46
- data/spec/extensions/blacklist_security_spec.rb +19 -18
- data/spec/extensions/blank_spec.rb +36 -36
- data/spec/extensions/boolean_readers_spec.rb +36 -36
- data/spec/extensions/caching_spec.rb +82 -82
- data/spec/extensions/class_table_inheritance_spec.rb +72 -72
- data/spec/extensions/column_conflicts_spec.rb +19 -14
- data/spec/extensions/column_select_spec.rb +19 -19
- data/spec/extensions/columns_introspection_spec.rb +43 -43
- data/spec/extensions/composition_spec.rb +64 -64
- data/spec/extensions/connection_validator_spec.rb +92 -90
- data/spec/extensions/constraint_validations_plugin_spec.rb +92 -92
- data/spec/extensions/constraint_validations_spec.rb +80 -80
- data/spec/extensions/core_refinements_spec.rb +220 -220
- data/spec/extensions/csv_serializer_spec.rb +44 -44
- data/spec/extensions/current_datetime_timestamp_spec.rb +8 -8
- data/spec/extensions/dataset_associations_spec.rb +65 -65
- data/spec/extensions/dataset_source_alias_spec.rb +16 -16
- data/spec/extensions/date_arithmetic_spec.rb +51 -58
- data/spec/extensions/defaults_setter_spec.rb +19 -19
- data/spec/extensions/delay_add_association_spec.rb +52 -0
- data/spec/extensions/dirty_spec.rb +51 -51
- data/spec/extensions/eager_each_spec.rb +8 -8
- data/spec/extensions/empty_array_ignore_nulls_spec.rb +10 -10
- data/spec/extensions/error_splitter_spec.rb +2 -2
- data/spec/extensions/error_sql_spec.rb +4 -4
- data/spec/extensions/eval_inspect_spec.rb +3 -3
- data/spec/extensions/filter_having_spec.rb +8 -8
- data/spec/extensions/force_encoding_spec.rb +30 -30
- data/spec/extensions/from_block_spec.rb +7 -7
- data/spec/extensions/graph_each_spec.rb +19 -19
- data/spec/extensions/hash_aliases_spec.rb +5 -5
- data/spec/extensions/hook_class_methods_spec.rb +100 -100
- data/spec/extensions/inflector_spec.rb +54 -54
- data/spec/extensions/input_transformer_spec.rb +10 -10
- data/spec/extensions/insert_returning_select_spec.rb +8 -8
- data/spec/extensions/instance_filters_spec.rb +26 -26
- data/spec/extensions/instance_hooks_spec.rb +85 -85
- data/spec/extensions/json_serializer_spec.rb +68 -68
- data/spec/extensions/lazy_attributes_spec.rb +49 -49
- data/spec/extensions/list_spec.rb +77 -75
- data/spec/extensions/looser_typecasting_spec.rb +16 -16
- data/spec/extensions/many_through_many_spec.rb +627 -627
- data/spec/extensions/meta_def_spec.rb +7 -7
- data/spec/extensions/migration_spec.rb +217 -217
- data/spec/extensions/modification_detection_spec.rb +20 -20
- data/spec/extensions/mssql_optimistic_locking_spec.rb +21 -21
- data/spec/extensions/named_timezones_spec.rb +18 -18
- data/spec/extensions/nested_attributes_spec.rb +107 -107
- data/spec/extensions/null_dataset_spec.rb +24 -24
- data/spec/extensions/optimistic_locking_spec.rb +21 -21
- data/spec/extensions/pagination_spec.rb +52 -52
- data/spec/extensions/pg_array_associations_spec.rb +273 -273
- data/spec/extensions/pg_array_ops_spec.rb +52 -52
- data/spec/extensions/pg_array_spec.rb +152 -152
- data/spec/extensions/pg_enum_spec.rb +13 -13
- data/spec/extensions/pg_hstore_ops_spec.rb +63 -63
- data/spec/extensions/pg_hstore_spec.rb +84 -84
- data/spec/extensions/pg_inet_spec.rb +15 -15
- data/spec/extensions/pg_interval_spec.rb +29 -29
- data/spec/extensions/pg_json_ops_spec.rb +86 -84
- data/spec/extensions/pg_json_spec.rb +104 -104
- data/spec/extensions/pg_loose_count_spec.rb +6 -6
- data/spec/extensions/pg_range_ops_spec.rb +24 -24
- data/spec/extensions/pg_range_spec.rb +143 -143
- data/spec/extensions/pg_row_ops_spec.rb +14 -14
- data/spec/extensions/pg_row_plugin_spec.rb +12 -12
- data/spec/extensions/pg_row_spec.rb +118 -118
- data/spec/extensions/pg_static_cache_updater_spec.rb +28 -28
- data/spec/extensions/pg_typecast_on_load_spec.rb +21 -21
- data/spec/extensions/prepared_statements_associations_spec.rb +42 -42
- data/spec/extensions/prepared_statements_safe_spec.rb +18 -18
- data/spec/extensions/prepared_statements_spec.rb +28 -28
- data/spec/extensions/prepared_statements_with_pk_spec.rb +11 -11
- data/spec/extensions/pretty_table_spec.rb +16 -16
- data/spec/extensions/query_literals_spec.rb +37 -37
- data/spec/extensions/query_spec.rb +32 -32
- data/spec/extensions/rcte_tree_spec.rb +141 -141
- data/spec/extensions/round_timestamps_spec.rb +21 -21
- data/spec/extensions/schema_caching_spec.rb +8 -8
- data/spec/extensions/schema_dumper_spec.rb +78 -78
- data/spec/extensions/schema_spec.rb +31 -27
- data/spec/extensions/scissors_spec.rb +3 -3
- data/spec/extensions/select_remove_spec.rb +14 -14
- data/spec/extensions/sequel_3_dataset_methods_spec.rb +28 -28
- data/spec/extensions/serialization_modification_detection_spec.rb +33 -33
- data/spec/extensions/serialization_spec.rb +79 -78
- data/spec/extensions/server_block_spec.rb +17 -17
- data/spec/extensions/set_overrides_spec.rb +30 -30
- data/spec/extensions/sharding_spec.rb +65 -65
- data/spec/extensions/shared_caching_spec.rb +29 -29
- data/spec/extensions/single_table_inheritance_spec.rb +79 -79
- data/spec/extensions/skip_create_refresh_spec.rb +3 -3
- data/spec/extensions/spec_helper.rb +4 -29
- data/spec/extensions/split_array_nil_spec.rb +9 -9
- data/spec/extensions/split_values_spec.rb +7 -7
- data/spec/extensions/sql_expr_spec.rb +32 -32
- data/spec/extensions/static_cache_spec.rb +123 -123
- data/spec/extensions/string_date_time_spec.rb +34 -34
- data/spec/extensions/string_stripper_spec.rb +15 -15
- data/spec/extensions/subclasses_spec.rb +31 -31
- data/spec/extensions/table_select_spec.rb +15 -15
- data/spec/extensions/tactical_eager_loading_spec.rb +23 -23
- data/spec/extensions/thread_local_timezones_spec.rb +13 -13
- data/spec/extensions/timestamps_spec.rb +40 -40
- data/spec/extensions/to_dot_spec.rb +34 -34
- data/spec/extensions/touch_spec.rb +52 -52
- data/spec/extensions/tree_spec.rb +72 -72
- data/spec/extensions/typecast_on_load_spec.rb +25 -25
- data/spec/extensions/unlimited_update_spec.rb +2 -2
- data/spec/extensions/update_or_create_spec.rb +36 -36
- data/spec/extensions/update_primary_key_spec.rb +35 -35
- data/spec/extensions/update_refresh_spec.rb +41 -0
- data/spec/extensions/validate_associated_spec.rb +52 -0
- data/spec/extensions/validation_class_methods_spec.rb +314 -317
- data/spec/extensions/validation_helpers_spec.rb +195 -195
- data/spec/extensions/xml_serializer_spec.rb +48 -48
- data/spec/guards_helper.rb +55 -0
- data/spec/integration/associations_test.rb +1089 -1088
- data/spec/integration/database_test.rb +29 -29
- data/spec/integration/dataset_test.rb +661 -661
- data/spec/integration/eager_loader_test.rb +147 -147
- data/spec/integration/migrator_test.rb +122 -122
- data/spec/integration/model_test.rb +70 -70
- data/spec/integration/plugin_test.rb +682 -640
- data/spec/integration/prepared_statement_test.rb +172 -172
- data/spec/integration/schema_test.rb +245 -245
- data/spec/integration/spec_helper.rb +1 -64
- data/spec/integration/timezone_test.rb +17 -17
- data/spec/integration/transaction_test.rb +87 -87
- data/spec/integration/type_test.rb +33 -33
- data/spec/model/association_reflection_spec.rb +130 -121
- data/spec/model/associations_spec.rb +1112 -1113
- data/spec/model/base_spec.rb +197 -196
- data/spec/model/class_dataset_methods_spec.rb +118 -118
- data/spec/model/dataset_methods_spec.rb +49 -49
- data/spec/model/eager_loading_spec.rb +705 -702
- data/spec/model/hooks_spec.rb +169 -168
- data/spec/model/inflector_spec.rb +5 -5
- data/spec/model/model_spec.rb +287 -297
- data/spec/model/plugins_spec.rb +47 -47
- data/spec/model/record_spec.rb +534 -535
- data/spec/model/spec_helper.rb +3 -21
- data/spec/model/validations_spec.rb +72 -70
- data/spec/spec_config.rb +8 -0
- metadata +41 -9
- data/lib/sequel/adapters/fdbsql.rb +0 -286
- data/lib/sequel/adapters/jdbc/fdbsql.rb +0 -66
- data/lib/sequel/adapters/openbase.rb +0 -54
- data/lib/sequel/adapters/shared/fdbsql.rb +0 -550
- data/spec/adapters/fdbsql_spec.rb +0 -429
- data/spec/rspec_helper.rb +0 -22
|
@@ -10,42 +10,42 @@ describe "accessed_columns plugin" do
|
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
it "should record columns accessed" do
|
|
13
|
-
@o.accessed_columns.
|
|
13
|
+
@o.accessed_columns.must_equal []
|
|
14
14
|
@o.name
|
|
15
|
-
@o.accessed_columns.
|
|
15
|
+
@o.accessed_columns.must_equal [:name]
|
|
16
16
|
@o.name
|
|
17
|
-
@o.accessed_columns.
|
|
17
|
+
@o.accessed_columns.must_equal [:name]
|
|
18
18
|
@o.b
|
|
19
|
-
@o.accessed_columns.sort_by{|s| s.to_s}.
|
|
19
|
+
@o.accessed_columns.sort_by{|s| s.to_s}.must_equal [:b, :name]
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it "should clear accessed columns when refreshing" do
|
|
23
23
|
@o.name
|
|
24
24
|
@o.refresh
|
|
25
|
-
@o.accessed_columns.
|
|
25
|
+
@o.accessed_columns.must_equal []
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
it "should clear accessed columns when saving" do
|
|
29
29
|
@o.name
|
|
30
30
|
@o.save
|
|
31
|
-
@o.accessed_columns.
|
|
31
|
+
@o.accessed_columns.must_equal []
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
it "should work when duping and cloning instances" do
|
|
35
35
|
@o.name
|
|
36
36
|
o = @o.dup
|
|
37
|
-
@o.accessed_columns.
|
|
37
|
+
@o.accessed_columns.must_equal [:name]
|
|
38
38
|
@o.b
|
|
39
|
-
@o.accessed_columns.sort_by{|s| s.to_s}.
|
|
40
|
-
o.accessed_columns.
|
|
39
|
+
@o.accessed_columns.sort_by{|s| s.to_s}.must_equal [:b, :name]
|
|
40
|
+
o.accessed_columns.must_equal [:name]
|
|
41
41
|
o2 = o.clone
|
|
42
42
|
o2.refresh
|
|
43
|
-
o.accessed_columns.
|
|
44
|
-
o2.accessed_columns.
|
|
43
|
+
o.accessed_columns.must_equal [:name]
|
|
44
|
+
o2.accessed_columns.must_equal []
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
it "should not raise exceptions when object is frozen" do
|
|
48
48
|
@o.freeze
|
|
49
|
-
|
|
49
|
+
@o.name
|
|
50
50
|
end
|
|
51
51
|
end
|
|
@@ -24,7 +24,7 @@ rescue LoadError => e
|
|
|
24
24
|
skip_warn "active_model plugin: can't load active_model (#{e.class}: #{e})"
|
|
25
25
|
else
|
|
26
26
|
describe "ActiveModel plugin" do
|
|
27
|
-
|
|
27
|
+
it "should be compliant to the ActiveModel spec" do
|
|
28
28
|
tc = Class.new(test_class)
|
|
29
29
|
tc.class_eval do
|
|
30
30
|
define_method(:setup) do
|
|
@@ -53,7 +53,7 @@ describe "ActiveModel plugin" do
|
|
|
53
53
|
|
|
54
54
|
# Should return self, not a proxy object
|
|
55
55
|
def test__to_model
|
|
56
|
-
assert_equal @m.to_model.object_id
|
|
56
|
+
assert_equal @m.to_model.object_id, @m.object_id
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def test__to_key
|
|
@@ -116,7 +116,7 @@ describe "ActiveModel plugin" do
|
|
|
116
116
|
if res.failure_count > 0
|
|
117
117
|
puts res.instance_variable_get(:@failures)
|
|
118
118
|
end
|
|
119
|
-
res.failure_count.
|
|
119
|
+
res.failure_count.must_equal 0
|
|
120
120
|
end
|
|
121
121
|
end
|
|
122
122
|
end
|
|
@@ -15,10 +15,10 @@ describe "Sequel::Plugins::AfterInitialize" do
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it "should have after_initialize hook be called for new objects" do
|
|
18
|
-
@c.new(:name=>'foo').values.
|
|
18
|
+
@c.new(:name=>'foo').values.must_equal(:name=>'foofoo')
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
it "should have after_initialize hook be called for objects loaded from the database" do
|
|
22
|
-
@c.call(:id=>1, :name=>'foo').values.
|
|
22
|
+
@c.call(:id=>1, :name=>'foo').values.must_equal(:id=>3, :name=>'foofoo')
|
|
23
23
|
end
|
|
24
24
|
end
|
|
@@ -6,78 +6,78 @@ describe "arbtirary servers" do
|
|
|
6
6
|
@db.extension :arbitrary_servers
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
it "should allow arbitrary server options using a hash" do
|
|
10
10
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c|
|
|
11
|
-
c.opts[:host].
|
|
12
|
-
c.opts[:database].
|
|
11
|
+
c.opts[:host].must_equal 'host1'
|
|
12
|
+
c.opts[:database].must_equal 'db1'
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
it "should not cache connections to arbitrary servers" do
|
|
17
17
|
x = nil
|
|
18
18
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c|
|
|
19
19
|
x = c
|
|
20
20
|
end
|
|
21
21
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c2|
|
|
22
|
-
c2.
|
|
22
|
+
c2.wont_be_same_as(x)
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
it "should yield same connection correctly when nesting" do
|
|
27
27
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c|
|
|
28
28
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c2|
|
|
29
|
-
c2.
|
|
29
|
+
c2.must_be_same_as(c)
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
it "should disconnect when connection is finished" do
|
|
35
35
|
x, x1 = nil, nil
|
|
36
36
|
@db.meta_def(:disconnect_connection){|c| x = c}
|
|
37
37
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c|
|
|
38
38
|
x1 = c
|
|
39
39
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c2|
|
|
40
|
-
c2.
|
|
40
|
+
c2.must_be_same_as(c)
|
|
41
41
|
end
|
|
42
|
-
x.
|
|
42
|
+
x.must_be_same_as(nil)
|
|
43
43
|
end
|
|
44
|
-
x.
|
|
44
|
+
x.must_be_same_as(x1)
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
it "should yield different connection correctly when nesting" do
|
|
48
48
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c|
|
|
49
|
-
c.opts[:host].
|
|
49
|
+
c.opts[:host].must_equal 'host1'
|
|
50
50
|
@db.synchronize(:host=>'host2', :database=>'db1') do |c2|
|
|
51
|
-
c2.opts[:host].
|
|
52
|
-
c2.
|
|
51
|
+
c2.opts[:host].must_equal 'host2'
|
|
52
|
+
c2.wont_be_same_as(c)
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
it "should respect multithreaded access" do
|
|
58
58
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c|
|
|
59
59
|
Thread.new do
|
|
60
60
|
@db.synchronize(:host=>'host1', :database=>'db1') do |c2|
|
|
61
|
-
c2.
|
|
61
|
+
_(c2).wont_be_same_as(c)
|
|
62
62
|
end
|
|
63
63
|
end.join
|
|
64
64
|
end
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
it "should work correctly with server_block plugin" do
|
|
68
68
|
@db.extension :server_block
|
|
69
69
|
@db.with_server(:host=>'host1', :database=>'db1') do
|
|
70
70
|
@db.synchronize do |c|
|
|
71
|
-
c.opts[:host].
|
|
72
|
-
c.opts[:database].
|
|
71
|
+
c.opts[:host].must_equal 'host1'
|
|
72
|
+
c.opts[:database].must_equal 'db1'
|
|
73
73
|
@db.synchronize do |c2|
|
|
74
|
-
c2.
|
|
74
|
+
c2.must_be_same_as(c)
|
|
75
75
|
end
|
|
76
76
|
end
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
it "should respect multithreaded access with server block plugin" do
|
|
81
81
|
@db.extension :server_block
|
|
82
82
|
q, q1 = Queue.new, Queue.new
|
|
83
83
|
|
|
@@ -103,7 +103,7 @@ describe "arbtirary servers" do
|
|
|
103
103
|
@db[:t].all
|
|
104
104
|
q1.push nil
|
|
105
105
|
t.join
|
|
106
|
-
@db.sqls.
|
|
106
|
+
@db.sqls.must_equal ['SELECT * FROM t', 'SELECT * FROM t -- {:host=>"a"}', 'SELECT * FROM t', 'SELECT * FROM t -- {:host=>"c"}', 'SELECT * FROM t -- {:host=>"d"}',
|
|
107
107
|
'SELECT * FROM t -- {:host=>"b"}', 'SELECT * FROM t -- {:host=>"a"}', 'SELECT * FROM t', 'SELECT * FROM t -- {:host=>"c"}', 'SELECT * FROM t']
|
|
108
108
|
end
|
|
109
109
|
end
|
|
@@ -18,100 +18,100 @@ describe "AssociationDependencies plugin" do
|
|
|
18
18
|
DB.reset
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
it "should allow destroying associated many_to_one associated object" do
|
|
22
22
|
@Album.add_association_dependencies :artist=>:destroy
|
|
23
23
|
@Album.load(:id=>1, :name=>'Al', :artist_id=>2).destroy
|
|
24
|
-
DB.sqls.
|
|
24
|
+
DB.sqls.must_equal ['DELETE FROM albums WHERE id = 1', 'SELECT * FROM artists WHERE (artists.id = 2) LIMIT 1', 'DELETE FROM artists WHERE id = 2']
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
it "should allow deleting associated many_to_one associated object" do
|
|
28
28
|
@Album.add_association_dependencies :artist=>:delete
|
|
29
29
|
@Album.load(:id=>1, :name=>'Al', :artist_id=>2).destroy
|
|
30
|
-
DB.sqls.
|
|
30
|
+
DB.sqls.must_equal ['DELETE FROM albums WHERE id = 1', 'DELETE FROM artists WHERE (artists.id = 2)']
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
it "should allow destroying associated one_to_one associated object" do
|
|
34
34
|
@Artist.add_association_dependencies :first_album=>:destroy
|
|
35
35
|
@Artist.load(:id=>2, :name=>'Ar').destroy
|
|
36
|
-
DB.sqls.
|
|
36
|
+
DB.sqls.must_equal ['SELECT * FROM albums WHERE ((position = 1) AND (albums.artist_id = 2)) LIMIT 1', 'DELETE FROM albums WHERE id = 1', 'DELETE FROM artists WHERE id = 2']
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
it "should allow deleting associated one_to_one associated object" do
|
|
40
40
|
@Artist.add_association_dependencies :first_album=>:delete
|
|
41
41
|
@Artist.load(:id=>2, :name=>'Ar').destroy
|
|
42
|
-
DB.sqls.
|
|
42
|
+
DB.sqls.must_equal ['DELETE FROM albums WHERE ((position = 1) AND (albums.artist_id = 2))', 'DELETE FROM artists WHERE id = 2']
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
it "should allow destroying associated one_to_many objects" do
|
|
46
46
|
@Artist.add_association_dependencies :albums=>:destroy
|
|
47
47
|
@Artist.load(:id=>2, :name=>'Ar').destroy
|
|
48
|
-
DB.sqls.
|
|
48
|
+
DB.sqls.must_equal ['SELECT * FROM albums WHERE (albums.artist_id = 2)', 'DELETE FROM albums WHERE id = 1', 'DELETE FROM artists WHERE id = 2']
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
it "should allow deleting associated one_to_many objects" do
|
|
52
52
|
@Artist.add_association_dependencies :albums=>:delete
|
|
53
53
|
@Artist.load(:id=>2, :name=>'Ar').destroy
|
|
54
|
-
DB.sqls.
|
|
54
|
+
DB.sqls.must_equal ['DELETE FROM albums WHERE (albums.artist_id = 2)', 'DELETE FROM artists WHERE id = 2']
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
it "should allow nullifying associated one_to_one objects" do
|
|
58
58
|
@Artist.add_association_dependencies :first_album=>:nullify
|
|
59
59
|
@Artist.load(:id=>2, :name=>'Ar').destroy
|
|
60
|
-
DB.sqls.
|
|
60
|
+
DB.sqls.must_equal ['UPDATE albums SET artist_id = NULL WHERE ((position = 1) AND (artist_id = 2))', 'DELETE FROM artists WHERE id = 2']
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
it "should allow nullifying associated one_to_many objects" do
|
|
64
64
|
@Artist.add_association_dependencies :albums=>:nullify
|
|
65
65
|
@Artist.load(:id=>2, :name=>'Ar').destroy
|
|
66
|
-
DB.sqls.
|
|
66
|
+
DB.sqls.must_equal ['UPDATE albums SET artist_id = NULL WHERE (artist_id = 2)', 'DELETE FROM artists WHERE id = 2']
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
it "should allow nullifying associated many_to_many associations" do
|
|
70
70
|
@Artist.add_association_dependencies :other_artists=>:nullify
|
|
71
71
|
@Artist.load(:id=>2, :name=>'Ar').destroy
|
|
72
|
-
DB.sqls.
|
|
72
|
+
DB.sqls.must_equal ['DELETE FROM aoa WHERE (l = 2)', 'DELETE FROM artists WHERE id = 2']
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
proc{@Album.add_association_dependencies :artist=>:nullify}.
|
|
75
|
+
it "should raise an error if attempting to nullify a many_to_one association" do
|
|
76
|
+
proc{@Album.add_association_dependencies :artist=>:nullify}.must_raise(Sequel::Error)
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
proc{@Album.add_association_dependencies :artist=>:blah}.
|
|
79
|
+
it "should raise an error if using an unrecognized dependence action" do
|
|
80
|
+
proc{@Album.add_association_dependencies :artist=>:blah}.must_raise(Sequel::Error)
|
|
81
81
|
end
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
proc{@Album.add_association_dependencies :blah=>:delete}.
|
|
83
|
+
it "should raise an error if a nonexistent association is used" do
|
|
84
|
+
proc{@Album.add_association_dependencies :blah=>:delete}.must_raise(Sequel::Error)
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
it "should raise an error if a invalid association type is used" do
|
|
88
88
|
@Artist.plugin :many_through_many
|
|
89
89
|
@Artist.many_through_many :other_albums, [[:id, :id, :id]]
|
|
90
|
-
proc{@Artist.add_association_dependencies :other_albums=>:nullify}.
|
|
90
|
+
proc{@Artist.add_association_dependencies :other_albums=>:nullify}.must_raise(Sequel::Error)
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
proc{@Artist.add_association_dependencies :other_artists=>:delete}.
|
|
93
|
+
it "should raise an error if using a many_to_many association type without nullify" do
|
|
94
|
+
proc{@Artist.add_association_dependencies :other_artists=>:delete}.must_raise(Sequel::Error)
|
|
95
95
|
end
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
it "should allow specifying association dependencies in the plugin call" do
|
|
98
98
|
@Album.plugin :association_dependencies, :artist=>:destroy
|
|
99
99
|
@Album.load(:id=>1, :name=>'Al', :artist_id=>2).destroy
|
|
100
|
-
DB.sqls.
|
|
100
|
+
DB.sqls.must_equal ['DELETE FROM albums WHERE id = 1', 'SELECT * FROM artists WHERE (artists.id = 2) LIMIT 1', 'DELETE FROM artists WHERE id = 2']
|
|
101
101
|
end
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
it "should work with subclasses" do
|
|
104
104
|
c = Class.new(@Album)
|
|
105
105
|
c.add_association_dependencies :artist=>:destroy
|
|
106
106
|
c.load(:id=>1, :name=>'Al', :artist_id=>2).destroy
|
|
107
|
-
DB.sqls.
|
|
107
|
+
DB.sqls.must_equal ['DELETE FROM albums WHERE id = 1', 'SELECT * FROM artists WHERE (artists.id = 2) LIMIT 1', 'DELETE FROM artists WHERE id = 2']
|
|
108
108
|
|
|
109
109
|
@Album.load(:id=>1, :name=>'Al', :artist_id=>2).destroy
|
|
110
|
-
DB.sqls.
|
|
110
|
+
DB.sqls.must_equal ['DELETE FROM albums WHERE id = 1']
|
|
111
111
|
|
|
112
112
|
@Album.add_association_dependencies :artist=>:destroy
|
|
113
113
|
c2 = Class.new(@Album)
|
|
114
114
|
c2.load(:id=>1, :name=>'Al', :artist_id=>2).destroy
|
|
115
|
-
DB.sqls.
|
|
115
|
+
DB.sqls.must_equal ['DELETE FROM albums WHERE id = 1', 'SELECT * FROM artists WHERE (artists.id = 2) LIMIT 1', 'DELETE FROM artists WHERE id = 2']
|
|
116
116
|
end
|
|
117
117
|
end
|
|
@@ -60,143 +60,143 @@ describe "Sequel::Plugins::AssociationPks" do
|
|
|
60
60
|
@db.sqls
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
@Artist.load(:id=>1).album_pks.
|
|
65
|
-
@Artist.load(:id=>2).album_pks.
|
|
63
|
+
it "should return correct associated pks for one_to_many associations" do
|
|
64
|
+
@Artist.load(:id=>1).album_pks.must_equal [1,2,3]
|
|
65
|
+
@Artist.load(:id=>2).album_pks.must_equal []
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
@Album.load(:id=>1).tag_pks.
|
|
70
|
-
@Album.load(:id=>2).tag_pks.
|
|
71
|
-
@Album.load(:id=>3).tag_pks.
|
|
68
|
+
it "should return correct associated pks for many_to_many associations" do
|
|
69
|
+
@Album.load(:id=>1).tag_pks.must_equal [1, 2]
|
|
70
|
+
@Album.load(:id=>2).tag_pks.must_equal [2, 3]
|
|
71
|
+
@Album.load(:id=>3).tag_pks.must_equal []
|
|
72
72
|
end
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
it "should set associated pks correctly for a one_to_many association" do
|
|
75
75
|
@Artist.load(:id=>1).album_pks = [1, 2]
|
|
76
|
-
@db.sqls.
|
|
76
|
+
@db.sqls.must_equal ["UPDATE albums SET artist_id = 1 WHERE (id IN (1, 2))",
|
|
77
77
|
"UPDATE albums SET artist_id = NULL WHERE ((albums.artist_id = 1) AND (id NOT IN (1, 2)))"]
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
it "should use associated class's primary key for a one_to_many association" do
|
|
81
81
|
@Album.set_primary_key :foo
|
|
82
82
|
@Artist.load(:id=>1).album_pks = [1, 2]
|
|
83
|
-
@db.sqls.
|
|
83
|
+
@db.sqls.must_equal ["UPDATE albums SET artist_id = 1 WHERE (foo IN (1, 2))",
|
|
84
84
|
"UPDATE albums SET artist_id = NULL WHERE ((albums.artist_id = 1) AND (foo NOT IN (1, 2)))"]
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
it "should set associated pks correctly for a many_to_many association" do
|
|
88
88
|
@Album.load(:id=>2).tag_pks = [1, 3]
|
|
89
89
|
sqls = @db.sqls
|
|
90
|
-
sqls[0].
|
|
91
|
-
sqls[1].
|
|
92
|
-
sqls[2].
|
|
93
|
-
sqls[3].
|
|
94
|
-
sqls[4].
|
|
95
|
-
sqls.length.
|
|
90
|
+
sqls[0].must_equal "DELETE FROM albums_tags WHERE ((album_id = 2) AND (tag_id NOT IN (1, 3)))"
|
|
91
|
+
sqls[1].must_equal 'SELECT tag_id FROM albums_tags WHERE (album_id = 2)'
|
|
92
|
+
sqls[2].must_equal 'BEGIN'
|
|
93
|
+
sqls[3].must_match(/INSERT INTO albums_tags \((album_id, tag_id|tag_id, album_id)\) VALUES \((2, 1|1, 2)\)/)
|
|
94
|
+
sqls[4].must_equal 'COMMIT'
|
|
95
|
+
sqls.length.must_equal 5
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
it "should return correct right-side associated cpks for one_to_many associations" do
|
|
99
99
|
@Album.one_to_many :vocalists, :class=>@Vocalist, :key=>:album_id
|
|
100
|
-
@Album.load(:id=>1).vocalist_pks.
|
|
101
|
-
@Album.load(:id=>2).vocalist_pks.
|
|
100
|
+
@Album.load(:id=>1).vocalist_pks.must_equal [["F1", "L1"], ["F2", "L2"]]
|
|
101
|
+
@Album.load(:id=>2).vocalist_pks.must_equal []
|
|
102
102
|
end
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
it "should return correct right-side associated cpks for many_to_many associations" do
|
|
105
105
|
@Album.many_to_many :vocalists, :class=>@Vocalist, :join_table=>:albums_vocalists, :left_key=>:album_id, :right_key=>[:first, :last]
|
|
106
|
-
@Album.load(:id=>1).vocalist_pks.
|
|
107
|
-
@Album.load(:id=>2).vocalist_pks.
|
|
108
|
-
@Album.load(:id=>3).vocalist_pks.
|
|
106
|
+
@Album.load(:id=>1).vocalist_pks.must_equal [["F1", "L1"], ["F2", "L2"]]
|
|
107
|
+
@Album.load(:id=>2).vocalist_pks.must_equal [["F2", "L2"], ["F3", "L3"]]
|
|
108
|
+
@Album.load(:id=>3).vocalist_pks.must_equal []
|
|
109
109
|
end
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
it "should set associated right-side cpks correctly for a one_to_many association" do
|
|
112
112
|
@Album.one_to_many :vocalists, :class=>@Vocalist, :key=>:album_id
|
|
113
113
|
@Album.load(:id=>1).vocalist_pks = [["F1", "L1"], ["F2", "L2"]]
|
|
114
|
-
@db.sqls.
|
|
114
|
+
@db.sqls.must_equal ["UPDATE vocalists SET album_id = 1 WHERE ((first, last) IN (('F1', 'L1'), ('F2', 'L2')))",
|
|
115
115
|
"UPDATE vocalists SET album_id = NULL WHERE ((vocalists.album_id = 1) AND ((first, last) NOT IN (('F1', 'L1'), ('F2', 'L2'))))"]
|
|
116
116
|
end
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
it "should set associated right-side cpks correctly for a many_to_many association" do
|
|
119
119
|
@Album.many_to_many :vocalists, :class=>@Vocalist, :join_table=>:albums_vocalists, :left_key=>:album_id, :right_key=>[:first, :last]
|
|
120
120
|
@Album.load(:id=>2).vocalist_pks = [["F1", "L1"], ["F2", "L2"]]
|
|
121
121
|
sqls = @db.sqls
|
|
122
|
-
sqls[0].
|
|
123
|
-
sqls[1].
|
|
122
|
+
sqls[0].must_equal "DELETE FROM albums_vocalists WHERE ((album_id = 2) AND ((first, last) NOT IN (('F1', 'L1'), ('F2', 'L2'))))"
|
|
123
|
+
sqls[1].must_equal 'SELECT first, last FROM albums_vocalists WHERE (album_id = 2)'
|
|
124
124
|
match = sqls[3].match(/INSERT INTO albums_vocalists \((.*)\) VALUES \((.*)\)/)
|
|
125
|
-
Hash[match[1].split(', ').zip(match[2].split(', '))].
|
|
126
|
-
sqls.length.
|
|
125
|
+
Hash[match[1].split(', ').zip(match[2].split(', '))].must_equal("first"=>"'F1'", "last"=>"'L1'", "album_id"=>"2")
|
|
126
|
+
sqls.length.must_equal 5
|
|
127
127
|
end
|
|
128
128
|
|
|
129
|
-
|
|
129
|
+
it "should return correct associated pks for left-side cpks for one_to_many associations" do
|
|
130
130
|
@Vocalist.one_to_many :instruments, :class=>@Instrument, :key=>[:first, :last]
|
|
131
|
-
@Vocalist.load(:first=>'F1', :last=>'L1').instrument_pks.
|
|
132
|
-
@Vocalist.load(:first=>'F2', :last=>'L2').instrument_pks.
|
|
131
|
+
@Vocalist.load(:first=>'F1', :last=>'L1').instrument_pks.must_equal [1, 2]
|
|
132
|
+
@Vocalist.load(:first=>'F2', :last=>'L2').instrument_pks.must_equal []
|
|
133
133
|
end
|
|
134
134
|
|
|
135
|
-
|
|
135
|
+
it "should return correct associated pks for left-side cpks for many_to_many associations" do
|
|
136
136
|
@Vocalist.many_to_many :instruments, :class=>@Instrument, :join_table=>:vocalists_instruments, :left_key=>[:first, :last]
|
|
137
|
-
@Vocalist.load(:first=>'F1', :last=>'L1').instrument_pks.
|
|
138
|
-
@Vocalist.load(:first=>'F2', :last=>'L2').instrument_pks.
|
|
139
|
-
@Vocalist.load(:first=>'F3', :last=>'L3').instrument_pks.
|
|
137
|
+
@Vocalist.load(:first=>'F1', :last=>'L1').instrument_pks.must_equal [1, 2]
|
|
138
|
+
@Vocalist.load(:first=>'F2', :last=>'L2').instrument_pks.must_equal [2, 3]
|
|
139
|
+
@Vocalist.load(:first=>'F3', :last=>'L3').instrument_pks.must_equal []
|
|
140
140
|
end
|
|
141
141
|
|
|
142
|
-
|
|
142
|
+
it "should set associated pks correctly for left-side cpks for a one_to_many association" do
|
|
143
143
|
@Vocalist.one_to_many :instruments, :class=>@Instrument, :key=>[:first, :last]
|
|
144
144
|
@Vocalist.load(:first=>'F1', :last=>'L1').instrument_pks = [1, 2]
|
|
145
145
|
sqls = @db.sqls
|
|
146
|
-
sqls[0].
|
|
147
|
-
sqls[1].
|
|
148
|
-
sqls.length.
|
|
146
|
+
sqls[0].must_match(/UPDATE instruments SET (first = 'F1', last = 'L1'|last = 'L1', first = 'F1') WHERE \(id IN \(1, 2\)\)/)
|
|
147
|
+
sqls[1].must_match(/UPDATE instruments SET (first = NULL, last = NULL|last = NULL, first = NULL) WHERE \(\(instruments.first = 'F1'\) AND \(instruments.last = 'L1'\) AND \(id NOT IN \(1, 2\)\)\)/)
|
|
148
|
+
sqls.length.must_equal 2
|
|
149
149
|
end
|
|
150
150
|
|
|
151
|
-
|
|
151
|
+
it "should set associated pks correctly for left-side cpks for a many_to_many association" do
|
|
152
152
|
@Vocalist.many_to_many :instruments, :class=>@Instrument, :join_table=>:vocalists_instruments, :left_key=>[:first, :last]
|
|
153
153
|
@Vocalist.load(:first=>'F2', :last=>'L2').instrument_pks = [1, 2]
|
|
154
154
|
sqls = @db.sqls
|
|
155
|
-
sqls[0].
|
|
156
|
-
sqls[1].
|
|
155
|
+
sqls[0].must_equal "DELETE FROM vocalists_instruments WHERE ((first = 'F2') AND (last = 'L2') AND (instrument_id NOT IN (1, 2)))"
|
|
156
|
+
sqls[1].must_equal "SELECT instrument_id FROM vocalists_instruments WHERE ((first = 'F2') AND (last = 'L2'))"
|
|
157
157
|
match = sqls[3].match(/INSERT INTO vocalists_instruments \((.*)\) VALUES \((.*)\)/)
|
|
158
|
-
Hash[match[1].split(', ').zip(match[2].split(', '))].
|
|
159
|
-
sqls.length.
|
|
158
|
+
Hash[match[1].split(', ').zip(match[2].split(', '))].must_equal("first"=>"'F2'", "last"=>"'L2'", "instrument_id"=>"1")
|
|
159
|
+
sqls.length.must_equal 5
|
|
160
160
|
end
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
it "should return correct right-side associated cpks for left-side cpks for one_to_many associations" do
|
|
163
163
|
@Vocalist.one_to_many :hits, :class=>@Hit, :key=>[:first, :last]
|
|
164
|
-
@Vocalist.load(:first=>'F1', :last=>'L1').hit_pks.
|
|
165
|
-
@Vocalist.load(:first=>'F2', :last=>'L2').hit_pks.
|
|
164
|
+
@Vocalist.load(:first=>'F1', :last=>'L1').hit_pks.must_equal [[1997, 1], [1997, 2]]
|
|
165
|
+
@Vocalist.load(:first=>'F2', :last=>'L2').hit_pks.must_equal []
|
|
166
166
|
end
|
|
167
167
|
|
|
168
|
-
|
|
168
|
+
it "should return correct right-side associated cpks for left-side cpks for many_to_many associations" do
|
|
169
169
|
@Vocalist.many_to_many :hits, :class=>@Hit, :join_table=>:vocalists_hits, :left_key=>[:first, :last], :right_key=>[:year, :week]
|
|
170
|
-
@Vocalist.load(:first=>'F1', :last=>'L1').hit_pks.
|
|
171
|
-
@Vocalist.load(:first=>'F2', :last=>'L2').hit_pks.
|
|
172
|
-
@Vocalist.load(:first=>'F3', :last=>'L3').hit_pks.
|
|
170
|
+
@Vocalist.load(:first=>'F1', :last=>'L1').hit_pks.must_equal [[1997, 1], [1997, 2]]
|
|
171
|
+
@Vocalist.load(:first=>'F2', :last=>'L2').hit_pks.must_equal [[1997, 2], [1997, 3]]
|
|
172
|
+
@Vocalist.load(:first=>'F3', :last=>'L3').hit_pks.must_equal []
|
|
173
173
|
end
|
|
174
174
|
|
|
175
|
-
|
|
175
|
+
it "should set associated right-side cpks correctly for left-side cpks for a one_to_many association" do
|
|
176
176
|
@Vocalist.one_to_many :hits, :class=>@Hit, :key=>[:first, :last], :order=>:week
|
|
177
177
|
@Vocalist.load(:first=>'F1', :last=>'L1').hit_pks = [[1997, 1], [1997, 2]]
|
|
178
178
|
sqls = @db.sqls
|
|
179
|
-
sqls[0].
|
|
180
|
-
sqls[1].
|
|
181
|
-
sqls.length.
|
|
179
|
+
sqls[0].must_match(/UPDATE hits SET (first = 'F1', last = 'L1'|last = 'L1', first = 'F1') WHERE \(\(year, week\) IN \(\(1997, 1\), \(1997, 2\)\)\)/)
|
|
180
|
+
sqls[1].must_match(/UPDATE hits SET (first = NULL, last = NULL|last = NULL, first = NULL) WHERE \(\(hits.first = 'F1'\) AND \(hits.last = 'L1'\) AND \(\(year, week\) NOT IN \(\(1997, 1\), \(1997, 2\)\)\)\)/)
|
|
181
|
+
sqls.length.must_equal 2
|
|
182
182
|
end
|
|
183
183
|
|
|
184
|
-
|
|
184
|
+
it "should set associated right-side cpks correctly for left-side cpks for a many_to_many association" do
|
|
185
185
|
@Vocalist.many_to_many :hits, :class=>@Hit, :join_table=>:vocalists_hits, :left_key=>[:first, :last], :right_key=>[:year, :week]
|
|
186
186
|
@Vocalist.load(:first=>'F2', :last=>'L2').hit_pks = [[1997, 1], [1997, 2]]
|
|
187
187
|
sqls = @db.sqls
|
|
188
|
-
sqls[0].
|
|
189
|
-
sqls[1].
|
|
188
|
+
sqls[0].must_equal "DELETE FROM vocalists_hits WHERE ((first = 'F2') AND (last = 'L2') AND ((year, week) NOT IN ((1997, 1), (1997, 2))))"
|
|
189
|
+
sqls[1].must_equal "SELECT year, week FROM vocalists_hits WHERE ((first = 'F2') AND (last = 'L2'))"
|
|
190
190
|
match = sqls[3].match(/INSERT INTO vocalists_hits \((.*)\) VALUES \((.*)\)/)
|
|
191
|
-
Hash[match[1].split(', ').zip(match[2].split(', '))].
|
|
192
|
-
sqls.length.
|
|
191
|
+
Hash[match[1].split(', ').zip(match[2].split(', '))].must_equal("first"=>"'F2'", "last"=>"'L2'", "year"=>"1997", "week"=>"1")
|
|
192
|
+
sqls.length.must_equal 5
|
|
193
193
|
end
|
|
194
194
|
|
|
195
|
-
|
|
195
|
+
it "should use transactions if the object is configured to use transactions" do
|
|
196
196
|
artist = @Artist.load(:id=>1)
|
|
197
197
|
artist.use_transactions = true
|
|
198
198
|
artist.album_pks = [1, 2]
|
|
199
|
-
@db.sqls.
|
|
199
|
+
@db.sqls.must_equal ["BEGIN",
|
|
200
200
|
"UPDATE albums SET artist_id = 1 WHERE (id IN (1, 2))",
|
|
201
201
|
"UPDATE albums SET artist_id = NULL WHERE ((albums.artist_id = 1) AND (id NOT IN (1, 2)))",
|
|
202
202
|
"COMMIT"]
|
|
@@ -205,77 +205,77 @@ describe "Sequel::Plugins::AssociationPks" do
|
|
|
205
205
|
album.use_transactions = true
|
|
206
206
|
album.tag_pks = [1, 3]
|
|
207
207
|
sqls = @db.sqls
|
|
208
|
-
sqls[0].
|
|
209
|
-
sqls[1].
|
|
210
|
-
sqls[2].
|
|
211
|
-
sqls[3].
|
|
212
|
-
sqls[4].
|
|
213
|
-
sqls.length.
|
|
208
|
+
sqls[0].must_equal "BEGIN"
|
|
209
|
+
sqls[1].must_equal "DELETE FROM albums_tags WHERE ((album_id = 2) AND (tag_id NOT IN (1, 3)))"
|
|
210
|
+
sqls[2].must_equal 'SELECT tag_id FROM albums_tags WHERE (album_id = 2)'
|
|
211
|
+
sqls[3].must_match(/INSERT INTO albums_tags \((album_id, tag_id|tag_id, album_id)\) VALUES \((2, 1|1, 2)\)/)
|
|
212
|
+
sqls[4].must_equal "COMMIT"
|
|
213
|
+
sqls.length.must_equal 5
|
|
214
214
|
end
|
|
215
215
|
|
|
216
|
-
|
|
216
|
+
it "should automatically convert keys to numbers if the primary key is an integer for one_to_many associations" do
|
|
217
217
|
@Album.db_schema[:id][:type] = :integer
|
|
218
218
|
@Artist.load(:id=>1).album_pks = %w'1 2'
|
|
219
|
-
@db.sqls.
|
|
219
|
+
@db.sqls.must_equal ["UPDATE albums SET artist_id = 1 WHERE (id IN (1, 2))",
|
|
220
220
|
"UPDATE albums SET artist_id = NULL WHERE ((albums.artist_id = 1) AND (id NOT IN (1, 2)))"]
|
|
221
221
|
end
|
|
222
222
|
|
|
223
|
-
|
|
223
|
+
it "should not automatically convert keys if the primary key is not an integer for one_to_many associations" do
|
|
224
224
|
@Album.db_schema[:id][:type] = :string
|
|
225
225
|
@Artist.load(:id=>1).album_pks = %w'1 2'
|
|
226
|
-
@db.sqls.
|
|
226
|
+
@db.sqls.must_equal ["UPDATE albums SET artist_id = 1 WHERE (id IN ('1', '2'))",
|
|
227
227
|
"UPDATE albums SET artist_id = NULL WHERE ((albums.artist_id = 1) AND (id NOT IN ('1', '2')))"]
|
|
228
228
|
end
|
|
229
229
|
|
|
230
|
-
|
|
230
|
+
it "should automatically convert keys to numbers if the primary key is an integer for many_to_many associations" do
|
|
231
231
|
@Tag.db_schema[:id][:type] = :integer
|
|
232
232
|
@Album.load(:id=>2).tag_pks = %w'1 3'
|
|
233
233
|
sqls = @db.sqls
|
|
234
|
-
sqls[0].
|
|
235
|
-
sqls[1].
|
|
236
|
-
sqls[3].
|
|
237
|
-
sqls.length.
|
|
234
|
+
sqls[0].must_equal "DELETE FROM albums_tags WHERE ((album_id = 2) AND (tag_id NOT IN (1, 3)))"
|
|
235
|
+
sqls[1].must_equal 'SELECT tag_id FROM albums_tags WHERE (album_id = 2)'
|
|
236
|
+
sqls[3].must_match(/INSERT INTO albums_tags \((album_id, tag_id|tag_id, album_id)\) VALUES \((2, 1|1, 2)\)/)
|
|
237
|
+
sqls.length.must_equal 5
|
|
238
238
|
end
|
|
239
239
|
|
|
240
|
-
|
|
240
|
+
it "should not automatically convert keys to numbers if the primary key is an integer for many_to_many associations" do
|
|
241
241
|
@Tag.db_schema[:id][:type] = :string
|
|
242
242
|
@Album.load(:id=>2).tag_pks = %w'1 3'
|
|
243
243
|
sqls = @db.sqls
|
|
244
|
-
sqls[0].
|
|
245
|
-
sqls[1].
|
|
246
|
-
sqls[3].
|
|
247
|
-
sqls[4].
|
|
248
|
-
sqls.length.
|
|
244
|
+
sqls[0].must_equal "DELETE FROM albums_tags WHERE ((album_id = 2) AND (tag_id NOT IN ('1', '3')))"
|
|
245
|
+
sqls[1].must_equal 'SELECT tag_id FROM albums_tags WHERE (album_id = 2)'
|
|
246
|
+
sqls[3].must_match(/INSERT INTO albums_tags \((album_id, tag_id|tag_id, album_id)\) VALUES \((2, '1'|'1', 2)\)/)
|
|
247
|
+
sqls[4].must_match(/INSERT INTO albums_tags \((album_id, tag_id|tag_id, album_id)\) VALUES \((2, '3'|'3', 2)\)/)
|
|
248
|
+
sqls.length.must_equal 6
|
|
249
249
|
end
|
|
250
250
|
|
|
251
|
-
|
|
251
|
+
it "should automatically convert keys to numbers for appropriate integer primary key for composite key associations" do
|
|
252
252
|
@Hit.db_schema[:year][:type] = :integer
|
|
253
253
|
@Hit.db_schema[:week][:type] = :integer
|
|
254
254
|
@Vocalist.many_to_many :hits, :class=>@Hit, :join_table=>:vocalists_hits, :left_key=>[:first, :last], :right_key=>[:year, :week]
|
|
255
255
|
@Vocalist.load(:first=>'F2', :last=>'L2').hit_pks = [['1997', '1'], ['1997', '2']]
|
|
256
256
|
sqls = @db.sqls
|
|
257
|
-
sqls[0].
|
|
258
|
-
sqls[1].
|
|
257
|
+
sqls[0].must_equal "DELETE FROM vocalists_hits WHERE ((first = 'F2') AND (last = 'L2') AND ((year, week) NOT IN ((1997, 1), (1997, 2))))"
|
|
258
|
+
sqls[1].must_equal "SELECT year, week FROM vocalists_hits WHERE ((first = 'F2') AND (last = 'L2'))"
|
|
259
259
|
match = sqls[3].match(/INSERT INTO vocalists_hits \((.*)\) VALUES \((.*)\)/)
|
|
260
|
-
Hash[match[1].split(', ').zip(match[2].split(', '))].
|
|
261
|
-
sqls.length.
|
|
260
|
+
Hash[match[1].split(', ').zip(match[2].split(', '))].must_equal("first"=>"'F2'", "last"=>"'L2'", "year"=>"1997", "week"=>"1")
|
|
261
|
+
sqls.length.must_equal 5
|
|
262
262
|
|
|
263
263
|
@Vocalist.db_schema[:first][:type] = :integer
|
|
264
264
|
@Vocalist.db_schema[:last][:type] = :integer
|
|
265
265
|
@Album.one_to_many :vocalists, :class=>@Vocalist, :key=>:album_id
|
|
266
266
|
@Album.load(:id=>1).vocalist_pks = [["11", "11"], ["12", "12"]]
|
|
267
|
-
@db.sqls.
|
|
267
|
+
@db.sqls.must_equal ["UPDATE vocalists SET album_id = 1 WHERE ((first, last) IN ((11, 11), (12, 12)))",
|
|
268
268
|
"UPDATE vocalists SET album_id = NULL WHERE ((vocalists.album_id = 1) AND ((first, last) NOT IN ((11, 11), (12, 12))))"]
|
|
269
269
|
|
|
270
270
|
@Album.many_to_many :vocalists, :class=>@Vocalist, :join_table=>:albums_vocalists, :left_key=>:album_id, :right_key=>[:first, :last]
|
|
271
271
|
@Album.load(:id=>2).vocalist_pks = [["11", "11"], ["12", "12"]]
|
|
272
272
|
sqls = @db.sqls
|
|
273
|
-
sqls[0].
|
|
274
|
-
sqls[1].
|
|
273
|
+
sqls[0].must_equal "DELETE FROM albums_vocalists WHERE ((album_id = 2) AND ((first, last) NOT IN ((11, 11), (12, 12))))"
|
|
274
|
+
sqls[1].must_equal 'SELECT first, last FROM albums_vocalists WHERE (album_id = 2)'
|
|
275
275
|
match = sqls[3].match(/INSERT INTO albums_vocalists \((.*)\) VALUES \((.*)\)/)
|
|
276
|
-
Hash[match[1].split(', ').zip(match[2].split(', '))].
|
|
276
|
+
Hash[match[1].split(', ').zip(match[2].split(', '))].must_equal("first"=>"11", "last"=>"11", "album_id"=>"2")
|
|
277
277
|
match = sqls[4].match(/INSERT INTO albums_vocalists \((.*)\) VALUES \((.*)\)/)
|
|
278
|
-
Hash[match[1].split(', ').zip(match[2].split(', '))].
|
|
279
|
-
sqls.length.
|
|
278
|
+
Hash[match[1].split(', ').zip(match[2].split(', '))].must_equal("first"=>"12", "last"=>"12", "album_id"=>"2")
|
|
279
|
+
sqls.length.must_equal 6
|
|
280
280
|
end
|
|
281
281
|
end
|