sequel 3.48.0 → 4.0.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 +114 -0
- data/Rakefile +10 -7
- data/doc/association_basics.rdoc +25 -23
- data/doc/code_order.rdoc +7 -0
- data/doc/core_extensions.rdoc +0 -10
- data/doc/object_model.rdoc +4 -1
- data/doc/querying.rdoc +3 -3
- data/doc/release_notes/4.0.0.txt +262 -0
- data/doc/security.rdoc +0 -28
- data/doc/testing.rdoc +8 -14
- data/lib/sequel/adapters/ado.rb +7 -11
- data/lib/sequel/adapters/ado/access.rb +8 -8
- data/lib/sequel/adapters/ado/mssql.rb +4 -4
- data/lib/sequel/adapters/amalgalite.rb +6 -6
- data/lib/sequel/adapters/cubrid.rb +7 -7
- data/lib/sequel/adapters/db2.rb +5 -9
- data/lib/sequel/adapters/dbi.rb +2 -6
- data/lib/sequel/adapters/do.rb +4 -4
- data/lib/sequel/adapters/firebird.rb +4 -4
- data/lib/sequel/adapters/ibmdb.rb +8 -8
- data/lib/sequel/adapters/informix.rb +2 -10
- data/lib/sequel/adapters/jdbc.rb +17 -17
- data/lib/sequel/adapters/jdbc/as400.rb +2 -2
- data/lib/sequel/adapters/jdbc/cubrid.rb +1 -1
- data/lib/sequel/adapters/jdbc/db2.rb +1 -1
- data/lib/sequel/adapters/jdbc/derby.rb +1 -1
- data/lib/sequel/adapters/jdbc/h2.rb +2 -2
- data/lib/sequel/adapters/jdbc/hsqldb.rb +1 -1
- data/lib/sequel/adapters/jdbc/informix.rb +1 -1
- data/lib/sequel/adapters/jdbc/mssql.rb +2 -2
- data/lib/sequel/adapters/jdbc/mysql.rb +1 -1
- data/lib/sequel/adapters/jdbc/oracle.rb +5 -1
- data/lib/sequel/adapters/jdbc/postgresql.rb +3 -3
- data/lib/sequel/adapters/jdbc/sqlite.rb +3 -3
- data/lib/sequel/adapters/jdbc/transactions.rb +3 -3
- data/lib/sequel/adapters/mock.rb +7 -7
- data/lib/sequel/adapters/mysql.rb +3 -3
- data/lib/sequel/adapters/mysql2.rb +4 -4
- data/lib/sequel/adapters/odbc.rb +2 -6
- data/lib/sequel/adapters/odbc/mssql.rb +1 -1
- data/lib/sequel/adapters/openbase.rb +1 -5
- data/lib/sequel/adapters/oracle.rb +13 -17
- data/lib/sequel/adapters/postgres.rb +20 -25
- data/lib/sequel/adapters/shared/cubrid.rb +3 -3
- data/lib/sequel/adapters/shared/db2.rb +2 -2
- data/lib/sequel/adapters/shared/firebird.rb +7 -7
- data/lib/sequel/adapters/shared/mssql.rb +9 -9
- data/lib/sequel/adapters/shared/mysql.rb +29 -13
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +7 -7
- data/lib/sequel/adapters/shared/oracle.rb +22 -13
- data/lib/sequel/adapters/shared/postgres.rb +61 -46
- data/lib/sequel/adapters/shared/sqlite.rb +9 -9
- data/lib/sequel/adapters/sqlite.rb +17 -11
- data/lib/sequel/adapters/swift.rb +3 -3
- data/lib/sequel/adapters/swift/mysql.rb +1 -1
- data/lib/sequel/adapters/swift/sqlite.rb +1 -1
- data/lib/sequel/adapters/tinytds.rb +8 -8
- data/lib/sequel/ast_transformer.rb +3 -1
- data/lib/sequel/connection_pool.rb +4 -2
- data/lib/sequel/connection_pool/sharded_single.rb +2 -2
- data/lib/sequel/connection_pool/sharded_threaded.rb +5 -5
- data/lib/sequel/connection_pool/threaded.rb +7 -7
- data/lib/sequel/core.rb +4 -67
- data/lib/sequel/database.rb +1 -0
- data/lib/sequel/database/connecting.rb +2 -8
- data/lib/sequel/database/dataset.rb +2 -7
- data/lib/sequel/database/dataset_defaults.rb +0 -18
- data/lib/sequel/database/features.rb +4 -4
- data/lib/sequel/database/misc.rb +6 -8
- data/lib/sequel/database/query.rb +5 -61
- data/lib/sequel/database/schema_generator.rb +22 -20
- data/lib/sequel/database/schema_methods.rb +48 -20
- data/lib/sequel/database/transactions.rb +7 -17
- data/lib/sequel/dataset.rb +2 -0
- data/lib/sequel/dataset/actions.rb +23 -91
- data/lib/sequel/dataset/features.rb +1 -4
- data/lib/sequel/dataset/graph.rb +3 -47
- data/lib/sequel/dataset/misc.rb +4 -33
- data/lib/sequel/dataset/prepared_statements.rb +3 -1
- data/lib/sequel/dataset/query.rb +116 -240
- data/lib/sequel/dataset/sql.rb +19 -97
- data/lib/sequel/deprecated.rb +0 -16
- data/lib/sequel/exceptions.rb +0 -3
- data/lib/sequel/extensions/_pretty_table.rb +1 -1
- data/lib/sequel/extensions/columns_introspection.rb +1 -12
- data/lib/sequel/extensions/constraint_validations.rb +3 -3
- data/lib/sequel/extensions/core_extensions.rb +0 -9
- data/lib/sequel/extensions/date_arithmetic.rb +1 -2
- data/lib/sequel/extensions/graph_each.rb +11 -0
- data/lib/sequel/extensions/migration.rb +5 -5
- data/lib/sequel/extensions/null_dataset.rb +11 -13
- data/lib/sequel/extensions/pagination.rb +3 -6
- data/lib/sequel/extensions/pg_array.rb +6 -4
- data/lib/sequel/extensions/pg_array_ops.rb +35 -1
- data/lib/sequel/extensions/pg_json.rb +12 -2
- data/lib/sequel/extensions/pg_json_ops.rb +266 -0
- data/lib/sequel/extensions/pg_range.rb +2 -2
- data/lib/sequel/extensions/pg_range_ops.rb +0 -8
- data/lib/sequel/extensions/pg_row.rb +2 -2
- data/lib/sequel/extensions/pretty_table.rb +0 -4
- data/lib/sequel/extensions/query.rb +3 -8
- data/lib/sequel/extensions/schema_caching.rb +0 -7
- data/lib/sequel/extensions/schema_dumper.rb +10 -17
- data/lib/sequel/extensions/select_remove.rb +0 -4
- data/lib/sequel/extensions/set_overrides.rb +28 -0
- data/lib/sequel/extensions/to_dot.rb +6 -10
- data/lib/sequel/model.rb +6 -7
- data/lib/sequel/model/associations.rb +127 -182
- data/lib/sequel/model/base.rb +88 -211
- data/lib/sequel/model/errors.rb +0 -13
- data/lib/sequel/model/plugins.rb +2 -2
- data/lib/sequel/no_core_ext.rb +0 -1
- data/lib/sequel/plugins/after_initialize.rb +11 -17
- data/lib/sequel/plugins/association_autoreloading.rb +1 -47
- data/lib/sequel/plugins/association_dependencies.rb +2 -2
- data/lib/sequel/plugins/auto_validations.rb +2 -8
- data/lib/sequel/plugins/blacklist_security.rb +32 -2
- data/lib/sequel/plugins/caching.rb +1 -1
- data/lib/sequel/plugins/class_table_inheritance.rb +2 -2
- data/lib/sequel/plugins/composition.rb +10 -8
- data/lib/sequel/plugins/constraint_validations.rb +2 -2
- data/lib/sequel/plugins/dataset_associations.rb +4 -0
- data/lib/sequel/plugins/defaults_setter.rb +8 -6
- data/lib/sequel/plugins/dirty.rb +6 -6
- data/lib/sequel/plugins/force_encoding.rb +13 -8
- data/lib/sequel/plugins/hook_class_methods.rb +1 -7
- data/lib/sequel/plugins/json_serializer.rb +13 -74
- data/lib/sequel/plugins/lazy_attributes.rb +2 -4
- data/lib/sequel/plugins/list.rb +1 -1
- data/lib/sequel/plugins/many_through_many.rb +4 -11
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +1 -49
- data/lib/sequel/plugins/nested_attributes.rb +1 -1
- data/lib/sequel/plugins/optimistic_locking.rb +3 -5
- data/lib/sequel/plugins/pg_array_associations.rb +453 -0
- data/lib/sequel/plugins/pg_typecast_on_load.rb +23 -7
- data/lib/sequel/plugins/prepared_statements.rb +1 -1
- data/lib/sequel/plugins/prepared_statements_associations.rb +20 -14
- data/lib/sequel/plugins/prepared_statements_safe.rb +2 -2
- data/lib/sequel/plugins/rcte_tree.rb +1 -1
- data/lib/sequel/plugins/serialization.rb +5 -4
- data/lib/sequel/plugins/serialization_modification_detection.rb +1 -1
- data/lib/sequel/plugins/sharding.rb +7 -1
- data/lib/sequel/plugins/single_table_inheritance.rb +1 -1
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/touch.rb +2 -2
- data/lib/sequel/plugins/tree.rb +1 -1
- data/lib/sequel/plugins/typecast_on_load.rb +19 -4
- data/lib/sequel/plugins/validation_class_methods.rb +0 -30
- data/lib/sequel/plugins/validation_helpers.rb +13 -31
- data/lib/sequel/plugins/xml_serializer.rb +18 -57
- data/lib/sequel/sql.rb +20 -22
- data/lib/sequel/version.rb +2 -2
- data/spec/adapters/db2_spec.rb +14 -23
- data/spec/adapters/firebird_spec.rb +25 -29
- data/spec/adapters/informix_spec.rb +11 -14
- data/spec/adapters/mssql_spec.rb +71 -77
- data/spec/adapters/mysql_spec.rb +165 -172
- data/spec/adapters/oracle_spec.rb +36 -39
- data/spec/adapters/postgres_spec.rb +175 -100
- data/spec/adapters/spec_helper.rb +13 -11
- data/spec/adapters/sqlite_spec.rb +36 -44
- data/spec/core/connection_pool_spec.rb +2 -1
- data/spec/core/database_spec.rb +55 -55
- data/spec/core/dataset_spec.rb +45 -249
- data/spec/core/deprecated_spec.rb +0 -8
- data/spec/core/expression_filters_spec.rb +23 -5
- data/spec/core/object_graph_spec.rb +4 -66
- data/spec/core/schema_spec.rb +35 -12
- data/spec/core/spec_helper.rb +3 -2
- data/spec/core_extensions_spec.rb +17 -19
- data/spec/extensions/arbitrary_servers_spec.rb +2 -3
- data/spec/extensions/association_dependencies_spec.rb +14 -14
- data/spec/extensions/auto_validations_spec.rb +7 -0
- data/spec/extensions/blacklist_security_spec.rb +5 -5
- data/spec/extensions/blank_spec.rb +2 -0
- data/spec/extensions/class_table_inheritance_spec.rb +2 -2
- data/spec/extensions/columns_introspection_spec.rb +2 -29
- data/spec/extensions/composition_spec.rb +10 -17
- data/spec/extensions/core_refinements_spec.rb +5 -1
- data/spec/extensions/dataset_associations_spec.rb +18 -0
- data/spec/extensions/date_arithmetic_spec.rb +2 -2
- data/spec/extensions/defaults_setter_spec.rb +9 -9
- data/spec/extensions/dirty_spec.rb +0 -5
- data/spec/extensions/eval_inspect_spec.rb +2 -0
- data/spec/extensions/force_encoding_spec.rb +2 -18
- data/spec/extensions/hash_aliases_spec.rb +8 -0
- data/spec/extensions/hook_class_methods_spec.rb +39 -58
- data/spec/extensions/inflector_spec.rb +2 -0
- data/spec/extensions/instance_filters_spec.rb +8 -8
- data/spec/extensions/json_serializer_spec.rb +1 -41
- data/spec/extensions/list_spec.rb +1 -1
- data/spec/extensions/many_through_many_spec.rb +106 -109
- data/spec/extensions/migration_spec.rb +2 -0
- data/spec/extensions/named_timezones_spec.rb +1 -0
- data/spec/extensions/pg_array_associations_spec.rb +603 -0
- data/spec/extensions/pg_array_ops_spec.rb +25 -0
- data/spec/extensions/pg_array_spec.rb +9 -1
- data/spec/extensions/pg_hstore_ops_spec.rb +13 -0
- data/spec/extensions/pg_hstore_spec.rb +1 -0
- data/spec/extensions/pg_json_ops_spec.rb +131 -0
- data/spec/extensions/pg_json_spec.rb +10 -4
- data/spec/extensions/pg_range_ops_spec.rb +2 -5
- data/spec/extensions/pg_range_spec.rb +6 -2
- data/spec/extensions/pg_row_ops_spec.rb +2 -0
- data/spec/extensions/prepared_statements_associations_spec.rb +26 -5
- data/spec/extensions/rcte_tree_spec.rb +15 -15
- data/spec/extensions/schema_dumper_spec.rb +0 -1
- data/spec/extensions/schema_spec.rb +9 -9
- data/spec/extensions/serialization_modification_detection_spec.rb +1 -1
- data/spec/extensions/serialization_spec.rb +18 -29
- data/spec/extensions/set_overrides_spec.rb +4 -0
- data/spec/extensions/{many_to_one_pk_lookup_spec.rb → shared_caching_spec.rb} +1 -4
- data/spec/extensions/single_table_inheritance_spec.rb +4 -4
- data/spec/extensions/spec_helper.rb +8 -9
- data/spec/extensions/sql_expr_spec.rb +2 -0
- data/spec/extensions/string_date_time_spec.rb +2 -0
- data/spec/extensions/string_stripper_spec.rb +2 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +12 -12
- data/spec/extensions/thread_local_timezones_spec.rb +2 -0
- data/spec/extensions/timestamps_spec.rb +1 -1
- data/spec/extensions/to_dot_spec.rb +1 -1
- data/spec/extensions/touch_spec.rb +24 -24
- data/spec/extensions/tree_spec.rb +7 -7
- data/spec/extensions/typecast_on_load_spec.rb +8 -1
- data/spec/extensions/update_primary_key_spec.rb +10 -10
- data/spec/extensions/validation_class_methods_spec.rb +10 -39
- data/spec/extensions/validation_helpers_spec.rb +29 -47
- data/spec/extensions/xml_serializer_spec.rb +1 -23
- data/spec/integration/associations_test.rb +231 -40
- data/spec/integration/database_test.rb +1 -1
- data/spec/integration/dataset_test.rb +64 -64
- data/spec/integration/eager_loader_test.rb +28 -28
- data/spec/integration/migrator_test.rb +1 -1
- data/spec/integration/model_test.rb +2 -2
- data/spec/integration/plugin_test.rb +21 -21
- data/spec/integration/prepared_statement_test.rb +7 -7
- data/spec/integration/schema_test.rb +115 -110
- data/spec/integration/spec_helper.rb +17 -27
- data/spec/integration/timezone_test.rb +1 -1
- data/spec/integration/transaction_test.rb +10 -10
- data/spec/integration/type_test.rb +2 -2
- data/spec/model/association_reflection_spec.rb +2 -28
- data/spec/model/associations_spec.rb +239 -188
- data/spec/model/base_spec.rb +27 -68
- data/spec/model/dataset_methods_spec.rb +4 -4
- data/spec/model/eager_loading_spec.rb +160 -172
- data/spec/model/hooks_spec.rb +62 -79
- data/spec/model/model_spec.rb +36 -51
- data/spec/model/plugins_spec.rb +5 -19
- data/spec/model/record_spec.rb +125 -151
- data/spec/model/spec_helper.rb +8 -6
- data/spec/model/validations_spec.rb +4 -17
- data/spec/spec_config.rb +2 -10
- metadata +50 -56
- data/lib/sequel/deprecated_core_extensions.rb +0 -135
- data/lib/sequel/extensions/pg_auto_parameterize.rb +0 -185
- data/lib/sequel/extensions/pg_statement_cache.rb +0 -318
- data/lib/sequel/plugins/identity_map.rb +0 -260
- data/lib/sequel_core.rb +0 -2
- data/lib/sequel_model.rb +0 -2
- data/spec/extensions/association_autoreloading_spec.rb +0 -102
- data/spec/extensions/identity_map_spec.rb +0 -337
- data/spec/extensions/pg_auto_parameterize_spec.rb +0 -70
- data/spec/extensions/pg_statement_cache_spec.rb +0 -208
- data/spec/rcov.opts +0 -8
- data/spec/spec_config.rb.example +0 -10
data/spec/model/hooks_spec.rb
CHANGED
|
@@ -1,22 +1,5 @@
|
|
|
1
1
|
require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
|
|
2
2
|
|
|
3
|
-
describe "Model#after_initialize" do
|
|
4
|
-
qspecify "should be called after initialization for both new object and objects retrieved from the database" do
|
|
5
|
-
|
|
6
|
-
a = Class.new(Sequel::Model) do
|
|
7
|
-
self::Foo = []
|
|
8
|
-
columns :x, :y
|
|
9
|
-
def after_initialize
|
|
10
|
-
model::Foo << @values.clone
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
a.new(:x => 1, :y => 2)
|
|
15
|
-
a.call(:x => 1, :y => 3)
|
|
16
|
-
a::Foo.should == [{:x => 1, :y => 2}, {:x => 1, :y => 3}]
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
3
|
describe "Model#before_create && Model#after_create" do
|
|
21
4
|
before do
|
|
22
5
|
@c = Class.new(Sequel::Model(:items)) do
|
|
@@ -25,22 +8,22 @@ describe "Model#before_create && Model#after_create" do
|
|
|
25
8
|
unrestrict_primary_key
|
|
26
9
|
|
|
27
10
|
def after_create
|
|
28
|
-
|
|
11
|
+
DB << "BLAH after"
|
|
29
12
|
end
|
|
30
13
|
end
|
|
31
|
-
|
|
14
|
+
DB.reset
|
|
32
15
|
end
|
|
33
16
|
|
|
34
17
|
specify "should be called around new record creation" do
|
|
35
|
-
@c.send(:define_method, :before_create){
|
|
18
|
+
@c.send(:define_method, :before_create){DB << "BLAH before"}
|
|
36
19
|
@c.create(:x => 2)
|
|
37
|
-
|
|
20
|
+
DB.sqls.should == ['BLAH before', 'INSERT INTO items (x) VALUES (2)', 'BLAH after', 'SELECT * FROM items WHERE (x = 2) LIMIT 1']
|
|
38
21
|
end
|
|
39
22
|
|
|
40
23
|
specify ".create should cancel the save and raise an error if before_create returns false and raise_on_save_failure is true" do
|
|
41
24
|
@c.send(:define_method, :before_create){false}
|
|
42
25
|
proc{@c.create(:x => 2)}.should raise_error(Sequel::BeforeHookFailed)
|
|
43
|
-
|
|
26
|
+
DB.sqls.should == []
|
|
44
27
|
proc{@c.load(:id => 2233).save}.should_not raise_error(Sequel::ValidationFailed)
|
|
45
28
|
end
|
|
46
29
|
|
|
@@ -48,7 +31,7 @@ describe "Model#before_create && Model#after_create" do
|
|
|
48
31
|
@c.send(:define_method, :before_create){false}
|
|
49
32
|
@c.raise_on_save_failure = false
|
|
50
33
|
@c.create(:x => 2).should == nil
|
|
51
|
-
|
|
34
|
+
DB.sqls.should == []
|
|
52
35
|
end
|
|
53
36
|
end
|
|
54
37
|
|
|
@@ -57,24 +40,24 @@ describe "Model#before_update && Model#after_update" do
|
|
|
57
40
|
@c = Class.new(Sequel::Model(:items)) do
|
|
58
41
|
columns :id, :x
|
|
59
42
|
def after_update
|
|
60
|
-
|
|
43
|
+
DB << "BLAH after"
|
|
61
44
|
end
|
|
62
45
|
end
|
|
63
|
-
|
|
46
|
+
DB.reset
|
|
64
47
|
end
|
|
65
48
|
|
|
66
49
|
specify "should be called around record update" do
|
|
67
|
-
@c.send(:define_method, :before_update){
|
|
50
|
+
@c.send(:define_method, :before_update){DB << "BLAH before"}
|
|
68
51
|
m = @c.load(:id => 2233, :x=>123)
|
|
69
52
|
m.save
|
|
70
|
-
|
|
53
|
+
DB.sqls.should == ['BLAH before', 'UPDATE items SET x = 123 WHERE (id = 2233)', 'BLAH after']
|
|
71
54
|
end
|
|
72
55
|
|
|
73
56
|
specify "#save should cancel the save and raise an error if before_update returns false and raise_on_save_failure is true" do
|
|
74
57
|
@c.send(:define_method, :before_update){false}
|
|
75
58
|
proc{@c.load(:id => 2233).save}.should_not raise_error(Sequel::ValidationFailed)
|
|
76
59
|
proc{@c.load(:id => 2233).save}.should raise_error(Sequel::BeforeHookFailed)
|
|
77
|
-
|
|
60
|
+
DB.sqls.should == []
|
|
78
61
|
end
|
|
79
62
|
|
|
80
63
|
specify "#save should cancel the save and raise an error if before_update returns false and raise_on_failure option is true" do
|
|
@@ -82,14 +65,14 @@ describe "Model#before_update && Model#after_update" do
|
|
|
82
65
|
@c.raise_on_save_failure = false
|
|
83
66
|
proc{@c.load(:id => 2233).save(:raise_on_failure => true)}.should_not raise_error(Sequel::ValidationFailed)
|
|
84
67
|
proc{@c.load(:id => 2233).save(:raise_on_failure => true)}.should raise_error(Sequel::BeforeHookFailed)
|
|
85
|
-
|
|
68
|
+
DB.sqls.should == []
|
|
86
69
|
end
|
|
87
70
|
|
|
88
71
|
specify "#save should cancel the save and return nil if before_update returns false and raise_on_save_failure is false" do
|
|
89
72
|
@c.send(:define_method, :before_update){false}
|
|
90
73
|
@c.raise_on_save_failure = false
|
|
91
74
|
@c.load(:id => 2233).save.should == nil
|
|
92
|
-
|
|
75
|
+
DB.sqls.should == []
|
|
93
76
|
end
|
|
94
77
|
end
|
|
95
78
|
|
|
@@ -98,32 +81,32 @@ describe "Model#before_save && Model#after_save" do
|
|
|
98
81
|
@c = Class.new(Sequel::Model(:items)) do
|
|
99
82
|
columns :x
|
|
100
83
|
def after_save
|
|
101
|
-
|
|
84
|
+
DB << "BLAH after"
|
|
102
85
|
end
|
|
103
86
|
end
|
|
104
|
-
|
|
87
|
+
DB.reset
|
|
105
88
|
end
|
|
106
89
|
|
|
107
90
|
specify "should be called around record update" do
|
|
108
|
-
@c.send(:define_method, :before_save){
|
|
91
|
+
@c.send(:define_method, :before_save){DB << "BLAH before"}
|
|
109
92
|
m = @c.load(:id => 2233, :x=>123)
|
|
110
93
|
m.save
|
|
111
|
-
|
|
94
|
+
DB.sqls.should == ['BLAH before', 'UPDATE items SET x = 123 WHERE (id = 2233)', 'BLAH after']
|
|
112
95
|
end
|
|
113
96
|
|
|
114
97
|
specify "should be called around record creation" do
|
|
115
|
-
@c.send(:define_method, :before_save){
|
|
98
|
+
@c.send(:define_method, :before_save){DB << "BLAH before"}
|
|
116
99
|
@c.set_primary_key :x
|
|
117
100
|
@c.unrestrict_primary_key
|
|
118
101
|
@c.create(:x => 2)
|
|
119
|
-
|
|
102
|
+
DB.sqls.should == ['BLAH before', 'INSERT INTO items (x) VALUES (2)', 'BLAH after', 'SELECT * FROM items WHERE (x = 2) LIMIT 1']
|
|
120
103
|
end
|
|
121
104
|
|
|
122
105
|
specify "#save should cancel the save and raise an error if before_save returns false and raise_on_save_failure is true" do
|
|
123
106
|
@c.send(:define_method, :before_save){false}
|
|
124
107
|
proc{@c.load(:id => 2233).save}.should_not raise_error(Sequel::ValidationFailed)
|
|
125
108
|
proc{@c.load(:id => 2233).save}.should raise_error(Sequel::BeforeHookFailed)
|
|
126
|
-
|
|
109
|
+
DB.sqls.should == []
|
|
127
110
|
end
|
|
128
111
|
|
|
129
112
|
specify "#save should cancel the save and raise an error if before_save returns false and raise_on_failure option is true" do
|
|
@@ -131,20 +114,20 @@ describe "Model#before_save && Model#after_save" do
|
|
|
131
114
|
@c.raise_on_save_failure = false
|
|
132
115
|
proc{@c.load(:id => 2233).save(:raise_on_failure => true)}.should_not raise_error(Sequel::ValidationFailed)
|
|
133
116
|
proc{@c.load(:id => 2233).save(:raise_on_failure => true)}.should raise_error(Sequel::BeforeHookFailed)
|
|
134
|
-
|
|
117
|
+
DB.sqls.should == []
|
|
135
118
|
end
|
|
136
119
|
|
|
137
120
|
specify "#save should cancel the save and return nil if before_save returns false and raise_on_save_failure is false" do
|
|
138
121
|
@c.send(:define_method, :before_save){false}
|
|
139
122
|
@c.raise_on_save_failure = false
|
|
140
123
|
@c.load(:id => 2233).save.should == nil
|
|
141
|
-
|
|
124
|
+
DB.sqls.should == []
|
|
142
125
|
end
|
|
143
126
|
|
|
144
127
|
specify "#save should have a raised exception reference the model instance" do
|
|
145
128
|
@c.send(:define_method, :before_save){false}
|
|
146
129
|
proc{@c.create(:x => 2233)}.should raise_error(Sequel::HookFailed){|e| e.model.should == @c.load(:x=>2233)}
|
|
147
|
-
|
|
130
|
+
DB.sqls.should == []
|
|
148
131
|
end
|
|
149
132
|
end
|
|
150
133
|
|
|
@@ -152,37 +135,37 @@ describe "Model#before_destroy && Model#after_destroy" do
|
|
|
152
135
|
before do
|
|
153
136
|
@c = Class.new(Sequel::Model(:items)) do
|
|
154
137
|
def after_destroy
|
|
155
|
-
|
|
138
|
+
DB << "BLAH after"
|
|
156
139
|
end
|
|
157
140
|
end
|
|
158
|
-
|
|
141
|
+
DB.reset
|
|
159
142
|
end
|
|
160
143
|
|
|
161
144
|
specify "should be called around record destruction" do
|
|
162
|
-
@c.send(:define_method, :before_destroy){
|
|
145
|
+
@c.send(:define_method, :before_destroy){DB << "BLAH before"}
|
|
163
146
|
m = @c.load(:id => 2233)
|
|
164
147
|
m.destroy
|
|
165
|
-
|
|
148
|
+
DB.sqls.should == ['BLAH before', 'DELETE FROM items WHERE id = 2233', 'BLAH after']
|
|
166
149
|
end
|
|
167
150
|
|
|
168
151
|
specify "#destroy should cancel the destroy and raise an error if before_destroy returns false and raise_on_save_failure is true" do
|
|
169
152
|
@c.send(:define_method, :before_destroy){false}
|
|
170
153
|
proc{@c.load(:id => 2233).destroy}.should raise_error(Sequel::BeforeHookFailed)
|
|
171
|
-
|
|
154
|
+
DB.sqls.should == []
|
|
172
155
|
end
|
|
173
156
|
|
|
174
157
|
specify "#destroy should cancel the destroy and raise an error if before_destroy returns false and raise_on_failure option is true" do
|
|
175
158
|
@c.send(:define_method, :before_destroy){false}
|
|
176
159
|
@c.raise_on_save_failure = false
|
|
177
160
|
proc{@c.load(:id => 2233).destroy(:raise_on_failure => true)}.should raise_error(Sequel::BeforeHookFailed)
|
|
178
|
-
|
|
161
|
+
DB.sqls.should == []
|
|
179
162
|
end
|
|
180
163
|
|
|
181
164
|
specify "#destroy should cancel the destroy and return nil if before_destroy returns false and raise_on_save_failure is false" do
|
|
182
165
|
@c.send(:define_method, :before_destroy){false}
|
|
183
166
|
@c.raise_on_save_failure = false
|
|
184
167
|
@c.load(:id => 2233).destroy.should == nil
|
|
185
|
-
|
|
168
|
+
DB.sqls.should == []
|
|
186
169
|
end
|
|
187
170
|
end
|
|
188
171
|
|
|
@@ -191,7 +174,7 @@ describe "Model#before_validation && Model#after_validation" do
|
|
|
191
174
|
@c = Class.new(Sequel::Model(:items)) do
|
|
192
175
|
columns :id
|
|
193
176
|
def after_validation
|
|
194
|
-
|
|
177
|
+
DB << "BLAH after"
|
|
195
178
|
end
|
|
196
179
|
|
|
197
180
|
def validate
|
|
@@ -201,33 +184,33 @@ describe "Model#before_validation && Model#after_validation" do
|
|
|
201
184
|
end
|
|
202
185
|
|
|
203
186
|
specify "should be called around validation" do
|
|
204
|
-
@c.send(:define_method, :before_validation){
|
|
187
|
+
@c.send(:define_method, :before_validation){DB << "BLAH before"}
|
|
205
188
|
m = @c.load(:id => 2233)
|
|
206
189
|
m.should be_valid
|
|
207
|
-
|
|
190
|
+
DB.sqls.should == ['BLAH before', 'BLAH after']
|
|
208
191
|
|
|
209
192
|
m = @c.load(:id => 22)
|
|
210
193
|
m.should_not be_valid
|
|
211
|
-
|
|
194
|
+
DB.sqls.should == ['BLAH before', 'BLAH after']
|
|
212
195
|
end
|
|
213
196
|
|
|
214
197
|
specify "should be called when calling save" do
|
|
215
|
-
@c.send(:define_method, :before_validation){
|
|
198
|
+
@c.send(:define_method, :before_validation){DB << "BLAH before"}
|
|
216
199
|
m = @c.load(:id => 2233, :x=>123)
|
|
217
200
|
m.save.should == m
|
|
218
|
-
|
|
201
|
+
DB.sqls.should == ['BLAH before', 'BLAH after', 'UPDATE items SET x = 123 WHERE (id = 2233)']
|
|
219
202
|
|
|
220
203
|
m = @c.load(:id => 22)
|
|
221
204
|
m.raise_on_save_failure = false
|
|
222
205
|
m.save.should == nil
|
|
223
|
-
|
|
206
|
+
DB.sqls.should == ['BLAH before', 'BLAH after']
|
|
224
207
|
end
|
|
225
208
|
|
|
226
209
|
specify "#save should cancel the save and raise an error if before_validation returns false and raise_on_save_failure is true" do
|
|
227
210
|
@c.send(:define_method, :before_validation){false}
|
|
228
211
|
proc{@c.load(:id => 2233).save}.should_not raise_error(Sequel::ValidationFailed)
|
|
229
212
|
proc{@c.load(:id => 2233).save}.should raise_error(Sequel::BeforeHookFailed)
|
|
230
|
-
|
|
213
|
+
DB.sqls.should == []
|
|
231
214
|
end
|
|
232
215
|
|
|
233
216
|
specify "#save should cancel the save and raise an error if before_validation returns false and raise_on_failure option is true" do
|
|
@@ -235,14 +218,14 @@ describe "Model#before_validation && Model#after_validation" do
|
|
|
235
218
|
@c.raise_on_save_failure = false
|
|
236
219
|
proc{@c.load(:id => 2233).save(:raise_on_failure => true)}.should_not raise_error(Sequel::ValidationFailed)
|
|
237
220
|
proc{@c.load(:id => 2233).save(:raise_on_failure => true)}.should raise_error(Sequel::BeforeHookFailed)
|
|
238
|
-
|
|
221
|
+
DB.sqls.should == []
|
|
239
222
|
end
|
|
240
223
|
|
|
241
224
|
specify "#save should cancel the save and return nil if before_validation returns false and raise_on_save_failure is false" do
|
|
242
225
|
@c.send(:define_method, :before_validation){false}
|
|
243
226
|
@c.raise_on_save_failure = false
|
|
244
227
|
@c.load(:id => 2233).save.should == nil
|
|
245
|
-
|
|
228
|
+
DB.sqls.should == []
|
|
246
229
|
end
|
|
247
230
|
|
|
248
231
|
specify "#valid? should return false if before_validation returns false" do
|
|
@@ -256,82 +239,82 @@ describe "Model around filters" do
|
|
|
256
239
|
@c = Class.new(Sequel::Model(:items)) do
|
|
257
240
|
columns :id, :x
|
|
258
241
|
end
|
|
259
|
-
|
|
242
|
+
DB.reset
|
|
260
243
|
end
|
|
261
244
|
|
|
262
245
|
specify "around_create should be called around new record creation" do
|
|
263
246
|
@c.class_eval do
|
|
264
247
|
def around_create
|
|
265
|
-
|
|
248
|
+
DB << 'ac_before'
|
|
266
249
|
super
|
|
267
|
-
|
|
250
|
+
DB << 'ac_after'
|
|
268
251
|
end
|
|
269
252
|
end
|
|
270
253
|
@c.create(:x => 2)
|
|
271
|
-
|
|
254
|
+
DB.sqls.should == ['ac_before', 'INSERT INTO items (x) VALUES (2)', 'ac_after', "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
272
255
|
end
|
|
273
256
|
|
|
274
257
|
specify "around_delete should be called around record destruction" do
|
|
275
258
|
@c.class_eval do
|
|
276
259
|
def around_destroy
|
|
277
|
-
|
|
260
|
+
DB << 'ad_before'
|
|
278
261
|
super
|
|
279
|
-
|
|
262
|
+
DB << 'ad_after'
|
|
280
263
|
end
|
|
281
264
|
end
|
|
282
265
|
@c.load(:id=>1, :x => 2).destroy
|
|
283
|
-
|
|
266
|
+
DB.sqls.should == ['ad_before', 'DELETE FROM items WHERE id = 1', 'ad_after']
|
|
284
267
|
end
|
|
285
268
|
|
|
286
269
|
specify "around_update should be called around updating existing records" do
|
|
287
270
|
@c.class_eval do
|
|
288
271
|
def around_update
|
|
289
|
-
|
|
272
|
+
DB << 'au_before'
|
|
290
273
|
super
|
|
291
|
-
|
|
274
|
+
DB << 'au_after'
|
|
292
275
|
end
|
|
293
276
|
end
|
|
294
277
|
@c.load(:id=>1, :x => 2).save
|
|
295
|
-
|
|
278
|
+
DB.sqls.should == ['au_before', 'UPDATE items SET x = 2 WHERE (id = 1)', 'au_after']
|
|
296
279
|
end
|
|
297
280
|
|
|
298
281
|
specify "around_save should be called around saving both new and existing records, around either after_create and after_update" do
|
|
299
282
|
@c.class_eval do
|
|
300
283
|
def around_update
|
|
301
|
-
|
|
284
|
+
DB << 'au_before'
|
|
302
285
|
super
|
|
303
|
-
|
|
286
|
+
DB << 'au_after'
|
|
304
287
|
end
|
|
305
288
|
def around_create
|
|
306
|
-
|
|
289
|
+
DB << 'ac_before'
|
|
307
290
|
super
|
|
308
|
-
|
|
291
|
+
DB << 'ac_after'
|
|
309
292
|
end
|
|
310
293
|
def around_save
|
|
311
|
-
|
|
294
|
+
DB << 'as_before'
|
|
312
295
|
super
|
|
313
|
-
|
|
296
|
+
DB << 'as_after'
|
|
314
297
|
end
|
|
315
298
|
end
|
|
316
299
|
@c.create(:x => 2)
|
|
317
|
-
|
|
300
|
+
DB.sqls.should == ['as_before', 'ac_before', 'INSERT INTO items (x) VALUES (2)', 'ac_after', 'as_after', "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
318
301
|
@c.load(:id=>1, :x => 2).save
|
|
319
|
-
|
|
302
|
+
DB.sqls.should == ['as_before', 'au_before', 'UPDATE items SET x = 2 WHERE (id = 1)', 'au_after', 'as_after']
|
|
320
303
|
end
|
|
321
304
|
|
|
322
305
|
specify "around_validation should be called around validating records" do
|
|
323
306
|
@c.class_eval do
|
|
324
307
|
def around_validation
|
|
325
|
-
|
|
308
|
+
DB << 'av_before'
|
|
326
309
|
super
|
|
327
|
-
|
|
310
|
+
DB << 'av_after'
|
|
328
311
|
end
|
|
329
312
|
def validate
|
|
330
|
-
|
|
313
|
+
DB << 'validate'
|
|
331
314
|
end
|
|
332
315
|
end
|
|
333
316
|
@c.new(:x => 2).valid?.should == true
|
|
334
|
-
|
|
317
|
+
DB.sqls.should == [ 'av_before', 'validate', 'av_after' ]
|
|
335
318
|
end
|
|
336
319
|
|
|
337
320
|
specify "around_validation should be able to catch validation errors and modify them" do
|
data/spec/model/model_spec.rb
CHANGED
|
@@ -150,12 +150,12 @@ describe Sequel::Model do
|
|
|
150
150
|
end
|
|
151
151
|
|
|
152
152
|
it "should be associated with a dataset" do
|
|
153
|
-
model_a = Class.new(Sequel::Model) { set_dataset
|
|
153
|
+
model_a = Class.new(Sequel::Model) { set_dataset DB[:as] }
|
|
154
154
|
|
|
155
155
|
model_a.dataset.should be_a_kind_of(Sequel::Mock::Dataset)
|
|
156
156
|
model_a.dataset.opts[:from].should == [:as]
|
|
157
157
|
|
|
158
|
-
model_b = Class.new(Sequel::Model) { set_dataset
|
|
158
|
+
model_b = Class.new(Sequel::Model) { set_dataset DB[:bs] }
|
|
159
159
|
|
|
160
160
|
model_b.dataset.should be_a_kind_of(Sequel::Mock::Dataset)
|
|
161
161
|
model_b.dataset.opts[:from].should == [:bs]
|
|
@@ -183,36 +183,36 @@ describe Sequel::Model do
|
|
|
183
183
|
end
|
|
184
184
|
|
|
185
185
|
it "allows dataset change" do
|
|
186
|
-
@model.set_dataset(
|
|
186
|
+
@model.set_dataset(DB[:foo])
|
|
187
187
|
@model.table_name.should == :foo
|
|
188
188
|
end
|
|
189
189
|
|
|
190
190
|
it "allows set_dataset to accept a Symbol" do
|
|
191
|
-
@model.db =
|
|
191
|
+
@model.db = DB
|
|
192
192
|
@model.set_dataset(:foo)
|
|
193
193
|
@model.table_name.should == :foo
|
|
194
194
|
end
|
|
195
195
|
|
|
196
196
|
it "allows set_dataset to accept a LiteralString" do
|
|
197
|
-
@model.db =
|
|
197
|
+
@model.db = DB
|
|
198
198
|
@model.set_dataset(Sequel.lit('foo'))
|
|
199
199
|
@model.table_name.should == Sequel.lit('foo')
|
|
200
200
|
end
|
|
201
201
|
|
|
202
202
|
it "allows set_dataset to acceptan SQL::Identifier" do
|
|
203
|
-
@model.db =
|
|
203
|
+
@model.db = DB
|
|
204
204
|
@model.set_dataset(Sequel.identifier(:foo))
|
|
205
205
|
@model.table_name.should == Sequel.identifier(:foo)
|
|
206
206
|
end
|
|
207
207
|
|
|
208
208
|
it "allows set_dataset to acceptan SQL::QualifiedIdentifier" do
|
|
209
|
-
@model.db =
|
|
209
|
+
@model.db = DB
|
|
210
210
|
@model.set_dataset(Sequel.qualify(:bar, :foo))
|
|
211
211
|
@model.table_name.should == Sequel.qualify(:bar, :foo)
|
|
212
212
|
end
|
|
213
213
|
|
|
214
214
|
it "allows set_dataset to acceptan SQL::AliasedExpression" do
|
|
215
|
-
@model.db =
|
|
215
|
+
@model.db = DB
|
|
216
216
|
@model.set_dataset(Sequel.as(:foo, :bar))
|
|
217
217
|
@model.table_name.should == :bar
|
|
218
218
|
end
|
|
@@ -227,14 +227,14 @@ describe Sequel::Model do
|
|
|
227
227
|
end
|
|
228
228
|
|
|
229
229
|
it "set_dataset should add the destroy method to the dataset that destroys each object" do
|
|
230
|
-
ds =
|
|
230
|
+
ds = DB[:foo]
|
|
231
231
|
ds.should_not respond_to(:destroy)
|
|
232
232
|
@model.set_dataset(ds)
|
|
233
233
|
ds.should respond_to(:destroy)
|
|
234
|
-
|
|
234
|
+
DB.sqls
|
|
235
235
|
ds._fetch = [{:id=>1}, {:id=>2}]
|
|
236
236
|
ds.destroy.should == 2
|
|
237
|
-
|
|
237
|
+
DB.sqls.should == ["SELECT * FROM foo", "DELETE FROM foo WHERE id = 1", "DELETE FROM foo WHERE id = 2"]
|
|
238
238
|
end
|
|
239
239
|
|
|
240
240
|
it "set_dataset should add the destroy method that respects sharding with transactions" do
|
|
@@ -254,12 +254,12 @@ describe Sequel::Model do
|
|
|
254
254
|
|
|
255
255
|
it "should not raise an error if there is a problem getting the columns for a dataset" do
|
|
256
256
|
def @model.columns() raise Sequel::Error end
|
|
257
|
-
proc{@model.set_dataset(
|
|
257
|
+
proc{@model.set_dataset(DB[:foo].join(:blah))}.should_not raise_error
|
|
258
258
|
end
|
|
259
259
|
|
|
260
260
|
it "doesn't raise an error on set_dataset if there is an error raised getting the schema" do
|
|
261
261
|
def @model.get_db_schema(*) raise Sequel::Error end
|
|
262
|
-
proc{@model.set_dataset(
|
|
262
|
+
proc{@model.set_dataset(DB[:foo])}.should_not raise_error
|
|
263
263
|
end
|
|
264
264
|
|
|
265
265
|
it "doesn't raise an error on inherited if there is an error setting the dataset" do
|
|
@@ -328,19 +328,12 @@ describe Sequel::Model, "constructors" do
|
|
|
328
328
|
o.values.should == {:a=>1}
|
|
329
329
|
o.new?.should be_false
|
|
330
330
|
end
|
|
331
|
-
|
|
332
|
-
qspecify "should have .new with a second true argument create an existing object" do
|
|
333
|
-
o = @m.new({:a=>1}, true)
|
|
334
|
-
o.should be_a_kind_of(@m)
|
|
335
|
-
o.values.should == {:a=>1}
|
|
336
|
-
o.new?.should be_false
|
|
337
|
-
end
|
|
338
331
|
end
|
|
339
332
|
|
|
340
333
|
describe Sequel::Model, "new" do
|
|
341
334
|
before do
|
|
342
335
|
@m = Class.new(Sequel::Model) do
|
|
343
|
-
set_dataset
|
|
336
|
+
set_dataset DB[:items]
|
|
344
337
|
columns :x, :id
|
|
345
338
|
end
|
|
346
339
|
end
|
|
@@ -374,7 +367,7 @@ end
|
|
|
374
367
|
describe Sequel::Model, ".subset" do
|
|
375
368
|
before do
|
|
376
369
|
@c = Class.new(Sequel::Model(:items))
|
|
377
|
-
|
|
370
|
+
DB.reset
|
|
378
371
|
end
|
|
379
372
|
|
|
380
373
|
specify "should create a filter on the underlying dataset" do
|
|
@@ -405,29 +398,29 @@ describe Sequel::Model, ".find" do
|
|
|
405
398
|
before do
|
|
406
399
|
@c = Class.new(Sequel::Model(:items))
|
|
407
400
|
@c.dataset._fetch = {:name => 'sharon', :id => 1}
|
|
408
|
-
|
|
401
|
+
DB.reset
|
|
409
402
|
end
|
|
410
403
|
|
|
411
404
|
it "should return the first record matching the given filter" do
|
|
412
405
|
@c.find(:name => 'sharon').should be_a_kind_of(@c)
|
|
413
|
-
|
|
406
|
+
DB.sqls.should == ["SELECT * FROM items WHERE (name = 'sharon') LIMIT 1"]
|
|
414
407
|
|
|
415
408
|
@c.find(Sequel.expr(:name).like('abc%')).should be_a_kind_of(@c)
|
|
416
|
-
|
|
409
|
+
DB.sqls.should == ["SELECT * FROM items WHERE (name LIKE 'abc%' ESCAPE '\\') LIMIT 1"]
|
|
417
410
|
end
|
|
418
411
|
|
|
419
412
|
specify "should accept filter blocks" do
|
|
420
413
|
@c.find{id > 1}.should be_a_kind_of(@c)
|
|
421
|
-
|
|
414
|
+
DB.sqls.should == ["SELECT * FROM items WHERE (id > 1) LIMIT 1"]
|
|
422
415
|
|
|
423
416
|
@c.find{(x > 1) & (y < 2)}.should be_a_kind_of(@c)
|
|
424
|
-
|
|
417
|
+
DB.sqls.should == ["SELECT * FROM items WHERE ((x > 1) AND (y < 2)) LIMIT 1"]
|
|
425
418
|
end
|
|
426
419
|
end
|
|
427
420
|
|
|
428
421
|
describe Sequel::Model, ".fetch" do
|
|
429
422
|
before do
|
|
430
|
-
|
|
423
|
+
DB.reset
|
|
431
424
|
@c = Class.new(Sequel::Model(:items))
|
|
432
425
|
end
|
|
433
426
|
|
|
@@ -448,19 +441,19 @@ describe Sequel::Model, ".find_or_create" do
|
|
|
448
441
|
set_primary_key :id
|
|
449
442
|
columns :x
|
|
450
443
|
end
|
|
451
|
-
|
|
444
|
+
DB.reset
|
|
452
445
|
end
|
|
453
446
|
|
|
454
447
|
it "should find the record" do
|
|
455
448
|
@c.find_or_create(:x => 1).should == @c.load(:x=>1, :id=>1)
|
|
456
|
-
|
|
449
|
+
DB.sqls.should == ["SELECT * FROM items WHERE (x = 1) LIMIT 1"]
|
|
457
450
|
end
|
|
458
451
|
|
|
459
452
|
it "should create the record if not found" do
|
|
460
453
|
@c.instance_dataset._fetch = @c.dataset._fetch = [[], {:x=>1, :id=>1}]
|
|
461
454
|
@c.instance_dataset.autoid = @c.dataset.autoid = 1
|
|
462
455
|
@c.find_or_create(:x => 1).should == @c.load(:x=>1, :id=>1)
|
|
463
|
-
|
|
456
|
+
DB.sqls.should == ["SELECT * FROM items WHERE (x = 1) LIMIT 1",
|
|
464
457
|
"INSERT INTO items (x) VALUES (1)",
|
|
465
458
|
"SELECT * FROM items WHERE (id = 1) LIMIT 1"]
|
|
466
459
|
end
|
|
@@ -469,7 +462,7 @@ describe Sequel::Model, ".find_or_create" do
|
|
|
469
462
|
@c.instance_dataset._fetch = @c.dataset._fetch = [[], {:x=>1, :id=>1}]
|
|
470
463
|
@c.instance_dataset.autoid = @c.dataset.autoid = 1
|
|
471
464
|
@c.find_or_create(:x => 1){|x| x[:y] = 2}.should == @c.load(:x=>1, :id=>1)
|
|
472
|
-
sqls =
|
|
465
|
+
sqls = DB.sqls
|
|
473
466
|
sqls.first.should == "SELECT * FROM items WHERE (x = 1) LIMIT 1"
|
|
474
467
|
["INSERT INTO items (x, y) VALUES (1, 2)", "INSERT INTO items (y, x) VALUES (2, 1)"].should include(sqls[1])
|
|
475
468
|
sqls.last.should == "SELECT * FROM items WHERE (id = 1) LIMIT 1"
|
|
@@ -489,7 +482,7 @@ describe Sequel::Model, "A model class without a primary key" do
|
|
|
489
482
|
columns :x
|
|
490
483
|
no_primary_key
|
|
491
484
|
end
|
|
492
|
-
|
|
485
|
+
DB.reset
|
|
493
486
|
end
|
|
494
487
|
|
|
495
488
|
it "should be able to insert records without selecting them back" do
|
|
@@ -498,7 +491,7 @@ describe Sequel::Model, "A model class without a primary key" do
|
|
|
498
491
|
i.class.should be(@c)
|
|
499
492
|
i.values.to_hash.should == {:x => 1}
|
|
500
493
|
|
|
501
|
-
|
|
494
|
+
DB.sqls.should == ['INSERT INTO items (x) VALUES (1)']
|
|
502
495
|
end
|
|
503
496
|
|
|
504
497
|
it "should raise when deleting" do
|
|
@@ -513,7 +506,7 @@ describe Sequel::Model, "A model class without a primary key" do
|
|
|
513
506
|
o = @c.new(:x => 2)
|
|
514
507
|
o.should be_new
|
|
515
508
|
o.save
|
|
516
|
-
|
|
509
|
+
DB.sqls.should == ['INSERT INTO items (x) VALUES (2)']
|
|
517
510
|
end
|
|
518
511
|
end
|
|
519
512
|
|
|
@@ -526,7 +519,7 @@ describe Sequel::Model, "attribute accessors" do
|
|
|
526
519
|
end
|
|
527
520
|
@dataset = db[:items].columns(:x, :z)
|
|
528
521
|
@c = Class.new(Sequel::Model)
|
|
529
|
-
|
|
522
|
+
DB.reset
|
|
530
523
|
end
|
|
531
524
|
|
|
532
525
|
it "should be created on set_dataset" do
|
|
@@ -581,46 +574,38 @@ describe Sequel::Model, ".[]" do
|
|
|
581
574
|
before do
|
|
582
575
|
@c = Class.new(Sequel::Model(:items))
|
|
583
576
|
@c.dataset._fetch = {:name => 'sharon', :id => 1}
|
|
584
|
-
|
|
577
|
+
DB.reset
|
|
585
578
|
end
|
|
586
579
|
|
|
587
580
|
it "should return the first record for the given pk" do
|
|
588
581
|
@c[1].should == @c.load(:name => 'sharon', :id => 1)
|
|
589
|
-
|
|
582
|
+
DB.sqls.should == ["SELECT * FROM items WHERE id = 1"]
|
|
590
583
|
@c[9999].should == @c.load(:name => 'sharon', :id => 1)
|
|
591
|
-
|
|
584
|
+
DB.sqls.should == ["SELECT * FROM items WHERE id = 9999"]
|
|
592
585
|
end
|
|
593
586
|
|
|
594
587
|
it "should have #[] return nil if no rows match" do
|
|
595
588
|
@c.dataset._fetch = []
|
|
596
589
|
@c[1].should == nil
|
|
597
|
-
|
|
590
|
+
DB.sqls.should == ["SELECT * FROM items WHERE id = 1"]
|
|
598
591
|
end
|
|
599
592
|
|
|
600
593
|
it "should work correctly for custom primary key" do
|
|
601
594
|
@c.set_primary_key :name
|
|
602
595
|
@c['sharon'].should == @c.load(:name => 'sharon', :id => 1)
|
|
603
|
-
|
|
596
|
+
DB.sqls.should == ["SELECT * FROM items WHERE name = 'sharon'"]
|
|
604
597
|
end
|
|
605
598
|
|
|
606
599
|
it "should return the first record for the given pk for a filtered dataset" do
|
|
607
600
|
@c.dataset = @c.dataset.filter(:active=>true)
|
|
608
601
|
@c[1].should == @c.load(:name => 'sharon', :id => 1)
|
|
609
|
-
|
|
602
|
+
DB.sqls.should == ["SELECT * FROM items WHERE ((active IS TRUE) AND (id = 1)) LIMIT 1"]
|
|
610
603
|
end
|
|
611
604
|
|
|
612
605
|
it "should work correctly for composite primary key specified as array" do
|
|
613
606
|
@c.set_primary_key [:node_id, :kind]
|
|
614
607
|
@c[3921, 201].should be_a_kind_of(@c)
|
|
615
|
-
sqls =
|
|
616
|
-
sqls.length.should == 1
|
|
617
|
-
sqls.first.should =~ /^SELECT \* FROM items WHERE \((\(node_id = 3921\) AND \(kind = 201\))|(\(kind = 201\) AND \(node_id = 3921\))\) LIMIT 1$/
|
|
618
|
-
end
|
|
619
|
-
|
|
620
|
-
qspecify "should work correctly for composite primary key specified as separate arguments" do
|
|
621
|
-
@c.set_primary_key :node_id, :kind
|
|
622
|
-
@c[3921, 201].should be_a_kind_of(@c)
|
|
623
|
-
sqls = MODEL_DB.sqls
|
|
608
|
+
sqls = DB.sqls
|
|
624
609
|
sqls.length.should == 1
|
|
625
610
|
sqls.first.should =~ /^SELECT \* FROM items WHERE \((\(node_id = 3921\) AND \(kind = 201\))|(\(kind = 201\) AND \(node_id = 3921\))\) LIMIT 1$/
|
|
626
611
|
end
|