forked from amberframework/granite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_spec.cr
95 lines (78 loc) · 2.42 KB
/
validator_spec.cr
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
require "../../spec_helper"
{% begin %}
{% adapter_literal = env("CURRENT_ADAPTER").id %}
class NameTest < Granite::Base
connection {{ adapter_literal }}
column id : Int64?, primary: true
column name : String?
validate :name, "cannot be blank", ->(s : NameTest) do
!s.name.to_s.blank?
end
end
class EmailTest < Granite::Base
connection {{ adapter_literal }}
column id : Int64?, primary: true
column email : String?
validate :email, "cannot be blank" do |email_test|
!email_test.email.to_s.blank?
end
end
class PasswordTest < Granite::Base
connection {{ adapter_literal }}
column id : Int64?, primary: true
column password : String?
column password_validation : String?
validate "password and validation should match" do |password_test|
password_test.password == password_test.password_validation
end
end
describe Granite::Validators do
describe "validates using proc" do
it "returns true if name is set" do
subject = NameTest.new
subject.name = "name"
subject.valid?.should eq true
end
it "returns false if name is blank" do
subject = NameTest.new
subject.name = ""
subject.valid?.should eq false
end
end
describe "validates using block" do
it "returns true if email is set" do
subject = EmailTest.new
subject.email = "test@example.com"
subject.valid?.should eq true
end
it "returns false if email is blank" do
subject = EmailTest.new
subject.email = ""
subject.valid?.should eq false
end
end
describe "validates using block without field" do
it "returns true if passwords match" do
subject = PasswordTest.new
subject.password = "123"
subject.password_validation = "123"
subject.valid?.should eq true
end
it "returns false if password does not match" do
subject = PasswordTest.new
subject.password = "123"
subject.password_validation = "1234"
subject.valid?.should eq false
end
end
describe "validates cleanly after previously failing" do
it "returns true if name is rectified after after failing" do
subject = NameTest.new
subject.name = ""
subject.valid?.should eq false
subject.name = "name"
subject.valid?.should eq true
end
end
end
{% end %}