def test_patch_context_manager(): assert echo("foo") == "echo: foo" def monkey(arg): return f"monkey: {arg}" with Patch(get_defining_object(echo), "echo", monkey): assert echo("foo") == "monkey: foo" assert echo("foo") == "echo: foo"
def test_patch_with_pass_target_context_manager(): assert echo("foo") == "echo: foo" def uppercase(target, arg): return target(arg).upper() with Patch(get_defining_object(echo), "echo", uppercase): assert echo("foo") == "ECHO: FOO" assert echo("foo") == "echo: foo"
def test_get_defining_object(): from localstack.utils import common from localstack.utils.common import short_uid # module assert get_defining_object(short_uid) == common # unbound method (=function defined by a class) assert get_defining_object(MyEchoer.do_echo) == MyEchoer obj = MyEchoer() # bound method assert get_defining_object(obj.do_echo) == obj # class method referenced by an object assert get_defining_object(obj.do_class_echo) == MyEchoer # class method referenced by the class assert get_defining_object(MyEchoer.do_class_echo) == MyEchoer # static method (= function defined by a class) assert get_defining_object(MyEchoer.do_static_echo) == MyEchoer