def test_fromkeys_with_default_value(self): assert Cut.fromkeys(["Bulbasaur", "Charmander", "Squirtle"], "captured") == Cut({ "Bulbasaur": "captured", "Charmander": "captured", "Squirtle": "captured" })
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)
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
def test_fromkeys(self): assert Cut.fromkeys(["Bulbasaur", "Charmander", "Squirtle"]) == Cut({ "Bulbasaur": None, "Charmander": None, "Squirtle": None })
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)
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"))
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)
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)
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)
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)
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)
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
def test_contains(self, dict_type, data, key, result): proxy = Cut(dict_type(data)) assert (key in proxy) is result
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)]
def test_setdefault(self, dict_type, data, key, result): proxy = Cut(dict_type(deepcopy(data))) assert proxy.setdefault(key) == result assert proxy[key] == result
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
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"})]
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
def test_popitem(dict_type): proxy = Cut(dict_type({"a": 1, "b": 2, "c": 3})) proxy.popitem() assert len(proxy) == 2
def test_len(dict_type): proxy = Cut(dict_type({"a": 1, "b": 2, "c": 3})) assert len(proxy) == 3
def test_keys(dict_type): proxy = Cut(dict_type({"a": 1, "b": 2, "c": 3})) assert sorted(proxy.keys()) == ["a", "b", "c"]
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"]
def test_from_dict(self, dict_type): proxy = Cut(dict_type({"a": {"b": 1}})) proxy.update({"a.b": 42}) assert proxy["a"]["b"] == 42
def test_get(self, dict_type, data, key, result): proxy = Cut(dict_type(data)) assert proxy.get(key) == result
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
def test_with_default(self, dict_type, data, key, default): proxy = Cut(dict_type(data)) assert proxy.get(key, default) == default
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
def test_delitem(self, dict_type, data, key): proxy = Cut(dict_type(deepcopy(data))) del proxy[key] assert key not in proxy
def test_setitem(self, dict_type, data, key, result): proxy = Cut(dict_type(data)) proxy[key] = result assert proxy[key] == result
def test_against_another_default_dict(self, dict_type): data = {"a": 42} proxy = Cut(dict_type(data)) assert proxy == defaultdict(None, data)