Skip to content

Commit

Permalink
Support multiple charismatic election dates
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoWill committed Apr 18, 2023
1 parent df0cb41 commit 107c9cb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
29 changes: 20 additions & 9 deletions polling_stations/apps/data_finder/helpers/every_election.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,29 @@ def get_future_election_dates(self):

def _get_next_election_date(self):
ballots = self.get_all_ballots()
next_charismatic_election_date = getattr(
settings, "NEXT_CHARISMATIC_ELECTION_DATE", None
)
# if no ballots, return early
if len(ballots) == 0:
return next_charismatic_election_date
return None

next_charismatic_election_dates = getattr(
settings, "NEXT_CHARISMATIC_ELECTION_DATES", []
)
next_charismatic_election_dates.sort()
dates = [datetime.strptime(b["poll_open_date"], "%Y-%m-%d") for b in ballots]
dates.sort()
return (
next_charismatic_election_date
if next_charismatic_election_date
else dates[0].strftime("%Y-%m-%d")
)

if next_charismatic_election_dates:
# If we have some dates return the first one that is in NEXT_CHARISMATIC_ELECTION_DATES
for date in dates:
if date in next_charismatic_election_dates:
return date
# If none of them are in NEXT_CHARISMATIC_ELECTION_DATES,
# return the earliest charismatic election date
return next_charismatic_election_dates[0]

# If we haven't set NEXT_CHARISMATIC_ELECTION_DATES,
# return the election
return dates[0].strftime("%Y-%m-%d")

def get_ballots_for_next_date(self):
if not self.request_success:
Expand Down
36 changes: 18 additions & 18 deletions polling_stations/apps/data_finder/tests/test_ee_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get_data_with_elections(self, query_url):
class EveryElectionWrapperTests(TestCase):
@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch("data_finder.helpers.EveryElectionWrapper.get_data", get_data_exception)
def test_exception(self):
Expand All @@ -133,7 +133,7 @@ def test_exception(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data", get_data_no_elections
Expand All @@ -150,7 +150,7 @@ def test_no_elections(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data", get_data_with_elections
Expand All @@ -169,16 +169,16 @@ def test_elections(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=(datetime.now() + timedelta(days=1)).strftime(
"%Y-%m-%d"
),
NEXT_CHARISMATIC_ELECTION_DATES=[
(datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d")
],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data", get_data_with_elections
)
def test_non_charismatic_elections(self):
# there are upcoming elections
# but they aren't on NEXT_CHARISMATIC_ELECTION_DATE
# but they aren't in NEXT_CHARISMATIC_ELECTION_DATES
ee = EveryElectionWrapper(postcode="AA11AA")
self.assertTrue(ee.request_success)
self.assertFalse(ee.has_election())
Expand All @@ -190,7 +190,7 @@ def test_non_charismatic_elections(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data", get_data_only_group
Expand All @@ -207,7 +207,7 @@ def test_elections_only_group(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data", get_data_group_and_ballot
Expand All @@ -224,7 +224,7 @@ def test_elections_group_and_ballot(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": False},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data", get_data_group_and_ballot
Expand All @@ -240,7 +240,7 @@ def test_settings_override_false(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": False},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data", get_data_only_group
Expand All @@ -257,7 +257,7 @@ def test_settings_override_true(self):
@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
ELECTION_BLACKLIST=["foo.bar.baz.date"],
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data", get_data_with_elections
Expand All @@ -271,7 +271,7 @@ def test_some_blacklisted(self):
@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
ELECTION_BLACKLIST=["foo.bar.baz.date", "foo.bar.qux.date"],
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data", get_data_with_elections
Expand Down Expand Up @@ -429,7 +429,7 @@ def get_data_one_cancelled_ballot_with_metadata(self, query_url):
class CancelledElectionTests(TestCase):
@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data",
Expand All @@ -448,7 +448,7 @@ def test_two_ballots_one_cancelled(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data",
Expand All @@ -467,7 +467,7 @@ def test_two_ballots_both_cancelled(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data",
Expand All @@ -486,7 +486,7 @@ def test_one_cancelled_ballot_no_replacement(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data",
Expand All @@ -508,7 +508,7 @@ def test_one_cancelled_ballot_with_replacement(self):

@override_settings(
EVERY_ELECTION={"CHECK": True, "HAS_ELECTION": True},
NEXT_CHARISMATIC_ELECTION_DATE=None,
NEXT_CHARISMATIC_ELECTION_DATES=[],
)
@mock.patch(
"data_finder.helpers.EveryElectionWrapper.get_data",
Expand Down

0 comments on commit 107c9cb

Please sign in to comment.