From 433b465ec7c336b50640b362f24a9c21a8876a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 14 Jun 2021 12:45:45 +0200 Subject: [PATCH 1/2] 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 --- spec/models/poll/partial_result_spec.rb | 12 ++++++++---- spec/models/poll/recount_spec.rb | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/spec/models/poll/partial_result_spec.rb b/spec/models/poll/partial_result_spec.rb index f53d6f522..b5a045329 100644 --- a/spec/models/poll/partial_result_spec.rb +++ b/spec/models/poll/partial_result_spec.rb @@ -39,19 +39,23 @@ describe Poll::PartialResult do expect(partial_result.officer_assignment_id_log).to eq("") partial_result.amount = 33 - partial_result.officer_assignment = create(:poll_officer_assignment, id: 10) + first_assignment = create(:poll_officer_assignment) + partial_result.officer_assignment = first_assignment partial_result.save! partial_result.amount = 32 - partial_result.officer_assignment = create(:poll_officer_assignment, id: 20) + 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, id: 30) + 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(":10:20") + expect(partial_result.officer_assignment_id_log).to eq( + ":#{first_assignment.id}:#{second_assignment.id}" + ) end it "updates author_id if amount changes" do diff --git a/spec/models/poll/recount_spec.rb b/spec/models/poll/recount_spec.rb index fae60fb85..11646edd8 100644 --- a/spec/models/poll/recount_spec.rb +++ b/spec/models/poll/recount_spec.rb @@ -58,19 +58,23 @@ describe Poll::Recount do expect(poll_recount.officer_assignment_id_log).to eq("") poll_recount.white_amount = 33 - poll_recount.officer_assignment = create(:poll_officer_assignment, id: 101) + second_assignment = create(:poll_officer_assignment) + poll_recount.officer_assignment = second_assignment poll_recount.save! poll_recount.white_amount = 32 - poll_recount.officer_assignment = create(:poll_officer_assignment, id: 102) + third_assignment = create(:poll_officer_assignment) + poll_recount.officer_assignment = third_assignment poll_recount.save! poll_recount.white_amount = 34 - poll_recount.officer_assignment = create(:poll_officer_assignment, id: 103) + poll_recount.officer_assignment = create(:poll_officer_assignment) poll_recount.save! expect(poll_recount.white_amount_log).to eq(":0:33:32") - expect(poll_recount.officer_assignment_id_log).to eq(":#{officer_assignment.id}:101:102") + expect(poll_recount.officer_assignment_id_log).to eq( + ":#{officer_assignment.id}:#{second_assignment.id}:#{third_assignment.id}" + ) end it "updates author_id if amount changes" do From 5f747122c5415708b91df60c06ee1f77e849f1e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 14 Jun 2021 17:10:20 +0200 Subject: [PATCH 2/2] Don't assign IDs to user records in tests This test will fail if the ID of the created record is the same as the ID of the first user we create in the test. The chance is very low due to the `rand(9999999)` which causes the test to fail just once every ten million times. However, why assigning the ID in the first place? Without it, the test will never fail due to conflicting IDs. --- spec/system/officing/voters_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/officing/voters_spec.rb b/spec/system/officing/voters_spec.rb index 433ec10a5..9ab626784 100644 --- a/spec/system/officing/voters_spec.rb +++ b/spec/system/officing/voters_spec.rb @@ -160,7 +160,7 @@ describe "Voters" do end scenario "Store officer and booth information" do - create(:user, :in_census, id: rand(9999999)) + create(:user, :in_census) poll1 = create(:poll, name: "¿Quieres que XYZ sea aprobado?") poll2 = create(:poll, name: "Pregunta de votación de prueba")