Skip to content

Commit

Permalink
add some tests for registration cards
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Oct 30, 2024
1 parent 0b8fb4a commit 787304a
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 49 deletions.
50 changes: 50 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
import uvicorn
from app import app
from starlette.testclient import TestClient
from template_sorter import (
ApiModes,
ElectionDateTemplateSorter,
TemplateSorter,
)
from uk_election_timetables.calendars import Country
from uk_election_timetables.election_ids import from_election_id


@pytest.fixture(scope="function")
Expand Down Expand Up @@ -33,3 +40,46 @@ def uvicorn_server():
time.sleep(0.3)
yield f"http://localhost:{port}"
proc.kill()


@pytest.fixture
def template_sorter():
def get_template_sorter(mock_response, date):
api_response = mock_response
# The dates exist in the api_response, but not
# in the format that matches the RootBuilder:
# dates = api_response._values["dates"] vs
# dates = api_response.dates so I've set it here.
api_response.dates = api_response._values["dates"]
mode = ApiModes.UPCOMING_ELECTIONS

sorter = TemplateSorter(
api_response=api_response, mode=mode, current_date=date
)
sorter.country = Country.ENGLAND
sorter.dates = api_response.dates

return sorter

return get_template_sorter


@pytest.fixture
def election_date_template_sorter():
def get_election_date_template_sorter(template_sorter, date):
election_date_sorter = ElectionDateTemplateSorter(
date_data=date,
country=template_sorter.country,
current_date=template_sorter.current_date,
response_type=template_sorter.response_type,
)
election_date_sorter.current_date = template_sorter.current_date

election_date_sorter.timetable = from_election_id(
template_sorter.dates[0].ballots[0].election_id,
country=template_sorter.country,
)

return election_date_sorter

return get_election_date_template_sorter
77 changes: 77 additions & 0 deletions tests/test_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import datetime

from freezegun import freeze_time
from mock_responses import (
CITY_OF_LONDON_COUNCIL_AND_PARL_DIFFERENT_DAYS,
CITY_OF_LONDON_COUNCIL_AND_PARL_SAME_DAY,
SINGLE_LOCAL_FUTURE_BALLOT_WITH_POLLING_STATION,
)
from template_sorter import (
CityOfLondonRegistrationDateSection,
RegistrationDateSection,
)


def _sections_of_type(sections, type_):
return [s for s in sections if type(s) == type_]


@freeze_time("2024-04-16")
def test_single_ballot_not_city_of_london(
template_sorter, election_date_template_sorter
):
single_ballot_sorter_before_deadline = template_sorter(
SINGLE_LOCAL_FUTURE_BALLOT_WITH_POLLING_STATION,
date=datetime.date(2024, 3, 16),
)
single_election_date_template_sorter = election_date_template_sorter(
single_ballot_sorter_before_deadline,
single_ballot_sorter_before_deadline.dates[0],
)
sections = single_election_date_template_sorter.sections
assert len(_sections_of_type(sections, RegistrationDateSection)) == 1
assert (
len(_sections_of_type(sections, CityOfLondonRegistrationDateSection))
== 0
)


@freeze_time("2024-04-16")
def test_city_of_london_and_parl_different_days(
template_sorter, election_date_template_sorter
):
single_ballot_sorter_before_deadline = template_sorter(
CITY_OF_LONDON_COUNCIL_AND_PARL_DIFFERENT_DAYS,
date=datetime.date(2024, 3, 16),
)
single_election_date_template_sorter = election_date_template_sorter(
single_ballot_sorter_before_deadline,
single_ballot_sorter_before_deadline.dates[0],
)
sections = single_election_date_template_sorter.sections
assert len(_sections_of_type(sections, RegistrationDateSection)) == 0
assert (
len(_sections_of_type(sections, CityOfLondonRegistrationDateSection))
== 1
)
# ignore the parl ballot in this test


@freeze_time("2024-04-16")
def test_city_of_london_and_parl_same_day(
template_sorter, election_date_template_sorter
):
single_ballot_sorter_before_deadline = template_sorter(
CITY_OF_LONDON_COUNCIL_AND_PARL_SAME_DAY,
date=datetime.date(2024, 3, 16),
)
single_election_date_template_sorter = election_date_template_sorter(
single_ballot_sorter_before_deadline,
single_ballot_sorter_before_deadline.dates[0],
)
sections = single_election_date_template_sorter.sections
assert len(_sections_of_type(sections, RegistrationDateSection)) == 1
assert (
len(_sections_of_type(sections, CityOfLondonRegistrationDateSection))
== 1
)
49 changes: 0 additions & 49 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
NO_LOCAL_BALLOTS,
SINGLE_LOCAL_FUTURE_BALLOT_WITH_POLLING_STATION,
)
from template_sorter import (
ApiModes,
ElectionDateTemplateSorter,
TemplateSorter,
)
from uk_election_timetables.calendars import Country
from uk_election_timetables.election_ids import from_election_id


Expand Down Expand Up @@ -59,49 +53,6 @@ def test_uk_election_timetable():
# https://election-timetable.democracyclub.org.uk/?election_date=2024-05-02


@pytest.fixture
def template_sorter():
def get_template_sorter(mock_response, date):
api_response = mock_response
# The dates exist in the api_response, but not
# in the format that matches the RootBuilder:
# dates = api_response._values["dates"] vs
# dates = api_response.dates so I've set it here.
api_response.dates = api_response._values["dates"]
mode = ApiModes.UPCOMING_ELECTIONS

sorter = TemplateSorter(
api_response=api_response, mode=mode, current_date=date
)
sorter.country = Country.ENGLAND
sorter.dates = api_response.dates

return sorter

return get_template_sorter


@pytest.fixture
def election_date_template_sorter():
def get_election_date_template_sorter(template_sorter, date):
election_date_sorter = ElectionDateTemplateSorter(
date_data=date,
country=template_sorter.country,
current_date=template_sorter.current_date,
response_type=template_sorter.response_type,
)
election_date_sorter.current_date = template_sorter.current_date

election_date_sorter.timetable = from_election_id(
template_sorter.dates[0].ballots[0].election_id,
country=template_sorter.country,
)

return election_date_sorter

return get_election_date_template_sorter


@freeze_time("2024-04-04")
def test_sopn_day(template_sorter, election_date_template_sorter):
"""this tests is after postal vote deadline, but
Expand Down

0 comments on commit 787304a

Please sign in to comment.