Skip to content

Commit 0f3404b

Browse files
committed
Add corner cases for get_item
Signed-off-by: Tushar Goel <[email protected]>
1 parent 9b99070 commit 0f3404b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

vulnerabilities/tests/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from packageurl import PackageURL
2424

2525
from vulnerabilities.utils import AffectedPackage
26+
from vulnerabilities.utils import get_item
2627
from vulnerabilities.utils import nearest_patched_package
2728
from vulnerabilities.utils import split_markdown_front_matter
2829

@@ -94,3 +95,17 @@ def test_split_markdown_front_matter():
9495

9596
results = split_markdown_front_matter(text)
9697
assert results == expected
98+
99+
100+
def test_get_item():
101+
d1 = {"a": {"b": {"c": None}}}
102+
assert get_item(d1, "a", "b", "c", "d") == None
103+
d2 = {"a": {"b": {"c": {"d": None}}}}
104+
assert get_item(d2, "a", "b", "c", "e") == None
105+
d3 = ["a", "b", "c", "d"]
106+
assert get_item(d3, "a", "b") == None
107+
d4 = {"a": {"b": {"c": {"d": []}}}}
108+
assert get_item(d4, "a", "b", "c", "d", "e") == None
109+
d5 = {"a": {"b": {"c": "d"}}}
110+
assert get_item(d5, "a", "b", "c", "d") == None
111+
assert get_item(d5, "a", "b", "c") == "d"

vulnerabilities/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,12 @@ def get_item(dictionary: dict, *attributes):
220220
for attribute in attributes:
221221
if not dictionary:
222222
return
223+
if not isinstance(dictionary, dict):
224+
logger.error("dictionary must be of type `dict`")
225+
return
223226
if attribute not in dictionary:
224227
logger.error(f"Missing attribute {attribute} in {dictionary}")
225-
return None
228+
return
226229
dictionary = dictionary[attribute]
227230
return dictionary
228231

0 commit comments

Comments
 (0)