Skip to content

Commit

Permalink
Black
Browse files Browse the repository at this point in the history
  • Loading branch information
symroe committed Apr 12, 2023
1 parent 10890cf commit f99ca7a
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 56 deletions.
11 changes: 9 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ def _get_description():
license="MIT",
url="https://github.com/DemocracyClub/uk-election-ids/",
packages=["uk_election_ids"],
package_data={'uk_election_ids': ['data/*.json']},
package_data={"uk_election_ids": ["data/*.json"]},
description="Create Democracy Club Election Identifiers",
long_description=_get_description(),
long_description_content_type="text/markdown",
extras_require={
"testing": ["coveralls"],
"development": ["sphinx", "sphinx_rtd_theme", "ghp-import", "pydantic"],
"development": [
"sphinx",
"sphinx_rtd_theme",
"ghp-import",
"pydantic",
"black==23.3.0",
"ruff==0.0.261",
],
},
classifiers=[
"License :: OSI Approved :: MIT License",
Expand Down
43 changes: 32 additions & 11 deletions tests/test_id_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def test_naw_sp_valid_subtype_no_division(self):
def test_naw_sp_with_org(self):
for election_type in ("naw", "sp", "senedd"):
with self.assertRaises(ValueError):
IdBuilder(election_type, date(2018, 5, 3)).with_organisation("test-org")
IdBuilder(election_type, date(2018, 5, 3)).with_organisation(
"test-org"
)

def test_naw_sp_with_division(self):
for election_type in ("naw", "sp", "senedd"):
Expand Down Expand Up @@ -94,7 +96,9 @@ def test_naw_sp_with_division(self):
def test_nia_parl_europarl_with_org(self):
for election_type in ("nia", "parl", "europarl"):
with self.assertRaises(ValueError):
IdBuilder(election_type, date(2018, 5, 3)).with_organisation("test-org")
IdBuilder(election_type, date(2018, 5, 3)).with_organisation(
"test-org"
)

def test_nia_parl_europarl_with_subtype(self):
for election_type in ("nia", "parl", "europarl"):
Expand Down Expand Up @@ -126,7 +130,9 @@ def test_nia_parl_europarl_with_div(self):
with self.assertRaises(ValueError):
id.organisation_group_id
ballot_id = id.ballot_id
self.assertEqual("%s.test-division.2018-05-03" % (election_type), ballot_id)
self.assertEqual(
"%s.test-division.2018-05-03" % (election_type), ballot_id
)
self.assertEqual(
[
"%s.2018-05-03" % (election_type),
Expand Down Expand Up @@ -173,7 +179,9 @@ def test_local_with_org_no_div(self):
self.assertEqual("local.test-org.2018-05-03", organisation_id)
with self.assertRaises(ValueError):
id.ballot_id
self.assertEqual(["local.2018-05-03", "local.test-org.2018-05-03"], id.ids)
self.assertEqual(
["local.2018-05-03", "local.test-org.2018-05-03"], id.ids
)

def test_local_with_org_with_div(self):
id = (
Expand Down Expand Up @@ -251,7 +259,9 @@ def test_pcc_mayor_with_org(self):
id.subtype_group_id
organisation_id = id.organisation_group_id
ballot_id = id.ballot_id
self.assertEqual("%s.test-org.2018-05-03" % (election_type), ballot_id)
self.assertEqual(
"%s.test-org.2018-05-03" % (election_type), ballot_id
)
self.assertEqual(organisation_id, ballot_id)
self.assertEqual(
[
Expand Down Expand Up @@ -333,7 +343,8 @@ def test_gla_constituency_with_division(self):
ballot_id = id.ballot_id
self.assertEqual("gla.c.test-div.2018-05-03", ballot_id)
self.assertEqual(
["gla.2018-05-03", "gla.c.2018-05-03", "gla.c.test-div.2018-05-03"], id.ids
["gla.2018-05-03", "gla.c.2018-05-03", "gla.c.test-div.2018-05-03"],
id.ids,
)

def test_by_elections(self):
Expand All @@ -349,7 +360,9 @@ def test_by_elections(self):
organisation_id = id.organisation_group_id
self.assertEqual("local.test-org.2018-05-03", organisation_id)
ballot_id = id.ballot_id
self.assertEqual("local.test-org.test-division.by.2018-05-03", ballot_id)
self.assertEqual(
"local.test-org.test-division.by.2018-05-03", ballot_id
)
self.assertEqual(
[
"local.2018-05-03",
Expand All @@ -372,7 +385,9 @@ def test_explicit_contest_type(self):
organisation_id = id.organisation_group_id
self.assertEqual("local.test-org.2018-05-03", organisation_id)
ballot_id = id.ballot_id
self.assertEqual("local.test-org.test-division.2018-05-03", ballot_id)
self.assertEqual(
"local.test-org.test-division.2018-05-03", ballot_id
)
self.assertEqual(
[
"local.2018-05-03",
Expand Down Expand Up @@ -405,7 +420,9 @@ def test_eq_not_equal(self):
.with_organisation("test-org")
.with_division("test-division")
)
obj2 = IdBuilder("local", date(2018, 5, 3)).with_organisation("test-org")
obj2 = IdBuilder("local", date(2018, 5, 3)).with_organisation(
"test-org"
)
self.assertNotEqual(obj1, obj2)

def test_eq_different_types(self):
Expand All @@ -423,15 +440,19 @@ def test_none(self):
.with_organisation("test-org")
.with_division(None)
)
self.assertEqual(["local.2018-05-03", "local.test-org.2018-05-03"], id.ids)
self.assertEqual(
["local.2018-05-03", "local.test-org.2018-05-03"], id.ids
)

def test_empty(self):
id = (
IdBuilder("local", date(2018, 5, 3))
.with_organisation("test-org")
.with_division("")
)
self.assertEqual(["local.2018-05-03", "local.test-org.2018-05-03"], id.ids)
self.assertEqual(
["local.2018-05-03", "local.test-org.2018-05-03"], id.ids
)

def test_slugger(self):
id1 = (
Expand Down
30 changes: 22 additions & 8 deletions tests/test_id_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def iter_defaults(data: dict, parent=None):
if default == "":
if parent not in ["dates", "nations"]:
self.assertTrue(
any([key in ["nations", "dates"] for key in data.keys()]),
msg=f"{data} requires either a `default`, `nations` or `dates` key"
any(
[key in ["nations", "dates"] for key in data.keys()]
),
msg=f"{data} requires either a `default`, `nations` or `dates` key",
)
for key, value in data.items():
if isinstance(value, dict):
Expand Down Expand Up @@ -46,7 +48,9 @@ def test_matcher_england(self):
assert result.get_id_requirements() == "EA-2022"

# UK parliamentary by-election post 2022 legislation
result = IDRequirementsMatcher("parl.stroud.by.2023-05-04", nation="ENG")
result = IDRequirementsMatcher(
"parl.stroud.by.2023-05-04", nation="ENG"
)
assert result.get_id_requirements() == "EA-2022"

# Local election on 2018 pilot scheme
Expand All @@ -66,11 +70,15 @@ def test_matcher_england(self):
assert result.get_id_requirements() is None

# GLA pre-2022 legislation
result = IDRequirementsMatcher("gla.c.brent-and-harrow.2016-05-05", nation="ENG")
result = IDRequirementsMatcher(
"gla.c.brent-and-harrow.2016-05-05", nation="ENG"
)
assert result.get_id_requirements() is None

# GLA post 2022 legislation
result = IDRequirementsMatcher("gla.c.brent-and-harrow.2023-05-04", nation="ENG")
result = IDRequirementsMatcher(
"gla.c.brent-and-harrow.2023-05-04", nation="ENG"
)
assert result.get_id_requirements() == "EA-2022"

def test_matcher_scotland(self):
Expand All @@ -91,7 +99,9 @@ def test_matcher_scotland(self):
assert result.get_id_requirements() is None

# UK parliamentary by-election post 2022 legislation
result = IDRequirementsMatcher("parl.stroud.by.2023-05-04", nation="SCT")
result = IDRequirementsMatcher(
"parl.stroud.by.2023-05-04", nation="SCT"
)
assert result.get_id_requirements() == "EA-2022"

def test_matcher_wales(self):
Expand Down Expand Up @@ -120,7 +130,9 @@ def test_matcher_wales(self):
assert result.get_id_requirements() == "EA-2022"

# UK parliamentary by-election post 2022 legislation
result = IDRequirementsMatcher("parl.stroud.by.2023-05-04", nation="WLS")
result = IDRequirementsMatcher(
"parl.stroud.by.2023-05-04", nation="WLS"
)
assert result.get_id_requirements() == "EA-2022"

def test_matcher_ni(self):
Expand All @@ -145,5 +157,7 @@ def test_matcher_ni(self):
assert result.get_id_requirements() == "EFA-2002"

# UK parliamentary by-election post 2022 legislation
result = IDRequirementsMatcher("parl.stroud.by.2023-05-04", nation="NIR")
result = IDRequirementsMatcher(
"parl.stroud.by.2023-05-04", nation="NIR"
)
assert result.get_id_requirements() == "EFA-2002"
12 changes: 9 additions & 3 deletions tests/test_meta_data_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ def test_meta_data_matcher_regex(self):
matcher = IDRequirementsMatcher("parl.corby.2022-05-04", nation="ENG")
assert matcher.match_id()[0] == "parl"

matcher = IDRequirementsMatcher("parl.stroud.by.2022-05-04", nation="ENG")
matcher = IDRequirementsMatcher(
"parl.stroud.by.2022-05-04", nation="ENG"
)
assert matcher.match_id()[0] == "parl.*.by"

matcher = IDRequirementsMatcher("parl.reading.tilehurst.by.2022-05-04", nation="ENG")
matcher = IDRequirementsMatcher(
"parl.reading.tilehurst.by.2022-05-04", nation="ENG"
)
assert matcher.match_id()[0] == "parl.*.by"

matcher = IDRequirementsMatcher("nonsense.parl.stroud.by.2022-05-04", nation="ENG")
matcher = IDRequirementsMatcher(
"nonsense.parl.stroud.by.2022-05-04", nation="ENG"
)
assert matcher.match_id()[0] is None
40 changes: 30 additions & 10 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ def test_valid_ids(self):
self.assertTrue(validate("sp.c.shetland-islands.by.2019-08-29"))
self.assertTrue(validate("nia.2017-03-02"))
self.assertTrue(validate("nia.fermanagh-and-south-tyrone.2017-03-02"))
self.assertTrue(validate("nia.fermanagh-and-south-tyrone.by.2017-03-02"))
self.assertTrue(
validate("nia.fermanagh-and-south-tyrone.by.2017-03-02")
)
self.assertTrue(validate("parl.2017-06-08"))
self.assertTrue(validate("parl.aberdeen-south.2017-06-08"))
self.assertTrue(validate("parl.aberdeen-south.by.2017-06-08"))
self.assertTrue(validate("local.2019-08-22"))
self.assertTrue(validate("local.rugby.2019-08-22"))
self.assertTrue(validate("local.rugby.rokeby-and-overslade.2019-08-22"))
self.assertTrue(validate("local.rugby.rokeby-and-overslade.by.2019-08-22"))
self.assertTrue(
validate("local.rugby.rokeby-and-overslade.by.2019-08-22")
)
self.assertTrue(validate("mayor.2019-05-02"))
self.assertTrue(validate("mayor.bedford.2019-05-02"))
self.assertTrue(validate("mayor.bedford.by.2019-05-02"))
Expand All @@ -47,11 +51,15 @@ def test_invalid_ids(self):
self.assertFalse(validate("foo.bar")) # doesn't end with a date
self.assertFalse(validate("local.2019-02-31")) # not a real date
self.assertFalse(validate("local.chips")) # not even slightly a date
self.assertFalse(validate("local.we$tward-ho!.2019-01-10")) # invalid chars
self.assertFalse(
validate("local.we$tward-ho!.2019-01-10")
) # invalid chars
self.assertFalse(
validate("parl.c.aberavon.2019-01-01")
) # should not have a subtype
self.assertFalse(validate("naw.aberavon.2019-01-01")) # should have a subtype
self.assertFalse(
validate("naw.aberavon.2019-01-01")
) # should have a subtype
self.assertFalse(
validate("gla.r.barnet-and-camden.2016-05-05")
) # invalid subtype
Expand All @@ -60,19 +68,31 @@ def test_invalid_ids(self):
validate("ref.croydon.by.2021-10-07")
) # there is no such thing as a by-referendum
# too many clauses
self.assertFalse(validate("naw.r.mid-and-west-wales.something-else.2016-05-05"))
self.assertFalse(validate("sp.c.shetland-islands.something-else.2019-08-29"))
self.assertFalse(
validate("naw.r.mid-and-west-wales.something-else.2016-05-05")
)
self.assertFalse(
validate("sp.c.shetland-islands.something-else.2019-08-29")
)
self.assertFalse(
validate("nia.fermanagh-and-south-tyrone.something-else.2017-03-02")
)
self.assertFalse(validate("parl.aberdeen-south.something-else.2017-06-08"))
self.assertFalse(
validate("local.rugby.rokeby-and-overslade.something-else.2019-08-22")
validate("parl.aberdeen-south.something-else.2017-06-08")
)
self.assertFalse(
validate(
"local.rugby.rokeby-and-overslade.something-else.2019-08-22"
)
)
self.assertFalse(validate("mayor.bedford.something-else.2019-05-02"))
self.assertFalse(validate("pcc.northumbria.something-else.2019-07-18"))
self.assertFalse(validate("gla.c.barnet-and-camden.something-else.2016-05-05"))
self.assertFalse(validate("europarl.uk-wales.something-else.2014-05-22"))
self.assertFalse(
validate("gla.c.barnet-and-camden.something-else.2016-05-05")
)
self.assertFalse(
validate("europarl.uk-wales.something-else.2014-05-22")
)
self.assertFalse(
validate("ref.croydon.some-division.something-else.2021-10-07")
)
6 changes: 4 additions & 2 deletions tests/test_voting_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def iter_defaults(data: dict, parent=None):
if not default:
if not parent in ["dates", "nations"]:
self.assertTrue(
any([key in ["nations", "dates"] for key in data.keys()]),
msg=f"{data} requires either a `default`, `nations` or `dates` key"
any(
[key in ["nations", "dates"] for key in data.keys()]
),
msg=f"{data} requires either a `default`, `nations` or `dates` key",
)
for key, value in data.items():
if isinstance(value, dict):
Expand Down
12 changes: 10 additions & 2 deletions uk_election_ids/datapackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@
"gla": {
"name": "Greater London Assembly elections",
"subtypes": [
{"name": "Constituencies", "election_subtype": "c", "can_have_divs": True},
{"name": "Additional", "election_subtype": "a", "can_have_divs": False},
{
"name": "Constituencies",
"election_subtype": "c",
"can_have_divs": True,
},
{
"name": "Additional",
"election_subtype": "a",
"can_have_divs": False,
},
],
"can_have_orgs": False,
},
Expand Down
Loading

0 comments on commit f99ca7a

Please sign in to comment.