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.
Files changed (57) hide show
  1. data/History.txt +15 -0
  2. data/Manifest.txt +21 -2
  3. data/lib/dm-validations.rb +14 -14
  4. data/lib/dm-validations/absent_field_validator.rb +3 -4
  5. data/lib/dm-validations/acceptance_validator.rb +6 -10
  6. data/lib/dm-validations/confirmation_validator.rb +5 -5
  7. data/lib/dm-validations/contextual_validators.rb +1 -1
  8. data/lib/dm-validations/custom_validator.rb +1 -1
  9. data/lib/dm-validations/format_validator.rb +6 -5
  10. data/lib/dm-validations/generic_validator.rb +7 -10
  11. data/lib/dm-validations/length_validator.rb +7 -7
  12. data/lib/dm-validations/method_validator.rb +2 -2
  13. data/lib/dm-validations/numeric_validator.rb +4 -4
  14. data/lib/dm-validations/primitive_validator.rb +4 -4
  15. data/lib/dm-validations/required_field_validator.rb +5 -5
  16. data/lib/dm-validations/uniqueness_validator.rb +2 -2
  17. data/lib/dm-validations/validation_errors.rb +34 -2
  18. data/lib/dm-validations/version.rb +1 -1
  19. data/lib/dm-validations/within_validator.rb +15 -13
  20. data/spec/integration/absent_field_validator_spec.rb +4 -2
  21. data/spec/integration/acceptance_validator_spec.rb +3 -3
  22. data/spec/integration/auto_validate_spec.rb +16 -9
  23. data/spec/integration/block_validator_spec.rb +2 -8
  24. data/spec/integration/confirmation_validator_spec.rb +11 -8
  25. data/spec/integration/contextual_validators_spec.rb +2 -1
  26. data/spec/integration/format_validator_spec.rb +1 -1
  27. data/spec/integration/length_validator/error_message_spec.rb +23 -0
  28. data/spec/integration/length_validator/maximum_spec.rb +31 -0
  29. data/spec/integration/length_validator/minimum_spec.rb +31 -0
  30. data/spec/integration/length_validator/range_spec.rb +95 -0
  31. data/spec/integration/length_validator/spec_helper.rb +12 -0
  32. data/spec/integration/length_validator/valid_objects_spec.rb +13 -0
  33. data/spec/integration/method_validator_spec.rb +3 -3
  34. data/spec/integration/numeric_validator/float_type_spec.rb +102 -0
  35. data/spec/integration/numeric_validator/integer_only_true_spec.rb +92 -0
  36. data/spec/integration/numeric_validator/integer_type_spec.rb +100 -0
  37. data/spec/integration/numeric_validator/spec_helper.rb +77 -0
  38. data/spec/integration/numeric_validator_spec.rb +19 -6
  39. data/spec/integration/primitive_validator_spec.rb +2 -1
  40. data/spec/integration/required_field_validator/association_spec.rb +98 -0
  41. data/spec/integration/required_field_validator/boolean_type_value_spec.rb +149 -0
  42. data/spec/integration/required_field_validator/date_type_value_spec.rb +126 -0
  43. data/spec/integration/required_field_validator/datetime_type_value_spec.rb +126 -0
  44. data/spec/integration/required_field_validator/float_type_value_spec.rb +130 -0
  45. data/spec/integration/required_field_validator/integer_type_value_spec.rb +98 -0
  46. data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +36 -0
  47. data/spec/integration/required_field_validator/shared_examples.rb +24 -0
  48. data/spec/integration/required_field_validator/spec_helper.rb +68 -0
  49. data/spec/integration/required_field_validator/string_type_value_spec.rb +164 -0
  50. data/spec/integration/required_field_validator/text_type_value_spec.rb +46 -0
  51. data/spec/integration/uniqueness_validator_spec.rb +10 -8
  52. data/spec/integration/validation_spec.rb +25 -25
  53. data/spec/integration/within_validator_spec.rb +36 -11
  54. data/tasks/spec.rb +1 -1
  55. metadata +24 -5
  56. data/spec/integration/length_validator_spec.rb +0 -115
  57. data/spec/integration/required_field_validator_spec.rb +0 -93
@@ -0,0 +1,77 @@
1
+ # -*- coding: utf-8 -*-
2
+ class BasketballPlayer
3
+ #
4
+ # Behaviors
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!
@@ -20,15 +20,18 @@ describe DataMapper::Validate::NumericValidator do
20
20
  it "should validate a floating point value on the instance of a resource" do
