Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
simfeld committed Mar 9, 2024
1 parent a90453e commit 5b3ec9b
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions spec/models/event/course_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,105 @@ def person(name, role)
expect(event).to_not be_attr_used(:globally_visible)
end
end

context 'validations' do
let(:kind_with_validation) { Fabricate(:event_kind, validate_course_number: true) }
let(:kind_without_validation) { event_kinds(:lpk) }

context 'course number' do
VALID_NUMBERS = ['PBS CH 947-00', 'PBS CH XX 007-99', 'PBS CH AB 123-45'].freeze
INVALID_NUMBERS = ['PBS CH 000-FP', 'PBS XY XX 475-31', 'CH AB 123-45', 'PBS CH 123-456',
'PBS CH AB 12-345', 'PBSCHAB123-45'].freeze

subject { Fabricate(:course, groups: [groups(:be)], kind: kind_with_validation, number: 'PBS CH BE 123-24') }

VALID_NUMBERS.each do |number|
it "'#{number}' is valid" do
subject.number = number
expect(subject.valid?).to be_truthy
end
end

INVALID_NUMBERS.each do |number|
it "'#{number}' is invalid" do
subject.number = number
expect(subject.valid?).to be_falsey
expect(subject.errors[:number]).to be_present
end
end
end

context 'when updating an existing event' do
subject { event }

context 'with event kind having validation enabled' do
before do
subject.kind = kind_with_validation
# fixture has an invalid course number, so we need to skip validations
subject.save(validate: false)
end

it 'is invalid for a number not matching the expected format' do
subject.number = '1234'
expect(subject.valid?).to be_falsey
expect(subject.errors[:number]).to be_present
end

it 'is valid for a number matching the expected format' do
subject.number = 'PBS CH BE 123-24'
expect(subject.valid?).to be_truthy
end

it 'is valid when switching to an event kind with validation disabled' do
subject.kind = kind_without_validation
expect(subject.valid?).to be_truthy
end
end

context 'with event kind having validation disabled' do
before do
subject.kind = kind_without_validation
subject.save
end

it 'is valid for a number not matching the expected format' do
subject.number = '1234'
expect(subject.valid?).to be_truthy
end

it 'is invalid when switching to an event kind with validation enabled' do
subject.kind = kind_with_validation
expect(subject.valid?).to be_falsey
expect(subject.errors[:number]).to be_present
end
end
end

context 'when creating a new event' do
subject { Event::Course.new(groups: [groups(:be)],
name: Faker::Lorem.word,
dates: [Event::Date.new(start_at: Time.zone.now)]) }

it 'is valid for an event kind with validation enabled and a valid number' do
subject.kind = kind_with_validation
subject.number = 'PBS CH BE 123-24'
subject.validate!
puts subject.errors.full_messages
expect(subject.valid?).to be_truthy
end

it 'is invalid for an event kind with validation enabled and an invalid number' do
subject.kind = kind_with_validation
subject.number = '1234'
expect(subject.valid?).to be_falsey
expect(subject.errors[:number]).to be_present
end

it 'is valid for an event kind with validation disabled and an invalid number' do
subject.kind = kind_without_validation
subject.number = '1234'
expect(subject.valid?).to be_truthy
end
end
end
end

0 comments on commit 5b3ec9b

Please sign in to comment.