예제 #1
0
def test_itemrollback_nop_error() -> None:
    d = {"foo": 42}
    with pytest.raises(RuntimeError, match="Catch this!"):
        with itemrollback(d, "foo"):
            assert d["foo"] == 42
            raise RuntimeError("Catch this!")
    assert d["foo"] == 42
예제 #2
0
def test_itemrollback_unset_error() -> None:
    d = {"foo": 42}
    with pytest.raises(RuntimeError, match="Catch this!"):
        with itemrollback(d, "bar"):
            assert "bar" not in d
            raise RuntimeError("Catch this!")
    assert "bar" not in d
예제 #3
0
def test_itemrollback_unset_modify_error() -> None:
    d: Dict[str, Any] = {"foo": 42}
    with pytest.raises(RuntimeError, match="Catch this!"):
        with itemrollback(d, "bar"):
            assert "bar" not in d
            d["bar"] = [3.14]
            raise RuntimeError("Catch this!")
    assert "bar" not in d
예제 #4
0
def test_itemrollback_modify_error() -> None:
    d: Dict[str, Any] = {"foo": 42}
    with pytest.raises(RuntimeError, match="Catch this!"):
        with itemrollback(d, "foo"):
            assert d["foo"] == 42
            d["foo"] = [3.14]
            raise RuntimeError("Catch this!")
    assert d["foo"] == 42
예제 #5
0
def test_itemrollback_no_copy() -> None:
    d: Dict[str, Dict[str, List[Any]]] = {
        "foo": {
            "bar": [1, 2, 3],
            "quux": ["a", "b", "c"]
        }
    }
    with itemrollback(d, "foo"):
        d["foo"]["bar"].append(4)
        d["foo"]["quux"] = ["x", "y", "z"]
    assert d["foo"] == {"bar": [1, 2, 3, 4], "quux": ["x", "y", "z"]}
예제 #6
0
def test_itemrollback_deepcopy(copy: bool) -> None:
    d: Dict[str, Dict[str, List[Any]]] = {
        "foo": {
            "bar": [1, 2, 3],
            "quux": ["a", "b", "c"]
        }
    }
    with itemrollback(d, "foo", copy=copy, deepcopy=True):
        d["foo"]["bar"].append(4)
        d["foo"]["quux"] = ["x", "y", "z"]
    assert d["foo"] == {"bar": [1, 2, 3], "quux": ["a", "b", "c"]}
예제 #7
0
def test_itemrollback_deepcopy_error(copy: bool) -> None:
    d: Dict[str, Dict[str, List[Any]]] = {
        "foo": {
            "bar": [1, 2, 3],
            "quux": ["a", "b", "c"]
        }
    }
    with pytest.raises(RuntimeError, match="Catch this!"):
        with itemrollback(d, "foo", copy=copy, deepcopy=True):
            d["foo"]["bar"].append(4)
            d["foo"]["quux"] = ["x", "y", "z"]
            raise RuntimeError("Catch this!")
    assert d["foo"] == {"bar": [1, 2, 3], "quux": ["a", "b", "c"]}
예제 #8
0
def test_itemrollback_unset_modify() -> None:
    d: Dict[str, Any] = {"foo": 42}
    with itemrollback(d, "bar"):
        assert "bar" not in d
        d["bar"] = [3.14]
    assert "bar" not in d
예제 #9
0
def test_itemrollback_unset() -> None:
    d = {"foo": 42}
    with itemrollback(d, "bar"):
        assert "bar" not in d
    assert "bar" not in d
예제 #10
0
def test_itemrollback_nop() -> None:
    d = {"foo": 42}
    with itemrollback(d, "foo"):
        assert d["foo"] == 42
    assert d["foo"] == 42
예제 #11
0
def test_itemrollback_del() -> None:
    d = {"foo": 42}
    with itemrollback(d, "foo"):
        assert d["foo"] == 42
        del d["foo"]
    assert d["foo"] == 42
예제 #12
0
def test_itemrollback_modify() -> None:
    d: Dict[str, Any] = {"foo": 42}
    with itemrollback(d, "foo"):
        assert d["foo"] == 42
        d["foo"] = [3.14]
    assert d["foo"] == 42