From 845023fee86dfeead87dcba1841bc71758ca68b9 Mon Sep 17 00:00:00 2001 From: r-leyshon Date: Tue, 17 Oct 2023 17:14:58 +0100 Subject: [PATCH] test: _filter_target_dict_with_list raises --- tests/osm/test_validate_osm.py | 103 +++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/tests/osm/test_validate_osm.py b/tests/osm/test_validate_osm.py index db70f9b7..653f6fcf 100644 --- a/tests/osm/test_validate_osm.py +++ b/tests/osm/test_validate_osm.py @@ -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, ) @@ -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 . Got ", + ): + _filter_target_dict_with_list( + targets=1, _list=list(), search_key="key", accepted_keys=list() + ) + with pytest.raises( + TypeError, + match="`search_key` expected . Got ", + ): + _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 ", + ): + _filter_target_dict_with_list( + targets=dict(), _list=1, search_key="key", accepted_keys=list() + ) + with pytest.raises( + TypeError, + match="`_list` must contain only. Found" + " : 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" + " ", + ): + _filter_target_dict_with_list( + targets=dict(), _list=[1], search_key="key", accepted_keys=1 + ) + with pytest.raises( + TypeError, + match="`accepted_keys` must contain only. Found" + " : 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"], + )