Skip to content

Commit 5c6c7b9

Browse files
Attempting safer inputs for choices_and_map (#278)
Shouldn't throw any errors, should either just return empty or the actual values. Fix #265.
1 parent fc60578 commit 5c6c7b9

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

docassemble/EFSPIntegration/conversions.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,17 @@ def choices_and_map(
216216
if backing is None:
217217
backing = "code"
218218

219-
if codes_list is None:
220-
return [], SafeDict({})
221-
222219
if isinstance(codes_list, str):
223220
log(f"choices_and_map codes_list is a string? {codes_list}")
224221
return [], SafeDict({})
225222

223+
if not codes_list:
224+
return [], SafeDict({})
225+
226+
if not isinstance(next(iter(codes_list)), dict):
227+
log(f"choices_and_map codes_list entries aren't dicts? {codes_list}")
228+
return [], SafeDict({})
229+
226230
choices_list = [
227231
(code_obj[backing], display.format(**code_obj)) for code_obj in codes_list
228232
]

docassemble/EFSPIntegration/test/test_conversions.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from docassemble.base.core import DAObject
66
from docassemble.AssemblyLine.al_general import ALIndividual
77
from ..efm_client import ProxyConnection, ApiResponse
8-
from ..conversions import parse_case_info, parse_service_contacts
8+
from ..conversions import choices_and_map, parse_case_info, parse_service_contacts
99

1010

1111
class TestConversions(unittest.TestCase):
@@ -120,3 +120,45 @@ def test_ignore_attorneys(self):
120120
self.assertEqual(len(case.attorneys.keys()), 2)
121121
self.assertTrue("e650827f-3a2b-4550-b76c-f7d22ed479ff" in case.attorneys.keys())
122122
self.assertTrue("7ff43f9b-53ff-4e6d-9253-e393318549d0" in case.attorneys.keys())
123+
124+
125+
class TestChoicesAndMap(unittest.TestCase):
126+
def setUp(self):
127+
self.data = [
128+
{
129+
"code": "169",
130+
"name": "Probate or Mental Health",
131+
"ecfcasetype": "CivilCase",
132+
"procedureremedyinitial": "Not Available",
133+
"procedureremedysubsequent": "Not Available",
134+
"damageamountinitial": "Not Available",
135+
"damageamountsubsequent": "Not Available",
136+
},
137+
{
138+
"code": "7405",
139+
"name": "Adoption",
140+
"ecfcasetype": "CivilCase",
141+
"procedureremedyinitial": "Not Available",
142+
"procedureremedysubsequent": "Not Available",
143+
"damageamountinitial": "Not Available",
144+
"damageamountsubsequent": "Not Available",
145+
},
146+
]
147+
148+
def test_choices_and_map_nones(self):
149+
# As long as nothing throws, we're good.
150+
choices_and_map(None)
151+
choices_and_map([])
152+
choices_and_map([None, None])
153+
choices_and_map(["blah", "blah"])
154+
choices_and_map("blah")
155+
156+
# Note: we still fail on empty dicts in the list. No good fallback for that.
157+
# choices_and_map([{}])
158+
159+
def test_choices_and_map_success(self):
160+
choices, code_map = choices_and_map(self.data)
161+
self.assertEqual(len(choices), 2)
162+
self.assertEqual(len(code_map), 2)
163+
self.assertEqual(choices[0], ("169", "Probate or Mental Health"))
164+
self.assertEqual(code_map["169"]["name"], "Probate or Mental Health")

0 commit comments

Comments
 (0)