From 2153bd9e98f5bd974bad10995f1003e4294d64f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 7 Sep 2022 18:57:41 +0200 Subject: [PATCH] Add tests for current/expired/recounting polls We didn't have model tests for some of these scopes, and neither scope tests nor instance method tests covered edge cases like the current date. --- spec/factories/polls.rb | 5 --- spec/models/poll/poll_spec.rb | 83 ++++++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/spec/factories/polls.rb b/spec/factories/polls.rb index fe98957cc..c55101c31 100644 --- a/spec/factories/polls.rb +++ b/spec/factories/polls.rb @@ -22,11 +22,6 @@ FactoryBot.define do ends_at { 2.months.ago } end - trait :recounting do - starts_at { 1.month.ago } - ends_at { Date.current } - end - trait :published do published { true } end diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index 10f4b03ce..c7b4656cf 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -86,17 +86,31 @@ describe Poll do end end - describe "#opened?" do + describe "#current?" do it "returns true only when it isn't too late" do - expect(create(:poll, :expired)).not_to be_current - expect(create(:poll)).to be_current + about_to_start = create(:poll, starts_at: Date.tomorrow) + just_started = create(:poll, starts_at: Date.current) + about_to_end = create(:poll, ends_at: Date.current) + just_ended = create(:poll, ends_at: Date.yesterday) + + expect(just_started).to be_current + expect(about_to_end).to be_current + expect(about_to_start).not_to be_current + expect(just_ended).not_to be_current end end describe "#expired?" do it "returns true only when it is too late" do - expect(create(:poll, :expired)).to be_expired - expect(create(:poll)).not_to be_expired + about_to_start = create(:poll, starts_at: Date.tomorrow) + about_to_end = create(:poll, ends_at: Date.current) + just_ended = create(:poll, ends_at: Date.yesterday) + recounting_ended = create(:poll, starts_at: 3.years.ago, ends_at: 2.years.ago) + + expect(just_ended).to be_expired + expect(recounting_ended).to be_expired + expect(about_to_start).not_to be_expired + expect(about_to_end).not_to be_expired end end @@ -311,30 +325,65 @@ describe Poll do end describe "scopes" do + describe ".current" do + it "returns polls which have started but not ended" do + about_to_start = create(:poll, starts_at: Date.tomorrow) + just_started = create(:poll, starts_at: Date.current) + about_to_end = create(:poll, ends_at: Date.current) + just_ended = create(:poll, ends_at: Date.yesterday) + + current_polls = Poll.current + + expect(current_polls).to match_array [just_started, about_to_end] + expect(current_polls).not_to include(about_to_start) + expect(current_polls).not_to include(just_ended) + end + end + + describe ".expired" do + it "returns polls which have already ended" do + about_to_start = create(:poll, starts_at: Date.tomorrow) + about_to_end = create(:poll, ends_at: Date.current) + just_ended = create(:poll, ends_at: Date.yesterday) + recounting_ended = create(:poll, starts_at: 3.years.ago, ends_at: 2.years.ago) + + expired_polls = Poll.expired + + expect(expired_polls).to match_array [just_ended, recounting_ended] + expect(expired_polls).not_to include(about_to_start) + expect(expired_polls).not_to include(about_to_end) + end + end + describe ".recounting" do it "returns polls in recount & scrutiny phase" do - current = create(:poll, :current) - expired = create(:poll, :expired) - recounting = create(:poll, :recounting) + about_to_start = create(:poll, starts_at: Date.tomorrow) + about_to_end = create(:poll, ends_at: Date.tomorrow) + just_ended = create(:poll, ends_at: Date.yesterday) + recounting_ended = create(:poll, starts_at: 3.years.ago, ends_at: 2.years.ago) recounting_polls = Poll.recounting - expect(recounting_polls).to eq [recounting] - expect(recounting_polls).not_to include(current) - expect(recounting_polls).not_to include(expired) + expect(recounting_polls).to eq [just_ended] + expect(recounting_polls).not_to include(about_to_start) + expect(recounting_polls).not_to include(about_to_end) + expect(recounting_polls).not_to include(recounting_ended) end end describe ".current_or_recounting" do it "returns current or recounting polls" do - current = create(:poll, :current) - expired = create(:poll, :expired) - recounting = create(:poll, :recounting) + about_to_start = create(:poll, starts_at: Date.tomorrow) + just_started = create(:poll, starts_at: Date.current) + about_to_end = create(:poll, ends_at: Date.current) + just_ended = create(:poll, ends_at: Date.yesterday) + recounting_ended = create(:poll, starts_at: 3.years.ago, ends_at: 2.years.ago) current_or_recounting = Poll.current_or_recounting - expect(current_or_recounting).to match_array [current, recounting] - expect(current_or_recounting).not_to include(expired) + expect(current_or_recounting).to match_array [just_started, about_to_end, just_ended] + expect(current_or_recounting).not_to include(about_to_start) + expect(current_or_recounting).not_to include(recounting_ended) end end @@ -425,7 +474,7 @@ describe Poll do end it "is false for recounting polls" do - poll = create(:poll, :recounting) + poll = create(:poll, ends_at: Date.yesterday) expect(poll.recounts_confirmed?).to be false end