/
validation_helpers_spec.rb
452 lines (404 loc) · 14.4 KB
/
validation_helpers_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
describe "Sequel::Plugins::ValidationHelpers" do
before do
@c = Class.new(Sequel::Model) do
def self.set_validations(&block)
define_method(:validate, &block)
end
columns :value
end
@c.plugin :validation_helpers
@m = @c.new
end
specify "should take an :allow_blank option" do
@c.set_validations{validates_format(/.+_.+/, :value, :allow_blank=>true)}
@m.value = 'abc_'
@m.should_not be_valid
@m.value = '1_1'
@m.should be_valid
o = Object.new
@m.value = o
@m.should_not be_valid
def o.blank?
true
end
@m.should be_valid
end
specify "should take an :allow_missing option" do
@c.set_validations{validates_format(/.+_.+/, :value, :allow_missing=>true)}
@m.values.clear
@m.should be_valid
@m.value = nil
@m.should_not be_valid
@m.value = '1_1'
@m.should be_valid
end
specify "should take an :allow_nil option" do
@c.set_validations{validates_format(/.+_.+/, :value, :allow_nil=>true)}
@m.value = 'abc_'
@m.should_not be_valid
@m.value = '1_1'
@m.should be_valid
@m.value = nil
@m.should be_valid
end
specify "should take a :message option" do
@c.set_validations{validates_format(/.+_.+/, :value, :message=>"is so blah")}
@m.value = 'abc_'
@m.should_not be_valid
@m.errors.full_messages.should == ['value is so blah']
@m.value = '1_1'
@m.should be_valid
end
specify "should allow a proc for the :message option" do
@c.set_validations{validates_format(/.+_.+/, :value, :message=>proc{|f| "doesn't match #{f.inspect}"})}
@m.value = 'abc_'
@m.should_not be_valid
@m.errors.should == {:value=>["doesn't match /.+_.+/"]}
end
specify "should take multiple attributes in the same call" do
@c.columns :value, :value2
@c.set_validations{validates_presence([:value, :value2])}
@m.should_not be_valid
@m.value = 1
@m.should_not be_valid
@m.value2 = 1
@m.should be_valid
end
specify "should support modifying default options for all models" do
@c.set_validations{validates_presence(:value)}
@m.should_not be_valid
@m.errors.should == {:value=>['is not present']}
o = Sequel::Plugins::ValidationHelpers::DEFAULT_OPTIONS[:presence].dup
Sequel::Plugins::ValidationHelpers::DEFAULT_OPTIONS[:presence][:message] = lambda{"was not entered"}
@m.should_not be_valid
@m.errors.should == {:value=>["was not entered"]}
@m.value = 1
@m.should be_valid
@m.values.clear
Sequel::Plugins::ValidationHelpers::DEFAULT_OPTIONS[:presence][:allow_missing] = true
@m.should be_valid
@m.value = nil
@m.should_not be_valid
@m.errors.should == {:value=>["was not entered"]}
c = Class.new(Sequel::Model)
c.class_eval do
plugin :validation_helpers
set_columns([:value])
def validate
validates_presence(:value)
end
end
m = c.new(:value=>nil)
m.should_not be_valid
m.errors.should == {:value=>["was not entered"]}
Sequel::Plugins::ValidationHelpers::DEFAULT_OPTIONS[:presence] = o
end
specify "should support modifying default validation options for a particular model" do
@c.set_validations{validates_presence(:value)}
@m.should_not be_valid
@m.errors.should == {:value=>['is not present']}
@c.class_eval do
def default_validation_helpers_options(type)
{:allow_missing=>true, :message=>proc{'was not entered'}}
end
end
@m.value = nil
@m.should_not be_valid
@m.errors.should == {:value=>["was not entered"]}
@m.value = 1
@m.should be_valid
@m.values.clear
@m.should be_valid
c = Class.new(Sequel::Model)
c.class_eval do
plugin :validation_helpers
attr_accessor :value
def validate
validates_presence(:value)
end
end
m = c.new
m.should_not be_valid
m.errors.should == {:value=>['is not present']}
end
specify "should support validates_exact_length" do
@c.set_validations{validates_exact_length(3, :value)}
@m.should_not be_valid
@m.value = '123'
@m.should be_valid
@m.value = '12'
@m.should_not be_valid
@m.value = '1234'
@m.should_not be_valid
end
specify "should support validate_format" do
@c.set_validations{validates_format(/.+_.+/, :value)}
@m.value = 'abc_'
@m.should_not be_valid
@m.value = 'abc_def'
@m.should be_valid
end
specify "should support validates_includes with an array" do
@c.set_validations{validates_includes([1,2], :value)}
@m.should_not be_valid
@m.value = 1
@m.should be_valid
@m.value = 1.5
@m.should_not be_valid
@m.value = 2
@m.should be_valid
@m.value = 3
@m.should_not be_valid
end
specify "should support validates_includes with a range" do
@c.set_validations{validates_includes(1..4, :value)}
@m.should_not be_valid
@m.value = 1
@m.should be_valid
@m.value = 1.5
@m.should be_valid
@m.value = 0
@m.should_not be_valid
@m.value = 5
@m.should_not be_valid
end
specify "should supports validates_integer" do
@c.set_validations{validates_integer(:value)}
@m.value = 'blah'
@m.should_not be_valid
@m.value = '123'
@m.should be_valid
@m.value = '123.1231'
@m.should_not be_valid
end
specify "should support validates_length_range" do
@c.set_validations{validates_length_range(2..5, :value)}
@m.should_not be_valid
@m.value = '12345'
@m.should be_valid
@m.value = '1'
@m.should_not be_valid
@m.value = '123456'
@m.should_not be_valid
end
specify "should support validates_max_length" do
@c.set_validations{validates_max_length(5, :value)}
@m.should_not be_valid
@m.value = '12345'
@m.should be_valid
@m.value = '123456'
@m.should_not be_valid
@m.errors[:value].should == ['is longer than 5 characters']
@m.value = nil
@m.should_not be_valid
@m.errors[:value].should == ['is not present']
end
specify "should support validates_max_length with nil value" do
@c.set_validations{validates_max_length(5, :value, :message=>'tl', :nil_message=>'np')}
@m.value = '123456'
@m.should_not be_valid
@m.errors[:value].should == ['tl']
@m.value = nil
@m.should_not be_valid
@m.errors[:value].should == ['np']
end
specify "should support validates_min_length" do
@c.set_validations{validates_min_length(5, :value)}
@m.should_not be_valid
@m.value = '12345'
@m.should be_valid
@m.value = '1234'
@m.should_not be_valid
end
specify "should support validates_not_string" do
@c.set_validations{validates_not_string(:value)}
@m.value = 123
@m.should be_valid
@m.value = '123'
@m.should_not be_valid
@m.errors.full_messages.should == ['value is a string']
@m.meta_def(:db_schema){{:value=>{:type=>:integer}}}
@m.should_not be_valid
@m.errors.full_messages.should == ['value is not a valid integer']
end
specify "should support validates_numeric" do
@c.set_validations{validates_numeric(:value)}
@m.value = 'blah'
@m.should_not be_valid
@m.value = '123'
@m.should be_valid
@m.value = '123.1231'
@m.should be_valid
@m.value = '+1'
@m.should be_valid
@m.value = '-1'
@m.should be_valid
@m.value = '+1.123'
@m.should be_valid
@m.value = '-0.123'
@m.should be_valid
@m.value = '-0.123E10'
@m.should be_valid
@m.value = '32.123e10'
@m.should be_valid
@m.value = '+32.123E10'
@m.should be_valid
@m.should be_valid
@m.value = '.0123'
end
specify "should support validates_type" do
@c.set_validations{validates_type(Integer, :value)}
@m.value = 123
@m.should be_valid
@m.value = '123'
@m.should_not be_valid
@m.errors.full_messages.should == ['value is not a Integer']
@c.set_validations{validates_type(:String, :value)}
@m.value = '123'
@m.should be_valid
@m.value = 123
@m.should_not be_valid
@m.errors.full_messages.should == ['value is not a String']
@c.set_validations{validates_type('Integer', :value)}
@m.value = 123
@m.should be_valid
@m.value = 123.05
@m.should_not be_valid
@m.errors.full_messages.should == ['value is not a Integer']
end
specify "should support validates_presence" do
@c.set_validations{validates_presence(:value)}
@m.should_not be_valid
@m.value = ''
@m.should_not be_valid
@m.value = 1234
@m.should be_valid
@m.value = nil
@m.should_not be_valid
@m.value = true
@m.should be_valid
@m.value = false
@m.should be_valid
@m.value = Time.now
@m.should be_valid
end
it "should support validates_unique with a single attribute" do
@c.columns(:id, :username, :password)
@c.set_dataset MODEL_DB[:items]
@c.set_validations{validates_unique(:username)}
@c.dataset.extend(Module.new {
def fetch_rows(sql)
@db << sql
case sql
when /COUNT.*username = '0records'/
yield({:v => 0})
when /COUNT.*username = '1record'/
yield({:v => 1})
end
end
})
@user = @c.new(:username => "0records", :password => "anothertest")
@user.should be_valid
@user = @c.load(:id=>3, :username => "0records", :password => "anothertest")
@user.should be_valid
@user = @c.new(:username => "1record", :password => "anothertest")
@user.should_not be_valid
@user.errors.full_messages.should == ['username is already taken']
@user = @c.load(:id=>4, :username => "1record", :password => "anothertest")
@user.should_not be_valid
@user.errors.full_messages.should == ['username is already taken']
ds1 = @c.dataset.filter([[:username, '0records']])
ds2 = ds1.exclude(:id=>1)
@c.dataset.should_receive(:filter).with([[:username, '0records']]).twice.and_return(ds1)
ds1.should_receive(:exclude).with(:id=>1).once.and_return(ds2)
@user = @c.load(:id=>1, :username => "0records", :password => "anothertest")
@user.should be_valid
MODEL_DB.sqls.last.should == "SELECT COUNT(*) AS count FROM items WHERE ((username = '0records') AND (id != 1)) LIMIT 1"
@user = @c.new(:username => "0records", :password => "anothertest")
@user.should be_valid
MODEL_DB.sqls.last.should == "SELECT COUNT(*) AS count FROM items WHERE (username = '0records') LIMIT 1"
end
it "should support validates_unique with multiple attributes" do
@c.columns(:id, :username, :password)
@c.set_dataset MODEL_DB[:items]
@c.set_validations{validates_unique([:username, :password])}
@c.dataset.extend(Module.new {
def fetch_rows(sql)
@db << sql
case sql
when /COUNT.*username = '0records'/
yield({:v => 0})
when /COUNT.*username = '1record'/
yield({:v => 1})
end
end
})
@user = @c.new(:username => "0records", :password => "anothertest")
@user.should be_valid
@user = @c.load(:id=>3, :username => "0records", :password => "anothertest")
@user.should be_valid
@user = @c.new(:username => "1record", :password => "anothertest")
@user.should_not be_valid
@user.errors.full_messages.should == ['username and password is already taken']
@user = @c.load(:id=>4, :username => "1record", :password => "anothertest")
@user.should_not be_valid
@user.errors.full_messages.should == ['username and password is already taken']
ds1 = @c.dataset.filter([[:username, '0records'], [:password, 'anothertest']])
ds2 = ds1.exclude(:id=>1)
@c.dataset.should_receive(:filter).with([[:username, '0records'], [:password, 'anothertest']]).twice.and_return(ds1)
ds1.should_receive(:exclude).with(:id=>1).once.and_return(ds2)
@user = @c.load(:id=>1, :username => "0records", :password => "anothertest")
@user.should be_valid
MODEL_DB.sqls.last.should == "SELECT COUNT(*) AS count FROM items WHERE ((username = '0records') AND (password = 'anothertest') AND (id != 1)) LIMIT 1"
@user = @c.new(:username => "0records", :password => "anothertest")
@user.should be_valid
MODEL_DB.sqls.last.should == "SELECT COUNT(*) AS count FROM items WHERE ((username = '0records') AND (password = 'anothertest')) LIMIT 1"
end
it "should support validates_unique with a block" do
@c.columns(:id, :username, :password)
@c.set_dataset MODEL_DB[:items]
@c.set_validations{validates_unique(:username){|ds| ds.filter(:active)}}
@c.dataset.extend(Module.new {
def fetch_rows (sql)
@db << sql
yield({:v => 0})
end
})
MODEL_DB.reset
@c.new(:username => "0records", :password => "anothertest").should be_valid
@c.load(:id=>3, :username => "0records", :password => "anothertest").should be_valid
MODEL_DB.sqls.should == ["SELECT COUNT(*) AS count FROM items WHERE ((username = '0records') AND active) LIMIT 1",
"SELECT COUNT(*) AS count FROM items WHERE ((username = '0records') AND active AND (id != 3)) LIMIT 1"]
end
it "should support :only_if_modified option for validates_unique, and not check uniqueness for existing records if values haven't changed" do
@c.columns(:id, :username, :password)
@c.set_dataset MODEL_DB[:items]
@c.set_validations{validates_unique([:username, :password], :only_if_modified=>true)}
@c.dataset.extend(Module.new {
def fetch_rows (sql)
@db << sql
yield({:v => 0})
end
})
MODEL_DB.reset
@c.new(:username => "0records", :password => "anothertest").should be_valid
MODEL_DB.sqls.should == ["SELECT COUNT(*) AS count FROM items WHERE ((username = '0records') AND (password = 'anothertest')) LIMIT 1"]
MODEL_DB.reset
m = @c.load(:id=>3, :username => "0records", :password => "anothertest")
m.should be_valid
MODEL_DB.sqls.should == []
m.username = '1'
m.should be_valid
MODEL_DB.sqls.should == ["SELECT COUNT(*) AS count FROM items WHERE ((username = '1') AND (password = 'anothertest') AND (id != 3)) LIMIT 1"]
m = @c.load(:id=>3, :username => "0records", :password => "anothertest")
MODEL_DB.reset
m.password = '1'
m.should be_valid
MODEL_DB.sqls.should == ["SELECT COUNT(*) AS count FROM items WHERE ((username = '0records') AND (password = '1') AND (id != 3)) LIMIT 1"]
MODEL_DB.reset
m.username = '2'
m.should be_valid
MODEL_DB.sqls.should == ["SELECT COUNT(*) AS count FROM items WHERE ((username = '2') AND (password = '1') AND (id != 3)) LIMIT 1"]
end
end