Skip to content

Commit

Permalink
add active in period for the requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiomagalhaes committed Nov 8, 2023
1 parent 8470df1 commit b81ac70
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/requirement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ class Requirement < ApplicationRecord
validates :coverage, presence: true
validates :start_date, presence: true
validates :end_date, presence: true

scope :active_in_period, ->(start_date, end_date) { where('start_date <= ? AND end_date >= ?', end_date, start_date) }
end
39 changes: 39 additions & 0 deletions spec/models/requirement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,43 @@
it { should belong_to(:profession) }
it { should have_many(:assignments) }
end

describe 'methods' do
context '#active_in_period' do
it 'should return true if dates are fully in the period' do
FactoryBot.create(:requirement, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(Requirement.active_in_period('2020-01-05', '2020-01-28').length).to eq(1)
end

it 'should return true if start date is in the period but the end date is out' do
FactoryBot.create(:requirement, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(Requirement.active_in_period('2020-01-05', '2020-02-28').length).to eq(1)
end

it 'should return true if start date is out of the period but the end date is in' do
FactoryBot.create(:requirement, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(Requirement.active_in_period('2019-01-05', '2020-02-28').length).to eq(1)
end

it 'should return true if both dates cover the start and end date' do
FactoryBot.create(:requirement, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(Requirement.active_in_period('2019-01-05', '2021-02-28').length).to eq(1)
end

it 'should return false if dates are fully out of the period' do
FactoryBot.create(:requirement, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(Requirement.active_in_period('2020-02-01', '2020-02-28').length).to eq(0)
end
end
end
end

0 comments on commit b81ac70

Please sign in to comment.