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,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'integration/numeric_validator/spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Validate::Fixtures::BasketballCourt do
|
5
|
+
before :all do
|
6
|
+
@model = DataMapper::Validate::Fixtures::BasketballCourt.valid_instance
|
7
|
+
@model.valid?
|
8
|
+
end
|
9
|
+
|
10
|
+
it_should_behave_like "valid model"
|
11
|
+
|
12
|
+
|
13
|
+
describe "with three point line distance of 7.2" do
|
14
|
+
before :all do
|
15
|
+
@model.three_point_line_distance = 7.2
|
16
|
+
@model.valid?
|
17
|
+
end
|
18
|
+
|
19
|
+
it_should_behave_like "valid model"
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
describe "with three point line distance of 3.5" do
|
24
|
+
before :all do
|
25
|
+
@model.three_point_line_distance = 3.5
|
26
|
+
@model.valid?
|
27
|
+
end
|
28
|
+
|
29
|
+
it_should_behave_like "invalid model"
|
30
|
+
|
31
|
+
it "has a meaningful error message" do
|
32
|
+
@model.errors.on(:three_point_line_distance).should == [ 'Three point line distance must be greater than 6.7' ]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'integration/numeric_validator/spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Validate::Fixtures::BasketballCourt do
|
5
|
+
before :all do
|
6
|
+
@model = DataMapper::Validate::Fixtures::BasketballCourt.valid_instance
|
7
|
+
@model.valid?
|
8
|
+
end
|
9
|
+
|
10
|
+
it_should_behave_like "valid model"
|
11
|
+
|
12
|
+
|
13
|
+
describe "with length of 15.0" do
|
14
|
+
before :all do
|
15
|
+
@model.length = 15.0
|
16
|
+
@model.valid?
|
17
|
+
end
|
18
|
+
|
19
|
+
it_should_behave_like "valid model"
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
describe "with length of 14.0" do
|
24
|
+
before :all do
|
25
|
+
@model.length = 14.0
|
26
|
+
@model.valid?
|
27
|
+
end
|
28
|
+
|
29
|
+
it_should_behave_like "invalid model"
|
30
|
+
|
31
|
+
it "has a meaningful error message" do
|
32
|
+
@model.errors.on(:length).should == [ 'Length must be greater than or equal to 15.0' ]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,8 +1,5 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
|
4
|
-
require __dir__.parent.parent + 'spec_helper'
|
5
|
-
require __dir__ + 'spec_helper'
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'integration/numeric_validator/spec_helper'
|
6
3
|
|
7
4
|
describe Country do
|
8
5
|
before(:each) do
|
@@ -86,7 +83,7 @@ describe Country do
|
|
86
83
|
|
87
84
|
it "has a meaningful error message on for the property" do
|
88
85
|
@country.valid?
|
89
|
-
@country.errors.on(:area).should
|
86
|
+
@country.errors.on(:area).should == [ 'Please use integers to specify area' ]
|
90
87
|
end
|
91
88
|
end
|
92
89
|
end
|
@@ -1,8 +1,5 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
|
4
|
-
require __dir__.parent.parent + 'spec_helper'
|
5
|
-
require __dir__ + 'spec_helper'
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'integration/numeric_validator/spec_helper'
|
6
3
|
|
7
4
|
describe City do
|
8
5
|
before(:each) do
|
@@ -53,28 +50,15 @@ describe City do
|
|
53
50
|
end
|
54
51
|
|
55
52
|
|
56
|
-
describe "with foundation year as string
|
53
|
+
describe "with foundation year as string that is not an integer or float" do
|
57
54
|
before(:each) do
|
58
|
-
@
|
59
|
-
end
|
60
|
-
|
61
|
-
it "is set to nil" do
|
62
|
-
@city.founded_in.should be(nil)
|
63
|
-
end
|
55
|
+
@string = "founded-in=1603"
|
64
56
|
|
65
|
-
|
66
|
-
@city.should_not be_valid
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
|
71
|
-
describe "with foundation year as string containing random punctuation characters" do
|
72
|
-
before(:each) do
|
73
|
-
@city.founded_in = "$$ * $?"
|
57
|
+
@city.founded_in = @string
|
74
58
|
end
|
75
59
|
|
76
|
-
it "is
|
77
|
-
@city.founded_in.should be(
|
60
|
+
it "is not altered" do
|
61
|
+
@city.founded_in.should be(@string)
|
78
62
|
end
|
79
63
|
|
80
64
|
it "IS NOT valid" do
|
@@ -94,7 +78,7 @@ describe City do
|
|
94
78
|
|
95
79
|
it "has a meaningful error message on for the property" do
|
96
80
|
@city.valid?
|
97
|
-
@city.errors.on(:founded_in).should
|
81
|
+
@city.errors.on(:founded_in).should == [ 'Foundation year must be an integer' ]
|
98
82
|
end
|
99
83
|
end
|
100
84
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'integration/numeric_validator/spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Validate::Fixtures::BasketballCourt do
|
5
|
+
before :all do
|
6
|
+
@model = DataMapper::Validate::Fixtures::BasketballCourt.valid_instance
|
7
|
+
@model.valid?
|
8
|
+
end
|
9
|
+
|
10
|
+
it_should_behave_like "valid model"
|
11
|
+
|
12
|
+
|
13
|
+
describe "with three point line distance of 6.8" do
|
14
|
+
before :all do
|
15
|
+
@model.three_point_line_distance = 6.8
|
16
|
+
@model.valid?
|
17
|
+
end
|
18
|
+
|
19
|
+
it_should_behave_like "valid model"
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
describe "with three point line distance of 10.0" do
|
24
|
+
before :all do
|
25
|
+
@model.three_point_line_distance = 10.0
|
26
|
+
@model.valid?
|
27
|
+
end
|
28
|
+
|
29
|
+
it_should_behave_like "invalid model"
|
30
|
+
|
31
|
+
it "has a meaningful error message" do
|
32
|
+
@model.errors.on(:three_point_line_distance).should == [ 'Three point line distance must be less than 7.24' ]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'integration/numeric_validator/spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Validate::Fixtures::BasketballCourt do
|
5
|
+
before :all do
|
6
|
+
@model = DataMapper::Validate::Fixtures::BasketballCourt.valid_instance
|
7
|
+
@model.valid?
|
8
|
+
end
|
9
|
+
|
10
|
+
it_should_behave_like "valid model"
|
11
|
+
|
12
|
+
|
13
|
+
describe "with length of 15.24" do
|
14
|
+
before :all do
|
15
|
+
@model.length = 15.24
|
16
|
+
@model.valid?
|
17
|
+
end
|
18
|
+
|
19
|
+
it_should_behave_like "valid model"
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
describe "with length of 20.0" do
|
24
|
+
before :all do
|
25
|
+
@model.length = 20.0
|
26
|
+
@model.valid?
|
27
|
+
end
|
28
|
+
|
29
|
+
it_should_behave_like "invalid model"
|
30
|
+
|
31
|
+
it "has a meaningful error message" do
|
32
|
+
@model.errors.on(:length).should == [ 'Length must be less than or equal to 15.24' ]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,77 +1,5 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
include DataMapper::Resource
|
8
|
-
|
9
|
-
#
|
10
|
-
# Properties
|
11
|
-
#
|
12
|
-
|
13
|
-
property :id, Serial
|
14
|
-
property :name, String
|
15
|
-
|
16
|
-
property :height, Float, :auto_validation => false
|
17
|
-
property :weight, Float, :auto_validation => false
|
18
|
-
|
19
|
-
#
|
20
|
-
# Validations
|
21
|
-
#
|
22
|
-
|
23
|
-
validates_is_number :height, :weight
|
24
|
-
end
|
25
|
-
BasketballPlayer.auto_migrate!
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
class City
|
30
|
-
#
|
31
|
-
# Behaviors
|
32
|
-
#
|
33
|
-
|
34
|
-
include DataMapper::Resource
|
35
|
-
|
36
|
-
#
|
37
|
-
# Properties
|
38
|
-
#
|
39
|
-
|
40
|
-
property :id, Serial
|
41
|
-
property :name, String
|
42
|
-
|
43
|
-
property :founded_in, Integer, :auto_validation => false
|
44
|
-
|
45
|
-
#
|
46
|
-
# Validations
|
47
|
-
#
|
48
|
-
|
49
|
-
validates_is_number :founded_in, :message => "Foundation year must be an integer"
|
50
|
-
end
|
51
|
-
City.auto_migrate!
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
class Country
|
56
|
-
#
|
57
|
-
# Behaviors
|
58
|
-
#
|
59
|
-
|
60
|
-
include DataMapper::Resource
|
61
|
-
|
62
|
-
#
|
63
|
-
# Properties
|
64
|
-
#
|
65
|
-
|
66
|
-
property :id, Serial
|
67
|
-
property :name, String
|
68
|
-
|
69
|
-
property :area, String, :integer_only => true
|
70
|
-
|
71
|
-
#
|
72
|
-
# Validations
|
73
|
-
#
|
74
|
-
|
75
|
-
validates_is_number :area, :message => "Please use integers to specify area"
|
76
|
-
end
|
77
|
-
Country.auto_migrate!
|
1
|
+
# make sure you check out spec/fixtures to see fixture models
|
2
|
+
# already written
|
3
|
+
#
|
4
|
+
# DataMapper developers feels strongly against foobars in the spec
|
5
|
+
# suite
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'integration/primitive_validator/spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Validate::Fixtures::MemoryObject do
|
5
|
+
include DataMapper::Validate::Fixtures
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
@model = DataMapper::Validate::Fixtures::MemoryObject.new
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "with color given as a string" do
|
12
|
+
before :all do
|
13
|
+
@model.color = "grey"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "is valid" do
|
17
|
+
@model.should be_valid
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
describe "with color given as an object" do
|
23
|
+
before :all do
|
24
|
+
# we have to go through the back door
|
25
|
+
# since writer= method does typecasting
|
26
|
+
# and Object is casted to String
|
27
|
+
@model.instance_variable_set(:@color, Object.new)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "is NOT valid" do
|
31
|
+
@model.should_not be_valid
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
describe "with mark flag set to true" do
|
37
|
+
before :all do
|
38
|
+
@model.marked = true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "is valid" do
|
42
|
+
@model.should be_valid
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
describe "with mark flag set to false" do
|
48
|
+
before :all do
|
49
|
+
@model.marked = false
|
50
|
+
end
|
51
|
+
|
52
|
+
it "is valid" do
|
53
|
+
@model.should be_valid
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "with mark flag set to an object" do
|
58
|
+
before :all do
|
59
|
+
# go through the back door to avoid typecasting
|
60
|
+
@model.instance_variable_set(:@marked, Object.new)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "is NOT valid" do
|
64
|
+
@model.should_not be_valid
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
describe "with color set to nil" do
|
70
|
+
before :all do
|
71
|
+
# go through the back door to avoid typecasting
|
72
|
+
@model.color = nil
|
73
|
+
end
|
74
|
+
|
75
|
+
it "is valid" do
|
76
|
+
@model.should be_valid
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
describe "with mark flag set to nil" do
|
82
|
+
before :all do
|
83
|
+
@model.marked = nil
|
84
|
+
end
|
85
|
+
|
86
|
+
it "is valid" do
|
87
|
+
@model.should be_valid
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module PureRubyObjects
|
4
|
+
# notice that it is a pure Ruby class, not a DataMapper resource
|
5
|
+
class Country
|
6
|
+
#
|
7
|
+
# Behaviors
|
8
|
+
#
|
9
|
+
|
10
|
+
include DataMapper::Validate
|
11
|
+
|
12
|
+
#
|
13
|
+
# Validations
|
14
|
+
#
|
15
|
+
|
16
|
+
validates_present :name, :when => [:default, :adding_to_encyclopedia]
|
17
|
+
validates_present :population, :when => :adding_to_encyclopedia, :message => Proc.new { |record|
|
18
|
+
"population really needs to be specified when adding %s to encyclopedia" % [record.class.name]
|
19
|
+
}
|
20
|
+
|
21
|
+
validates_length :name, :in => (4..50)
|
22
|
+
|
23
|
+
#
|
24
|
+
# API
|
25
|
+
#
|
26
|
+
|
27
|
+
attr_accessor :name, :population
|
28
|
+
|
29
|
+
def initialize(name, population = nil)
|
30
|
+
@name = name
|
31
|
+
@population = population
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe PureRubyObjects::Country do
|
37
|
+
before :each do
|
38
|
+
# Powerset says so
|
39
|
+
@model = PureRubyObjects::Country.new("Italy", 58_147_733)
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "without name" do
|
43
|
+
before :each do
|
44
|
+
@model.name = nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it_should_behave_like "object invalid in default context"
|
48
|
+
|
49
|
+
it "is not valid in encyclopedia context" do
|
50
|
+
@model.should_not be_valid(:adding_to_encyclopedia)
|
51
|
+
@model.should_not be_valid_for_adding_to_encyclopedia
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
describe "without name and without population information" do
|
57
|
+
before :each do
|
58
|
+
@model.name = nil
|
59
|
+
@model.population = nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it_should_behave_like "object invalid in default context"
|
63
|
+
|
64
|
+
it "is not valid in encyclopedia context" do
|
65
|
+
@model.should_not be_valid(:adding_to_encyclopedia)
|
66
|
+
@model.should_not be_valid_for_adding_to_encyclopedia
|
67
|
+
end
|
68
|
+
|
69
|
+
it "has a meaningful error message" do
|
70
|
+
# trigger validation => have errors on the object
|
71
|
+
@model.valid_for_adding_to_encyclopedia?
|
72
|
+
@model.errors.on(:population).should == ["population really needs to be specified when adding PureRubyObjects::Country to encyclopedia"]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
describe "with name and without population information" do
|
78
|
+
before :each do
|
79
|
+
@model.population = nil
|
80
|
+
end
|
81
|
+
|
82
|
+
it_should_behave_like "object valid in default context"
|
83
|
+
|
84
|
+
it "is not valid in encyclopedia context" do
|
85
|
+
@model.should_not be_valid(:adding_to_encyclopedia)
|
86
|
+
@model.should_not be_valid_for_adding_to_encyclopedia
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
describe "with name and population information" do
|
92
|
+
it_should_behave_like "object valid in default context"
|
93
|
+
|
94
|
+
it "is valid in encyclopedia context" do
|
95
|
+
@model.should be_valid(:adding_to_encyclopedia)
|
96
|
+
@model.should be_valid_for_adding_to_encyclopedia
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
describe "with a 2 characters long name" do
|
102
|
+
before :each do
|
103
|
+
@model.name = "It"
|
104
|
+
@model.valid?
|
105
|
+
end
|
106
|
+
|
107
|
+
it_should_behave_like "object invalid in default context"
|
108
|
+
|
109
|
+
it "has errors on name" do
|
110
|
+
@model.errors.on(:name).should_not be_blank
|
111
|
+
end
|
112
|
+
|
113
|
+
it "is valid in encyclopedia context" do
|
114
|
+
@model.should be_valid(:adding_to_encyclopedia)
|
115
|
+
@model.should be_valid_for_adding_to_encyclopedia
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|