File: validation_contexts_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 (31 lines) | stat: -rw-r--r-- 1,079 bytes parent folder | download | duplicates (4)
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
require_relative "spec_helper"

describe "Sequel::Plugins::ValidationHelpers" do
  before do
    @c = Class.new(Sequel::Model(:foo))
    @c.class_eval do
      columns :a, :b, :c
      plugin :validation_contexts
      def validate
        errors.add(:a, 'bad') if a == 1 && validation_context == :create
        errors.add(:b, 'bad') if b == 2 && validation_context == :update
        errors.add(:c, 'bad') if c == 3 && validation_context == :foo
      end
    end
  end

  it "should support :validation_context option to valid?" do
    @c.new(:c=>3).valid?.must_equal true
    @c.new(:c=>3).valid?(:validation_context=>:foo).must_equal false
  end

  it "should support :validation_context option to save?" do
    @c.new(:c=>3).save
    proc{@c.new(:c=>3).save(:validation_context=>:foo)}.must_raise Sequel::ValidationFailed
  end

  it "should raise error if using a validation context on a frozen model instance" do
    @c.new(:c=>3).freeze.valid?.must_equal true
    proc{@c.new(:c=>3).freeze.valid?(:validation_context=>:foo)}.must_raise RuntimeError, TypeError
  end
end