Skip to content

Commit 0664277

Browse files
authored
Fix election date parsing bug (#566)
Refactor the election date parsing to both DRY up the code, add tests, simplify string regex to just expect a `(.+)` pattern for date string.
1 parent af88188 commit 0664277

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

ksvotes/services/nvris_client.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
from ksvotes.services.form_filler_service import FormFillerService
6+
from ksvotes.utils import parse_election_date as parse_election_date_util
67
import logging
78

89
logger = logging.getLogger(__name__)
@@ -73,15 +74,13 @@ def marshall_payload(self, flavor, **kwargs):
7374
return payload
7475

7576
def parse_election_date(self, election):
76-
pattern = r"(Prim\w+|General) \((.+)\)"
77-
m = re.search(pattern, election)
78-
if m:
79-
return m.group(2)
80-
else:
77+
date = parse_election_date_util(election)
78+
if not date:
8179
logger.error(
8280
"%s No match for election '%s'" % (self.registrant.session_id, election)
8381
)
8482
return "(none)"
83+
return date.strftime("%m/%d/%Y")
8584

8685
def normalize_unit(self, unit):
8786
return re.sub(r"^(#|apt.? |apartment )", "", unit, flags=re.IGNORECASE)

ksvotes/tests/test_utils.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
from ksvotes.utils import zip_code_matches
2+
from ksvotes.utils import zip_code_matches, parse_election_date
3+
from datetime import datetime
34
from test_plus import TestCase
45
from django.conf import settings
56

@@ -12,12 +13,22 @@ def update_session(self, registrant):
1213
self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
1314
return session
1415

16+
def test_zip_code_matches(self):
17+
sosrec = {"Address": "123 Main St #456 Wichita, KS, 12345-9999"}
1518

16-
def test_zip_code_matches():
17-
sosrec = {"Address": "123 Main St #456 Wichita, KS, 12345-9999"}
19+
self.assertTrue(zip_code_matches(sosrec, 12345))
20+
self.assertTrue(zip_code_matches(sosrec, "12345"))
21+
self.assertFalse(zip_code_matches(sosrec, 98765))
22+
self.assertFalse(zip_code_matches(sosrec, 9999))
23+
self.assertFalse(zip_code_matches(sosrec, "myzip"))
1824

19-
assert zip_code_matches(sosrec, 12345) == True
20-
assert zip_code_matches(sosrec, "12345") == True
21-
assert zip_code_matches(sosrec, 98765) == False
22-
assert zip_code_matches(sosrec, 9999) == False
23-
assert zip_code_matches(sosrec, "myzip") == False
25+
def test_parse_election_date(self):
26+
dates = {
27+
"Presidential Preference Primary (March 19, 2024)": datetime(2024, 3, 19),
28+
"General (November 5, 2024)": datetime(2024, 11, 5),
29+
"Special Election (March 3, 2026)": datetime(2026, 3, 3),
30+
"Elecciones Especiales (3 de Marzo de 2026)": datetime(2026, 3, 3),
31+
}
32+
for election, dt in dates.items():
33+
d = parse_election_date(election)
34+
self.assertEqual(dt, d, f"parsed {election}")

ksvotes/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,11 @@ def construct_county_choices(default):
161161

162162

163163
def parse_election_date(election):
164-
pattern = (
165-
r"(Primary|Primaria|General|Special Election|Elecciones Especiales) \((.+)\)"
166-
)
164+
pattern = r".+ \((.+)\)"
167165
m = re.match(pattern, str(election))
168166
if not m:
169167
return None
170-
date = m.group(2)
168+
date = m.group(1)
171169
return dateparser.parse(date)
172170

173171

0 commit comments

Comments
 (0)