Skip to content

Commit

Permalink
chore: update format spec to be in line with model validator specs
Browse files Browse the repository at this point in the history
  • Loading branch information
adzap committed Mar 24, 2023
1 parent 7504074 commit fdbf3ff
Showing 1 changed file with 21 additions and 45 deletions.
66 changes: 21 additions & 45 deletions spec/validates_timeliness/validator/format_spec.rb
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

0 comments on commit fdbf3ff

Please sign in to comment.