dm-validations 0.9.11 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{History.txt → History.rdoc} +10 -3
- data/LICENSE +1 -0
- data/Manifest.txt +103 -33
- data/README.rdoc +172 -0
- data/Rakefile +4 -5
- data/lib/dm-validations.rb +73 -75
- data/lib/dm-validations/auto_validate.rb +98 -56
- data/lib/dm-validations/contextual_validators.rb +37 -14
- data/lib/dm-validations/exceptions.rb +3 -0
- data/lib/dm-validations/formats/url.rb +3 -1
- data/lib/dm-validations/support/context.rb +54 -0
- data/lib/dm-validations/validation_errors.rb +51 -27
- data/lib/dm-validations/{absent_field_validator.rb → validators/absent_field_validator.rb} +2 -8
- data/lib/dm-validations/{acceptance_validator.rb → validators/acceptance_validator.rb} +7 -7
- data/lib/dm-validations/{block_validator.rb → validators/block_validator.rb} +0 -0
- data/lib/dm-validations/{confirmation_validator.rb → validators/confirmation_validator.rb} +7 -7
- data/lib/dm-validations/{format_validator.rb → validators/format_validator.rb} +10 -16
- data/lib/dm-validations/{generic_validator.rb → validators/generic_validator.rb} +28 -6
- data/lib/dm-validations/validators/length_validator.rb +214 -0
- data/lib/dm-validations/validators/method_validator.rb +68 -0
- data/lib/dm-validations/validators/numeric_validator.rb +171 -0
- data/lib/dm-validations/{primitive_validator.rb → validators/primitive_validator.rb} +2 -7
- data/lib/dm-validations/{required_field_validator.rb → validators/required_field_validator.rb} +0 -6
- data/lib/dm-validations/{uniqueness_validator.rb → validators/uniqueness_validator.rb} +9 -20
- data/lib/dm-validations/{within_validator.rb → validators/within_validator.rb} +4 -4
- data/lib/dm-validations/version.rb +1 -1
- data/spec/fixtures/barcode.rb +32 -0
- data/spec/fixtures/basketball_court.rb +58 -0
- data/spec/fixtures/basketball_player.rb +37 -0
- data/spec/fixtures/beta_tester_account.rb +33 -0
- data/spec/fixtures/bill_of_landing.rb +42 -0
- data/spec/fixtures/boat_dock.rb +26 -0
- data/spec/fixtures/city.rb +25 -0
- data/spec/fixtures/company.rb +95 -0
- data/spec/fixtures/corporate_world.rb +47 -0
- data/spec/fixtures/country.rb +25 -0
- data/spec/fixtures/currency.rb +42 -0
- data/spec/fixtures/ethernet_frame.rb +56 -0
- data/spec/fixtures/event.rb +44 -0
- data/spec/fixtures/g3_concert.rb +57 -0
- data/spec/fixtures/jabberwock.rb +27 -0
- data/spec/fixtures/kayak.rb +28 -0
- data/spec/fixtures/lernean_hydra.rb +39 -0
- data/spec/fixtures/mathematical_function.rb +34 -0
- data/spec/fixtures/memory_object.rb +30 -0
- data/spec/fixtures/mittelschnauzer.rb +35 -0
- data/spec/fixtures/motor_launch.rb +21 -0
- data/spec/fixtures/page.rb +32 -0
- data/spec/fixtures/phone_number.rb +28 -0
- data/spec/fixtures/pirogue.rb +28 -0
- data/spec/fixtures/programming_language.rb +83 -0
- data/spec/fixtures/reservation.rb +38 -0
- data/spec/fixtures/scm_operation.rb +67 -0
- data/spec/fixtures/sms_message.rb +22 -0
- data/spec/fixtures/udp_packet.rb +49 -0
- data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +86 -0
- data/spec/integration/absent_field_validator/spec_helper.rb +7 -0
- data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +196 -0
- data/spec/integration/acceptance_validator/spec_helper.rb +7 -0
- data/spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +47 -0
- data/spec/integration/automatic_validation/disabling_inferred_validation_spec.rb +41 -0
- data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +104 -0
- data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +35 -0
- data/spec/integration/automatic_validation/inferred_format_validation_spec.rb +33 -0
- data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +73 -0
- data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +121 -0
- data/spec/integration/automatic_validation/inferred_presence_validation_spec.rb +41 -0
- data/spec/integration/automatic_validation/inferred_primitive_validation_spec.rb +20 -0
- data/spec/integration/automatic_validation/inferred_within_validation_spec.rb +33 -0
- data/spec/integration/automatic_validation/spec_helper.rb +74 -0
- data/spec/integration/block_validator/block_validator_spec.rb +32 -0
- data/spec/integration/block_validator/spec_helper.rb +5 -0
- data/spec/integration/conditional_validation/if_condition_spec.rb +61 -0
- data/spec/integration/conditional_validation/spec_helper.rb +5 -0
- data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +74 -0
- data/spec/integration/confirmation_validator/spec_helper.rb +5 -0
- data/spec/integration/datamapper_models/association_validation_spec.rb +23 -0
- data/spec/integration/datamapper_models/inheritance_spec.rb +78 -0
- data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +22 -0
- data/spec/integration/duplicated_validations/spec_helper.rb +5 -0
- data/spec/integration/format_validator/email_format_validator_spec.rb +113 -0
- data/spec/integration/format_validator/format_validator_spec.rb +60 -0
- data/spec/integration/format_validator/regexp_validator_spec.rb +29 -0
- data/spec/integration/format_validator/spec_helper.rb +5 -0
- data/spec/integration/format_validator/url_format_validator_spec.rb +63 -0
- data/spec/integration/length_validator/default_value_spec.rb +12 -0
- data/spec/integration/length_validator/equality_spec.rb +79 -0
- data/spec/integration/length_validator/error_message_spec.rb +15 -18
- data/spec/integration/length_validator/maximum_spec.rb +40 -24
- data/spec/integration/length_validator/minimum_spec.rb +32 -21
- data/spec/integration/length_validator/range_spec.rb +50 -60
- data/spec/integration/length_validator/spec_helper.rb +7 -12
- data/spec/integration/method_validator/method_validator_spec.rb +239 -0
- data/spec/integration/method_validator/spec_helper.rb +5 -0
- data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +61 -0
- data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +37 -0
- data/spec/integration/numeric_validator/float_type_spec.rb +42 -56
- data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +35 -0
- data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +35 -0
- data/spec/integration/numeric_validator/integer_only_true_spec.rb +3 -6
- data/spec/integration/numeric_validator/integer_type_spec.rb +8 -24
- data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +35 -0
- data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +35 -0
- data/spec/integration/numeric_validator/spec_helper.rb +5 -77
- data/spec/integration/primitive_validator/primitive_validator_spec.rb +90 -0
- data/spec/integration/primitive_validator/spec_helper.rb +5 -0
- data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +118 -0
- data/spec/integration/required_field_validator/association_spec.rb +5 -8
- data/spec/integration/required_field_validator/boolean_type_value_spec.rb +4 -7
- data/spec/integration/required_field_validator/date_type_value_spec.rb +3 -6
- data/spec/integration/required_field_validator/datetime_type_value_spec.rb +3 -6
- data/spec/integration/required_field_validator/float_type_value_spec.rb +3 -6
- data/spec/integration/required_field_validator/integer_type_value_spec.rb +2 -5
- data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +5 -8
- data/spec/integration/required_field_validator/shared_examples.rb +1 -1
- data/spec/integration/required_field_validator/spec_helper.rb +6 -67
- data/spec/integration/required_field_validator/string_type_value_spec.rb +2 -5
- data/spec/integration/required_field_validator/text_type_value_spec.rb +2 -5
- data/spec/integration/shared/default_validation_context.rb +13 -0
- data/spec/integration/shared/valid_and_invalid_model.rb +27 -0
- data/spec/integration/uniqueness_validator/spec_helper.rb +5 -0
- data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +114 -0
- data/spec/integration/within_validator/spec_helper.rb +5 -0
- data/spec/integration/within_validator/within_validator_spec.rb +164 -0
- data/spec/public/resource_spec.rb +68 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +12 -4
- data/spec/unit/contextual_validators/emptiness_spec.rb +50 -0
- data/spec/unit/contextual_validators/execution_spec.rb +55 -0
- data/spec/unit/contextual_validators/spec_helper.rb +37 -0
- data/spec/unit/generic_validator/equality_operator_spec.rb +26 -0
- data/spec/unit/validation_errors/adding_spec.rb +54 -0
- data/spec/unit/validation_errors/emptiness_spec.rb +38 -0
- data/spec/unit/validation_errors/enumerable_spec.rb +32 -0
- data/tasks/install.rb +1 -1
- data/tasks/spec.rb +4 -4
- metadata +118 -55
- data/README.txt +0 -72
- data/lib/dm-validations/custom_validator.rb +0 -72
- data/lib/dm-validations/length_validator.rb +0 -113
- data/lib/dm-validations/method_validator.rb +0 -68
- data/lib/dm-validations/numeric_validator.rb +0 -83
- data/spec/integration/absent_field_validator_spec.rb +0 -36
- data/spec/integration/acceptance_validator_spec.rb +0 -87
- data/spec/integration/auto_validate_spec.rb +0 -342
- data/spec/integration/block_validator_spec.rb +0 -30
- data/spec/integration/confirmation_validator_spec.rb +0 -105
- data/spec/integration/contextual_validators_spec.rb +0 -27
- data/spec/integration/custom_validator_spec.rb +0 -9
- data/spec/integration/format_validator_spec.rb +0 -193
- data/spec/integration/generic_validator_spec.rb +0 -17
- data/spec/integration/length_validator/valid_objects_spec.rb +0 -13
- data/spec/integration/method_validator_spec.rb +0 -58
- data/spec/integration/numeric_validator_spec.rb +0 -253
- data/spec/integration/primitive_validator_spec.rb +0 -30
- data/spec/integration/uniqueness_validator_spec.rb +0 -97
- data/spec/integration/validation_errors_spec.rb +0 -18
- data/spec/integration/validation_spec.rb +0 -404
- data/spec/integration/within_validator_spec.rb +0 -79
@@ -0,0 +1,164 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'integration/within_validator/spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Validate::Fixtures::PhoneNumber do
|
5
|
+
before(:all) do
|
6
|
+
@model = DataMapper::Validate::Fixtures::PhoneNumber.new(:type_of_number => 'cell')
|
7
|
+
@model.should be_valid
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "with type of number set to 'home'" do
|
11
|
+
before :all do
|
12
|
+
@model.type_of_number = 'home'
|
13
|
+
end
|
14
|
+
|
15
|
+
it_should_behave_like "valid model"
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
describe "with type of number set to 'cell'" do
|
20
|
+
before :all do
|
21
|
+
@model.type_of_number = 'cell'
|
22
|
+
end
|
23
|
+
|
24
|
+
it_should_behave_like "valid model"
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
describe "with type of number set to 'work'" do
|
29
|
+
before :all do
|
30
|
+
@model.type_of_number = 'home'
|
31
|
+
end
|
32
|
+
|
33
|
+
it_should_behave_like "valid model"
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
describe "with type of number set to 'fax'" do
|
38
|
+
before :all do
|
39
|
+
@model.type_of_number = 'fax'
|
40
|
+
end
|
41
|
+
|
42
|
+
it_should_behave_like "invalid model"
|
43
|
+
|
44
|
+
it "has meaningful error message on invalid property" do
|
45
|
+
@model.errors.on(:type_of_number).should == [ 'Should be one of: home, cell or work' ]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
describe DataMapper::Validate::Fixtures::MathematicalFunction do
|
53
|
+
before :all do
|
54
|
+
@model = DataMapper::Validate::Fixtures::MathematicalFunction.new(:input => 2, :output => -2)
|
55
|
+
@model.should be_valid
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "with input = 0" do
|
59
|
+
before :all do
|
60
|
+
@model.input = 0
|
61
|
+
end
|
62
|
+
|
63
|
+
it_should_behave_like "invalid model"
|
64
|
+
|
65
|
+
it "notices 'greater than or equal to 1' in the error message" do
|
66
|
+
@model.errors.on(:input).should == [ 'Input must be greater than or equal to 1' ]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "with input = -10" do
|
71
|
+
before :all do
|
72
|
+
@model.input = -10
|
73
|
+
end
|
74
|
+
|
75
|
+
it_should_behave_like "invalid model"
|
76
|
+
|
77
|
+
it "notices 'greater than or equal to 1' in the error message" do
|
78
|
+
@model.errors.on(:input).should == [ 'Input must be greater than or equal to 1' ]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "with input = -Infinity" do
|
83
|
+
before :all do
|
84
|
+
@model.input = -(1.0/0)
|
85
|
+
end
|
86
|
+
|
87
|
+
it_should_behave_like "invalid model"
|
88
|
+
|
89
|
+
it "notices 'greater than or equal to 1' in the error message" do
|
90
|
+
@model.errors.on(:input).should == [ 'Input must be greater than or equal to 1' ]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "with input = 10" do
|
95
|
+
before :all do
|
96
|
+
@model.input = 10
|
97
|
+
end
|
98
|
+
|
99
|
+
it_should_behave_like "valid model"
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
describe "with input = Infinity" do
|
104
|
+
before :all do
|
105
|
+
@model.input = (1.0/0)
|
106
|
+
end
|
107
|
+
|
108
|
+
it_should_behave_like "valid model"
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
#
|
113
|
+
# Function range
|
114
|
+
#
|
115
|
+
|
116
|
+
describe "with output = 0" do
|
117
|
+
before :all do
|
118
|
+
@model.output = 0
|
119
|
+
end
|
120
|
+
|
121
|
+
it_should_behave_like "valid model"
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "with output = -10" do
|
125
|
+
before :all do
|
126
|
+
@model.output = -10
|
127
|
+
end
|
128
|
+
|
129
|
+
it_should_behave_like "valid model"
|
130
|
+
end
|
131
|
+
|
132
|
+
describe "with output = -Infinity" do
|
133
|
+
before :all do
|
134
|
+
@model.output = -(1.0/0)
|
135
|
+
end
|
136
|
+
|
137
|
+
it_should_behave_like "valid model"
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "with output = 10" do
|
141
|
+
before :all do
|
142
|
+
@model.output = 10
|
143
|
+
end
|
144
|
+
|
145
|
+
it_should_behave_like "invalid model"
|
146
|
+
|
147
|
+
it "uses overriden error message" do
|
148
|
+
@model.errors.on(:output).should == [ 'Negative values or zero only, please' ]
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
describe "with output = Infinity" do
|
154
|
+
before :all do
|
155
|
+
@model.output = (1.0/0)
|
156
|
+
end
|
157
|
+
|
158
|
+
it_should_behave_like "invalid model"
|
159
|
+
|
160
|
+
it "uses overriden error message" do
|
161
|
+
@model.errors.on(:output).should == [ 'Negative values or zero only, please' ]
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DataMapper::Resource do
|
4
|
+
before :all do
|
5
|
+
DataMapper::Validate::Fixtures::Barcode.auto_migrate!
|
6
|
+
|
7
|
+
@resource = DataMapper::Validate::Fixtures::Barcode.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#update' do
|
11
|
+
describe 'when provided valid attributes' do
|
12
|
+
before :all do
|
13
|
+
@response = @resource.update(:code => 'a' * 10)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should return true' do
|
17
|
+
@response.should be_true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'when provided invalid attributes' do
|
22
|
+
before :all do
|
23
|
+
@response = @resource.update(:code => 'a' * 11)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should return false' do
|
27
|
+
@response.should be_false
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should set errors' do
|
31
|
+
@resource.errors.to_a.should == [ [ 'Code must be at most 10 characters long' ] ]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'when provided invalid attributes and a context' do
|
36
|
+
before :all do
|
37
|
+
# remove data from previous spec runs
|
38
|
+
::DataMapper::Validate::Fixtures::Organisation.all.destroy!
|
39
|
+
::DataMapper::Validate::Fixtures::Department.all.destroy!
|
40
|
+
::DataMapper::Validate::Fixtures::User.all.destroy!
|
41
|
+
|
42
|
+
organization = DataMapper::Validate::Fixtures::Organisation.create(:name => 'Org 101', :domain => '101')
|
43
|
+
dept = DataMapper::Validate::Fixtures::Department.create(:name => 'accounting')
|
44
|
+
|
45
|
+
attributes = {
|
46
|
+
:organisation => organization,
|
47
|
+
:user_name => 'guy',
|
48
|
+
:department => dept,
|
49
|
+
}
|
50
|
+
|
51
|
+
# create a record that will be a dupe when User#update is executed below
|
52
|
+
DataMapper::Validate::Fixtures::User.create(attributes).should be_saved
|
53
|
+
|
54
|
+
@resource = DataMapper::Validate::Fixtures::User.new
|
55
|
+
|
56
|
+
@response = @resource.update(attributes, :signing_up_for_department_account)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should return false' do
|
60
|
+
@response.should be_false
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should set errors' do
|
64
|
+
@resource.errors.to_a.should == [ [ 'User name is already taken' ] ]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec.opts
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
|
-
require 'pathname'
|
2
1
|
require 'rubygems'
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
# use local dm-core if running from a typical dev checkout.
|
4
|
+
lib = File.join('..', '..', 'dm-core', 'lib')
|
5
|
+
$LOAD_PATH.unshift(lib) if File.directory?(lib)
|
6
|
+
require 'dm-core'
|
6
7
|
|
7
|
-
|
8
|
+
# Support running specs with 'rake spec' and 'spec'
|
9
|
+
$LOAD_PATH.unshift('lib') unless $LOAD_PATH.include?('lib')
|
10
|
+
|
11
|
+
require 'dm-validations'
|
8
12
|
|
9
13
|
def load_driver(name, default_uri)
|
10
14
|
return false if ENV['ADAPTER'] != name.to_s
|
@@ -24,3 +28,7 @@ ENV['ADAPTER'] ||= 'sqlite3'
|
|
24
28
|
HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
|
25
29
|
HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
|
26
30
|
HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
|
31
|
+
|
32
|
+
ROOT = Pathname(__FILE__).dirname.parent
|
33
|
+
Dir[ROOT / 'spec' / 'integration' / 'shared' / '**' / '*.rb'].each { |rb| require(rb) }
|
34
|
+
Dir[ROOT / 'spec' / 'fixtures' / '**' / '*.rb'].each { |rb| require(rb) }
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'unit/contextual_validators/spec_helper'
|
4
|
+
|
5
|
+
describe DataMapper::Validate::ContextualValidators do
|
6
|
+
before :all do
|
7
|
+
@model = DataMapper::Validate::ContextualValidators.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "initially" do
|
11
|
+
it "is empty" do
|
12
|
+
@model.should be_empty
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
describe "after first reference to context" do
|
18
|
+
before :all do
|
19
|
+
@model.context(:create)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "initializes list of validators for referred context" do
|
23
|
+
@model.context(:create).should be_empty
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
describe "after a context being added" do
|
29
|
+
before :all do
|
30
|
+
@model.context(:default) << DataMapper::Validate::RequiredFieldValidator.new(:toc, :when => [:publishing])
|
31
|
+
end
|
32
|
+
|
33
|
+
it "is no longer empty" do
|
34
|
+
@model.should_not be_empty
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
describe "when cleared" do
|
40
|
+
before :all do
|
41
|
+
@model.context(:default) << DataMapper::Validate::RequiredFieldValidator.new(:toc, :when => [:publishing])
|
42
|
+
@model.should_not be_empty
|
43
|
+
@model.clear!
|
44
|
+
end
|
45
|
+
|
46
|
+
it "becomes empty again" do
|
47
|
+
@model.should be_empty
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'unit/contextual_validators/spec_helper'
|
4
|
+
|
5
|
+
describe DataMapper::Validate::ContextualValidators do
|
6
|
+
before :all do
|
7
|
+
@model = DataMapper::Validate::ContextualValidators.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#execute(name, target)" do
|
11
|
+
before :each do
|
12
|
+
@validator_one = DataMapper::Validate::RequiredFieldValidator.new(:name)
|
13
|
+
@validator_two = DataMapper::Validate::WithinValidator.new(:operating_system, :set => ["Mac OS X", "Linux", "FreeBSD", "Solaris"])
|
14
|
+
|
15
|
+
@model.context(:default) << @validator_one << @validator_two
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "when context was never referred to before" do
|
19
|
+
it "raises ArgumentError" do
|
20
|
+
lambda { @model.execute(:some_unknown_context, Object.new) }.
|
21
|
+
should raise_error(ArgumentError, /validation context some_unknown_context doesn't seem to be defined/)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
describe "when target satisfies all validators" do
|
27
|
+
before :each do
|
28
|
+
@target = ::DataMapper::Validate::Fixtures::PieceOfSoftware.new(:name => 'gcc', :operating_system => "Mac OS X")
|
29
|
+
@validator_one.call(@target).should be_true
|
30
|
+
@validator_two.call(@target).should be_true
|
31
|
+
|
32
|
+
@result = @model.execute(:default, @target)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns true" do
|
36
|
+
@result.should be_true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
describe "when target does not satisfy all validators" do
|
42
|
+
before :each do
|
43
|
+
@target = ::DataMapper::Validate::Fixtures::PieceOfSoftware.new(:name => 'Skitch', :operating_system => "Haiku")
|
44
|
+
@validator_one.call(@target).should be_true
|
45
|
+
@validator_two.call(@target).should be_false
|
46
|
+
|
47
|
+
@result = @model.execute(:default, @target)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns true" do
|
51
|
+
@result.should be_false
|
52
|
+
end
|
53
|
+
end # describe "when target does not satisfy all validators"
|
54
|
+
end # describe "#execute(name, target)"
|
55
|
+
end # describe DataMapper::Validate::ContextualValidators
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module DataMapper
|
4
|
+
module Validate
|
5
|
+
module Fixtures
|
6
|
+
|
7
|
+
class PieceOfSoftware
|
8
|
+
#
|
9
|
+
# Behaviors
|
10
|
+
#
|
11
|
+
|
12
|
+
include DataMapper::Validate
|
13
|
+
|
14
|
+
#
|
15
|
+
# Attributes
|
16
|
+
#
|
17
|
+
|
18
|
+
attr_accessor :name, :operating_system
|
19
|
+
|
20
|
+
#
|
21
|
+
# Validations
|
22
|
+
#
|
23
|
+
|
24
|
+
#
|
25
|
+
# API
|
26
|
+
#
|
27
|
+
|
28
|
+
def initialize(attributes = {})
|
29
|
+
attributes.each do |key, value|
|
30
|
+
self.send("#{key}=", value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end # Fixtures
|
36
|
+
end # Validate
|
37
|
+
end # DataMapper
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DataMapper::Validate::GenericValidator do
|
4
|
+
describe "when types and fields are equal" do
|
5
|
+
it "returns true" do
|
6
|
+
DataMapper::Validate::RequiredFieldValidator.new(:name).
|
7
|
+
should == DataMapper::Validate::RequiredFieldValidator.new(:name)
|
8
|
+
end
|
9
|
+
end # describe "#==" do
|
10
|
+
|
11
|
+
|
12
|
+
describe "when types differ" do
|
13
|
+
it "returns false" do
|
14
|
+
DataMapper::Validate::RequiredFieldValidator.new(:name).
|
15
|
+
should_not == DataMapper::Validate::UniquenessValidator.new(:name)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
describe "when property names differ" do
|
21
|
+
it "returns false" do
|
22
|
+
DataMapper::Validate::RequiredFieldValidator.new(:first_name).
|
23
|
+
should_not == DataMapper::Validate::RequiredFieldValidator.new(:last_name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end # describe DataMapper::Validate::GenericValidator
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Validate::ValidationErrors do
|
5
|
+
before :all do
|
6
|
+
@model = DataMapper::Validate::ValidationErrors.new(Object.new)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "after first error being added" do
|
10
|
+
before :all do
|
11
|
+
@model.add(:property, "can't be valid, no way")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "is no longer empty" do
|
15
|
+
@model.should_not be_empty
|
16
|
+
end
|
17
|
+
|
18
|
+
it "adds error message to list of errors for given property name" do
|
19
|
+
@model.on(:property).should == ["can't be valid, no way"]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
describe "after second error being added" do
|
25
|
+
before :all do
|
26
|
+
@model.add(:property, "can't be valid, no way")
|
27
|
+
@model.add(:property, "something else is wrong")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "is no longer empty" do
|
31
|
+
@model.should_not be_empty
|
32
|
+
end
|
33
|
+
|
34
|
+
it "appends error message to list of errors for given property name" do
|
35
|
+
@model.on(:property).should == ["can't be valid, no way", "something else is wrong"]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
describe "when duplicate error being added" do
|
41
|
+
before :all do
|
42
|
+
@model.add(:property, "can't be valid, no way")
|
43
|
+
@model.add(:property, "can't be valid, no way")
|
44
|
+
end
|
45
|
+
|
46
|
+
it "is no longer empty" do
|
47
|
+
@model.should_not be_empty
|
48
|
+
end
|
49
|
+
|
50
|
+
it "DOES NOT allow duplication" do
|
51
|
+
@model.on(:property).should == ["can't be valid, no way"]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|