示例#1
0
 def test_fromkeys_with_default_value(self):
     assert Cut.fromkeys(["Bulbasaur", "Charmander", "Squirtle"],
                         "captured") == Cut({
                             "Bulbasaur": "captured",
                             "Charmander": "captured",
                             "Squirtle": "captured"
                         })
示例#2
0
    def test_pop_index_error_when_no_default_provided(self, dict_type):
        proxy = Cut(dict_type({"a": [[1]]}))
        with pytest.raises(IndexError) as error:
            proxy.pop("a[0][1]")

        expected_error = IndexError(
            f"Cannot access index '1' in path 'a[0][1]', "
            f"because of error: {repr(IndexError('pop index out of range'))}.")
        assert str(error.value) == str(expected_error)
示例#3
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
示例#4
0
 def test_fromkeys(self):
     assert Cut.fromkeys(["Bulbasaur", "Charmander", "Squirtle"]) == Cut({
         "Bulbasaur":
         None,
         "Charmander":
         None,
         "Squirtle":
         None
     })
示例#5
0
    def test_key_error_when_no_default_provided(self, dict_type, data, path,
                                                failing_key):
        proxy = Cut(dict_type(deepcopy(data)))
        with pytest.raises(KeyError) as error:
            proxy.pop(path)

        expected_error = KeyError(
            f"Cannot access key '{failing_key}' in path '{path}', "
            f"because of error: {repr(KeyError(failing_key))}.")
        assert str(error.value) == str(expected_error)
示例#6
0
    def test_keep_the_same_operator(self, dict_type):
        proxy = Cut(dict_type({"users": [{
            "name": "a"
        }, {
            "name": "b"
        }]}),
                    sep="/")
        separators = []

        assert all(item.sep == "/" for item in proxy.all("users"))
示例#7
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)
示例#8
0
    def test_attribute_error(self, dict_type):
        proxy = Cut(dict_type({"a": 42}))
        with pytest.raises(AttributeError) as error:
            proxy.pop("a.b")

        expected_error = AttributeError(
            "Unable to pop item 'b' in key 'a.b': "
            "the element must be a dictionary or a list but is of type '<class 'int'>'."
        )
        assert str(error.value) == str(expected_error)
示例#9
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)
示例#10
0
    def test_list_index_error_when_no_default_provided(self, dict_type, data,
                                                       path, failing_index):
        proxy = Cut(dict_type(deepcopy(data)))
        with pytest.raises(IndexError) as error:
            proxy.pop(path)

        expected_error = IndexError(
            f"Cannot access index '{failing_index}' in path '{path}', "
            f"because of error: {repr(IndexError('list index out of range'))}."
        )
        assert str(error.value) == str(expected_error)
示例#11
0
    def test_key_error(self, dict_type):
        proxy = Cut(dict_type({"a": {"b": 42}}))
        with pytest.raises(KeyError) as error:
            del proxy["a.c"]

        expected_error = KeyError(
            f"Cannot access key 'c' in path 'a.c', because of error: {repr(KeyError('c'))}."
        )
        assert str(error.value) == str(expected_error)
示例#12
0
 def test_pop(self, dict_type, data, key, result):
     proxy = Cut(dict_type(deepcopy(data)))
     assert proxy.pop(key) == result
     assert key not in proxy
示例#13
0
 def test_contains(self, dict_type, data, key, result):
     proxy = Cut(dict_type(data))
     assert (key in proxy) is result
示例#14
0
def test_items(dict_type):
    proxy = Cut(dict_type({"a": 1, "b": 2, "c": 3}))
    assert sorted(proxy.items()) == [("a", 1), ("b", 2), ("c", 3)]
示例#15
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
示例#16
0
 def test_from_list(self, dict_type):
     proxy = Cut(dict_type({"a": {"b": 1}}))
     from_other = [("a.b", 42)]
     proxy.update(from_other)
     assert proxy["a"]["b"] == 42
示例#17
0
 def test_all(self, dict_type):
     proxy = Cut(dict_type({"users": [{"name": "a"}, {"name": "b"}]}))
     values = [item for item in proxy.all("users")]
     assert values == [Cut({"name": "a"}), Cut({"name": "b"})]
示例#18
0
 def test_return_generator(self, dict_type):
     proxy = Cut(dict_type({"users": [{"name": "a"}, {"name": "b"}]}))
     result = proxy.all("users")
     assert isinstance(result, GeneratorType) is True
示例#19
0
def test_popitem(dict_type):
    proxy = Cut(dict_type({"a": 1, "b": 2, "c": 3}))
    proxy.popitem()
    assert len(proxy) == 2
示例#20
0
def test_len(dict_type):
    proxy = Cut(dict_type({"a": 1, "b": 2, "c": 3}))
    assert len(proxy) == 3
示例#21
0
def test_keys(dict_type):
    proxy = Cut(dict_type({"a": 1, "b": 2, "c": 3}))
    assert sorted(proxy.keys()) == ["a", "b", "c"]
示例#22
0
def test_iter(dict_type):
    proxy = Cut(dict_type({"a": 1, "b": 2, "c": 3}))
    assert sorted([key for key in proxy]) == ["a", "b", "c"]
示例#23
0
 def test_from_dict(self, dict_type):
     proxy = Cut(dict_type({"a": {"b": 1}}))
     proxy.update({"a.b": 42})
     assert proxy["a"]["b"] == 42
示例#24
0
 def test_get(self, dict_type, data, key, result):
     proxy = Cut(dict_type(data))
     assert proxy.get(key) == result
示例#25
0
 def test_from_list_and_keyword_args(self, dict_type):
     proxy = Cut(dict_type({"a": {"b": 1}}))
     from_other = [("a.b", 42)]
     proxy.update(from_other, c=666)
     assert proxy["a"]["b"] == 42
     assert proxy["c"] == 666
示例#26
0
 def test_with_default(self, dict_type, data, key, default):
     proxy = Cut(dict_type(data))
     assert proxy.get(key, default) == default
示例#27
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
示例#28
0
 def test_delitem(self, dict_type, data, key):
     proxy = Cut(dict_type(deepcopy(data)))
     del proxy[key]
     assert key not in proxy
示例#29
0
 def test_setitem(self, dict_type, data, key, result):
     proxy = Cut(dict_type(data))
     proxy[key] = result
     assert proxy[key] == result
示例#30
0
 def test_against_another_default_dict(self, dict_type):
     data = {"a": 42}
     proxy = Cut(dict_type(data))
     assert proxy == defaultdict(None, data)