diff --git a/tests/conftest.py b/tests/conftest.py index c51dd26..6cc0de7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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") @@ -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 diff --git a/tests/test_registration.py b/tests/test_registration.py new file mode 100644 index 0000000..3b032c8 --- /dev/null +++ b/tests/test_registration.py @@ -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 + ) diff --git a/tests/test_result.py b/tests/test_result.py index 7ec8d5d..93e2d08 100644 --- a/tests/test_result.py +++ b/tests/test_result.py @@ -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 @@ -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