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
|
@@ -9,52 +9,52 @@ describe "Sequel::Postgres::PGRowOp" do
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
it "#[] should access members of the composite type" do
|
|
12
|
-
@db.literal(@a[:b]).
|
|
12
|
+
@db.literal(@a[:b]).must_equal "(a).b"
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
it "#[] should be chainable" do
|
|
16
|
-
@db.literal(@a[:b][:c]).
|
|
16
|
+
@db.literal(@a[:b][:c]).must_equal "((a).b).c"
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
it "#[] should support array access if not given an identifier" do
|
|
20
|
-
@db.literal(@a[:b][1]).
|
|
20
|
+
@db.literal(@a[:b][1]).must_equal "(a).b[1]"
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
it "#[] should be chainable with array access" do
|
|
24
|
-
@db.literal(@a[1][:b]).
|
|
24
|
+
@db.literal(@a[1][:b]).must_equal "(a[1]).b"
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it "#splat should return a splatted argument inside parentheses" do
|
|
28
|
-
@db.literal(@a.splat).
|
|
28
|
+
@db.literal(@a.splat).must_equal "(a.*)"
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "#splat(type) should return a splatted argument cast to given type" do
|
|
32
|
-
@db.literal(@a.splat(:b)).
|
|
32
|
+
@db.literal(@a.splat(:b)).must_equal "(a.*)::b"
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "#splat should not work on an already accessed composite type" do
|
|
36
|
-
proc{@a[:a].splat(:b)}.
|
|
36
|
+
proc{@a[:a].splat(:b)}.must_raise(Sequel::Error)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
it "#* should reference all members of the composite type as separate columns if given no arguments" do
|
|
40
|
-
@db.literal(@a.*).
|
|
41
|
-
@db.literal(@a[:b].*).
|
|
40
|
+
@db.literal(@a.*).must_equal "(a).*"
|
|
41
|
+
@db.literal(@a[:b].*).must_equal "((a).b).*"
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
it "#* should use a multiplication operation if any arguments are given" do
|
|
45
|
-
@db.literal(@a.*(1)).
|
|
46
|
-
@db.literal(@a[:b].*(1)).
|
|
45
|
+
@db.literal(@a.*(1)).must_equal "(a * 1)"
|
|
46
|
+
@db.literal(@a[:b].*(1)).must_equal "((a).b * 1)"
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it "#pg_row should be callable on literal strings" do
|
|
50
|
-
@db.literal(Sequel.lit('a').pg_row[:b]).
|
|
50
|
+
@db.literal(Sequel.lit('a').pg_row[:b]).must_equal "(a).b"
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
it "#pg_row should be callable on Sequel expressions" do
|
|
54
|
-
@db.literal(Sequel.function(:a).pg_row[:b]).
|
|
54
|
+
@db.literal(Sequel.function(:a).pg_row[:b]).must_equal "(a()).b"
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
it "Sequel.pg_row should work as well if the pg_row extension is loaded" do
|
|
58
|
-
@db.literal(Sequel.pg_row(Sequel.function(:a))[:b]).
|
|
58
|
+
@db.literal(Sequel.pg_row(Sequel.function(:a))[:b]).must_equal "(a()).b"
|
|
59
59
|
end
|
|
60
60
|
end
|
|
@@ -20,43 +20,43 @@ describe "Sequel::Plugins::PgRow" do
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it "should have schema_type_class include Sequel::Model" do
|
|
23
|
-
@c2.new.send(:schema_type_class, :address).
|
|
24
|
-
@db.conversion_procs[1098].call('(123 Foo St,Bar City)').
|
|
23
|
+
@c2.new.send(:schema_type_class, :address).must_equal @c
|
|
24
|
+
@db.conversion_procs[1098].call('(123 Foo St,Bar City)').must_equal @c.load(:street=>'123 Foo St', :city=>'Bar City')
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it "should set up a parser for the type that creates a model class" do
|
|
28
|
-
@db.conversion_procs[1098].call('(123 Foo St,Bar City)').
|
|
28
|
+
@db.conversion_procs[1098].call('(123 Foo St,Bar City)').must_equal @c.load(:street=>'123 Foo St', :city=>'Bar City')
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "should set up type casting for the type" do
|
|
32
|
-
@c2.new(:address=>{'street'=>123, 'city'=>:Bar}).address.
|
|
32
|
+
@c2.new(:address=>{'street'=>123, 'city'=>:Bar}).address.must_equal @c.load(:street=>'123', :city=>'Bar')
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "should return model instances as is when typecasting to rows" do
|
|
36
36
|
o = @c.load(:street=>'123', :city=>'Bar')
|
|
37
|
-
@c2.new(:address=>o).address.
|
|
37
|
+
@c2.new(:address=>o).address.must_be_same_as(o)
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
it "should handle literalizing model instances" do
|
|
41
|
-
@db.literal(@c.load(:street=>'123 Foo St', :city=>'Bar City')).
|
|
41
|
+
@db.literal(@c.load(:street=>'123 Foo St', :city=>'Bar City')).must_equal "ROW('123 Foo St', 'Bar City')::address"
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
it "should handle literalizing model instances when model table is aliased" do
|
|
45
45
|
@c.dataset.opts[:from] = [Sequel.as(:address, :a)]
|
|
46
|
-
@db.literal(@c.load(:street=>'123 Foo St', :city=>'Bar City')).
|
|
46
|
+
@db.literal(@c.load(:street=>'123 Foo St', :city=>'Bar City')).must_equal "ROW('123 Foo St', 'Bar City')::address"
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it "should handle model instances in bound variables" do
|
|
50
|
-
@db.bound_variable_arg(1, nil).
|
|
51
|
-
@db.bound_variable_arg(@c.load(:street=>'123 Foo St', :city=>'Bar City'), nil).
|
|
50
|
+
@db.bound_variable_arg(1, nil).must_equal 1
|
|
51
|
+
@db.bound_variable_arg(@c.load(:street=>'123 Foo St', :city=>'Bar City'), nil).must_equal '("123 Foo St","Bar City")'
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
it "should handle model instances in arrays of bound variables" do
|
|
55
|
-
@db.bound_variable_arg(1, nil).
|
|
56
|
-
@db.bound_variable_arg(Sequel.pg_array([@c.load(:street=>'123 Foo St', :city=>'Bar City')]), nil).
|
|
55
|
+
@db.bound_variable_arg(1, nil).must_equal 1
|
|
56
|
+
@db.bound_variable_arg(Sequel.pg_array([@c.load(:street=>'123 Foo St', :city=>'Bar City')]), nil).must_equal '{"(\\"123 Foo St\\",\\"Bar City\\")"}'
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
it "should allow inserting just this model value" do
|
|
60
|
-
@c2.dataset.insert_sql(@c.load(:street=>'123', :city=>'Bar')).
|
|
60
|
+
@c2.dataset.insert_sql(@c.load(:street=>'123', :city=>'Bar')).must_equal "INSERT INTO company VALUES (ROW('123', 'Bar')::address)"
|
|
61
61
|
end
|
|
62
62
|
end
|
|
@@ -10,128 +10,128 @@ describe "pg_row extension" do
|
|
|
10
10
|
|
|
11
11
|
it "should parse record objects as arrays" do
|
|
12
12
|
a = Sequel::Postgres::PG_TYPES[2249].call("(a,b,c)")
|
|
13
|
-
a.
|
|
14
|
-
a.to_a.
|
|
15
|
-
a[0].
|
|
16
|
-
a.
|
|
17
|
-
a.db_type.
|
|
18
|
-
@db.literal(a).
|
|
13
|
+
a.class.must_equal(@m::ArrayRow)
|
|
14
|
+
a.to_a.must_be_kind_of(Array)
|
|
15
|
+
a[0].must_equal 'a'
|
|
16
|
+
a.must_equal %w'a b c'
|
|
17
|
+
a.db_type.must_equal nil
|
|
18
|
+
@db.literal(a).must_equal "ROW('a', 'b', 'c')"
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
it "should parse arrays of record objects as arrays of arrays" do
|
|
22
22
|
as = Sequel::Postgres::PG_TYPES[2287].call('{"(a,b,c)","(d,e,f)"}')
|
|
23
|
-
as.
|
|
23
|
+
as.must_equal [%w'a b c', %w'd e f']
|
|
24
24
|
as.each do |a|
|
|
25
|
-
a.
|
|
26
|
-
a.to_a.
|
|
27
|
-
a.db_type.
|
|
25
|
+
a.class.must_equal(@m::ArrayRow)
|
|
26
|
+
a.to_a.must_be_kind_of(Array)
|
|
27
|
+
a.db_type.must_equal nil
|
|
28
28
|
end
|
|
29
|
-
@db.literal(as).
|
|
29
|
+
@db.literal(as).must_equal "ARRAY[ROW('a', 'b', 'c'),ROW('d', 'e', 'f')]::record[]"
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
it "should be able to register custom parsing of row types as array-like objects" do
|
|
33
33
|
klass = @m::ArrayRow.subclass(:foo)
|
|
34
34
|
parser = @m::Parser.new(:converter=>klass)
|
|
35
35
|
a = parser.call("(a,b,c)")
|
|
36
|
-
a.
|
|
37
|
-
a.to_a.
|
|
38
|
-
a[0].
|
|
39
|
-
a.
|
|
40
|
-
a.db_type.
|
|
41
|
-
@db.literal(a).
|
|
36
|
+
a.class.must_equal(klass)
|
|
37
|
+
a.to_a.must_be_kind_of(Array)
|
|
38
|
+
a[0].must_equal 'a'
|
|
39
|
+
a.must_equal %w'a b c'
|
|
40
|
+
a.db_type.must_equal :foo
|
|
41
|
+
@db.literal(a).must_equal "ROW('a', 'b', 'c')::foo"
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
it "should be able to register custom parsing of row types as hash-like objects" do
|
|
45
45
|
klass = @m::HashRow.subclass(:foo, [:a, :b, :c])
|
|
46
46
|
parser = @m::Parser.new(:converter=>klass, :columns=>[:a, :b, :c])
|
|
47
47
|
a = parser.call("(a,b,c)")
|
|
48
|
-
a.
|
|
49
|
-
a.to_hash.
|
|
50
|
-
a[:a].
|
|
51
|
-
a.
|
|
52
|
-
a.db_type.
|
|
53
|
-
a.columns.
|
|
54
|
-
@db.literal(a).
|
|
48
|
+
a.class.must_equal(klass)
|
|
49
|
+
a.to_hash.must_be_kind_of(Hash)
|
|
50
|
+
a[:a].must_equal 'a'
|
|
51
|
+
a.must_equal(:a=>'a', :b=>'b', :c=>'c')
|
|
52
|
+
a.db_type.must_equal :foo
|
|
53
|
+
a.columns.must_equal [:a, :b, :c]
|
|
54
|
+
@db.literal(a).must_equal "ROW('a', 'b', 'c')::foo"
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
it "should raise an error if attempting to literalize a HashRow without column information" do
|
|
58
58
|
h = @m::HashRow.call(:a=>'a', :b=>'b', :c=>'c')
|
|
59
|
-
proc{@db.literal(h)}.
|
|
59
|
+
proc{@db.literal(h)}.must_raise(Sequel::Error)
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
it "should be able to manually override db_type per ArrayRow instance" do
|
|
63
63
|
a = @m::ArrayRow.call(%w'a b c')
|
|
64
64
|
a.db_type = :foo
|
|
65
|
-
@db.literal(a).
|
|
65
|
+
@db.literal(a).must_equal "ROW('a', 'b', 'c')::foo"
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
it "should be able to manually override db_type and columns per HashRow instance" do
|
|
69
69
|
h = @m::HashRow.call(:a=>'a', :c=>'c', :b=>'b')
|
|
70
70
|
h.db_type = :foo
|
|
71
71
|
h.columns = [:a, :b, :c]
|
|
72
|
-
@db.literal(h).
|
|
72
|
+
@db.literal(h).must_equal "ROW('a', 'b', 'c')::foo"
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
it "should correctly split an empty row" do
|
|
76
|
-
@m::Splitter.new("()").parse.
|
|
76
|
+
@m::Splitter.new("()").parse.must_equal [nil]
|
|
77
77
|
end
|
|
78
78
|
|
|
79
79
|
it "should correctly split a row with a single value" do
|
|
80
|
-
@m::Splitter.new("(1)").parse.
|
|
80
|
+
@m::Splitter.new("(1)").parse.must_equal %w'1'
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
it "should correctly split a row with multiple values" do
|
|
84
|
-
@m::Splitter.new("(1,2)").parse.
|
|
84
|
+
@m::Splitter.new("(1,2)").parse.must_equal %w'1 2'
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
it "should correctly NULL values when splitting" do
|
|
88
|
-
@m::Splitter.new("(1,)").parse.
|
|
88
|
+
@m::Splitter.new("(1,)").parse.must_equal ['1', nil]
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
it "should correctly empty string values when splitting" do
|
|
92
|
-
@m::Splitter.new('(1,"")').parse.
|
|
92
|
+
@m::Splitter.new('(1,"")').parse.must_equal ['1', '']
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
it "should handle quoted values when splitting" do
|
|
96
|
-
@m::Splitter.new('("1","2")').parse.
|
|
96
|
+
@m::Splitter.new('("1","2")').parse.must_equal %w'1 2'
|
|
97
97
|
end
|
|
98
98
|
|
|
99
99
|
it "should handle escaped backslashes in quoted values when splitting" do
|
|
100
|
-
@m::Splitter.new('("\\\\1","2\\\\")').parse.
|
|
100
|
+
@m::Splitter.new('("\\\\1","2\\\\")').parse.must_equal ['\\1', '2\\']
|
|
101
101
|
end
|
|
102
102
|
|
|
103
103
|
it "should handle doubled quotes in quoted values when splitting" do
|
|
104
|
-
@m::Splitter.new('("""1","2""")').parse.
|
|
104
|
+
@m::Splitter.new('("""1","2""")').parse.must_equal ['"1', '2"']
|
|
105
105
|
end
|
|
106
106
|
|
|
107
107
|
it "should correctly convert types when parsing into an array" do
|
|
108
|
-
@m::Parser.new(:column_converters=>[proc{|s| s*2}, proc{|s| s*3}, proc{|s| s*4}]).call("(a,b,c)").
|
|
108
|
+
@m::Parser.new(:column_converters=>[proc{|s| s*2}, proc{|s| s*3}, proc{|s| s*4}]).call("(a,b,c)").must_equal %w'aa bbb cccc'
|
|
109
109
|
end
|
|
110
110
|
|
|
111
111
|
it "should correctly convert types into hashes if columns are known" do
|
|
112
|
-
@m::Parser.new(:columns=>[:a, :b, :c]).call("(a,b,c)").
|
|
112
|
+
@m::Parser.new(:columns=>[:a, :b, :c]).call("(a,b,c)").must_equal(:a=>'a', :b=>'b', :c=>'c')
|
|
113
113
|
end
|
|
114
114
|
|
|
115
115
|
it "should correctly handle type conversion when converting into hashes" do
|
|
116
|
-
@m::Parser.new(:column_converters=>[proc{|s| s*2}, proc{|s| s*3}, proc{|s| s*4}], :columns=>[:a, :b, :c]).call("(a,b,c)").
|
|
116
|
+
@m::Parser.new(:column_converters=>[proc{|s| s*2}, proc{|s| s*3}, proc{|s| s*4}], :columns=>[:a, :b, :c]).call("(a,b,c)").must_equal(:a=>'aa', :b=>'bbb', :c=>'cccc')
|
|
117
117
|
end
|
|
118
118
|
|
|
119
119
|
it "should correctly wrap arrays when converting" do
|
|
120
|
-
@m::Parser.new(:converter=>proc{|s| [:foo, s]}).call("(a,b,c)").
|
|
120
|
+
@m::Parser.new(:converter=>proc{|s| [:foo, s]}).call("(a,b,c)").must_equal [:foo, %w'a b c']
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
it "should correctly wrap hashes when converting" do
|
|
124
|
-
@m::Parser.new(:converter=>proc{|s| [:foo, s]}, :columns=>[:a, :b, :c]).call("(a,b,c)").
|
|
124
|
+
@m::Parser.new(:converter=>proc{|s| [:foo, s]}, :columns=>[:a, :b, :c]).call("(a,b,c)").must_equal [:foo, {:a=>'a', :b=>'b', :c=>'c'}]
|
|
125
125
|
end
|
|
126
126
|
|
|
127
127
|
it "should have parser store reflection information" do
|
|
128
128
|
p = @m::Parser.new(:oid=>1, :column_oids=>[2], :columns=>[:a], :converter=>Array, :typecaster=>Hash, :column_converters=>[Array])
|
|
129
|
-
p.oid.
|
|
130
|
-
p.column_oids.
|
|
131
|
-
p.columns.
|
|
132
|
-
p.converter.
|
|
133
|
-
p.typecaster.
|
|
134
|
-
p.column_converters.
|
|
129
|
+
p.oid.must_equal 1
|
|
130
|
+
p.column_oids.must_equal [2]
|
|
131
|
+
p.columns.must_equal [:a]
|
|
132
|
+
p.converter.must_equal Array
|
|
133
|
+
p.typecaster.must_equal Hash
|
|
134
|
+
p.column_converters.must_equal [Array]
|
|
135
135
|
end
|
|
136
136
|
|
|
137
137
|
it "should reload registered row types when reseting conversion procs" do
|
|
@@ -142,7 +142,7 @@ describe "pg_row extension" do
|
|
|
142
142
|
db.sqls
|
|
143
143
|
db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}], [{:attname=>'bar', :atttypid=>4}, {:attname=>'baz', :atttypid=>5}]]
|
|
144
144
|
db.register_row_type(:foo)
|
|
145
|
-
db.sqls.
|
|
145
|
+
db.sqls.must_equal ["SELECT pg_type.oid, typrelid, typarray FROM pg_type WHERE ((typtype = 'c') AND (typname = 'foo')) LIMIT 1",
|
|
146
146
|
"SELECT attname, (CASE pg_type.typbasetype WHEN 0 THEN atttypid ELSE pg_type.typbasetype END) AS atttypid FROM pg_attribute INNER JOIN pg_type ON (pg_type.oid = pg_attribute.atttypid) WHERE ((attrelid = 2) AND (attnum > 0) AND NOT attisdropped) ORDER BY attnum"]
|
|
147
147
|
|
|
148
148
|
begin
|
|
@@ -150,7 +150,7 @@ describe "pg_row extension" do
|
|
|
150
150
|
Sequel::Postgres::PG_NAMED_TYPES.clear
|
|
151
151
|
db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}], [{:attname=>'bar', :atttypid=>4}, {:attname=>'baz', :atttypid=>5}]]
|
|
152
152
|
db.reset_conversion_procs
|
|
153
|
-
db.sqls.
|
|
153
|
+
db.sqls.must_equal ["SELECT pg_type.oid, typrelid, typarray FROM pg_type WHERE ((typtype = 'c') AND (typname = 'foo')) LIMIT 1",
|
|
154
154
|
"SELECT attname, (CASE pg_type.typbasetype WHEN 0 THEN atttypid ELSE pg_type.typbasetype END) AS atttypid FROM pg_attribute INNER JOIN pg_type ON (pg_type.oid = pg_attribute.atttypid) WHERE ((attrelid = 2) AND (attnum > 0) AND NOT attisdropped) ORDER BY attnum"]
|
|
155
155
|
ensure
|
|
156
156
|
Sequel::Postgres::PG_NAMED_TYPES.replace pgnt
|
|
@@ -158,22 +158,22 @@ describe "pg_row extension" do
|
|
|
158
158
|
end
|
|
159
159
|
|
|
160
160
|
it "should handle ArrayRows and HashRows in bound variables" do
|
|
161
|
-
@db.bound_variable_arg(1, nil).
|
|
162
|
-
@db.bound_variable_arg(@m::ArrayRow.call(["1", "abc\\'\","]), nil).
|
|
163
|
-
@db.bound_variable_arg(@m::HashRow.subclass(nil, [:a, :b]).call(:a=>"1", :b=>"abc\\'\","), nil).
|
|
161
|
+
@db.bound_variable_arg(1, nil).must_equal 1
|
|
162
|
+
@db.bound_variable_arg(@m::ArrayRow.call(["1", "abc\\'\","]), nil).must_equal '("1","abc\\\\\'\\",")'
|
|
163
|
+
@db.bound_variable_arg(@m::HashRow.subclass(nil, [:a, :b]).call(:a=>"1", :b=>"abc\\'\","), nil).must_equal '("1","abc\\\\\'\\",")'
|
|
164
164
|
end
|
|
165
165
|
|
|
166
166
|
it "should handle ArrayRows and HashRows in arrays in bound variables" do
|
|
167
|
-
@db.bound_variable_arg(1, nil).
|
|
168
|
-
@db.bound_variable_arg([@m::ArrayRow.call(["1", "abc\\'\","])], nil).
|
|
169
|
-
@db.bound_variable_arg([@m::HashRow.subclass(nil, [:a, :b]).call(:a=>"1", :b=>"abc\\'\",")], nil).
|
|
167
|
+
@db.bound_variable_arg(1, nil).must_equal 1
|
|
168
|
+
@db.bound_variable_arg([@m::ArrayRow.call(["1", "abc\\'\","])], nil).must_equal '{"(\\"1\\",\\"abc\\\\\\\\\'\\\\\\",\\")"}'
|
|
169
|
+
@db.bound_variable_arg([@m::HashRow.subclass(nil, [:a, :b]).call(:a=>"1", :b=>"abc\\'\",")], nil).must_equal '{"(\\"1\\",\\"abc\\\\\\\\\'\\\\\\",\\")"}'
|
|
170
170
|
end
|
|
171
171
|
|
|
172
172
|
it "should handle nils in bound variables" do
|
|
173
|
-
@db.bound_variable_arg(@m::ArrayRow.call([nil, nil]), nil).
|
|
174
|
-
@db.bound_variable_arg(@m::HashRow.subclass(nil, [:a, :b]).call(:a=>nil, :b=>nil), nil).
|
|
175
|
-
@db.bound_variable_arg([@m::ArrayRow.call([nil, nil])], nil).
|
|
176
|
-
@db.bound_variable_arg([@m::HashRow.subclass(nil, [:a, :b]).call(:a=>nil, :b=>nil)], nil).
|
|
173
|
+
@db.bound_variable_arg(@m::ArrayRow.call([nil, nil]), nil).must_equal '(,)'
|
|
174
|
+
@db.bound_variable_arg(@m::HashRow.subclass(nil, [:a, :b]).call(:a=>nil, :b=>nil), nil).must_equal '(,)'
|
|
175
|
+
@db.bound_variable_arg([@m::ArrayRow.call([nil, nil])], nil).must_equal '{"(,)"}'
|
|
176
|
+
@db.bound_variable_arg([@m::HashRow.subclass(nil, [:a, :b]).call(:a=>nil, :b=>nil)], nil).must_equal '{"(,)"}'
|
|
177
177
|
end
|
|
178
178
|
|
|
179
179
|
it "should allow registering row type parsers by introspecting system tables" do
|
|
@@ -181,27 +181,27 @@ describe "pg_row extension" do
|
|
|
181
181
|
@db.conversion_procs[5] = p5 = proc{|s| s * 2}
|
|
182
182
|
@db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}], [{:attname=>'bar', :atttypid=>4}, {:attname=>'baz', :atttypid=>5}]]
|
|
183
183
|
@db.register_row_type(:foo)
|
|
184
|
-
@db.sqls.
|
|
184
|
+
@db.sqls.must_equal ["SELECT pg_type.oid, typrelid, typarray FROM pg_type WHERE ((typtype = 'c') AND (typname = 'foo')) LIMIT 1",
|
|
185
185
|
"SELECT attname, (CASE pg_type.typbasetype WHEN 0 THEN atttypid ELSE pg_type.typbasetype END) AS atttypid FROM pg_attribute INNER JOIN pg_type ON (pg_type.oid = pg_attribute.atttypid) WHERE ((attrelid = 2) AND (attnum > 0) AND NOT attisdropped) ORDER BY attnum"]
|
|
186
186
|
p1 = @db.conversion_procs[1]
|
|
187
|
-
p1.columns.
|
|
188
|
-
p1.column_oids.
|
|
189
|
-
p1.column_converters.
|
|
190
|
-
p1.oid.
|
|
191
|
-
@db.send(:schema_column_type, 'foo').
|
|
192
|
-
@db.send(:schema_column_type, 'integer').
|
|
187
|
+
p1.columns.must_equal [:bar, :baz]
|
|
188
|
+
p1.column_oids.must_equal [4, 5]
|
|
189
|
+
p1.column_converters.must_equal [p4, p5]
|
|
190
|
+
p1.oid.must_equal 1
|
|
191
|
+
@db.send(:schema_column_type, 'foo').must_equal :pg_row_foo
|
|
192
|
+
@db.send(:schema_column_type, 'integer').must_equal :integer
|
|
193
193
|
|
|
194
194
|
c = p1.converter
|
|
195
|
-
c.superclass.
|
|
196
|
-
c.columns.
|
|
197
|
-
c.db_type.
|
|
198
|
-
p1.typecaster.
|
|
195
|
+
c.superclass.must_equal @m::HashRow
|
|
196
|
+
c.columns.must_equal [:bar, :baz]
|
|
197
|
+
c.db_type.must_equal :foo
|
|
198
|
+
p1.typecaster.must_equal c
|
|
199
199
|
|
|
200
|
-
p1.call('(1,b)').
|
|
201
|
-
@db.typecast_value(:pg_row_foo, %w'1 b').
|
|
202
|
-
@db.typecast_value(:pg_row_foo, %w'1 b').
|
|
203
|
-
@db.typecast_value(:pg_row_foo, :bar=>'1', :baz=>'b').
|
|
204
|
-
@db.literal(p1.call('(1,b)')).
|
|
200
|
+
p1.call('(1,b)').must_equal(:bar=>1, :baz=>'bb')
|
|
201
|
+
@db.typecast_value(:pg_row_foo, %w'1 b').class.must_be :<, @m::HashRow
|
|
202
|
+
@db.typecast_value(:pg_row_foo, %w'1 b').must_equal(:bar=>'1', :baz=>'b')
|
|
203
|
+
@db.typecast_value(:pg_row_foo, :bar=>'1', :baz=>'b').must_equal(:bar=>'1', :baz=>'b')
|
|
204
|
+
@db.literal(p1.call('(1,b)')).must_equal "ROW(1, 'bb')::foo"
|
|
205
205
|
end
|
|
206
206
|
|
|
207
207
|
it "should allow registering row type parsers for schema qualify types" do
|
|
@@ -209,24 +209,24 @@ describe "pg_row extension" do
|
|
|
209
209
|
@db.conversion_procs[5] = p5 = proc{|s| s * 2}
|
|
210
210
|
@db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}], [{:attname=>'bar', :atttypid=>4}, {:attname=>'baz', :atttypid=>5}]]
|
|
211
211
|
@db.register_row_type(:foo__bar)
|
|
212
|
-
@db.sqls.
|
|
212
|
+
@db.sqls.must_equal ["SELECT pg_type.oid, typrelid, typarray FROM pg_type INNER JOIN pg_namespace ON ((pg_namespace.oid = pg_type.typnamespace) AND (pg_namespace.nspname = 'foo')) WHERE ((typtype = 'c') AND (typname = 'bar')) LIMIT 1",
|
|
213
213
|
"SELECT attname, (CASE pg_type.typbasetype WHEN 0 THEN atttypid ELSE pg_type.typbasetype END) AS atttypid FROM pg_attribute INNER JOIN pg_type ON (pg_type.oid = pg_attribute.atttypid) WHERE ((attrelid = 2) AND (attnum > 0) AND NOT attisdropped) ORDER BY attnum"]
|
|
214
214
|
p1 = @db.conversion_procs[1]
|
|
215
|
-
p1.columns.
|
|
216
|
-
p1.column_oids.
|
|
217
|
-
p1.column_converters.
|
|
218
|
-
p1.oid.
|
|
215
|
+
p1.columns.must_equal [:bar, :baz]
|
|
216
|
+
p1.column_oids.must_equal [4, 5]
|
|
217
|
+
p1.column_converters.must_equal [p4, p5]
|
|
218
|
+
p1.oid.must_equal 1
|
|
219
219
|
|
|
220
220
|
c = p1.converter
|
|
221
|
-
c.superclass.
|
|
222
|
-
c.columns.
|
|
223
|
-
c.db_type.
|
|
224
|
-
p1.typecaster.
|
|
221
|
+
c.superclass.must_equal @m::HashRow
|
|
222
|
+
c.columns.must_equal [:bar, :baz]
|
|
223
|
+
c.db_type.must_equal :foo__bar
|
|
224
|
+
p1.typecaster.must_equal c
|
|
225
225
|
|
|
226
|
-
p1.call('(1,b)').
|
|
227
|
-
@db.typecast_value(:pg_row_foo__bar, %w'1 b').
|
|
228
|
-
@db.typecast_value(:pg_row_foo__bar, :bar=>'1', :baz=>'b').
|
|
229
|
-
@db.literal(p1.call('(1,b)')).
|
|
226
|
+
p1.call('(1,b)').must_equal(:bar=>1, :baz=>'bb')
|
|
227
|
+
@db.typecast_value(:pg_row_foo__bar, %w'1 b').must_equal(:bar=>'1', :baz=>'b')
|
|
228
|
+
@db.typecast_value(:pg_row_foo__bar, :bar=>'1', :baz=>'b').must_equal(:bar=>'1', :baz=>'b')
|
|
229
|
+
@db.literal(p1.call('(1,b)')).must_equal "ROW(1, 'bb')::foo.bar"
|
|
230
230
|
end
|
|
231
231
|
|
|
232
232
|
it "should allow registering with a custom converter" do
|
|
@@ -236,8 +236,8 @@ describe "pg_row extension" do
|
|
|
236
236
|
c = proc{|h| [h]}
|
|
237
237
|
@db.register_row_type(:foo, :converter=>c)
|
|
238
238
|
o = @db.conversion_procs[1].call('(1,b)')
|
|
239
|
-
o.
|
|
240
|
-
o.first.
|
|
239
|
+
o.must_equal [{:bar=>1, :baz=>'bb'}]
|
|
240
|
+
o.first.must_be_kind_of(Hash)
|
|
241
241
|
end
|
|
242
242
|
|
|
243
243
|
it "should allow registering with a custom typecaster" do
|
|
@@ -245,15 +245,15 @@ describe "pg_row extension" do
|
|
|
245
245
|
@db.conversion_procs[5] = proc{|s| s * 2}
|
|
246
246
|
@db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}], [{:attname=>'bar', :atttypid=>4}, {:attname=>'baz', :atttypid=>5}]]
|
|
247
247
|
@db.register_row_type(:foo, :typecaster=>proc{|h| {:bar=>(h[:bar]||0).to_i, :baz=>(h[:baz] || 'a')*2}})
|
|
248
|
-
@db.typecast_value(:pg_row_foo, %w'1 b').
|
|
249
|
-
@db.typecast_value(:pg_row_foo, %w'1 b').
|
|
250
|
-
@db.typecast_value(:pg_row_foo, :bar=>'1', :baz=>'b').
|
|
251
|
-
@db.typecast_value(:pg_row_foo, 'bar'=>'1', 'baz'=>'b').
|
|
248
|
+
@db.typecast_value(:pg_row_foo, %w'1 b').must_be_kind_of(Hash)
|
|
249
|
+
@db.typecast_value(:pg_row_foo, %w'1 b').must_equal(:bar=>1, :baz=>'bb')
|
|
250
|
+
@db.typecast_value(:pg_row_foo, :bar=>'1', :baz=>'b').must_equal(:bar=>1, :baz=>'bb')
|
|
251
|
+
@db.typecast_value(:pg_row_foo, 'bar'=>'1', 'baz'=>'b').must_equal(:bar=>0, :baz=>'aa')
|
|
252
252
|
@db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}], [{:attname=>'bar', :atttypid=>4}, {:attname=>'baz', :atttypid=>5}]]
|
|
253
253
|
@db.register_row_type(:foo, :typecaster=>proc{|h| {:bar=>(h[:bar] || h['bar'] || 0).to_i, :baz=>(h[:baz] || h['baz'] || 'a')*2}})
|
|
254
|
-
@db.typecast_value(:pg_row_foo, %w'1 b').
|
|
255
|
-
@db.typecast_value(:pg_row_foo, :bar=>'1', :baz=>'b').
|
|
256
|
-
@db.typecast_value(:pg_row_foo, 'bar'=>'1', 'baz'=>'b').
|
|
254
|
+
@db.typecast_value(:pg_row_foo, %w'1 b').must_equal(:bar=>1, :baz=>'bb')
|
|
255
|
+
@db.typecast_value(:pg_row_foo, :bar=>'1', :baz=>'b').must_equal(:bar=>1, :baz=>'bb')
|
|
256
|
+
@db.typecast_value(:pg_row_foo, 'bar'=>'1', 'baz'=>'b').must_equal(:bar=>1, :baz=>'bb')
|
|
257
257
|
end
|
|
258
258
|
|
|
259
259
|
it "should handle conversion procs that aren't added until later" do
|
|
@@ -262,7 +262,7 @@ describe "pg_row extension" do
|
|
|
262
262
|
c = proc{|h| [h]}
|
|
263
263
|
@db.register_row_type(:foo, :converter=>c)
|
|
264
264
|
@db.conversion_procs[4] = proc{|s| s.to_i}
|
|
265
|
-
@db.conversion_procs[1].call('(1,b)').
|
|
265
|
+
@db.conversion_procs[1].call('(1,b)').must_equal [{:bar=>1, :baz=>'bb'}]
|
|
266
266
|
end
|
|
267
267
|
|
|
268
268
|
it "should handle nil values when converting columns" do
|
|
@@ -271,8 +271,8 @@ describe "pg_row extension" do
|
|
|
271
271
|
called = false
|
|
272
272
|
@db.conversion_procs[4] = proc{|s| called = true; s}
|
|
273
273
|
@db.register_row_type(:foo)
|
|
274
|
-
@db.conversion_procs[1].call('()').
|
|
275
|
-
called.
|
|
274
|
+
@db.conversion_procs[1].call('()').must_equal(:bar=>nil)
|
|
275
|
+
called.must_equal false
|
|
276
276
|
end
|
|
277
277
|
|
|
278
278
|
it "should registering array type for row type if type has an array oid" do
|
|
@@ -282,13 +282,13 @@ describe "pg_row extension" do
|
|
|
282
282
|
@db.register_row_type(:foo, :typecaster=>proc{|h| {:bar=>(h[:bar]||0).to_i, :baz=>(h[:baz] || 'a')*2}})
|
|
283
283
|
p3 = @db.conversion_procs[3]
|
|
284
284
|
|
|
285
|
-
p3.call('{"(1,b)"}').
|
|
286
|
-
@db.literal(p3.call('{"(1,b)"}')).
|
|
287
|
-
@db.typecast_value(:foo_array, [{:bar=>'1', :baz=>'b'}]).
|
|
285
|
+
p3.call('{"(1,b)"}').must_equal [{:bar=>1, :baz=>'bb'}]
|
|
286
|
+
@db.literal(p3.call('{"(1,b)"}')).must_equal "ARRAY[ROW(1, 'bb')::foo]::foo[]"
|
|
287
|
+
@db.typecast_value(:foo_array, [{:bar=>'1', :baz=>'b'}]).must_equal [{:bar=>1, :baz=>'bb'}]
|
|
288
288
|
end
|
|
289
289
|
|
|
290
290
|
it "should allow creating unregisted row types via Database#row_type" do
|
|
291
|
-
@db.literal(@db.row_type(:foo, [1, 2])).
|
|
291
|
+
@db.literal(@db.row_type(:foo, [1, 2])).must_equal 'ROW(1, 2)::foo'
|
|
292
292
|
end
|
|
293
293
|
|
|
294
294
|
it "should allow typecasting of registered row types via Database#row_type" do
|
|
@@ -296,8 +296,8 @@ describe "pg_row extension" do
|
|
|
296
296
|
@db.conversion_procs[5] = proc{|s| s * 2}
|
|
297
297
|
@db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}], [{:attname=>'bar', :atttypid=>4}, {:attname=>'baz', :atttypid=>5}]]
|
|
298
298
|
@db.register_row_type(:foo, :typecaster=>proc{|h| @m::HashRow.subclass(:foo, [:bar, :baz]).new({:bar=>(h[:bar]||0).to_i, :baz=>(h[:baz] || 'a')*2})})
|
|
299
|
-
@db.literal(@db.row_type(:foo, ['1', 'b'])).
|
|
300
|
-
@db.literal(@db.row_type(:foo, {:bar=>'1', :baz=>'b'})).
|
|
299
|
+
@db.literal(@db.row_type(:foo, ['1', 'b'])).must_equal "ROW(1, 'bb')::foo"
|
|
300
|
+
@db.literal(@db.row_type(:foo, {:bar=>'1', :baz=>'b'})).must_equal "ROW(1, 'bb')::foo"
|
|
301
301
|
end
|
|
302
302
|
|
|
303
303
|
it "should allow parsing when typecasting registered row types via Database#row_type" do
|
|
@@ -305,48 +305,48 @@ describe "pg_row extension" do
|
|
|
305
305
|
@db.conversion_procs[5] = proc{|s| s * 2}
|
|
306
306
|
@db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}], [{:attname=>'bar', :atttypid=>4}, {:attname=>'baz', :atttypid=>5}]]
|
|
307
307
|
@db.register_row_type(:foo, :typecaster=>proc{|h| @m::HashRow.subclass(:foo, [:bar, :baz]).new(:bar=>(h[:bar]||0).to_i, :baz=>(h[:baz] || 'a')*2)})
|
|
308
|
-
@db.literal(@db.row_type(:foo, ['1', 'b'])).
|
|
308
|
+
@db.literal(@db.row_type(:foo, ['1', 'b'])).must_equal "ROW(1, 'bb')::foo"
|
|
309
309
|
end
|
|
310
310
|
|
|
311
311
|
it "should raise an error if attempt to use Database#row_type with an unregistered type and hash" do
|
|
312
|
-
proc{@db.literal(@db.row_type(:foo, {:bar=>'1', :baz=>'b'}))}.
|
|
312
|
+
proc{@db.literal(@db.row_type(:foo, {:bar=>'1', :baz=>'b'}))}.must_raise(Sequel::InvalidValue)
|
|
313
313
|
end
|
|
314
314
|
|
|
315
315
|
it "should raise an error if attempt to use Database#row_type with an unhandled type" do
|
|
316
|
-
proc{@db.literal(@db.row_type(:foo, 1))}.
|
|
316
|
+
proc{@db.literal(@db.row_type(:foo, 1))}.must_raise(Sequel::InvalidValue)
|
|
317
317
|
end
|
|
318
318
|
|
|
319
319
|
it "should return ArrayRow and HashRow values as-is" do
|
|
320
320
|
h = @m::HashRow.call(:a=>1)
|
|
321
321
|
a = @m::ArrayRow.call([1])
|
|
322
|
-
@db.row_type(:foo, h).
|
|
323
|
-
@db.row_type(:foo, a).
|
|
322
|
+
@db.row_type(:foo, h).object_id.must_equal(h.object_id)
|
|
323
|
+
@db.row_type(:foo, a).object_id.must_equal(a.object_id)
|
|
324
324
|
end
|
|
325
325
|
|
|
326
326
|
it "should have Sequel.pg_row return a plain ArrayRow" do
|
|
327
|
-
@db.literal(Sequel.pg_row([1, 2, 3])).
|
|
327
|
+
@db.literal(Sequel.pg_row([1, 2, 3])).must_equal 'ROW(1, 2, 3)'
|
|
328
328
|
end
|
|
329
329
|
|
|
330
330
|
it "should raise an error if attempting to typecast a hash for a parser without columns" do
|
|
331
|
-
proc{@m::Parser.new.typecast(:a=>1)}.
|
|
331
|
+
proc{@m::Parser.new.typecast(:a=>1)}.must_raise(Sequel::Error)
|
|
332
332
|
end
|
|
333
333
|
|
|
334
334
|
it "should raise an error if attempting to typecast a unhandled value for a parser" do
|
|
335
|
-
proc{@m::Parser.new.typecast(1)}.
|
|
335
|
+
proc{@m::Parser.new.typecast(1)}.must_raise(Sequel::Error)
|
|
336
336
|
end
|
|
337
337
|
|
|
338
338
|
it "should handle typecasting for a parser without a typecaster" do
|
|
339
|
-
@m::Parser.new.typecast([1]).
|
|
339
|
+
@m::Parser.new.typecast([1]).must_equal [1]
|
|
340
340
|
end
|
|
341
341
|
|
|
342
342
|
it "should raise an error if no columns are returned when registering a custom row type" do
|
|
343
343
|
@db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}]]
|
|
344
|
-
proc{@db.register_row_type(:foo)}.
|
|
344
|
+
proc{@db.register_row_type(:foo)}.must_raise(Sequel::Error)
|
|
345
345
|
end
|
|
346
346
|
|
|
347
347
|
it "should raise an error when registering a custom row type if the type is found found" do
|
|
348
348
|
@db.fetch = []
|
|
349
|
-
proc{@db.register_row_type(:foo)}.
|
|
349
|
+
proc{@db.register_row_type(:foo)}.must_raise(Sequel::Error)
|
|
350
350
|
end
|
|
351
351
|
|
|
352
352
|
it "should return correct results for Database#schema_type_class" do
|
|
@@ -354,7 +354,7 @@ describe "pg_row extension" do
|
|
|
354
354
|
@db.conversion_procs[5] = proc{|s| s * 2}
|
|
355
355
|
@db.fetch = [[{:oid=>1, :typrelid=>2, :typarray=>3}], [{:attname=>'bar', :atttypid=>4}, {:attname=>'baz', :atttypid=>5}]]
|
|
356
356
|
@db.register_row_type(:foo, :typecaster=>proc{|h| {:bar=>(h[:bar]||0).to_i, :baz=>(h[:baz] || 'a')*2}})
|
|
357
|
-
@db.schema_type_class(:pg_row_foo).
|
|
358
|
-
@db.schema_type_class(:integer).
|
|
357
|
+
@db.schema_type_class(:pg_row_foo).must_equal [Sequel::Postgres::PGRow::HashRow, Sequel::Postgres::PGRow::ArrayRow]
|
|
358
|
+
@db.schema_type_class(:integer).must_equal Integer
|
|
359
359
|
end
|
|
360
360
|
end
|