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.
Files changed (159) hide show
  1. data/{History.txt → History.rdoc} +10 -3
  2. data/LICENSE +1 -0
  3. data/Manifest.txt +103 -33
  4. data/README.rdoc +172 -0
  5. data/Rakefile +4 -5
  6. data/lib/dm-validations.rb +73 -75
  7. data/lib/dm-validations/auto_validate.rb +98 -56
  8. data/lib/dm-validations/contextual_validators.rb +37 -14
  9. data/lib/dm-validations/exceptions.rb +3 -0
  10. data/lib/dm-validations/formats/url.rb +3 -1
  11. data/lib/dm-validations/support/context.rb +54 -0
  12. data/lib/dm-validations/validation_errors.rb +51 -27
  13. data/lib/dm-validations/{absent_field_validator.rb → validators/absent_field_validator.rb} +2 -8
  14. data/lib/dm-validations/{acceptance_validator.rb → validators/acceptance_validator.rb} +7 -7
  15. data/lib/dm-validations/{block_validator.rb → validators/block_validator.rb} +0 -0
  16. data/lib/dm-validations/{confirmation_validator.rb → validators/confirmation_validator.rb} +7 -7
  17. data/lib/dm-validations/{format_validator.rb → validators/format_validator.rb} +10 -16
  18. data/lib/dm-validations/{generic_validator.rb → validators/generic_validator.rb} +28 -6
  19. data/lib/dm-validations/validators/length_validator.rb +214 -0
  20. data/lib/dm-validations/validators/method_validator.rb +68 -0
  21. data/lib/dm-validations/validators/numeric_validator.rb +171 -0
  22. data/lib/dm-validations/{primitive_validator.rb → validators/primitive_validator.rb} +2 -7
  23. data/lib/dm-validations/{required_field_validator.rb → validators/required_field_validator.rb} +0 -6
  24. data/lib/dm-validations/{uniqueness_validator.rb → validators/uniqueness_validator.rb} +9 -20
  25. data/lib/dm-validations/{within_validator.rb → validators/within_validator.rb} +4 -4
  26. data/lib/dm-validations/version.rb +1 -1
  27. data/spec/fixtures/barcode.rb +32 -0
  28. data/spec/fixtures/basketball_court.rb +58 -0
  29. data/spec/fixtures/basketball_player.rb +37 -0
  30. data/spec/fixtures/beta_tester_account.rb +33 -0
  31. data/spec/fixtures/bill_of_landing.rb +42 -0
  32. data/spec/fixtures/boat_dock.rb +26 -0
  33. data/spec/fixtures/city.rb +25 -0
  34. data/spec/fixtures/company.rb +95 -0
  35. data/spec/fixtures/corporate_world.rb +47 -0
  36. data/spec/fixtures/country.rb +25 -0
  37. data/spec/fixtures/currency.rb +42 -0
  38. data/spec/fixtures/ethernet_frame.rb +56 -0
  39. data/spec/fixtures/event.rb +44 -0
  40. data/spec/fixtures/g3_concert.rb +57 -0
  41. data/spec/fixtures/jabberwock.rb +27 -0
  42. data/spec/fixtures/kayak.rb +28 -0
  43. data/spec/fixtures/lernean_hydra.rb +39 -0
  44. data/spec/fixtures/mathematical_function.rb +34 -0
  45. data/spec/fixtures/memory_object.rb +30 -0
  46. data/spec/fixtures/mittelschnauzer.rb +35 -0
  47. data/spec/fixtures/motor_launch.rb +21 -0
  48. data/spec/fixtures/page.rb +32 -0
  49. data/spec/fixtures/phone_number.rb +28 -0
  50. data/spec/fixtures/pirogue.rb +28 -0
  51. data/spec/fixtures/programming_language.rb +83 -0
  52. data/spec/fixtures/reservation.rb +38 -0
  53. data/spec/fixtures/scm_operation.rb +67 -0
  54. data/spec/fixtures/sms_message.rb +22 -0
  55. data/spec/fixtures/udp_packet.rb +49 -0
  56. data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +86 -0
  57. data/spec/integration/absent_field_validator/spec_helper.rb +7 -0
  58. data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +196 -0
  59. data/spec/integration/acceptance_validator/spec_helper.rb +7 -0
  60. data/spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +47 -0
  61. data/spec/integration/automatic_validation/disabling_inferred_validation_spec.rb +41 -0
  62. data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +104 -0
  63. data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +35 -0
  64. data/spec/integration/automatic_validation/inferred_format_validation_spec.rb +33 -0
  65. data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +73 -0
  66. data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +121 -0
  67. data/spec/integration/automatic_validation/inferred_presence_validation_spec.rb +41 -0
  68. data/spec/integration/automatic_validation/inferred_primitive_validation_spec.rb +20 -0
  69. data/spec/integration/automatic_validation/inferred_within_validation_spec.rb +33 -0
  70. data/spec/integration/automatic_validation/spec_helper.rb +74 -0
  71. data/spec/integration/block_validator/block_validator_spec.rb +32 -0
  72. data/spec/integration/block_validator/spec_helper.rb +5 -0
  73. data/spec/integration/conditional_validation/if_condition_spec.rb +61 -0
  74. data/spec/integration/conditional_validation/spec_helper.rb +5 -0
  75. data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +74 -0
  76. data/spec/integration/confirmation_validator/spec_helper.rb +5 -0
  77. data/spec/integration/datamapper_models/association_validation_spec.rb +23 -0
  78. data/spec/integration/datamapper_models/inheritance_spec.rb +78 -0
  79. data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +22 -0
  80. data/spec/integration/duplicated_validations/spec_helper.rb +5 -0
  81. data/spec/integration/format_validator/email_format_validator_spec.rb +113 -0
  82. data/spec/integration/format_validator/format_validator_spec.rb +60 -0
  83. data/spec/integration/format_validator/regexp_validator_spec.rb +29 -0
  84. data/spec/integration/format_validator/spec_helper.rb +5 -0
  85. data/spec/integration/format_validator/url_format_validator_spec.rb +63 -0
  86. data/spec/integration/length_validator/default_value_spec.rb +12 -0
  87. data/spec/integration/length_validator/equality_spec.rb +79 -0
  88. data/spec/integration/length_validator/error_message_spec.rb +15 -18
  89. data/spec/integration/length_validator/maximum_spec.rb +40 -24
  90. data/spec/integration/length_validator/minimum_spec.rb +32 -21
  91. data/spec/integration/length_validator/range_spec.rb +50 -60
  92. data/spec/integration/length_validator/spec_helper.rb +7 -12
  93. data/spec/integration/method_validator/method_validator_spec.rb +239 -0
  94. data/spec/integration/method_validator/spec_helper.rb +5 -0
  95. data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +61 -0
  96. data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +37 -0
  97. data/spec/integration/numeric_validator/float_type_spec.rb +42 -56
  98. data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +35 -0
  99. data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +35 -0
  100. data/spec/integration/numeric_validator/integer_only_true_spec.rb +3 -6
  101. data/spec/integration/numeric_validator/integer_type_spec.rb +8 -24
  102. data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +35 -0
  103. data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +35 -0
  104. data/spec/integration/numeric_validator/spec_helper.rb +5 -77
  105. data/spec/integration/primitive_validator/primitive_validator_spec.rb +90 -0
  106. data/spec/integration/primitive_validator/spec_helper.rb +5 -0
  107. data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +118 -0
  108. data/spec/integration/required_field_validator/association_spec.rb +5 -8
  109. data/spec/integration/required_field_validator/boolean_type_value_spec.rb +4 -7
  110. data/spec/integration/required_field_validator/date_type_value_spec.rb +3 -6
  111. data/spec/integration/required_field_validator/datetime_type_value_spec.rb +3 -6
  112. data/spec/integration/required_field_validator/float_type_value_spec.rb +3 -6
  113. data/spec/integration/required_field_validator/integer_type_value_spec.rb +2 -5
  114. data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +5 -8
  115. data/spec/integration/required_field_validator/shared_examples.rb +1 -1
  116. data/spec/integration/required_field_validator/spec_helper.rb +6 -67
  117. data/spec/integration/required_field_validator/string_type_value_spec.rb +2 -5
  118. data/spec/integration/required_field_validator/text_type_value_spec.rb +2 -5
  119. data/spec/integration/shared/default_validation_context.rb +13 -0
  120. data/spec/integration/shared/valid_and_invalid_model.rb +27 -0
  121. data/spec/integration/uniqueness_validator/spec_helper.rb +5 -0
  122. data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +114 -0
  123. data/spec/integration/within_validator/spec_helper.rb +5 -0
  124. data/spec/integration/within_validator/within_validator_spec.rb +164 -0
  125. data/spec/public/resource_spec.rb +68 -0
  126. data/spec/spec.opts +1 -0
  127. data/spec/spec_helper.rb +12 -4
  128. data/spec/unit/contextual_validators/emptiness_spec.rb +50 -0
  129. data/spec/unit/contextual_validators/execution_spec.rb +55 -0
  130. data/spec/unit/contextual_validators/spec_helper.rb +37 -0
  131. data/spec/unit/generic_validator/equality_operator_spec.rb +26 -0
  132. data/spec/unit/validation_errors/adding_spec.rb +54 -0
  133. data/spec/unit/validation_errors/emptiness_spec.rb +38 -0
  134. data/spec/unit/validation_errors/enumerable_spec.rb +32 -0
  135. data/tasks/install.rb +1 -1
  136. data/tasks/spec.rb +4 -4
  137. metadata +118 -55
  138. data/README.txt +0 -72
  139. data/lib/dm-validations/custom_validator.rb +0 -72
  140. data/lib/dm-validations/length_validator.rb +0 -113
  141. data/lib/dm-validations/method_validator.rb +0 -68
  142. data/lib/dm-validations/numeric_validator.rb +0 -83
  143. data/spec/integration/absent_field_validator_spec.rb +0 -36
  144. data/spec/integration/acceptance_validator_spec.rb +0 -87
  145. data/spec/integration/auto_validate_spec.rb +0 -342
  146. data/spec/integration/block_validator_spec.rb +0 -30
  147. data/spec/integration/confirmation_validator_spec.rb +0 -105
  148. data/spec/integration/contextual_validators_spec.rb +0 -27
  149. data/spec/integration/custom_validator_spec.rb +0 -9
  150. data/spec/integration/format_validator_spec.rb +0 -193
  151. data/spec/integration/generic_validator_spec.rb +0 -17
  152. data/spec/integration/length_validator/valid_objects_spec.rb +0 -13
  153. data/spec/integration/method_validator_spec.rb +0 -58
  154. data/spec/integration/numeric_validator_spec.rb +0 -253
  155. data/spec/integration/primitive_validator_spec.rb +0 -30
  156. data/spec/integration/uniqueness_validator_spec.rb +0 -97
  157. data/spec/integration/validation_errors_spec.rb +0 -18
  158. data/spec/integration/validation_spec.rb +0 -404
  159. data/spec/integration/within_validator_spec.rb +0 -79