21
21
  b = Bill.new
22
22
  b.should_not be_valid
23
+ b.errors.on(:amount_1).should include('Amount 1 must be a number')
24
+ b.errors.on(:amount_2).should include('Amount 2 must be a number')
23
25
  b.amount_1 = 'ABC'
24
26
  b.amount_2 = 27.343
25
27
  b.should_not be_valid
28
+ b.errors.on(:amount_1).should include('Amount 1 must be a number')
26
29
  b.amount_1 = '34.33'
27
30
  b.should be_valid
28
31
  end
29
32
 
30
33
  it "should validate an integer value on the instance of a resource" do
31
- class Bill
34
+ class ::Bill
32
35
  property :quantity_1, String, :auto_validation => false
33
36
  property :quantity_2, Integer, :auto_validation => false
34
37
 
@@ -37,12 +40,22 @@ describe DataMapper::Validate::NumericValidator do
37
40
  end
38
41
  b = Bill.new
39
42
  b.valid?.should_not == true
43
+ b.errors.on(:quantity_1).should include('Quantity 1 must be an integer')
44
+ b.errors.on(:quantity_2).should include('Quantity 2 must be an integer')
40
45
  b.quantity_1 = '12.334'
41
46
  b.quantity_2 = 27.343
42
47
  b.valid?.should_not == true
48
+ b.errors.on(:quantity_1).should include('Quantity 1 must be an integer')
49
+ pending 'dm-core truncates float to integer' do
50
+ # FIXME: The next line should pass, but :quantity_2 has no errors. This is
51
+ # because 27.343 has been truncated to 27 by the time it reaches the
52
+ # validation. Is this a bug?
53
+ b.errors.on(:quantity_2).should include('Quantity 2 must be an integer')
54
+ end
43
55
  b.quantity_1 = '34.33'
44
56
  b.quantity_2 = 22
45
57
  b.valid?.should_not == true
58
+ b.errors.on(:quantity_1).should include('Quantity 1 must be an integer')
46
59
  b.quantity_1 = '34'
47
60
  b.valid?.should == true
48
61
 
@@ -55,7 +68,7 @@ describe DataMapper::Validate::NumericValidator do
55
68
 
56
69
  describe 'auto validation' do
57
70
  before :all do
58
- class Fish
71
+ class ::Fish
59
72
  include DataMapper::Resource
60
73
  property :id, Integer, :serial => true
61
74
  property :scales, Integer
@@ -65,7 +78,7 @@ describe DataMapper::Validate::NumericValidator do
65
78
  describe 'Float' do
66
79
  describe 'with default precision and scale' do
67
80
  before :all do
68
- class CloudFish < Fish
81
+ class ::CloudFish < Fish
69
82
  property :average_weight, Float
70
83
  end
71
84
  end
@@ -109,7 +122,7 @@ describe DataMapper::Validate::NumericValidator do
109
122
 
110
123
  describe 'with default precision and scaleof 0' do
111
124
  before :all do
112
- class RobotFish < Fish
125
+ class ::RobotFish < Fish
113
126
  property :average_weight, Float, :scale => 0
114
127
  end
115
128
  end
@@ -148,7 +161,7 @@ describe DataMapper::Validate::NumericValidator do
148
161
 
149
162
  describe 'with a precision of 4 and a scale of 2' do
150
163
  before :all do
151
- class GoldFish < Fish
164
+ class ::GoldFish < Fish
152
165
  property :average_weight, Float, :precision => 4, :scale => 2
153
166
  end
154
167
  end
@@ -195,7 +208,7 @@ describe DataMapper::Validate::NumericValidator do
195
208
 
196
209
  describe 'with a precision of 2 and a scale of 2' do
197
210
  before :all do
198
- class SilverFish < Fish
211
+ class ::SilverFish < Fish
199
212
  property :average_weight, Float, :precision => 2, :scale => 2
200
213
  end
201
214
  end
@@ -16,10 +16,11 @@ describe DataMapper::Validate::PrimitiveValidator do
16
16
 
17
17
  b.birth_date = 'ABC'
18
18
  b.should_not be_valid
19
+ b.errors.on(:birth_date).should include('Birth date must be of type Date')
19
20
  b.birth_date.should eql('ABC')
20
21
  b.birth_date = '2008-01-01'
