Files
grecia/spec/models/poll/partial_result_spec.rb
Javi Martín 433b465ec7 Don't assign IDs to assignment records in tests
The test "updates officer_assignment_id_log if amount changes" failed
when the ID we assigned when creating the records was the same as the ID
of the first officer assignment created during that test. It's recently
happened while running our test suite [1] with the following error:

```
1) Poll::Recount logging changes updates officer_assignment_id_log if
amount changes
  Failure/Error: poll_recount.officer_assignment =
                  create(:poll_officer_assignment, id: 101)

     ActiveRecord::RecordNotUnique:
       PG::UniqueViolation: ERROR:  duplicate key value violates unique
       constraint "poll_officer_assignments_pkey"
       DETAIL:  Key (id)=(101) already exists.
```

We're also removing the IDs in the "updates officer_assignment_id_log if
amount changes" test to avoid any possible issues, even if in this test
I think no other officer assignments are created and so there can't be
another record with the same ID.

[1] https://github.com/consul/consul/runs/2818889296
2021-06-17 01:46:48 +02:00

88 lines
2.9 KiB
Ruby

require "rails_helper"
describe Poll::PartialResult do
describe "validations" do
it "validates that the answers are included in the Poll::Question's list" do
question = create(:poll_question)
create(:poll_question_answer, title: "One", question: question)
create(:poll_question_answer, title: "Two", question: question)
create(:poll_question_answer, title: "Three", question: question)
expect(build(:poll_partial_result, question: question, answer: "One")).to be_valid
expect(build(:poll_partial_result, question: question, answer: "Two")).to be_valid
expect(build(:poll_partial_result, question: question, answer: "Three")).to be_valid
expect(build(:poll_partial_result, question: question, answer: "Four")).not_to be_valid
end
end
describe "logging changes" do
it "updates amount_log if amount changes" do
partial_result = create(:poll_partial_result, amount: 33)
expect(partial_result.amount_log).to eq("")
partial_result.amount = 33
partial_result.save!
partial_result.amount = 32
partial_result.save!
partial_result.amount = 34
partial_result.save!
expect(partial_result.amount_log).to eq(":33:32")
end
it "updates officer_assignment_id_log if amount changes" do
partial_result = create(:poll_partial_result, amount: 33)
expect(partial_result.amount_log).to eq("")
expect(partial_result.officer_assignment_id_log).to eq("")
partial_result.amount = 33
first_assignment = create(:poll_officer_assignment)
partial_result.officer_assignment = first_assignment
partial_result.save!
partial_result.amount = 32
second_assignment = create(:poll_officer_assignment)
partial_result.officer_assignment = second_assignment
partial_result.save!
partial_result.amount = 34
partial_result.officer_assignment = create(:poll_officer_assignment)
partial_result.save!
expect(partial_result.amount_log).to eq(":33:32")
expect(partial_result.officer_assignment_id_log).to eq(
":#{first_assignment.id}:#{second_assignment.id}"
)
end
it "updates author_id if amount changes" do
partial_result = create(:poll_partial_result, amount: 33)
expect(partial_result.amount_log).to eq("")
expect(partial_result.author_id_log).to eq("")
author1 = create(:poll_officer).user
author2 = create(:poll_officer).user
author3 = create(:poll_officer).user
partial_result.amount = 33
partial_result.author_id = author1.id
partial_result.save!
partial_result.amount = 32
partial_result.author_id = author2.id
partial_result.save!
partial_result.amount = 34
partial_result.author_id = author3.id
partial_result.save!
expect(partial_result.amount_log).to eq(":33:32")
expect(partial_result.author_id_log).to eq(":#{author1.id}:#{author2.id}")
end
end
end