Files
nairobi/spec/models/poll/shift_spec.rb
Javi Martín 7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00

121 lines
3.8 KiB
Ruby

require "rails_helper"
describe Poll::Shift do
let(:poll) { create(:poll) }
let(:booth) { create(:poll_booth) }
let(:user) { create(:user, username: "Ana", email: "ana@example.com") }
let(:officer) { create(:poll_officer, user: user) }
let(:recount_shift) { build(:poll_shift, booth: booth, officer: officer, date: Date.current, task: :recount_scrutiny) }
describe "validations" do
let(:shift) { build(:poll_shift) }
it "is valid" do
expect(shift).to be_valid
end
it "is not valid without a booth" do
shift.booth = nil
expect(shift).not_to be_valid
end
it "is not valid without an officer" do
shift.officer = nil
expect(shift).not_to be_valid
end
it "is not valid without a date" do
shift.date = nil
expect(shift).not_to be_valid
end
it "is not valid without a task" do
shift.task = nil
expect(shift).not_to be_valid
end
it "is not valid with same booth, officer, date and task" do
recount_shift.save!
expect(build(:poll_shift, booth: booth, officer: officer, date: Date.current, task: :recount_scrutiny)).not_to be_valid
end
it "is valid with same booth, officer and date but different task" do
recount_shift.save!
expect(build(:poll_shift, booth: booth, officer: officer, date: Date.current, task: :vote_collection)).to be_valid
end
it "is valid with same booth, officer and task but different date" do
recount_shift.save!
expect(build(:poll_shift, booth: booth, officer: officer, date: Date.tomorrow, task: :recount_scrutiny)).to be_valid
end
end
describe "officer_assignments" do
it "creates and destroy corresponding officer_assignments" do
booth_assignment1 = create(:poll_booth_assignment, booth: booth)
booth_assignment2 = create(:poll_booth_assignment, booth: booth)
expect { create(:poll_shift, booth: booth, officer: officer, date: Date.current) }.to change { Poll::OfficerAssignment.all.count }.by(2)
officer_assignments = Poll::OfficerAssignment.all
oa1 = officer_assignments.first
oa2 = officer_assignments.second
expect(oa1.officer).to eq(officer)
expect(oa1.date).to eq(Date.current)
expect(oa1.booth_assignment).to eq(booth_assignment1)
expect(oa1.final).to be_falsey
expect(oa2.officer).to eq(officer)
expect(oa2.date).to eq(Date.current)
expect(oa2.booth_assignment).to eq(booth_assignment2)
expect(oa2.final).to be_falsey
create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment1, date: Date.tomorrow)
expect { Poll::Shift.last.destroy }.to change { Poll::OfficerAssignment.all.count }.by(-2)
end
it "creates final officer_assignments" do
booth_assignment = create(:poll_booth_assignment, poll: poll, booth: booth)
recount_shift.save!
officer_assignments = Poll::OfficerAssignment.all
expect(officer_assignments.count).to eq(1)
officer_assignment = officer_assignments.first
expect(officer_assignment.officer).to eq(officer)
expect(officer_assignment.date).to eq(Date.current)
expect(officer_assignment.booth_assignment).to eq(booth_assignment)
expect(officer_assignment.final).to be_truthy
end
end
describe "#persist_data" do
let(:shift) { create(:poll_shift, officer: officer, booth: booth) }
it "maintains officer data after destroying associated user" do
shift.officer.user.destroy!
expect(shift.officer_name).to eq "Ana"
expect(shift.officer_email).to eq "ana@example.com"
end
it "maintains officer data after destroying officer role" do
shift.officer.destroy!
expect(shift.officer_name).to eq "Ana"
expect(shift.officer_email).to eq "ana@example.com"
end
end
end