From 42a25ded3f5cbc2f409cc9b3f2fa5b460131fff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 11 Apr 2019 15:59:26 +0200 Subject: [PATCH] Fix booth recounts poll stats discrepancies Due to technical issues, sometimes users voted in booths and their vote couldn't be added to the database. So we're including them in the users with no demographic data. --- app/models/poll/stats.rb | 12 ++++++++++++ spec/models/poll/stats_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index d7c9826c5..d7e0031da 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -89,6 +89,10 @@ class Poll::Stats end end + def total_no_demographic_data + super + total_unregistered_booth + end + private def participant_ids @@ -103,6 +107,14 @@ class Poll::Stats @recounts ||= poll.recounts end + def total_registered_booth + voters.where(origin: "booth").count + end + + def total_unregistered_booth + [total_participants_booth - total_registered_booth, 0].max + end + stats_cache(*stats_methods) def stats_cache(key, &block) diff --git a/spec/models/poll/stats_spec.rb b/spec/models/poll/stats_spec.rb index 0f0957dd5..8f8b3583d 100644 --- a/spec/models/poll/stats_spec.rb +++ b/spec/models/poll/stats_spec.rb @@ -186,6 +186,34 @@ describe Poll::Stats do end end + describe "#total_no_demographic_data" do + before do + create(:poll_voter, :from_web, poll: poll, user: create(:user, :level_two, gender: nil)) + end + + context "more registered participants than participants in recounts" do + before do + create(:poll_recount, :from_booth, poll: poll, total_amount: 1) + 2.times { create(:poll_voter, :from_booth, poll: poll) } + end + + it "returns registered users with no demographic data" do + expect(stats.total_no_demographic_data).to eq 1 + end + end + + context "more participants in recounts than registered participants" do + before do + create(:poll_recount, :from_booth, poll: poll, total_amount: 3) + 2.times { create(:poll_voter, :from_booth, poll: poll) } + end + + it "returns registered users with no demographic data plus users not registered" do + expect(stats.total_no_demographic_data).to eq 2 + end + end + end + describe "#channels" do context "no participants" do it "returns no channels" do