sequel 4.22.0 → 4.23.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +22 -0
- data/README.rdoc +6 -0
- data/Rakefile +59 -81
- data/doc/migration.rdoc +2 -0
- data/doc/release_notes/4.23.0.txt +65 -0
- data/doc/sharding.rdoc +16 -14
- data/doc/testing.rdoc +61 -77
- data/lib/sequel/adapters/jdbc.rb +1 -0
- data/lib/sequel/adapters/mock.rb +0 -1
- data/lib/sequel/adapters/postgres.rb +1 -0
- data/lib/sequel/adapters/postgresql.rb +1 -0
- data/lib/sequel/adapters/shared/postgres.rb +3 -3
- data/lib/sequel/connection_pool/sharded_threaded.rb +5 -0
- data/lib/sequel/connection_pool/threaded.rb +9 -1
- data/lib/sequel/database/connecting.rb +1 -1
- data/lib/sequel/database/transactions.rb +2 -1
- data/lib/sequel/dataset/prepared_statements.rb +1 -1
- data/lib/sequel/extensions/constraint_validations.rb +12 -12
- data/lib/sequel/extensions/date_arithmetic.rb +0 -4
- data/lib/sequel/extensions/pagination.rb +14 -2
- data/lib/sequel/extensions/pg_enum.rb +2 -2
- data/lib/sequel/extensions/pg_hstore.rb +1 -1
- data/lib/sequel/extensions/pg_json_ops.rb +2 -2
- data/lib/sequel/plugins/csv_serializer.rb +2 -0
- data/lib/sequel/plugins/delay_add_association.rb +50 -0
- data/lib/sequel/plugins/list.rb +2 -2
- data/lib/sequel/plugins/nested_attributes.rb +8 -28
- data/lib/sequel/plugins/update_refresh.rb +50 -0
- data/lib/sequel/plugins/validate_associated.rb +55 -0
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +29 -29
- data/spec/adapters/firebird_spec.rb +97 -103
- data/spec/adapters/informix_spec.rb +25 -25
- data/spec/adapters/mssql_spec.rb +156 -172
- data/spec/adapters/mysql_spec.rb +334 -359
- data/spec/adapters/oracle_spec.rb +67 -69
- data/spec/adapters/postgres_spec.rb +1298 -1249
- data/spec/adapters/spec_helper.rb +2 -35
- data/spec/adapters/sqlanywhere_spec.rb +39 -39
- data/spec/adapters/sqlite_spec.rb +203 -200
- data/spec/bin_spec.rb +57 -59
- data/spec/core/connection_pool_spec.rb +402 -401
- data/spec/core/database_spec.rb +953 -944
- data/spec/core/dataset_spec.rb +2178 -2168
- data/spec/core/deprecated_spec.rb +19 -19
- data/spec/core/expression_filters_spec.rb +415 -415
- data/spec/core/mock_adapter_spec.rb +212 -212
- data/spec/core/object_graph_spec.rb +73 -73
- data/spec/core/placeholder_literalizer_spec.rb +71 -71
- data/spec/core/schema_generator_spec.rb +44 -44
- data/spec/core/schema_spec.rb +470 -472
- data/spec/core/spec_helper.rb +5 -20
- data/spec/core/version_spec.rb +2 -2
- data/spec/core_extensions_spec.rb +320 -320
- data/spec/extensions/accessed_columns_spec.rb +12 -12
- data/spec/extensions/active_model_spec.rb +3 -3
- data/spec/extensions/after_initialize_spec.rb +2 -2
- data/spec/extensions/arbitrary_servers_spec.rb +23 -23
- data/spec/extensions/association_dependencies_spec.rb +34 -34
- data/spec/extensions/association_pks_spec.rb +98 -98
- data/spec/extensions/association_proxies_spec.rb +33 -33
- data/spec/extensions/auto_validations_spec.rb +46 -46
- data/spec/extensions/blacklist_security_spec.rb +19 -18
- data/spec/extensions/blank_spec.rb +36 -36
- data/spec/extensions/boolean_readers_spec.rb +36 -36
- data/spec/extensions/caching_spec.rb +82 -82
- data/spec/extensions/class_table_inheritance_spec.rb +72 -72
- data/spec/extensions/column_conflicts_spec.rb +19 -14
- data/spec/extensions/column_select_spec.rb +19 -19
- data/spec/extensions/columns_introspection_spec.rb +43 -43
- data/spec/extensions/composition_spec.rb +64 -64
- data/spec/extensions/connection_validator_spec.rb +92 -90
- data/spec/extensions/constraint_validations_plugin_spec.rb +92 -92
- data/spec/extensions/constraint_validations_spec.rb +80 -80
- data/spec/extensions/core_refinements_spec.rb +220 -220
- data/spec/extensions/csv_serializer_spec.rb +44 -44
- data/spec/extensions/current_datetime_timestamp_spec.rb +8 -8
- data/spec/extensions/dataset_associations_spec.rb +65 -65
- data/spec/extensions/dataset_source_alias_spec.rb +16 -16
- data/spec/extensions/date_arithmetic_spec.rb +51 -58
- data/spec/extensions/defaults_setter_spec.rb +19 -19
- data/spec/extensions/delay_add_association_spec.rb +52 -0
- data/spec/extensions/dirty_spec.rb +51 -51
- data/spec/extensions/eager_each_spec.rb +8 -8
- data/spec/extensions/empty_array_ignore_nulls_spec.rb +10 -10
- data/spec/extensions/error_splitter_spec.rb +2 -2
- data/spec/extensions/error_sql_spec.rb +4 -4
- data/spec/extensions/eval_inspect_spec.rb +3 -3
- data/spec/extensions/filter_having_spec.rb +8 -8
- data/spec/extensions/force_encoding_spec.rb +30 -30
- data/spec/extensions/from_block_spec.rb +7 -7
- data/spec/extensions/graph_each_spec.rb +19 -19
- data/spec/extensions/hash_aliases_spec.rb +5 -5
- data/spec/extensions/hook_class_methods_spec.rb +100 -100
- data/spec/extensions/inflector_spec.rb +54 -54
- data/spec/extensions/input_transformer_spec.rb +10 -10
- data/spec/extensions/insert_returning_select_spec.rb +8 -8
- data/spec/extensions/instance_filters_spec.rb +26 -26
- data/spec/extensions/instance_hooks_spec.rb +85 -85
- data/spec/extensions/json_serializer_spec.rb +68 -68
- data/spec/extensions/lazy_attributes_spec.rb +49 -49
- data/spec/extensions/list_spec.rb +77 -75
- data/spec/extensions/looser_typecasting_spec.rb +16 -16
- data/spec/extensions/many_through_many_spec.rb +627 -627
- data/spec/extensions/meta_def_spec.rb +7 -7
- data/spec/extensions/migration_spec.rb +217 -217
- data/spec/extensions/modification_detection_spec.rb +20 -20
- data/spec/extensions/mssql_optimistic_locking_spec.rb +21 -21
- data/spec/extensions/named_timezones_spec.rb +18 -18
- data/spec/extensions/nested_attributes_spec.rb +107 -107
- data/spec/extensions/null_dataset_spec.rb +24 -24
- data/spec/extensions/optimistic_locking_spec.rb +21 -21
- data/spec/extensions/pagination_spec.rb +52 -52
- data/spec/extensions/pg_array_associations_spec.rb +273 -273
- data/spec/extensions/pg_array_ops_spec.rb +52 -52
- data/spec/extensions/pg_array_spec.rb +152 -152
- data/spec/extensions/pg_enum_spec.rb +13 -13
- data/spec/extensions/pg_hstore_ops_spec.rb +63 -63
- data/spec/extensions/pg_hstore_spec.rb +84 -84
- data/spec/extensions/pg_inet_spec.rb +15 -15
- data/spec/extensions/pg_interval_spec.rb +29 -29
- data/spec/extensions/pg_json_ops_spec.rb +86 -84
- data/spec/extensions/pg_json_spec.rb +104 -104
- data/spec/extensions/pg_loose_count_spec.rb +6 -6
- data/spec/extensions/pg_range_ops_spec.rb +24 -24
- data/spec/extensions/pg_range_spec.rb +143 -143
- data/spec/extensions/pg_row_ops_spec.rb +14 -14
- data/spec/extensions/pg_row_plugin_spec.rb +12 -12
- data/spec/extensions/pg_row_spec.rb +118 -118
- data/spec/extensions/pg_static_cache_updater_spec.rb +28 -28
- data/spec/extensions/pg_typecast_on_load_spec.rb +21 -21
- data/spec/extensions/prepared_statements_associations_spec.rb +42 -42
- data/spec/extensions/prepared_statements_safe_spec.rb +18 -18
- data/spec/extensions/prepared_statements_spec.rb +28 -28
- data/spec/extensions/prepared_statements_with_pk_spec.rb +11 -11
- data/spec/extensions/pretty_table_spec.rb +16 -16
- data/spec/extensions/query_literals_spec.rb +37 -37
- data/spec/extensions/query_spec.rb +32 -32
- data/spec/extensions/rcte_tree_spec.rb +141 -141
- data/spec/extensions/round_timestamps_spec.rb +21 -21
- data/spec/extensions/schema_caching_spec.rb +8 -8
- data/spec/extensions/schema_dumper_spec.rb +78 -78
- data/spec/extensions/schema_spec.rb +31 -27
- data/spec/extensions/scissors_spec.rb +3 -3
- data/spec/extensions/select_remove_spec.rb +14 -14
- data/spec/extensions/sequel_3_dataset_methods_spec.rb +28 -28
- data/spec/extensions/serialization_modification_detection_spec.rb +33 -33
- data/spec/extensions/serialization_spec.rb +79 -78
- data/spec/extensions/server_block_spec.rb +17 -17
- data/spec/extensions/set_overrides_spec.rb +30 -30
- data/spec/extensions/sharding_spec.rb +65 -65
- data/spec/extensions/shared_caching_spec.rb +29 -29
- data/spec/extensions/single_table_inheritance_spec.rb +79 -79
- data/spec/extensions/skip_create_refresh_spec.rb +3 -3
- data/spec/extensions/spec_helper.rb +4 -29
- data/spec/extensions/split_array_nil_spec.rb +9 -9
- data/spec/extensions/split_values_spec.rb +7 -7
- data/spec/extensions/sql_expr_spec.rb +32 -32
- data/spec/extensions/static_cache_spec.rb +123 -123
- data/spec/extensions/string_date_time_spec.rb +34 -34
- data/spec/extensions/string_stripper_spec.rb +15 -15
- data/spec/extensions/subclasses_spec.rb +31 -31
- data/spec/extensions/table_select_spec.rb +15 -15
- data/spec/extensions/tactical_eager_loading_spec.rb +23 -23
- data/spec/extensions/thread_local_timezones_spec.rb +13 -13
- data/spec/extensions/timestamps_spec.rb +40 -40
- data/spec/extensions/to_dot_spec.rb +34 -34
- data/spec/extensions/touch_spec.rb +52 -52
- data/spec/extensions/tree_spec.rb +72 -72
- data/spec/extensions/typecast_on_load_spec.rb +25 -25
- data/spec/extensions/unlimited_update_spec.rb +2 -2
- data/spec/extensions/update_or_create_spec.rb +36 -36
- data/spec/extensions/update_primary_key_spec.rb +35 -35
- data/spec/extensions/update_refresh_spec.rb +41 -0
- data/spec/extensions/validate_associated_spec.rb +52 -0
- data/spec/extensions/validation_class_methods_spec.rb +314 -317
- data/spec/extensions/validation_helpers_spec.rb +195 -195
- data/spec/extensions/xml_serializer_spec.rb +48 -48
- data/spec/guards_helper.rb +55 -0
- data/spec/integration/associations_test.rb +1089 -1088
- data/spec/integration/database_test.rb +29 -29
- data/spec/integration/dataset_test.rb +661 -661
- data/spec/integration/eager_loader_test.rb +147 -147
- data/spec/integration/migrator_test.rb +122 -122
- data/spec/integration/model_test.rb +70 -70
- data/spec/integration/plugin_test.rb +682 -640
- data/spec/integration/prepared_statement_test.rb +172 -172
- data/spec/integration/schema_test.rb +245 -245
- data/spec/integration/spec_helper.rb +1 -64
- data/spec/integration/timezone_test.rb +17 -17
- data/spec/integration/transaction_test.rb +87 -87
- data/spec/integration/type_test.rb +33 -33
- data/spec/model/association_reflection_spec.rb +130 -121
- data/spec/model/associations_spec.rb +1112 -1113
- data/spec/model/base_spec.rb +197 -196
- data/spec/model/class_dataset_methods_spec.rb +118 -118
- data/spec/model/dataset_methods_spec.rb +49 -49
- data/spec/model/eager_loading_spec.rb +705 -702
- data/spec/model/hooks_spec.rb +169 -168
- data/spec/model/inflector_spec.rb +5 -5
- data/spec/model/model_spec.rb +287 -297
- data/spec/model/plugins_spec.rb +47 -47
- data/spec/model/record_spec.rb +534 -535
- data/spec/model/spec_helper.rb +3 -21
- data/spec/model/validations_spec.rb +72 -70
- data/spec/spec_config.rb +8 -0
- metadata +41 -9
- data/lib/sequel/adapters/fdbsql.rb +0 -286
- data/lib/sequel/adapters/jdbc/fdbsql.rb +0 -66
- data/lib/sequel/adapters/openbase.rb +0 -54
- data/lib/sequel/adapters/shared/fdbsql.rb +0 -550
- data/spec/adapters/fdbsql_spec.rb +0 -429
- data/spec/rspec_helper.rb +0 -22
@@ -11,90 +11,90 @@ describe "Sequel::Plugins::UpdatePrimaryKey" do
|
|
11
11
|
DB.reset
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
it "should handle regular updates" do
|
15
15
|
@ds._fetch = [[{:a=>1, :b=>3}], [{:a=>1, :b=>4}], [{:a=>1, :b=>4}], [{:a=>1, :b=>5}], [{:a=>1, :b=>5}], [{:a=>1, :b=>6}], [{:a=>1, :b=>6}]]
|
16
16
|
@c.first.update(:b=>4)
|
17
|
-
@c.all.
|
18
|
-
DB.sqls.
|
17
|
+
@c.all.must_equal [@c.load(:a=>1, :b=>4)]
|
18
|
+
DB.sqls.must_equal ["SELECT * FROM a LIMIT 1", "UPDATE a SET b = 4 WHERE (a = 1)", "SELECT * FROM a"]
|
19
19
|
@c.first.set(:b=>5).save
|
20
|
-
@c.all.
|
21
|
-
DB.sqls.
|
20
|
+
@c.all.must_equal [@c.load(:a=>1, :b=>5)]
|
21
|
+
DB.sqls.must_equal ["SELECT * FROM a LIMIT 1", "UPDATE a SET b = 5 WHERE (a = 1)", "SELECT * FROM a"]
|
22
22
|
@c.first.set(:b=>6).save(:columns=>:b)
|
23
|
-
@c.all.
|
24
|
-
DB.sqls.
|
23
|
+
@c.all.must_equal [@c.load(:a=>1, :b=>6)]
|
24
|
+
DB.sqls.must_equal ["SELECT * FROM a LIMIT 1", "UPDATE a SET b = 6 WHERE (a = 1)", "SELECT * FROM a"]
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
it "should handle updating the primary key field with another field" do
|
28
28
|
@ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>4}]]
|
29
29
|
@c.first.update(:a=>2, :b=>4)
|
30
|
-
@c.all.
|
30
|
+
@c.all.must_equal [@c.load(:a=>2, :b=>4)]
|
31
31
|
sqls = DB.sqls
|
32
|
-
["UPDATE a SET a = 2, b = 4 WHERE (a = 1)", "UPDATE a SET b = 4, a = 2 WHERE (a = 1)"].
|
33
|
-
sqls.
|
32
|
+
["UPDATE a SET a = 2, b = 4 WHERE (a = 1)", "UPDATE a SET b = 4, a = 2 WHERE (a = 1)"].must_include(sqls.slice!(1))
|
33
|
+
sqls.must_equal ["SELECT * FROM a LIMIT 1", "SELECT * FROM a"]
|
34
34
|
end
|
35
35
|
|
36
|
-
|
36
|
+
it "should handle updating just the primary key field when saving changes" do
|
37
37
|
@ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>3}], [{:a=>2, :b=>3}], [{:a=>3, :b=>3}]]
|
38
38
|
@c.first.update(:a=>2)
|
39
|
-
@c.all.
|
40
|
-
DB.sqls.
|
39
|
+
@c.all.must_equal [@c.load(:a=>2, :b=>3)]
|
40
|
+
DB.sqls.must_equal ["SELECT * FROM a LIMIT 1", "UPDATE a SET a = 2 WHERE (a = 1)", "SELECT * FROM a"]
|
41
41
|
@c.first.set(:a=>3).save(:columns=>:a)
|
42
|
-
@c.all.
|
43
|
-
DB.sqls.
|
42
|
+
@c.all.must_equal [@c.load(:a=>3, :b=>3)]
|
43
|
+
DB.sqls.must_equal ["SELECT * FROM a LIMIT 1", "UPDATE a SET a = 3 WHERE (a = 2)", "SELECT * FROM a"]
|
44
44
|
end
|
45
45
|
|
46
|
-
|
46
|
+
it "should handle saving after modifying the primary key field with another field" do
|
47
47
|
@ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>4}]]
|
48
48
|
@c.first.set(:a=>2, :b=>4).save
|
49
|
-
@c.all.
|
49
|
+
@c.all.must_equal [@c.load(:a=>2, :b=>4)]
|
50
50
|
sqls = DB.sqls
|
51
|
-
["UPDATE a SET a = 2, b = 4 WHERE (a = 1)", "UPDATE a SET b = 4, a = 2 WHERE (a = 1)"].
|
52
|
-
sqls.
|
51
|
+
["UPDATE a SET a = 2, b = 4 WHERE (a = 1)", "UPDATE a SET b = 4, a = 2 WHERE (a = 1)"].must_include(sqls.slice!(1))
|
52
|
+
sqls.must_equal ["SELECT * FROM a LIMIT 1", "SELECT * FROM a"]
|
53
53
|
end
|
54
54
|
|
55
|
-
|
55
|
+
it "should handle saving after modifying just the primary key field" do
|
56
56
|
@ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>3}]]
|
57
57
|
@c.first.set(:a=>2).save
|
58
|
-
@c.all.
|
58
|
+
@c.all.must_equal [@c.load(:a=>2, :b=>3)]
|
59
59
|
sqls = DB.sqls
|
60
|
-
["UPDATE a SET a = 2, b = 3 WHERE (a = 1)", "UPDATE a SET b = 3, a = 2 WHERE (a = 1)"].
|
61
|
-
sqls.
|
60
|
+
["UPDATE a SET a = 2, b = 3 WHERE (a = 1)", "UPDATE a SET b = 3, a = 2 WHERE (a = 1)"].must_include(sqls.slice!(1))
|
61
|
+
sqls.must_equal ["SELECT * FROM a LIMIT 1", "SELECT * FROM a"]
|
62
62
|
end
|
63
63
|
|
64
|
-
|
64
|
+
it "should handle saving after updating the primary key" do
|
65
65
|
@ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>5}]]
|
66
66
|
@c.first.update(:a=>2).update(:b=>4).set(:b=>5).save
|
67
|
-
@c.all.
|
68
|
-
DB.sqls.
|
67
|
+
@c.all.must_equal [@c.load(:a=>2, :b=>5)]
|
68
|
+
DB.sqls.must_equal ["SELECT * FROM a LIMIT 1", "UPDATE a SET a = 2 WHERE (a = 1)", "UPDATE a SET b = 4 WHERE (a = 2)", "UPDATE a SET b = 5 WHERE (a = 2)", "SELECT * FROM a"]
|
69
69
|
end
|
70
70
|
|
71
|
-
|
71
|
+
it "should work correctly when using the prepared_statements plugin" do
|
72
72
|
@c.plugin :prepared_statements
|
73
73
|
@ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>4}]]
|
74
74
|
o = @c.first
|
75
75
|
o.update(:a=>2, :b=>4)
|
76
|
-
@c.all.
|
76
|
+
@c.all.must_equal [@c.load(:a=>2, :b=>4)]
|
77
77
|
sqls = DB.sqls
|
78
|
-
["UPDATE a SET a = 2, b = 4 WHERE (a = 1)", "UPDATE a SET b = 4, a = 2 WHERE (a = 1)"].
|
79
|
-
sqls.
|
78
|
+
["UPDATE a SET a = 2, b = 4 WHERE (a = 1)", "UPDATE a SET b = 4, a = 2 WHERE (a = 1)"].must_include(sqls.slice!(1))
|
79
|
+
sqls.must_equal ["SELECT * FROM a LIMIT 1", "SELECT * FROM a"]
|
80
80
|
|
81
81
|
o.delete
|
82
82
|
end
|
83
83
|
|
84
|
-
|
84
|
+
it "should clear the associations cache of non-many_to_one associations when changing the primary key" do
|
85
85
|
@c.one_to_many :cs, :class=>@c
|
86
86
|
@c.many_to_one :c, :class=>@c
|
87
87
|
o = @c.new(:a=>1)
|
88
88
|
o.associations[:cs] = @c.new
|
89
89
|
o.associations[:c] = o2 = @c.new
|
90
90
|
o.a = 2
|
91
|
-
o.associations.
|
91
|
+
o.associations.must_equal(:c=>o2)
|
92
92
|
end
|
93
93
|
|
94
|
-
|
94
|
+
it "should handle frozen instances" do
|
95
95
|
o = @c.new
|
96
96
|
o.a = 1
|
97
97
|
o.freeze
|
98
|
-
o.pk_hash.
|
98
|
+
o.pk_hash.must_equal(:a=>1)
|
99
99
|
end
|
100
100
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
|
2
|
+
|
3
|
+
describe "Sequel::Plugins::UpdateRefresh" do
|
4
|
+
before do
|
5
|
+
@db = Sequel.mock(:numrows=>1, :fetch=>{:id=>1, :name=>'b'})
|
6
|
+
@c = Class.new(Sequel::Model(@db[:test]))
|
7
|
+
@ds = @c.dataset
|
8
|
+
@c.columns :id, :name
|
9
|
+
@c.plugin :update_refresh
|
10
|
+
@db.sqls
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should refresh the instance after updating" do
|
14
|
+
o = @c.load(:id=>1, :name=>'a')
|
15
|
+
o.save
|
16
|
+
@db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1)", "SELECT * FROM test WHERE (id = 1) LIMIT 1"]
|
17
|
+
o.name.must_equal 'b'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should refresh the instance after updating" do
|
21
|
+
@db.extend_datasets{def supports_returning?(x) true end; def update_sql(*); sql = super; update_returning_sql(sql); sql end}
|
22
|
+
@c.dataset = @db[:test]
|
23
|
+
@db.sqls
|
24
|
+
o = @c.load(:id=>1, :name=>'a')
|
25
|
+
o.save
|
26
|
+
@db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1) RETURNING *"]
|
27
|
+
o.name.must_equal 'b'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should refresh the instance after updating when returning specific columns" do
|
31
|
+
@db.extend_datasets{def supports_returning?(x) true end; def update_sql(*); sql = super; update_returning_sql(sql); sql end}
|
32
|
+
@c.plugin :insert_returning_select
|
33
|
+
@c.dataset = @db[:test].select(:id, :name)
|
34
|
+
@db.sqls
|
35
|
+
o = @c.load(:id=>1, :name=>'a')
|
36
|
+
o.this.opts[:returning] = [:id, :name]
|
37
|
+
o.save
|
38
|
+
@db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1) RETURNING id, name"]
|
39
|
+
o.name.must_equal 'b'
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
|
2
|
+
|
3
|
+
describe "ValidatesAssociated plugin" do
|
4
|
+
before do
|
5
|
+
@db = Sequel.mock(:autoid=>1, :numrows=>1, :fetch=>{:id=>1, :name=>'a', :c_id=>nil})
|
6
|
+
@c = Class.new(Sequel::Model(@db[:cs]))
|
7
|
+
@c.plugin :validate_associated
|
8
|
+
@c.columns :id, :name, :c_id
|
9
|
+
@c.one_to_many :cs, :class=>@c, :key=>:c_id
|
10
|
+
@o = @c.load(:id=>1, :name=>'a')
|
11
|
+
@db.sqls
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return nil when saving if the associated object is invalid when raise_on_save_failure is false" do
|
15
|
+
@c.raise_on_save_failure = false
|
16
|
+
@c.send(:define_method, :validate){|*| errors.add(:name, 'is b') if name == 'b'}
|
17
|
+
o = @c.load(:id=>2, :name=>'b')
|
18
|
+
@o.send(:delay_validate_associated_object, @c.association_reflection(:cs), o)
|
19
|
+
@o.save.must_equal nil
|
20
|
+
@o.errors[:cs].must_equal ["name is b"]
|
21
|
+
o.errors[:name].must_equal ['is b']
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should support creating new one_to_many and one_to_one objects with presence validations on the foreign key" do
|
25
|
+
@c.class_eval do
|
26
|
+
plugin :validation_helpers
|
27
|
+
def validate
|
28
|
+
validates_integer :c_id
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
@o.c_id = 5
|
33
|
+
@o.send(:delay_validate_associated_object, @c.association_reflection(:cs), @c.load(:id=>2, :name=>'b', :c_id=>2))
|
34
|
+
@o.valid?.must_equal true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should should not remove existing values from object when validating" do
|
38
|
+
o = @c.load(:id=>2, :name=>'b', :c_id=>3)
|
39
|
+
@o.send(:delay_validate_associated_object, @c.association_reflection(:cs), o)
|
40
|
+
@o.valid?.must_equal true
|
41
|
+
o.c_id.must_equal 3
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should not attempt to validate associated_object if the :validate=>false option is passed to save" do
|
45
|
+
@c.one_to_many :cs, :class=>@c, :key=>:c_id
|
46
|
+
@c.send(:define_method, :validate){|*| errors.add(:name, 'is b') if name == 'b'}
|
47
|
+
o = @c.load(:id=>2, :name=>'b', :c_id=>3)
|
48
|
+
@o.send(:delay_validate_associated_object, @c.association_reflection(:cs), o)
|
49
|
+
@o.save(:validate=>false).must_equal @o
|
50
|
+
@db.sqls.must_equal ["UPDATE cs SET name = 'a' WHERE (id = 1)"]
|
51
|
+
end
|
52
|
+
end
|
@@ -16,46 +16,46 @@ describe Sequel::Model do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
@c.
|
21
|
-
@c.
|
22
|
-
@c.
|
19
|
+
it "should respond to validations, has_validations?, and validation_reflections" do
|
20
|
+
@c.must_respond_to(:validations)
|
21
|
+
@c.must_respond_to(:has_validations?)
|
22
|
+
@c.must_respond_to(:validation_reflections)
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
@c.validation_reflections.
|
25
|
+
it "should be able to reflect on validations" do
|
26
|
+
@c.validation_reflections.must_equal({})
|
27
27
|
@c.validates_acceptance_of(:a)
|
28
|
-
@c.validation_reflections.
|
28
|
+
@c.validation_reflections.must_equal(:a=>[[:acceptance, {:tag=>:acceptance, :message=>"is not accepted", :allow_nil=>true, :accept=>"1"}]])
|
29
29
|
@c.validates_presence_of(:a)
|
30
|
-
@c.validation_reflections[:a].length.
|
31
|
-
@c.validation_reflections[:a].last.
|
30
|
+
@c.validation_reflections[:a].length.must_equal 2
|
31
|
+
@c.validation_reflections[:a].last.must_equal [:presence, {:tag=>:presence, :message=>"is not present"}]
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
it "should handle validation reflections correctly when subclassing" do
|
35
35
|
@c.validates_acceptance_of(:a)
|
36
36
|
c = Class.new(@c)
|
37
|
-
c.validation_reflections.map{|k,v| k}.
|
37
|
+
c.validation_reflections.map{|k,v| k}.must_equal [:a]
|
38
38
|
c.validates_presence_of(:a)
|
39
|
-
@c.validation_reflections.
|
40
|
-
c.validation_reflections[:a].last.
|
39
|
+
@c.validation_reflections.must_equal(:a=>[[:acceptance, {:tag=>:acceptance, :message=>"is not accepted", :allow_nil=>true, :accept=>"1"}]])
|
40
|
+
c.validation_reflections[:a].last.must_equal [:presence, {:tag=>:presence, :message=>"is not present"}]
|
41
41
|
end
|
42
42
|
|
43
|
-
|
43
|
+
it "should acccept validation definitions using validates_each" do
|
44
44
|
@c.validates_each(:xx, :yy) {|o, a, v| o.errors.add(a, 'too low') if v < 50}
|
45
45
|
o = @c.new
|
46
|
-
o.
|
47
|
-
o.
|
48
|
-
o.valid?.
|
49
|
-
o.errors.full_messages.
|
46
|
+
def o.xx; 40; end
|
47
|
+
def o.yy; 60; end
|
48
|
+
o.valid?.must_equal false
|
49
|
+
o.errors.full_messages.must_equal ['xx too low']
|
50
50
|
end
|
51
51
|
|
52
|
-
|
53
|
-
@c.has_validations?.
|
52
|
+
it "should return true/false for has_validations?" do
|
53
|
+
@c.has_validations?.must_equal false
|
54
54
|
@c.validates_each(:xx) {1}
|
55
|
-
@c.has_validations?.
|
55
|
+
@c.has_validations?.must_equal true
|
56
56
|
end
|
57
57
|
|
58
|
-
|
58
|
+
it "should validate multiple attributes at once" do
|
59
59
|
o = @c.new
|
60
60
|
def o.xx
|
61
61
|
1
|
@@ -67,11 +67,11 @@ describe Sequel::Model do
|
|
67
67
|
atts = nil
|
68
68
|
@c.validates_each([:xx, :yy]){|obj,a,v| atts=a; vals=v}
|
69
69
|
o.valid?
|
70
|
-
vals.
|
71
|
-
atts.
|
70
|
+
vals.must_equal [1,2]
|
71
|
+
atts.must_equal [:xx, :yy]
|
72
72
|
end
|
73
73
|
|
74
|
-
|
74
|
+
it "should respect allow_missing option when using multiple attributes" do
|
75
75
|
o = @c.new
|
76
76
|
def o.xx
|
77
77
|
self[:xx]
|
@@ -85,26 +85,26 @@ describe Sequel::Model do
|
|
85
85
|
|
86
86
|
o.values[:xx] = 1
|
87
87
|
o.valid?
|
88
|
-
vals.
|
89
|
-
atts.
|
88
|
+
vals.must_equal [1,nil]
|
89
|
+
atts.must_equal [:xx, :yy]
|
90
90
|
|
91
91
|
vals = nil
|
92
92
|
atts = nil
|
93
93
|
o.values.clear
|
94
94
|
o.values[:yy] = 2
|
95
95
|
o.valid?
|
96
|
-
vals.
|
97
|
-
atts.
|
96
|
+
vals.must_equal [nil, 2]
|
97
|
+
atts.must_equal [:xx, :yy]
|
98
98
|
|
99
99
|
vals = nil
|
100
100
|
atts = nil
|
101
101
|
o.values.clear
|
102
|
-
o.valid?.
|
103
|
-
vals.
|
104
|
-
atts.
|
102
|
+
o.valid?.must_equal true
|
103
|
+
vals.must_equal nil
|
104
|
+
atts.must_equal nil
|
105
105
|
end
|
106
106
|
|
107
|
-
|
107
|
+
it "should overwrite existing validation with the same tag and attribute" do
|
108
108
|
@c.validates_each(:xx, :xx, :tag=>:low) {|o, a, v| o.xxx; o.errors.add(a, 'too low') if v < 50}
|
109
109
|
@c.validates_each(:yy, :yy) {|o, a, v| o.yyy; o.errors.add(a, 'too low') if v < 50}
|
110
110
|
@c.validates_presence_of(:zz, :zz)
|
@@ -118,33 +118,33 @@ describe Sequel::Model do
|
|
118
118
|
@b ||= 0
|
119
119
|
@b += 1
|
120
120
|
end
|
121
|
-
o.
|
122
|
-
o.
|
123
|
-
o.
|
124
|
-
o.
|
125
|
-
o.valid?.
|
126
|
-
o.zz.
|
127
|
-
o.aa.
|
128
|
-
o.errors.full_messages.
|
121
|
+
def o.xx; 40; end
|
122
|
+
def o.yy; 60; end
|
123
|
+
def o.xxx; end
|
124
|
+
def o.yyy; end
|
125
|
+
o.valid?.must_equal false
|
126
|
+
o.zz.must_equal 2
|
127
|
+
o.aa.must_equal 2
|
128
|
+
o.errors.full_messages.must_equal ['xx too low']
|
129
129
|
end
|
130
130
|
|
131
|
-
|
131
|
+
it "should provide a validates method that takes block with validation definitions" do
|
132
132
|
@c.validates do
|
133
133
|
coolness_of :blah
|
134
134
|
end
|
135
|
-
@c.validations[:blah].
|
135
|
+
@c.validations[:blah].wont_be :empty?
|
136
136
|
o = @c.new
|
137
|
-
o.
|
138
|
-
o.valid?.
|
139
|
-
o.errors.full_messages.
|
137
|
+
def o.blah; end
|
138
|
+
o.valid?.must_equal false
|
139
|
+
o.errors.full_messages.must_equal ['blah is not cool']
|
140
140
|
end
|
141
141
|
|
142
|
-
|
142
|
+
it "should have the validates block have appropriate respond_to?" do
|
143
143
|
c = nil
|
144
144
|
@c.validates{c = respond_to?(:foo)}
|
145
|
-
c.
|
145
|
+
c.must_equal false
|
146
146
|
@c.validates{c = respond_to?(:length_of)}
|
147
|
-
c.
|
147
|
+
c.must_equal true
|
148
148
|
end if RUBY_VERSION >= '1.9'
|
149
149
|
end
|
150
150
|
|
@@ -160,22 +160,22 @@ describe Sequel::Model do
|
|
160
160
|
@o = @c.new
|
161
161
|
end
|
162
162
|
|
163
|
-
|
163
|
+
it "should supply a #valid? method that returns true if validations pass" do
|
164
164
|
@o.score = 50
|
165
|
-
@o.
|
165
|
+
@o.wont_be :valid?
|
166
166
|
@o.score = 100
|
167
|
-
@o.
|
167
|
+
@o.must_be :valid?
|
168
168
|
end
|
169
169
|
|
170
|
-
|
170
|
+
it "should provide an errors object" do
|
171
171
|
@o.score = 100
|
172
|
-
@o.
|
173
|
-
@o.errors.
|
172
|
+
@o.must_be :valid?
|
173
|
+
@o.errors.must_be :empty?
|
174
174
|
|
175
175
|
@o.score = 86
|
176
|
-
@o.
|
177
|
-
@o.errors[:score].
|
178
|
-
@o.errors.on(:blah).
|
176
|
+
@o.wont_be :valid?
|
177
|
+
@o.errors[:score].must_equal ['too low']
|
178
|
+
@o.errors.on(:blah).must_equal nil
|
179
179
|
end
|
180
180
|
end
|
181
181
|
|
@@ -190,11 +190,11 @@ describe "Sequel::Plugins::ValidationClassMethods::ClassMethods::Generator" do
|
|
190
190
|
end
|
191
191
|
end
|
192
192
|
|
193
|
-
|
193
|
+
it "should instance_eval the block, sending everything to its receiver" do
|
194
194
|
@c.validates do
|
195
195
|
blah
|
196
196
|
end
|
197
|
-
@testit.
|
197
|
+
@testit.must_equal [1324]
|
198
198
|
end
|
199
199
|
end
|
200
200
|
|
@@ -215,379 +215,379 @@ describe Sequel::Model do
|
|
215
215
|
@m = @c.new
|
216
216
|
end
|
217
217
|
|
218
|
-
|
218
|
+
it "should validate acceptance_of" do
|
219
219
|
@c.validates_acceptance_of :value
|
220
|
-
@m.
|
220
|
+
@m.must_be :valid?
|
221
221
|
@m.value = '1'
|
222
|
-
@m.
|
222
|
+
@m.must_be :valid?
|
223
223
|
end
|
224
224
|
|
225
|
-
|
225
|
+
it "should validate acceptance_of with accept" do
|
226
226
|
@c.validates_acceptance_of :value, :accept => 'true'
|
227
227
|
@m.value = '1'
|
228
|
-
@m.
|
228
|
+
@m.wont_be :valid?
|
229
229
|
@m.value = 'true'
|
230
|
-
@m.
|
230
|
+
@m.must_be :valid?
|
231
231
|
end
|
232
232
|
|
233
|
-
|
233
|
+
it "should validate acceptance_of with allow_nil => false" do
|
234
234
|
@c.validates_acceptance_of :value, :allow_nil => false
|
235
|
-
@m.
|
235
|
+
@m.wont_be :valid?
|
236
236
|
end
|
237
237
|
|
238
|
-
|
238
|
+
it "should validate acceptance_of with allow_missing => true" do
|
239
239
|
@c.validates_acceptance_of :value, :allow_missing => true
|
240
|
-
@m.
|
240
|
+
@m.must_be :valid?
|
241
241
|
end
|
242
242
|
|
243
|
-
|
243
|
+
it "should validate acceptance_of with allow_missing => true and allow_nil => false" do
|
244
244
|
@c.validates_acceptance_of :value, :allow_missing => true, :allow_nil => false
|
245
|
-
@m.
|
245
|
+
@m.must_be :valid?
|
246
246
|
@m.value = nil
|
247
|
-
@m.
|
247
|
+
@m.wont_be :valid?
|
248
248
|
end
|
249
249
|
|
250
|
-
|
250
|
+
it "should validate acceptance_of with if => true" do
|
251
251
|
@c.validates_acceptance_of :value, :if => :dont_skip
|
252
252
|
@m.value = '0'
|
253
|
-
@m.
|
253
|
+
@m.wont_be :valid?
|
254
254
|
end
|
255
255
|
|
256
|
-
|
256
|
+
it "should validate acceptance_of with if => false" do
|
257
257
|
@c.validates_acceptance_of :value, :if => :skip
|
258
258
|
@m.value = '0'
|
259
|
-
@m.
|
259
|
+
@m.must_be :valid?
|
260
260
|
end
|
261
261
|
|
262
|
-
|
262
|
+
it "should validate acceptance_of with if proc that evaluates to true" do
|
263
263
|
@c.validates_acceptance_of :value, :if => proc{true}
|
264
264
|
@m.value = '0'
|
265
|
-
@m.
|
265
|
+
@m.wont_be :valid?
|
266
266
|
end
|
267
267
|
|
268
|
-
|
268
|
+
it "should validate acceptance_of with if proc that evaluates to false" do
|
269
269
|
@c.validates_acceptance_of :value, :if => proc{false}
|
270
270
|
@m.value = '0'
|
271
|
-
@m.
|
271
|
+
@m.must_be :valid?
|
272
272
|
end
|
273
273
|
|
274
|
-
|
274
|
+
it "should raise an error if :if option is not a Symbol, Proc, or nil" do
|
275
275
|
@c.validates_acceptance_of :value, :if => 1
|
276
276
|
@m.value = '0'
|
277
|
-
proc{@m.valid?}.
|
277
|
+
proc{@m.valid?}.must_raise(Sequel::Error)
|
278
278
|
end
|
279
279
|
|
280
|
-
|
280
|
+
it "should validate confirmation_of" do
|
281
281
|
@c.send(:attr_accessor, :value_confirmation)
|
282
282
|
@c.validates_confirmation_of :value
|
283
283
|
|
284
284
|
@m.value = 'blah'
|
285
|
-
@m.
|
285
|
+
@m.wont_be :valid?
|
286
286
|
|
287
287
|
@m.value_confirmation = 'blah'
|
288
|
-
@m.
|
288
|
+
@m.must_be :valid?
|
289
289
|
end
|
290
290
|
|
291
|
-
|
291
|
+
it "should validate confirmation_of with if => true" do
|
292
292
|
@c.send(:attr_accessor, :value_confirmation)
|
293
293
|
@c.validates_confirmation_of :value, :if => :dont_skip
|
294
294
|
|
295
295
|
@m.value = 'blah'
|
296
|
-
@m.
|
296
|
+
@m.wont_be :valid?
|
297
297
|
end
|
298
298
|
|
299
|
-
|
299
|
+
it "should validate confirmation_of with if => false" do
|
300
300
|
@c.send(:attr_accessor, :value_confirmation)
|
301
301
|
@c.validates_confirmation_of :value, :if => :skip
|
302
302
|
|
303
303
|
@m.value = 'blah'
|
304
|
-
@m.
|
304
|
+
@m.must_be :valid?
|
305
305
|
end
|
306
306
|
|
307
|
-
|
307
|
+
it "should validate confirmation_of with allow_missing => true" do
|
308
308
|
@c.send(:attr_accessor, :value_confirmation)
|
309
309
|
@c.validates_acceptance_of :value, :allow_missing => true
|
310
|
-
@m.
|
310
|
+
@m.must_be :valid?
|
311
311
|
@m.value_confirmation = 'blah'
|
312
|
-
@m.
|
312
|
+
@m.must_be :valid?
|
313
313
|
@m.value = nil
|
314
|
-
@m.
|
314
|
+
@m.wont_be :valid?
|
315
315
|
end
|
316
316
|
|
317
|
-
|
317
|
+
it "should validate format_of" do
|
318
318
|
@c.validates_format_of :value, :with => /.+_.+/
|
319
319
|
@m.value = 'abc_'
|
320
|
-
@m.
|
320
|
+
@m.wont_be :valid?
|
321
321
|
@m.value = 'abc_def'
|
322
|
-
@m.
|
322
|
+
@m.must_be :valid?
|
323
323
|
end
|
324
324
|
|
325
|
-
|
326
|
-
proc {@c.validates_format_of :value}.
|
327
|
-
proc {@c.validates_format_of :value, :with => :blah}.
|
325
|
+
it "should raise for validate_format_of without regexp" do
|
326
|
+
proc {@c.validates_format_of :value}.must_raise(ArgumentError)
|
327
|
+
proc {@c.validates_format_of :value, :with => :blah}.must_raise(ArgumentError)
|
328
328
|
end
|
329
329
|
|
330
|
-
|
330
|
+
it "should validate format_of with if => true" do
|
331
331
|
@c.validates_format_of :value, :with => /_/, :if => :dont_skip
|
332
332
|
|
333
333
|
@m.value = 'a'
|
334
|
-
@m.
|
334
|
+
@m.wont_be :valid?
|
335
335
|
end
|
336
336
|
|
337
|
-
|
337
|
+
it "should validate format_of with if => false" do
|
338
338
|
@c.validates_format_of :value, :with => /_/, :if => :skip
|
339
339
|
|
340
340
|
@m.value = 'a'
|
341
|
-
@m.
|
341
|
+
@m.must_be :valid?
|
342
342
|
end
|
343
343
|
|
344
|
-
|
344
|
+
it "should validate format_of with allow_missing => true" do
|
345
345
|
@c.validates_format_of :value, :allow_missing => true, :with=>/./
|
346
|
-
@m.
|
346
|
+
@m.must_be :valid?
|
347
347
|
@m.value = nil
|
348
|
-
@m.
|
348
|
+
@m.wont_be :valid?
|
349
349
|
end
|
350
350
|
|
351
|
-
|
351
|
+
it "should validate length_of with maximum" do
|
352
352
|
@c.validates_length_of :value, :maximum => 5
|
353
|
-
@m.
|
353
|
+
@m.wont_be :valid?
|
354
354
|
@m.value = '12345'
|
355
|
-
@m.
|
355
|
+
@m.must_be :valid?
|
356
356
|
@m.value = '123456'
|
357
|
-
@m.
|
358
|
-
@m.errors[:value].
|
357
|
+
@m.wont_be :valid?
|
358
|
+
@m.errors[:value].must_equal ['is too long']
|
359
359
|
@m.value = nil
|
360
|
-
@m.
|
361
|
-
@m.errors[:value].
|
360
|
+
@m.wont_be :valid?
|
361
|
+
@m.errors[:value].must_equal ['is not present']
|
362
362
|
end
|
363
363
|
|
364
|
-
|
364
|
+
it "should validate length_of with maximum using customized error messages" do
|
365
365
|
@c.validates_length_of :value, :maximum => 5, :too_long=>'tl', :nil_message=>'np'
|
366
366
|
@m.value = '123456'
|
367
|
-
@m.
|
368
|
-
@m.errors[:value].
|
367
|
+
@m.wont_be :valid?
|
368
|
+
@m.errors[:value].must_equal ['tl']
|
369
369
|
@m.value = nil
|
370
|
-
@m.
|
371
|
-
@m.errors[:value].
|
370
|
+
@m.wont_be :valid?
|
371
|
+
@m.errors[:value].must_equal ['np']
|
372
372
|
end
|
373
373
|
|
374
|
-
|
374
|
+
it "should validate length_of with minimum" do
|
375
375
|
@c.validates_length_of :value, :minimum => 5
|
376
|
-
@m.
|
376
|
+
@m.wont_be :valid?
|
377
377
|
@m.value = '12345'
|
378
|
-
@m.
|
378
|
+
@m.must_be :valid?
|
379
379
|
@m.value = '1234'
|
380
|
-
@m.
|
380
|
+
@m.wont_be :valid?
|
381
381
|
end
|
382
382
|
|
383
|
-
|
383
|
+
it "should validate length_of with within" do
|
384
384
|
@c.validates_length_of :value, :within => 2..5
|
385
|
-
@m.
|
385
|
+
@m.wont_be :valid?
|
386
386
|
@m.value = '12345'
|
387
|
-
@m.
|
387
|
+
@m.must_be :valid?
|
388
388
|
@m.value = '1'
|
389
|
-
@m.
|
389
|
+
@m.wont_be :valid?
|
390
390
|
@m.value = '123456'
|
391
|
-
@m.
|
391
|
+
@m.wont_be :valid?
|
392
392
|
end
|
393
393
|
|
394
|
-
|
394
|
+
it "should validate length_of with is" do
|
395
395
|
@c.validates_length_of :value, :is => 3
|
396
|
-
@m.
|
396
|
+
@m.wont_be :valid?
|
397
397
|
@m.value = '123'
|
398
|
-
@m.
|
398
|
+
@m.must_be :valid?
|
399
399
|
@m.value = '12'
|
400
|
-
@m.
|
400
|
+
@m.wont_be :valid?
|
401
401
|
@m.value = '1234'
|
402
|
-
@m.
|
402
|
+
@m.wont_be :valid?
|
403
403
|
end
|
404
404
|
|
405
|
-
|
405
|
+
it "should validate length_of with allow_nil" do
|
406
406
|
@c.validates_length_of :value, :is => 3, :allow_nil => true
|
407
|
-
@m.
|
407
|
+
@m.must_be :valid?
|
408
408
|
end
|
409
409
|
|
410
|
-
|
410
|
+
it "should validate length_of with if => true" do
|
411
411
|
@c.validates_length_of :value, :is => 3, :if => :dont_skip
|
412
412
|
|
413
413
|
@m.value = 'a'
|
414
|
-
@m.
|
414
|
+
@m.wont_be :valid?
|
415
415
|
end
|
416
416
|
|
417
|
-
|
417
|
+
it "should validate length_of with if => false" do
|
418
418
|
@c.validates_length_of :value, :is => 3, :if => :skip
|
419
419
|
|
420
420
|
@m.value = 'a'
|
421
|
-
@m.
|
421
|
+
@m.must_be :valid?
|
422
422
|
end
|
423
423
|
|
424
|
-
|
424
|
+
it "should validate length_of with allow_missing => true" do
|
425
425
|
@c.validates_length_of :value, :allow_missing => true, :minimum => 5
|
426
|
-
@m.
|
426
|
+
@m.must_be :valid?
|
427
427
|
@m.value = nil
|
428
|
-
@m.
|
428
|
+
@m.wont_be :valid?
|
429
429
|
end
|
430
430
|
|
431
|
-
|
431
|
+
it "should allow multiple calls to validates_length_of with different options without overwriting" do
|
432
432
|
@c.validates_length_of :value, :maximum => 5
|
433
433
|
@c.validates_length_of :value, :minimum => 5
|
434
|
-
@m.
|
434
|
+
@m.wont_be :valid?
|
435
435
|
@m.value = '12345'
|
436
|
-
@m.
|
436
|
+
@m.must_be :valid?
|
437
437
|
@m.value = '123456'
|
438
|
-
@m.
|
438
|
+
@m.wont_be :valid?
|
439
439
|
@m.value = '12345'
|
440
|
-
@m.
|
440
|
+
@m.must_be :valid?
|
441
441
|
@m.value = '1234'
|
442
|
-
@m.
|
442
|
+
@m.wont_be :valid?
|
443
443
|
end
|
444
444
|
|
445
|
-
|
445
|
+
it "should validate numericality_of" do
|
446
446
|
@c.validates_numericality_of :value
|
447
447
|
@m.value = 'blah'
|
448
|
-
@m.
|
448
|
+
@m.wont_be :valid?
|
449
449
|
@m.value = '123'
|
450
|
-
@m.
|
450
|
+
@m.must_be :valid?
|
451
451
|
@m.value = '123.1231'
|
452
|
-
@m.
|
452
|
+
@m.must_be :valid?
|
453
453
|
@m.value = '+1'
|
454
|
-
@m.
|
454
|
+
@m.must_be :valid?
|
455
455
|
@m.value = '-1'
|
456
|
-
@m.
|
456
|
+
@m.must_be :valid?
|
457
457
|
@m.value = '+1.123'
|
458
|
-
@m.
|
458
|
+
@m.must_be :valid?
|
459
459
|
@m.value = '-0.123'
|
460
|
-
@m.
|
460
|
+
@m.must_be :valid?
|
461
461
|
@m.value = '-0.123E10'
|
462
|
-
@m.
|
462
|
+
@m.must_be :valid?
|
463
463
|
@m.value = '32.123e10'
|
464
|
-
@m.
|
464
|
+
@m.must_be :valid?
|
465
465
|
@m.value = '+32.123E10'
|
466
|
-
@m.
|
467
|
-
@m.
|
466
|
+
@m.must_be :valid?
|
467
|
+
@m.must_be :valid?
|
468
468
|
@m.value = '.0123'
|
469
469
|
end
|
470
470
|
|
471
|
-
|
471
|
+
it "should validate numericality_of with only_integer" do
|
472
472
|
@c.validates_numericality_of :value, :only_integer => true
|
473
473
|
@m.value = 'blah'
|
474
|
-
@m.
|
474
|
+
@m.wont_be :valid?
|
475
475
|
@m.value = '123'
|
476
|
-
@m.
|
476
|
+
@m.must_be :valid?
|
477
477
|
@m.value = '123.1231'
|
478
|
-
@m.
|
478
|
+
@m.wont_be :valid?
|
479
479
|
end
|
480
480
|
|
481
|
-
|
481
|
+
it "should validate numericality_of with if => true" do
|
482
482
|
@c.validates_numericality_of :value, :if => :dont_skip
|
483
483
|
|
484
484
|
@m.value = 'a'
|
485
|
-
@m.
|
485
|
+
@m.wont_be :valid?
|
486
486
|
end
|
487
487
|
|
488
|
-
|
488
|
+
it "should validate numericality_of with if => false" do
|
489
489
|
@c.validates_numericality_of :value, :if => :skip
|
490
490
|
|
491
491
|
@m.value = 'a'
|
492
|
-
@m.
|
492
|
+
@m.must_be :valid?
|
493
493
|
end
|
494
494
|
|
495
|
-
|
495
|
+
it "should validate numericality_of with allow_missing => true" do
|
496
496
|
@c.validates_numericality_of :value, :allow_missing => true
|
497
|
-
@m.
|
497
|
+
@m.must_be :valid?
|
498
498
|
@m.value = nil
|
499
|
-
@m.
|
499
|
+
@m.wont_be :valid?
|
500
500
|
end
|
501
501
|
|
502
|
-
|
502
|
+
it "should validate presence_of" do
|
503
503
|
@c.validates_presence_of :value
|
504
|
-
@m.
|
504
|
+
@m.wont_be :valid?
|
505
505
|
@m.value = ''
|
506
|
-
@m.
|
506
|
+
@m.wont_be :valid?
|
507
507
|
@m.value = 1234
|
508
|
-
@m.
|
508
|
+
@m.must_be :valid?
|
509
509
|
@m.value = nil
|
510
|
-
@m.
|
510
|
+
@m.wont_be :valid?
|
511
511
|
@m.value = true
|
512
|
-
@m.
|
512
|
+
@m.must_be :valid?
|
513
513
|
@m.value = false
|
514
|
-
@m.
|
514
|
+
@m.must_be :valid?
|
515
515
|
end
|
516
516
|
|
517
|
-
|
517
|
+
it "should validate inclusion_of with an array" do
|
518
518
|
@c.validates_inclusion_of :value, :in => [1,2]
|
519
|
-
@m.
|
519
|
+
@m.wont_be :valid?
|
520
520
|
@m.value = 1
|
521
|
-
@m.
|
521
|
+
@m.must_be :valid?
|
522
522
|
@m.value = 1.5
|
523
|
-
@m.
|
523
|
+
@m.wont_be :valid?
|
524
524
|
@m.value = 2
|
525
|
-
@m.
|
525
|
+
@m.must_be :valid?
|
526
526
|
@m.value = 3
|
527
|
-
@m.
|
527
|
+
@m.wont_be :valid?
|
528
528
|
end
|
529
529
|
|
530
|
-
|
530
|
+
it "should validate inclusion_of with a range" do
|
531
531
|
@c.validates_inclusion_of :value, :in => 1..4
|
532
|
-
@m.
|
532
|
+
@m.wont_be :valid?
|
533
533
|
@m.value = 1
|
534
|
-
@m.
|
534
|
+
@m.must_be :valid?
|
535
535
|
@m.value = 1.5
|
536
|
-
@m.
|
536
|
+
@m.must_be :valid?
|
537
537
|
@m.value = 0
|
538
|
-
@m.
|
538
|
+
@m.wont_be :valid?
|
539
539
|
@m.value = 5
|
540
|
-
@m.
|
540
|
+
@m.wont_be :valid?
|
541
541
|
end
|
542
542
|
|
543
|
-
|
544
|
-
lambda{@c.validates_inclusion_of :value}.
|
545
|
-
lambda{@c.validates_inclusion_of :value, :in => 1}.
|
543
|
+
it "should raise an error if inclusion_of doesn't receive a valid :in option" do
|
544
|
+
lambda{@c.validates_inclusion_of :value}.must_raise(ArgumentError)
|
545
|
+
lambda{@c.validates_inclusion_of :value, :in => 1}.must_raise(ArgumentError)
|
546
546
|
end
|
547
547
|
|
548
|
-
|
548
|
+
it "should raise an error if inclusion_of handles :allow_nil too" do
|
549
549
|
@c.validates_inclusion_of :value, :in => 1..4, :allow_nil => true
|
550
550
|
@m.value = nil
|
551
|
-
@m.
|
551
|
+
@m.must_be :valid?
|
552
552
|
@m.value = 0
|
553
|
-
@m.
|
553
|
+
@m.wont_be :valid?
|
554
554
|
end
|
555
555
|
|
556
|
-
|
556
|
+
it "should validate presence_of with if => true" do
|
557
557
|
@c.validates_presence_of :value, :if => :dont_skip
|
558
|
-
@m.
|
558
|
+
@m.wont_be :valid?
|
559
559
|
end
|
560
560
|
|
561
|
-
|
561
|
+
it "should validate presence_of with if => false" do
|
562
562
|
@c.validates_presence_of :value, :if => :skip
|
563
|
-
@m.
|
563
|
+
@m.must_be :valid?
|
564
564
|
end
|
565
565
|
|
566
|
-
|
566
|
+
it "should validate presence_of with allow_missing => true" do
|
567
567
|
@c.validates_presence_of :value, :allow_missing => true
|
568
|
-
@m.
|
568
|
+
@m.must_be :valid?
|
569
569
|
@m.value = nil
|
570
|
-
@m.
|
570
|
+
@m.wont_be :valid?
|
571
571
|
end
|
572
572
|
|
573
|
-
|
573
|
+
it "should validate uniqueness_of with if => true" do
|
574
574
|
@c.validates_uniqueness_of :value, :if => :dont_skip
|
575
575
|
|
576
576
|
@m.value = 'a'
|
577
|
-
@m.
|
577
|
+
@m.wont_be :valid?
|
578
578
|
end
|
579
579
|
|
580
|
-
|
580
|
+
it "should validate uniqueness_of with if => false" do
|
581
581
|
@c.validates_uniqueness_of :value, :if => :skip
|
582
582
|
@m.value = 'a'
|
583
|
-
@m.
|
583
|
+
@m.must_be :valid?
|
584
584
|
end
|
585
585
|
|
586
|
-
|
586
|
+
it "should validate uniqueness_of with allow_missing => true" do
|
587
587
|
@c.validates_uniqueness_of :value, :allow_missing => true
|
588
|
-
@m.
|
588
|
+
@m.must_be :valid?
|
589
589
|
@m.value = 1
|
590
|
-
@m.
|
590
|
+
@m.wont_be :valid?
|
591
591
|
end
|
592
592
|
end
|
593
593
|
|
@@ -605,44 +605,44 @@ describe "Superclass validations" do
|
|
605
605
|
end
|
606
606
|
end
|
607
607
|
|
608
|
-
|
608
|
+
it "should be checked when validating" do
|
609
609
|
o = @c2.new
|
610
610
|
o.value = 'ab'
|
611
|
-
o.valid?.
|
612
|
-
o.errors.full_messages.
|
611
|
+
o.valid?.must_equal false
|
612
|
+
o.errors.full_messages.must_equal ['value is too short']
|
613
613
|
|
614
614
|
o.value = '12'
|
615
|
-
o.valid?.
|
616
|
-
o.errors.full_messages.
|
615
|
+
o.valid?.must_equal false
|
616
|
+
o.errors.full_messages.must_equal ['value is too short', 'value is invalid']
|
617
617
|
|
618
618
|
o.value = 'abcde'
|
619
|
-
o.valid?.
|
619
|
+
o.valid?.must_equal true
|
620
620
|
end
|
621
621
|
|
622
|
-
|
623
|
-
@c2.skip_superclass_validations?.
|
622
|
+
it "should have skip_superclass_validations? return whether superclass validations were skipped" do
|
623
|
+
@c2.skip_superclass_validations?.must_equal nil
|
624
624
|
@c2.skip_superclass_validations
|
625
|
-
@c2.skip_superclass_validations?.
|
625
|
+
@c2.skip_superclass_validations?.must_equal true
|
626
626
|
end
|
627
627
|
|
628
|
-
|
628
|
+
it "should be skipped if skip_superclass_validations is called" do
|
629
629
|
@c2.skip_superclass_validations
|
630
630
|
|
631
631
|
o = @c2.new
|
632
632
|
o.value = 'ab'
|
633
|
-
o.valid?.
|
633
|
+
o.valid?.must_equal true
|
634
634
|
|
635
635
|
o.value = '12'
|
636
|
-
o.valid?.
|
637
|
-
o.errors.full_messages.
|
636
|
+
o.valid?.must_equal false
|
637
|
+
o.errors.full_messages.must_equal ['value is invalid']
|
638
638
|
|
639
639
|
o.value = 'abcde'
|
640
|
-
o.valid?.
|
640
|
+
o.valid?.must_equal true
|
641
641
|
end
|
642
642
|
end
|
643
643
|
|
644
644
|
describe ".validates with block" do
|
645
|
-
|
645
|
+
it "should support calling .each" do
|
646
646
|
@c = model_class.call Sequel::Model do
|
647
647
|
columns :vvv
|
648
648
|
validates do
|
@@ -654,15 +654,14 @@ describe ".validates with block" do
|
|
654
654
|
|
655
655
|
o = @c.new
|
656
656
|
o.vvv = 1
|
657
|
-
o.
|
657
|
+
o.must_be :valid?
|
658
658
|
o.vvv = -1
|
659
|
-
o.
|
659
|
+
o.wont_be :valid?
|
660
660
|
end
|
661
661
|
end
|
662
662
|
|
663
663
|
describe Sequel::Model, "Validations" do
|
664
|
-
|
665
|
-
before(:all) do
|
664
|
+
before do
|
666
665
|
class ::Person < Sequel::Model
|
667
666
|
plugin :validation_class_methods
|
668
667
|
columns :id,:name,:first_name,:last_name,:middle_name,:initials,:age, :terms
|
@@ -691,7 +690,7 @@ describe Sequel::Model, "Validations" do
|
|
691
690
|
columns :id, :zip_code
|
692
691
|
end
|
693
692
|
end
|
694
|
-
after
|
693
|
+
after do
|
695
694
|
[:Person, :Smurf, :Cow, :User, :Address].each{|c| Object.send(:remove_const, c)}
|
696
695
|
end
|
697
696
|
|
@@ -702,11 +701,11 @@ describe Sequel::Model, "Validations" do
|
|
702
701
|
end
|
703
702
|
|
704
703
|
@cow = Cow.new
|
705
|
-
@cow.
|
706
|
-
@cow.errors.full_messages.
|
704
|
+
@cow.wont_be :valid?
|
705
|
+
@cow.errors.full_messages.must_equal ["got_milk is not accepted"]
|
707
706
|
|
708
707
|
@cow.got_milk = "blah"
|
709
|
-
@cow.
|
708
|
+
@cow.must_be :valid?
|
710
709
|
end
|
711
710
|
|
712
711
|
it "should validate the confirmation of a column" do
|
@@ -720,11 +719,11 @@ describe Sequel::Model, "Validations" do
|
|
720
719
|
end
|
721
720
|
|
722
721
|
@user = User.new
|
723
|
-
@user.
|
724
|
-
@user.errors.full_messages.
|
722
|
+
@user.wont_be :valid?
|
723
|
+
@user.errors.full_messages.must_equal ["password is not confirmed"]
|
725
724
|
|
726
725
|
@user.password = "test"
|
727
|
-
@user.
|
726
|
+
@user.must_be :valid?
|
728
727
|
end
|
729
728
|
|
730
729
|
it "should validate format of column" do
|
@@ -733,9 +732,9 @@ describe Sequel::Model, "Validations" do
|
|
733
732
|
end
|
734
733
|
|
735
734
|
@person = Person.new :first_name => "Lancelot99"
|
736
|
-
@person.valid?.
|
735
|
+
@person.valid?.must_equal false
|
737
736
|
@person = Person.new :first_name => "Anita"
|
738
|
-
@person.valid?.
|
737
|
+
@person.valid?.must_equal true
|
739
738
|
end
|
740
739
|
|
741
740
|
it "should validate length of column" do
|
@@ -754,20 +753,20 @@ describe Sequel::Model, "Validations" do
|
|
754
753
|
:middle_name => "danger"
|
755
754
|
)
|
756
755
|
|
757
|
-
@person.
|
758
|
-
@person.errors.full_messages.size.
|
759
|
-
@person.errors.full_messages.
|
756
|
+
@person.wont_be :valid?
|
757
|
+
@person.errors.full_messages.size.must_equal 4
|
758
|
+
@person.errors.full_messages.sort.must_equal [
|
760
759
|
'first_name is too long',
|
760
|
+
'initials is the wrong length',
|
761
761
|
'last_name is too short',
|
762
|
-
'middle_name is the wrong length'
|
763
|
-
|
764
|
-
)
|
762
|
+
'middle_name is the wrong length'
|
763
|
+
]
|
765
764
|
|
766
765
|
@person.first_name = "Lancelot"
|
767
766
|
@person.last_name = "1234567890123456789012345678901"
|
768
767
|
@person.initials = "LC"
|
769
768
|
@person.middle_name = "Will"
|
770
|
-
@person.
|
769
|
+
@person.must_be :valid?
|
771
770
|
end
|
772
771
|
|
773
772
|
it "should validate that a column has the correct type for the schema column" do
|
@@ -780,19 +779,19 @@ describe Sequel::Model, "Validations" do
|
|
780
779
|
end
|
781
780
|
|
782
781
|
@person = p.new
|
783
|
-
@person.
|
782
|
+
@person.must_be :valid?
|
784
783
|
|
785
784
|
@person.age = 'a'
|
786
|
-
@person.
|
787
|
-
@person.errors.full_messages.
|
785
|
+
@person.wont_be :valid?
|
786
|
+
@person.errors.full_messages.must_equal ['age is not a valid integer']
|
788
787
|
@person.age = 1
|
789
|
-
@person.
|
788
|
+
@person.must_be :valid?
|
790
789
|
|
791
790
|
@person.d = 'a'
|
792
|
-
@person.
|
793
|
-
@person.errors.full_messages.
|
791
|
+
@person.wont_be :valid?
|
792
|
+
@person.errors.full_messages.must_equal ['d is a bad choice']
|
794
793
|
@person.d = Date.today
|
795
|
-
@person.
|
794
|
+
@person.must_be :valid?
|
796
795
|
end
|
797
796
|
|
798
797
|
it "should validate numericality of column" do
|
@@ -802,11 +801,11 @@ describe Sequel::Model, "Validations" do
|
|
802
801
|
end
|
803
802
|
|
804
803
|
@person = Person.new :age => "Twenty"
|
805
|
-
@person.
|
806
|
-
@person.errors.full_messages.
|
804
|
+
@person.wont_be :valid?
|
805
|
+
@person.errors.full_messages.must_equal ['age is not a number']
|
807
806
|
|
808
807
|
@person.age = 20
|
809
|
-
@person.
|
808
|
+
@person.must_be :valid?
|
810
809
|
end
|
811
810
|
|
812
811
|
it "should validate the presence of a column" do
|
@@ -816,11 +815,11 @@ describe Sequel::Model, "Validations" do
|
|
816
815
|
end
|
817
816
|
|
818
817
|
@cow = Cow.new
|
819
|
-
@cow.
|
820
|
-
@cow.errors.full_messages.
|
818
|
+
@cow.wont_be :valid?
|
819
|
+
@cow.errors.full_messages.must_equal ['name is not present']
|
821
820
|
|
822
821
|
@cow.name = "Betsy"
|
823
|
-
@cow.
|
822
|
+
@cow.must_be :valid?
|
824
823
|
end
|
825
824
|
|
826
825
|
it "should validate the uniqueness of a column" do
|
@@ -844,30 +843,30 @@ describe Sequel::Model, "Validations" do
|
|
844
843
|
end
|
845
844
|
|
846
845
|
@user = User.new(:username => "2records", :password => "anothertest")
|
847
|
-
@user.
|
848
|
-
@user.errors.full_messages.
|
846
|
+
@user.wont_be :valid?
|
847
|
+
@user.errors.full_messages.must_equal ['username is already taken']
|
849
848
|
|
850
849
|
@user = User.new(:username => "1record", :password => "anothertest")
|
851
|
-
@user.
|
852
|
-
@user.errors.full_messages.
|
850
|
+
@user.wont_be :valid?
|
851
|
+
@user.errors.full_messages.must_equal ['username is already taken']
|
853
852
|
|
854
853
|
@user = User.load(:id=>4, :username => "1record", :password => "anothertest")
|
855
|
-
@user.
|
856
|
-
@user.errors.full_messages.
|
854
|
+
@user.wont_be :valid?
|
855
|
+
@user.errors.full_messages.must_equal ['username is already taken']
|
857
856
|
|
858
857
|
@user = User.load(:id=>3, :username => "1record", :password => "anothertest")
|
859
|
-
@user.
|
860
|
-
@user.errors.full_messages.
|
858
|
+
@user.must_be :valid?
|
859
|
+
@user.errors.full_messages.must_equal []
|
861
860
|
|
862
861
|
@user = User.new(:username => "0records", :password => "anothertest")
|
863
|
-
@user.
|
864
|
-
@user.errors.full_messages.
|
862
|
+
@user.must_be :valid?
|
863
|
+
@user.errors.full_messages.must_equal []
|
865
864
|
|
866
865
|
User.db.sqls
|
867
866
|
@user = User.new(:password => "anothertest")
|
868
|
-
@user.
|
869
|
-
@user.errors.full_messages.
|
870
|
-
User.db.sqls.
|
867
|
+
@user.must_be :valid?
|
868
|
+
@user.errors.full_messages.must_equal []
|
869
|
+
User.db.sqls.must_equal []
|
871
870
|
end
|
872
871
|
|
873
872
|
it "should validate the uniqueness of multiple columns" do
|
@@ -895,40 +894,40 @@ describe Sequel::Model, "Validations" do
|
|
895
894
|
end
|
896
895
|
|
897
896
|
@user = User.new(:username => "2records", :password => "anothertest")
|
898
|
-
@user.
|
899
|
-
@user.errors.full_messages.
|
897
|
+
@user.wont_be :valid?
|
898
|
+
@user.errors.full_messages.must_equal ['username and password is already taken']
|
900
899
|
|
901
900
|
@user = User.new(:username => "1record", :password => "anothertest")
|
902
|
-
@user.
|
903
|
-
@user.errors.full_messages.
|
901
|
+
@user.wont_be :valid?
|
902
|
+
@user.errors.full_messages.must_equal ['username and password is already taken']
|
904
903
|
|
905
904
|
@user = User.load(:id=>4, :username => "1record", :password => "anothertest")
|
906
|
-
@user.
|
907
|
-
@user.errors.full_messages.
|
905
|
+
@user.wont_be :valid?
|
906
|
+
@user.errors.full_messages.must_equal ['username and password is already taken']
|
908
907
|
|
909
908
|
@user = User.load(:id=>3, :username => "1record", :password => "test")
|
910
|
-
@user.
|
911
|
-
@user.errors.full_messages.
|
909
|
+
@user.wont_be :valid?
|
910
|
+
@user.errors.full_messages.must_equal ['username and password is already taken']
|
912
911
|
|
913
912
|
@user = User.load(:id=>3, :username => "1record", :password => "anothertest")
|
914
|
-
@user.
|
915
|
-
@user.errors.full_messages.
|
913
|
+
@user.must_be :valid?
|
914
|
+
@user.errors.full_messages.must_equal []
|
916
915
|
|
917
916
|
@user = User.new(:username => "0records", :password => "anothertest")
|
918
|
-
@user.
|
919
|
-
@user.errors.full_messages.
|
917
|
+
@user.must_be :valid?
|
918
|
+
@user.errors.full_messages.must_equal []
|
920
919
|
|
921
920
|
User.db.sqls
|
922
921
|
@user = User.new(:password => "anothertest")
|
923
|
-
@user.
|
924
|
-
@user.errors.full_messages.
|
922
|
+
@user.must_be :valid?
|
923
|
+
@user.errors.full_messages.must_equal []
|
925
924
|
@user = User.new(:username => "0records")
|
926
|
-
@user.
|
927
|
-
@user.errors.full_messages.
|
925
|
+
@user.must_be :valid?
|
926
|
+
@user.errors.full_messages.must_equal []
|
928
927
|
@user = User.new
|
929
|
-
@user.
|
930
|
-
@user.errors.full_messages.
|
931
|
-
User.db.sqls.
|
928
|
+
@user.must_be :valid?
|
929
|
+
@user.errors.full_messages.must_equal []
|
930
|
+
User.db.sqls.must_equal []
|
932
931
|
end
|
933
932
|
|
934
933
|
it "should have a validates block that contains multiple validations" do
|
@@ -940,23 +939,21 @@ describe Sequel::Model, "Validations" do
|
|
940
939
|
end
|
941
940
|
end
|
942
941
|
|
943
|
-
Person.validations[:first_name].size.
|
942
|
+
Person.validations[:first_name].size.must_equal 2
|
944
943
|
|
945
944
|
@person = Person.new :first_name => "Lancelot99"
|
946
|
-
@person.valid?.
|
945
|
+
@person.valid?.must_equal false
|
947
946
|
|
948
947
|
@person2 = Person.new :first_name => "Wayne"
|
949
|
-
@person2.valid?.
|
948
|
+
@person2.valid?.must_equal true
|
950
949
|
end
|
951
950
|
|
952
951
|
it "should allow 'longhand' validations direcly within the model." do
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
}.should_not raise_error
|
959
|
-
Person.validations.length.should eql(1)
|
952
|
+
class ::Person < Sequel::Model
|
953
|
+
validations.clear
|
954
|
+
validates_length_of :first_name, :maximum => 30
|
955
|
+
end
|
956
|
+
Person.validations.length.must_equal(1)
|
960
957
|
end
|
961
958
|
|
962
959
|
it "should define a has_validations? method which returns true if the model has validations, false otherwise" do
|
@@ -972,8 +969,8 @@ describe Sequel::Model, "Validations" do
|
|
972
969
|
validations.clear
|
973
970
|
end
|
974
971
|
|
975
|
-
Person.
|
976
|
-
Smurf.
|
972
|
+
Person.validations.wont_be :empty?
|
973
|
+
Smurf.validations.must_be :empty?
|
977
974
|
end
|
978
975
|
|
979
976
|
it "should validate correctly instances initialized with string keys" do
|
@@ -981,8 +978,8 @@ describe Sequel::Model, "Validations" do
|
|
981
978
|
validates_length_of :name, :minimum => 4
|
982
979
|
end
|
983
980
|
|
984
|
-
Can.new('name' => 'ab').
|
985
|
-
Can.new('name' => 'abcd').
|
981
|
+
Can.new('name' => 'ab').wont_be :valid?
|
982
|
+
Can.new('name' => 'abcd').must_be :valid?
|
986
983
|
end
|
987
984
|
|
988
985
|
end
|
@@ -1000,31 +997,31 @@ describe "Model#save" do
|
|
1000
997
|
DB.reset
|
1001
998
|
end
|
1002
999
|
|
1003
|
-
|
1000
|
+
it "should save only if validations pass" do
|
1004
1001
|
@m.raise_on_save_failure = false
|
1005
|
-
@m.
|
1002
|
+
@m.wont_be :valid?
|
1006
1003
|
@m.save
|
1007
|
-
DB.sqls.
|
1004
|
+
DB.sqls.must_be :empty?
|
1008
1005
|
|
1009
1006
|
@m.x = 7
|
1010
|
-
@m.
|
1011
|
-
@m.save.
|
1012
|
-
DB.sqls.
|
1007
|
+
@m.must_be :valid?
|
1008
|
+
@m.save.wont_equal false
|
1009
|
+
DB.sqls.must_equal ['UPDATE people SET x = 7 WHERE (id = 4)']
|
1013
1010
|
end
|
1014
1011
|
|
1015
|
-
|
1012
|
+
it "should skip validations if the :validate=>false option is used" do
|
1016
1013
|
@m.raise_on_save_failure = false
|
1017
|
-
@m.
|
1014
|
+
@m.wont_be :valid?
|
1018
1015
|
@m.save(:validate=>false)
|
1019
|
-
DB.sqls.
|
1016
|
+
DB.sqls.must_equal ['UPDATE people SET x = 6 WHERE (id = 4)']
|
1020
1017
|
end
|
1021
1018
|
|
1022
|
-
|
1023
|
-
proc{@m.save}.
|
1019
|
+
it "should raise error if validations fail and raise_on_save_faiure is true" do
|
1020
|
+
proc{@m.save}.must_raise(Sequel::ValidationFailed)
|
1024
1021
|
end
|
1025
1022
|
|
1026
|
-
|
1023
|
+
it "should return nil if validations fail and raise_on_save_faiure is false" do
|
1027
1024
|
@m.raise_on_save_failure = false
|
1028
|
-
@m.save.
|
1025
|
+
@m.save.must_equal nil
|
1029
1026
|
end
|
1030
1027
|
end
|