Beispiel #1
0
    def test_nested_index_error(self, dict_type, data, key, error_message):
        proxy = Cut(dict_type(data))
        with pytest.raises(IndexError) as error:
            proxy.setdefault(key, 42)

        expected_error_message = (
            f"{error_message} {repr(IndexError('list index out of range'))}.")

        assert str(error.value) == expected_error_message
Beispiel #2
0
    def test_index_error(self, dict_type):
        proxy = Cut(dict_type({"a": [42]}))
        with pytest.raises(IndexError) as error:
            proxy.setdefault("a[1]")

        expected_error = IndexError(
            "Cannot access index '1' in path 'a[1]', "
            f"because of error: {repr(IndexError('list index out of range'))}."
        )
        assert str(error.value) == str(expected_error)
Beispiel #3
0
    def test_type_error(self, dict_type):
        proxy = Cut(dict_type({"a": 1}))
        with pytest.raises(TypeError) as error:
            proxy.setdefault("a[1]")

        expected_error = TypeError(
            f"Cannot access key '1' in path 'a[1]': "
            f"the element must be a dictionary or a list but is of type '<class 'int'>'."
        )
        assert str(error.value) == str(expected_error)
Beispiel #4
0
 def test_with_default(self, dict_type, data, key, default):
     proxy = Cut(dict_type(deepcopy(data)))
     assert proxy.setdefault(key, default) == default
     assert proxy[key] == default
Beispiel #5
0
 def test_setdefault(self, dict_type, data, key, result):
     proxy = Cut(dict_type(deepcopy(data)))
     assert proxy.setdefault(key) == result
     assert proxy[key] == result