dm-validations 0.9.9 → 0.9.10
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +15 -0
- data/Manifest.txt +21 -2
- data/lib/dm-validations.rb +14 -14
- data/lib/dm-validations/absent_field_validator.rb +3 -4
- data/lib/dm-validations/acceptance_validator.rb +6 -10
- data/lib/dm-validations/confirmation_validator.rb +5 -5
- data/lib/dm-validations/contextual_validators.rb +1 -1
- data/lib/dm-validations/custom_validator.rb +1 -1
- data/lib/dm-validations/format_validator.rb +6 -5
- data/lib/dm-validations/generic_validator.rb +7 -10
- data/lib/dm-validations/length_validator.rb +7 -7
- data/lib/dm-validations/method_validator.rb +2 -2
- data/lib/dm-validations/numeric_validator.rb +4 -4
- data/lib/dm-validations/primitive_validator.rb +4 -4
- data/lib/dm-validations/required_field_validator.rb +5 -5
- data/lib/dm-validations/uniqueness_validator.rb +2 -2
- data/lib/dm-validations/validation_errors.rb +34 -2
- data/lib/dm-validations/version.rb +1 -1
- data/lib/dm-validations/within_validator.rb +15 -13
- data/spec/integration/absent_field_validator_spec.rb +4 -2
- data/spec/integration/acceptance_validator_spec.rb +3 -3
- data/spec/integration/auto_validate_spec.rb +16 -9
- data/spec/integration/block_validator_spec.rb +2 -8
- data/spec/integration/confirmation_validator_spec.rb +11 -8
- data/spec/integration/contextual_validators_spec.rb +2 -1
- data/spec/integration/format_validator_spec.rb +1 -1
- data/spec/integration/length_validator/error_message_spec.rb +23 -0
- data/spec/integration/length_validator/maximum_spec.rb +31 -0
- data/spec/integration/length_validator/minimum_spec.rb +31 -0
- data/spec/integration/length_validator/range_spec.rb +95 -0
- data/spec/integration/length_validator/spec_helper.rb +12 -0
- data/spec/integration/length_validator/valid_objects_spec.rb +13 -0
- data/spec/integration/method_validator_spec.rb +3 -3
- data/spec/integration/numeric_validator/float_type_spec.rb +102 -0
- data/spec/integration/numeric_validator/integer_only_true_spec.rb +92 -0
- data/spec/integration/numeric_validator/integer_type_spec.rb +100 -0
- data/spec/integration/numeric_validator/spec_helper.rb +77 -0
- data/spec/integration/numeric_validator_spec.rb +19 -6
- data/spec/integration/primitive_validator_spec.rb +2 -1
- data/spec/integration/required_field_validator/association_spec.rb +98 -0
- data/spec/integration/required_field_validator/boolean_type_value_spec.rb +149 -0
- data/spec/integration/required_field_validator/date_type_value_spec.rb +126 -0
- data/spec/integration/required_field_validator/datetime_type_value_spec.rb +126 -0
- data/spec/integration/required_field_validator/float_type_value_spec.rb +130 -0
- data/spec/integration/required_field_validator/integer_type_value_spec.rb +98 -0
- data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +36 -0
- data/spec/integration/required_field_validator/shared_examples.rb +24 -0
- data/spec/integration/required_field_validator/spec_helper.rb +68 -0
- data/spec/integration/required_field_validator/string_type_value_spec.rb +164 -0
- data/spec/integration/required_field_validator/text_type_value_spec.rb +46 -0
- data/spec/integration/uniqueness_validator_spec.rb +10 -8
- data/spec/integration/validation_spec.rb +25 -25
- data/spec/integration/within_validator_spec.rb +36 -11
- data/tasks/spec.rb +1 -1
- metadata +24 -5
- data/spec/integration/length_validator_spec.rb +0 -115
- data/spec/integration/required_field_validator_spec.rb +0 -93
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
__dir__ = Pathname(__FILE__).dirname.expand_path
|
3
|
+
|
4
|
+
require __dir__.parent.parent + 'spec_helper'
|
5
|
+
require __dir__ + 'spec_helper'
|
6
|
+
|
7
|
+
if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
8
|
+
class ScheduledOperation
|
9
|
+
#
|
10
|
+
# Behaviors
|
11
|
+
#
|
12
|
+
|
13
|
+
include DataMapper::Resource
|
14
|
+
|
15
|
+
#
|
16
|
+
# Properties
|
17
|
+
#
|
18
|
+
|
19
|
+
property :id, Integer, :serial => true
|
20
|
+
property :at, DateTime, :auto_validation => false
|
21
|
+
|
22
|
+
#
|
23
|
+
# Validations
|
24
|
+
#
|
25
|
+
|
26
|
+
validates_present :at
|
27
|
+
end
|
28
|
+
ScheduledOperation.auto_migrate!
|
29
|
+
|
30
|
+
|
31
|
+
describe ScheduledOperation do
|
32
|
+
before :each do
|
33
|
+
@operation = ScheduledOperation.new(:at => DateTime.civil(2008, 06, 07, 15, 00, 00))
|
34
|
+
@operation.should be_valid
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
describe "with on = nil" do
|
39
|
+
before(:each) do
|
40
|
+
@operation.at = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it "is NOT valid" do
|
44
|
+
# nil = missing for Date value
|
45
|
+
# and ScheduledOperation only has default validation context
|
46
|
+
@operation.should_not be_valid
|
47
|
+
|
48
|
+
# sanity check
|
49
|
+
@operation.at = Date.new(2008, 12, 31)
|
50
|
+
@operation.should be_valid
|
51
|
+
end
|
52
|
+
end # describe "with on = nil"
|
53
|
+
|
54
|
+
|
55
|
+
describe "with on = valid date" do
|
56
|
+
before(:each) do
|
57
|
+
@operation.at = 0.0
|
58
|
+
end
|
59
|
+
|
60
|
+
it "IS valid" do
|
61
|
+
# yes, presence validator does not care
|
62
|
+
@operation.should be_valid
|
63
|
+
end
|
64
|
+
end # describe "with on = 0.0"
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
describe "with on = 0" do
|
69
|
+
before(:each) do
|
70
|
+
@operation.at = 0
|
71
|
+
end
|
72
|
+
|
73
|
+
it "IS valid" do
|
74
|
+
# yes, presence validator does not care
|
75
|
+
@operation.should be_valid
|
76
|
+
end
|
77
|
+
end # describe "with on = 0"
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
describe "with on = 100" do
|
82
|
+
before(:each) do
|
83
|
+
@operation.at = 100
|
84
|
+
end
|
85
|
+
|
86
|
+
it "IS valid" do
|
87
|
+
@operation.should be_valid
|
88
|
+
end
|
89
|
+
end # describe "with on = 100"
|
90
|
+
|
91
|
+
|
92
|
+
describe "with on = 100.0" do
|
93
|
+
before(:each) do
|
94
|
+
@operation.at = 100.0
|
95
|
+
end
|
96
|
+
|
97
|
+
it "IS valid" do
|
98
|
+
@operation.should be_valid
|
99
|
+
end
|
100
|
+
end # describe "with on = 100.0"
|
101
|
+
|
102
|
+
|
103
|
+
describe "with on = -1100" do
|
104
|
+
before(:each) do
|
105
|
+
# presence validator does not care
|
106
|
+
@operation.at = -1100
|
107
|
+
end
|
108
|
+
|
109
|
+
it "IS valid" do
|
110
|
+
@operation.should be_valid
|
111
|
+
end
|
112
|
+
end # describe "with on = -1100"
|
113
|
+
|
114
|
+
|
115
|
+
describe "with on = -1100.5" do
|
116
|
+
before(:each) do
|
117
|
+
# presence validator does not care
|
118
|
+
@operation.at = -1100.5
|
119
|
+
end
|
120
|
+
|
121
|
+
it "IS valid" do
|
122
|
+
@operation.should be_valid
|
123
|
+
end
|
124
|
+
end # describe "with on = -1100.5"
|
125
|
+
end # describe ScheduledOperation
|
126
|
+
end # if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
__dir__ = Pathname(__FILE__).dirname.expand_path
|
3
|
+
|
4
|
+
require __dir__.parent.parent + 'spec_helper'
|
5
|
+
require __dir__ + 'spec_helper'
|
6
|
+
|
7
|
+
if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
8
|
+
#
|
9
|
+
# Especially stupid example since Hg adds local repository revision
|
10
|
+
# to each new commit, but lets roll on with this SCM-ish classes and
|
11
|
+
# still show how Integer type values are validated for presence
|
12
|
+
#
|
13
|
+
class CpuConsumption
|
14
|
+
#
|
15
|
+
# Behaviors
|
16
|
+
#
|
17
|
+
|
18
|
+
include DataMapper::Resource
|
19
|
+
|
20
|
+
#
|
21
|
+
# Properties
|
22
|
+
#
|
23
|
+
|
24
|
+
property :id, Integer, :serial => true
|
25
|
+
property :percent, Float, :auto_validation => false
|
26
|
+
|
27
|
+
#
|
28
|
+
# Validations
|
29
|
+
#
|
30
|
+
|
31
|
+
validates_present :percent
|
32
|
+
end
|
33
|
+
CpuConsumption.auto_migrate!
|
34
|
+
|
35
|
+
|
36
|
+
describe CpuConsumption do
|
37
|
+
before :each do
|
38
|
+
@metric = CpuConsumption.new(:percent => 20.0)
|
39
|
+
@metric.should be_valid
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "with percentage = 0.0" do
|
43
|
+
before(:each) do
|
44
|
+
@metric.percent = 0.0
|
45
|
+
end
|
46
|
+
|
47
|
+
it "IS valid" do
|
48
|
+
# yes, presence validator does not care
|
49
|
+
@metric.should be_valid
|
50
|
+
end
|
51
|
+
end # describe "with percentage = 0.0"
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
describe "with percentage = 0" do
|
56
|
+
before(:each) do
|
57
|
+
@metric.percent = 0
|
58
|
+
end
|
59
|
+
|
60
|
+
it "IS valid" do
|
61
|
+
# yes, presence validator does not care
|
62
|
+
@metric.should be_valid
|
63
|
+
end
|
64
|
+
end # describe "with percentage = 0"
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
describe "with percentage = 100" do
|
69
|
+
before(:each) do
|
70
|
+
@metric.percent = 100
|
71
|
+
end
|
72
|
+
|
73
|
+
it "IS valid" do
|
74
|
+
@metric.should be_valid
|
75
|
+
end
|
76
|
+
end # describe "with percentage = 100"
|
77
|
+
|
78
|
+
|
79
|
+
describe "with percentage = 100.0" do
|
80
|
+
before(:each) do
|
81
|
+
@metric.percent = 100.0
|
82
|
+
end
|
83
|
+
|
84
|
+
it "IS valid" do
|
85
|
+
@metric.should be_valid
|
86
|
+
end
|
87
|
+
end # describe "with percentage = 100.0"
|
88
|
+
|
89
|
+
|
90
|
+
describe "with percentage = -1100" do
|
91
|
+
before(:each) do
|
92
|
+
# presence validator does not care
|
93
|
+
@metric.percent = -1100
|
94
|
+
end
|
95
|
+
|
96
|
+
it "IS valid" do
|
97
|
+
@metric.should be_valid
|
98
|
+
end
|
99
|
+
end # describe "with percentage = -1100"
|
100
|
+
|
101
|
+
|
102
|
+
describe "with percentage = -1100.5" do
|
103
|
+
before(:each) do
|
104
|
+
# presence validator does not care
|
105
|
+
@metric.percent = -1100.5
|
106
|
+
end
|
107
|
+
|
108
|
+
it "IS valid" do
|
109
|
+
@metric.should be_valid
|
110
|
+
end
|
111
|
+
end # describe "with percentage = -1100.5"
|
112
|
+
|
113
|
+
|
114
|
+
describe "with percentage = nil" do
|
115
|
+
before(:each) do
|
116
|
+
@metric.percent = nil
|
117
|
+
end
|
118
|
+
|
119
|
+
it "is NOT valid" do
|
120
|
+
# nil = missing for float value
|
121
|
+
# and CpuConsumption only has default validation context
|
122
|
+
@metric.should_not be_valid
|
123
|
+
|
124
|
+
# sanity check
|
125
|
+
@metric.percent = 100
|
126
|
+
@metric.should be_valid
|
127
|
+
end
|
128
|
+
end # describe "with percentage = nil"
|
129
|
+
end # describe CpuConsumption
|
130
|
+
end # if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
__dir__ = Pathname(__FILE__).dirname.expand_path
|
3
|
+
|
4
|
+
require __dir__.parent.parent + 'spec_helper'
|
5
|
+
require __dir__ + 'spec_helper'
|
6
|
+
|
7
|
+
if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
8
|
+
#
|
9
|
+
# Especially stupid example since Hg adds local repository revision
|
10
|
+
# to each new commit, but lets roll on with this SCM-ish classes and
|
11
|
+
# still show how Integer type values are validated for presence
|
12
|
+
#
|
13
|
+
class HgCommit < ScmOperation
|
14
|
+
#
|
15
|
+
# Properties
|
16
|
+
#
|
17
|
+
|
18
|
+
property :local_repo_revision_num, Integer, :auto_validation => false
|
19
|
+
|
20
|
+
#
|
21
|
+
# Validations
|
22
|
+
#
|
23
|
+
|
24
|
+
validates_present :local_repo_revision_num
|
25
|
+
end
|
26
|
+
HgCommit.auto_migrate!
|
27
|
+
|
28
|
+
|
29
|
+
describe HgCommit do
|
30
|
+
before :each do
|
31
|
+
@operation = HgCommit.new(:local_repo_revision_num => 90, :name => "ci")
|
32
|
+
@operation.should be_valid
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "with local revision number = 0" do
|
36
|
+
before(:each) do
|
37
|
+
@operation.local_repo_revision_num = 0
|
38
|
+
end
|
39
|
+
|
40
|
+
it "IS valid" do
|
41
|
+
# yes, presence validator does not care
|
42
|
+
@operation.should be_valid
|
43
|
+
end
|
44
|
+
end # describe "with local revision number = 0"
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
describe "with local revision number = 100" do
|
49
|
+
before(:each) do
|
50
|
+
@operation.local_repo_revision_num = 100
|
51
|
+
end
|
52
|
+
|
53
|
+
it "IS valid" do
|
54
|
+
@operation.should be_valid
|
55
|
+
end
|
56
|
+
end # describe "with local revision number = 100"
|
57
|
+
|
58
|
+
|
59
|
+
describe "with local revision number = 100.0 (float!)" do
|
60
|
+
before(:each) do
|
61
|
+
@operation.local_repo_revision_num = 100.0
|
62
|
+
end
|
63
|
+
|
64
|
+
it "IS valid" do
|
65
|
+
@operation.should be_valid
|
66
|
+
end
|
67
|
+
end # describe "with local revision number = 100.0 (float!)"
|
68
|
+
|
69
|
+
|
70
|
+
describe "with local revision number = -1100" do
|
71
|
+
before(:each) do
|
72
|
+
# presence validator does not care
|
73
|
+
@operation.local_repo_revision_num = -1100
|
74
|
+
end
|
75
|
+
|
76
|
+
it "IS valid" do
|
77
|
+
@operation.should be_valid
|
78
|
+
end
|
79
|
+
end # describe "with local revision number = -1100"
|
80
|
+
|
81
|
+
|
82
|
+
describe "with local revision number = nil" do
|
83
|
+
before(:each) do
|
84
|
+
@operation.local_repo_revision_num = nil
|
85
|
+
end
|
86
|
+
|
87
|
+
it "is NOT valid" do
|
88
|
+
# nil = missing for integer value
|
89
|
+
# and HgCommit only has default validation context
|
90
|
+
@operation.should_not be_valid
|
91
|
+
|
92
|
+
# sanity check
|
93
|
+
@operation.local_repo_revision_num = 100
|
94
|
+
@operation.should be_valid
|
95
|
+
end
|
96
|
+
end # describe "with local revision number = nil"
|
97
|
+
end # describe HgCommit
|
98
|
+
end # if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
__dir__ = Pathname(__FILE__).dirname.expand_path
|
3
|
+
|
4
|
+
require __dir__.parent.parent + 'spec_helper'
|
5
|
+
require __dir__ + 'spec_helper'
|
6
|
+
|
7
|
+
if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
8
|
+
describe "A plain old Ruby object (not a DM resource)" do
|
9
|
+
before do
|
10
|
+
class PlainClass
|
11
|
+
extend DataMapper::Validate::ClassMethods
|
12
|
+
include DataMapper::Validate
|
13
|
+
attr_accessor :accessor
|
14
|
+
validates_present :here, :empty, :nil, :accessor
|
15
|
+
def here; "here" end
|
16
|
+
def empty; "" end
|
17
|
+
def nil; nil end
|
18
|
+
end
|
19
|
+
|
20
|
+
@pc = PlainClass.new
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should fail validation with empty, nil, or blank fields" do
|
24
|
+
@pc.should_not be_valid
|
25
|
+
@pc.errors.on(:empty).should include("Empty must not be blank")
|
26
|
+
@pc.errors.on(:nil).should include("Nil must not be blank")
|
27
|
+
@pc.errors.on(:accessor).should include("Accessor must not be blank")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "giving accessor a value should remove validation error" do
|
31
|
+
@pc.accessor = "full"
|
32
|
+
@pc.valid?
|
33
|
+
@pc.errors.on(:accessor).should be_nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe GitOperation do
|
2
|
+
before :each do
|
3
|
+
@operation = GitOperation.new
|
4
|
+
end
|
5
|
+
|
6
|
+
describe "unnamed SCM operation", :shared => true do
|
7
|
+
before :each do
|
8
|
+
@operation.name = nil
|
9
|
+
@operation.valid?
|
10
|
+
end
|
11
|
+
|
12
|
+
it "is not valid" do
|
13
|
+
@operation.should_not be_valid
|
14
|
+
end
|
15
|
+
|
16
|
+
it "is not valid in default validation context" do
|
17
|
+
@operation.should_not be_valid(:default)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "points to blank name in the error message" do
|
21
|
+
@operation.errors.on(:name).should include('Name must not be blank')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
2
|
+
|
3
|
+
#
|
4
|
+
# SCMs
|
5
|
+
#
|
6
|
+
# This example may look stupid (I am sure it is),
|
7
|
+
# but it is way better than foobars and easier to read/add cases
|
8
|
+
# compared to gardening examples because every software engineer has idea
|
9
|
+
# about SCMs and not every software engineer does gardening often.
|
10
|
+
#
|
11
|
+
|
12
|
+
class ScmOperation
|
13
|
+
include DataMapper::Resource
|
14
|
+
|
15
|
+
#
|
16
|
+
# Property
|
17
|
+
#
|
18
|
+
|
19
|
+
property :id, Integer, :serial => true
|
20
|
+
|
21
|
+
# operation name
|
22
|
+
property :name, String, :auto_validation => false
|
23
|
+
|
24
|
+
property :committer_name, String, :auto_validation => false, :default => "Just another Ruby hacker"
|
25
|
+
property :author_name, String, :auto_validation => false, :default => "Just another Ruby hacker"
|
26
|
+
property :network_connection, Boolean, :auto_validation => false
|
27
|
+
property :message, Text, :auto_validation => false
|
28
|
+
property :clean_working_copy, Boolean, :auto_validation => false
|
29
|
+
|
30
|
+
#
|
31
|
+
# Validations
|
32
|
+
#
|
33
|
+
|
34
|
+
validates_present :name
|
35
|
+
end
|
36
|
+
|
37
|
+
class SubversionOperation < ScmOperation
|
38
|
+
#
|
39
|
+
# Validations
|
40
|
+
#
|
41
|
+
|
42
|
+
validates_present :network_connection, :when => [:committing, :log_viewing]
|
43
|
+
end
|
44
|
+
|
45
|
+
class GitOperation < ScmOperation
|
46
|
+
#
|
47
|
+
# Validations
|
48
|
+
#
|
49
|
+
|
50
|
+
validates_present :author_name, :when => :committing
|
51
|
+
validates_present :committer_name, :when => :committing
|
52
|
+
|
53
|
+
validates_present :message, :when => :committing
|
54
|
+
validates_present :network_connection, :when => [:pushing, :pulling], :message => {
|
55
|
+
:pushing => "though git is advanced, it cannot push without network connectivity",
|
56
|
+
:pulling => "you must have network connectivity to pull from others"
|
57
|
+
}
|
58
|
+
validates_present :clean_working_copy, :when => :pulling
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
[ScmOperation, SubversionOperation, GitOperation].each do |dm_resource|
|
63
|
+
dm_resource.auto_migrate!
|
64
|
+
end
|
65
|
+
|
66
|
+
__dir__ = File.dirname(__FILE__)
|
67
|
+
require File.join(__dir__, "shared_examples")
|
68
|
+
end
|