Skip to content

Commit

Permalink
feat(for_id): Optional country should be a Country object, not string
Browse files Browse the repository at this point in the history
* It's up to consumer libraries to derive Country from their internal representations. This simplifies the .for_id logic quite a bit.
  • Loading branch information
mrwilson committed Aug 24, 2019
1 parent d7f0e7e commit d1e3b52
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 39 deletions.
23 changes: 6 additions & 17 deletions sopn_publish_date/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,17 @@ def __init__(self):
}
self.calendar = UnitedKingdomBankHolidays()

def for_id(self, election_id: str, country: str = None) -> date:
def for_id(self, election_id: str, country: Country = None) -> date:
"""
Calculate the publish date for an election given in `uk-election-ids <https://elections.democracyclub.org.uk/reference_definition/>`_ format and an optional country if necessary (for example, local or parliamentary elections), or raise an exception if that election id is ambiguous (could correspond to elections in multiple countries with different electoral legislation)
:param election_id: a string representing an election id in uk-election-ids format
:param country: an optional string representing the country where the election will be held
:param country: an optional Country representing the country where the election will be held
:return: a datetime representing the expected publish date
"""

election_type, poll_date = type_and_poll_date(election_id)

countries = {
"ENG": Country.ENGLAND,
"WLS": Country.WALES,
"SCT": Country.SCOTLAND,
"NIR": Country.NORTHERN_IRELAND,
}

def valid_election_type(el_type):
return el_type in self.election_id_lookup or el_type in ["local", "parl"]

Expand All @@ -53,20 +46,16 @@ def requires_country(el_type):
if not valid_election_type(election_type):
raise NoSuchElectionTypeError(election_type)

if requires_country(election_type) and (
country is None or country not in countries
):
raise AmbiguousElectionIdError(election_id, country)
if requires_country(election_type) and country is None:
raise AmbiguousElectionIdError(election_id)

if election_type in self.election_id_lookup:
return self.election_id_lookup[election_type](poll_date)

real_country = countries[country]

if election_type == "local":
return self.local(poll_date, country=real_country)
return self.local(poll_date, country=country)
elif election_type == "parl":
return self.uk_parliament(poll_date, country=real_country)
return self.uk_parliament(poll_date, country=country)

def northern_ireland_assembly(self, poll_date: date) -> date:
"""
Expand Down
11 changes: 2 additions & 9 deletions sopn_publish_date/election_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,11 @@ class AmbiguousElectionIdError(BaseException):
An exception type to represent when an election id (usually a group such as `local.2019-05-02`) can correspond to elections in multiple countries with different legislation governing the publish date of Statements of Persons Nominated.
"""

def __init__(self, election_id: str, country: str = None):
def __init__(self, election_id: str):
self.election_id = election_id
self.country = country

def __str__(self):
if self.country:
return "Election id [%s] requires a valid country, got [%s]" % (
self.election_id,
self.country,
)
else:
return "Cannot derive country from election id [%s]" % self.election_id
return "Cannot derive country from election id [%s]" % self.election_id


def type_and_poll_date(election_id: str) -> (str, date):
Expand Down
15 changes: 2 additions & 13 deletions tests/test_sopn_publish_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,17 @@ def test_publish_date_local_id():


def test_publish_date_local_id_with_country():
publish_date = sopn_publish_date.for_id("local.2019-02-21", country="ENG")
publish_date = sopn_publish_date.for_id("local.2019-02-21", country=Country.ENGLAND)

assert publish_date == date(2019, 1, 28)


def test_publish_date_parl_id_with_country():
publish_date = sopn_publish_date.for_id("parl.2019-02-21", country="ENG")
publish_date = sopn_publish_date.for_id("parl.2019-02-21", country=Country.ENGLAND)

assert publish_date == date(2019, 1, 25)


def test_publish_date_parl_id_with_invalid_country():

with raises(AmbiguousElectionIdError) as err:
sopn_publish_date.for_id("parl.2019-02-21", country="USA")

assert (
str(err.value)
== "Election id [parl.2019-02-21] requires a valid country, got [USA]"
)


def test_publish_date_not_an_election_type():

with raises(NoSuchElectionTypeError) as err:
Expand Down

0 comments on commit d1e3b52

Please sign in to comment.