File: auto_validations_spec.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (339 lines) | stat: -rw-r--r-- 12,440 bytes parent folder | download | duplicates (2)
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
require_relative "spec_helper"

describe "Sequel::Plugins::AutoValidations" do
  before do
    db = Sequel.mock(:fetch=>proc{|sql| sql =~ /'a{51}'|'uniq'/ ? {:v=>0} : {:v=>1}})
    def db.schema_parse_table(*) true; end
    def db.schema(t, *)
      t = t.first_source if t.is_a?(Sequel::Dataset)
      return [] if t != :test
      [[:id, {:primary_key=>true, :type=>:integer, :allow_null=>false}],
       [:name, {:primary_key=>false, :type=>:string, :allow_null=>false, :max_length=>50}],
       [:num, {:primary_key=>false, :type=>:integer, :allow_null=>true, :min_value=>-100000, :max_value=>100000}],
       [:d, {:primary_key=>false, :type=>:date, :allow_null=>false}],
       [:nnd, {:primary_key=>false, :type=>:string, :allow_null=>false, :default=>'nnd'}]]
    end
    def db.supports_index_parsing?() true end
    db.singleton_class.send(:alias_method, :supports_index_parsing?, :supports_index_parsing?)
    def db.indexes(t, *)
      raise if t.is_a?(Sequel::Dataset)
      return [] if t != :test
      {:a=>{:columns=>[:name, :num], :unique=>true}, :b=>{:columns=>[:num], :unique=>false}}
    end
    db.singleton_class.send(:alias_method, :indexes, :indexes)
    @c = Class.new(Sequel::Model(db[:test]))
    @c.send(:def_column_accessor, :id, :name, :num, :d, :nnd)
    @c.raise_on_typecast_failure = false
    @c.plugin :auto_validations
    @m = @c.new
    db.sqls
  end

  it "should have automatically created validations" do
    @m.num = 100001
    @m.valid?.must_equal false
    @m.errors.must_equal(:d=>["is not present"], :name=>["is not present"], :num=>["is greater than maximum allowed value"])

    @m.set(:num=>-100001, :name=>"")
    @m.valid?.must_equal false
    @m.errors.must_equal(:d=>["is not present"], :num=>["is less than minimum allowed value"])

    @m.set(:d=>'/', :num=>'a', :name=>"a\0b")
    @m.valid?.must_equal false
    @m.errors.must_equal(:d=>["is not a valid date"], :num=>["is not a valid integer"], :name=>["contains a null byte"])

    @m.set(:d=>Date.today, :num=>1, :name=>'')
    @m.valid?.must_equal false
    @m.errors.must_equal([:name, :num]=>["is already taken"])

    @m.set(:name=>'a'*51)
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>["is longer than 50 characters"])
  end

  it "should add errors to columns that already have errors by default" do
    def @m.validate
      errors.add(:name, 'no good')
      super
    end
    @m.d = Date.today
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>['no good', "is not present"])
  end

  it "should not add errors to columns that already have errors when using :skip_invalid plugin option" do
    @c.plugin :auto_validations, :skip_invalid=>true
    def @m.validate
      errors.add(:name, 'no good')
      super
    end
    @m.d = Date.today
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>['no good'])
  end

  it "should handle simple unique indexes correctly" do
    def (@c.db).indexes(t, *)
      raise if t.is_a?(Sequel::Dataset)
      return [] if t != :test
      {:a=>{:columns=>[:name], :unique=>true}}
    end
    @c.plugin :auto_validations
    @m.set(:name=>'foo', :d=>Date.today)
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>["is already taken"])
  end

  it "should validate using the underlying column values" do
    @c.send(:define_method, :name){super() * 2}
    @c.db.fetch = {:v=>0}
    @m.set(:d=>Date.today, :num=>1, :name=>'b'*26)
    @m.valid?.must_equal true
  end

  it "should handle databases that don't support index parsing" do
    def (@m.db).supports_index_parsing?() false end
    @m.model.send(:setup_auto_validations)
    @m.set(:d=>Date.today, :num=>1, :name=>'1')
    @m.valid?.must_equal true
  end

  it "should handle models that select from subqueries" do
    @c.set_dataset @c.dataset.from_self
    @c.send(:setup_auto_validations)
  end

  it "should support :not_null=>:presence option" do
    @c.plugin :auto_validations, :not_null=>:presence
    @m.set(:d=>Date.today, :num=>'')
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>["is not present"])
  end

  it "should automatically validate explicit nil values for columns with not nil defaults" do
    @m.set(:d=>Date.today, :name=>1, :nnd=>nil)
    @m.id = nil
    @m.valid?.must_equal false
    @m.errors.must_equal(:id=>["is not present"], :nnd=>["is not present"])
  end

  it "should allow skipping validations by type" do
    @c = Class.new(@c)
    @m = @c.new
    @m.skip_auto_validations(:not_null) do
      @m.valid?.must_equal true
      @m.nnd = nil
      @m.valid?.must_equal true
    end
    @m.set(:nnd => 'nnd')
    @c.skip_auto_validations(:not_null)
    @m.valid?.must_equal true
    @m.nnd = nil
    @m.valid?.must_equal true

    @m.set(:d=>'/', :num=>'a', :name=>'1')
    @m.valid?.must_equal false
    @m.errors.must_equal(:d=>["is not a valid date"], :num=>["is not a valid integer"])

    @m.skip_auto_validations(:types, :unique) do
      @m.valid?.must_equal true
    end
    @m.skip_auto_validations(:types) do
      @m.valid?.must_equal false
      @m.errors.must_equal([:name, :num]=>["is already taken"])
    end
    @c.skip_auto_validations(:types)
    @m.valid?.must_equal false
    @m.errors.must_equal([:name, :num]=>["is already taken"])

    @m.skip_auto_validations(:unique) do
      @m.valid?.must_equal true
    end
    @c.skip_auto_validations(:unique)
    @m.valid?.must_equal true

    @m.set(:name=>'a'*51)
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>["is longer than 50 characters"])

    @m.skip_auto_validations(:max_length) do
      @m.valid?.must_equal true
    end
    @c.skip_auto_validations(:max_length)
    @m.valid?.must_equal true
  end

  it "should allow skipping all auto validations" do
    @c = Class.new(@c)
    @m = @c.new
    @m.skip_auto_validations(:all) do
      @m.valid?.must_equal true
      @m.set(:d=>'/', :num=>'a', :name=>'1')
      @m.valid?.must_equal true
      @m.set(:name=>'a'*51)
      @m.valid?.must_equal true
    end
    @m = @c.new
    @c.skip_auto_validations(:all)
    @m.valid?.must_equal true
    @m.set(:d=>'/', :num=>'a', :name=>'1')
    @m.valid?.must_equal true
    @m.set(:name=>'a'*51)
    @m.valid?.must_equal true
  end

  it "should skip min/max value validations when skipping type validations" do
    @m.set(:d=>Date.today, :num=>100001, :name=>'uniq')
    @m.valid?.must_equal false
    @m.skip_auto_validations(:types) do
      @m.valid?.must_equal true
    end

    @m.num = -100001
    @m.valid?.must_equal false
    @m.skip_auto_validations(:types) do
      @m.valid?.must_equal true
    end
  end

  it "should default to skipping all auto validations if no arguments given to instance method" do
    @c = Class.new(@c)
    @m = @c.new
    @m.skip_auto_validations do
      @m.valid?.must_equal true
      @m.set(:d=>'/', :num=>'a', :name=>'1')
      @m.valid?.must_equal true
      @m.set(:name=>'a'*51)
      @m.valid?.must_equal true
    end
  end

  it "should work correctly in subclasses" do
    @c = Class.new(@c)
    @m = @c.new
    @m.num = 100001
    @m.valid?.must_equal false
    @m.errors.must_equal(:d=>["is not present"], :name=>["is not present"], :num=>["is greater than maximum allowed value"])

    @m.set(:num=>-100001, :name=>"")
    @m.valid?.must_equal false
    @m.errors.must_equal(:d=>["is not present"], :num=>["is less than minimum allowed value"])

    @m.set(:d=>'/', :num=>'a', :name=>"a\0b")
    @m.valid?.must_equal false
    @m.errors.must_equal(:d=>["is not a valid date"], :num=>["is not a valid integer"], :name=>["contains a null byte"])

    @m.set(:d=>Date.today, :num=>1, :name=>'')
    @m.valid?.must_equal false
    @m.errors.must_equal([:name, :num]=>["is already taken"])

    @m.set(:name=>'a'*51)
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>["is longer than 50 characters"])
  end

  it "should work correctly in STI subclasses" do
    @c.plugin(:single_table_inheritance, :num, :model_map=>{1=>@c}, :key_map=>proc{[1, 2]})
    sc = Class.new(@c)
    @m = sc.new
    @m.valid?.must_equal false
    @m.errors.must_equal(:d=>["is not present"], :name=>["is not present"])

    @m.set(:d=>'/', :num=>'a', :name=>'1')
    @m.valid?.must_equal false
    @m.errors.must_equal(:d=>["is not a valid date"], :num=>["is not a valid integer"])

    @m.db.sqls
    @m.set(:d=>Date.today, :num=>1)
    @m.valid?.must_equal false
    @m.errors.must_equal([:name, :num]=>["is already taken"])
    @m.db.sqls.must_equal ["SELECT count(*) AS count FROM test WHERE ((name = '1') AND (num = 1)) LIMIT 1"]

    @m.set(:name=>'a'*51)
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>["is longer than 50 characters"])
  end

  it "should work correctly when changing the dataset" do
    @c.set_dataset(@c.db[:foo])
    @c.new.valid?.must_equal true
  end

  it "should support setting validator options" do
    sc = Class.new(@c)
    sc.plugin :auto_validations,
      :max_length_opts=> {:message=> 'ml_message'},
      :max_value_opts=> {:message=> 'mv_message'},
      :min_value_opts=> {:message=> 'min_message'},
      :no_null_byte_opts=> {:message=> 'nnb_message'},
      :schema_types_opts=> {:message=> 'st_message'},
      :explicit_not_null_opts=> {:message=> 'enn_message'},
      :unique_opts=> {:message=> 'u_message'}

    @m = sc.new
    @m.set(:name=>'a'*51, :d => '/', :nnd => nil, :num=>1)
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>["ml_message"], :d=>["st_message"], :nnd=>["enn_message"])

    @m = sc.new
    @m.set(:name=>1, :num=>1, :d=>Date.today)
    @m.valid?.must_equal false
    @m.errors.must_equal([:name, :num]=>["u_message"])

    @m.set(:num=>100001, :name=>"a\0b")
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>["nnb_message"], :num=>["mv_message"])

    @m.num = -100001
    @m.valid?.must_equal false
    @m.errors.must_equal(:name=>["nnb_message"], :num=>["min_message"])
  end

  it "should store modifying auto validation information in mutable auto_validate_* attributes" do
    @c.auto_validate_not_null_columns.frozen?.must_equal false
    @c.auto_validate_explicit_not_null_columns.frozen?.must_equal false
    @c.auto_validate_max_length_columns.frozen?.must_equal false
    @c.auto_validate_unique_columns.frozen?.must_equal false
    @c.auto_validate_no_null_byte_columns.frozen?.must_equal false
    @c.auto_validate_max_value_columns.frozen?.must_equal false
    @c.auto_validate_min_value_columns.frozen?.must_equal false
    @c.auto_validate_not_null_columns.frozen?.must_equal false

    @c.auto_validate_explicit_not_null_columns.sort.must_equal [:id, :nnd]
    @c.auto_validate_max_length_columns.sort.must_equal [[:name, 50]]
    @c.auto_validate_unique_columns.sort.must_equal [[:name, :num]]
    @c.auto_validate_no_null_byte_columns.sort.must_equal [:name, :nnd]
    @c.auto_validate_max_value_columns.sort.must_equal [[:num, 100000]]
    @c.auto_validate_min_value_columns.sort.must_equal [[:num, -100000]]
  end

  it "should copy auto validation information when subclassing" do
    sc = Class.new(@c)
    @c.auto_validate_not_null_columns.clear
    @c.auto_validate_explicit_not_null_columns.clear
    @c.auto_validate_max_length_columns.clear
    @c.auto_validate_unique_columns.clear
    @c.auto_validate_no_null_byte_columns.clear
    @c.auto_validate_max_value_columns.clear
    @c.auto_validate_min_value_columns.clear
    @c.auto_validate_not_null_columns.clear

    sc.auto_validate_explicit_not_null_columns.sort.must_equal [:id, :nnd]
    sc.auto_validate_max_length_columns.sort.must_equal [[:name, 50]]
    sc.auto_validate_unique_columns.sort.must_equal [[:name, :num]]
    sc.auto_validate_no_null_byte_columns.sort.must_equal [:name, :nnd]
    sc.auto_validate_max_value_columns.sort.must_equal [[:num, 100000]]
    sc.auto_validate_min_value_columns.sort.must_equal [[:num, -100000]]
  end

  it "should not allow modifying auto validation information for frozen model classes" do
    @c.freeze
    @c.auto_validate_not_null_columns.frozen?.must_equal true
    @c.auto_validate_explicit_not_null_columns.frozen?.must_equal true
    @c.auto_validate_max_length_columns.frozen?.must_equal true
    @c.auto_validate_unique_columns.frozen?.must_equal true
    @c.auto_validate_no_null_byte_columns.frozen?.must_equal true
    @c.auto_validate_max_value_columns.frozen?.must_equal true
    @c.auto_validate_min_value_columns.frozen?.must_equal true
  end
end