sequel 4.22.0 → 4.23.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -13,80 +13,80 @@ describe "pg_static_cache_updater extension" do
|
|
13
13
|
@db.sqls
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
it "#create_static_cache_update_function should create a function in the database" do
|
17
17
|
@db.create_static_cache_update_function
|
18
|
-
@db.sqls.first.gsub(/\s+/, ' ').
|
18
|
+
@db.sqls.first.gsub(/\s+/, ' ').must_equal " CREATE FUNCTION sequel_static_cache_update() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN PERFORM pg_notify(''sequel_static_cache_update'', TG_RELID::text); RETURN NULL; END ' "
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
it "#create_static_cache_update_function should support :channel_name and :function_name options" do
|
22
22
|
@db.create_static_cache_update_function(:channel_name=>'foo', :function_name=>'bar')
|
23
|
-
@db.sqls.first.gsub(/\s+/, ' ').
|
23
|
+
@db.sqls.first.gsub(/\s+/, ' ').must_equal " CREATE FUNCTION bar() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN PERFORM pg_notify(''foo'', TG_RELID::text); RETURN NULL; END ' "
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
it "#create_static_cache_update_trigger should create a trigger for the database table" do
|
27
27
|
@db.create_static_cache_update_trigger(:tab)
|
28
|
-
@db.sqls.first.gsub(/\s+/, ' ').
|
28
|
+
@db.sqls.first.gsub(/\s+/, ' ').must_equal "CREATE TRIGGER sequel_static_cache_update AFTER INSERT OR UPDATE OR DELETE ON tab EXECUTE PROCEDURE sequel_static_cache_update()"
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
it "#create_static_cache_update_trigger should support :trigger_name and :function_name options" do
|
32
32
|
@db.create_static_cache_update_trigger(:tab, :trigger_name=>'foo', :function_name=>'bar')
|
33
|
-
@db.sqls.first.gsub(/\s+/, ' ').
|
33
|
+
@db.sqls.first.gsub(/\s+/, ' ').must_equal "CREATE TRIGGER foo AFTER INSERT OR UPDATE OR DELETE ON tab EXECUTE PROCEDURE bar()"
|
34
34
|
end
|
35
35
|
|
36
|
-
|
37
|
-
@db.default_static_cache_update_name.
|
36
|
+
it "#default_static_cache_update_name should return the default name for function, trigger, and channel" do
|
37
|
+
@db.default_static_cache_update_name.must_equal :sequel_static_cache_update
|
38
38
|
end
|
39
39
|
|
40
|
-
|
40
|
+
it "#listen_for_static_cache_updates should listen for changes to model tables and reload model classes" do
|
41
41
|
@db.fetch = {:v=>1234}
|
42
42
|
@db.listen_for_static_cache_updates([@model], :yield=>[nil, nil, 1234]).join
|
43
|
-
@db.sqls.
|
43
|
+
@db.sqls.must_equal ["SELECT CAST(CAST('table' AS regclass) AS oid) AS v LIMIT 1", "LISTEN sequel_static_cache_update", "SELECT * FROM table"]
|
44
44
|
end
|
45
45
|
|
46
|
-
|
46
|
+
it "#listen_for_static_cache_updates should not reload model classes if oid doesn't match" do
|
47
47
|
@db.fetch = {:v=>1234}
|
48
48
|
@db.listen_for_static_cache_updates([@model], :yield=>[nil, nil, 12345]).join
|
49
|
-
@db.sqls.
|
49
|
+
@db.sqls.must_equal ["SELECT CAST(CAST('table' AS regclass) AS oid) AS v LIMIT 1", "LISTEN sequel_static_cache_update"]
|
50
50
|
end
|
51
51
|
|
52
|
-
|
52
|
+
it "#listen_for_static_cache_updates should support a single model argument" do
|
53
53
|
@db.fetch = {:v=>1234}
|
54
54
|
@db.listen_for_static_cache_updates(@model, :yield=>[nil, nil, 1234]).join
|
55
|
-
@db.sqls.
|
55
|
+
@db.sqls.must_equal ["SELECT CAST(CAST('table' AS regclass) AS oid) AS v LIMIT 1", "LISTEN sequel_static_cache_update", "SELECT * FROM table"]
|
56
56
|
end
|
57
57
|
|
58
|
-
|
58
|
+
it "#listen_for_static_cache_updates should support the :channel_name option" do
|
59
59
|
@db.fetch = {:v=>1234}
|
60
60
|
@db.listen_for_static_cache_updates([@model], :yield=>[nil, nil, 12345], :channel_name=>:foo).join
|
61
|
-
@db.sqls.
|
61
|
+
@db.sqls.must_equal ["SELECT CAST(CAST('table' AS regclass) AS oid) AS v LIMIT 1", "LISTEN foo"]
|
62
62
|
end
|
63
63
|
|
64
|
-
|
64
|
+
it "#listen_for_static_cache_updates should raise an error if given an empty array" do
|
65
65
|
@db.fetch = {:v=>1234}
|
66
|
-
proc{@db.listen_for_static_cache_updates([])}.
|
66
|
+
proc{@db.listen_for_static_cache_updates([])}.must_raise(Sequel::Error)
|
67
67
|
end
|
68
68
|
|
69
|
-
|
69
|
+
it "#listen_for_static_cache_updates should raise an error if one of the models is not using the static cache plugin" do
|
70
70
|
@db.fetch = {:v=>1234}
|
71
|
-
proc{@db.listen_for_static_cache_updates(Class.new(Sequel::Model(@db[:table])))}.
|
71
|
+
proc{@db.listen_for_static_cache_updates(Class.new(Sequel::Model(@db[:table])))}.must_raise(Sequel::Error)
|
72
72
|
end
|
73
73
|
|
74
|
-
|
74
|
+
it "#listen_for_static_cache_updates should raise an error if the database doesn't respond to listen" do
|
75
75
|
@db = Sequel.mock(:host=>'postgres')
|
76
76
|
@db.extension(:pg_static_cache_updater)
|
77
77
|
@db.fetch = {:v=>1234}
|
78
|
-
proc{@db.listen_for_static_cache_updates(Class.new(Sequel::Model(@db[:table])))}.
|
78
|
+
proc{@db.listen_for_static_cache_updates(Class.new(Sequel::Model(@db[:table])))}.must_raise(Sequel::Error)
|
79
79
|
end
|
80
80
|
|
81
|
-
|
81
|
+
it "#listen_for_static_cache_updates should handle a :before_thread_exit option" do
|
82
82
|
a = []
|
83
83
|
@db.listen_for_static_cache_updates([@model], :yield=>[nil, nil, 12345], :before_thread_exit=>proc{a << 1}).join
|
84
|
-
a.
|
84
|
+
a.must_equal [1]
|
85
85
|
end
|
86
86
|
|
87
|
-
|
87
|
+
it "#listen_for_static_cache_updates should call :before_thread_exit option even if listen raises an exception" do
|
88
88
|
a = []
|
89
89
|
@db.listen_for_static_cache_updates([@model], :yield=>[nil, nil, 12345], :after_listen=>proc{raise ArgumentError}, :before_thread_exit=>proc{a << 1}).join
|
90
|
-
a.
|
90
|
+
a.must_equal [1]
|
91
91
|
end
|
92
92
|
end
|
@@ -10,54 +10,54 @@ describe Sequel::Model, "PgTypecastOnLoad plugin" do
|
|
10
10
|
@c.plugin :pg_typecast_on_load, :b, :y
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
@c.first.values.
|
13
|
+
it "should call the database conversion proc for all given columns" do
|
14
|
+
@c.first.values.must_equal(:id=>1, :b=>true, :y=>0)
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
@c.first.refresh.values.
|
17
|
+
it "should call the database conversion proc with value when reloading the object, for all given columns" do
|
18
|
+
@c.first.refresh.values.must_equal(:id=>1, :b=>true, :y=>0)
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
it "should not fail if schema oid does not have a related conversion proc" do
|
22
22
|
@c.db_schema[:b][:oid] = 0
|
23
|
-
@c.first.refresh.values.
|
23
|
+
@c.first.refresh.values.must_equal(:id=>1, :b=>"t", :y=>0)
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
it "should call the database conversion proc with value when automatically reloading the object on creation via insert_select" do
|
27
27
|
@c.dataset.meta_def(:insert_select){|h| insert(h); first}
|
28
|
-
@c.create.values.
|
28
|
+
@c.create.values.must_equal(:id=>1, :b=>true, :y=>0)
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
it "should allowing setting columns separately via add_pg_typecast_on_load_columns" do
|
32
32
|
@c = Class.new(Sequel::Model(@db[:items]))
|
33
33
|
@c.plugin :pg_typecast_on_load
|
34
|
-
@c.first.values.
|
34
|
+
@c.first.values.must_equal(:id=>1, :b=>"t", :y=>"0")
|
35
35
|
@c.add_pg_typecast_on_load_columns :b
|
36
|
-
@c.first.values.
|
36
|
+
@c.first.values.must_equal(:id=>1, :b=>true, :y=>"0")
|
37
37
|
@c.add_pg_typecast_on_load_columns :y
|
38
|
-
@c.first.values.
|
38
|
+
@c.first.values.must_equal(:id=>1, :b=>true, :y=>0)
|
39
39
|
end
|
40
40
|
|
41
|
-
|
41
|
+
it "should work with subclasses" do
|
42
42
|
@c = Class.new(Sequel::Model(@db[:items]))
|
43
43
|
@c.plugin :pg_typecast_on_load
|
44
|
-
@c.first.values.
|
44
|
+
@c.first.values.must_equal(:id=>1, :b=>"t", :y=>"0")
|
45
45
|
|
46
46
|
c1 = Class.new(@c)
|
47
47
|
@c.add_pg_typecast_on_load_columns :b
|
48
|
-
@c.first.values.
|
49
|
-
c1.first.values.
|
48
|
+
@c.first.values.must_equal(:id=>1, :b=>true, :y=>"0")
|
49
|
+
c1.first.values.must_equal(:id=>1, :b=>"t", :y=>"0")
|
50
50
|
|
51
51
|
c2 = Class.new(@c)
|
52
52
|
@c.add_pg_typecast_on_load_columns :y
|
53
|
-
@c.first.values.
|
54
|
-
c2.first.values.
|
53
|
+
@c.first.values.must_equal(:id=>1, :b=>true, :y=>0)
|
54
|
+
c2.first.values.must_equal(:id=>1, :b=>true, :y=>"0")
|
55
55
|
|
56
56
|
c1.add_pg_typecast_on_load_columns :y
|
57
|
-
c1.first.values.
|
57
|
+
c1.first.values.must_equal(:id=>1, :b=>"t", :y=>0)
|
58
58
|
end
|
59
59
|
|
60
|
-
|
61
|
-
@c.first.modified?.
|
60
|
+
it "should not mark the object as modified" do
|
61
|
+
@c.first.modified?.must_equal false
|
62
62
|
end
|
63
63
|
end
|
@@ -29,30 +29,30 @@ describe "Sequel::Plugins::PreparedStatementsAssociations" do
|
|
29
29
|
@db.sqls
|
30
30
|
end
|
31
31
|
|
32
|
-
|
32
|
+
it "should run correct SQL for associations" do
|
33
33
|
@Artist.load(:id=>1).albums
|
34
|
-
@db.sqls.
|
34
|
+
@db.sqls.must_equal ["SELECT id, artist_id, id2, artist_id2 FROM albums WHERE (albums.artist_id = 1) -- prepared"]
|
35
35
|
|
36
36
|
@Artist.load(:id=>1).album
|
37
|
-
@db.sqls.
|
37
|
+
@db.sqls.must_equal ["SELECT id, artist_id, id2, artist_id2 FROM albums WHERE (albums.artist_id = 1) LIMIT 1 -- prepared"]
|
38
38
|
|
39
39
|
@Album.load(:id=>1, :artist_id=>2).artist
|
40
|
-
@db.sqls.
|
40
|
+
@db.sqls.must_equal ["SELECT id, id2 FROM artists WHERE (artists.id = 2) LIMIT 1 -- prepared"]
|
41
41
|
|
42
42
|
@Album.load(:id=>1, :artist_id=>2).tags
|
43
|
-
@db.sqls.
|
43
|
+
@db.sqls.must_equal ["SELECT tags.id, tags.id2 FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE (albums_tags.album_id = 1) -- prepared"]
|
44
44
|
|
45
45
|
@Album.load(:id=>1, :artist_id=>2).tag
|
46
|
-
@db.sqls.
|
46
|
+
@db.sqls.must_equal ["SELECT tags.id, tags.id2 FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE (albums_tags.album_id = 1) LIMIT 1 -- prepared"]
|
47
47
|
|
48
48
|
@Artist.load(:id=>1).tags
|
49
|
-
@db.sqls.
|
49
|
+
@db.sqls.must_equal ["SELECT tags.id, tags.id2 FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) WHERE (albums.artist_id = 1) -- prepared"]
|
50
50
|
|
51
51
|
@Artist.load(:id=>1).tag
|
52
|
-
@db.sqls.
|
52
|
+
@db.sqls.must_equal ["SELECT tags.id, tags.id2 FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) WHERE (albums.artist_id = 1) LIMIT 1 -- prepared"]
|
53
53
|
end
|
54
54
|
|
55
|
-
|
55
|
+
it "should run correct SQL for composite key associations" do
|
56
56
|
@Artist.one_to_many :albums, :class=>@Album, :key=>[:artist_id, :artist_id2], :primary_key=>[:id, :id2]
|
57
57
|
@Artist.one_to_one :album, :class=>@Album, :key=>[:artist_id, :artist_id2], :primary_key=>[:id, :id2]
|
58
58
|
@Album.many_to_one :artist, :class=>@Artist, :key=>[:artist_id, :artist_id2], :primary_key=>[:id, :id2]
|
@@ -63,97 +63,97 @@ describe "Sequel::Plugins::PreparedStatementsAssociations" do
|
|
63
63
|
@Artist.one_through_many :tag, :clone=>:tags
|
64
64
|
|
65
65
|
@Artist.load(:id=>1, :id2=>2).albums
|
66
|
-
@db.sqls.
|
66
|
+
@db.sqls.must_equal ["SELECT id, artist_id, id2, artist_id2 FROM albums WHERE ((albums.artist_id = 1) AND (albums.artist_id2 = 2)) -- prepared"]
|
67
67
|
|
68
68
|
@Artist.load(:id=>1, :id2=>2).album
|
69
|
-
@db.sqls.
|
69
|
+
@db.sqls.must_equal ["SELECT id, artist_id, id2, artist_id2 FROM albums WHERE ((albums.artist_id = 1) AND (albums.artist_id2 = 2)) LIMIT 1 -- prepared"]
|
70
70
|
|
71
71
|
@Album.load(:id=>1, :artist_id=>2, :artist_id2=>3).artist
|
72
|
-
@db.sqls.
|
72
|
+
@db.sqls.must_equal ["SELECT id, id2 FROM artists WHERE ((artists.id = 2) AND (artists.id2 = 3)) LIMIT 1 -- prepared"]
|
73
73
|
|
74
74
|
@Album.load(:id=>1, :artist_id=>2, :id2=>3).tags
|
75
|
-
@db.sqls.
|
75
|
+
@db.sqls.must_equal ["SELECT tags.id, tags.id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id = tags.id) AND (albums_tags.tag_id2 = tags.id2)) WHERE ((albums_tags.album_id = 1) AND (albums_tags.album_id2 = 3)) -- prepared"]
|
76
76
|
|
77
77
|
@Album.load(:id=>1, :artist_id=>2, :id2=>3).tag
|
78
|
-
@db.sqls.
|
78
|
+
@db.sqls.must_equal ["SELECT tags.id, tags.id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id = tags.id) AND (albums_tags.tag_id2 = tags.id2)) WHERE ((albums_tags.album_id = 1) AND (albums_tags.album_id2 = 3)) LIMIT 1 -- prepared"]
|
79
79
|
|
80
80
|
@Artist.load(:id=>1, :id2=>2).tags
|
81
|
-
@db.sqls.
|
81
|
+
@db.sqls.must_equal ["SELECT tags.id, tags.id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id = tags.id) AND (albums_tags.tag_id2 = tags.id2)) INNER JOIN albums ON ((albums.id = albums_tags.album_id) AND (albums.id2 = albums_tags.album_id2)) WHERE ((albums.artist_id = 1) AND (albums.artist_id2 = 2)) -- prepared"]
|
82
82
|
|
83
83
|
@Artist.load(:id=>1, :id2=>2).tag
|
84
|
-
@db.sqls.
|
84
|
+
@db.sqls.must_equal ["SELECT tags.id, tags.id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id = tags.id) AND (albums_tags.tag_id2 = tags.id2)) INNER JOIN albums ON ((albums.id = albums_tags.album_id) AND (albums.id2 = albums_tags.album_id2)) WHERE ((albums.artist_id = 1) AND (albums.artist_id2 = 2)) LIMIT 1 -- prepared"]
|
85
85
|
end
|
86
86
|
|
87
|
-
|
88
|
-
@Artist.new.albums.
|
89
|
-
@Album.new.artist.
|
90
|
-
@db.sqls.
|
87
|
+
it "should not run query if no objects can be associated" do
|
88
|
+
@Artist.new.albums.must_equal []
|
89
|
+
@Album.new.artist.must_equal nil
|
90
|
+
@db.sqls.must_equal []
|
91
91
|
end
|
92
92
|
|
93
|
-
|
93
|
+
it "should run a regular query if not caching association metadata" do
|
94
94
|
@Artist.cache_associations = false
|
95
95
|
@Artist.load(:id=>1).albums
|
96
|
-
@db.sqls.
|
96
|
+
@db.sqls.must_equal ["SELECT * FROM albums WHERE (albums.artist_id = 1)"]
|
97
97
|
@Artist.load(:id=>1).album
|
98
|
-
@db.sqls.
|
98
|
+
@db.sqls.must_equal ["SELECT * FROM albums WHERE (albums.artist_id = 1) LIMIT 1"]
|
99
99
|
end
|
100
100
|
|
101
|
-
|
101
|
+
it "should run a regular query if there is a callback" do
|
102
102
|
@Artist.load(:id=>1).albums(proc{|ds| ds})
|
103
|
-
@db.sqls.
|
103
|
+
@db.sqls.must_equal ["SELECT * FROM albums WHERE (albums.artist_id = 1)"]
|
104
104
|
@Artist.load(:id=>1).album(proc{|ds| ds})
|
105
|
-
@db.sqls.
|
105
|
+
@db.sqls.must_equal ["SELECT * FROM albums WHERE (albums.artist_id = 1) LIMIT 1"]
|
106
106
|
end
|
107
107
|
|
108
|
-
|
108
|
+
it "should run a regular query if :prepared_statement=>false option is used for the association" do
|
109
109
|
@Artist.one_to_many :albums, :class=>@Album, :key=>:artist_id, :prepared_statement=>false
|
110
110
|
@Artist.load(:id=>1).albums
|
111
|
-
@db.sqls.
|
111
|
+
@db.sqls.must_equal ["SELECT * FROM albums WHERE (albums.artist_id = 1)"]
|
112
112
|
end
|
113
113
|
|
114
|
-
|
114
|
+
it "should run a regular query if unrecognized association is used" do
|
115
115
|
a = @Artist.one_to_many :albums, :class=>@Album, :key=>:artist_id
|
116
116
|
a[:type] = :foo
|
117
117
|
@Artist.load(:id=>1).albums
|
118
|
-
@db.sqls.
|
118
|
+
@db.sqls.must_equal ["SELECT * FROM albums WHERE (albums.artist_id = 1)"]
|
119
119
|
end
|
120
120
|
|
121
|
-
|
121
|
+
it "should run a regular query if a block is used when defining the association" do
|
122
122
|
@Artist.one_to_many :albums, :class=>@Album, :key=>:artist_id do |ds| ds end
|
123
123
|
@Artist.load(:id=>1).albums
|
124
|
-
@db.sqls.
|
124
|
+
@db.sqls.must_equal ["SELECT * FROM albums WHERE (albums.artist_id = 1)"]
|
125
125
|
end
|
126
126
|
|
127
|
-
|
127
|
+
it "should use a prepared statement if the associated dataset has conditions" do
|
128
128
|
@Album.dataset = @Album.dataset.where(:a=>2)
|
129
129
|
@Artist.one_to_many :albums, :class=>@Album, :key=>:artist_id
|
130
130
|
@Artist.load(:id=>1).albums
|
131
|
-
@db.sqls.
|
131
|
+
@db.sqls.must_equal ["SELECT id, artist_id, id2, artist_id2 FROM albums WHERE ((a = 2) AND (albums.artist_id = 1)) -- prepared"]
|
132
132
|
end
|
133
133
|
|
134
|
-
|
134
|
+
it "should use a prepared statement if the :conditions association option" do
|
135
135
|
@Artist.one_to_many :albums, :class=>@Album, :key=>:artist_id, :conditions=>{:a=>2}
|
136
136
|
@Artist.load(:id=>1).albums
|
137
|
-
@db.sqls.
|
137
|
+
@db.sqls.must_equal ["SELECT id, artist_id, id2, artist_id2 FROM albums WHERE ((a = 2) AND (albums.artist_id = 1)) -- prepared"]
|
138
138
|
end
|
139
139
|
|
140
|
-
|
140
|
+
it "should not use a prepared statement if :conditions association option uses an identifier" do
|
141
141
|
@Artist.one_to_many :albums, :class=>@Album, :key=>:artist_id, :conditions=>{Sequel.identifier('a')=>2}
|
142
142
|
@Artist.load(:id=>1).albums
|
143
|
-
@db.sqls.
|
143
|
+
@db.sqls.must_equal ["SELECT id, artist_id, id2, artist_id2 FROM albums WHERE ((a = 2) AND (albums.artist_id = 1)) -- prepared"]
|
144
144
|
end
|
145
145
|
|
146
|
-
|
146
|
+
it "should run a regular query if :dataset option is used when defining the association" do
|
147
147
|
album = @Album
|
148
148
|
@Artist.one_to_many :albums, :class=>@Album, :dataset=>proc{album.filter(:artist_id=>id)}
|
149
149
|
@Artist.load(:id=>1).albums
|
150
|
-
@db.sqls.
|
150
|
+
@db.sqls.must_equal ["SELECT * FROM albums WHERE (artist_id = 1)"]
|
151
151
|
end
|
152
152
|
|
153
|
-
|
153
|
+
it "should run a regular query if :cloning an association that doesn't used prepared statements" do
|
154
154
|
@Artist.one_to_many :albums, :class=>@Album, :key=>:artist_id do |ds| ds end
|
155
155
|
@Artist.one_to_many :oalbums, :clone=>:albums
|
156
156
|
@Artist.load(:id=>1).oalbums
|
157
|
-
@db.sqls.
|
157
|
+
@db.sqls.must_equal ["SELECT * FROM albums WHERE (albums.artist_id = 1)"]
|
158
158
|
end
|
159
159
|
end
|
@@ -11,51 +11,51 @@ describe "prepared_statements_safe plugin" do
|
|
11
11
|
@db.sqls
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
@c.plugins.
|
14
|
+
it "should load the prepared_statements plugin" do
|
15
|
+
@c.plugins.must_include(Sequel::Plugins::PreparedStatements)
|
16
16
|
end
|
17
17
|
|
18
|
-
|
19
|
-
@c.prepared_statements_column_defaults.
|
18
|
+
it "should set default values correctly" do
|
19
|
+
@c.prepared_statements_column_defaults.must_equal(:name=>nil, :i=>nil)
|
20
20
|
@c.instance_variable_set(:@db_schema, {:i=>{:default=>'f(x)'}, :name=>{:ruby_default=>'foo'}, :id=>{:primary_key=>true}})
|
21
|
-
Class.new(@c).prepared_statements_column_defaults.
|
21
|
+
Class.new(@c).prepared_statements_column_defaults.must_equal(:name=>'foo')
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
it "should set default values when creating" do
|
25
25
|
@c.create
|
26
|
-
@db.sqls.first.
|
26
|
+
@db.sqls.first.must_match(/INSERT INTO people \((i|name), (i|name)\) VALUES \(NULL, NULL\)/)
|
27
27
|
@c.create(:name=>'foo')
|
28
|
-
@db.sqls.first.
|
28
|
+
@db.sqls.first.must_match(/INSERT INTO people \((i|name), (i|name)\) VALUES \((NULL|'foo'), (NULL|'foo')\)/)
|
29
29
|
@c.create(:name=>'foo', :i=>2)
|
30
|
-
@db.sqls.first.
|
30
|
+
@db.sqls.first.must_match(/INSERT INTO people \((i|name), (i|name)\) VALUES \((2|'foo'), (2|'foo')\)/)
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
it "should use database default values" do
|
34
34
|
@c.instance_variable_set(:@db_schema, {:i=>{:ruby_default=>2}, :name=>{:ruby_default=>'foo'}, :id=>{:primary_key=>true}})
|
35
35
|
c = Class.new(@c)
|
36
36
|
c.create
|
37
|
-
@db.sqls.first.
|
37
|
+
@db.sqls.first.must_match(/INSERT INTO people \((i|name), (i|name)\) VALUES \((2|'foo'), (2|'foo')\)/)
|
38
38
|
end
|
39
39
|
|
40
|
-
|
40
|
+
it "should not set defaults for unparseable dataset default values" do
|
41
41
|
@c.instance_variable_set(:@db_schema, {:i=>{:default=>'f(x)'}, :name=>{:ruby_default=>'foo'}, :id=>{:primary_key=>true}})
|
42
42
|
c = Class.new(@c)
|
43
43
|
c.create
|
44
|
-
@db.sqls.first.
|
44
|
+
@db.sqls.first.must_equal "INSERT INTO people (name) VALUES ('foo')"
|
45
45
|
end
|
46
46
|
|
47
|
-
|
47
|
+
it "should save all fields when updating" do
|
48
48
|
@p.update(:i=>3)
|
49
|
-
@db.sqls.first.
|
49
|
+
@db.sqls.first.must_match(/UPDATE people SET (name = 'foo'|i = 3), (name = 'foo'|i = 3) WHERE \(id = 1\)/)
|
50
50
|
end
|
51
51
|
|
52
|
-
|
52
|
+
it "should work with abstract classes" do
|
53
53
|
c = Class.new(Sequel::Model)
|
54
54
|
c.plugin :prepared_statements_safe
|
55
55
|
c1 = Class.new(c)
|
56
56
|
c1.meta_def(:get_db_schema){@db_schema = {:i=>{:default=>'f(x)'}, :name=>{:ruby_default=>'foo'}, :id=>{:primary_key=>true}}}
|
57
57
|
c1.set_dataset(:people)
|
58
|
-
c1.prepared_statements_column_defaults.
|
59
|
-
Class.new(c1).prepared_statements_column_defaults.
|
58
|
+
c1.prepared_statements_column_defaults.must_equal(:name=>'foo')
|
59
|
+
Class.new(c1).prepared_statements_column_defaults.must_equal(:name=>'foo')
|
60
60
|
end
|
61
61
|
end
|
@@ -12,28 +12,28 @@ describe "prepared_statements plugin" do
|
|
12
12
|
@db.sqls
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
@c[1].
|
17
|
-
@db.sqls.
|
15
|
+
it "should correctly lookup by primary key" do
|
16
|
+
@c[1].must_equal @p
|
17
|
+
@db.sqls.must_equal ["SELECT id, name, i FROM people WHERE (id = 1) LIMIT 1 -- read_only"]
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
@p.destroy.
|
23
|
-
@db.sqls.
|
20
|
+
prepared_statements_spec = shared_description do
|
21
|
+
it "should correctly delete instance" do
|
22
|
+
@p.destroy.must_equal @p
|
23
|
+
@db.sqls.must_equal ["DELETE FROM people WHERE (id = 1)"]
|
24
24
|
end
|
25
25
|
|
26
|
-
|
27
|
-
@p.update(:name=>'bar').
|
28
|
-
@db.sqls.
|
26
|
+
it "should correctly update instance" do
|
27
|
+
@p.update(:name=>'bar').must_equal @c.load(:id=>1, :name=>'bar', :i => 2)
|
28
|
+
@db.sqls.must_equal ["UPDATE people SET name = 'bar' WHERE (id = 1)"]
|
29
29
|
end
|
30
30
|
|
31
|
-
|
32
|
-
@c.create(:name=>'foo').
|
33
|
-
@db.sqls.
|
31
|
+
it "should correctly create instance" do
|
32
|
+
@c.create(:name=>'foo').must_equal @c.load(:id=>1, :name=>'foo', :i => 2)
|
33
|
+
@db.sqls.must_equal ["INSERT INTO people (name) VALUES ('foo')", "SELECT #{@columns} FROM people WHERE (id = 1) LIMIT 1"]
|
34
34
|
end
|
35
35
|
|
36
|
-
|
36
|
+
it "should correctly create instance if dataset supports insert_select" do
|
37
37
|
@c.dataset_module do
|
38
38
|
def supports_insert_select?
|
39
39
|
true
|
@@ -49,26 +49,26 @@ describe "prepared_statements plugin" do
|
|
49
49
|
"#{insert_sql(*v)} RETURNING #{(opts[:returning] && !opts[:returning].empty?) ? opts[:returning].map{|c| literal(c)}.join(', ') : '*'}"
|
50
50
|
end
|
51
51
|
end
|
52
|
-
@c.create(:name=>'foo').
|
53
|
-
@db.sqls.
|
52
|
+
@c.create(:name=>'foo').must_equal @c.load(:id=>1, :name=>'foo', :i => 2)
|
53
|
+
@db.sqls.must_equal ["INSERT INTO people (name) VALUES ('foo') RETURNING #{@columns}"]
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
it_should_behave_like "prepared_statements plugin"
|
58
|
-
|
59
57
|
describe "when #use_prepared_statements_for? returns false" do
|
60
58
|
before do
|
61
59
|
@columns = "*"
|
62
60
|
@c.class_eval{def use_prepared_statements_for?(type) false end}
|
63
61
|
end
|
64
62
|
|
65
|
-
|
63
|
+
include prepared_statements_spec
|
66
64
|
end
|
67
65
|
|
68
|
-
|
66
|
+
include prepared_statements_spec
|
67
|
+
|
68
|
+
it "should work correctly when subclassing" do
|
69
69
|
c = Class.new(@c)
|
70
|
-
c[1].
|
71
|
-
@db.sqls.
|
70
|
+
c[1].must_equal c.load(:id=>1, :name=>'foo', :i=>2)
|
71
|
+
@db.sqls.must_equal ["SELECT id, name, i FROM people WHERE (id = 1) LIMIT 1 -- read_only"]
|
72
72
|
end
|
73
73
|
|
74
74
|
describe " with placeholder type specifiers" do
|
@@ -76,12 +76,12 @@ describe "prepared_statements plugin" do
|
|
76
76
|
@ds.meta_def(:requires_placeholder_type_specifiers?){true}
|
77
77
|
end
|
78
78
|
|
79
|
-
|
80
|
-
@c[1].
|
81
|
-
@db.sqls.
|
79
|
+
it "should correctly handle without schema type" do
|
80
|
+
@c[1].must_equal @p
|
81
|
+
@db.sqls.must_equal ["SELECT id, name, i FROM people WHERE (id = 1) LIMIT 1 -- read_only"]
|
82
82
|
end
|
83
83
|
|
84
|
-
|
84
|
+
it "should correctly handle with schema type" do
|
85
85
|
@c.db_schema[:id][:type] = :integer
|
86
86
|
ds = @c.send(:prepared_lookup)
|
87
87
|
def ds.literal_symbol_append(sql, v)
|
@@ -96,8 +96,8 @@ describe "prepared_statements plugin" do
|
|
96
96
|
super
|
97
97
|
end
|
98
98
|
end
|
99
|
-
@c[1].
|
100
|
-
@db.sqls.
|
99
|
+
@c[1].must_equal @p
|
100
|
+
@db.sqls.must_equal ["SELECT id, name, i FROM people WHERE (id = 1) LIMIT 1 -- read_only"]
|
101
101
|
end
|
102
102
|
end
|
103
103
|
end
|