def test_multiple_call(): Foo, PatchedFoo = simple_classes() f = Foo("val", True) patch(Foo, PatchedFoo) patch(Foo, PatchedFoo) patch(Foo, PatchedFoo) assert f.compute_string() == "(val)_patched" assert not f.compute_bool() f.added_method() unpatch(Foo) assert f.compute_string() == "(val)" assert f.compute_bool() with pytest.raises(Exception): f.added_method() unpatch(Foo) assert f.compute_string() == "(val)" assert f.compute_bool() with pytest.raises(Exception): f.added_method()
def test_patch_instance(): Foo, PatchedFoo = simple_classes() f = Foo("val", True) f2 = Foo("val", True) patch(f, PatchedFoo) assert f.compute_string() == "(val)_patched" assert not f.compute_bool() f.added_method() assert f2.compute_string() == "(val)" assert f2.compute_bool() with pytest.raises(Exception): f2.added_method() unpatch(f) assert f.compute_string() == "(val)" assert f.compute_bool() with pytest.raises(Exception): f.added_method()
def test_patcher_no_unpatch(): Foo, PatchedFoo = simple_classes() f = Foo("Ko", True) def func1(): Patcher(f, PatchedFoo) def func2(): Patcher(f, PatchedFoo, unpatch_on_delete=False) func1() assert f.compute_string() == "(Ko)" assert f.compute_bool() with pytest.raises(Exception): f.added_method() func2() assert f.compute_string() == "(Ko)_patched" assert not f.compute_bool() f.added_method() monkeypatch.unpatch(f) assert f.compute_string() == "(Ko)" assert f.compute_bool() with pytest.raises(Exception): f.added_method()
def test_force_repatch(): Foo, PatchedFoo = simple_classes() class OtherPatch: def compute_string(self): return "second" f = Foo("val", True) patch(Foo, PatchedFoo) assert f.compute_string() == "(val)_patched" patch(Foo, OtherPatch) assert f.compute_string() == "(val)_patched" patch(Foo, OtherPatch, True) assert f.compute_string() == "second" unpatch(Foo) assert f.compute_string() == "(val)"