Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validation bug on missions update #338 #339

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/enrollment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Enrollment < ApplicationRecord
validates_with EnrollmentValidators::CashRegisterProficiencyValidator
validates_with EnrollmentValidators::DatetimesInclusionValidator
validates_with EnrollmentValidators::UniquenessEnrollmentValidator, on: :create
validates_with EnrollmentValidators::AvailabilityPlaceValidator
validates_with EnrollmentValidators::AvailabilityPlaceValidator, on: :create
validates_with EnrollmentValidators::AvailabilitySlotValidator
validates_with EnrollmentValidators::DurationValidator
validates_with EnrollmentValidators::MatchingMissionTimeSlotsValidator
Expand Down
23 changes: 23 additions & 0 deletions spec/models/enrollment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
end
end

describe 'validations' do
context 'when a new enrollment is created where mission is full' do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] is created on a full mission
might be better ?

it 'raises an error' do
mission = create :mission, max_member_count: 4
enroll_n_members_on_mission(mission, 4)
extra_enrollment = Enrollment.new(mission: mission)

expect(extra_enrollment).to_not be_valid
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a method helper that is used only once in that file (meaning, it only moves the logic elsewhere), I think this setup can be done directly under this context. You could either create a new trait in the mission factory, or directly set the setup here in a let. Here, the let directly illustrates what is the actual context in terms of code

Suggested change
it 'raises an error' do
mission = create :mission, max_member_count: 4
enroll_n_members_on_mission(mission, 4)
extra_enrollment = Enrollment.new(mission: mission)
expect(extra_enrollment).to_not be_valid
end
let(:full_mission) do
create :mission, max_member_count: 4 do |mission|
create_list :enrollment,
4,
mission: mission,
start_time: mission.start_time,
end_time: mission.start_date + 3.hours
end
end
end
it 'is invalid' do
extra_enrollment = Enrollment.new(mission: full_mission)
expect(extra_enrollment).to_not be_valid
end

Also, it's better to be specific if we can on what error is raised. Here, it's an invalidation

end
end

describe '#duration' do
subject(:create_enrollment) do
create :enrollment, start_time: mission.start_date, end_time: mission.start_date + 2.hours, mission: mission
Expand Down Expand Up @@ -58,4 +70,15 @@
end
end
end

def enroll_n_members_on_mission(mission, members_count)
members = create_list :member, members_count
members.each do |member|
create :enrollment,
member: member,
mission: mission,
start_time: mission.start_date,
end_time: mission.start_date + 3.hours
end
end
end