forked from adzap/validates_timeliness
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update format spec to be in line with model validator specs
- Loading branch information
Showing
1 changed file
with
21 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,57 @@ | ||
class FormatTestModel | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
include ActiveModel::Validations | ||
include ValidatesTimeliness::ORM::ActiveModel | ||
|
||
attribute :date, :date, default: Date.new | ||
attribute :time, :time, default: Time.new | ||
attribute :datetime, :time, default: DateTime.new | ||
|
||
validates :date, timeliness: {type: :date, format: "yyyy-mm-dd"} | ||
validates :time, timeliness: {type: :time, format: "hh:nn:ss"} | ||
validates :datetime, timeliness: {type: :datetime, format: "yyyy-mm-dd hh:nn:ss"} | ||
end | ||
|
||
RSpec.describe ValidatesTimeliness::Validator, ":format option" do | ||
with_config(:use_plugin_parser, true) | ||
|
||
describe "for date type" do | ||
it "should not be valid for string given in the wrong format" do | ||
model = FormatTestModel.new(date: "01-01-2010") | ||
before do | ||
Person.validates_date :birth_date, format: "yyyy-mm-dd" | ||
end | ||
|
||
expect(model).to_not be_valid | ||
expect(model.errors.messages_for(:date)).to eq(["is not a valid date"]) | ||
it "should not be valid for string given in the wrong format" do | ||
invalid!(:birth_date, '23/12/2023', /is not a valid date/) | ||
end | ||
|
||
it "should be valid for string given in the right format" do | ||
model = FormatTestModel.new(date: "2010-01-01") | ||
|
||
expect(model).to be_valid, -> { model.errors.full_messages.join("\n") } | ||
valid!(:birth_date, '2023-12-23') | ||
end | ||
|
||
it "should be valid for date instance" do | ||
model = FormatTestModel.new(date: Date.new(2010, 1, 1)) | ||
|
||
expect(model).to be_valid, -> { model.errors.full_messages.join("\n") } | ||
valid!(:birth_date, Date.new(2022,12,23)) | ||
end | ||
end | ||
|
||
describe "for time type" do | ||
it "should not be valid for string given in the wrong format" do | ||
model = FormatTestModel.new(time: "00-00-00") | ||
before do | ||
Person.validates_time :birth_time, format: "hh:nn:ss" | ||
end | ||
|
||
expect(model).to_not be_valid | ||
expect(model.errors.messages_for(:time)).to eq(["is not a valid time"]) | ||
it "should not be valid for string given in the wrong format" do | ||
invalid!(:birth_time, "00-00-00", /is not a valid time/) | ||
end | ||
|
||
it "should be valid for string given in the right format" do | ||
model = FormatTestModel.new(time: "00:00:00") | ||
|
||
expect(model).to be_valid, -> { model.errors.full_messages.join("\n") } | ||
valid!(:birth_time, "00:00:00") | ||
end | ||
|
||
it "should be valid for date instance" do | ||
model = FormatTestModel.new(time: Time.new(2010, 1, 1, 0, 0, 0)) | ||
|
||
expect(model).to be_valid, -> { model.errors.full_messages.join("\n") } | ||
valid!(:birth_time, Time.new(2010, 1, 1, 0, 0, 0)) | ||
end | ||
end | ||
|
||
describe "for datetime type" do | ||
it "should not be valid for string given in the wrong format" do | ||
model = FormatTestModel.new(datetime: "01-01-2010 00-00-00") | ||
before do | ||
Person.validates_datetime :birth_datetime, format: "yyyy-mm-dd hh:nn:ss" | ||
end | ||
|
||
expect(model).to_not be_valid | ||
expect(model.errors.messages_for(:datetime)).to eq(["is not a valid datetime"]) | ||
it "should not be valid for string given in the wrong format" do | ||
invalid!(:birth_datetime, "01-01-2010 00-00-00", /is not a valid datetime/) | ||
end | ||
|
||
it "should be valid for string given in the right format" do | ||
model = FormatTestModel.new(datetime: "2010-01-01 00:00:00") | ||
|
||
expect(model).to be_valid, -> { model.errors.full_messages.join("\n") } | ||
valid!(:birth_datetime, "2010-01-01 00:00:00") | ||
end | ||
|
||
it "should be valid for date instance" do | ||
model = FormatTestModel.new(datetime: DateTime.new(2010, 1, 1, 0, 0, 0)) | ||
|
||
expect(model).to be_valid, -> { model.errors.full_messages.join("\n") } | ||
valid!(:birth_datetime, DateTime.new(2010, 1, 1, 0, 0, 0)) | ||
end | ||
end | ||
end |