Simplify poll stats test

Adding the option to assign a poll to a poll recount factory meant we
didn't need to create so much data.

Also note we're removing the `create(:poll_voter, origin: "booth")`
code, since it isn't used in the stats calculations.
This commit is contained in:
Javi Martín
2019-01-11 20:52:39 +01:00
parent 4830b563ea
commit 4a3115607a
2 changed files with 24 additions and 10 deletions

View File

@@ -88,7 +88,12 @@ FactoryBot.define do
poll poll
association :user, :level_two association :user, :level_two
association :officer, factory: :poll_officer association :officer, factory: :poll_officer
origin "web" from_web
trait :from_web do
origin "web"
token SecureRandom.hex(32)
end
trait :from_booth do trait :from_booth do
origin "booth" origin "booth"
@@ -129,6 +134,16 @@ FactoryBot.define do
factory :poll_recount, class: "Poll::Recount" do factory :poll_recount, class: "Poll::Recount" do
association :author, factory: :user association :author, factory: :user
origin "web" origin "web"
trait :from_booth do
origin "booth"
transient { poll nil }
booth_assignment do
association :poll_booth_assignment, poll: poll
end
end
end end
factory :officing_residence, class: "Officing::Residence" do factory :officing_residence, class: "Officing::Residence" do

View File

@@ -2,16 +2,15 @@ require "rails_helper"
describe Poll::Stats do describe Poll::Stats do
describe "Calculate stats" do describe "#generate" do
it "Generate the correct stats" do it "generates the correct stats" do
poll = create(:poll) poll = create(:poll)
booth = create(:poll_booth) 2.times { create(:poll_voter, :from_web, poll: poll) }
booth_assignment = create(:poll_booth_assignment, poll: poll, booth: booth) 3.times { create(:poll_voter, :from_booth, poll: poll) }
create(:poll_voter, poll: poll, origin: "web") create(:poll_recount, :from_booth, poll: poll,
3.times {create(:poll_voter, poll: poll, origin: "booth")} white_amount: 1, null_amount: 0, total_amount: 2)
create(:poll_voter, poll: poll)
create(:poll_recount, origin: "booth", white_amount: 1, null_amount: 0, total_amount: 2, booth_assignment_id: booth_assignment.id) stats = Poll::Stats.new(poll).generate
stats = described_class.new(poll).generate
expect(stats[:total_participants]).to eq(5) expect(stats[:total_participants]).to eq(5)
expect(stats[:total_participants_web]).to eq(2) expect(stats[:total_participants_web]).to eq(2)