minitest-sequel 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +379 -133
- data/Rakefile +25 -4
- data/lib/minitest/sequel/associations.rb +183 -0
- data/lib/minitest/sequel/columns.rb +161 -0
- data/lib/minitest/sequel/validations.rb +549 -0
- data/lib/minitest/sequel/version.rb +3 -1
- data/lib/minitest/sequel.rb +20 -268
- data/minitest-sequel.gemspec +15 -9
- metadata +90 -3
@@ -0,0 +1,549 @@
|
|
1
|
+
require 'minitest/sequel'
|
2
|
+
|
3
|
+
# reopening to add validations functionality
|
4
|
+
module Minitest::Assertions
|
5
|
+
|
6
|
+
# Test for validating presence of a model attribute
|
7
|
+
#
|
8
|
+
# it { assert_validates_presence(model, :title) }
|
9
|
+
# it { model.must_validate_presence_of(:title, { message: '...' }) }
|
10
|
+
#
|
11
|
+
def assert_validates_presence(obj, attribute, opts = {}, msg = nil)
|
12
|
+
assert_validates(obj, :presence, attribute, opts, msg)
|
13
|
+
end
|
14
|
+
alias_method :assert_validates_presence_of, :assert_validates_presence
|
15
|
+
|
16
|
+
# Test for validating the length of a model's attribute.
|
17
|
+
#
|
18
|
+
# Available options:
|
19
|
+
#
|
20
|
+
# * :message - The message to use (no default, overrides :nil_message, :too_long, :too_short, and :wrong_length options if present)
|
21
|
+
# * :nil_message - The message to use use if :maximum option is used and the value is nil (default: 'is not present')
|
22
|
+
#
|
23
|
+
# * :too_long - The message to use use if it the value is too long (default: 'is too long')
|
24
|
+
#
|
25
|
+
# * :too_short - The message to use use if it the value is too short (default: 'is too short')
|
26
|
+
#
|
27
|
+
# * :wrong_length - The message to use use if it the value is not valid (default: 'is the wrong length')
|
28
|
+
#
|
29
|
+
# Size related options:
|
30
|
+
#
|
31
|
+
# * :is - The exact size required for the value to be valid (no default)
|
32
|
+
#
|
33
|
+
# * :minimum - The minimum size allowed for the value (no default)
|
34
|
+
#
|
35
|
+
# * :maximum - The maximum size allowed for the value (no default)
|
36
|
+
#
|
37
|
+
# * :within - The array/range that must include the size of the value for it to be valid (no default)
|
38
|
+
#
|
39
|
+
#
|
40
|
+
# it { assert_validates_length(model, :title, { maximum: 12 }) }
|
41
|
+
# it { model.must_validate_length_of(:title, { within: 4..12 }) }
|
42
|
+
#
|
43
|
+
def assert_validates_length(obj, attribute, opts = {}, msg = nil)
|
44
|
+
assert_validates(obj, :length, attribute, opts, msg)
|
45
|
+
end
|
46
|
+
alias_method :assert_validates_length_of, :assert_validates_length
|
47
|
+
|
48
|
+
# Test for validating the exact length of a model's attribute.
|
49
|
+
#
|
50
|
+
# it { assert_validates_exact_length(model, :title, 12, { message: '...' }) }
|
51
|
+
# it { model.must_validate_exact_length_of(:title, 12, { message: '...' }) }
|
52
|
+
#
|
53
|
+
def assert_validates_exact_length(obj, attribute, exact_length, opts = {}, msg = nil)
|
54
|
+
opts.merge!({ is: exact_length })
|
55
|
+
assert_validates(obj, :length, attribute, opts, msg)
|
56
|
+
end
|
57
|
+
alias_method :assert_validates_exact_length_of, :assert_validates_exact_length
|
58
|
+
|
59
|
+
# Test for validating the exact length of a model's attribute.
|
60
|
+
#
|
61
|
+
# it { assert_validates_length_range(model, :title, 4..12, { message: '...' }) }
|
62
|
+
# it { model.must_validate_length_range_of(:title, 4..12, { message: '...' }) }
|
63
|
+
#
|
64
|
+
def assert_validates_length_range(obj, attribute, range, opts = {}, msg = nil)
|
65
|
+
opts.merge!({ within: range })
|
66
|
+
assert_validates(obj, :length, attribute, opts, msg)
|
67
|
+
end
|
68
|
+
alias_method :assert_validates_length_range_of, :assert_validates_length_range
|
69
|
+
|
70
|
+
# Test for validating the maximum length of a model's attribute.
|
71
|
+
#
|
72
|
+
# it { assert_validates_max_length(model, :title, 12, { message: '...' }) }
|
73
|
+
# it { model.must_validate_max_length_of(:title, 12, { message: '...' }) }
|
74
|
+
#
|
75
|
+
def assert_validates_max_length(obj, attribute, max_length, opts = {}, msg = nil)
|
76
|
+
opts.merge!({ maximum: max_length })
|
77
|
+
assert_validates(obj, :length, attribute, opts, msg)
|
78
|
+
end
|
79
|
+
alias_method :assert_validates_max_length_of, :assert_validates_max_length
|
80
|
+
|
81
|
+
# Test for validating the minimum length of a model's attribute.
|
82
|
+
#
|
83
|
+
# it { assert_validates_min_length(model, :title, 12, { message: '...' }) }
|
84
|
+
# it { model.must_validate_min_length_of(:title, 12, { message: '...' }) }
|
85
|
+
#
|
86
|
+
def assert_validates_min_length(obj, attribute, min_length, opts = {}, msg = nil)
|
87
|
+
opts.merge!({ minimum: min_length })
|
88
|
+
assert_validates(obj, :length, attribute, opts, msg)
|
89
|
+
end
|
90
|
+
alias_method :assert_validates_min_length_of, :assert_validates_min_length
|
91
|
+
|
92
|
+
# Test for validating the format of a model's attribute with a regexp.
|
93
|
+
#
|
94
|
+
# it { assert_validates_format(model, :title, 12, { with: /[a-z+]/ }) }
|
95
|
+
# it { model.must_validate_format_of(:title, 12, { with: /[a-z]+/ }) }
|
96
|
+
#
|
97
|
+
def assert_validates_format(obj, attribute, opts = {}, msg = nil)
|
98
|
+
assert_validates(obj, :format, attribute, opts, msg)
|
99
|
+
end
|
100
|
+
alias_method :assert_validates_format_of, :assert_validates_format
|
101
|
+
|
102
|
+
# Test for validating that a model's attribute is within a specified range or set of values.
|
103
|
+
#
|
104
|
+
# it { assert_validates_inclusion(model, :status, { in: [:a, :b, :c] }) }
|
105
|
+
# it { model.must_validate_inclusion_of(:status, { in: [:a, :b, :c] }) }
|
106
|
+
#
|
107
|
+
def assert_validates_inclusion(obj, attribute, opts = {}, msg = nil)
|
108
|
+
assert_validates(obj, :inclusion, attribute, opts, msg)
|
109
|
+
end
|
110
|
+
alias_method :assert_validates_inclusion_of, :assert_validates_inclusion
|
111
|
+
|
112
|
+
# Test for validating that a a model's attribute is an integer.
|
113
|
+
#
|
114
|
+
# it { assert_validates_integer(model, :author_id, { message: '...' }) }
|
115
|
+
# it { model.must_validate_integer_of(:author_id, { message: '...' }) }
|
116
|
+
#
|
117
|
+
def assert_validates_integer(obj, attribute, opts = {}, msg = nil)
|
118
|
+
opts.merge!({ only_integer: true })
|
119
|
+
assert_validates(obj, :numericality, attribute, opts, msg)
|
120
|
+
end
|
121
|
+
|
122
|
+
# Test for validating that a model's attribute is numeric (number).
|
123
|
+
#
|
124
|
+
# it { assert_validates_numericality(model, :author_id, { message: '...' }) }
|
125
|
+
# it { model.must_validate_numericality_of(:author_id, { message: '...' }) }
|
126
|
+
#
|
127
|
+
def assert_validates_numericality(obj, attribute, opts = {}, msg = nil)
|
128
|
+
assert_validates(obj, :numericality, attribute, opts, msg)
|
129
|
+
end
|
130
|
+
alias_method :assert_validates_numericality_of, :assert_validates_numericality
|
131
|
+
|
132
|
+
# Test for validating that a model's attribute is unique.
|
133
|
+
#
|
134
|
+
# it { assert_validates_uniqueness(model, :urlslug, { message: '...' }) }
|
135
|
+
# it { model.must_validate_uniqueness_of(:urlslug, { message: '...' }) }
|
136
|
+
#
|
137
|
+
def assert_validates_uniqueness(obj, attribute, opts = {}, msg = nil)
|
138
|
+
assert_validates(obj, :uniqueness, attribute, opts, msg)
|
139
|
+
end
|
140
|
+
alias_method :assert_validates_uniqueness_of, :assert_validates_uniqueness
|
141
|
+
|
142
|
+
# Validates acceptance of an attribute. Just checks that the value is equal to the :accept option. This method is unique in that :allow_nil is assumed to be true instead of false.
|
143
|
+
|
144
|
+
# Test for validating the acceptance of a model's attribute.
|
145
|
+
#
|
146
|
+
# it { assert_validates_acceptance(Order.new, :toc, { message: '...' }) }
|
147
|
+
# it { model.must_validate_acceptance_of(:toc, { message: '...' }) }
|
148
|
+
#
|
149
|
+
def assert_validates_acceptance(obj, attribute, opts = {}, msg = nil)
|
150
|
+
assert_validates(obj, :acceptance, attribute, opts, msg)
|
151
|
+
end
|
152
|
+
alias_method :assert_validates_acceptance_of, :assert_validates_acceptance
|
153
|
+
|
154
|
+
# Test for validating the confirmation of a model's attribute.
|
155
|
+
#
|
156
|
+
# it { assert_validates_confirmation(User.new, :password, { message: '...' }) }
|
157
|
+
# it { User.new.must_validate_confirmation_of(:password, { message: '...' }) }
|
158
|
+
#
|
159
|
+
def assert_validates_confirmation(obj, attribute, opts = {}, msg = nil)
|
160
|
+
assert_validates(obj, :confirmation, attribute, opts, msg)
|
161
|
+
end
|
162
|
+
alias_method :assert_validates_confirmation_of, :assert_validates_confirmation
|
163
|
+
|
164
|
+
# Base test for validations of a model, used mainly as a shortcut for other assertions
|
165
|
+
def assert_validates(obj, validation_type, attribute, opts = {}, msg = nil, &blk)
|
166
|
+
msg = msg.nil? ? '' : "#{msg}\n"
|
167
|
+
err_msg = []
|
168
|
+
conf_msg = []
|
169
|
+
unless obj.respond_to?(attribute)
|
170
|
+
assert(false, "Column :#{attribute} is not defined in #{obj.class.to_s}")
|
171
|
+
end
|
172
|
+
|
173
|
+
msg << "Expected #{obj.class} to validate :#{validation_type} for :#{attribute} column"
|
174
|
+
|
175
|
+
if _validated_model?(obj)
|
176
|
+
|
177
|
+
if _validated_column?(obj, attribute)
|
178
|
+
|
179
|
+
# checks if the model column is validated by the validation type
|
180
|
+
if _validated_with_validation_type?(obj, attribute, validation_type)
|
181
|
+
matching = true
|
182
|
+
|
183
|
+
# bail out if options provided are invalid
|
184
|
+
val_opts = _valid_validation_options(validation_type)
|
185
|
+
|
186
|
+
invalid_opts = opts.keys.reject { |o| val_opts.include?(o) }
|
187
|
+
unless invalid_opts.empty?
|
188
|
+
msg << ", but the following invalid option(s) was found: { "
|
189
|
+
invalid_opts.each { |o| msg << "#{o.inspect}; " }
|
190
|
+
msg << " }. Valid options are: #{val_opts.inspect}"
|
191
|
+
assert(false, msg)
|
192
|
+
end
|
193
|
+
|
194
|
+
h = _validation_types_hash_for_column(obj, attribute)
|
195
|
+
_available_validation_options.each do |ov|
|
196
|
+
unless opts[ov].nil?
|
197
|
+
expected = (h[validation_type][ov].to_s == opts[ov].to_s)
|
198
|
+
conf_msg << "#{ov}: '#{opts[ov]}'"
|
199
|
+
unless expected
|
200
|
+
err_msg << "#{ov}: '#{h[validation_type][ov]}'"
|
201
|
+
end
|
202
|
+
matching &&= expected
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
msg = msg << " with: { #{conf_msg.join(', ')} }" unless conf_msg.empty?
|
207
|
+
msg << " but found: { #{err_msg.join(', ')} }" unless err_msg.empty?
|
208
|
+
assert(matching, msg)
|
209
|
+
|
210
|
+
else
|
211
|
+
msg << ", but no :#{validation_type} validation is defined for :#{attribute}"
|
212
|
+
assert(false, msg)
|
213
|
+
end
|
214
|
+
else
|
215
|
+
msg << ", but no validations are defined for :#{attribute}"
|
216
|
+
assert(false, msg)
|
217
|
+
end
|
218
|
+
else
|
219
|
+
assert(false, "No validations defined in #{obj.class.to_s}")
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
|
224
|
+
# Test for validating presence of a model attribute
|
225
|
+
#
|
226
|
+
# it { refute_validates_presence(model, :title) }
|
227
|
+
# it { model.must_validate_presence_of(:title, { message: '...' }) }
|
228
|
+
#
|
229
|
+
def refute_validates_presence(obj, attribute, opts = {}, msg = nil)
|
230
|
+
refute_validates(obj, :presence, attribute, opts, msg)
|
231
|
+
end
|
232
|
+
alias_method :refute_validates_presence_of, :refute_validates_presence
|
233
|
+
|
234
|
+
# Test for validating the length of a model's attribute.
|
235
|
+
#
|
236
|
+
# Available options:
|
237
|
+
#
|
238
|
+
# :message :: The message to use (no default, overrides :nil_message, :too_long,
|
239
|
+
# :too_short, and :wrong_length options if present)
|
240
|
+
#
|
241
|
+
# :nil_message :: The message to use use if :maximum option is used and the value is nil
|
242
|
+
# (default: 'is not present')
|
243
|
+
#
|
244
|
+
# :too_long :: The message to use use if it the value is too long (default: 'is too long')
|
245
|
+
#
|
246
|
+
# :too_short :: The message to use use if it the value is too short
|
247
|
+
# (default: 'is too short')
|
248
|
+
#
|
249
|
+
# :wrong_length :: The message to use use if it the value is not valid
|
250
|
+
# (default: 'is the wrong length')
|
251
|
+
#
|
252
|
+
# SIZE RELATED OPTIONS:
|
253
|
+
#
|
254
|
+
# :is :: The exact size required for the value to be valid (no default)
|
255
|
+
# :minimum :: The minimum size allowed for the value (no default)
|
256
|
+
# :maximum :: The maximum size allowed for the value (no default)
|
257
|
+
# :within :: The array/range that must include the size of the value for it to be valid
|
258
|
+
# (no default)
|
259
|
+
#
|
260
|
+
# it { refute_validates_length(model, :title, { maximum: 12 }) }
|
261
|
+
# it { model.must_validate_length_of(:title, { within: 4..12 }) }
|
262
|
+
#
|
263
|
+
def refute_validates_length(obj, attribute, opts = {}, msg = nil)
|
264
|
+
refute_validates(obj, :length, attribute, opts, msg)
|
265
|
+
end
|
266
|
+
alias_method :refute_validates_length_of, :refute_validates_length
|
267
|
+
|
268
|
+
# Test for validating the exact length of a model's attribute.
|
269
|
+
#
|
270
|
+
# it { refute_validates_exact_length(model, :title, 12, { message: '...' }) }
|
271
|
+
# it { model.must_validate_exact_length_of(:title, 12, { message: '...' }) }
|
272
|
+
#
|
273
|
+
def refute_validates_exact_length(obj, attribute, exact_length, opts = {}, msg = nil)
|
274
|
+
opts.merge!({ is: exact_length })
|
275
|
+
refute_validates(obj, :length, attribute, opts, msg)
|
276
|
+
end
|
277
|
+
alias_method :refute_validates_exact_length_of, :refute_validates_exact_length
|
278
|
+
|
279
|
+
# Test for validating the exact length of a model's attribute.
|
280
|
+
#
|
281
|
+
# it { refute_validates_length_range(model, :title, 4..12, { message: '...' }) }
|
282
|
+
# it { model.must_validate_length_range_of(:title, 4..12, { message: '...' }) }
|
283
|
+
#
|
284
|
+
def refute_validates_length_range(obj, attribute, range, opts = {}, msg = nil)
|
285
|
+
opts.merge!({ within: range })
|
286
|
+
refute_validates(obj, :length, attribute, opts, msg)
|
287
|
+
end
|
288
|
+
alias_method :refute_validates_length_range_of, :refute_validates_length_range
|
289
|
+
|
290
|
+
# Test for validating the maximum length of a model's attribute.
|
291
|
+
#
|
292
|
+
# it { refute_validates_max_length(model, :title, 12, { message: '...' }) }
|
293
|
+
# it { model.must_validate_max_length_of(:title, 12, { message: '...' }) }
|
294
|
+
#
|
295
|
+
def refute_validates_max_length(obj, attribute, max_length, opts = {}, msg = nil)
|
296
|
+
opts.merge!({ maximum: max_length })
|
297
|
+
refute_validates(obj, :length, attribute, opts, msg)
|
298
|
+
end
|
299
|
+
alias_method :refute_validates_max_length_of, :refute_validates_max_length
|
300
|
+
|
301
|
+
# Test for validating the minimum length of a model's attribute.
|
302
|
+
#
|
303
|
+
# it { refute_validates_min_length(model, :title, 12, { message: '...' }) }
|
304
|
+
# it { model.must_validate_min_length_of(:title, 12, { message: '...' }) }
|
305
|
+
#
|
306
|
+
def refute_validates_min_length(obj, attribute, min_length, opts = {}, msg = nil)
|
307
|
+
opts.merge!({ minimum: min_length })
|
308
|
+
refute_validates(obj, :length, attribute, opts, msg)
|
309
|
+
end
|
310
|
+
alias_method :refute_validates_min_length_of, :refute_validates_min_length
|
311
|
+
|
312
|
+
# Test for validating the format of a model's attribute with a regexp.
|
313
|
+
#
|
314
|
+
# it { refute_validates_format(model, :title, 12, { with: /[a-z+]/ }) }
|
315
|
+
# it { model.must_validate_format_of(:title, 12, { with: /[a-z]+/ }) }
|
316
|
+
#
|
317
|
+
def refute_validates_format(obj, attribute, opts = {}, msg = nil)
|
318
|
+
refute_validates(obj, :format, attribute, opts, msg)
|
319
|
+
end
|
320
|
+
alias_method :refute_validates_format_of, :refute_validates_format
|
321
|
+
|
322
|
+
# Test for validating that a model's attribute is within a specified range or set of values.
|
323
|
+
#
|
324
|
+
# it { refute_validates_inclusion(model, :status, { in: [:a, :b, :c] }) }
|
325
|
+
# it { model.must_validate_inclusion_of(:status, { in: [:a, :b, :c] }) }
|
326
|
+
#
|
327
|
+
def refute_validates_inclusion(obj, attribute, opts = {}, msg = nil)
|
328
|
+
refute_validates(obj, :inclusion, attribute, opts, msg)
|
329
|
+
end
|
330
|
+
alias_method :refute_validates_inclusion_of, :refute_validates_inclusion
|
331
|
+
|
332
|
+
# Test for validating that a a model's attribute is an integer.
|
333
|
+
#
|
334
|
+
# it { refute_validates_integer(model, :author_id, { message: '...' }) }
|
335
|
+
# it { model.must_validate_integer_of(:author_id, { message: '...' }) }
|
336
|
+
#
|
337
|
+
def refute_validates_integer(obj, attribute, opts = {}, msg = nil)
|
338
|
+
opts.merge!({ only_integer: true })
|
339
|
+
refute_validates(obj, :numericality, attribute, opts, msg)
|
340
|
+
end
|
341
|
+
|
342
|
+
# Test for validating that a model's attribute is numeric (number).
|
343
|
+
#
|
344
|
+
# it { refute_validates_numericality(model, :author_id, { message: '...' }) }
|
345
|
+
# it { model.must_validate_numericality_of(:author_id, { message: '...' }) }
|
346
|
+
#
|
347
|
+
def refute_validates_numericality(obj, attribute, opts = {}, msg = nil)
|
348
|
+
refute_validates(obj, :numericality, attribute, opts, msg)
|
349
|
+
end
|
350
|
+
alias_method :refute_validates_numericality_of, :refute_validates_numericality
|
351
|
+
|
352
|
+
# Test for validating that a model's attribute is unique.
|
353
|
+
#
|
354
|
+
# it { refute_validates_uniqueness(model, :urlslug, { message: '...' }) }
|
355
|
+
# it { model.must_validate_uniqueness_of(:urlslug, { message: '...' }) }
|
356
|
+
#
|
357
|
+
def refute_validates_uniqueness(obj, attribute, opts = {}, msg = nil)
|
358
|
+
refute_validates(obj, :uniqueness, attribute, opts, msg)
|
359
|
+
end
|
360
|
+
alias_method :refute_validates_uniqueness_of, :refute_validates_uniqueness
|
361
|
+
|
362
|
+
# Validates acceptance of an attribute. Just checks that the value is equal to the :accept option. This method is unique in that :allow_nil is assumed to be true instead of false.
|
363
|
+
|
364
|
+
# Test for validating the acceptance of a model's attribute.
|
365
|
+
#
|
366
|
+
# it { refute_validates_acceptance(Order.new, :toc, { message: '...' }) }
|
367
|
+
# it { model.must_validate_acceptance_of(:toc, { message: '...' }) }
|
368
|
+
#
|
369
|
+
def refute_validates_acceptance(obj, attribute, opts = {}, msg = nil)
|
370
|
+
refute_validates(obj, :acceptance, attribute, opts, msg)
|
371
|
+
end
|
372
|
+
alias_method :refute_validates_acceptance_of, :refute_validates_acceptance
|
373
|
+
|
374
|
+
# Test for validating the confirmation of a model's attribute.
|
375
|
+
#
|
376
|
+
# it { refute_validates_confirmation(User.new, :password, { message: '...' }) }
|
377
|
+
# it { User.new.must_validate_confirmation_of(:password, { message: '...' }) }
|
378
|
+
#
|
379
|
+
def refute_validates_confirmation(obj, attribute, opts = {}, msg = nil)
|
380
|
+
refute_validates(obj, :confirmation, attribute, opts, msg)
|
381
|
+
end
|
382
|
+
alias_method :refute_validates_confirmation_of, :refute_validates_confirmation
|
383
|
+
|
384
|
+
# Base test for validations of a model, used mainly as a shortcut for other assertions
|
385
|
+
def refute_validates(obj, validation_type, attribute, opts = {}, msg = nil, &blk)
|
386
|
+
msg = msg.nil? ? '' : "#{msg}\n"
|
387
|
+
err_msg = []
|
388
|
+
conf_msg = []
|
389
|
+
unless obj.respond_to?(attribute)
|
390
|
+
assert(false, "Column :#{attribute} is not defined in #{obj.class.to_s}, so cannot be validated")
|
391
|
+
end
|
392
|
+
|
393
|
+
msg << "Expected #{obj.class} NOT to validate :#{attribute} with :#{validation_type}"
|
394
|
+
|
395
|
+
if _validated_model?(obj)
|
396
|
+
|
397
|
+
if _validated_column?(obj, attribute)
|
398
|
+
msg << ", but the column :#{attribute} was validated with :#{validation_type}"
|
399
|
+
assert(false, msg)
|
400
|
+
else
|
401
|
+
assert(true, msg)
|
402
|
+
end
|
403
|
+
else
|
404
|
+
assert(false, "No validations defined in #{obj.class.to_s}")
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
|
409
|
+
#
|
410
|
+
def assert_raises_validation_failed(obj)
|
411
|
+
assert_raises(::Sequel::ValidationFailed) { obj.save }
|
412
|
+
end
|
413
|
+
alias_method :assert_fails_validation, :assert_raises_validation_failed
|
414
|
+
|
415
|
+
|
416
|
+
private
|
417
|
+
|
418
|
+
|
419
|
+
#
|
420
|
+
def _validated_model?(model)
|
421
|
+
return (model.class.respond_to?(:has_validations?) && model.class.has_validations?)
|
422
|
+
end
|
423
|
+
|
424
|
+
#
|
425
|
+
def _validated_column?(model, attribute)
|
426
|
+
return false unless _validated_model?(model)
|
427
|
+
return model.class.validation_reflections.keys.include?(attribute.to_sym)
|
428
|
+
end
|
429
|
+
|
430
|
+
#
|
431
|
+
def _validated_with_validation_type?(model, attribute, validation_type)
|
432
|
+
return false unless _validated_column?(model, attribute)
|
433
|
+
return _validation_types_hash_for_column(model, attribute).keys.include?(validation_type)
|
434
|
+
end
|
435
|
+
|
436
|
+
#
|
437
|
+
def _validation_types_hash_for_column(model, attribute)
|
438
|
+
h = {}
|
439
|
+
model.class.validation_reflections[attribute].each { |c| h[c[0]] = c[1] }
|
440
|
+
h
|
441
|
+
end
|
442
|
+
|
443
|
+
#
|
444
|
+
def _available_validation_types
|
445
|
+
[:format, :length, :presence, :numericality, :confirmation, :acceptance, :inclusion, :uniqueness]
|
446
|
+
end
|
447
|
+
|
448
|
+
#
|
449
|
+
def _available_validation_options
|
450
|
+
[
|
451
|
+
:message, :if, :is, :in, :allow_blank, :allow_missing, :allow_nil, :accept, :with, :within,
|
452
|
+
:only_integer, :maximum, :minimum, :nil_message, :too_long, :too_short, :wrong_length
|
453
|
+
]
|
454
|
+
end
|
455
|
+
|
456
|
+
#
|
457
|
+
def _valid_validation_options(type = nil)
|
458
|
+
arr = [:message]
|
459
|
+
case type.to_sym
|
460
|
+
when :each
|
461
|
+
# validates_each (*atts, &block)
|
462
|
+
# Adds a validation for each of the given attributes using the supplied block.
|
463
|
+
[:allow_blank, :allow_missing, :allow_nil].each { |a| arr << a }
|
464
|
+
when :acceptance
|
465
|
+
# The value required for the object to be valid (default: '1')
|
466
|
+
arr << :accept
|
467
|
+
when :format
|
468
|
+
# The regular expression to validate the value with (required).
|
469
|
+
arr << :with
|
470
|
+
when :inclusion
|
471
|
+
# An array or range of values to check for validity (required)
|
472
|
+
arr << :in
|
473
|
+
when :numericality
|
474
|
+
# Whether only integers are valid values (default: false)
|
475
|
+
arr << :only_integer
|
476
|
+
when :length
|
477
|
+
#
|
478
|
+
# :message :: The message to use (no default, overrides :nil_message, :too_long,
|
479
|
+
# :too_short, and :wrong_length options if present)
|
480
|
+
#
|
481
|
+
# :nil_message :: The message to use use if :maximum option is used and the value is nil
|
482
|
+
# (default: 'is not present')
|
483
|
+
#
|
484
|
+
# :too_long :: The message to use use if it the value is too long (default: 'is too long')
|
485
|
+
#
|
486
|
+
# :too_short :: The message to use use if it the value is too short
|
487
|
+
# (default: 'is too short')
|
488
|
+
#
|
489
|
+
# :wrong_length :: The message to use use if it the value is not valid
|
490
|
+
# (default: 'is the wrong length')
|
491
|
+
#
|
492
|
+
# SIZE
|
493
|
+
# :is :: The exact size required for the value to be valid (no default)
|
494
|
+
# :minimum :: The minimum size allowed for the value (no default)
|
495
|
+
# :maximum :: The maximum size allowed for the value (no default)
|
496
|
+
# :within :: The array/range that must include the size of the value for it to be valid
|
497
|
+
# (no default)
|
498
|
+
|
499
|
+
[ :is, :maximum, :minimum, :nil_message, :too_long, :too_short, :within, :wrong_length ]
|
500
|
+
.each { |a| arr << a }
|
501
|
+
else
|
502
|
+
arr
|
503
|
+
end unless type.nil?
|
504
|
+
arr
|
505
|
+
end
|
506
|
+
|
507
|
+
end
|
508
|
+
|
509
|
+
|
510
|
+
# add support for Spec syntax
|
511
|
+
module Minitest::Expectations
|
512
|
+
infect_an_assertion :assert_validates, :must_validate, :reverse
|
513
|
+
infect_an_assertion :assert_validates_presence, :must_validate_presence_of, :reverse
|
514
|
+
infect_an_assertion :assert_validates_format, :must_validate_format_of, :reverse
|
515
|
+
|
516
|
+
infect_an_assertion :assert_validates_length, :must_validate_length_of, :reverse
|
517
|
+
infect_an_assertion :assert_validates_exact_length, :must_validate_exact_length_of, :reverse
|
518
|
+
infect_an_assertion :assert_validates_min_length, :must_validate_min_length_of, :reverse
|
519
|
+
infect_an_assertion :assert_validates_max_length, :must_validate_max_length_of, :reverse
|
520
|
+
infect_an_assertion :assert_validates_length_range, :must_validate_length_range_of, :reverse
|
521
|
+
|
522
|
+
infect_an_assertion :assert_validates_integer, :must_validate_integer_of, :reverse
|
523
|
+
infect_an_assertion :assert_validates_numericality, :must_validate_numericality_of, :reverse
|
524
|
+
infect_an_assertion :assert_validates_confirmation, :must_validate_confirmation_of, :reverse
|
525
|
+
infect_an_assertion :assert_validates_acceptance, :must_validate_acceptance_of, :reverse
|
526
|
+
infect_an_assertion :assert_validates_inclusion, :must_validate_inclusion_of, :reverse
|
527
|
+
infect_an_assertion :assert_validates_uniqueness, :must_validate_uniqueness_of, :reverse
|
528
|
+
|
529
|
+
infect_an_assertion :assert_fails_validation, :must_fail_validation, :reverse
|
530
|
+
|
531
|
+
|
532
|
+
infect_an_assertion :refute_validates, :wont_validate, :reverse
|
533
|
+
infect_an_assertion :refute_validates_presence, :wont_validate_presence_of, :reverse
|
534
|
+
infect_an_assertion :refute_validates_format, :wont_validate_format_of, :reverse
|
535
|
+
|
536
|
+
infect_an_assertion :refute_validates_length, :wont_validate_length_of, :reverse
|
537
|
+
infect_an_assertion :refute_validates_exact_length, :wont_validate_exact_length_of, :reverse
|
538
|
+
infect_an_assertion :refute_validates_min_length, :wont_validate_min_length_of, :reverse
|
539
|
+
infect_an_assertion :refute_validates_max_length, :wont_validate_max_length_of, :reverse
|
540
|
+
infect_an_assertion :refute_validates_length_range, :wont_validate_length_range_of, :reverse
|
541
|
+
|
542
|
+
infect_an_assertion :refute_validates_integer, :wont_validate_integer_of, :reverse
|
543
|
+
infect_an_assertion :refute_validates_numericality, :wont_validate_numericality_of, :reverse
|
544
|
+
infect_an_assertion :refute_validates_confirmation, :wont_validate_confirmation_of, :reverse
|
545
|
+
infect_an_assertion :refute_validates_acceptance, :wont_validate_acceptance_of, :reverse
|
546
|
+
infect_an_assertion :refute_validates_inclusion, :wont_validate_inclusion_of, :reverse
|
547
|
+
infect_an_assertion :refute_validates_uniqueness, :wont_validate_uniqueness_of, :reverse
|
548
|
+
|
549
|
+
end
|