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
|
require_relative "spec_helper"
describe "pg_auto_validate_enums plugin" do
before do
@db = Sequel.mock(:host=>'postgres')
def @db.schema(table, *)
table = table.first_source if table.is_a?(Sequel::Dataset)
[[:c, {:enum_values=>["a", "b", table.to_s]}]]
end
@c = Sequel::Model(@db[:test])
@c.plugin :pg_auto_validate_enums
@o = @c.new
end
it "validates enum values automatically" do
@o.c = "a"
@o.valid?.must_equal true
@o.c = "b"
@o.valid?.must_equal true
@o.c = "test"
@o.valid?.must_equal true
@o.c = "c"
@o.valid?.must_equal false
@o.errors.must_equal({:c => ['is not in range or set: ["a", "b", "test"]']})
end
it "handles case where no enum columns exist in model's table" do
@db = Sequel.mock(:host=>'postgres')
def @db.schema(table, *)
table = table.first_source if table.is_a?(Sequel::Dataset)
[[:c, {}]]
end
@c = Sequel::Model(@db[:test])
@c.plugin :pg_auto_validate_enums
@o = @c.new
@o.c = "a"
@o.valid?.must_equal true
end
it "validates underlying enum value, not method return value" do
@o.c = "a"
def @o.c; "invalid" end
@o.valid?.must_equal true
end
it "treats plugin options as validates_includes options for validations" do
@c.plugin :pg_auto_validate_enums, :message=>"is not valid"
@o.c = "c"
@o.valid?.must_equal false
@o.errors.must_equal({:c => ['is not valid']})
end
it "reloads enum_values metadata when dataset is changed" do
@c.dataset = :test2
@o.c = "test2"
@o.valid?.must_equal true
@o.c = "test"
@o.valid?.must_equal false
@o.errors.must_equal({:c => ['is not in range or set: ["a", "b", "test2"]']})
end
it "works when subclassing" do
c = Sequel::Model(@db)
c.plugin :pg_auto_validate_enums
sc = c::Model(:test2)
o = sc.new
o.c = "a"
o.valid?.must_equal true
o.c = "b"
o.valid?.must_equal true
o.c = "test2"
o.valid?.must_equal true
o.c = "c"
o.valid?.must_equal false
o.errors.must_equal({:c => ['is not in range or set: ["a", "b", "test2"]']})
end
it "exposes metadata in .pg_auto_validate_enums_metadata" do
@c.pg_auto_validate_enums_metadata.must_equal(:c => ["a", "b", "test"])
end
it "exposes options in .pg_auto_validate_enums_opts" do
@c.pg_auto_validate_enums_opts.must_equal(:allow_nil => true, :from => :values)
@c.plugin :pg_auto_validate_enums, :message=>"is not valid"
@c.pg_auto_validate_enums_opts.must_equal(:allow_nil => true, :message => "is not valid", :from => :values)
end
end
|