@@ -1,253 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- class Bill # :nodoc:
5
- include DataMapper::Resource
6
- property :id, Integer, :serial => true
7
- property :amount_1, String, :auto_validation => false
8
- property :amount_2, Float, :auto_validation => false
9
- validates_is_number :amount_1, :amount_2
10
- end
11
-
12
- class Hillary # :nodoc:
13
- include DataMapper::Resource
14
- property :id, Integer, :serial => true
15
- property :amount_1, Float, :auto_validation => false, :default => 0.01
16
- validates_is_number :amount_1
17
- end
18
-
19
- describe DataMapper::Validate::NumericValidator do
20
- it "should validate a floating point value on the instance of a resource" do
21
- b = Bill.new
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')
25
- b.amount_1 = 'ABC'
26
- b.amount_2 = 27.343
27
- b.should_not be_valid
28
- b.errors.on(:amount_1).should include('Amount 1 must be a number')
29
- b.amount_1 = '34.33'
30
- b.should be_valid
31
- end
32
-
33
- it "should validate an integer value on the instance of a resource" do
34
- class ::Bill
35
- property :quantity_1, String, :auto_validation => false
36
- property :quantity_2, Integer, :auto_validation => false
37
-
38
- validators.clear!
39
- validates_is_number :quantity_1, :quantity_2, :integer_only => true
40
- end
41
- b = Bill.new
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')
45
- b.quantity_1 = '12.334'
46
- b.quantity_2 = 27.343
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
55
- b.quantity_1 = '34.33'
56
- b.quantity_2 = 22
57
- b.valid?.should_not == true
58
- b.errors.on(:quantity_1).should include('Quantity 1 must be an integer')
59
- b.quantity_1 = '34'
60
- b.valid?.should == true
61
-
62
- end
63
-
64
- it "should validate if a default fufills the requirements" do
65
- h = Hillary.new
66
- h.should be_valid
67
- end
68
-
69
- describe 'auto validation' do
70
- before :all do
71
- class ::Fish
72
- include DataMapper::Resource
73
- property :id, Integer, :serial => true
74
- property :scales, Integer
75
- end
76
- end
77
-
78
- describe 'Float' do
79
- describe 'with default precision and scale' do
80
- before :all do
81
- class ::CloudFish < Fish
82
- property :average_weight, Float
83
- end
84
- end
85
-
86
- before do
87
- @cloud_fish = CloudFish.new
88
- end
89
-
90
- it 'should allow up to 10 digits before the decimal' do
91
- @cloud_fish.average_weight = 0
92
- @cloud_fish.should be_valid
93
-
94
- @cloud_fish.average_weight = 9_999_999_999
95
- @cloud_fish.should be_valid
96
-
97
- @cloud_fish.average_weight = 10_000_000_000
98
- @cloud_fish.should_not be_valid
99
- end
100
-
101
- it 'should allow 0 digits after the decimal' do
102
- @cloud_fish.average_weight = 0
103
- @cloud_fish.should be_valid
104
- end
105
-
106
- it 'should allow any digits after the decimal' do
107
- @cloud_fish.average_weight = 1.2
108
- @cloud_fish.should be_valid
109
-
110
- @cloud_fish.average_weight = 123.456
111
- @cloud_fish.should be_valid
112
- end
113
-
114
- it "should only allow up to 10 digits overall" do
115
- @cloud_fish.average_weight = 1.234567890
116
- @cloud_fish.should be_valid
117
-
118
- @cloud_fish.average_weight = 1.2345678901
119
- @cloud_fish.should_not be_valid
120
- end
121
- end
122
-
123
- describe 'with default precision and scaleof 0' do
124
- before :all do
125
- class ::RobotFish < Fish
126
- property :average_weight, Float, :scale => 0
127
- end
128
- end
129
-
130
- before do
131
- @robot_fish = RobotFish.new
132
- end
133
-
134
- it 'should allow up to 10 digits before the decimal' do
135
- @robot_fish.average_weight = 0
136
- @robot_fish.should be_valid
137
-
138
- @robot_fish.average_weight = 9_999_999_999
139
- @robot_fish.should be_valid
140
-
141
- @robot_fish.average_weight = 10_000_000_000
142
- @robot_fish.should_not be_valid
143
- end
144
-
145
- it 'should allow 0 digits after the decimal' do
146
- @robot_fish.average_weight = 0
147
- @robot_fish.should be_valid
148
- end
149
-
150
- it 'should allow 1 digit after the decimal if it is a zero' do
151
- @robot_fish.average_weight = 0.0
152
- @robot_fish.should be_valid
153
-
154
- @robot_fish.average_weight = 9_999_999_999.0
155
- @robot_fish.should be_valid
156
-
157
- @robot_fish.average_weight = 0.1
158
- @robot_fish.should_not be_valid
159
- end
160
- end
161
-
162
- describe 'with a precision of 4 and a scale of 2' do
163
- before :all do
164
- class ::GoldFish < Fish
165
- property :average_weight, Float, :precision => 4, :scale => 2
166
- end
167
- end
168
-
169
- before do
170
- @gold_fish = GoldFish.new
171
- end
172
-
173
- it "should have scale of 2" do
174
- @gold_fish.model.average_weight.scale.should == 2
175
- end
176
-
177
- it 'should allow up to 2 digits before the decimal' do
178
- @gold_fish.average_weight = 0
179
- @gold_fish.should be_valid
180
-
181
- @gold_fish.average_weight = 99
182
- @gold_fish.should be_valid
183
-
184
- @gold_fish.average_weight = -99
185
- @gold_fish.should be_valid
186
-
187
- @gold_fish.average_weight = 100
188
- @gold_fish.should_not be_valid
189
-
190
- @gold_fish.average_weight = -100
191
- @gold_fish.should_not be_valid
192
- end
193
-
194
- it 'should allow 2 digits after the decimal' do
195
- @gold_fish.average_weight = 99.99
196
- @gold_fish.should be_valid
197
-
198
- @gold_fish.average_weight = -99.99
199
- @gold_fish.should be_valid
200
-
201
- @gold_fish.average_weight = 99.999
202
- @gold_fish.should_not be_valid
203
-
204
- @gold_fish.average_weight = -99.999
205
- @gold_fish.should_not be_valid
206
- end
207
- end
208
-
209
- describe 'with a precision of 2 and a scale of 2' do
210
- before :all do
211
- class ::SilverFish < Fish
212
- property :average_weight, Float, :precision => 2, :scale => 2
213
- end
214
- end
215
-
216
- before do
217
- @silver_fish = SilverFish.new
218
- end
219
-
220
- it 'should allow a 0 before the decimal' do
221
- @silver_fish.average_weight = 0
222
- @silver_fish.should be_valid
223
-
224
- @silver_fish.average_weight = 0.1
225
- @silver_fish.should be_valid
226
-
227
- @silver_fish.average_weight = -0.1
228
- @silver_fish.should be_valid
229
-
230
- @silver_fish.average_weight = 1
231
- @silver_fish.should_not be_valid
232
-
233
- @silver_fish.average_weight = -1
234
- @silver_fish.should_not be_valid
235
- end
236
-
237
- it 'should allow 2 digits after the decimal' do
238
- @silver_fish.average_weight = 0.99
239
- @silver_fish.should be_valid
240
-
241
- @silver_fish.average_weight = -0.99
242
- @silver_fish.should be_valid
243
-
244
- @silver_fish.average_weight = 0.999
245
- @silver_fish.should_not be_valid
246
-
247
- @silver_fish.average_weight = -0.999
248
- @silver_fish.should_not be_valid
249
- end
250
- end
251
- end
252
- end
253
- end
@@ -1,30 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- class Monica # :nodoc:
5
- include DataMapper::Resource
6
- property :id, Integer, :serial => true
7
- property :birth_date, Date, :auto_validation => false
8
- property :happy, Boolean
9
- validates_is_primitive :birth_date
10
- end
11
-
12
- describe DataMapper::Validate::PrimitiveValidator do
13
- it "should validate a property to check for the type" do
14
- b = Monica.new
15
- b.should be_valid
16
-
17
- b.birth_date = 'ABC'
18
- b.should_not be_valid
19
- b.errors.on(:birth_date).should include('Birth date must be of type Date')
20
- b.birth_date.should eql('ABC')
21
- b.birth_date = '2008-01-01'
22
- b.should be_valid
23
- b.birth_date.should eql(Date.civil(2008, 1, 1))
24
- end
25
- it "should accept FalseClass even when the property type is TrueClass" do
26
- b = Monica.new
27
- b.happy = false
28
- b.valid?.should == true
29
- end
30
- end
@@ -1,97 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
5
- describe DataMapper::Validate::UniquenessValidator do
6
-
7
- before do
8
- class ::Organisation
9
- include DataMapper::Resource
10
- property :id, Serial
11
- property :name, String
12
- property :domain, String #, :unique => true
13
-
14
- validates_is_unique :domain, :allow_nil => true
15
- end
16
-
17
- class ::User
18
- include DataMapper::Resource
19
- property :id, Serial
20
- property :organisation_id, Integer
21
- property :user_name, String
22
-
23
- belongs_to :organisation #has :organisation, n..1
24
-
25
- validates_is_unique :user_name, :when => :testing_association, :scope => [:organisation]
26
- validates_is_unique :user_name, :when => :testing_property, :scope => [:organisation_id]
27
- end
28
-
29
- Organisation.auto_migrate!
30
- User.auto_migrate!
31
-
32
- repository do
33
- Organisation.new(:id=>1, :name=>'Org One', :domain=>'taken').save
34
- Organisation.new(:id=>2, :name=>'Org Two', :domain=>'two').save
35
-
36
- User.new(:id=>1, :organisation_id=>1, :user_name=>'guy').save
37
- end
38
- end
39
-
40
- it 'should validate the uniqueness of a value on a resource' do
41
- repository do
42
- o = Organisation.get!(1)
43
- o.should be_valid
44
-
45
- o = Organisation.new(:id=>20, :name=>"Org Twenty", :domain=>nil)
46
- o.should be_valid
47
- o.save
48
-
49
- o = Organisation.new(:id=>30, :name=>"Org Thirty", :domain=>nil)
50
- o.should be_valid
51
- end
52
- end
53
-
54
- it "should not even check if :allow_nil is true" do
55
- repository do
56
- o = Organisation.get!(1)
57
- o.should be_valid
58
-
59
- o = Organisation.new(:id=>2, :name=>"Org Two", :domain=>"taken")
60
- o.should_not be_valid
61
- o.errors.on(:domain).should include('Domain is already taken')
62
-
63
- o = Organisation.new(:id=>2, :name=>"Org Two", :domain=>"not_taken")
64
- o.should be_valid
65
- end
66
- end
67
-
68
- it 'should validate uniqueness on a string key' do
69
- class ::Department
70
- include DataMapper::Resource
71
- property :name, String, :key => true
72
-
73
- validates_is_unique :name
74
- auto_migrate!
75
- end
76
-
77
- hr = Department.create(:name => "HR")
78
- hr2 = Department.new(:name => "HR")
79
- hr2.valid?.should == false
80
- end
81
-
82
- it 'should validate the uniqueness of a value with scope' do
83
- repository do
84
- u = User.new(:id => 2, :organisation_id=>1, :user_name => 'guy')
85
- u.should_not be_valid_for_testing_property
86
- u.errors.on(:user_name).should include('User name is already taken')
87
- u.should_not be_valid_for_testing_association
88
- u.errors.on(:user_name).should include('User name is already taken')
89
-
90
-
91
- u = User.new(:id => 2, :organisation_id => 2, :user_name => 'guy')
92
- u.should be_valid_for_testing_property
93
- u.should be_valid_for_testing_association
94
- end
95
- end
96
- end
97
- end
@@ -1,18 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- describe DataMapper::Validate::ValidationErrors do
5
-
6
- before(:each) do
7
- @errors = DataMapper::Validate::ValidationErrors.new
8
- end
9
-
10
- it "should report that it is empty on first creation" do
11
- @errors.empty?.should == true
12
- end
13
-
14
- it "should continue to report that it is empty even after being checked" do
15
- @errors.on(:foo)
16
- @errors.empty?.should == true
17
- end
18
- end
@@ -1,404 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- describe DataMapper::Validate do
5
- before :all do
6
- class ::Yacht
7
- include DataMapper::Resource
8
- property :id, Integer, :serial => true
9
- property :name, String, :auto_validation => false
10
-
11
- validates_present :name
12
- end
13
- end
14
-
15
- describe '#validations' do
16
- it 'should support more different validations of a different type' do
17
- number_of_validators_before = Yacht.validators.contexts[:default].length
18
- class ::Yacht
19
- validates_is_unique :name
20
- end
21
- number_of_validators_after = Yacht.validators.contexts[:default].length
22
- (number_of_validators_after - number_of_validators_before).should == 1
23
- end
24
-
25
- end
26
-
27
- it 'should respond to save' do
28
- Yacht.new.should respond_to(:save)
29
- end
30
-
31
- describe '#save' do
32
- before do
33
- Yacht.auto_migrate!
34
- @yacht = Yacht.new :name => 'The Gertrude'
35
- end
36
-
37
- describe 'without context specified' do
38
- it 'should validate using the default context' do
39
- @yacht.should_receive(:valid?).with(:default)
40
- @yacht.save
41
- end
42
-
43
- it 'should save if the object is valid for the default context' do
44
- @yacht.should be_valid
45
- @yacht.save.should be_true
46
- @yacht.should_not be_new_record
47
- end
48
-
49
- it 'should not save if the object is not valid for the default context' do
50
- @yacht.name = 'a'
51
- @yacht.should be_valid
52
-
53
- @yacht.name = nil
54
- @yacht.should_not be_valid
55
- @yacht.save.should be_false
56
- @yacht.should be_new_record
57
- end
58
- end
59
-
60
- describe 'with context specified' do
61
- before :all do
62
- class ::Yacht
63
- validates_length :name, :min => 2, :context => [ :strict_name ]
64
- end
65
- end
66
-
67
- it 'should validate using the specified context' do
68
- @yacht.should_receive(:valid?).with(:strict_name)
69
- @yacht.save(:strict_name)
70
- end
71
-
72
- it 'should save if the object is valid for the specified context' do
73
- @yacht.should be_valid(:strict_name)
74
- @yacht.save(:strict_name).should be_true
75
- @yacht.should_not be_new_record
76
- end
77
-
78
- it 'should not save if the object is not valid for the specified context' do
79
- @yacht.name = 'aa'
80
- @yacht.should be_valid(:strict_name)
81
-
82
- @yacht.name = 'a'
83
- @yacht.should_not be_valid(:strict_name)
84
- @yacht.save(:strict_name).should be_false
85
- @yacht.should be_new_record
86
- end
87
- end
88
- end
89
-
90
- describe '#save!' do
91
- before do
92
- Yacht.auto_migrate!
93
- @yacht = Yacht.new
94
- end
95
-
96
- it "should save object without running validations" do
97
- @yacht.should_not_receive(:valid?)
98
- @yacht.save!
99
- @yacht.should_not be_new_record
100
- end
101
- end
102
-
103
- describe '#create!' do
104
- before do
105
- Yacht.auto_migrate!
106
- end
107
-
108
- it "should save object without running validations" do
109
- Yacht.create!.should be_a_kind_of(Yacht)
110
- end
111
- end
112
-
113
- describe "#create" do
114
- before do
115
- Yacht.auto_migrate!
116
- end
117
-
118
- it "should run validations" do
119
- Yacht.create.new_record?.should be_true
120
- end
121
- end
122
-
123
- it "should respond to validatable? (for recursing associations)" do
124
- Yacht.new.should be_validatable
125
- Class.new.new.should_not be_validatable
126
- end
127
-
128
- it "should have a set of errors on the instance of the resource" do
129
- shamrock = Yacht.new
130
- shamrock.should respond_to(:errors)
131
- end
132
-
133
- it "should have a set of contextual validations on the class of the resource" do
134
- Yacht.should respond_to(:validators)
135
- end
136
-
137
- it "should execute all validators for a given context against the resource" do
138
- Yacht.validators.should respond_to(:execute)
139
- end
140
-
141
- it "should place a validator in the :default context if a named context is not provided" do
142
- Yacht.validators.context(:default).length.should == 2
143
- end
144
-
145
- it "should allow multiple user defined contexts for a validator" do
146
- class ::Yacht
147
- property :port, String, :auto_validation => false
148
- validates_present :port, :context => [:at_sea, :in_harbor]
149
- end
150
-
151
- Yacht.validators.context(:at_sea).length.should == 1
152
- Yacht.validators.context(:in_harbor).length.should == 1
153
- Yacht.validators.context(:no_such_context).length.should == 0
154
- end
155
-
156
- it "should alias :on and :when for :context" do
157
- class ::Yacht
158
- property :owner, String, :auto_validation => false
159
- property :bosun, String, :auto_validation => false
160
-
161
- validates_present :owner, :on => :owned_vessel
162
- validates_present :bosun, :when => [:under_way]
163
- end
164
- Yacht.validators.context(:owned_vessel).length.should == 1
165
- Yacht.validators.context(:under_way).length.should == 1
166
- end
167
-
168
- it "should alias :group for :context (backward compat with Validatable??)" do
169
- class ::Yacht
170
- property :captain, String, :auto_validation => false
171
- validates_present :captain, :group => [:captained_vessel]
172
- end
173
- Yacht.validators.context(:captained_vessel).length.should == 1
174
- end
175
-
176
- it "should add a method valid_for_<context_name>? for each context" do
177
- class ::Yacht
178
- property :engine_size, String, :auto_validation => false
179
- validates_present :engine_size, :when => :power_boat
180
- end
181
-
182
- cigaret = Yacht.new
183
- cigaret.valid_for_default?.should_not == true
184
- cigaret.should respond_to(:valid_for_power_boat?)
185
- cigaret.valid_for_power_boat?.should_not == true
186
-
187
- cigaret.engine_size = '4 liter V8'
188
- cigaret.valid_for_power_boat?.should == true
189
- end
190
-
191
- it "should add a method all_valid_for_<context_name>? for each context" do
192
- class ::Yacht
193
- property :mast_height, String, :auto_validation => false
194
- validates_present :mast_height, :when => :sailing_vessel
195
- end
196
- swift = Yacht.new
197
- swift.should respond_to(:all_valid_for_sailing_vessel?)
198
- end
199
-
200
- it "should be able to translate the error message" # needs String::translations
201
-
202
- it "should be able to get the error message for a given field" do
203
- class ::Yacht
204
- property :wood_type, String, :auto_validation => false
205
- validates_present :wood_type, :on => :wooden_boats
206
- end
207
- fantasy = Yacht.new
208
- fantasy.valid_for_wooden_boats?.should == false
209
- fantasy.errors.on(:wood_type).first.should == 'Wood type must not be blank'
210
- fantasy.wood_type = 'birch'
211
- fantasy.valid_for_wooden_boats?.should == true
212
- end
213
-
214
- it "should be able to specify a custom error message" do
215
- class ::Yacht
216
- property :year_built, String, :auto_validation => false
217
- validates_present :year_built, :when => :built, :message => 'Year built is a must enter field'
218
- end
219
-
220
- sea = Yacht.new
221
- sea.valid_for_built?.should == false
222
- sea.errors.full_messages.first.should == 'Year built is a must enter field'
223
- end
224
-
225
- it "should execute a Proc when provided in an :if clause and run validation if the Proc returns true" do
226
- class ::Dingy
227
- include DataMapper::Resource
228
- property :id, Integer, :serial => true
229
- property :owner, String, :auto_validation => false
230
- validates_present :owner, :if => Proc.new{|resource| resource.owned?}
231
-
232
- def owned?
233
- false
234
- end
235
- end
236
-
237
- Dingy.new.valid?.should == true
238
-
239
- class ::Dingy
240
- def owned?
241
- true
242
- end
243
- end
244
-
245
- Dingy.new.valid?.should_not == true
246
- end
247
-
248
- it "should execute a symbol or method name provided in an :if clause and run validation if the method returns true" do
249
- class ::Dingy
250
- validators.clear!
251
- validates_present :owner, :if => :owned?
252
-
253
- def owned?
254
- false
255
- end
256
- end
257
-
258
- Dingy.new.valid?.should == true
259
-
260
- class ::Dingy
261
- def owned?
262
- true
263
- end
264
- end
265
-
266
- Dingy.new.valid?.should_not == true
267
- end
268
-
269
- it "should execute a Proc when provided in an :unless clause and not run validation if the Proc returns true" do
270
- class ::RowBoat
271
- include DataMapper::Resource
272
- property :id, Integer, :serial => true
273
- validates_present :salesman, :unless => Proc.new{|resource| resource.sold?}
274
-
275
- def sold?
276
- false
277
- end
278
- end
279
-
280
- RowBoat.new.valid?.should_not == true
281
-
282
- class ::RowBoat
283
- def sold?
284
- true
285
- end
286
- end
287
-
288
- RowBoat.new.valid?.should == true
289
- end
290
-
291
- it "should execute a symbol or method name provided in an :unless clause and not run validation if the method returns true" do
292
- class ::Dingy
293
- validators.clear!
294
- validates_present :salesman, :unless => :sold?
295
-
296
- def sold?
297
- false
298
- end
299
- end
300
-
301
- Dingy.new.valid?.should_not == true #not sold and no salesman
302
-
303
- class ::Dingy
304
- def sold?
305
- true
306
- end
307
- end
308
-
309
- Dingy.new.valid?.should == true # sold and no salesman
310
- end
311
-
312
- it "should perform automatic recursive validation #all_valid? checking all instance variables (and ivar.each items if valid)" do
313
- class ::Invoice
314
- include DataMapper::Resource
315
- property :id, Integer, :serial => true
316
- property :customer, String, :auto_validation => false
317
- validates_present :customer
318
-
319
- def line_items
320
- @line_items || @line_items = []
321
- end
322
-
323
- def comment
324
- @comment || nil
325
- end
326
-
327
- def comment=(value)
328
- @comment = value
329
- end
330
- end
331
-
332
- class ::LineItem
333
- include DataMapper::Resource
334
- property :id, Integer, :serial => true
335
- property :price, String, :auto_validation => false
336
- validates_is_number :price
337
-
338
- def initialize(price)
339
- @price = price
340
- end
341
- end
342
-
343
- class ::Comment
344
- include DataMapper::Resource
345
- property :id, Integer, :serial => true
346
- property :note, String, :auto_validation => false
347
-
348
- validates_present :note
349
- end
350
-
351
- invoice = Invoice.new(:customer => 'Billy Bob')
352
- invoice.valid?.should == true
353
-
354
- for i in 1..6 do
355
- invoice.line_items << LineItem.new(i.to_s)
356
- end
357
- invoice.line_items[1].price = 'BAD VALUE'
358
- invoice.comment = Comment.new
359
-
360
- invoice.comment.valid?.should == false
361
- invoice.line_items[1].valid?.should == false
362
-
363
- invoice.all_valid?.should == false
364
- invoice.comment.note = 'This is a note'
365
-
366
- invoice.all_valid?.should == false
367
- invoice.line_items[1].price = '23.44'
368
-
369
- invoice.all_valid?.should == true
370
- end
371
-
372
- it "should retrieve private instance variables for validation" do
373
- class ::Raft
374
- include DataMapper::Resource
375
- property :length, Integer, :accessor => :private
376
-
377
- def initialize(length)
378
- @length = length
379
- end
380
- end
381
-
382
- Raft.new(10).validation_property_value("length").should == 10
383
- end
384
-
385
- it "should duplicate validations to STI models" do
386
- class ::Company
387
- include DataMapper::Resource
388
-
389
- validates_present :title, :message => "Company name is a required field"
390
-
391
- property :id, Integer, :serial => true, :key => true
392
- property :title, String
393
- property :type, Discriminator
394
- end
395
-
396
- class ::ServiceCompany < Company
397
- end
398
-
399
- class ::ProductCompany < Company
400
- end
401
- company = ServiceCompany.new
402
- company.should_not be_valid
403
- end
404
- end