Skip to content

Commit

Permalink
Adds missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasjpr committed Aug 10, 2020
1 parent e27f42d commit e3240b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 10 additions & 6 deletions spec/validation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class UserModel
property alive : Bool
property childrens : Array(String)
property childrens_ages : Array(Int32)
property last_name : String

validation do
use UniqueRecordValidator, EmailValidator
Expand All @@ -38,6 +39,7 @@ class UserModel
validate alive, eq: true
validate childrens
validate childrens_ages, if: something?
validate last_name, presence: true, message: "Last name is required"

predicates do
def some?(value, compare) : Bool
Expand All @@ -50,7 +52,7 @@ class UserModel
end
end

def initialize(@email, @name, @age, @alive, @childrens, @childrens_ages)
def initialize(@email, @name, @age, @alive, @childrens, @childrens_ages, @last_name)
end

def something?
Expand All @@ -69,22 +71,23 @@ describe Schema::Validation do
25,
true,
["Child 1", "Child 2"],
[9, 12]
[9, 12],
""
)

context "with custom validator" do
it "it validates the user" do
subject.errors.clear
subject.valid?.should be_falsey
subject.errors.map(&.message).should eq ["Email must be valid!", "Must be 24 and 30 years old"]
subject.errors.map(&.message).should eq ["Email must be valid!", "Must be 24 and 30 years old", "Last name is required"]
end
end

context "with custom predicate" do
it "validates the user" do
subject.errors.clear
subject.valid?.should be_falsey
subject.errors.map(&.message).should eq ["Email must be valid!", "Must be 24 and 30 years old"]
subject.errors.map(&.message).should eq ["Email must be valid!", "Must be 24 and 30 years old", "Last name is required"]
end
end

Expand All @@ -95,14 +98,15 @@ describe Schema::Validation do
25,
true,
["Child 1", "Child 2"],
[9, 12]
[9, 12],
""
)

it "adds custom rules" do
subject.errors.clear
subject.add_custom_rule

subject.errors.size.should eq 3
subject.errors.size.should eq 4
subject.errors.map(&.message).should contain "fake error message"
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/validators_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ describe Schema::Validators do
it { exclude?(1, (0..3)).should be_false }
end

describe "#presense?" do
it { presence?("", nil).should be_false }
it { presence?(nil, nil).should be_false }
it { presence?("hello", nil).should be_true }
end

describe "#gte?" do
it { gte?(1, -1).should be_true }
it { gte?(1, 0).should be_true }
Expand Down

0 comments on commit e3240b1

Please sign in to comment.