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
data/spec/core/database_spec.rb
CHANGED
|
@@ -10,230 +10,235 @@ describe "A new Database" do
|
|
|
10
10
|
Sequel.identifier_output_method = nil
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
@db.opts[1].
|
|
15
|
-
@db.opts[:logger].
|
|
13
|
+
it "should receive options" do
|
|
14
|
+
@db.opts[1].must_equal 2
|
|
15
|
+
@db.opts[:logger].must_equal 3
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
@db.loggers.
|
|
20
|
-
Sequel::Database.new(1 => 2, :loggers => 3).loggers.
|
|
21
|
-
Sequel::Database.new(1 => 2, :loggers => [3]).loggers.
|
|
22
|
-
Sequel::Database.new(1 => 2, :logger => 4, :loggers => 3).loggers.
|
|
23
|
-
Sequel::Database.new(1 => 2, :logger => [4], :loggers => [3]).loggers.
|
|
18
|
+
it "should set the logger from opts[:logger] and opts[:loggers]" do
|
|
19
|
+
@db.loggers.must_equal [3]
|
|
20
|
+
Sequel::Database.new(1 => 2, :loggers => 3).loggers.must_equal [3]
|
|
21
|
+
Sequel::Database.new(1 => 2, :loggers => [3]).loggers.must_equal [3]
|
|
22
|
+
Sequel::Database.new(1 => 2, :logger => 4, :loggers => 3).loggers.must_equal [4,3]
|
|
23
|
+
Sequel::Database.new(1 => 2, :logger => [4], :loggers => [3]).loggers.must_equal [4,3]
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
@db.pool.size.
|
|
26
|
+
it "should support :preconnect option to preconnect to database" do
|
|
27
|
+
@db.pool.size.must_equal 0
|
|
28
28
|
c = Class.new(Sequel::Database) do
|
|
29
29
|
def connect(_)
|
|
30
30
|
:connect
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
db = c.new(1 => 2, :logger => 3, :preconnect=>true)
|
|
34
|
-
db.pool.size.
|
|
34
|
+
db.pool.size.must_equal db.pool.max_size
|
|
35
35
|
end
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
@db.default_string_column_size.
|
|
37
|
+
it "should handle the default string column size" do
|
|
38
|
+
@db.default_string_column_size.must_equal 255
|
|
39
39
|
db = Sequel::Database.new(:default_string_column_size=>50)
|
|
40
|
-
db.default_string_column_size.
|
|
40
|
+
db.default_string_column_size.must_equal 50
|
|
41
41
|
db.default_string_column_size = 2
|
|
42
|
-
db.default_string_column_size.
|
|
42
|
+
db.default_string_column_size.must_equal 2
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
Sequel::Database.new(1 => 2, :sql_log_level=>:debug).sql_log_level.
|
|
47
|
-
Sequel::Database.new(1 => 2, :sql_log_level=>'debug').sql_log_level.
|
|
45
|
+
it "should set the sql_log_level from opts[:sql_log_level]" do
|
|
46
|
+
Sequel::Database.new(1 => 2, :sql_log_level=>:debug).sql_log_level.must_equal :debug
|
|
47
|
+
Sequel::Database.new(1 => 2, :sql_log_level=>'debug').sql_log_level.must_equal :debug
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
@db.pool.
|
|
52
|
-
@db.pool.max_size.
|
|
50
|
+
it "should create a connection pool" do
|
|
51
|
+
@db.pool.must_be_kind_of(Sequel::ConnectionPool)
|
|
52
|
+
@db.pool.max_size.must_equal 4
|
|
53
53
|
|
|
54
|
-
Sequel::Database.new(:max_connections => 10).pool.max_size.
|
|
54
|
+
Sequel::Database.new(:max_connections => 10).pool.max_size.must_equal 10
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
it "should have the connection pool use the connect method to get connections" do
|
|
58
58
|
cc = nil
|
|
59
59
|
d = Sequel::Database.new
|
|
60
60
|
meta_def(d, :connect){|c| 1234}
|
|
61
61
|
d.synchronize {|c| cc = c}
|
|
62
|
-
cc.
|
|
62
|
+
cc.must_equal 1234
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
it "should respect the :single_threaded option" do
|
|
66
66
|
db = Sequel::Database.new(:single_threaded=>true){123}
|
|
67
|
-
db.pool.
|
|
67
|
+
db.pool.must_be_kind_of(Sequel::SingleConnectionPool)
|
|
68
68
|
db = Sequel::Database.new(:single_threaded=>'t'){123}
|
|
69
|
-
db.pool.
|
|
69
|
+
db.pool.must_be_kind_of(Sequel::SingleConnectionPool)
|
|
70
70
|
db = Sequel::Database.new(:single_threaded=>'1'){123}
|
|
71
|
-
db.pool.
|
|
71
|
+
db.pool.must_be_kind_of(Sequel::SingleConnectionPool)
|
|
72
72
|
db = Sequel::Database.new(:single_threaded=>false){123}
|
|
73
|
-
db.pool.
|
|
73
|
+
db.pool.must_be_kind_of(Sequel::ConnectionPool)
|
|
74
74
|
db = Sequel::Database.new(:single_threaded=>'f'){123}
|
|
75
|
-
db.pool.
|
|
75
|
+
db.pool.must_be_kind_of(Sequel::ConnectionPool)
|
|
76
76
|
db = Sequel::Database.new(:single_threaded=>'0'){123}
|
|
77
|
-
db.pool.
|
|
77
|
+
db.pool.must_be_kind_of(Sequel::ConnectionPool)
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
it "should respect the :quote_identifiers option" do
|
|
81
81
|
db = Sequel::Database.new(:quote_identifiers=>false)
|
|
82
|
-
db.quote_identifiers?.
|
|
82
|
+
db.quote_identifiers?.must_equal false
|
|
83
83
|
db = Sequel::Database.new(:quote_identifiers=>true)
|
|
84
|
-
db.quote_identifiers?.
|
|
84
|
+
db.quote_identifiers?.must_equal true
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
it "should upcase on input and downcase on output by default" do
|
|
88
88
|
db = Sequel::Database.new
|
|
89
|
-
db.send(:identifier_input_method_default).
|
|
90
|
-
db.send(:identifier_output_method_default).
|
|
89
|
+
db.send(:identifier_input_method_default).must_equal :upcase
|
|
90
|
+
db.send(:identifier_output_method_default).must_equal :downcase
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
-
|
|
93
|
+
it "should respect the :identifier_input_method option" do
|
|
94
94
|
Sequel.identifier_input_method = nil
|
|
95
|
-
Sequel::Database.identifier_input_method.
|
|
95
|
+
Sequel::Database.identifier_input_method.must_equal false
|
|
96
96
|
db = Sequel::Database.new(:identifier_input_method=>nil)
|
|
97
|
-
db.identifier_input_method.
|
|
97
|
+
db.identifier_input_method.must_equal nil
|
|
98
98
|
db.identifier_input_method = :downcase
|
|
99
|
-
db.identifier_input_method.
|
|
99
|
+
db.identifier_input_method.must_equal :downcase
|
|
100
100
|
db = Sequel::Database.new(:identifier_input_method=>:upcase)
|
|
101
|
-
db.identifier_input_method.
|
|
101
|
+
db.identifier_input_method.must_equal :upcase
|
|
102
102
|
db.identifier_input_method = nil
|
|
103
|
-
db.identifier_input_method.
|
|
103
|
+
db.identifier_input_method.must_equal nil
|
|
104
104
|
Sequel.identifier_input_method = :downcase
|
|
105
|
-
Sequel::Database.identifier_input_method.
|
|
105
|
+
Sequel::Database.identifier_input_method.must_equal :downcase
|
|
106
106
|
db = Sequel::Database.new(:identifier_input_method=>nil)
|
|
107
|
-
db.identifier_input_method.
|
|
107
|
+
db.identifier_input_method.must_equal nil
|
|
108
108
|
db.identifier_input_method = :upcase
|
|
109
|
-
db.identifier_input_method.
|
|
109
|
+
db.identifier_input_method.must_equal :upcase
|
|
110
110
|
db = Sequel::Database.new(:identifier_input_method=>:upcase)
|
|
111
|
-
db.identifier_input_method.
|
|
111
|
+
db.identifier_input_method.must_equal :upcase
|
|
112
112
|
db.identifier_input_method = nil
|
|
113
|
-
db.identifier_input_method.
|
|
113
|
+
db.identifier_input_method.must_equal nil
|
|
114
114
|
end
|
|
115
115
|
|
|
116
|
-
|
|
116
|
+
it "should respect the :identifier_output_method option" do
|
|
117
117
|
Sequel.identifier_output_method = nil
|
|
118
|
-
Sequel::Database.identifier_output_method.
|
|
118
|
+
Sequel::Database.identifier_output_method.must_equal false
|
|
119
119
|
db = Sequel::Database.new(:identifier_output_method=>nil)
|
|
120
|
-
db.identifier_output_method.
|
|
120
|
+
db.identifier_output_method.must_equal nil
|
|
121
121
|
db.identifier_output_method = :downcase
|
|
122
|
-
db.identifier_output_method.
|
|
122
|
+
db.identifier_output_method.must_equal :downcase
|
|
123
123
|
db = Sequel::Database.new(:identifier_output_method=>:upcase)
|
|
124
|
-
db.identifier_output_method.
|
|
124
|
+
db.identifier_output_method.must_equal :upcase
|
|
125
125
|
db.identifier_output_method = nil
|
|
126
|
-
db.identifier_output_method.
|
|
126
|
+
db.identifier_output_method.must_equal nil
|
|
127
127
|
Sequel.identifier_output_method = :downcase
|
|
128
|
-
Sequel::Database.identifier_output_method.
|
|
128
|
+
Sequel::Database.identifier_output_method.must_equal :downcase
|
|
129
129
|
db = Sequel::Database.new(:identifier_output_method=>nil)
|
|
130
|
-
db.identifier_output_method.
|
|
130
|
+
db.identifier_output_method.must_equal nil
|
|
131
131
|
db.identifier_output_method = :upcase
|
|
132
|
-
db.identifier_output_method.
|
|
132
|
+
db.identifier_output_method.must_equal :upcase
|
|
133
133
|
db = Sequel::Database.new(:identifier_output_method=>:upcase)
|
|
134
|
-
db.identifier_output_method.
|
|
134
|
+
db.identifier_output_method.must_equal :upcase
|
|
135
135
|
db.identifier_output_method = nil
|
|
136
|
-
db.identifier_output_method.
|
|
136
|
+
db.identifier_output_method.must_equal nil
|
|
137
137
|
end
|
|
138
138
|
|
|
139
|
-
|
|
139
|
+
it "should use the default Sequel.quote_identifiers value" do
|
|
140
140
|
Sequel.quote_identifiers = true
|
|
141
|
-
Sequel::Database.new({}).quote_identifiers?.
|
|
141
|
+
Sequel::Database.new({}).quote_identifiers?.must_equal true
|
|
142
142
|
Sequel.quote_identifiers = false
|
|
143
|
-
Sequel::Database.new({}).quote_identifiers?.
|
|
143
|
+
Sequel::Database.new({}).quote_identifiers?.must_equal false
|
|
144
144
|
Sequel::Database.quote_identifiers = true
|
|
145
|
-
Sequel::Database.new({}).quote_identifiers?.
|
|
145
|
+
Sequel::Database.new({}).quote_identifiers?.must_equal true
|
|
146
146
|
Sequel::Database.quote_identifiers = false
|
|
147
|
-
Sequel::Database.new({}).quote_identifiers?.
|
|
147
|
+
Sequel::Database.new({}).quote_identifiers?.must_equal false
|
|
148
148
|
end
|
|
149
149
|
|
|
150
|
-
|
|
150
|
+
it "should use the default Sequel.identifier_input_method value" do
|
|
151
151
|
Sequel.identifier_input_method = :downcase
|
|
152
|
-
Sequel::Database.new({}).identifier_input_method.
|
|
152
|
+
Sequel::Database.new({}).identifier_input_method.must_equal :downcase
|
|
153
153
|
Sequel.identifier_input_method = :upcase
|
|
154
|
-
Sequel::Database.new({}).identifier_input_method.
|
|
154
|
+
Sequel::Database.new({}).identifier_input_method.must_equal :upcase
|
|
155
155
|
Sequel::Database.identifier_input_method = :downcase
|
|
156
|
-
Sequel::Database.new({}).identifier_input_method.
|
|
156
|
+
Sequel::Database.new({}).identifier_input_method.must_equal :downcase
|
|
157
157
|
Sequel::Database.identifier_input_method = :upcase
|
|
158
|
-
Sequel::Database.new({}).identifier_input_method.
|
|
158
|
+
Sequel::Database.new({}).identifier_input_method.must_equal :upcase
|
|
159
159
|
end
|
|
160
160
|
|
|
161
|
-
|
|
161
|
+
it "should use the default Sequel.identifier_output_method value" do
|
|
162
162
|
Sequel.identifier_output_method = :downcase
|
|
163
|
-
Sequel::Database.new({}).identifier_output_method.
|
|
163
|
+
Sequel::Database.new({}).identifier_output_method.must_equal :downcase
|
|
164
164
|
Sequel.identifier_output_method = :upcase
|
|
165
|
-
Sequel::Database.new({}).identifier_output_method.
|
|
165
|
+
Sequel::Database.new({}).identifier_output_method.must_equal :upcase
|
|
166
166
|
Sequel::Database.identifier_output_method = :downcase
|
|
167
|
-
Sequel::Database.new({}).identifier_output_method.
|
|
167
|
+
Sequel::Database.new({}).identifier_output_method.must_equal :downcase
|
|
168
168
|
Sequel::Database.identifier_output_method = :upcase
|
|
169
|
-
Sequel::Database.new({}).identifier_output_method.
|
|
169
|
+
Sequel::Database.new({}).identifier_output_method.must_equal :upcase
|
|
170
170
|
end
|
|
171
171
|
|
|
172
|
-
|
|
172
|
+
it "should respect the quote_indentifiers_default method if Sequel.quote_identifiers = nil" do
|
|
173
173
|
Sequel.quote_identifiers = nil
|
|
174
|
-
Sequel::Database.new({}).quote_identifiers?.
|
|
174
|
+
Sequel::Database.new({}).quote_identifiers?.must_equal true
|
|
175
175
|
x = Class.new(Sequel::Database){def quote_identifiers_default; false end}
|
|
176
|
-
x.new({}).quote_identifiers?.
|
|
176
|
+
x.new({}).quote_identifiers?.must_equal false
|
|
177
177
|
y = Class.new(Sequel::Database){def quote_identifiers_default; true end}
|
|
178
|
-
y.new({}).quote_identifiers?.
|
|
178
|
+
y.new({}).quote_identifiers?.must_equal true
|
|
179
179
|
end
|
|
180
180
|
|
|
181
|
-
|
|
181
|
+
it "should respect the identifier_input_method_default method" do
|
|
182
182
|
class Sequel::Database
|
|
183
183
|
@identifier_input_method = nil
|
|
184
184
|
end
|
|
185
185
|
x = Class.new(Sequel::Database){def identifier_input_method_default; :downcase end}
|
|
186
|
-
x.new({}).identifier_input_method.
|
|
186
|
+
x.new({}).identifier_input_method.must_equal :downcase
|
|
187
187
|
y = Class.new(Sequel::Database){def identifier_input_method_default; :camelize end}
|
|
188
|
-
y.new({}).identifier_input_method.
|
|
188
|
+
y.new({}).identifier_input_method.must_equal :camelize
|
|
189
189
|
end
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
it "should respect the identifier_output_method_default method if Sequel.identifier_output_method is not called" do
|
|
192
192
|
class Sequel::Database
|
|
193
193
|
@identifier_output_method = nil
|
|
194
194
|
end
|
|
195
195
|
x = Class.new(Sequel::Database){def identifier_output_method_default; :upcase end}
|
|
196
|
-
x.new({}).identifier_output_method.
|
|
196
|
+
x.new({}).identifier_output_method.must_equal :upcase
|
|
197
197
|
y = Class.new(Sequel::Database){def identifier_output_method_default; :underscore end}
|
|
198
|
-
y.new({}).identifier_output_method.
|
|
198
|
+
y.new({}).identifier_output_method.must_equal :underscore
|
|
199
199
|
end
|
|
200
200
|
|
|
201
|
-
|
|
202
|
-
Sequel::Database.
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
db.
|
|
201
|
+
it "should just use a :uri option for jdbc with the full connection string" do
|
|
202
|
+
db = Sequel::Database.stub(:adapter_class, Sequel::Database) do
|
|
203
|
+
Sequel.connect('jdbc:test://host/db_name')
|
|
204
|
+
end
|
|
205
|
+
db.must_be_kind_of(Sequel::Database)
|
|
206
|
+
db.opts[:uri].must_equal 'jdbc:test://host/db_name'
|
|
206
207
|
end
|
|
207
208
|
|
|
208
|
-
|
|
209
|
-
Sequel::Database.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
db.
|
|
209
|
+
it "should just use a :uri option for do with the full connection string" do
|
|
210
|
+
db = Sequel::Database.stub(:adapter_class, Sequel::Database) do
|
|
211
|
+
Sequel.connect('do:test://host/db_name')
|
|
212
|
+
end
|
|
213
|
+
db.must_be_kind_of(Sequel::Database)
|
|
214
|
+
db.opts[:uri].must_equal 'do:test://host/db_name'
|
|
213
215
|
end
|
|
214
216
|
|
|
215
|
-
|
|
216
|
-
Sequel.connect('mock:/').opts[:adapter].
|
|
217
|
+
it "should populate :adapter option when using connection string" do
|
|
218
|
+
Sequel.connect('mock:/').opts[:adapter].must_equal "mock"
|
|
217
219
|
end
|
|
218
220
|
|
|
219
|
-
|
|
221
|
+
it "should respect the :keep_reference option for not keeping a reference in Sequel::DATABASES" do
|
|
220
222
|
db = Sequel.connect('mock:///?keep_reference=f')
|
|
221
|
-
Sequel::DATABASES.
|
|
223
|
+
Sequel::DATABASES.wont_include(db)
|
|
222
224
|
end
|
|
223
225
|
end
|
|
224
226
|
|
|
225
227
|
describe "Database#disconnect" do
|
|
226
|
-
|
|
228
|
+
it "should call pool.disconnect" do
|
|
227
229
|
d = Sequel::Database.new
|
|
228
230
|
p = d.pool
|
|
229
|
-
p.
|
|
230
|
-
|
|
231
|
+
def p.disconnect(h)
|
|
232
|
+
raise unless h == {}
|
|
233
|
+
2
|
|
234
|
+
end
|
|
235
|
+
d.disconnect.must_equal 2
|
|
231
236
|
end
|
|
232
237
|
end
|
|
233
238
|
|
|
234
239
|
describe "Sequel.extension" do
|
|
235
|
-
|
|
236
|
-
proc{Sequel.extension :blah}.
|
|
240
|
+
it "should attempt to load the given extension" do
|
|
241
|
+
proc{Sequel.extension :blah}.must_raise(LoadError)
|
|
237
242
|
end
|
|
238
243
|
end
|
|
239
244
|
|
|
@@ -246,14 +251,14 @@ describe "Database#log_info" do
|
|
|
246
251
|
@db = Sequel::Database.new(:logger=>@o)
|
|
247
252
|
end
|
|
248
253
|
|
|
249
|
-
|
|
254
|
+
it "should log message at info level to all loggers" do
|
|
250
255
|
@db.log_info('blah')
|
|
251
|
-
@o.logs.
|
|
256
|
+
@o.logs.must_equal [[:info, 'blah']]
|
|
252
257
|
end
|
|
253
258
|
|
|
254
|
-
|
|
259
|
+
it "should log message with args at info level to all loggers" do
|
|
255
260
|
@db.log_info('blah', [1, 2])
|
|
256
|
-
@o.logs.
|
|
261
|
+
@o.logs.must_equal [[:info, 'blah; [1, 2]']]
|
|
257
262
|
end
|
|
258
263
|
end
|
|
259
264
|
|
|
@@ -267,66 +272,66 @@ describe "Database#log_yield" do
|
|
|
267
272
|
@db = Sequel::Database.new(:logger=>@o)
|
|
268
273
|
end
|
|
269
274
|
|
|
270
|
-
|
|
275
|
+
it "should yield to the passed block" do
|
|
271
276
|
a = nil
|
|
272
277
|
@db.log_yield('blah'){a = 1}
|
|
273
|
-
a.
|
|
278
|
+
a.must_equal 1
|
|
274
279
|
end
|
|
275
280
|
|
|
276
|
-
|
|
277
|
-
proc{@db.log_yield('blah')}.
|
|
281
|
+
it "should raise an exception if a block is not passed" do
|
|
282
|
+
proc{@db.log_yield('blah')}.must_raise LocalJumpError
|
|
278
283
|
end
|
|
279
284
|
|
|
280
|
-
|
|
285
|
+
it "should log message with duration at info level to all loggers" do
|
|
281
286
|
@db.log_yield('blah'){}
|
|
282
|
-
@o.logs.length.
|
|
283
|
-
@o.logs.first.length.
|
|
284
|
-
@o.logs.first.first.
|
|
285
|
-
@o.logs.first.last.
|
|
287
|
+
@o.logs.length.must_equal 1
|
|
288
|
+
@o.logs.first.length.must_equal 2
|
|
289
|
+
@o.logs.first.first.must_equal :info
|
|
290
|
+
@o.logs.first.last.must_match(/\A\(\d\.\d{6}s\) blah\z/)
|
|
286
291
|
end
|
|
287
292
|
|
|
288
|
-
|
|
293
|
+
it "should respect sql_log_level setting" do
|
|
289
294
|
@db.sql_log_level = :debug
|
|
290
295
|
@db.log_yield('blah'){}
|
|
291
|
-
@o.logs.length.
|
|
292
|
-
@o.logs.first.length.
|
|
293
|
-
@o.logs.first.first.
|
|
294
|
-
@o.logs.first.last.
|
|
296
|
+
@o.logs.length.must_equal 1
|
|
297
|
+
@o.logs.first.length.must_equal 2
|
|
298
|
+
@o.logs.first.first.must_equal :debug
|
|
299
|
+
@o.logs.first.last.must_match(/\A\(\d\.\d{6}s\) blah\z/)
|
|
295
300
|
end
|
|
296
301
|
|
|
297
|
-
|
|
302
|
+
it "should log message with duration at warn level if duration greater than log_warn_duration" do
|
|
298
303
|
@db.log_warn_duration = 0
|
|
299
304
|
@db.log_yield('blah'){}
|
|
300
|
-
@o.logs.length.
|
|
301
|
-
@o.logs.first.length.
|
|
302
|
-
@o.logs.first.first.
|
|
303
|
-
@o.logs.first.last.
|
|
305
|
+
@o.logs.length.must_equal 1
|
|
306
|
+
@o.logs.first.length.must_equal 2
|
|
307
|
+
@o.logs.first.first.must_equal :warn
|
|
308
|
+
@o.logs.first.last.must_match(/\A\(\d\.\d{6}s\) blah\z/)
|
|
304
309
|
end
|
|
305
310
|
|
|
306
|
-
|
|
311
|
+
it "should log message with duration at info level if duration less than log_warn_duration" do
|
|
307
312
|
@db.log_warn_duration = 1000
|
|
308
313
|
@db.log_yield('blah'){}
|
|
309
|
-
@o.logs.length.
|
|
310
|
-
@o.logs.first.length.
|
|
311
|
-
@o.logs.first.first.
|
|
312
|
-
@o.logs.first.last.
|
|
314
|
+
@o.logs.length.must_equal 1
|
|
315
|
+
@o.logs.first.length.must_equal 2
|
|
316
|
+
@o.logs.first.first.must_equal :info
|
|
317
|
+
@o.logs.first.last.must_match(/\A\(\d\.\d{6}s\) blah\z/)
|
|
313
318
|
end
|
|
314
319
|
|
|
315
|
-
|
|
320
|
+
it "should log message at error level if block raises an error" do
|
|
316
321
|
@db.log_warn_duration = 0
|
|
317
|
-
proc{@db.log_yield('blah'){raise Sequel::Error, 'adsf'}}.
|
|
318
|
-
@o.logs.length.
|
|
319
|
-
@o.logs.first.length.
|
|
320
|
-
@o.logs.first.first.
|
|
321
|
-
@o.logs.first.last.
|
|
322
|
+
proc{@db.log_yield('blah'){raise Sequel::Error, 'adsf'}}.must_raise Sequel::Error
|
|
323
|
+
@o.logs.length.must_equal 1
|
|
324
|
+
@o.logs.first.length.must_equal 2
|
|
325
|
+
@o.logs.first.first.must_equal :error
|
|
326
|
+
@o.logs.first.last.must_match(/\ASequel::Error: adsf: blah\z/)
|
|
322
327
|
end
|
|
323
328
|
|
|
324
|
-
|
|
329
|
+
it "should include args with message if args passed" do
|
|
325
330
|
@db.log_yield('blah', [1, 2]){}
|
|
326
|
-
@o.logs.length.
|
|
327
|
-
@o.logs.first.length.
|
|
328
|
-
@o.logs.first.first.
|
|
329
|
-
@o.logs.first.last.
|
|
331
|
+
@o.logs.length.must_equal 1
|
|
332
|
+
@o.logs.first.length.must_equal 2
|
|
333
|
+
@o.logs.first.first.must_equal :info
|
|
334
|
+
@o.logs.first.last.must_match(/\A\(\d\.\d{6}s\) blah; \[1, 2\]\z/)
|
|
330
335
|
end
|
|
331
336
|
end
|
|
332
337
|
|
|
@@ -339,29 +344,29 @@ describe "Database#uri" do
|
|
|
339
344
|
@db = Sequel.connect('mau://user:pass@localhost:9876/maumau')
|
|
340
345
|
end
|
|
341
346
|
|
|
342
|
-
|
|
343
|
-
@db.uri.
|
|
347
|
+
it "should return the connection URI for the database" do
|
|
348
|
+
@db.uri.must_equal 'mau://user:pass@localhost:9876/maumau'
|
|
344
349
|
end
|
|
345
350
|
|
|
346
|
-
|
|
347
|
-
Sequel.mock.uri.
|
|
351
|
+
it "should return nil if a connection uri was not used" do
|
|
352
|
+
Sequel.mock.uri.must_equal nil
|
|
348
353
|
end
|
|
349
354
|
|
|
350
|
-
|
|
351
|
-
@db.url.
|
|
355
|
+
it "should be aliased as #url" do
|
|
356
|
+
@db.url.must_equal 'mau://user:pass@localhost:9876/maumau'
|
|
352
357
|
end
|
|
353
358
|
end
|
|
354
359
|
|
|
355
360
|
describe "Database.adapter_scheme and #adapter_scheme" do
|
|
356
|
-
|
|
357
|
-
Sequel::Database.adapter_scheme.
|
|
361
|
+
it "should return the database scheme" do
|
|
362
|
+
Sequel::Database.adapter_scheme.must_equal nil
|
|
358
363
|
|
|
359
364
|
@c = Class.new(Sequel::Database) do
|
|
360
365
|
set_adapter_scheme :mau
|
|
361
366
|
end
|
|
362
367
|
|
|
363
|
-
@c.adapter_scheme.
|
|
364
|
-
@c.new({}).adapter_scheme.
|
|
368
|
+
@c.adapter_scheme.must_equal :mau
|
|
369
|
+
@c.new({}).adapter_scheme.must_equal :mau
|
|
365
370
|
end
|
|
366
371
|
end
|
|
367
372
|
|
|
@@ -371,38 +376,38 @@ describe "Database#dataset" do
|
|
|
371
376
|
@ds = @db.dataset
|
|
372
377
|
end
|
|
373
378
|
|
|
374
|
-
|
|
375
|
-
@ds.
|
|
376
|
-
@ds.opts.
|
|
377
|
-
@ds.db.
|
|
379
|
+
it "should provide a blank dataset through #dataset" do
|
|
380
|
+
@ds.must_be_kind_of(Sequel::Dataset)
|
|
381
|
+
@ds.opts.must_equal({})
|
|
382
|
+
@ds.db.must_be_same_as(@db)
|
|
378
383
|
end
|
|
379
384
|
|
|
380
|
-
|
|
385
|
+
it "should provide a #from dataset" do
|
|
381
386
|
d = @db.from(:mau)
|
|
382
|
-
d.
|
|
383
|
-
d.sql.
|
|
387
|
+
d.must_be_kind_of(Sequel::Dataset)
|
|
388
|
+
d.sql.must_equal 'SELECT * FROM mau'
|
|
384
389
|
|
|
385
390
|
e = @db[:miu]
|
|
386
|
-
e.
|
|
387
|
-
e.sql.
|
|
391
|
+
e.must_be_kind_of(Sequel::Dataset)
|
|
392
|
+
e.sql.must_equal 'SELECT * FROM miu'
|
|
388
393
|
end
|
|
389
394
|
|
|
390
|
-
|
|
395
|
+
it "should provide a filtered #from dataset if a block is given" do
|
|
391
396
|
d = @db.from(:mau){x.sql_number > 100}
|
|
392
|
-
d.
|
|
393
|
-
d.sql.
|
|
397
|
+
d.must_be_kind_of(Sequel::Dataset)
|
|
398
|
+
d.sql.must_equal 'SELECT * FROM mau WHERE (x > 100)'
|
|
394
399
|
end
|
|
395
400
|
|
|
396
|
-
|
|
401
|
+
it "should provide a #select dataset" do
|
|
397
402
|
d = @db.select(:a, :b, :c).from(:mau)
|
|
398
|
-
d.
|
|
399
|
-
d.sql.
|
|
403
|
+
d.must_be_kind_of(Sequel::Dataset)
|
|
404
|
+
d.sql.must_equal 'SELECT a, b, c FROM mau'
|
|
400
405
|
end
|
|
401
406
|
|
|
402
|
-
|
|
407
|
+
it "should allow #select to take a block" do
|
|
403
408
|
d = @db.select(:a, :b){c}.from(:mau)
|
|
404
|
-
d.
|
|
405
|
-
d.sql.
|
|
409
|
+
d.must_be_kind_of(Sequel::Dataset)
|
|
410
|
+
d.sql.must_equal 'SELECT a, b, c FROM mau'
|
|
406
411
|
end
|
|
407
412
|
end
|
|
408
413
|
|
|
@@ -412,18 +417,18 @@ describe "Database#dataset_class" do
|
|
|
412
417
|
@dsc = Class.new(Sequel::Dataset)
|
|
413
418
|
end
|
|
414
419
|
|
|
415
|
-
|
|
420
|
+
it "should have setter set the class to use to create datasets" do
|
|
416
421
|
@db.dataset_class = @dsc
|
|
417
422
|
ds = @db.dataset
|
|
418
|
-
ds.
|
|
419
|
-
ds.opts.
|
|
420
|
-
ds.db.
|
|
423
|
+
ds.must_be_kind_of(@dsc)
|
|
424
|
+
ds.opts.must_equal({})
|
|
425
|
+
ds.db.must_be_same_as(@db)
|
|
421
426
|
end
|
|
422
427
|
|
|
423
|
-
|
|
424
|
-
[@db.dataset_class, @db.dataset_class.superclass].
|
|
428
|
+
it "should have getter return the class to use to create datasets" do
|
|
429
|
+
[@db.dataset_class, @db.dataset_class.superclass].must_include(Sequel::Dataset)
|
|
425
430
|
@db.dataset_class = @dsc
|
|
426
|
-
[@db.dataset_class, @db.dataset_class.superclass].
|
|
431
|
+
[@db.dataset_class, @db.dataset_class.superclass].must_include(@dsc)
|
|
427
432
|
end
|
|
428
433
|
end
|
|
429
434
|
|
|
@@ -435,77 +440,77 @@ describe "Database#extend_datasets" do
|
|
|
435
440
|
@db.extend_datasets(@m)
|
|
436
441
|
end
|
|
437
442
|
|
|
438
|
-
|
|
443
|
+
it "should clear a cached dataset" do
|
|
439
444
|
@db = Sequel::Database.new
|
|
440
|
-
@db.literal(1).
|
|
445
|
+
@db.literal(1).must_equal '1'
|
|
441
446
|
@db.extend_datasets{def literal(v) '2' end}
|
|
442
|
-
@db.literal(1).
|
|
447
|
+
@db.literal(1).must_equal '2'
|
|
443
448
|
end
|
|
444
449
|
|
|
445
|
-
|
|
446
|
-
@db.dataset_class.superclass.
|
|
450
|
+
it "should change the dataset class to a subclass the first time it is called" do
|
|
451
|
+
@db.dataset_class.superclass.must_equal Sequel::Dataset
|
|
447
452
|
end
|
|
448
453
|
|
|
449
|
-
|
|
454
|
+
it "should not create a subclass of the dataset class if called more than once" do
|
|
450
455
|
@db.extend_datasets(@m2)
|
|
451
|
-
@db.dataset_class.superclass.
|
|
456
|
+
@db.dataset_class.superclass.must_equal Sequel::Dataset
|
|
452
457
|
end
|
|
453
458
|
|
|
454
|
-
|
|
455
|
-
@db.dataset_class.ancestors.
|
|
456
|
-
@db.dataset_class.ancestors.
|
|
459
|
+
it "should make the dataset class include the module" do
|
|
460
|
+
@db.dataset_class.ancestors.must_include(@m)
|
|
461
|
+
@db.dataset_class.ancestors.wont_include(@m2)
|
|
457
462
|
@db.extend_datasets(@m2)
|
|
458
|
-
@db.dataset_class.ancestors.
|
|
459
|
-
@db.dataset_class.ancestors.
|
|
463
|
+
@db.dataset_class.ancestors.must_include(@m)
|
|
464
|
+
@db.dataset_class.ancestors.must_include(@m2)
|
|
460
465
|
end
|
|
461
466
|
|
|
462
|
-
|
|
463
|
-
@db.dataset.foo.
|
|
467
|
+
it "should have datasets respond to the module's methods" do
|
|
468
|
+
@db.dataset.foo.must_equal [3]
|
|
464
469
|
@db.extend_datasets(@m2)
|
|
465
|
-
@db.dataset.foo.
|
|
470
|
+
@db.dataset.foo.must_equal [4, 3]
|
|
466
471
|
end
|
|
467
472
|
|
|
468
|
-
|
|
469
|
-
@db.dataset.foo.
|
|
473
|
+
it "should take a block and create a module from it to use" do
|
|
474
|
+
@db.dataset.foo.must_equal [3]
|
|
470
475
|
@db.extend_datasets{def foo() [5] + super end}
|
|
471
|
-
@db.dataset.foo.
|
|
476
|
+
@db.dataset.foo.must_equal [5, 3]
|
|
472
477
|
end
|
|
473
478
|
|
|
474
|
-
|
|
475
|
-
proc{@db.extend_datasets(@m2){def foo() [5] + super end}}.
|
|
479
|
+
it "should raise an error if both a module and a block are provided" do
|
|
480
|
+
proc{@db.extend_datasets(@m2){def foo() [5] + super end}}.must_raise(Sequel::Error)
|
|
476
481
|
end
|
|
477
482
|
|
|
478
|
-
|
|
483
|
+
it "should be able to override methods defined in the original Dataset class" do
|
|
479
484
|
@db.extend_datasets(Module.new{def select(*a, &block) super.order(*a, &block) end})
|
|
480
|
-
@db[:t].select(:a, :b).sql.
|
|
485
|
+
@db[:t].select(:a, :b).sql.must_equal 'SELECT a, b FROM t ORDER BY a, b'
|
|
481
486
|
end
|
|
482
487
|
|
|
483
|
-
|
|
488
|
+
it "should reapply settings if dataset_class is changed" do
|
|
484
489
|
c = Class.new(Sequel::Dataset)
|
|
485
490
|
@db.dataset_class = c
|
|
486
|
-
@db.dataset_class.superclass.
|
|
487
|
-
@db.dataset_class.ancestors.
|
|
488
|
-
@db.dataset.foo.
|
|
491
|
+
@db.dataset_class.superclass.must_equal c
|
|
492
|
+
@db.dataset_class.ancestors.must_include(@m)
|
|
493
|
+
@db.dataset.foo.must_equal [3]
|
|
489
494
|
end
|
|
490
495
|
end
|
|
491
496
|
|
|
492
497
|
describe "Database#disconnect_connection" do
|
|
493
|
-
|
|
498
|
+
it "should call close on the connection" do
|
|
494
499
|
o = Object.new
|
|
495
500
|
def o.close() @closed=true end
|
|
496
501
|
Sequel::Database.new.disconnect_connection(o)
|
|
497
|
-
o.instance_variable_get(:@closed).
|
|
502
|
+
o.instance_variable_get(:@closed).must_equal true
|
|
498
503
|
end
|
|
499
504
|
end
|
|
500
505
|
|
|
501
506
|
describe "Database#valid_connection?" do
|
|
502
|
-
|
|
507
|
+
it "should issue a query to validate the connection" do
|
|
503
508
|
db = Sequel.mock
|
|
504
|
-
db.synchronize{|c| db.valid_connection?(c)}.
|
|
509
|
+
db.synchronize{|c| db.valid_connection?(c)}.must_equal true
|
|
505
510
|
db.synchronize do |c|
|
|
506
511
|
def c.execute(*) raise Sequel::DatabaseError, "error" end
|
|
507
512
|
db.valid_connection?(c)
|
|
508
|
-
end.
|
|
513
|
+
end.must_equal false
|
|
509
514
|
end
|
|
510
515
|
end
|
|
511
516
|
|
|
@@ -514,23 +519,23 @@ describe "Database#run" do
|
|
|
514
519
|
@db = Sequel.mock(:servers=>{:s1=>{}})
|
|
515
520
|
end
|
|
516
521
|
|
|
517
|
-
|
|
522
|
+
it "should execute the code on the database" do
|
|
518
523
|
@db.run("DELETE FROM items")
|
|
519
|
-
@db.sqls.
|
|
524
|
+
@db.sqls.must_equal ["DELETE FROM items"]
|
|
520
525
|
end
|
|
521
526
|
|
|
522
|
-
|
|
527
|
+
it "should handle placeholder literal strings" do
|
|
523
528
|
@db.run(Sequel.lit("DELETE FROM ?", :items))
|
|
524
|
-
@db.sqls.
|
|
529
|
+
@db.sqls.must_equal ["DELETE FROM items"]
|
|
525
530
|
end
|
|
526
531
|
|
|
527
|
-
|
|
528
|
-
@db.run("DELETE FROM items").
|
|
532
|
+
it "should return nil" do
|
|
533
|
+
@db.run("DELETE FROM items").must_equal nil
|
|
529
534
|
end
|
|
530
535
|
|
|
531
|
-
|
|
536
|
+
it "should accept options passed to execute_ddl" do
|
|
532
537
|
@db.run("DELETE FROM items", :server=>:s1)
|
|
533
|
-
@db.sqls.
|
|
538
|
+
@db.sqls.must_equal ["DELETE FROM items -- s1"]
|
|
534
539
|
end
|
|
535
540
|
end
|
|
536
541
|
|
|
@@ -539,19 +544,19 @@ describe "Database#<<" do
|
|
|
539
544
|
@db = Sequel.mock
|
|
540
545
|
end
|
|
541
546
|
|
|
542
|
-
|
|
547
|
+
it "should execute the code on the database" do
|
|
543
548
|
@db << "DELETE FROM items"
|
|
544
|
-
@db.sqls.
|
|
549
|
+
@db.sqls.must_equal ["DELETE FROM items"]
|
|
545
550
|
end
|
|
546
551
|
|
|
547
|
-
|
|
552
|
+
it "should handle placeholder literal strings" do
|
|
548
553
|
@db << Sequel.lit("DELETE FROM ?", :items)
|
|
549
|
-
@db.sqls.
|
|
554
|
+
@db.sqls.must_equal ["DELETE FROM items"]
|
|
550
555
|
end
|
|
551
556
|
|
|
552
|
-
|
|
557
|
+
it "should be chainable" do
|
|
553
558
|
@db << "DELETE FROM items" << "DELETE FROM items2"
|
|
554
|
-
@db.sqls.
|
|
559
|
+
@db.sqls.must_equal ["DELETE FROM items", "DELETE FROM items2"]
|
|
555
560
|
end
|
|
556
561
|
end
|
|
557
562
|
|
|
@@ -561,19 +566,19 @@ describe "Database#synchronize" do
|
|
|
561
566
|
meta_def(@db, :connect){|c| 12345}
|
|
562
567
|
end
|
|
563
568
|
|
|
564
|
-
|
|
569
|
+
it "should wrap the supplied block in pool.hold" do
|
|
565
570
|
q, q1, q2 = Queue.new, Queue.new, Queue.new
|
|
566
571
|
c1, c2 = nil
|
|
567
572
|
t1 = Thread.new{@db.synchronize{|c| c1 = c; q.push nil; q1.pop}; q.push nil}
|
|
568
573
|
q.pop
|
|
569
|
-
c1.
|
|
574
|
+
c1.must_equal 12345
|
|
570
575
|
t2 = Thread.new{@db.synchronize{|c| c2 = c; q2.push nil}}
|
|
571
|
-
@db.pool.available_connections.
|
|
572
|
-
c2.
|
|
576
|
+
@db.pool.available_connections.must_be :empty?
|
|
577
|
+
c2.must_equal nil
|
|
573
578
|
q1.push nil
|
|
574
579
|
q.pop
|
|
575
580
|
q2.pop
|
|
576
|
-
c2.
|
|
581
|
+
c2.must_equal 12345
|
|
577
582
|
t1.join
|
|
578
583
|
t2.join
|
|
579
584
|
end
|
|
@@ -586,65 +591,65 @@ describe "Database#test_connection" do
|
|
|
586
591
|
meta_def(@db, :connect){|c| pr.call}
|
|
587
592
|
end
|
|
588
593
|
|
|
589
|
-
|
|
594
|
+
it "should attempt to get a connection" do
|
|
590
595
|
@db.test_connection
|
|
591
|
-
@test.
|
|
596
|
+
@test.wont_equal nil
|
|
592
597
|
end
|
|
593
598
|
|
|
594
|
-
|
|
595
|
-
@db.test_connection.
|
|
599
|
+
it "should return true if successful" do
|
|
600
|
+
@db.test_connection.must_equal true
|
|
596
601
|
end
|
|
597
602
|
|
|
598
|
-
|
|
603
|
+
it "should raise an error if the attempting to connect raises an error" do
|
|
599
604
|
def @db.connect(*) raise Sequel::Error end
|
|
600
|
-
proc{@db.test_connection}.
|
|
605
|
+
proc{@db.test_connection}.must_raise(Sequel::DatabaseConnectionError)
|
|
601
606
|
end
|
|
602
607
|
end
|
|
603
608
|
|
|
604
609
|
describe "Database#table_exists?" do
|
|
605
|
-
|
|
610
|
+
it "should test existence by selecting a row from the table's dataset" do
|
|
606
611
|
db = Sequel.mock(:fetch=>[Sequel::Error, [], [{:a=>1}]])
|
|
607
|
-
db.table_exists?(:a).
|
|
608
|
-
db.sqls.
|
|
609
|
-
db.table_exists?(:b).
|
|
610
|
-
db.table_exists?(:c).
|
|
612
|
+
db.table_exists?(:a).must_equal false
|
|
613
|
+
db.sqls.must_equal ["SELECT NULL AS nil FROM a LIMIT 1"]
|
|
614
|
+
db.table_exists?(:b).must_equal true
|
|
615
|
+
db.table_exists?(:c).must_equal true
|
|
611
616
|
end
|
|
612
617
|
end
|
|
613
618
|
|
|
614
|
-
|
|
615
|
-
|
|
619
|
+
DatabaseTransactionSpecs = shared_description do
|
|
620
|
+
it "should wrap the supplied block with BEGIN + COMMIT statements" do
|
|
616
621
|
@db.transaction{@db.execute 'DROP TABLE test;'}
|
|
617
|
-
@db.sqls.
|
|
622
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test;', 'COMMIT']
|
|
618
623
|
end
|
|
619
624
|
|
|
620
|
-
|
|
625
|
+
it "should support transaction isolation levels" do
|
|
621
626
|
meta_def(@db, :supports_transaction_isolation_levels?){true}
|
|
622
627
|
[:uncommitted, :committed, :repeatable, :serializable].each do |l|
|
|
623
628
|
@db.transaction(:isolation=>l){@db.run "DROP TABLE #{l}"}
|
|
624
629
|
end
|
|
625
|
-
@db.sqls.
|
|
630
|
+
@db.sqls.must_equal ['BEGIN', 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED', 'DROP TABLE uncommitted', 'COMMIT',
|
|
626
631
|
'BEGIN', 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED', 'DROP TABLE committed', 'COMMIT',
|
|
627
632
|
'BEGIN', 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ', 'DROP TABLE repeatable', 'COMMIT',
|
|
628
633
|
'BEGIN', 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE', 'DROP TABLE serializable', 'COMMIT']
|
|
629
634
|
end
|
|
630
635
|
|
|
631
|
-
|
|
636
|
+
it "should allow specifying a default transaction isolation level" do
|
|
632
637
|
meta_def(@db, :supports_transaction_isolation_levels?){true}
|
|
633
638
|
[:uncommitted, :committed, :repeatable, :serializable].each do |l|
|
|
634
639
|
@db.transaction_isolation_level = l
|
|
635
640
|
@db.transaction{@db.run "DROP TABLE #{l}"}
|
|
636
641
|
end
|
|
637
|
-
@db.sqls.
|
|
642
|
+
@db.sqls.must_equal ['BEGIN', 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED', 'DROP TABLE uncommitted', 'COMMIT',
|
|
638
643
|
'BEGIN', 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED', 'DROP TABLE committed', 'COMMIT',
|
|
639
644
|
'BEGIN', 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ', 'DROP TABLE repeatable', 'COMMIT',
|
|
640
645
|
'BEGIN', 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE', 'DROP TABLE serializable', 'COMMIT']
|
|
641
646
|
end
|
|
642
647
|
|
|
643
|
-
|
|
648
|
+
it "should support :retry_on option for automatically retrying transactions" do
|
|
644
649
|
a = []
|
|
645
650
|
@db.transaction(:retry_on=>Sequel::DatabaseDisconnectError){a << 1; raise Sequel::DatabaseDisconnectError if a.length < 2}
|
|
646
|
-
@db.sqls.
|
|
647
|
-
a.
|
|
651
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK', 'BEGIN', 'COMMIT']
|
|
652
|
+
a.must_equal [1, 1]
|
|
648
653
|
|
|
649
654
|
a = []
|
|
650
655
|
@db.transaction(:retry_on=>[Sequel::ConstraintViolation, Sequel::SerializationFailure]) do
|
|
@@ -652,11 +657,11 @@ shared_examples_for "Database#transaction" do
|
|
|
652
657
|
raise Sequel::SerializationFailure if a.length == 1
|
|
653
658
|
raise Sequel::ConstraintViolation if a.length == 2
|
|
654
659
|
end
|
|
655
|
-
@db.sqls.
|
|
656
|
-
a.
|
|
660
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK', 'BEGIN', 'ROLLBACK', 'BEGIN', 'COMMIT']
|
|
661
|
+
a.must_equal [1, 1, 1]
|
|
657
662
|
end
|
|
658
663
|
|
|
659
|
-
|
|
664
|
+
it "should support :num_retries option for limiting the number of retry times" do
|
|
660
665
|
a = []
|
|
661
666
|
lambda do
|
|
662
667
|
@db.transaction(:num_retries=>1, :retry_on=>[Sequel::ConstraintViolation, Sequel::SerializationFailure]) do
|
|
@@ -664,12 +669,12 @@ shared_examples_for "Database#transaction" do
|
|
|
664
669
|
raise Sequel::SerializationFailure if a.length == 1
|
|
665
670
|
raise Sequel::ConstraintViolation if a.length == 2
|
|
666
671
|
end
|
|
667
|
-
end.
|
|
668
|
-
@db.sqls.
|
|
669
|
-
a.
|
|
672
|
+
end.must_raise(Sequel::ConstraintViolation)
|
|
673
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK', 'BEGIN', 'ROLLBACK']
|
|
674
|
+
a.must_equal [1, 1]
|
|
670
675
|
end
|
|
671
676
|
|
|
672
|
-
|
|
677
|
+
it "should support :num_retries=>nil option to retry indefinitely" do
|
|
673
678
|
a = []
|
|
674
679
|
lambda do
|
|
675
680
|
@db.transaction(:num_retries=>nil, :retry_on=>[Sequel::ConstraintViolation]) do
|
|
@@ -677,30 +682,30 @@ shared_examples_for "Database#transaction" do
|
|
|
677
682
|
raise Sequel::SerializationFailure if a.length >= 100
|
|
678
683
|
raise Sequel::ConstraintViolation
|
|
679
684
|
end
|
|
680
|
-
end.
|
|
681
|
-
@db.sqls.
|
|
682
|
-
a.
|
|
685
|
+
end.must_raise(Sequel::SerializationFailure)
|
|
686
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK'] * 100
|
|
687
|
+
a.must_equal [1] * 100
|
|
683
688
|
end
|
|
684
689
|
|
|
685
|
-
|
|
690
|
+
it "should support :before_retry option for invoking callback before retrying" do
|
|
686
691
|
a, errs, calls = [], [], []
|
|
687
692
|
retryer = proc{|n, err| calls << n; errs << err }
|
|
688
693
|
@db.transaction(:retry_on=>Sequel::DatabaseDisconnectError, :before_retry => retryer) do
|
|
689
694
|
a << 1; raise Sequel::DatabaseDisconnectError if a.length < 3
|
|
690
695
|
end
|
|
691
|
-
@db.sqls.
|
|
692
|
-
a.
|
|
693
|
-
errs.count.
|
|
694
|
-
errs.each { |e| e.class.
|
|
695
|
-
calls.
|
|
696
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK', 'BEGIN', 'ROLLBACK', 'BEGIN', 'COMMIT']
|
|
697
|
+
a.must_equal [1, 1, 1]
|
|
698
|
+
errs.count.must_equal 2
|
|
699
|
+
errs.each { |e| e.class.must_equal Sequel::DatabaseDisconnectError }
|
|
700
|
+
calls.must_equal [1, 2]
|
|
696
701
|
end
|
|
697
702
|
|
|
698
|
-
|
|
699
|
-
proc{@db.transaction{@db.transaction(:retry_on=>Sequel::ConstraintViolation){}}}.
|
|
700
|
-
@db.sqls.
|
|
703
|
+
it "should raise an error if attempting to use :retry_on inside another transaction" do
|
|
704
|
+
proc{@db.transaction{@db.transaction(:retry_on=>Sequel::ConstraintViolation){}}}.must_raise(Sequel::Error)
|
|
705
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
701
706
|
end
|
|
702
707
|
|
|
703
|
-
|
|
708
|
+
it "should handle returning inside of the block by committing" do
|
|
704
709
|
def @db.ret_commit
|
|
705
710
|
transaction do
|
|
706
711
|
execute 'DROP TABLE test;'
|
|
@@ -708,17 +713,17 @@ shared_examples_for "Database#transaction" do
|
|
|
708
713
|
end
|
|
709
714
|
end
|
|
710
715
|
@db.ret_commit
|
|
711
|
-
@db.sqls.
|
|
716
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test;', 'COMMIT']
|
|
712
717
|
end
|
|
713
718
|
|
|
714
|
-
|
|
719
|
+
it "should issue ROLLBACK if an exception is raised, and re-raise" do
|
|
715
720
|
@db.transaction {@db.execute 'DROP TABLE test'; raise RuntimeError} rescue nil
|
|
716
|
-
@db.sqls.
|
|
721
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test', 'ROLLBACK']
|
|
717
722
|
|
|
718
|
-
proc {@db.transaction {raise RuntimeError}}.
|
|
723
|
+
proc {@db.transaction {raise RuntimeError}}.must_raise(RuntimeError)
|
|
719
724
|
end
|
|
720
725
|
|
|
721
|
-
|
|
726
|
+
it "should handle errors when sending BEGIN" do
|
|
722
727
|
ec = Class.new(StandardError)
|
|
723
728
|
meta_def(@db, :database_error_classes){[ec]}
|
|
724
729
|
meta_def(@db, :log_connection_execute){|c, sql| sql =~ /BEGIN/ ? raise(ec, 'bad') : super(c, sql)}
|
|
@@ -726,12 +731,12 @@ shared_examples_for "Database#transaction" do
|
|
|
726
731
|
@db.transaction{@db.execute 'DROP TABLE test;'}
|
|
727
732
|
rescue Sequel::DatabaseError => e
|
|
728
733
|
end
|
|
729
|
-
e.
|
|
730
|
-
e.wrapped_exception.
|
|
731
|
-
@db.sqls.
|
|
734
|
+
e.wont_equal nil
|
|
735
|
+
e.wrapped_exception.must_be_kind_of(ec)
|
|
736
|
+
@db.sqls.must_equal ['ROLLBACK']
|
|
732
737
|
end
|
|
733
738
|
|
|
734
|
-
|
|
739
|
+
it "should handle errors when sending COMMIT" do
|
|
735
740
|
ec = Class.new(StandardError)
|
|
736
741
|
meta_def(@db, :database_error_classes){[ec]}
|
|
737
742
|
meta_def(@db, :log_connection_execute){|c, sql| sql =~ /COMMIT/ ? raise(ec, 'bad') : super(c, sql)}
|
|
@@ -739,12 +744,12 @@ shared_examples_for "Database#transaction" do
|
|
|
739
744
|
@db.transaction{@db.execute 'DROP TABLE test;'}
|
|
740
745
|
rescue Sequel::DatabaseError => e
|
|
741
746
|
end
|
|
742
|
-
e.
|
|
743
|
-
e.wrapped_exception.
|
|
744
|
-
@db.sqls.
|
|
747
|
+
e.wont_equal nil
|
|
748
|
+
e.wrapped_exception.must_be_kind_of(ec)
|
|
749
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test;', 'ROLLBACK']
|
|
745
750
|
end
|
|
746
751
|
|
|
747
|
-
|
|
752
|
+
it "should raise original exception if there is an exception raised when rolling back" do
|
|
748
753
|
ec = Class.new(StandardError)
|
|
749
754
|
meta_def(@db, :database_error_classes){[ec]}
|
|
750
755
|
meta_def(@db, :log_connection_execute){|c, sql| sql =~ /ROLLBACK/ ? raise(ec, 'bad') : super(c, sql)}
|
|
@@ -752,72 +757,72 @@ shared_examples_for "Database#transaction" do
|
|
|
752
757
|
@db.transaction{raise ArgumentError, 'asdf'}
|
|
753
758
|
rescue => e
|
|
754
759
|
end
|
|
755
|
-
e.
|
|
756
|
-
@db.sqls.
|
|
760
|
+
e.must_be_kind_of(ArgumentError)
|
|
761
|
+
@db.sqls.must_equal ['BEGIN']
|
|
757
762
|
end
|
|
758
763
|
|
|
759
|
-
|
|
764
|
+
it "should issue ROLLBACK if Sequel::Rollback is called in the transaction" do
|
|
760
765
|
@db.transaction do
|
|
761
766
|
@db.drop_table(:a)
|
|
762
767
|
raise Sequel::Rollback
|
|
763
768
|
@db.drop_table(:b)
|
|
764
769
|
end
|
|
765
770
|
|
|
766
|
-
@db.sqls.
|
|
771
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE a', 'ROLLBACK']
|
|
767
772
|
end
|
|
768
773
|
|
|
769
|
-
|
|
774
|
+
it "should have in_transaction? return true if inside a transaction" do
|
|
770
775
|
c = nil
|
|
771
776
|
@db.transaction{c = @db.in_transaction?}
|
|
772
|
-
c.
|
|
777
|
+
c.must_equal true
|
|
773
778
|
end
|
|
774
779
|
|
|
775
|
-
|
|
780
|
+
it "should have in_transaction? handle sharding correctly" do
|
|
776
781
|
c = []
|
|
777
782
|
@db.transaction(:server=>:test){c << @db.in_transaction?}
|
|
778
783
|
@db.transaction(:server=>:test){c << @db.in_transaction?(:server=>:test)}
|
|
779
|
-
c.
|
|
784
|
+
c.must_equal [false, true]
|
|
780
785
|
end
|
|
781
786
|
|
|
782
|
-
|
|
783
|
-
@db.in_transaction?.
|
|
787
|
+
it "should have in_transaction? return false if not in a transaction" do
|
|
788
|
+
@db.in_transaction?.must_equal false
|
|
784
789
|
end
|
|
785
790
|
|
|
786
|
-
|
|
787
|
-
@db.transaction{raise Sequel::Rollback}.
|
|
791
|
+
it "should return nil if Sequel::Rollback is called in the transaction" do
|
|
792
|
+
@db.transaction{raise Sequel::Rollback}.must_equal nil
|
|
788
793
|
end
|
|
789
794
|
|
|
790
|
-
|
|
791
|
-
proc {@db.transaction(:rollback=>:reraise){raise Sequel::Rollback}}.
|
|
792
|
-
@db.sqls.
|
|
793
|
-
proc {@db.transaction(:rollback=>:reraise){raise ArgumentError}}.
|
|
794
|
-
@db.sqls.
|
|
795
|
-
@db.transaction(:rollback=>:reraise){1}.
|
|
796
|
-
@db.sqls.
|
|
795
|
+
it "should reraise Sequel::Rollback errors when using :rollback=>:reraise option is given" do
|
|
796
|
+
proc {@db.transaction(:rollback=>:reraise){raise Sequel::Rollback}}.must_raise(Sequel::Rollback)
|
|
797
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
798
|
+
proc {@db.transaction(:rollback=>:reraise){raise ArgumentError}}.must_raise(ArgumentError)
|
|
799
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
800
|
+
@db.transaction(:rollback=>:reraise){1}.must_equal 1
|
|
801
|
+
@db.sqls.must_equal ['BEGIN', 'COMMIT']
|
|
797
802
|
end
|
|
798
803
|
|
|
799
|
-
|
|
800
|
-
proc {@db.transaction(:rollback=>:always){raise ArgumentError}}.
|
|
801
|
-
@db.sqls.
|
|
802
|
-
@db.transaction(:rollback=>:always){raise Sequel::Rollback}.
|
|
803
|
-
@db.sqls.
|
|
804
|
-
@db.transaction(:rollback=>:always){1}.
|
|
805
|
-
@db.sqls.
|
|
804
|
+
it "should always rollback if :rollback=>:always option is given" do
|
|
805
|
+
proc {@db.transaction(:rollback=>:always){raise ArgumentError}}.must_raise(ArgumentError)
|
|
806
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
807
|
+
@db.transaction(:rollback=>:always){raise Sequel::Rollback}.must_equal nil
|
|
808
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
809
|
+
@db.transaction(:rollback=>:always){1}.must_equal 1
|
|
810
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
806
811
|
catch(:foo) do
|
|
807
812
|
@db.transaction(:rollback=>:always){throw :foo}
|
|
808
813
|
end
|
|
809
|
-
@db.sqls.
|
|
814
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
810
815
|
end
|
|
811
|
-
|
|
812
|
-
|
|
816
|
+
|
|
817
|
+
it "should raise database errors when commiting a transaction as Sequel::DatabaseError" do
|
|
813
818
|
meta_def(@db, :commit_transaction){raise ArgumentError}
|
|
814
|
-
lambda{@db.transaction{}}.
|
|
819
|
+
lambda{@db.transaction{}}.must_raise(ArgumentError)
|
|
815
820
|
|
|
816
821
|
meta_def(@db, :database_error_classes){[ArgumentError]}
|
|
817
|
-
lambda{@db.transaction{}}.
|
|
822
|
+
lambda{@db.transaction{}}.must_raise(Sequel::DatabaseError)
|
|
818
823
|
end
|
|
819
824
|
|
|
820
|
-
|
|
825
|
+
it "should be re-entrant" do
|
|
821
826
|
q, q1 = Queue.new, Queue.new
|
|
822
827
|
cc = nil
|
|
823
828
|
t = Thread.new do
|
|
@@ -830,26 +835,26 @@ shared_examples_for "Database#transaction" do
|
|
|
830
835
|
end
|
|
831
836
|
q.push nil
|
|
832
837
|
q1.pop
|
|
833
|
-
cc.
|
|
838
|
+
cc.must_be_kind_of(Sequel::Mock::Connection)
|
|
834
839
|
tr = @db.instance_variable_get(:@transactions)
|
|
835
|
-
tr.keys.
|
|
840
|
+
tr.keys.must_equal [cc]
|
|
836
841
|
q.push nil
|
|
837
842
|
t.join
|
|
838
|
-
tr.
|
|
843
|
+
tr.must_be :empty?
|
|
839
844
|
end
|
|
840
845
|
|
|
841
|
-
|
|
846
|
+
it "should correctly handle nested transacation use with separate shards" do
|
|
842
847
|
@db.transaction do |c1|
|
|
843
848
|
@db.transaction(:server=>:test) do |c2|
|
|
844
|
-
c1.
|
|
849
|
+
c1.wont_equal c2
|
|
845
850
|
@db.execute 'DROP TABLE test;'
|
|
846
851
|
end
|
|
847
852
|
end
|
|
848
|
-
@db.sqls.
|
|
853
|
+
@db.sqls.must_equal ['BEGIN', 'BEGIN -- test', 'DROP TABLE test;', 'COMMIT -- test', 'COMMIT']
|
|
849
854
|
end
|
|
850
855
|
|
|
851
|
-
if (!defined?(RUBY_ENGINE) or RUBY_ENGINE == 'ruby'
|
|
852
|
-
|
|
856
|
+
if (!defined?(RUBY_ENGINE) or RUBY_ENGINE == 'ruby') and !RUBY_VERSION.start_with?('1.9')
|
|
857
|
+
it "should handle Thread#kill for transactions inside threads" do
|
|
853
858
|
q = Queue.new
|
|
854
859
|
q1 = Queue.new
|
|
855
860
|
t = Thread.new do
|
|
@@ -863,92 +868,92 @@ shared_examples_for "Database#transaction" do
|
|
|
863
868
|
q1.pop
|
|
864
869
|
t.kill
|
|
865
870
|
t.join
|
|
866
|
-
@db.sqls.
|
|
871
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test', 'ROLLBACK']
|
|
867
872
|
end
|
|
868
873
|
end
|
|
869
874
|
|
|
870
|
-
|
|
871
|
-
proc{@db.after_commit}.
|
|
872
|
-
proc{@db.after_rollback}.
|
|
875
|
+
it "should raise an Error if after_commit or after_rollback is called without a block" do
|
|
876
|
+
proc{@db.after_commit}.must_raise(Sequel::Error)
|
|
877
|
+
proc{@db.after_rollback}.must_raise(Sequel::Error)
|
|
873
878
|
end
|
|
874
879
|
|
|
875
|
-
|
|
880
|
+
it "should have after_commit and after_rollback respect :server option" do
|
|
876
881
|
@db.transaction(:server=>:test){@db.after_commit(:server=>:test){@db.execute('foo', :server=>:test)}}
|
|
877
|
-
@db.sqls.
|
|
882
|
+
@db.sqls.must_equal ['BEGIN -- test', 'COMMIT -- test', 'foo -- test']
|
|
878
883
|
@db.transaction(:server=>:test){@db.after_rollback(:server=>:test){@db.execute('foo', :server=>:test)}; raise Sequel::Rollback}
|
|
879
|
-
@db.sqls.
|
|
884
|
+
@db.sqls.must_equal ['BEGIN -- test', 'ROLLBACK -- test', 'foo -- test']
|
|
880
885
|
end
|
|
881
886
|
|
|
882
|
-
|
|
887
|
+
it "should execute after_commit outside transactions" do
|
|
883
888
|
@db.after_commit{@db.execute('foo')}
|
|
884
|
-
@db.sqls.
|
|
889
|
+
@db.sqls.must_equal ['foo']
|
|
885
890
|
end
|
|
886
891
|
|
|
887
|
-
|
|
892
|
+
it "should ignore after_rollback outside transactions" do
|
|
888
893
|
@db.after_rollback{@db.execute('foo')}
|
|
889
|
-
@db.sqls.
|
|
894
|
+
@db.sqls.must_equal []
|
|
890
895
|
end
|
|
891
896
|
|
|
892
|
-
|
|
897
|
+
it "should support after_commit inside transactions" do
|
|
893
898
|
@db.transaction{@db.after_commit{@db.execute('foo')}}
|
|
894
|
-
@db.sqls.
|
|
899
|
+
@db.sqls.must_equal ['BEGIN', 'COMMIT', 'foo']
|
|
895
900
|
end
|
|
896
901
|
|
|
897
|
-
|
|
902
|
+
it "should support after_rollback inside transactions" do
|
|
898
903
|
@db.transaction{@db.after_rollback{@db.execute('foo')}}
|
|
899
|
-
@db.sqls.
|
|
904
|
+
@db.sqls.must_equal ['BEGIN', 'COMMIT']
|
|
900
905
|
end
|
|
901
906
|
|
|
902
|
-
|
|
907
|
+
it "should have transaction inside after_commit work correctly" do
|
|
903
908
|
@db.transaction{@db.after_commit{@db.transaction{@db.execute('foo')}}}
|
|
904
|
-
@db.sqls.
|
|
909
|
+
@db.sqls.must_equal ['BEGIN', 'COMMIT', 'BEGIN', 'foo', 'COMMIT']
|
|
905
910
|
end
|
|
906
911
|
|
|
907
|
-
|
|
912
|
+
it "should have transaction inside after_rollback work correctly" do
|
|
908
913
|
@db.transaction(:rollback=>:always){@db.after_rollback{@db.transaction{@db.execute('foo')}}}
|
|
909
|
-
@db.sqls.
|
|
914
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK', 'BEGIN', 'foo', 'COMMIT']
|
|
910
915
|
end
|
|
911
916
|
|
|
912
|
-
|
|
917
|
+
it "should not call after_commit if the transaction rolls back" do
|
|
913
918
|
@db.transaction{@db.after_commit{@db.execute('foo')}; raise Sequel::Rollback}
|
|
914
|
-
@db.sqls.
|
|
919
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
915
920
|
end
|
|
916
921
|
|
|
917
|
-
|
|
922
|
+
it "should call after_rollback if the transaction rolls back" do
|
|
918
923
|
@db.transaction{@db.after_rollback{@db.execute('foo')}; raise Sequel::Rollback}
|
|
919
|
-
@db.sqls.
|
|
924
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK', 'foo']
|
|
920
925
|
end
|
|
921
926
|
|
|
922
|
-
|
|
927
|
+
it "should call multiple after_commit blocks in order if called inside transactions" do
|
|
923
928
|
@db.transaction{@db.after_commit{@db.execute('foo')}; @db.after_commit{@db.execute('bar')}}
|
|
924
|
-
@db.sqls.
|
|
929
|
+
@db.sqls.must_equal ['BEGIN', 'COMMIT', 'foo', 'bar']
|
|
925
930
|
end
|
|
926
931
|
|
|
927
|
-
|
|
932
|
+
it "should call multiple after_rollback blocks in order if called inside transactions" do
|
|
928
933
|
@db.transaction{@db.after_rollback{@db.execute('foo')}; @db.after_rollback{@db.execute('bar')}; raise Sequel::Rollback}
|
|
929
|
-
@db.sqls.
|
|
934
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK', 'foo', 'bar']
|
|
930
935
|
end
|
|
931
936
|
|
|
932
|
-
|
|
937
|
+
it "should support after_commit inside nested transactions" do
|
|
933
938
|
@db.transaction{@db.transaction{@db.after_commit{@db.execute('foo')}}}
|
|
934
|
-
@db.sqls.
|
|
939
|
+
@db.sqls.must_equal ['BEGIN', 'COMMIT', 'foo']
|
|
935
940
|
end
|
|
936
941
|
|
|
937
|
-
|
|
942
|
+
it "should support after_rollback inside nested transactions" do
|
|
938
943
|
@db.transaction{@db.transaction{@db.after_rollback{@db.execute('foo')}}; raise Sequel::Rollback}
|
|
939
|
-
@db.sqls.
|
|
944
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK', 'foo']
|
|
940
945
|
end
|
|
941
946
|
|
|
942
|
-
|
|
947
|
+
it "should raise an error if you attempt to use after_commit inside a prepared transaction" do
|
|
943
948
|
meta_def(@db, :supports_prepared_transactions?){true}
|
|
944
|
-
proc{@db.transaction(:prepare=>'XYZ'){@db.after_commit{@db.execute('foo')}}}.
|
|
945
|
-
@db.sqls.
|
|
949
|
+
proc{@db.transaction(:prepare=>'XYZ'){@db.after_commit{@db.execute('foo')}}}.must_raise(Sequel::Error)
|
|
950
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
946
951
|
end
|
|
947
952
|
|
|
948
|
-
|
|
953
|
+
it "should raise an error if you attempt to use after_rollback inside a prepared transaction" do
|
|
949
954
|
meta_def(@db, :supports_prepared_transactions?){true}
|
|
950
|
-
proc{@db.transaction(:prepare=>'XYZ'){@db.after_rollback{@db.execute('foo')}}}.
|
|
951
|
-
@db.sqls.
|
|
955
|
+
proc{@db.transaction(:prepare=>'XYZ'){@db.after_rollback{@db.execute('foo')}}}.must_raise(Sequel::Error)
|
|
956
|
+
@db.sqls.must_equal ['BEGIN', 'ROLLBACK']
|
|
952
957
|
end
|
|
953
958
|
end
|
|
954
959
|
|
|
@@ -957,37 +962,37 @@ describe "Database#transaction with savepoint support" do
|
|
|
957
962
|
@db = Sequel.mock(:servers=>{:test=>{}})
|
|
958
963
|
end
|
|
959
964
|
|
|
960
|
-
|
|
965
|
+
include DatabaseTransactionSpecs
|
|
961
966
|
|
|
962
|
-
|
|
967
|
+
it "should support after_commit inside savepoints" do
|
|
963
968
|
@db.transaction do
|
|
964
969
|
@db.after_commit{@db.execute('foo')}
|
|
965
970
|
@db.transaction(:savepoint=>true){@db.after_commit{@db.execute('bar')}}
|
|
966
971
|
@db.after_commit{@db.execute('baz')}
|
|
967
972
|
end
|
|
968
|
-
@db.sqls.
|
|
973
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'RELEASE SAVEPOINT autopoint_1', 'COMMIT', 'foo', 'bar', 'baz']
|
|
969
974
|
end
|
|
970
975
|
|
|
971
|
-
|
|
976
|
+
it "should support after_rollback inside savepoints" do
|
|
972
977
|
@db.transaction do
|
|
973
978
|
@db.after_rollback{@db.execute('foo')}
|
|
974
979
|
@db.transaction(:savepoint=>true){@db.after_rollback{@db.execute('bar')}}
|
|
975
980
|
@db.after_rollback{@db.execute('baz')}
|
|
976
981
|
raise Sequel::Rollback
|
|
977
982
|
end
|
|
978
|
-
@db.sqls.
|
|
983
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'RELEASE SAVEPOINT autopoint_1', 'ROLLBACK', 'foo', 'bar', 'baz']
|
|
979
984
|
end
|
|
980
985
|
|
|
981
|
-
|
|
986
|
+
it "should raise an error if you attempt to use after_commit inside a savepoint in a prepared transaction" do
|
|
982
987
|
meta_def(@db, :supports_prepared_transactions?){true}
|
|
983
|
-
proc{@db.transaction(:prepare=>'XYZ'){@db.transaction(:savepoint=>true){@db.after_commit{@db.execute('foo')}}}}.
|
|
984
|
-
@db.sqls.
|
|
988
|
+
proc{@db.transaction(:prepare=>'XYZ'){@db.transaction(:savepoint=>true){@db.after_commit{@db.execute('foo')}}}}.must_raise(Sequel::Error)
|
|
989
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1','ROLLBACK TO SAVEPOINT autopoint_1', 'ROLLBACK']
|
|
985
990
|
end
|
|
986
991
|
|
|
987
|
-
|
|
992
|
+
it "should raise an error if you attempt to use after_rollback inside a savepoint in a prepared transaction" do
|
|
988
993
|
meta_def(@db, :supports_prepared_transactions?){true}
|
|
989
|
-
proc{@db.transaction(:prepare=>'XYZ'){@db.transaction(:savepoint=>true){@db.after_rollback{@db.execute('foo')}}}}.
|
|
990
|
-
@db.sqls.
|
|
994
|
+
proc{@db.transaction(:prepare=>'XYZ'){@db.transaction(:savepoint=>true){@db.after_rollback{@db.execute('foo')}}}}.must_raise(Sequel::Error)
|
|
995
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1','ROLLBACK TO SAVEPOINT autopoint_1', 'ROLLBACK']
|
|
991
996
|
end
|
|
992
997
|
end
|
|
993
998
|
|
|
@@ -997,7 +1002,7 @@ describe "Database#transaction without savepoint support" do
|
|
|
997
1002
|
meta_def(@db, :supports_savepoints?){false}
|
|
998
1003
|
end
|
|
999
1004
|
|
|
1000
|
-
|
|
1005
|
+
include DatabaseTransactionSpecs
|
|
1001
1006
|
end
|
|
1002
1007
|
|
|
1003
1008
|
describe "Sequel.transaction" do
|
|
@@ -1008,29 +1013,29 @@ describe "Sequel.transaction" do
|
|
|
1008
1013
|
@db3 = Sequel.mock(:append=>'3', :sqls=>@sqls)
|
|
1009
1014
|
end
|
|
1010
1015
|
|
|
1011
|
-
|
|
1012
|
-
Sequel.transaction([@db1, @db2, @db3]){1}.
|
|
1013
|
-
@sqls.
|
|
1016
|
+
it "should run the block inside transacitons on all three databases" do
|
|
1017
|
+
Sequel.transaction([@db1, @db2, @db3]){1}.must_equal 1
|
|
1018
|
+
@sqls.must_equal ['BEGIN -- 1', 'BEGIN -- 2', 'BEGIN -- 3', 'COMMIT -- 3', 'COMMIT -- 2', 'COMMIT -- 1']
|
|
1014
1019
|
end
|
|
1015
1020
|
|
|
1016
|
-
|
|
1017
|
-
Sequel.transaction([@db1, @db2, @db3], :rollback=>:always){1}.
|
|
1018
|
-
@sqls.
|
|
1021
|
+
it "should pass options to all the blocks" do
|
|
1022
|
+
Sequel.transaction([@db1, @db2, @db3], :rollback=>:always){1}.must_equal 1
|
|
1023
|
+
@sqls.must_equal ['BEGIN -- 1', 'BEGIN -- 2', 'BEGIN -- 3', 'ROLLBACK -- 3', 'ROLLBACK -- 2', 'ROLLBACK -- 1']
|
|
1019
1024
|
end
|
|
1020
1025
|
|
|
1021
|
-
|
|
1022
|
-
Sequel.transaction([@db1, @db2, @db3]){raise Sequel::Rollback}.
|
|
1023
|
-
@sqls.
|
|
1026
|
+
it "should handle Sequel::Rollback exceptions raised by the block to rollback on all databases" do
|
|
1027
|
+
Sequel.transaction([@db1, @db2, @db3]){raise Sequel::Rollback}.must_equal nil
|
|
1028
|
+
@sqls.must_equal ['BEGIN -- 1', 'BEGIN -- 2', 'BEGIN -- 3', 'ROLLBACK -- 3', 'ROLLBACK -- 2', 'ROLLBACK -- 1']
|
|
1024
1029
|
end
|
|
1025
1030
|
|
|
1026
|
-
|
|
1027
|
-
Sequel.transaction([@db1, @db2, @db3]){Sequel.transaction([@db1, @db2, @db3]){1}}.
|
|
1028
|
-
@sqls.
|
|
1031
|
+
it "should handle nested transactions" do
|
|
1032
|
+
Sequel.transaction([@db1, @db2, @db3]){Sequel.transaction([@db1, @db2, @db3]){1}}.must_equal 1
|
|
1033
|
+
@sqls.must_equal ['BEGIN -- 1', 'BEGIN -- 2', 'BEGIN -- 3', 'COMMIT -- 3', 'COMMIT -- 2', 'COMMIT -- 1']
|
|
1029
1034
|
end
|
|
1030
1035
|
|
|
1031
|
-
|
|
1032
|
-
Sequel.transaction([@db1, @db2, @db3]){Sequel.transaction([@db1, @db2, @db3], :savepoint=>true){1}}.
|
|
1033
|
-
@sqls.
|
|
1036
|
+
it "should handle savepoints" do
|
|
1037
|
+
Sequel.transaction([@db1, @db2, @db3]){Sequel.transaction([@db1, @db2, @db3], :savepoint=>true){1}}.must_equal 1
|
|
1038
|
+
@sqls.must_equal ['BEGIN -- 1', 'BEGIN -- 2', 'BEGIN -- 3',
|
|
1034
1039
|
'SAVEPOINT autopoint_1 -- 1', 'SAVEPOINT autopoint_1 -- 2', 'SAVEPOINT autopoint_1 -- 3',
|
|
1035
1040
|
'RELEASE SAVEPOINT autopoint_1 -- 3', 'RELEASE SAVEPOINT autopoint_1 -- 2', 'RELEASE SAVEPOINT autopoint_1 -- 1',
|
|
1036
1041
|
'COMMIT -- 3', 'COMMIT -- 2', 'COMMIT -- 1']
|
|
@@ -1042,46 +1047,46 @@ describe "Database#transaction with savepoints" do
|
|
|
1042
1047
|
@db = Sequel.mock
|
|
1043
1048
|
end
|
|
1044
1049
|
|
|
1045
|
-
|
|
1050
|
+
it "should wrap the supplied block with BEGIN + COMMIT statements" do
|
|
1046
1051
|
@db.transaction {@db.execute 'DROP TABLE test;'}
|
|
1047
|
-
@db.sqls.
|
|
1052
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test;', 'COMMIT']
|
|
1048
1053
|
end
|
|
1049
1054
|
|
|
1050
|
-
|
|
1055
|
+
it "should use savepoints if given the :savepoint option" do
|
|
1051
1056
|
@db.transaction{@db.transaction(:savepoint=>true){@db.execute 'DROP TABLE test;'}}
|
|
1052
|
-
@db.sqls.
|
|
1057
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE test;', 'RELEASE SAVEPOINT autopoint_1', 'COMMIT']
|
|
1053
1058
|
end
|
|
1054
1059
|
|
|
1055
|
-
|
|
1060
|
+
it "should use savepoints if surrounding transaction uses :auto_savepoint option" do
|
|
1056
1061
|
@db.transaction(:auto_savepoint=>true){@db.transaction{@db.execute 'DROP TABLE test;'}}
|
|
1057
|
-
@db.sqls.
|
|
1062
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE test;', 'RELEASE SAVEPOINT autopoint_1', 'COMMIT']
|
|
1058
1063
|
|
|
1059
1064
|
@db.transaction(:auto_savepoint=>true){@db.transaction{@db.transaction{@db.execute 'DROP TABLE test;'}}}
|
|
1060
|
-
@db.sqls.
|
|
1065
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE test;', 'RELEASE SAVEPOINT autopoint_1', 'COMMIT']
|
|
1061
1066
|
|
|
1062
1067
|
@db.transaction(:auto_savepoint=>true){@db.transaction(:auto_savepoint=>true){@db.transaction{@db.execute 'DROP TABLE test;'}}}
|
|
1063
|
-
@db.sqls.
|
|
1068
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'SAVEPOINT autopoint_2', 'DROP TABLE test;', 'RELEASE SAVEPOINT autopoint_2', 'RELEASE SAVEPOINT autopoint_1', 'COMMIT']
|
|
1064
1069
|
|
|
1065
1070
|
@db.transaction{@db.transaction(:auto_savepoint=>true, :savepoint=>true){@db.transaction{@db.execute 'DROP TABLE test;'}}}
|
|
1066
|
-
@db.sqls.
|
|
1071
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'SAVEPOINT autopoint_2', 'DROP TABLE test;', 'RELEASE SAVEPOINT autopoint_2', 'RELEASE SAVEPOINT autopoint_1', 'COMMIT']
|
|
1067
1072
|
end
|
|
1068
1073
|
|
|
1069
|
-
|
|
1074
|
+
it "should not use savepoints if surrounding transaction uses :auto_savepoint and current transaction uses :savepoint=>false option" do
|
|
1070
1075
|
@db.transaction(:auto_savepoint=>true){@db.transaction(:savepoint=>false){@db.execute 'DROP TABLE test;'}}
|
|
1071
|
-
@db.sqls.
|
|
1076
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test;', 'COMMIT']
|
|
1072
1077
|
end
|
|
1073
1078
|
|
|
1074
|
-
|
|
1079
|
+
it "should not use a savepoint if no transaction is in progress" do
|
|
1075
1080
|
@db.transaction(:savepoint=>true){@db.execute 'DROP TABLE test;'}
|
|
1076
|
-
@db.sqls.
|
|
1081
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test;', 'COMMIT']
|
|
1077
1082
|
end
|
|
1078
1083
|
|
|
1079
|
-
|
|
1084
|
+
it "should reuse the current transaction if no :savepoint option is given" do
|
|
1080
1085
|
@db.transaction{@db.transaction{@db.execute 'DROP TABLE test;'}}
|
|
1081
|
-
@db.sqls.
|
|
1086
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test;', 'COMMIT']
|
|
1082
1087
|
end
|
|
1083
1088
|
|
|
1084
|
-
|
|
1089
|
+
it "should handle returning inside of the block by committing" do
|
|
1085
1090
|
def @db.ret_commit
|
|
1086
1091
|
transaction do
|
|
1087
1092
|
execute 'DROP TABLE test;'
|
|
@@ -1089,10 +1094,10 @@ describe "Database#transaction with savepoints" do
|
|
|
1089
1094
|
end
|
|
1090
1095
|
end
|
|
1091
1096
|
@db.ret_commit
|
|
1092
|
-
@db.sqls.
|
|
1097
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test;', 'COMMIT']
|
|
1093
1098
|
end
|
|
1094
1099
|
|
|
1095
|
-
|
|
1100
|
+
it "should handle returning inside of a savepoint by committing" do
|
|
1096
1101
|
def @db.ret_commit
|
|
1097
1102
|
transaction do
|
|
1098
1103
|
transaction(:savepoint=>true) do
|
|
@@ -1102,34 +1107,34 @@ describe "Database#transaction with savepoints" do
|
|
|
1102
1107
|
end
|
|
1103
1108
|
end
|
|
1104
1109
|
@db.ret_commit
|
|
1105
|
-
@db.sqls.
|
|
1110
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE test;', 'RELEASE SAVEPOINT autopoint_1', 'COMMIT']
|
|
1106
1111
|
end
|
|
1107
1112
|
|
|
1108
|
-
|
|
1113
|
+
it "should issue ROLLBACK if an exception is raised, and re-raise" do
|
|
1109
1114
|
@db.transaction {@db.execute 'DROP TABLE test'; raise RuntimeError} rescue nil
|
|
1110
|
-
@db.sqls.
|
|
1115
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE test', 'ROLLBACK']
|
|
1111
1116
|
|
|
1112
|
-
proc {@db.transaction {raise RuntimeError}}.
|
|
1117
|
+
proc {@db.transaction {raise RuntimeError}}.must_raise(RuntimeError)
|
|
1113
1118
|
end
|
|
1114
1119
|
|
|
1115
|
-
|
|
1120
|
+
it "should issue ROLLBACK SAVEPOINT if an exception is raised inside a savepoint, and re-raise" do
|
|
1116
1121
|
@db.transaction{@db.transaction(:savepoint=>true){@db.execute 'DROP TABLE test'; raise RuntimeError}} rescue nil
|
|
1117
|
-
@db.sqls.
|
|
1122
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE test', 'ROLLBACK TO SAVEPOINT autopoint_1', 'ROLLBACK']
|
|
1118
1123
|
|
|
1119
|
-
proc {@db.transaction {raise RuntimeError}}.
|
|
1124
|
+
proc {@db.transaction {raise RuntimeError}}.must_raise(RuntimeError)
|
|
1120
1125
|
end
|
|
1121
1126
|
|
|
1122
|
-
|
|
1127
|
+
it "should issue ROLLBACK if Sequel::Rollback is raised in the transaction" do
|
|
1123
1128
|
@db.transaction do
|
|
1124
1129
|
@db.drop_table(:a)
|
|
1125
1130
|
raise Sequel::Rollback
|
|
1126
1131
|
@db.drop_table(:b)
|
|
1127
1132
|
end
|
|
1128
1133
|
|
|
1129
|
-
@db.sqls.
|
|
1134
|
+
@db.sqls.must_equal ['BEGIN', 'DROP TABLE a', 'ROLLBACK']
|
|
1130
1135
|
end
|
|
1131
1136
|
|
|
1132
|
-
|
|
1137
|
+
it "should issue ROLLBACK SAVEPOINT if Sequel::Rollback is raised in a savepoint" do
|
|
1133
1138
|
@db.transaction do
|
|
1134
1139
|
@db.transaction(:savepoint=>true) do
|
|
1135
1140
|
@db.drop_table(:a)
|
|
@@ -1138,58 +1143,59 @@ describe "Database#transaction with savepoints" do
|
|
|
1138
1143
|
@db.drop_table(:b)
|
|
1139
1144
|
end
|
|
1140
1145
|
|
|
1141
|
-
@db.sqls.
|
|
1146
|
+
@db.sqls.must_equal ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE a', 'ROLLBACK TO SAVEPOINT autopoint_1', 'DROP TABLE b', 'COMMIT']
|
|
1142
1147
|
end
|
|
1143
1148
|
|
|
1144
|
-
|
|
1149
|
+
it "should raise database errors when commiting a transaction as Sequel::DatabaseError" do
|
|
1145
1150
|
meta_def(@db, :commit_transaction){raise ArgumentError}
|
|
1146
|
-
lambda{@db.transaction{}}.
|
|
1147
|
-
lambda{@db.transaction{@db.transaction(:savepoint=>true){}}}.
|
|
1151
|
+
lambda{@db.transaction{}}.must_raise(ArgumentError)
|
|
1152
|
+
lambda{@db.transaction{@db.transaction(:savepoint=>true){}}}.must_raise(ArgumentError)
|
|
1148
1153
|
|
|
1149
1154
|
meta_def(@db, :database_error_classes){[ArgumentError]}
|
|
1150
|
-
lambda{@db.transaction{}}.
|
|
1151
|
-
lambda{@db.transaction{@db.transaction(:savepoint=>true){}}}.
|
|
1155
|
+
lambda{@db.transaction{}}.must_raise(Sequel::DatabaseError)
|
|
1156
|
+
lambda{@db.transaction{@db.transaction(:savepoint=>true){}}}.must_raise(Sequel::DatabaseError)
|
|
1152
1157
|
end
|
|
1153
1158
|
end
|
|
1154
1159
|
|
|
1155
1160
|
describe "A Database adapter with a scheme" do
|
|
1156
1161
|
before do
|
|
1162
|
+
require 'sequel/adapters/mock'
|
|
1157
1163
|
@ccc = Class.new(Sequel::Mock::Database)
|
|
1158
1164
|
@ccc.send(:set_adapter_scheme, :ccc)
|
|
1159
1165
|
end
|
|
1160
1166
|
|
|
1161
|
-
|
|
1162
|
-
Sequel::ADAPTER_MAP[:ccc].
|
|
1167
|
+
it "should be registered in the ADAPTER_MAP" do
|
|
1168
|
+
Sequel::ADAPTER_MAP[:ccc].must_equal @ccc
|
|
1163
1169
|
end
|
|
1164
1170
|
|
|
1165
|
-
|
|
1166
|
-
@ccc.new.database_type.
|
|
1171
|
+
it "should give the database_type as the adapter scheme by default" do
|
|
1172
|
+
@ccc.new.database_type.must_equal :ccc
|
|
1167
1173
|
end
|
|
1168
1174
|
|
|
1169
|
-
|
|
1175
|
+
it "should be instantiated when its scheme is specified" do
|
|
1170
1176
|
c = Sequel::Database.connect('ccc://localhost/db')
|
|
1171
|
-
c.
|
|
1172
|
-
c.opts[:host].
|
|
1173
|
-
c.opts[:database].
|
|
1177
|
+
c.must_be_kind_of(@ccc)
|
|
1178
|
+
c.opts[:host].must_equal 'localhost'
|
|
1179
|
+
c.opts[:database].must_equal 'db'
|
|
1174
1180
|
end
|
|
1175
1181
|
|
|
1176
|
-
|
|
1182
|
+
it "should be accessible through Sequel.connect" do
|
|
1177
1183
|
c = Sequel.connect 'ccc://localhost/db'
|
|
1178
|
-
c.
|
|
1179
|
-
c.opts[:host].
|
|
1180
|
-
c.opts[:database].
|
|
1184
|
+
c.must_be_kind_of(@ccc)
|
|
1185
|
+
c.opts[:host].must_equal 'localhost'
|
|
1186
|
+
c.opts[:database].must_equal 'db'
|
|
1181
1187
|
end
|
|
1182
1188
|
|
|
1183
|
-
|
|
1189
|
+
it "should be accessible through Sequel.connect via a block" do
|
|
1184
1190
|
x = nil
|
|
1185
1191
|
y = nil
|
|
1186
1192
|
z = nil
|
|
1187
1193
|
returnValue = 'anything'
|
|
1188
1194
|
|
|
1189
1195
|
p = proc do |c|
|
|
1190
|
-
c.
|
|
1191
|
-
c.opts[:host].
|
|
1192
|
-
c.opts[:database].
|
|
1196
|
+
c.must_be_kind_of(@ccc)
|
|
1197
|
+
c.opts[:host].must_equal 'localhost'
|
|
1198
|
+
c.opts[:database].must_equal 'db'
|
|
1193
1199
|
z = y
|
|
1194
1200
|
y = x
|
|
1195
1201
|
x = c
|
|
@@ -1202,109 +1208,109 @@ describe "A Database adapter with a scheme" do
|
|
|
1202
1208
|
self.class::DISCONNECTS << self
|
|
1203
1209
|
end
|
|
1204
1210
|
end
|
|
1205
|
-
Sequel::Database.connect('ccc://localhost/db', &p).
|
|
1206
|
-
@ccc::DISCONNECTS.
|
|
1211
|
+
Sequel::Database.connect('ccc://localhost/db', &p).must_equal returnValue
|
|
1212
|
+
@ccc::DISCONNECTS.must_equal [x]
|
|
1207
1213
|
|
|
1208
|
-
Sequel.connect('ccc://localhost/db', &p).
|
|
1209
|
-
@ccc::DISCONNECTS.
|
|
1214
|
+
Sequel.connect('ccc://localhost/db', &p).must_equal returnValue
|
|
1215
|
+
@ccc::DISCONNECTS.must_equal [y, x]
|
|
1210
1216
|
|
|
1211
1217
|
Sequel.send(:def_adapter_method, :ccc)
|
|
1212
|
-
Sequel.ccc('db', :host=>'localhost', &p).
|
|
1213
|
-
@ccc::DISCONNECTS.
|
|
1218
|
+
Sequel.ccc('db', :host=>'localhost', &p).must_equal returnValue
|
|
1219
|
+
@ccc::DISCONNECTS.must_equal [z, y, x]
|
|
1214
1220
|
class << Sequel; remove_method(:ccc) end
|
|
1215
1221
|
end
|
|
1216
1222
|
|
|
1217
|
-
|
|
1223
|
+
it "should be accessible through Sequel.<adapter>" do
|
|
1218
1224
|
Sequel.send(:def_adapter_method, :ccc)
|
|
1219
1225
|
|
|
1220
1226
|
# invalid parameters
|
|
1221
|
-
proc {Sequel.ccc('abc', 'def')}.
|
|
1222
|
-
proc {Sequel.ccc(1)}.
|
|
1227
|
+
proc {Sequel.ccc('abc', 'def')}.must_raise(Sequel::Error)
|
|
1228
|
+
proc {Sequel.ccc(1)}.must_raise(Sequel::Error)
|
|
1223
1229
|
|
|
1224
1230
|
c = Sequel.ccc('mydb')
|
|
1225
|
-
c.
|
|
1226
|
-
c.opts.values_at(:adapter, :database, :adapter_class).
|
|
1231
|
+
c.must_be_kind_of(@ccc)
|
|
1232
|
+
c.opts.values_at(:adapter, :database, :adapter_class).must_equal [:ccc, 'mydb', @ccc]
|
|
1227
1233
|
|
|
1228
1234
|
c = Sequel.ccc('mydb', :host => 'localhost')
|
|
1229
|
-
c.
|
|
1230
|
-
c.opts.values_at(:adapter, :database, :host, :adapter_class).
|
|
1235
|
+
c.must_be_kind_of(@ccc)
|
|
1236
|
+
c.opts.values_at(:adapter, :database, :host, :adapter_class).must_equal [:ccc, 'mydb', 'localhost', @ccc]
|
|
1231
1237
|
|
|
1232
1238
|
c = Sequel.ccc
|
|
1233
|
-
c.
|
|
1234
|
-
c.opts.values_at(:adapter, :adapter_class).
|
|
1239
|
+
c.must_be_kind_of(@ccc)
|
|
1240
|
+
c.opts.values_at(:adapter, :adapter_class).must_equal [:ccc, @ccc]
|
|
1235
1241
|
|
|
1236
1242
|
c = Sequel.ccc(:database => 'mydb', :host => 'localhost')
|
|
1237
|
-
c.
|
|
1238
|
-
c.opts.values_at(:adapter, :database, :host, :adapter_class).
|
|
1243
|
+
c.must_be_kind_of(@ccc)
|
|
1244
|
+
c.opts.values_at(:adapter, :database, :host, :adapter_class).must_equal [:ccc, 'mydb', 'localhost', @ccc]
|
|
1239
1245
|
class << Sequel; remove_method(:ccc) end
|
|
1240
1246
|
end
|
|
1241
1247
|
|
|
1242
|
-
|
|
1248
|
+
it "should be accessible through Sequel.connect with options" do
|
|
1243
1249
|
c = Sequel.connect(:adapter => :ccc, :database => 'mydb')
|
|
1244
|
-
c.
|
|
1245
|
-
c.opts[:adapter].
|
|
1250
|
+
c.must_be_kind_of(@ccc)
|
|
1251
|
+
c.opts[:adapter].must_equal :ccc
|
|
1246
1252
|
end
|
|
1247
1253
|
|
|
1248
|
-
|
|
1254
|
+
it "should be accessible through Sequel.connect with URL parameters" do
|
|
1249
1255
|
c = Sequel.connect 'ccc:///db?host=/tmp&user=test'
|
|
1250
|
-
c.
|
|
1251
|
-
c.opts[:host].
|
|
1252
|
-
c.opts[:database].
|
|
1253
|
-
c.opts[:user].
|
|
1256
|
+
c.must_be_kind_of(@ccc)
|
|
1257
|
+
c.opts[:host].must_equal '/tmp'
|
|
1258
|
+
c.opts[:database].must_equal 'db'
|
|
1259
|
+
c.opts[:user].must_equal 'test'
|
|
1254
1260
|
end
|
|
1255
1261
|
|
|
1256
|
-
|
|
1262
|
+
it "should have URL parameters take precedence over fixed URL parts" do
|
|
1257
1263
|
c = Sequel.connect 'ccc://localhost/db?host=a&database=b'
|
|
1258
|
-
c.
|
|
1259
|
-
c.opts[:host].
|
|
1260
|
-
c.opts[:database].
|
|
1264
|
+
c.must_be_kind_of(@ccc)
|
|
1265
|
+
c.opts[:host].must_equal 'a'
|
|
1266
|
+
c.opts[:database].must_equal 'b'
|
|
1261
1267
|
end
|
|
1262
1268
|
|
|
1263
|
-
|
|
1269
|
+
it "should have hash options take predence over URL parameters or parts" do
|
|
1264
1270
|
c = Sequel.connect 'ccc://localhost/db?host=/tmp', :host=>'a', :database=>'b', :user=>'c'
|
|
1265
|
-
c.
|
|
1266
|
-
c.opts[:host].
|
|
1267
|
-
c.opts[:database].
|
|
1268
|
-
c.opts[:user].
|
|
1271
|
+
c.must_be_kind_of(@ccc)
|
|
1272
|
+
c.opts[:host].must_equal 'a'
|
|
1273
|
+
c.opts[:database].must_equal 'b'
|
|
1274
|
+
c.opts[:user].must_equal 'c'
|
|
1269
1275
|
end
|
|
1270
1276
|
|
|
1271
|
-
|
|
1277
|
+
it "should unescape values of URL parameters and parts" do
|
|
1272
1278
|
c = Sequel.connect 'ccc:///d%5bb%5d?host=domain%5cinstance'
|
|
1273
|
-
c.
|
|
1274
|
-
c.opts[:database].
|
|
1275
|
-
c.opts[:host].
|
|
1279
|
+
c.must_be_kind_of(@ccc)
|
|
1280
|
+
c.opts[:database].must_equal 'd[b]'
|
|
1281
|
+
c.opts[:host].must_equal 'domain\\instance'
|
|
1276
1282
|
end
|
|
1277
1283
|
|
|
1278
|
-
|
|
1284
|
+
it "should test the connection if test parameter is truthy" do
|
|
1279
1285
|
@ccc.send(:define_method, :connect){}
|
|
1280
|
-
proc{Sequel.connect 'ccc:///d%5bb%5d?test=t'}.
|
|
1281
|
-
proc{Sequel.connect 'ccc:///d%5bb%5d?test=1'}.
|
|
1282
|
-
proc{Sequel.connect 'ccc:///d%5bb%5d', :test=>true}.
|
|
1283
|
-
proc{Sequel.connect 'ccc:///d%5bb%5d', :test=>'t'}.
|
|
1286
|
+
proc{Sequel.connect 'ccc:///d%5bb%5d?test=t'}.must_raise(Sequel::DatabaseConnectionError)
|
|
1287
|
+
proc{Sequel.connect 'ccc:///d%5bb%5d?test=1'}.must_raise(Sequel::DatabaseConnectionError)
|
|
1288
|
+
proc{Sequel.connect 'ccc:///d%5bb%5d', :test=>true}.must_raise(Sequel::DatabaseConnectionError)
|
|
1289
|
+
proc{Sequel.connect 'ccc:///d%5bb%5d', :test=>'t'}.must_raise(Sequel::DatabaseConnectionError)
|
|
1284
1290
|
end
|
|
1285
1291
|
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1292
|
+
it "should not test the connection if test parameter is not truthy" do
|
|
1293
|
+
Sequel.connect 'ccc:///d%5bb%5d?test=f'
|
|
1294
|
+
Sequel.connect 'ccc:///d%5bb%5d?test=0'
|
|
1295
|
+
Sequel.connect 'ccc:///d%5bb%5d', :test=>false
|
|
1296
|
+
Sequel.connect 'ccc:///d%5bb%5d', :test=>'f'
|
|
1291
1297
|
end
|
|
1292
1298
|
end
|
|
1293
1299
|
|
|
1294
1300
|
describe "Sequel::Database.connect" do
|
|
1295
|
-
|
|
1296
|
-
proc{Sequel::Database.connect(nil)}.
|
|
1297
|
-
proc{Sequel::Database.connect(Object.new)}.
|
|
1301
|
+
it "should raise an Error if not given a String or Hash" do
|
|
1302
|
+
proc{Sequel::Database.connect(nil)}.must_raise(Sequel::Error)
|
|
1303
|
+
proc{Sequel::Database.connect(Object.new)}.must_raise(Sequel::Error)
|
|
1298
1304
|
end
|
|
1299
1305
|
end
|
|
1300
1306
|
|
|
1301
1307
|
describe "An unknown database scheme" do
|
|
1302
|
-
|
|
1303
|
-
proc {Sequel::Database.connect('ddd://localhost/db')}.
|
|
1308
|
+
it "should raise an error in Sequel::Database.connect" do
|
|
1309
|
+
proc {Sequel::Database.connect('ddd://localhost/db')}.must_raise(Sequel::AdapterNotFound)
|
|
1304
1310
|
end
|
|
1305
1311
|
|
|
1306
|
-
|
|
1307
|
-
proc {Sequel.connect('ddd://localhost/db')}.
|
|
1312
|
+
it "should raise an error in Sequel.connect" do
|
|
1313
|
+
proc {Sequel.connect('ddd://localhost/db')}.must_raise(Sequel::AdapterNotFound)
|
|
1308
1314
|
end
|
|
1309
1315
|
end
|
|
1310
1316
|
|
|
@@ -1318,14 +1324,14 @@ describe "A broken adapter (lib is there but the class is not)" do
|
|
|
1318
1324
|
File.delete(@fn)
|
|
1319
1325
|
end
|
|
1320
1326
|
|
|
1321
|
-
|
|
1322
|
-
proc {Sequel.connect('blah://blow')}.
|
|
1327
|
+
it "should raise an error" do
|
|
1328
|
+
proc {Sequel.connect('blah://blow')}.must_raise(Sequel::AdapterNotFound)
|
|
1323
1329
|
end
|
|
1324
1330
|
end
|
|
1325
1331
|
|
|
1326
1332
|
describe "Sequel::Database.load_adapter" do
|
|
1327
|
-
|
|
1328
|
-
Sequel::Database.load_adapter(:foo, :subdir=>'bar').
|
|
1333
|
+
it "should not raise an error if subadapter does not exist" do
|
|
1334
|
+
Sequel::Database.load_adapter(:foo, :subdir=>'bar').must_equal nil
|
|
1329
1335
|
end
|
|
1330
1336
|
end
|
|
1331
1337
|
|
|
@@ -1334,26 +1340,26 @@ describe "A single threaded database" do
|
|
|
1334
1340
|
Sequel::Database.single_threaded = false
|
|
1335
1341
|
end
|
|
1336
1342
|
|
|
1337
|
-
|
|
1343
|
+
it "should use a SingleConnectionPool instead of a ConnectionPool" do
|
|
1338
1344
|
db = Sequel::Database.new(:single_threaded => true){123}
|
|
1339
|
-
db.pool.
|
|
1345
|
+
db.pool.must_be_kind_of(Sequel::SingleConnectionPool)
|
|
1340
1346
|
end
|
|
1341
1347
|
|
|
1342
|
-
|
|
1348
|
+
it "should be constructable using :single_threaded => true option" do
|
|
1343
1349
|
db = Sequel::Database.new(:single_threaded => true){123}
|
|
1344
|
-
db.pool.
|
|
1350
|
+
db.pool.must_be_kind_of(Sequel::SingleConnectionPool)
|
|
1345
1351
|
end
|
|
1346
1352
|
|
|
1347
|
-
|
|
1353
|
+
it "should be constructable using Database.single_threaded = true" do
|
|
1348
1354
|
Sequel::Database.single_threaded = true
|
|
1349
1355
|
db = Sequel::Database.new{123}
|
|
1350
|
-
db.pool.
|
|
1356
|
+
db.pool.must_be_kind_of(Sequel::SingleConnectionPool)
|
|
1351
1357
|
end
|
|
1352
1358
|
|
|
1353
|
-
|
|
1359
|
+
it "should be constructable using Sequel.single_threaded = true" do
|
|
1354
1360
|
Sequel.single_threaded = true
|
|
1355
1361
|
db = Sequel::Database.new{123}
|
|
1356
|
-
db.pool.
|
|
1362
|
+
db.pool.must_be_kind_of(Sequel::SingleConnectionPool)
|
|
1357
1363
|
end
|
|
1358
1364
|
end
|
|
1359
1365
|
|
|
@@ -1366,31 +1372,31 @@ describe "A single threaded database" do
|
|
|
1366
1372
|
end
|
|
1367
1373
|
end
|
|
1368
1374
|
|
|
1369
|
-
|
|
1370
|
-
@db.pool.hold {|c| c.
|
|
1371
|
-
@db.pool.hold {|c| c.
|
|
1375
|
+
it "should invoke connection_proc only once" do
|
|
1376
|
+
@db.pool.hold {|c| c.must_equal 1234568}
|
|
1377
|
+
@db.pool.hold {|c| c.must_equal 1234568}
|
|
1372
1378
|
end
|
|
1373
1379
|
|
|
1374
|
-
|
|
1380
|
+
it "should disconnect correctly" do
|
|
1375
1381
|
def @db.disconnect_connection(c); @dc = c end
|
|
1376
1382
|
def @db.dc; @dc end
|
|
1377
1383
|
x = nil
|
|
1378
1384
|
@db.pool.hold{|c| x = c}
|
|
1379
|
-
@db.pool.hold{|c| c.
|
|
1380
|
-
|
|
1381
|
-
@db.dc.
|
|
1385
|
+
@db.pool.hold{|c| c.must_equal x}
|
|
1386
|
+
@db.disconnect
|
|
1387
|
+
@db.dc.must_equal x
|
|
1382
1388
|
end
|
|
1383
1389
|
|
|
1384
|
-
|
|
1390
|
+
it "should convert an Exception on connection into a DatabaseConnectionError" do
|
|
1385
1391
|
db = Sequel::Database.new(:single_threaded => true, :servers=>{})
|
|
1386
1392
|
def db.connect(*) raise Exception end
|
|
1387
|
-
proc {db.pool.hold {|c|}}.
|
|
1393
|
+
proc {db.pool.hold {|c|}}.must_raise(Sequel::DatabaseConnectionError)
|
|
1388
1394
|
end
|
|
1389
1395
|
|
|
1390
|
-
|
|
1396
|
+
it "should raise a DatabaseConnectionError if the connection proc returns nil" do
|
|
1391
1397
|
db = Sequel.mock(:single_threaded => true, :servers=>{})
|
|
1392
1398
|
def db.connect(*) end
|
|
1393
|
-
proc {db.pool.hold {|c|}}.
|
|
1399
|
+
proc {db.pool.hold {|c|}}.must_raise(Sequel::DatabaseConnectionError)
|
|
1394
1400
|
end
|
|
1395
1401
|
end
|
|
1396
1402
|
|
|
@@ -1399,41 +1405,41 @@ describe "A database" do
|
|
|
1399
1405
|
Sequel::Database.single_threaded = false
|
|
1400
1406
|
end
|
|
1401
1407
|
|
|
1402
|
-
|
|
1408
|
+
it "should have single_threaded? respond to true if in single threaded mode" do
|
|
1403
1409
|
db = Sequel::Database.new(:single_threaded => true){1234}
|
|
1404
|
-
db.
|
|
1410
|
+
db.must_be :single_threaded?
|
|
1405
1411
|
|
|
1406
1412
|
db = Sequel::Database.new(:max_options => 1)
|
|
1407
|
-
db.
|
|
1413
|
+
db.wont_be :single_threaded?
|
|
1408
1414
|
|
|
1409
1415
|
db = Sequel::Database.new
|
|
1410
|
-
db.
|
|
1416
|
+
db.wont_be :single_threaded?
|
|
1411
1417
|
|
|
1412
1418
|
Sequel::Database.single_threaded = true
|
|
1413
1419
|
|
|
1414
1420
|
db = Sequel::Database.new{123}
|
|
1415
|
-
db.
|
|
1421
|
+
db.must_be :single_threaded?
|
|
1416
1422
|
|
|
1417
1423
|
db = Sequel::Database.new(:max_options => 4){123}
|
|
1418
|
-
db.
|
|
1424
|
+
db.must_be :single_threaded?
|
|
1419
1425
|
end
|
|
1420
1426
|
|
|
1421
|
-
|
|
1427
|
+
it "should be able to set loggers via the logger= and loggers= methods" do
|
|
1422
1428
|
db = Sequel::Database.new
|
|
1423
1429
|
s = "I'm a logger"
|
|
1424
1430
|
db.logger = s
|
|
1425
|
-
db.loggers.
|
|
1431
|
+
db.loggers.must_equal [s]
|
|
1426
1432
|
db.logger = nil
|
|
1427
|
-
db.loggers.
|
|
1433
|
+
db.loggers.must_equal []
|
|
1428
1434
|
|
|
1429
1435
|
db.loggers = [s]
|
|
1430
|
-
db.loggers.
|
|
1436
|
+
db.loggers.must_equal [s]
|
|
1431
1437
|
db.loggers = []
|
|
1432
|
-
db.loggers.
|
|
1438
|
+
db.loggers.must_equal []
|
|
1433
1439
|
|
|
1434
1440
|
t = "I'm also a logger"
|
|
1435
1441
|
db.loggers = [s, t]
|
|
1436
|
-
db.loggers.
|
|
1442
|
+
db.loggers.must_equal [s,t]
|
|
1437
1443
|
end
|
|
1438
1444
|
end
|
|
1439
1445
|
|
|
@@ -1442,44 +1448,44 @@ describe "Database#fetch" do
|
|
|
1442
1448
|
@db = Sequel.mock(:fetch=>proc{|sql| {:sql => sql}})
|
|
1443
1449
|
end
|
|
1444
1450
|
|
|
1445
|
-
|
|
1451
|
+
it "should create a dataset and invoke its fetch_rows method with the given sql" do
|
|
1446
1452
|
sql = nil
|
|
1447
1453
|
@db.fetch('select * from xyz') {|r| sql = r[:sql]}
|
|
1448
|
-
sql.
|
|
1454
|
+
sql.must_equal 'select * from xyz'
|
|
1449
1455
|
end
|
|
1450
1456
|
|
|
1451
|
-
|
|
1457
|
+
it "should format the given sql with any additional arguments" do
|
|
1452
1458
|
sql = nil
|
|
1453
1459
|
@db.fetch('select * from xyz where x = ? and y = ?', 15, 'abc') {|r| sql = r[:sql]}
|
|
1454
|
-
sql.
|
|
1460
|
+
sql.must_equal "select * from xyz where x = 15 and y = 'abc'"
|
|
1455
1461
|
|
|
1456
1462
|
@db.fetch('select name from table where name = ? or id in ?', 'aman', [3,4,7]) {|r| sql = r[:sql]}
|
|
1457
|
-
sql.
|
|
1463
|
+
sql.must_equal "select name from table where name = 'aman' or id in (3, 4, 7)"
|
|
1458
1464
|
end
|
|
1459
1465
|
|
|
1460
|
-
|
|
1466
|
+
it "should format the given sql with named arguments" do
|
|
1461
1467
|
sql = nil
|
|
1462
1468
|
@db.fetch('select * from xyz where x = :x and y = :y', :x=>15, :y=>'abc') {|r| sql = r[:sql]}
|
|
1463
|
-
sql.
|
|
1469
|
+
sql.must_equal "select * from xyz where x = 15 and y = 'abc'"
|
|
1464
1470
|
end
|
|
1465
1471
|
|
|
1466
|
-
|
|
1467
|
-
@db.fetch('select * from xyz').
|
|
1472
|
+
it "should return the dataset if no block is given" do
|
|
1473
|
+
@db.fetch('select * from xyz').must_be_kind_of(Sequel::Dataset)
|
|
1468
1474
|
|
|
1469
|
-
@db.fetch('select a from b').map {|r| r[:sql]}.
|
|
1475
|
+
@db.fetch('select a from b').map {|r| r[:sql]}.must_equal ['select a from b']
|
|
1470
1476
|
|
|
1471
|
-
@db.fetch('select c from d').inject([]) {|m, r| m << r; m}.
|
|
1477
|
+
@db.fetch('select c from d').inject([]) {|m, r| m << r; m}.must_equal \
|
|
1472
1478
|
[{:sql => 'select c from d'}]
|
|
1473
1479
|
end
|
|
1474
1480
|
|
|
1475
|
-
|
|
1481
|
+
it "should return a dataset that always uses the given sql for SELECTs" do
|
|
1476
1482
|
ds = @db.fetch('select * from xyz')
|
|
1477
|
-
ds.select_sql.
|
|
1478
|
-
ds.sql.
|
|
1483
|
+
ds.select_sql.must_equal 'select * from xyz'
|
|
1484
|
+
ds.sql.must_equal 'select * from xyz'
|
|
1479
1485
|
|
|
1480
1486
|
ds.filter!{price.sql_number < 100}
|
|
1481
|
-
ds.select_sql.
|
|
1482
|
-
ds.sql.
|
|
1487
|
+
ds.select_sql.must_equal 'select * from xyz'
|
|
1488
|
+
ds.sql.must_equal 'select * from xyz'
|
|
1483
1489
|
end
|
|
1484
1490
|
end
|
|
1485
1491
|
|
|
@@ -1489,31 +1495,31 @@ describe "Database#[]" do
|
|
|
1489
1495
|
@db = Sequel.mock
|
|
1490
1496
|
end
|
|
1491
1497
|
|
|
1492
|
-
|
|
1498
|
+
it "should return a dataset when symbols are given" do
|
|
1493
1499
|
ds = @db[:items]
|
|
1494
|
-
ds.
|
|
1495
|
-
ds.opts[:from].
|
|
1500
|
+
ds.must_be_kind_of(Sequel::Dataset)
|
|
1501
|
+
ds.opts[:from].must_equal [:items]
|
|
1496
1502
|
end
|
|
1497
1503
|
|
|
1498
|
-
|
|
1504
|
+
it "should return a dataset when a string is given" do
|
|
1499
1505
|
@db.fetch = proc{|sql| {:sql=>sql}}
|
|
1500
1506
|
sql = nil
|
|
1501
1507
|
@db['select * from xyz where x = ? and y = ?', 15, 'abc'].each {|r| sql = r[:sql]}
|
|
1502
|
-
sql.
|
|
1508
|
+
sql.must_equal "select * from xyz where x = 15 and y = 'abc'"
|
|
1503
1509
|
end
|
|
1504
1510
|
end
|
|
1505
1511
|
|
|
1506
1512
|
describe "Database#inspect" do
|
|
1507
|
-
|
|
1508
|
-
Sequel.connect('mock://foo/bar').inspect.
|
|
1513
|
+
it "should include the class name and the connection url" do
|
|
1514
|
+
Sequel.connect('mock://foo/bar').inspect.must_equal '#<Sequel::Mock::Database: "mock://foo/bar">'
|
|
1509
1515
|
end
|
|
1510
1516
|
|
|
1511
|
-
|
|
1512
|
-
Sequel.connect(:adapter=>:mock).inspect.
|
|
1517
|
+
it "should include the class name and the connection options if an options hash was given" do
|
|
1518
|
+
Sequel.connect(:adapter=>:mock).inspect.must_match(/#<Sequel::Mock::Database: \{:adapter=>:mock\}>/)
|
|
1513
1519
|
end
|
|
1514
1520
|
|
|
1515
|
-
|
|
1516
|
-
Sequel.connect('mock://foo', :database=>'bar').inspect.
|
|
1521
|
+
it "should include the class name, uri, and connection options if uri and options hash was given" do
|
|
1522
|
+
Sequel.connect('mock://foo', :database=>'bar').inspect.must_match(/#<Sequel::Mock::Database: "mock:\/\/foo" \{:database=>"bar"\}>/)
|
|
1517
1523
|
end
|
|
1518
1524
|
end
|
|
1519
1525
|
|
|
@@ -1522,68 +1528,68 @@ describe "Database#get" do
|
|
|
1522
1528
|
@db = Sequel.mock(:fetch=>{:a=>1})
|
|
1523
1529
|
end
|
|
1524
1530
|
|
|
1525
|
-
|
|
1526
|
-
@db.get(:a).
|
|
1527
|
-
@db.sqls.
|
|
1531
|
+
it "should use Dataset#get to get a single value" do
|
|
1532
|
+
@db.get(:a).must_equal 1
|
|
1533
|
+
@db.sqls.must_equal ['SELECT a LIMIT 1']
|
|
1528
1534
|
|
|
1529
1535
|
@db.get(Sequel.function(:version).as(:version))
|
|
1530
|
-
@db.sqls.
|
|
1536
|
+
@db.sqls.must_equal ['SELECT version() AS version LIMIT 1']
|
|
1531
1537
|
end
|
|
1532
1538
|
|
|
1533
|
-
|
|
1539
|
+
it "should accept a block" do
|
|
1534
1540
|
@db.get{a}
|
|
1535
|
-
@db.sqls.
|
|
1541
|
+
@db.sqls.must_equal ['SELECT a LIMIT 1']
|
|
1536
1542
|
|
|
1537
1543
|
@db.get{version(a).as(version)}
|
|
1538
|
-
@db.sqls.
|
|
1544
|
+
@db.sqls.must_equal ['SELECT version(a) AS version LIMIT 1']
|
|
1539
1545
|
end
|
|
1540
1546
|
|
|
1541
|
-
|
|
1542
|
-
@db.get(1).
|
|
1543
|
-
@db.sqls.
|
|
1547
|
+
it "should work when an alias cannot be determined" do
|
|
1548
|
+
@db.get(1).must_equal 1
|
|
1549
|
+
@db.sqls.must_equal ['SELECT 1 AS v LIMIT 1']
|
|
1544
1550
|
end
|
|
1545
1551
|
end
|
|
1546
1552
|
|
|
1547
1553
|
describe "Database#call" do
|
|
1548
|
-
|
|
1554
|
+
it "should call the prepared statement with the given name" do
|
|
1549
1555
|
db = Sequel.mock(:fetch=>{:id => 1, :x => 1})
|
|
1550
1556
|
db[:items].prepare(:select, :select_all)
|
|
1551
|
-
db.call(:select_all).
|
|
1557
|
+
db.call(:select_all).must_equal [{:id => 1, :x => 1}]
|
|
1552
1558
|
db[:items].filter(:n=>:$n).prepare(:select, :select_n)
|
|
1553
|
-
db.call(:select_n, :n=>1).
|
|
1554
|
-
db.sqls.
|
|
1559
|
+
db.call(:select_n, :n=>1).must_equal [{:id => 1, :x => 1}]
|
|
1560
|
+
db.sqls.must_equal ['SELECT * FROM items', 'SELECT * FROM items WHERE (n = 1)']
|
|
1555
1561
|
end
|
|
1556
1562
|
end
|
|
1557
1563
|
|
|
1558
1564
|
describe "Database#server_opts" do
|
|
1559
|
-
|
|
1565
|
+
it "should return the general opts if no :servers option is used" do
|
|
1560
1566
|
opts = {:host=>1, :database=>2}
|
|
1561
|
-
Sequel::Database.new(opts).send(:server_opts, :server1)[:host].
|
|
1567
|
+
Sequel::Database.new(opts).send(:server_opts, :server1)[:host].must_equal 1
|
|
1562
1568
|
end
|
|
1563
1569
|
|
|
1564
|
-
|
|
1570
|
+
it "should return the general opts if entry for the server is present in the :servers option" do
|
|
1565
1571
|
opts = {:host=>1, :database=>2, :servers=>{}}
|
|
1566
|
-
Sequel::Database.new(opts).send(:server_opts, :server1)[:host].
|
|
1572
|
+
Sequel::Database.new(opts).send(:server_opts, :server1)[:host].must_equal 1
|
|
1567
1573
|
end
|
|
1568
1574
|
|
|
1569
|
-
|
|
1575
|
+
it "should return the general opts merged with the specific opts if given as a hash" do
|
|
1570
1576
|
opts = {:host=>1, :database=>2, :servers=>{:server1=>{:host=>3}}}
|
|
1571
|
-
Sequel::Database.new(opts).send(:server_opts, :server1)[:host].
|
|
1577
|
+
Sequel::Database.new(opts).send(:server_opts, :server1)[:host].must_equal 3
|
|
1572
1578
|
end
|
|
1573
1579
|
|
|
1574
|
-
|
|
1580
|
+
it "should return the sgeneral opts merged with the specific opts if given as a proc" do
|
|
1575
1581
|
opts = {:host=>1, :database=>2, :servers=>{:server1=>proc{|db| {:host=>4}}}}
|
|
1576
|
-
Sequel::Database.new(opts).send(:server_opts, :server1)[:host].
|
|
1582
|
+
Sequel::Database.new(opts).send(:server_opts, :server1)[:host].must_equal 4
|
|
1577
1583
|
end
|
|
1578
1584
|
|
|
1579
|
-
|
|
1585
|
+
it "should raise an error if the specific opts is not a proc or hash" do
|
|
1580
1586
|
opts = {:host=>1, :database=>2, :servers=>{:server1=>2}}
|
|
1581
|
-
proc{Sequel::Database.new(opts).send(:server_opts, :server1)}.
|
|
1587
|
+
proc{Sequel::Database.new(opts).send(:server_opts, :server1)}.must_raise(Sequel::Error)
|
|
1582
1588
|
end
|
|
1583
1589
|
|
|
1584
|
-
|
|
1590
|
+
it "should return the general opts merged with given opts if given opts is a Hash" do
|
|
1585
1591
|
opts = {:host=>1, :database=>2}
|
|
1586
|
-
Sequel::Database.new(opts).send(:server_opts, :host=>2)[:host].
|
|
1592
|
+
Sequel::Database.new(opts).send(:server_opts, :host=>2)[:host].must_equal 2
|
|
1587
1593
|
end
|
|
1588
1594
|
end
|
|
1589
1595
|
|
|
@@ -1592,36 +1598,36 @@ describe "Database#add_servers" do
|
|
|
1592
1598
|
@db = Sequel.mock(:host=>1, :database=>2, :servers=>{:server1=>{:host=>3}})
|
|
1593
1599
|
end
|
|
1594
1600
|
|
|
1595
|
-
|
|
1596
|
-
@db.synchronize{|c| c.opts[:host].
|
|
1597
|
-
@db.synchronize(:server1){|c| c.opts[:host].
|
|
1598
|
-
@db.synchronize(:server2){|c| c.opts[:host].
|
|
1601
|
+
it "should add new servers to the connection pool" do
|
|
1602
|
+
@db.synchronize{|c| c.opts[:host].must_equal 1}
|
|
1603
|
+
@db.synchronize(:server1){|c| c.opts[:host].must_equal 3}
|
|
1604
|
+
@db.synchronize(:server2){|c| c.opts[:host].must_equal 1}
|
|
1599
1605
|
|
|
1600
1606
|
@db.add_servers(:server2=>{:host=>6})
|
|
1601
|
-
@db.synchronize{|c| c.opts[:host].
|
|
1602
|
-
@db.synchronize(:server1){|c| c.opts[:host].
|
|
1603
|
-
@db.synchronize(:server2){|c| c.opts[:host].
|
|
1607
|
+
@db.synchronize{|c| c.opts[:host].must_equal 1}
|
|
1608
|
+
@db.synchronize(:server1){|c| c.opts[:host].must_equal 3}
|
|
1609
|
+
@db.synchronize(:server2){|c| c.opts[:host].must_equal 6}
|
|
1604
1610
|
|
|
1605
1611
|
@db.disconnect
|
|
1606
|
-
@db.synchronize{|c| c.opts[:host].
|
|
1607
|
-
@db.synchronize(:server1){|c| c.opts[:host].
|
|
1608
|
-
@db.synchronize(:server2){|c| c.opts[:host].
|
|
1612
|
+
@db.synchronize{|c| c.opts[:host].must_equal 1}
|
|
1613
|
+
@db.synchronize(:server1){|c| c.opts[:host].must_equal 3}
|
|
1614
|
+
@db.synchronize(:server2){|c| c.opts[:host].must_equal 6}
|
|
1609
1615
|
end
|
|
1610
1616
|
|
|
1611
|
-
|
|
1612
|
-
@db.synchronize{|c| c.opts[:host].
|
|
1613
|
-
@db.synchronize(:server1){|c| c.opts[:host].
|
|
1614
|
-
@db.synchronize(:server2){|c| c.opts[:host].
|
|
1617
|
+
it "should replace options for future connections to existing servers" do
|
|
1618
|
+
@db.synchronize{|c| c.opts[:host].must_equal 1}
|
|
1619
|
+
@db.synchronize(:server1){|c| c.opts[:host].must_equal 3}
|
|
1620
|
+
@db.synchronize(:server2){|c| c.opts[:host].must_equal 1}
|
|
1615
1621
|
|
|
1616
1622
|
@db.add_servers(:default=>proc{{:host=>4}}, :server1=>{:host=>8})
|
|
1617
|
-
@db.synchronize{|c| c.opts[:host].
|
|
1618
|
-
@db.synchronize(:server1){|c| c.opts[:host].
|
|
1619
|
-
@db.synchronize(:server2){|c| c.opts[:host].
|
|
1623
|
+
@db.synchronize{|c| c.opts[:host].must_equal 1}
|
|
1624
|
+
@db.synchronize(:server1){|c| c.opts[:host].must_equal 3}
|
|
1625
|
+
@db.synchronize(:server2){|c| c.opts[:host].must_equal 1}
|
|
1620
1626
|
|
|
1621
1627
|
@db.disconnect
|
|
1622
|
-
@db.synchronize{|c| c.opts[:host].
|
|
1623
|
-
@db.synchronize(:server1){|c| c.opts[:host].
|
|
1624
|
-
@db.synchronize(:server2){|c| c.opts[:host].
|
|
1628
|
+
@db.synchronize{|c| c.opts[:host].must_equal 4}
|
|
1629
|
+
@db.synchronize(:server1){|c| c.opts[:host].must_equal 8}
|
|
1630
|
+
@db.synchronize(:server2){|c| c.opts[:host].must_equal 4}
|
|
1625
1631
|
end
|
|
1626
1632
|
end
|
|
1627
1633
|
|
|
@@ -1630,70 +1636,73 @@ describe "Database#remove_servers" do
|
|
|
1630
1636
|
@db = Sequel.mock(:host=>1, :database=>2, :servers=>{:server1=>{:host=>3}, :server2=>{:host=>4}})
|
|
1631
1637
|
end
|
|
1632
1638
|
|
|
1633
|
-
|
|
1634
|
-
@db.synchronize{|c| c.opts[:host].
|
|
1635
|
-
@db.synchronize(:server1){|c| c.opts[:host].
|
|
1636
|
-
@db.synchronize(:server2){|c| c.opts[:host].
|
|
1639
|
+
it "should remove servers from the connection pool" do
|
|
1640
|
+
@db.synchronize{|c| c.opts[:host].must_equal 1}
|
|
1641
|
+
@db.synchronize(:server1){|c| c.opts[:host].must_equal 3}
|
|
1642
|
+
@db.synchronize(:server2){|c| c.opts[:host].must_equal 4}
|
|
1637
1643
|
|
|
1638
1644
|
@db.remove_servers(:server1, :server2)
|
|
1639
|
-
@db.synchronize{|c| c.opts[:host].
|
|
1640
|
-
@db.synchronize(:server1){|c| c.opts[:host].
|
|
1641
|
-
@db.synchronize(:server2){|c| c.opts[:host].
|
|
1645
|
+
@db.synchronize{|c| c.opts[:host].must_equal 1}
|
|
1646
|
+
@db.synchronize(:server1){|c| c.opts[:host].must_equal 1}
|
|
1647
|
+
@db.synchronize(:server2){|c| c.opts[:host].must_equal 1}
|
|
1642
1648
|
end
|
|
1643
1649
|
|
|
1644
|
-
|
|
1650
|
+
it "should accept arrays of symbols" do
|
|
1645
1651
|
@db.remove_servers([:server1, :server2])
|
|
1646
|
-
@db.synchronize{|c| c.opts[:host].
|
|
1647
|
-
@db.synchronize(:server1){|c| c.opts[:host].
|
|
1648
|
-
@db.synchronize(:server2){|c| c.opts[:host].
|
|
1652
|
+
@db.synchronize{|c| c.opts[:host].must_equal 1}
|
|
1653
|
+
@db.synchronize(:server1){|c| c.opts[:host].must_equal 1}
|
|
1654
|
+
@db.synchronize(:server2){|c| c.opts[:host].must_equal 1}
|
|
1649
1655
|
end
|
|
1650
1656
|
|
|
1651
|
-
|
|
1657
|
+
it "should allow removal while connections are still open" do
|
|
1652
1658
|
@db.synchronize do |c1|
|
|
1653
|
-
c1.opts[:host].
|
|
1659
|
+
c1.opts[:host].must_equal 1
|
|
1654
1660
|
@db.synchronize(:server1) do |c2|
|
|
1655
|
-
c2.opts[:host].
|
|
1661
|
+
c2.opts[:host].must_equal 3
|
|
1656
1662
|
@db.synchronize(:server2) do |c3|
|
|
1657
|
-
c3.opts[:host].
|
|
1663
|
+
c3.opts[:host].must_equal 4
|
|
1658
1664
|
@db.remove_servers(:server1, :server2)
|
|
1659
1665
|
@db.synchronize(:server1) do |c4|
|
|
1660
|
-
c4.
|
|
1661
|
-
c4.
|
|
1662
|
-
c4.opts[:host].
|
|
1666
|
+
c4.wont_equal c2
|
|
1667
|
+
c4.must_equal c1
|
|
1668
|
+
c4.opts[:host].must_equal 1
|
|
1663
1669
|
@db.synchronize(:server2) do |c5|
|
|
1664
|
-
c5.
|
|
1665
|
-
c5.
|
|
1666
|
-
c5.opts[:host].
|
|
1670
|
+
c5.wont_equal c3
|
|
1671
|
+
c5.must_equal c1
|
|
1672
|
+
c5.opts[:host].must_equal 1
|
|
1667
1673
|
end
|
|
1668
1674
|
end
|
|
1669
|
-
c3.opts[:host].
|
|
1675
|
+
c3.opts[:host].must_equal 4
|
|
1670
1676
|
end
|
|
1671
|
-
c2.opts[:host].
|
|
1677
|
+
c2.opts[:host].must_equal 3
|
|
1672
1678
|
end
|
|
1673
|
-
c1.opts[:host].
|
|
1679
|
+
c1.opts[:host].must_equal 1
|
|
1674
1680
|
end
|
|
1675
1681
|
end
|
|
1676
1682
|
end
|
|
1677
1683
|
|
|
1678
1684
|
describe "Database#each_server with do/jdbc adapter connection string without :adapter option" do
|
|
1679
|
-
|
|
1685
|
+
it "should yield a separate database object for each server" do
|
|
1680
1686
|
klass = Class.new(Sequel::Database)
|
|
1681
|
-
klass.
|
|
1687
|
+
def klass.adapter_class(v)
|
|
1688
|
+
raise unless v == :jdbc
|
|
1689
|
+
Sequel::Mock::Database
|
|
1690
|
+
end
|
|
1682
1691
|
@db = klass.connect('jdbc:blah:', :host=>1, :database=>2, :servers=>{:server1=>{:host=>3}})
|
|
1683
1692
|
|
|
1684
1693
|
hosts = []
|
|
1685
1694
|
@db.each_server do |db|
|
|
1686
|
-
db.
|
|
1687
|
-
db.
|
|
1688
|
-
db.opts[:adapter_class].
|
|
1689
|
-
db.opts[:database].
|
|
1695
|
+
db.must_be_kind_of(Sequel::Database)
|
|
1696
|
+
db.wont_equal @db
|
|
1697
|
+
db.opts[:adapter_class].must_equal Sequel::Mock::Database
|
|
1698
|
+
db.opts[:database].must_equal 2
|
|
1690
1699
|
hosts << db.opts[:host]
|
|
1691
1700
|
end
|
|
1692
|
-
hosts.sort.
|
|
1701
|
+
hosts.sort.must_equal [1, 3]
|
|
1693
1702
|
end
|
|
1694
1703
|
|
|
1695
|
-
|
|
1696
|
-
proc{Sequel.mock.each_server}.
|
|
1704
|
+
it "should raise if not given a block" do
|
|
1705
|
+
proc{Sequel.mock.each_server}.must_raise(Sequel::Error)
|
|
1697
1706
|
end
|
|
1698
1707
|
end
|
|
1699
1708
|
|
|
@@ -1702,30 +1711,30 @@ describe "Database#each_server" do
|
|
|
1702
1711
|
@db = Sequel.mock(:host=>1, :database=>2, :servers=>{:server1=>{:host=>3}, :server2=>{:host=>4}})
|
|
1703
1712
|
end
|
|
1704
1713
|
|
|
1705
|
-
|
|
1714
|
+
it "should yield a separate database object for each server" do
|
|
1706
1715
|
hosts = []
|
|
1707
1716
|
@db.each_server do |db|
|
|
1708
|
-
db.
|
|
1709
|
-
db.
|
|
1710
|
-
db.opts[:adapter].
|
|
1711
|
-
db.opts[:database].
|
|
1717
|
+
db.must_be_kind_of(Sequel::Database)
|
|
1718
|
+
db.wont_equal @db
|
|
1719
|
+
db.opts[:adapter].must_equal :mock
|
|
1720
|
+
db.opts[:database].must_equal 2
|
|
1712
1721
|
hosts << db.opts[:host]
|
|
1713
1722
|
end
|
|
1714
|
-
hosts.sort.
|
|
1723
|
+
hosts.sort.must_equal [1, 3, 4]
|
|
1715
1724
|
end
|
|
1716
1725
|
|
|
1717
|
-
|
|
1726
|
+
it "should disconnect and remove entry from Sequel::DATABASES after use" do
|
|
1718
1727
|
dbs = []
|
|
1719
1728
|
dcs = []
|
|
1720
1729
|
@db.each_server do |db|
|
|
1721
1730
|
dbs << db
|
|
1722
|
-
Sequel::DATABASES.
|
|
1731
|
+
Sequel::DATABASES.must_include(db)
|
|
1723
1732
|
meta_def(db, :disconnect){dcs << db}
|
|
1724
1733
|
end
|
|
1725
1734
|
dbs.each do |db|
|
|
1726
|
-
Sequel::DATABASES.
|
|
1735
|
+
Sequel::DATABASES.wont_include(db)
|
|
1727
1736
|
end
|
|
1728
|
-
dbs.
|
|
1737
|
+
dbs.must_equal dcs
|
|
1729
1738
|
end
|
|
1730
1739
|
end
|
|
1731
1740
|
|
|
@@ -1734,29 +1743,29 @@ describe "Database#raise_error" do
|
|
|
1734
1743
|
@db = Sequel.mock
|
|
1735
1744
|
end
|
|
1736
1745
|
|
|
1737
|
-
|
|
1746
|
+
it "should reraise if the exception class is not in opts[:classes]" do
|
|
1738
1747
|
e = Class.new(StandardError)
|
|
1739
|
-
proc{@db.send(:raise_error, e.new(''), :classes=>[])}.
|
|
1748
|
+
proc{@db.send(:raise_error, e.new(''), :classes=>[])}.must_raise(e)
|
|
1740
1749
|
end
|
|
1741
1750
|
|
|
1742
|
-
|
|
1743
|
-
proc{@db.send(:raise_error, Interrupt.new(''), :classes=>[Interrupt])}.
|
|
1751
|
+
it "should convert the exception to a DatabaseError if the exception class is in opts[:classes]" do
|
|
1752
|
+
proc{@db.send(:raise_error, Interrupt.new(''), :classes=>[Interrupt])}.must_raise(Sequel::DatabaseError)
|
|
1744
1753
|
end
|
|
1745
1754
|
|
|
1746
|
-
|
|
1747
|
-
proc{@db.send(:raise_error, Interrupt.new(''))}.
|
|
1755
|
+
it "should convert the exception to a DatabaseError if opts[:classes] if not present" do
|
|
1756
|
+
proc{@db.send(:raise_error, Interrupt.new(''))}.must_raise(Sequel::DatabaseError)
|
|
1748
1757
|
end
|
|
1749
1758
|
|
|
1750
|
-
|
|
1751
|
-
proc{@db.send(:raise_error, Interrupt.new(''), :disconnect=>true)}.
|
|
1759
|
+
it "should convert the exception to a DatabaseDisconnectError if opts[:disconnect] is true" do
|
|
1760
|
+
proc{@db.send(:raise_error, Interrupt.new(''), :disconnect=>true)}.must_raise(Sequel::DatabaseDisconnectError)
|
|
1752
1761
|
end
|
|
1753
1762
|
|
|
1754
|
-
|
|
1763
|
+
it "should convert the exception to an appropriate error if exception message matches regexp" do
|
|
1755
1764
|
def @db.database_error_regexps
|
|
1756
1765
|
{/foo/ => Sequel::DatabaseDisconnectError, /bar/ => Sequel::ConstraintViolation}
|
|
1757
1766
|
end
|
|
1758
|
-
proc{@db.send(:raise_error, Interrupt.new('foo'))}.
|
|
1759
|
-
proc{@db.send(:raise_error, Interrupt.new('bar'))}.
|
|
1767
|
+
proc{@db.send(:raise_error, Interrupt.new('foo'))}.must_raise(Sequel::DatabaseDisconnectError)
|
|
1768
|
+
proc{@db.send(:raise_error, Interrupt.new('bar'))}.must_raise(Sequel::ConstraintViolation)
|
|
1760
1769
|
end
|
|
1761
1770
|
end
|
|
1762
1771
|
|
|
@@ -1765,58 +1774,58 @@ describe "Database#typecast_value" do
|
|
|
1765
1774
|
@db = Sequel::Database.new
|
|
1766
1775
|
end
|
|
1767
1776
|
|
|
1768
|
-
|
|
1769
|
-
proc{@db.typecast_value(:integer, "13a")}.
|
|
1770
|
-
proc{@db.typecast_value(:float, "4.e2")}.
|
|
1771
|
-
proc{@db.typecast_value(:decimal, :invalid_value)}.
|
|
1772
|
-
proc{@db.typecast_value(:date, Object.new)}.
|
|
1773
|
-
proc{@db.typecast_value(:date, 'a')}.
|
|
1774
|
-
proc{@db.typecast_value(:time, Date.new)}.
|
|
1775
|
-
proc{@db.typecast_value(:datetime, 4)}.
|
|
1777
|
+
it "should raise an InvalidValue when given an invalid value" do
|
|
1778
|
+
proc{@db.typecast_value(:integer, "13a")}.must_raise(Sequel::InvalidValue)
|
|
1779
|
+
proc{@db.typecast_value(:float, "4.e2")}.must_raise(Sequel::InvalidValue)
|
|
1780
|
+
proc{@db.typecast_value(:decimal, :invalid_value)}.must_raise(Sequel::InvalidValue)
|
|
1781
|
+
proc{@db.typecast_value(:date, Object.new)}.must_raise(Sequel::InvalidValue)
|
|
1782
|
+
proc{@db.typecast_value(:date, 'a')}.must_raise(Sequel::InvalidValue)
|
|
1783
|
+
proc{@db.typecast_value(:time, Date.new)}.must_raise(Sequel::InvalidValue)
|
|
1784
|
+
proc{@db.typecast_value(:datetime, 4)}.must_raise(Sequel::InvalidValue)
|
|
1776
1785
|
end
|
|
1777
1786
|
|
|
1778
|
-
|
|
1779
|
-
@db.typecast_value(:integer, "013").
|
|
1780
|
-
@db.typecast_value(:integer, "08").
|
|
1781
|
-
@db.typecast_value(:integer, "000013").
|
|
1782
|
-
@db.typecast_value(:integer, "000008").
|
|
1787
|
+
it "should handle integers with leading 0 as base 10" do
|
|
1788
|
+
@db.typecast_value(:integer, "013").must_equal 13
|
|
1789
|
+
@db.typecast_value(:integer, "08").must_equal 8
|
|
1790
|
+
@db.typecast_value(:integer, "000013").must_equal 13
|
|
1791
|
+
@db.typecast_value(:integer, "000008").must_equal 8
|
|
1783
1792
|
end
|
|
1784
1793
|
|
|
1785
|
-
|
|
1786
|
-
@db.typecast_value(:integer, "0x013").
|
|
1787
|
-
@db.typecast_value(:integer, "0x80").
|
|
1794
|
+
it "should handle integers with leading 0x as base 16" do
|
|
1795
|
+
@db.typecast_value(:integer, "0x013").must_equal 19
|
|
1796
|
+
@db.typecast_value(:integer, "0x80").must_equal 128
|
|
1788
1797
|
end
|
|
1789
1798
|
|
|
1790
|
-
|
|
1799
|
+
it "should typecast blobs as as Sequel::SQL::Blob" do
|
|
1791
1800
|
v = @db.typecast_value(:blob, "0x013")
|
|
1792
|
-
v.
|
|
1793
|
-
v.
|
|
1794
|
-
@db.typecast_value(:blob, v).object_id.
|
|
1795
|
-
end
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
@db.typecast_value(:boolean, false).
|
|
1799
|
-
@db.typecast_value(:boolean, 0).
|
|
1800
|
-
@db.typecast_value(:boolean, "0").
|
|
1801
|
-
@db.typecast_value(:boolean, 'f').
|
|
1802
|
-
@db.typecast_value(:boolean, 'false').
|
|
1803
|
-
@db.typecast_value(:boolean, true).
|
|
1804
|
-
@db.typecast_value(:boolean, 1).
|
|
1805
|
-
@db.typecast_value(:boolean, '1').
|
|
1806
|
-
@db.typecast_value(:boolean, 't').
|
|
1807
|
-
@db.typecast_value(:boolean, 'true').
|
|
1808
|
-
@db.typecast_value(:boolean, '').
|
|
1809
|
-
end
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
@db.typecast_value(:date, Date.today).
|
|
1813
|
-
@db.typecast_value(:date, DateTime.now).
|
|
1814
|
-
@db.typecast_value(:date, Time.now).
|
|
1815
|
-
@db.typecast_value(:date, Date.today.to_s).
|
|
1816
|
-
@db.typecast_value(:date, :year=>Date.today.year, :month=>Date.today.month, :day=>Date.today.day).
|
|
1817
|
-
end
|
|
1818
|
-
|
|
1819
|
-
|
|
1801
|
+
v.must_be_kind_of(Sequel::SQL::Blob)
|
|
1802
|
+
v.must_equal Sequel::SQL::Blob.new("0x013")
|
|
1803
|
+
@db.typecast_value(:blob, v).object_id.must_equal v.object_id
|
|
1804
|
+
end
|
|
1805
|
+
|
|
1806
|
+
it "should typecast boolean values to true, false, or nil" do
|
|
1807
|
+
@db.typecast_value(:boolean, false).must_equal false
|
|
1808
|
+
@db.typecast_value(:boolean, 0).must_equal false
|
|
1809
|
+
@db.typecast_value(:boolean, "0").must_equal false
|
|
1810
|
+
@db.typecast_value(:boolean, 'f').must_equal false
|
|
1811
|
+
@db.typecast_value(:boolean, 'false').must_equal false
|
|
1812
|
+
@db.typecast_value(:boolean, true).must_equal true
|
|
1813
|
+
@db.typecast_value(:boolean, 1).must_equal true
|
|
1814
|
+
@db.typecast_value(:boolean, '1').must_equal true
|
|
1815
|
+
@db.typecast_value(:boolean, 't').must_equal true
|
|
1816
|
+
@db.typecast_value(:boolean, 'true').must_equal true
|
|
1817
|
+
@db.typecast_value(:boolean, '').must_equal nil
|
|
1818
|
+
end
|
|
1819
|
+
|
|
1820
|
+
it "should typecast date values to Date" do
|
|
1821
|
+
@db.typecast_value(:date, Date.today).must_equal Date.today
|
|
1822
|
+
@db.typecast_value(:date, DateTime.now).must_equal Date.today
|
|
1823
|
+
@db.typecast_value(:date, Time.now).must_equal Date.today
|
|
1824
|
+
@db.typecast_value(:date, Date.today.to_s).must_equal Date.today
|
|
1825
|
+
@db.typecast_value(:date, :year=>Date.today.year, :month=>Date.today.month, :day=>Date.today.day).must_equal Date.today
|
|
1826
|
+
end
|
|
1827
|
+
|
|
1828
|
+
it "should have Sequel.application_to_database_timestamp convert to Sequel.database_timezone" do
|
|
1820
1829
|
begin
|
|
1821
1830
|
t = Time.utc(2011, 1, 2, 3, 4, 5) # UTC Time
|
|
1822
1831
|
t2 = Time.mktime(2011, 1, 2, 3, 4, 5) # Local Time
|
|
@@ -1824,42 +1833,42 @@ describe "Database#typecast_value" do
|
|
|
1824
1833
|
t4 = Time.mktime(2011, 1, 2, 3, 4, 5) + (t - t2) # UTC Time in Local Time
|
|
1825
1834
|
Sequel.application_timezone = :utc
|
|
1826
1835
|
Sequel.database_timezone = :local
|
|
1827
|
-
Sequel.application_to_database_timestamp(t).
|
|
1836
|
+
Sequel.application_to_database_timestamp(t).must_equal t4
|
|
1828
1837
|
Sequel.application_timezone = :local
|
|
1829
1838
|
Sequel.database_timezone = :utc
|
|
1830
|
-
Sequel.application_to_database_timestamp(t2).
|
|
1839
|
+
Sequel.application_to_database_timestamp(t2).must_equal t3
|
|
1831
1840
|
ensure
|
|
1832
1841
|
Sequel.default_timezone = nil
|
|
1833
1842
|
end
|
|
1834
1843
|
end
|
|
1835
1844
|
|
|
1836
|
-
|
|
1845
|
+
it "should have Database#to_application_timestamp convert values using the database's timezone" do
|
|
1837
1846
|
begin
|
|
1838
1847
|
t = Time.utc(2011, 1, 2, 3, 4, 5) # UTC Time
|
|
1839
1848
|
t2 = Time.mktime(2011, 1, 2, 3, 4, 5) # Local Time
|
|
1840
1849
|
t3 = Time.utc(2011, 1, 2, 3, 4, 5) - (t - t2) # Local Time in UTC Time
|
|
1841
1850
|
t4 = Time.mktime(2011, 1, 2, 3, 4, 5) + (t - t2) # UTC Time in Local Time
|
|
1842
1851
|
Sequel.default_timezone = :utc
|
|
1843
|
-
@db.to_application_timestamp('2011-01-02 03:04:05').
|
|
1852
|
+
@db.to_application_timestamp('2011-01-02 03:04:05').must_equal t
|
|
1844
1853
|
Sequel.database_timezone = :local
|
|
1845
|
-
@db.to_application_timestamp('2011-01-02 03:04:05').
|
|
1854
|
+
@db.to_application_timestamp('2011-01-02 03:04:05').must_equal t3
|
|
1846
1855
|
Sequel.default_timezone = :local
|
|
1847
|
-
@db.to_application_timestamp('2011-01-02 03:04:05').
|
|
1856
|
+
@db.to_application_timestamp('2011-01-02 03:04:05').must_equal t2
|
|
1848
1857
|
Sequel.database_timezone = :utc
|
|
1849
|
-
@db.to_application_timestamp('2011-01-02 03:04:05').
|
|
1858
|
+
@db.to_application_timestamp('2011-01-02 03:04:05').must_equal t4
|
|
1850
1859
|
|
|
1851
1860
|
Sequel.default_timezone = :utc
|
|
1852
1861
|
@db.timezone = :local
|
|
1853
|
-
@db.to_application_timestamp('2011-01-02 03:04:05').
|
|
1862
|
+
@db.to_application_timestamp('2011-01-02 03:04:05').must_equal t3
|
|
1854
1863
|
Sequel.default_timezone = :local
|
|
1855
1864
|
@db.timezone = :utc
|
|
1856
|
-
@db.to_application_timestamp('2011-01-02 03:04:05').
|
|
1865
|
+
@db.to_application_timestamp('2011-01-02 03:04:05').must_equal t4
|
|
1857
1866
|
ensure
|
|
1858
1867
|
Sequel.default_timezone = nil
|
|
1859
1868
|
end
|
|
1860
1869
|
end
|
|
1861
1870
|
|
|
1862
|
-
|
|
1871
|
+
it "should typecast datetime values to Sequel.datetime_class with correct timezone handling" do
|
|
1863
1872
|
t = Time.utc(2011, 1, 2, 3, 4, 5, 500000) # UTC Time
|
|
1864
1873
|
t2 = Time.mktime(2011, 1, 2, 3, 4, 5, 500000) # Local Time
|
|
1865
1874
|
t3 = Time.utc(2011, 1, 2, 3, 4, 5, 500000) - (t - t2) # Local Time in UTC Time
|
|
@@ -1872,40 +1881,40 @@ describe "Database#typecast_value" do
|
|
|
1872
1881
|
dt3 = DateTime.civil(2011, 1, 2, 3, 4, secs) - r2
|
|
1873
1882
|
dt4 = DateTime.civil(2011, 1, 2, 3, 4, secs, r1) + r2
|
|
1874
1883
|
|
|
1875
|
-
t.
|
|
1876
|
-
t2.
|
|
1877
|
-
dt.
|
|
1878
|
-
dt2.
|
|
1884
|
+
t.must_equal t4
|
|
1885
|
+
t2.must_equal t3
|
|
1886
|
+
dt.must_equal dt4
|
|
1887
|
+
dt2.must_equal dt3
|
|
1879
1888
|
|
|
1880
1889
|
check = proc do |i, o|
|
|
1881
1890
|
v = @db.typecast_value(:datetime, i)
|
|
1882
|
-
v.
|
|
1891
|
+
v.must_equal o
|
|
1883
1892
|
if o.is_a?(Time)
|
|
1884
|
-
v.utc_offset.
|
|
1893
|
+
v.utc_offset.must_equal o.utc_offset
|
|
1885
1894
|
else
|
|
1886
|
-
v.offset.
|
|
1895
|
+
v.offset.must_equal o.offset
|
|
1887
1896
|
end
|
|
1888
1897
|
end
|
|
1889
1898
|
@db.extend_datasets(Module.new{def supports_timestamp_timezones?; true; end})
|
|
1890
1899
|
begin
|
|
1891
|
-
@db.typecast_value(:datetime, dt).
|
|
1892
|
-
@db.typecast_value(:datetime, dt2).
|
|
1893
|
-
@db.typecast_value(:datetime, t).
|
|
1894
|
-
@db.typecast_value(:datetime, t2).
|
|
1895
|
-
@db.typecast_value(:datetime, @db.literal(dt)[1...-1]).
|
|
1896
|
-
@db.typecast_value(:datetime, dt.strftime('%F %T.%N')).
|
|
1897
|
-
@db.typecast_value(:datetime, Date.civil(2011, 1, 2)).
|
|
1898
|
-
@db.typecast_value(:datetime, :year=>dt.year, :month=>dt.month, :day=>dt.day, :hour=>dt.hour, :minute=>dt.min, :second=>dt.sec, :nanos=>500000000).
|
|
1900
|
+
@db.typecast_value(:datetime, dt).must_equal t
|
|
1901
|
+
@db.typecast_value(:datetime, dt2).must_equal t2
|
|
1902
|
+
@db.typecast_value(:datetime, t).must_equal t
|
|
1903
|
+
@db.typecast_value(:datetime, t2).must_equal t2
|
|
1904
|
+
@db.typecast_value(:datetime, @db.literal(dt)[1...-1]).must_equal t
|
|
1905
|
+
@db.typecast_value(:datetime, dt.strftime('%F %T.%N')).must_equal t2
|
|
1906
|
+
@db.typecast_value(:datetime, Date.civil(2011, 1, 2)).must_equal Time.mktime(2011, 1, 2, 0, 0, 0)
|
|
1907
|
+
@db.typecast_value(:datetime, :year=>dt.year, :month=>dt.month, :day=>dt.day, :hour=>dt.hour, :minute=>dt.min, :second=>dt.sec, :nanos=>500000000).must_equal t2
|
|
1899
1908
|
|
|
1900
1909
|
Sequel.datetime_class = DateTime
|
|
1901
|
-
@db.typecast_value(:datetime, dt).
|
|
1902
|
-
@db.typecast_value(:datetime, dt2).
|
|
1903
|
-
@db.typecast_value(:datetime, t).
|
|
1904
|
-
@db.typecast_value(:datetime, t2).
|
|
1905
|
-
@db.typecast_value(:datetime, @db.literal(dt)[1...-1]).
|
|
1906
|
-
@db.typecast_value(:datetime, dt.strftime('%F %T.%N')).
|
|
1907
|
-
@db.typecast_value(:datetime, Date.civil(2011, 1, 2)).
|
|
1908
|
-
@db.typecast_value(:datetime, :year=>dt.year, :month=>dt.month, :day=>dt.day, :hour=>dt.hour, :minute=>dt.min, :second=>dt.sec, :nanos=>500000000).
|
|
1910
|
+
@db.typecast_value(:datetime, dt).must_equal dt
|
|
1911
|
+
@db.typecast_value(:datetime, dt2).must_equal dt2
|
|
1912
|
+
@db.typecast_value(:datetime, t).must_equal dt
|
|
1913
|
+
@db.typecast_value(:datetime, t2).must_equal dt2
|
|
1914
|
+
@db.typecast_value(:datetime, @db.literal(dt)[1...-1]).must_equal dt
|
|
1915
|
+
@db.typecast_value(:datetime, dt.strftime('%F %T.%N')).must_equal dt
|
|
1916
|
+
@db.typecast_value(:datetime, Date.civil(2011, 1, 2)).must_equal DateTime.civil(2011, 1, 2, 0, 0, 0)
|
|
1917
|
+
@db.typecast_value(:datetime, :year=>dt.year, :month=>dt.month, :day=>dt.day, :hour=>dt.hour, :minute=>dt.min, :second=>dt.sec, :nanos=>500000000).must_equal dt
|
|
1909
1918
|
|
|
1910
1919
|
Sequel.application_timezone = :utc
|
|
1911
1920
|
Sequel.typecast_timezone = :local
|
|
@@ -1998,366 +2007,366 @@ describe "Database#typecast_value" do
|
|
|
1998
2007
|
end
|
|
1999
2008
|
end
|
|
2000
2009
|
|
|
2001
|
-
|
|
2010
|
+
it "should handle arrays when typecasting timestamps" do
|
|
2002
2011
|
begin
|
|
2003
|
-
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14]).
|
|
2004
|
-
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14, 500000000]).
|
|
2012
|
+
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14]).must_equal Time.local(2011, 10, 11, 12, 13, 14)
|
|
2013
|
+
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14, 500000000]).must_equal Time.local(2011, 10, 11, 12, 13, 14, 500000)
|
|
2005
2014
|
|
|
2006
2015
|
Sequel.datetime_class = DateTime
|
|
2007
|
-
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14]).
|
|
2008
|
-
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14, 500000000]).
|
|
2009
|
-
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14, 500000000, (defined?(Rational) ? Rational(1, 2) : 0.5)]).
|
|
2016
|
+
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14]).must_equal DateTime.civil(2011, 10, 11, 12, 13, 14)
|
|
2017
|
+
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14, 500000000]).must_equal DateTime.civil(2011, 10, 11, 12, 13, (defined?(Rational) ? Rational(29, 2) : 14.5))
|
|
2018
|
+
@db.typecast_value(:datetime, [2011, 10, 11, 12, 13, 14, 500000000, (defined?(Rational) ? Rational(1, 2) : 0.5)]).must_equal DateTime.civil(2011, 10, 11, 12, 13, (defined?(Rational) ? Rational(29, 2) : 14.5), (defined?(Rational) ? Rational(1, 2) : 0.5))
|
|
2010
2019
|
ensure
|
|
2011
2020
|
Sequel.datetime_class = Time
|
|
2012
2021
|
end
|
|
2013
2022
|
end
|
|
2014
2023
|
|
|
2015
|
-
|
|
2024
|
+
it "should handle hashes when typecasting timestamps" do
|
|
2016
2025
|
begin
|
|
2017
|
-
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14).
|
|
2018
|
-
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14, :nanos=>500000000).
|
|
2019
|
-
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14).
|
|
2020
|
-
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14, 'nanos'=>500000000).
|
|
2026
|
+
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14).must_equal Time.local(2011, 10, 11, 12, 13, 14)
|
|
2027
|
+
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14, :nanos=>500000000).must_equal Time.local(2011, 10, 11, 12, 13, 14, 500000)
|
|
2028
|
+
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14).must_equal Time.local(2011, 10, 11, 12, 13, 14)
|
|
2029
|
+
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14, 'nanos'=>500000000).must_equal Time.local(2011, 10, 11, 12, 13, 14, 500000)
|
|
2021
2030
|
|
|
2022
2031
|
Sequel.datetime_class = DateTime
|
|
2023
|
-
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14).
|
|
2024
|
-
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14, :nanos=>500000000).
|
|
2025
|
-
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14).
|
|
2026
|
-
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14, 'nanos'=>500000000).
|
|
2027
|
-
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14, :offset=>(defined?(Rational) ? Rational(1, 2) : 0.5)).
|
|
2028
|
-
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14, :nanos=>500000000, :offset=>(defined?(Rational) ? Rational(1, 2) : 0.5)).
|
|
2029
|
-
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14, 'offset'=>(defined?(Rational) ? Rational(1, 2) : 0.5)).
|
|
2030
|
-
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14, 'nanos'=>500000000, 'offset'=>(defined?(Rational) ? Rational(1, 2) : 0.5)).
|
|
2032
|
+
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14).must_equal DateTime.civil(2011, 10, 11, 12, 13, 14)
|
|
2033
|
+
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14, :nanos=>500000000).must_equal DateTime.civil(2011, 10, 11, 12, 13, (defined?(Rational) ? Rational(29, 2) : 14.5))
|
|
2034
|
+
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14).must_equal DateTime.civil(2011, 10, 11, 12, 13, 14)
|
|
2035
|
+
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14, 'nanos'=>500000000).must_equal DateTime.civil(2011, 10, 11, 12, 13, (defined?(Rational) ? Rational(29, 2) : 14.5))
|
|
2036
|
+
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14, :offset=>(defined?(Rational) ? Rational(1, 2) : 0.5)).must_equal DateTime.civil(2011, 10, 11, 12, 13, 14, (defined?(Rational) ? Rational(1, 2) : 0.5))
|
|
2037
|
+
@db.typecast_value(:datetime, :year=>2011, :month=>10, :day=>11, :hour=>12, :minute=>13, :second=>14, :nanos=>500000000, :offset=>(defined?(Rational) ? Rational(1, 2) : 0.5)).must_equal DateTime.civil(2011, 10, 11, 12, 13, (defined?(Rational) ? Rational(29, 2) : 14.5), (defined?(Rational) ? Rational(1, 2) : 0.5))
|
|
2038
|
+
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14, 'offset'=>(defined?(Rational) ? Rational(1, 2) : 0.5)).must_equal DateTime.civil(2011, 10, 11, 12, 13, 14, (defined?(Rational) ? Rational(1, 2) : 0.5))
|
|
2039
|
+
@db.typecast_value(:datetime, 'year'=>2011, 'month'=>10, 'day'=>11, 'hour'=>12, 'minute'=>13, 'second'=>14, 'nanos'=>500000000, 'offset'=>(defined?(Rational) ? Rational(1, 2) : 0.5)).must_equal DateTime.civil(2011, 10, 11, 12, 13, (defined?(Rational) ? Rational(29, 2) : 14.5), (defined?(Rational) ? Rational(1, 2) : 0.5))
|
|
2031
2040
|
ensure
|
|
2032
2041
|
Sequel.datetime_class = Time
|
|
2033
2042
|
end
|
|
2034
2043
|
end
|
|
2035
2044
|
|
|
2036
|
-
|
|
2045
|
+
it "should typecast decimal values to BigDecimal" do
|
|
2037
2046
|
[1.0, 1, '1.0', BigDecimal('1.0')].each do |i|
|
|
2038
2047
|
v = @db.typecast_value(:decimal, i)
|
|
2039
|
-
v.
|
|
2040
|
-
v.
|
|
2048
|
+
v.must_be_kind_of(BigDecimal)
|
|
2049
|
+
v.must_equal BigDecimal.new('1.0')
|
|
2041
2050
|
end
|
|
2042
2051
|
end
|
|
2043
2052
|
|
|
2044
|
-
|
|
2053
|
+
it "should typecast float values to Float" do
|
|
2045
2054
|
[1.0, 1, '1.0', BigDecimal('1.0')].each do |i|
|
|
2046
2055
|
v = @db.typecast_value(:float, i)
|
|
2047
|
-
v.
|
|
2048
|
-
v.
|
|
2056
|
+
v.must_be_kind_of(Float)
|
|
2057
|
+
v.must_equal 1.0
|
|
2049
2058
|
end
|
|
2050
2059
|
end
|
|
2051
2060
|
|
|
2052
|
-
|
|
2061
|
+
it "should typecast string values to String" do
|
|
2053
2062
|
[1.0, '1.0', Sequel.blob('1.0')].each do |i|
|
|
2054
2063
|
v = @db.typecast_value(:string, i)
|
|
2055
|
-
v.
|
|
2056
|
-
v.
|
|
2064
|
+
v.must_be_instance_of(String)
|
|
2065
|
+
v.must_equal "1.0"
|
|
2057
2066
|
end
|
|
2058
2067
|
end
|
|
2059
2068
|
|
|
2060
|
-
|
|
2069
|
+
it "should raise errors when typecasting hash and array values to String" do
|
|
2061
2070
|
[[], {}].each do |i|
|
|
2062
|
-
proc{@db.typecast_value(:string, i)}.
|
|
2071
|
+
proc{@db.typecast_value(:string, i)}.must_raise(Sequel::InvalidValue)
|
|
2063
2072
|
end
|
|
2064
2073
|
end
|
|
2065
2074
|
|
|
2066
|
-
|
|
2075
|
+
it "should typecast time values to SQLTime" do
|
|
2067
2076
|
t = Time.now
|
|
2068
2077
|
st = Sequel::SQLTime.local(t.year, t.month, t.day, 1, 2, 3)
|
|
2069
2078
|
[st, Time.utc(t.year, t.month, t.day, 1, 2, 3), Time.local(t.year, t.month, t.day, 1, 2, 3), '01:02:03', {:hour=>1, :minute=>2, :second=>3}].each do |i|
|
|
2070
2079
|
v = @db.typecast_value(:time, i)
|
|
2071
|
-
v.
|
|
2072
|
-
v.
|
|
2080
|
+
v.must_be_instance_of(Sequel::SQLTime)
|
|
2081
|
+
v.must_equal st
|
|
2073
2082
|
end
|
|
2074
2083
|
end
|
|
2075
2084
|
|
|
2076
|
-
|
|
2085
|
+
it "should correctly handle time value conversion to SQLTime with fractional seconds" do
|
|
2077
2086
|
t = Time.now
|
|
2078
2087
|
st = Sequel::SQLTime.local(t.year, t.month, t.day, 1, 2, 3, 500000)
|
|
2079
2088
|
t = Time.local(t.year, t.month, t.day, 1, 2, 3, 500000)
|
|
2080
|
-
@db.typecast_value(:time, t).
|
|
2089
|
+
@db.typecast_value(:time, t).must_equal st
|
|
2081
2090
|
end
|
|
2082
2091
|
|
|
2083
|
-
|
|
2092
|
+
it "should have an underlying exception class available at wrapped_exception" do
|
|
2084
2093
|
begin
|
|
2085
2094
|
@db.typecast_value(:date, 'a')
|
|
2086
|
-
true.
|
|
2095
|
+
true.must_equal false
|
|
2087
2096
|
rescue Sequel::InvalidValue => e
|
|
2088
|
-
e.wrapped_exception.
|
|
2097
|
+
e.wrapped_exception.must_be_kind_of(ArgumentError)
|
|
2089
2098
|
end
|
|
2090
2099
|
end
|
|
2091
2100
|
|
|
2092
|
-
|
|
2101
|
+
it "should include underlying exception class in #inspect" do
|
|
2093
2102
|
begin
|
|
2094
2103
|
@db.typecast_value(:date, 'a')
|
|
2095
|
-
true.
|
|
2104
|
+
true.must_equal false
|
|
2096
2105
|
rescue Sequel::InvalidValue => e
|
|
2097
|
-
e.inspect.
|
|
2106
|
+
e.inspect.must_match(/\A#<Sequel::InvalidValue: ArgumentError: .*>\z/)
|
|
2098
2107
|
end
|
|
2099
2108
|
end
|
|
2100
2109
|
end
|
|
2101
2110
|
|
|
2102
2111
|
describe "Database#blank_object?" do
|
|
2103
|
-
|
|
2112
|
+
it "should return whether the object is considered blank" do
|
|
2104
2113
|
db = Sequel::Database.new
|
|
2105
2114
|
c = lambda{|meth, value| Class.new{define_method(meth){value}}.new}
|
|
2106
2115
|
|
|
2107
|
-
db.send(:blank_object?, "").
|
|
2108
|
-
db.send(:blank_object?, " ").
|
|
2109
|
-
db.send(:blank_object?, nil).
|
|
2110
|
-
db.send(:blank_object?, false).
|
|
2111
|
-
db.send(:blank_object?, []).
|
|
2112
|
-
db.send(:blank_object?, {}).
|
|
2113
|
-
db.send(:blank_object?, c[:empty?, true]).
|
|
2114
|
-
db.send(:blank_object?, c[:blank?, true]).
|
|
2116
|
+
db.send(:blank_object?, "").must_equal true
|
|
2117
|
+
db.send(:blank_object?, " ").must_equal true
|
|
2118
|
+
db.send(:blank_object?, nil).must_equal true
|
|
2119
|
+
db.send(:blank_object?, false).must_equal true
|
|
2120
|
+
db.send(:blank_object?, []).must_equal true
|
|
2121
|
+
db.send(:blank_object?, {}).must_equal true
|
|
2122
|
+
db.send(:blank_object?, c[:empty?, true]).must_equal true
|
|
2123
|
+
db.send(:blank_object?, c[:blank?, true]).must_equal true
|
|
2115
2124
|
|
|
2116
|
-
db.send(:blank_object?, " a ").
|
|
2117
|
-
db.send(:blank_object?, 1).
|
|
2118
|
-
db.send(:blank_object?, 1.0).
|
|
2119
|
-
db.send(:blank_object?, true).
|
|
2120
|
-
db.send(:blank_object?, [1]).
|
|
2121
|
-
db.send(:blank_object?, {1.0=>2.0}).
|
|
2122
|
-
db.send(:blank_object?, c[:empty?, false]).
|
|
2123
|
-
db.send(:blank_object?, c[:blank?, false]).
|
|
2125
|
+
db.send(:blank_object?, " a ").must_equal false
|
|
2126
|
+
db.send(:blank_object?, 1).must_equal false
|
|
2127
|
+
db.send(:blank_object?, 1.0).must_equal false
|
|
2128
|
+
db.send(:blank_object?, true).must_equal false
|
|
2129
|
+
db.send(:blank_object?, [1]).must_equal false
|
|
2130
|
+
db.send(:blank_object?, {1.0=>2.0}).must_equal false
|
|
2131
|
+
db.send(:blank_object?, c[:empty?, false]).must_equal false
|
|
2132
|
+
db.send(:blank_object?, c[:blank?, false]).must_equal false
|
|
2124
2133
|
end
|
|
2125
2134
|
end
|
|
2126
2135
|
|
|
2127
2136
|
describe "Database#schema_autoincrementing_primary_key?" do
|
|
2128
|
-
|
|
2137
|
+
it "should indicate whether the parsed schema row indicates a primary key" do
|
|
2129
2138
|
m = Sequel::Database.new.method(:schema_autoincrementing_primary_key?)
|
|
2130
|
-
m.call(:primary_key=>true, :auto_increment=>true).
|
|
2131
|
-
m.call(:primary_key=>true, :auto_increment=>false).
|
|
2132
|
-
m.call(:primary_key=>false).
|
|
2139
|
+
m.call(:primary_key=>true, :auto_increment=>true).must_equal true
|
|
2140
|
+
m.call(:primary_key=>true, :auto_increment=>false).must_equal false
|
|
2141
|
+
m.call(:primary_key=>false).must_equal false
|
|
2133
2142
|
end
|
|
2134
2143
|
end
|
|
2135
2144
|
|
|
2136
2145
|
describe "Database#supports_schema_parsing?" do
|
|
2137
|
-
|
|
2138
|
-
Sequel::Database.new.supports_schema_parsing?.
|
|
2146
|
+
it "should be false by default" do
|
|
2147
|
+
Sequel::Database.new.supports_schema_parsing?.must_equal false
|
|
2139
2148
|
end
|
|
2140
2149
|
|
|
2141
|
-
|
|
2150
|
+
it "should be true if the database implements schema_parse_table" do
|
|
2142
2151
|
db = Sequel::Database.new
|
|
2143
2152
|
def db.schema_parse_table(*) end
|
|
2144
|
-
db.supports_schema_parsing?.
|
|
2153
|
+
db.supports_schema_parsing?.must_equal true
|
|
2145
2154
|
end
|
|
2146
2155
|
end
|
|
2147
2156
|
|
|
2148
2157
|
describe "Database#supports_foreign_key_parsing?" do
|
|
2149
|
-
|
|
2150
|
-
Sequel::Database.new.supports_foreign_key_parsing?.
|
|
2158
|
+
it "should be false by default" do
|
|
2159
|
+
Sequel::Database.new.supports_foreign_key_parsing?.must_equal false
|
|
2151
2160
|
end
|
|
2152
2161
|
|
|
2153
|
-
|
|
2162
|
+
it "should be true if the database implements foreign_key_list" do
|
|
2154
2163
|
db = Sequel::Database.new
|
|
2155
2164
|
def db.foreign_key_list(*) end
|
|
2156
|
-
db.supports_foreign_key_parsing?.
|
|
2165
|
+
db.supports_foreign_key_parsing?.must_equal true
|
|
2157
2166
|
end
|
|
2158
2167
|
end
|
|
2159
2168
|
|
|
2160
2169
|
describe "Database#supports_index_parsing?" do
|
|
2161
|
-
|
|
2162
|
-
Sequel::Database.new.supports_index_parsing?.
|
|
2170
|
+
it "should be false by default" do
|
|
2171
|
+
Sequel::Database.new.supports_index_parsing?.must_equal false
|
|
2163
2172
|
end
|
|
2164
2173
|
|
|
2165
|
-
|
|
2174
|
+
it "should be true if the database implements indexes" do
|
|
2166
2175
|
db = Sequel::Database.new
|
|
2167
2176
|
def db.indexes(*) end
|
|
2168
|
-
db.supports_index_parsing?.
|
|
2177
|
+
db.supports_index_parsing?.must_equal true
|
|
2169
2178
|
end
|
|
2170
2179
|
end
|
|
2171
2180
|
|
|
2172
2181
|
describe "Database#supports_table_listing?" do
|
|
2173
|
-
|
|
2174
|
-
Sequel::Database.new.supports_table_listing?.
|
|
2182
|
+
it "should be false by default" do
|
|
2183
|
+
Sequel::Database.new.supports_table_listing?.must_equal false
|
|
2175
2184
|
end
|
|
2176
2185
|
|
|
2177
|
-
|
|
2186
|
+
it "should be true if the database implements tables" do
|
|
2178
2187
|
db = Sequel::Database.new
|
|
2179
2188
|
def db.tables(*) end
|
|
2180
|
-
db.supports_table_listing?.
|
|
2189
|
+
db.supports_table_listing?.must_equal true
|
|
2181
2190
|
end
|
|
2182
2191
|
end
|
|
2183
2192
|
|
|
2184
2193
|
describe "Database#supports_view_listing?" do
|
|
2185
|
-
|
|
2186
|
-
Sequel::Database.new.supports_view_listing?.
|
|
2194
|
+
it "should be false by default" do
|
|
2195
|
+
Sequel::Database.new.supports_view_listing?.must_equal false
|
|
2187
2196
|
end
|
|
2188
2197
|
|
|
2189
|
-
|
|
2198
|
+
it "should be true if the database implements views" do
|
|
2190
2199
|
db = Sequel::Database.new
|
|
2191
2200
|
def db.views(*) end
|
|
2192
|
-
db.supports_view_listing?.
|
|
2201
|
+
db.supports_view_listing?.must_equal true
|
|
2193
2202
|
end
|
|
2194
2203
|
end
|
|
2195
2204
|
|
|
2196
2205
|
describe "Database#supports_deferrable_constraints?" do
|
|
2197
|
-
|
|
2198
|
-
Sequel::Database.new.supports_deferrable_constraints?.
|
|
2206
|
+
it "should be false by default" do
|
|
2207
|
+
Sequel::Database.new.supports_deferrable_constraints?.must_equal false
|
|
2199
2208
|
end
|
|
2200
2209
|
end
|
|
2201
2210
|
|
|
2202
2211
|
describe "Database#supports_deferrable_foreign_key_constraints?" do
|
|
2203
|
-
|
|
2204
|
-
Sequel::Database.new.supports_deferrable_foreign_key_constraints?.
|
|
2212
|
+
it "should be false by default" do
|
|
2213
|
+
Sequel::Database.new.supports_deferrable_foreign_key_constraints?.must_equal false
|
|
2205
2214
|
end
|
|
2206
2215
|
end
|
|
2207
2216
|
|
|
2208
2217
|
describe "Database#supports_transactional_ddl?" do
|
|
2209
|
-
|
|
2210
|
-
Sequel::Database.new.supports_transactional_ddl?.
|
|
2218
|
+
it "should be false by default" do
|
|
2219
|
+
Sequel::Database.new.supports_transactional_ddl?.must_equal false
|
|
2211
2220
|
end
|
|
2212
2221
|
end
|
|
2213
2222
|
|
|
2214
2223
|
describe "Database#global_index_namespace?" do
|
|
2215
|
-
|
|
2216
|
-
Sequel::Database.new.global_index_namespace?.
|
|
2224
|
+
it "should be true by default" do
|
|
2225
|
+
Sequel::Database.new.global_index_namespace?.must_equal true
|
|
2217
2226
|
end
|
|
2218
2227
|
end
|
|
2219
2228
|
|
|
2220
2229
|
describe "Database#supports_savepoints?" do
|
|
2221
|
-
|
|
2222
|
-
Sequel::Database.new.supports_savepoints?.
|
|
2230
|
+
it "should be false by default" do
|
|
2231
|
+
Sequel::Database.new.supports_savepoints?.must_equal false
|
|
2223
2232
|
end
|
|
2224
2233
|
end
|
|
2225
2234
|
|
|
2226
2235
|
describe "Database#supports_views_with_check_option?" do
|
|
2227
|
-
|
|
2228
|
-
Sequel::Database.new.supports_views_with_check_option?.
|
|
2236
|
+
it "should be false by default" do
|
|
2237
|
+
Sequel::Database.new.supports_views_with_check_option?.must_equal false
|
|
2229
2238
|
end
|
|
2230
2239
|
end
|
|
2231
2240
|
|
|
2232
2241
|
describe "Database#supports_views_with_local_check_option?" do
|
|
2233
|
-
|
|
2234
|
-
Sequel::Database.new.supports_views_with_local_check_option?.
|
|
2242
|
+
it "should be false by default" do
|
|
2243
|
+
Sequel::Database.new.supports_views_with_local_check_option?.must_equal false
|
|
2235
2244
|
end
|
|
2236
2245
|
end
|
|
2237
2246
|
|
|
2238
2247
|
describe "Database#supports_savepoints_in_prepared_transactions?" do
|
|
2239
|
-
|
|
2240
|
-
Sequel::Database.new.supports_savepoints_in_prepared_transactions?.
|
|
2248
|
+
it "should be false by default" do
|
|
2249
|
+
Sequel::Database.new.supports_savepoints_in_prepared_transactions?.must_equal false
|
|
2241
2250
|
end
|
|
2242
2251
|
|
|
2243
|
-
|
|
2252
|
+
it "should be true if both savepoints and prepared transactions are supported" do
|
|
2244
2253
|
db = Sequel::Database.new
|
|
2245
2254
|
meta_def(db, :supports_savepoints?){true}
|
|
2246
2255
|
meta_def(db, :supports_prepared_transactions?){true}
|
|
2247
|
-
db.supports_savepoints_in_prepared_transactions?.
|
|
2256
|
+
db.supports_savepoints_in_prepared_transactions?.must_equal true
|
|
2248
2257
|
end
|
|
2249
2258
|
end
|
|
2250
2259
|
|
|
2251
2260
|
describe "Database#supports_prepared_transactions?" do
|
|
2252
|
-
|
|
2253
|
-
Sequel::Database.new.supports_prepared_transactions?.
|
|
2261
|
+
it "should be false by default" do
|
|
2262
|
+
Sequel::Database.new.supports_prepared_transactions?.must_equal false
|
|
2254
2263
|
end
|
|
2255
2264
|
end
|
|
2256
2265
|
|
|
2257
2266
|
describe "Database#supports_transaction_isolation_levels?" do
|
|
2258
|
-
|
|
2259
|
-
Sequel::Database.new.supports_transaction_isolation_levels?.
|
|
2267
|
+
it "should be false by default" do
|
|
2268
|
+
Sequel::Database.new.supports_transaction_isolation_levels?.must_equal false
|
|
2260
2269
|
end
|
|
2261
2270
|
end
|
|
2262
2271
|
|
|
2263
2272
|
describe "Database#input_identifier_meth" do
|
|
2264
|
-
|
|
2273
|
+
it "should be the input_identifer method of a default dataset for this database" do
|
|
2265
2274
|
db = Sequel::Database.new
|
|
2266
|
-
db.send(:input_identifier_meth).call(:a).
|
|
2275
|
+
db.send(:input_identifier_meth).call(:a).must_equal 'a'
|
|
2267
2276
|
db.identifier_input_method = :upcase
|
|
2268
|
-
db.send(:input_identifier_meth).call(:a).
|
|
2277
|
+
db.send(:input_identifier_meth).call(:a).must_equal 'A'
|
|
2269
2278
|
end
|
|
2270
2279
|
end
|
|
2271
2280
|
|
|
2272
2281
|
describe "Database#output_identifier_meth" do
|
|
2273
|
-
|
|
2282
|
+
it "should be the output_identifer method of a default dataset for this database" do
|
|
2274
2283
|
db = Sequel::Database.new
|
|
2275
|
-
db.send(:output_identifier_meth).call('A').
|
|
2284
|
+
db.send(:output_identifier_meth).call('A').must_equal :A
|
|
2276
2285
|
db.identifier_output_method = :downcase
|
|
2277
|
-
db.send(:output_identifier_meth).call('A').
|
|
2286
|
+
db.send(:output_identifier_meth).call('A').must_equal :a
|
|
2278
2287
|
end
|
|
2279
2288
|
end
|
|
2280
2289
|
|
|
2281
2290
|
describe "Database#metadata_dataset" do
|
|
2282
|
-
|
|
2291
|
+
it "should be a dataset with the default settings for identifier_input_method and identifier_output_method" do
|
|
2283
2292
|
ds = Sequel::Database.new.send(:metadata_dataset)
|
|
2284
|
-
ds.literal(:a).
|
|
2285
|
-
ds.send(:output_identifier, 'A').
|
|
2293
|
+
ds.literal(:a).must_equal 'A'
|
|
2294
|
+
ds.send(:output_identifier, 'A').must_equal :a
|
|
2286
2295
|
end
|
|
2287
2296
|
end
|
|
2288
2297
|
|
|
2289
2298
|
describe "Database#column_schema_to_ruby_default" do
|
|
2290
|
-
|
|
2299
|
+
it "should handle converting many default formats" do
|
|
2291
2300
|
db = Sequel::Database.new
|
|
2292
2301
|
p = lambda{|d,t| db.send(:column_schema_to_ruby_default, d, t)}
|
|
2293
|
-
p[nil, :integer].
|
|
2294
|
-
p[1, :integer].
|
|
2295
|
-
p['1', :integer].
|
|
2296
|
-
p['-1', :integer].
|
|
2297
|
-
p[1.0, :float].
|
|
2298
|
-
p['1.0', :float].
|
|
2299
|
-
p['-1.0', :float].
|
|
2300
|
-
p['1.0', :decimal].
|
|
2301
|
-
p['-1.0', :decimal].
|
|
2302
|
-
p[true, :boolean].
|
|
2303
|
-
p[false, :boolean].
|
|
2304
|
-
p['1', :boolean].
|
|
2305
|
-
p['0', :boolean].
|
|
2306
|
-
p['true', :boolean].
|
|
2307
|
-
p['false', :boolean].
|
|
2308
|
-
p["'t'", :boolean].
|
|
2309
|
-
p["'f'", :boolean].
|
|
2310
|
-
p["'a'", :string].
|
|
2311
|
-
p["'a'", :blob].
|
|
2312
|
-
p["'a'", :blob].
|
|
2313
|
-
p["''", :string].
|
|
2314
|
-
p["'\\a''b'", :string].
|
|
2315
|
-
p["'NULL'", :string].
|
|
2316
|
-
p[Date.today, :date].
|
|
2317
|
-
p["'2009-10-29'", :date].
|
|
2318
|
-
p["CURRENT_TIMESTAMP", :date].
|
|
2319
|
-
p["CURRENT_DATE", :date].
|
|
2320
|
-
p["now()", :date].
|
|
2321
|
-
p["getdate()", :date].
|
|
2322
|
-
p["CURRENT_TIMESTAMP", :datetime].
|
|
2323
|
-
p["CURRENT_DATE", :datetime].
|
|
2324
|
-
p["now()", :datetime].
|
|
2325
|
-
p["getdate()", :datetime].
|
|
2326
|
-
p["'2009-10-29T10:20:30-07:00'", :datetime].
|
|
2327
|
-
p["'2009-10-29 10:20:30'", :datetime].
|
|
2328
|
-
p["'10:20:30'", :time].
|
|
2329
|
-
p["NaN", :float].
|
|
2302
|
+
p[nil, :integer].must_equal nil
|
|
2303
|
+
p[1, :integer].must_equal 1
|
|
2304
|
+
p['1', :integer].must_equal 1
|
|
2305
|
+
p['-1', :integer].must_equal(-1)
|
|
2306
|
+
p[1.0, :float].must_equal 1.0
|
|
2307
|
+
p['1.0', :float].must_equal 1.0
|
|
2308
|
+
p['-1.0', :float].must_equal(-1.0)
|
|
2309
|
+
p['1.0', :decimal].must_equal BigDecimal.new('1.0')
|
|
2310
|
+
p['-1.0', :decimal].must_equal BigDecimal.new('-1.0')
|
|
2311
|
+
p[true, :boolean].must_equal true
|
|
2312
|
+
p[false, :boolean].must_equal false
|
|
2313
|
+
p['1', :boolean].must_equal true
|
|
2314
|
+
p['0', :boolean].must_equal false
|
|
2315
|
+
p['true', :boolean].must_equal true
|
|
2316
|
+
p['false', :boolean].must_equal false
|
|
2317
|
+
p["'t'", :boolean].must_equal true
|
|
2318
|
+
p["'f'", :boolean].must_equal false
|
|
2319
|
+
p["'a'", :string].must_equal 'a'
|
|
2320
|
+
p["'a'", :blob].must_equal Sequel.blob('a')
|
|
2321
|
+
p["'a'", :blob].must_be_kind_of(Sequel::SQL::Blob)
|
|
2322
|
+
p["''", :string].must_equal ''
|
|
2323
|
+
p["'\\a''b'", :string].must_equal "\\a'b"
|
|
2324
|
+
p["'NULL'", :string].must_equal "NULL"
|
|
2325
|
+
p[Date.today, :date].must_equal Date.today
|
|
2326
|
+
p["'2009-10-29'", :date].must_equal Date.new(2009,10,29)
|
|
2327
|
+
p["CURRENT_TIMESTAMP", :date].must_equal Sequel::CURRENT_DATE
|
|
2328
|
+
p["CURRENT_DATE", :date].must_equal Sequel::CURRENT_DATE
|
|
2329
|
+
p["now()", :date].must_equal Sequel::CURRENT_DATE
|
|
2330
|
+
p["getdate()", :date].must_equal Sequel::CURRENT_DATE
|
|
2331
|
+
p["CURRENT_TIMESTAMP", :datetime].must_equal Sequel::CURRENT_TIMESTAMP
|
|
2332
|
+
p["CURRENT_DATE", :datetime].must_equal Sequel::CURRENT_TIMESTAMP
|
|
2333
|
+
p["now()", :datetime].must_equal Sequel::CURRENT_TIMESTAMP
|
|
2334
|
+
p["getdate()", :datetime].must_equal Sequel::CURRENT_TIMESTAMP
|
|
2335
|
+
p["'2009-10-29T10:20:30-07:00'", :datetime].must_equal DateTime.parse('2009-10-29T10:20:30-07:00')
|
|
2336
|
+
p["'2009-10-29 10:20:30'", :datetime].must_equal DateTime.parse('2009-10-29 10:20:30')
|
|
2337
|
+
p["'10:20:30'", :time].must_equal Time.parse('10:20:30')
|
|
2338
|
+
p["NaN", :float].must_equal nil
|
|
2330
2339
|
|
|
2331
2340
|
db = Sequel.mock(:host=>'postgres')
|
|
2332
|
-
p["''::text", :string].
|
|
2333
|
-
p["'\\a''b'::character varying", :string].
|
|
2334
|
-
p["'a'::bpchar", :string].
|
|
2335
|
-
p["(-1)", :integer].
|
|
2336
|
-
p["(-1.0)", :float].
|
|
2337
|
-
p['(-1.0)', :decimal].
|
|
2338
|
-
p["'a'::bytea", :blob].
|
|
2339
|
-
p["'a'::bytea", :blob].
|
|
2340
|
-
p["'2009-10-29'::date", :date].
|
|
2341
|
-
p["'2009-10-29 10:20:30.241343'::timestamp without time zone", :datetime].
|
|
2342
|
-
p["'10:20:30'::time without time zone", :time].
|
|
2341
|
+
p["''::text", :string].must_equal ""
|
|
2342
|
+
p["'\\a''b'::character varying", :string].must_equal "\\a'b"
|
|
2343
|
+
p["'a'::bpchar", :string].must_equal "a"
|
|
2344
|
+
p["(-1)", :integer].must_equal(-1)
|
|
2345
|
+
p["(-1.0)", :float].must_equal(-1.0)
|
|
2346
|
+
p['(-1.0)', :decimal].must_equal BigDecimal.new('-1.0')
|
|
2347
|
+
p["'a'::bytea", :blob].must_equal Sequel.blob('a')
|
|
2348
|
+
p["'a'::bytea", :blob].must_be_kind_of(Sequel::SQL::Blob)
|
|
2349
|
+
p["'2009-10-29'::date", :date].must_equal Date.new(2009,10,29)
|
|
2350
|
+
p["'2009-10-29 10:20:30.241343'::timestamp without time zone", :datetime].must_equal DateTime.parse('2009-10-29 10:20:30.241343')
|
|
2351
|
+
p["'10:20:30'::time without time zone", :time].must_equal Time.parse('10:20:30')
|
|
2343
2352
|
|
|
2344
2353
|
db = Sequel.mock(:host=>'mysql')
|
|
2345
|
-
p["\\a'b", :string].
|
|
2346
|
-
p["a", :string].
|
|
2347
|
-
p["NULL", :string].
|
|
2348
|
-
p["-1", :float].
|
|
2349
|
-
p['-1', :decimal].
|
|
2350
|
-
p["2009-10-29", :date].
|
|
2351
|
-
p["2009-10-29 10:20:30", :datetime].
|
|
2352
|
-
p["10:20:30", :time].
|
|
2353
|
-
p["a", :enum].
|
|
2354
|
-
p["a,b", :set].
|
|
2354
|
+
p["\\a'b", :string].must_equal "\\a'b"
|
|
2355
|
+
p["a", :string].must_equal "a"
|
|
2356
|
+
p["NULL", :string].must_equal "NULL"
|
|
2357
|
+
p["-1", :float].must_equal(-1.0)
|
|
2358
|
+
p['-1', :decimal].must_equal BigDecimal.new('-1.0')
|
|
2359
|
+
p["2009-10-29", :date].must_equal Date.new(2009,10,29)
|
|
2360
|
+
p["2009-10-29 10:20:30", :datetime].must_equal DateTime.parse('2009-10-29 10:20:30')
|
|
2361
|
+
p["10:20:30", :time].must_equal Time.parse('10:20:30')
|
|
2362
|
+
p["a", :enum].must_equal "a"
|
|
2363
|
+
p["a,b", :set].must_equal "a,b"
|
|
2355
2364
|
|
|
2356
2365
|
db = Sequel.mock(:host=>'mssql')
|
|
2357
|
-
p["(N'a')", :string].
|
|
2358
|
-
p["((-12))", :integer].
|
|
2359
|
-
p["((12.1))", :float].
|
|
2360
|
-
p["((-12.1))", :decimal].
|
|
2366
|
+
p["(N'a')", :string].must_equal "a"
|
|
2367
|
+
p["((-12))", :integer].must_equal(-12)
|
|
2368
|
+
p["((12.1))", :float].must_equal 12.1
|
|
2369
|
+
p["((-12.1))", :decimal].must_equal BigDecimal.new('-12.1')
|
|
2361
2370
|
end
|
|
2362
2371
|
end
|
|
2363
2372
|
|
|
@@ -2384,54 +2393,54 @@ describe "Database extensions" do
|
|
|
2384
2393
|
Sequel::Database.instance_variable_set(:@initialize_hook, Proc.new {|db| })
|
|
2385
2394
|
end
|
|
2386
2395
|
|
|
2387
|
-
|
|
2396
|
+
it "should be able to register an extension with a module have Database#extension extend the module" do
|
|
2388
2397
|
Sequel::Database.register_extension(:foo, Module.new{def a; 1; end})
|
|
2389
|
-
@db.extension(:foo).a.
|
|
2398
|
+
@db.extension(:foo).a.must_equal 1
|
|
2390
2399
|
end
|
|
2391
2400
|
|
|
2392
|
-
|
|
2401
|
+
it "should be able to register an extension with a block and have Database#extension call the block" do
|
|
2393
2402
|
@db.quote_identifiers = false
|
|
2394
2403
|
Sequel::Database.register_extension(:foo){|db| db.quote_identifiers = true}
|
|
2395
|
-
@db.extension(:foo).quote_identifiers?.
|
|
2404
|
+
@db.extension(:foo).quote_identifiers?.must_equal true
|
|
2396
2405
|
end
|
|
2397
2406
|
|
|
2398
|
-
|
|
2407
|
+
it "should be able to register an extension with a callable and Database#extension call the callable" do
|
|
2399
2408
|
@db.quote_identifiers = false
|
|
2400
2409
|
Sequel::Database.register_extension(:foo, proc{|db| db.quote_identifiers = true})
|
|
2401
|
-
@db.extension(:foo).quote_identifiers?.
|
|
2410
|
+
@db.extension(:foo).quote_identifiers?.must_equal true
|
|
2402
2411
|
end
|
|
2403
2412
|
|
|
2404
|
-
|
|
2413
|
+
it "should be able to load multiple extensions in the same call" do
|
|
2405
2414
|
@db.quote_identifiers = false
|
|
2406
2415
|
@db.identifier_input_method = :downcase
|
|
2407
2416
|
Sequel::Database.register_extension(:foo, proc{|db| db.quote_identifiers = true})
|
|
2408
2417
|
Sequel::Database.register_extension(:bar, proc{|db| db.identifier_input_method = nil})
|
|
2409
2418
|
@db.extension(:foo, :bar)
|
|
2410
|
-
@db.quote_identifiers?.
|
|
2411
|
-
@db.identifier_input_method.
|
|
2419
|
+
@db.quote_identifiers?.must_equal true
|
|
2420
|
+
@db.identifier_input_method.must_equal nil
|
|
2412
2421
|
end
|
|
2413
2422
|
|
|
2414
|
-
|
|
2423
|
+
it "should return the receiver" do
|
|
2415
2424
|
Sequel::Database.register_extension(:foo, Module.new{def a; 1; end})
|
|
2416
|
-
@db.extension(:foo).
|
|
2425
|
+
@db.extension(:foo).must_be_same_as(@db)
|
|
2417
2426
|
end
|
|
2418
2427
|
|
|
2419
|
-
|
|
2420
|
-
proc{Sequel::Database.register_extension(:foo, Module.new){}}.
|
|
2428
|
+
it "should raise an Error if registering with both a module and a block" do
|
|
2429
|
+
proc{Sequel::Database.register_extension(:foo, Module.new){}}.must_raise(Sequel::Error)
|
|
2421
2430
|
end
|
|
2422
2431
|
|
|
2423
|
-
|
|
2424
|
-
proc{@db.extension(:foo2)}.
|
|
2432
|
+
it "should raise an Error if attempting to load an incompatible extension" do
|
|
2433
|
+
proc{@db.extension(:foo2)}.must_raise(Sequel::Error)
|
|
2425
2434
|
end
|
|
2426
2435
|
|
|
2427
|
-
|
|
2436
|
+
it "should be able to load an extension into all future Databases with Database.extension" do
|
|
2428
2437
|
Sequel::Database.register_extension(:foo, Module.new{def a; 1; end})
|
|
2429
2438
|
Sequel::Database.register_extension(:bar, Module.new{def b; 2; end})
|
|
2430
2439
|
Sequel::Database.extension(:foo, :bar)
|
|
2431
|
-
@db.
|
|
2432
|
-
@db.
|
|
2433
|
-
Sequel.mock.a.
|
|
2434
|
-
Sequel.mock.b.
|
|
2440
|
+
@db.wont_respond_to(:a)
|
|
2441
|
+
@db.wont_respond_to(:b)
|
|
2442
|
+
Sequel.mock.a.must_equal 1
|
|
2443
|
+
Sequel.mock.b.must_equal 2
|
|
2435
2444
|
end
|
|
2436
2445
|
end
|
|
2437
2446
|
|
|
@@ -2447,18 +2456,18 @@ describe "Database specific exception classes" do
|
|
|
2447
2456
|
end
|
|
2448
2457
|
end
|
|
2449
2458
|
|
|
2450
|
-
|
|
2459
|
+
it "should use appropriate exception classes for given SQL states" do
|
|
2451
2460
|
@db.fetch = ArgumentError
|
|
2452
2461
|
@db.sql_state = '23502'
|
|
2453
|
-
proc{@db.get(:a)}.
|
|
2462
|
+
proc{@db.get(:a)}.must_raise(Sequel::NotNullConstraintViolation)
|
|
2454
2463
|
@db.sql_state = '23503'
|
|
2455
|
-
proc{@db.get(:a)}.
|
|
2464
|
+
proc{@db.get(:a)}.must_raise(Sequel::ForeignKeyConstraintViolation)
|
|
2456
2465
|
@db.sql_state = '23505'
|
|
2457
|
-
proc{@db.get(:a)}.
|
|
2466
|
+
proc{@db.get(:a)}.must_raise(Sequel::UniqueConstraintViolation)
|
|
2458
2467
|
@db.sql_state = '23513'
|
|
2459
|
-
proc{@db.get(:a)}.
|
|
2468
|
+
proc{@db.get(:a)}.must_raise(Sequel::CheckConstraintViolation)
|
|
2460
2469
|
@db.sql_state = '40001'
|
|
2461
|
-
proc{@db.get(:a)}.
|
|
2470
|
+
proc{@db.get(:a)}.must_raise(Sequel::SerializationFailure)
|
|
2462
2471
|
end
|
|
2463
2472
|
end
|
|
2464
2473
|
|
|
@@ -2467,36 +2476,36 @@ describe "Database.after_initialize" do
|
|
|
2467
2476
|
Sequel::Database.instance_variable_set(:@initialize_hook, Proc.new {|db| })
|
|
2468
2477
|
end
|
|
2469
2478
|
|
|
2470
|
-
|
|
2479
|
+
it "should allow a block to be run after each new instance is created" do
|
|
2471
2480
|
Sequel::Database.after_initialize{|db| db.sql_log_level = :debug }
|
|
2472
2481
|
db = Sequel.mock
|
|
2473
|
-
db.sql_log_level.
|
|
2482
|
+
db.sql_log_level.must_equal :debug
|
|
2474
2483
|
end
|
|
2475
2484
|
|
|
2476
|
-
|
|
2485
|
+
it "should allow multiple hooks to be registered" do
|
|
2477
2486
|
Sequel::Database.after_initialize{|db| db.sql_log_level = :debug }
|
|
2478
2487
|
Sequel::Database.after_initialize{|db| db.loggers << 11 }
|
|
2479
2488
|
|
|
2480
2489
|
db = Sequel.mock
|
|
2481
2490
|
|
|
2482
|
-
db.sql_log_level.
|
|
2483
|
-
db.loggers.
|
|
2491
|
+
db.sql_log_level.must_equal :debug
|
|
2492
|
+
db.loggers.must_include(11)
|
|
2484
2493
|
end
|
|
2485
2494
|
|
|
2486
|
-
|
|
2495
|
+
it "should raise an error if registration is called without a block" do
|
|
2487
2496
|
proc {
|
|
2488
2497
|
Sequel::Database.after_initialize
|
|
2489
|
-
}.
|
|
2498
|
+
}.must_raise(Sequel::Error, /must provide block/i)
|
|
2490
2499
|
end
|
|
2491
2500
|
end
|
|
2492
2501
|
|
|
2493
2502
|
describe "Database#schema_type_class" do
|
|
2494
|
-
|
|
2503
|
+
it "should return the class or array of classes for the given type symbol" do
|
|
2495
2504
|
db = Sequel.mock
|
|
2496
2505
|
{:string=>String, :integer=>Integer, :date=>Date, :datetime=>[Time, DateTime],
|
|
2497
2506
|
:time=>Sequel::SQLTime, :boolean=>[TrueClass, FalseClass], :float=>Float, :decimal=>BigDecimal,
|
|
2498
2507
|
:blob=>Sequel::SQL::Blob}.each do |sym, klass|
|
|
2499
|
-
db.schema_type_class(sym).
|
|
2508
|
+
db.schema_type_class(sym).must_equal klass
|
|
2500
2509
|
end
|
|
2501
2510
|
end
|
|
2502
2511
|
end
|
|
@@ -2512,10 +2521,10 @@ describe "Database#execute_{dui,ddl,insert}" do
|
|
|
2512
2521
|
end
|
|
2513
2522
|
end
|
|
2514
2523
|
|
|
2515
|
-
|
|
2524
|
+
it "should execute the SQL" do
|
|
2516
2525
|
@db.execute_dui "DELETE FROM table"
|
|
2517
2526
|
@db.execute_ddl "SET foo"
|
|
2518
2527
|
@db.execute_insert "INSERT INTO table DEFAULT VALUES"
|
|
2519
|
-
@db.sqls.
|
|
2528
|
+
@db.sqls.must_equal ["DELETE FROM table", "SET foo", "INSERT INTO table DEFAULT VALUES"]
|
|
2520
2529
|
end
|
|
2521
2530
|
end
|