Skip to content

Commit

Permalink
test: _filter_target_dict_with_list raises
Browse files Browse the repository at this point in the history
  • Loading branch information
r-leyshon committed Oct 17, 2023
1 parent 343b55e commit 845023f
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions tests/osm/test_validate_osm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"""Test validate_osm."""
import pytest
import re

from transport_performance.osm.validate_osm import (
_compile_tags,
_check_dict_values_all_equal,
_filter_target_dict_with_list,
)


Expand Down Expand Up @@ -51,3 +55,102 @@ def test__check_dict_values_all_equal(self):
unequal_dict = {"a": "foo", "b": "bar"}
assert _check_dict_values_all_equal(equal_dict, "foo")
assert not _check_dict_values_all_equal(unequal_dict, "bar")


class Test_FilterTargetDictWithList(object):
"""Tests for _filter_target_dict_with_list internal."""

def test_filter_target_dict_with_list_raises(self):
"""Check on raise conditions."""
with pytest.raises(
TypeError,
match="`targets` expected <class 'dict'>. Got <class 'int'>",
):
_filter_target_dict_with_list(
targets=1, _list=list(), search_key="key", accepted_keys=list()
)
with pytest.raises(
TypeError,
match="`search_key` expected <class 'str'>. Got <class 'int'>",
):
_filter_target_dict_with_list(
targets={}, _list=list(), search_key=1, accepted_keys=list()
)
with pytest.raises(
TypeError,
match="`_list` should be a list. Instead found <class 'int'>",
):
_filter_target_dict_with_list(
targets=dict(), _list=1, search_key="key", accepted_keys=list()
)
with pytest.raises(
TypeError,
match="`_list` must contain <class 'int'> only. Found"
" <class 'float'> : 3.0",
):
_filter_target_dict_with_list(
targets=dict(),
_list=[1, 2, 3.0],
search_key="key",
accepted_keys=list(),
)
with pytest.raises(
TypeError,
match="`accepted_keys` should be a list. Instead found"
" <class 'int'>",
):
_filter_target_dict_with_list(
targets=dict(), _list=[1], search_key="key", accepted_keys=1
)
with pytest.raises(
TypeError,
match="`accepted_keys` must contain <class 'str'> only. Found"
" <class 'int'> : 2",
):
_filter_target_dict_with_list(
targets=dict(),
_list=[1],
search_key="key",
accepted_keys=["1", 2, "3"],
)
with pytest.raises(
ValueError,
match=re.escape(
"'search_key' expected one of the following:['list', 'of',"
" 'keys'] Got unacceptable_key"
),
):
_filter_target_dict_with_list(
targets=dict(),
_list=list(),
search_key="unacceptable_key",
accepted_keys=["list", "of", "keys"],
)
# check catching conditions where search key doesn't match the name
# of any dictionary target
empty_dict = {"some_dict": dict()}
with pytest.raises(
KeyError,
match=re.escape(
"`search_key`: no_match did not match keys in `targets`:"
" dict_keys(['some_dict'])"
),
):
_filter_target_dict_with_list(
targets=empty_dict,
_list=[1, 2, 3],
search_key="no_match",
accepted_keys=["no_match"],
)
# this check is to simulate providing the incorrect search key
# resulting in an empty dictionary returned.
with pytest.raises(
ValueError,
match="No tags found. Did you specify the correct search_key?",
):
_filter_target_dict_with_list(
targets=empty_dict,
_list=[1, 2, 3],
search_key="some_dict",
accepted_keys=["some_dict"],
)

0 comments on commit 845023f

Please sign in to comment.