def test_key_error(self): with pytest.raises(KeyError) as error: traverse(data={"a": 42}, keys=["b"], original_path="...") expected_error = KeyError( f"Cannot access key 'b' in path '...', because of error: {repr(KeyError('b'))}." ) assert str(error.value) == str(expected_error)
def test_index_error(self): with pytest.raises(IndexError) as error: traverse(data={"a": [42]}, keys=["a", 1], original_path="...") expected_error = IndexError( "Cannot access index '1' in path '...', " f"because of error: {repr(IndexError('list index out of range'))}." ) assert str(error.value) == str(expected_error)
def test_type_error(self, data, keys, original_path, failing_key, failing_item_type): with pytest.raises(TypeError) as error: traverse(data=data, keys=keys, original_path=original_path) expected_error = TypeError( f"Cannot access key '{failing_key}' in path '{original_path}': " f"the element must be a dictionary or a list but is of type '{failing_item_type}'." ) assert str(error.value) == str(expected_error)
def test_traverse(self, data, keys, original_path, result): assert traverse(data=data, keys=keys, original_path=original_path) == result