From 9042e252dc98168cf32ba3f604a64def0999ad7c Mon Sep 17 00:00:00 2001 From: adrian peralta Date: Mon, 12 Aug 2024 06:25:21 +0000 Subject: [PATCH] feat: #10 validate tiem is between 0 and 23 hours --- backend/app/models/availability.rb | 2 ++ backend/spec/models/availability_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 backend/spec/models/availability_spec.rb diff --git a/backend/app/models/availability.rb b/backend/app/models/availability.rb index 5a3d502..71eea8b 100644 --- a/backend/app/models/availability.rb +++ b/backend/app/models/availability.rb @@ -1,3 +1,5 @@ class Availability < ApplicationRecord belongs_to :engineer + + validates :time, inclusion: { in: 0..23, message: "must be between 0 and 23" } end diff --git a/backend/spec/models/availability_spec.rb b/backend/spec/models/availability_spec.rb new file mode 100644 index 0000000..107fcd2 --- /dev/null +++ b/backend/spec/models/availability_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe Availability, type: :model do + let(:engineer) { create(:engineer) } + + it "is valid with a time between 0 and 23" do + availability = Availability.new(engineer: engineer, week: "2024-01", day: "Monday", time: 10) + expect(availability).to be_valid + end + + it "is invalid with a time less than 0" do + availability = Availability.new(engineer: engineer, week: "2024-01", day: "Monday", time: -1) + expect(availability).to_not be_valid + expect(availability.errors[:time]).to include("must be between 0 and 23") + end + + it "is invalid with a time greater than 23" do + availability = Availability.new(engineer: engineer, week: "2024-01", day: "Monday", time: 24) + expect(availability).to_not be_valid + expect(availability.errors[:time]).to include("must be between 0 and 23") + end +end