21
22
  b.should be_valid
22
- b.birth_date.should eql(Date.civil(2008,1,1))
23
+ b.birth_date.should eql(Date.civil(2008, 1, 1))
23
24
  end
24
25
  it "should accept FalseClass even when the property type is TrueClass" do
25
26
  b = Monica.new
@@ -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
+ class Artist
9
+ #
10
+ # Behaviors
11
+ #
12
+
13
+ include DataMapper::Resource
14
+
15
+ #
16
+ # Properties
17
+ #
18
+
19
+ property :id, Integer, :serial => true
20
+ property :name, String, :auto_validation => false
21
+
22
+ #
23
+ # Associations
24
+ #
25
+
26
+ has n, :albums
27
+
28
+ #
29
+ # Validations
30
+ #
31
+
32
+ validates_present :name
33
+ end
34
+
35
+ class Album
36
+ #
37
+ # Behaviors
38
+ #
39
+
40
+ include DataMapper::Resource
41
+
42
+ #
43
+ # Properties
44
+ #
45
+
46
+ property :id, Integer, :serial => true
47
+ property :name, String, :auto_validation => false
48
+ property :artist_id, Integer, :index => :artist
49
+
50
+ #
51
+ # Associations
52
+ #
53
+
54
+ belongs_to :artist
55
+
56
+ #
57
+ # Validations
58
+ #
59
+
60
+ validates_present :name, :artist
61
+ end
62
+ Artist.auto_migrate!
63
+ Album.auto_migrate!
64
+
65
+
66
+
67
+ describe Album do
68
+ before :each do
69
+ @artist = Artist.create(:name => "Oceanlab")
70
+ @album = @artist.albums.new(:name => "Sirens of the sea")
71
+ end
72
+
73
+ describe 'with a missing artist' do
74
+ before :each do
75
+ @album.artist = nil
76
+ end
77
+
78
+ it 'is not valid' do
79
+ @album.should_not be_valid
80
+ end
81
+
82
+ it 'has a meaninful error messages on association key property' do
83
+ @album.valid?
84
+ @album.errors.on(:artist).should include("Artist must not be blank")
85
+ end
86
+ end
87
+
88
+ describe 'with specified artist and name' do
89
+ before :each do
90
+ # no op
91
+ end
92
+
93
+ it 'is valid' do
94
+ @album.should be_valid
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,149 @@
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
+ # keep in mind any ScmOperation has a default value for brand property
9
+ # so it is used
10
+ describe GitOperation do
11
+ before :each do
12
+ @operation = GitOperation.new(:network_connection => true,
13
+ :clean_working_copy => true,
14
+ :message => "I did it! I did it!! Hell yeah!!!")
15
+ end
16
+
17
+ describe "without operation name" do
18
+ before(:each) do
19
+ @operation.name = nil
20
+ end
21
+ it_should_behave_like "unnamed SCM operation"
22
+ end
23
+
24
+
25
+
26
+ describe "without network connection" do
27
+ before(:each) do
28
+ # now note that false make sense from readability
29
+ # point of view but is incorrect from validator
30
+ # point of view ;)
31
+ @operation.network_connection = nil
32
+ end
33
+
34
+ it "is valid for committing" do
35
+ @operation.should be_valid_for_committing
36
+ @operation.errors.on(:network_connection).should be_blank
37
+ end
38
+
39
+ it "is not valid for pushing" do
40
+ @operation.should_not be_valid_for_pushing
41
+ @operation.errors.on(:network_connection).
42
+ first[:pushing].should include("cannot push without network connectivity")
43
+ end
44
+
45
+ it "is not valid for pulling" do
46
+ @operation.should_not be_valid_for_pulling
47
+ @operation.errors.on(:network_connection).
48
+ first[:pulling].should include("you must have network connectivity to pull from others")
49
+ end
50
+
51
+ it "is not valid in default context" do
52
+ @operation.should_not be_valid
53
+ end
54
+ end # describe "without network connection"
55
+
56
+ describe "with a network connection" do
57
+ before(:each) do
58
+ @operation.network_connection = false
59
+ end
60
+
61
+ it "is valid for committing" do
62
+ @operation.should be_valid_for_committing
63
+ end
64
+
65
+ it "is valid for pushing" do
66
+ @operation.should be_valid_for_pushing
67
+ end
68
+
69
+ it "is valid for pulling" do
70
+ @operation.should be_valid_for_pulling
71
+ end
72
+
73
+ it "is not valid in default context" do
74
+ @operation.should_not be_valid
75
+ end
76
+ end # describe "with a network connection"
77
+
78
+
79
+ describe "WITHOUT a clean working copy" do
80
+ before(:each) do
81
+ @operation.clean_working_copy = nil
82
+ end
83
+
84
+ it "is valid for committing" do
85
+ @operation.should be_valid_for_committing
86
+ end
87
+
88
+ it "is valid for pushing" do
89
+ @operation.should be_valid_for_pushing
90
+ end
91
+
92
+ it "is not valid for pulling" do
93
+ @operation.should_not be_valid_for_pulling
94
+ end
95
+
96
+ it "is not valid in default context" do
97
+ @operation.should_not be_valid
98
+ end
99
+ end # describe "without network connection"
100
+
101
+ describe "with a clean working copy" do
102
+ before(:each) do
103
+ @operation.clean_working_copy = true
104
+ end
105
+
106
+ it "is valid for committing" do
107
+ @operation.should be_valid_for_committing
108
+ end
109
+
110
+ it "is valid for pushing" do
111
+ @operation.should be_valid_for_pushing
112
+ end
113
+
114
+ it "is valid for pulling" do
115
+ @operation.should be_valid_for_pulling
116
+ end
117
+
118
+ it "is not valid in default context" do
119
+ @operation.should_not be_valid
120
+ end
121
+ end # describe "with a network connection"
122
+ end # describe GitOperation
123
+
124
+
125
+ describe SubversionOperation do
126
+ before(:each) do
127
+ @operation = SubversionOperation.new :name => "ci", :network_connection => true,
128
+ :message => "v1.5.8", :clean_working_copy => true
129
+ end
130
+
131
+ describe "without operation name" do
132
+ before(:each) do
133
+ @operation.name = nil
134
+ end
135
+ it_should_behave_like "unnamed SCM operation"
136
+ end
137
+
138
+ describe "without network connection" do
139
+ before(:each) do
140
+ @operation.network_connection = nil
141
+ end
142
+
143
+ it "virtually useless" do
144
+ @operation.should_not be_valid_for_committing
145
+ @operation.should_not be_valid_for_log_viewing
146
+ end
147
+ end # describe "without network connection"
148
+ end
149
+ end # if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
@@ -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 Holiday
9
+ #
10
+ # Behaviors
11
+ #
12
+
13
+ include DataMapper::Resource
14
+
15
+ #
16
+ # Properties
17
+ #
18
+
19
+ property :id, Integer, :serial => true
20
+ property :on, Date, :auto_validation => false
21
+
22
+ #
23
+ # Validations
24
+ #
25
+
26
+ validates_present :on
27
+ end
28
+ Holiday.auto_migrate!
29
+
30
+
31
+ describe Holiday do
32
+ before :each do
33
+ @ny09 = Holiday.new(:on => Date.new(2008, 12, 31))
34
+ @ny09.should be_valid
35
+ end
36
+
37
+
38
+ describe "with on = nil" do
39
+ before(:each) do
40
+ @ny09.on = nil
41
+ end
42
+
43
+ it "is NOT valid" do
44
+ # nil = missing for Date value
45
+ # and Holiday only has default validation context
46
+ @ny09.should_not be_valid
47
+
48
+ # sanity check
49
+ @ny09.on = Date.new(2008, 12, 31)
50
+ @ny09.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
+ @ny09.on = 0.0
58
+ end
59
+
60
+ it "IS valid" do
61
+ # yes, presence validator does not care
62
+ @ny09.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
+ @ny09.on = 0
71
+ end
72
+
73
+ it "IS valid" do
74
+ # yes, presence validator does not care
75
+ @ny09.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
+ @ny09.on = 100
84
+ end
85
+
86
+ it "IS valid" do
87
+ @ny09.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
+ @ny09.on = 100.0
95
+ end
96
+
97
+ it "IS valid" do
98
+ @ny09.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
+ @ny09.on = -1100
107
+ end
108
+
109
+ it "IS valid" do
110
+ @ny09.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
+ @ny09.on = -1100.5
119
+ end
120
+
121
+ it "IS valid" do
122
+ @ny09.should be_valid
123
+ end
124
+ end # describe "with on = -1100.5"
125
+ end # describe Holiday
126
+ end # if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES