Skip to content

@mocketize as TestCase class decorator #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions mocket/decorators/mocketizer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from __future__ import annotations

import functools
import inspect

from mocket.mocket import Mocket
from mocket.mode import MocketMode
from mocket.utils import get_mocketize
Expand Down Expand Up @@ -60,7 +65,7 @@
def factory(test, truesocket_recording_dir, strict_mode, strict_mode_allowed, args):
instance = args[0] if args else None
namespace = None
if truesocket_recording_dir:
if truesocket_recording_dir and instance:
namespace = ".".join(
(
instance.__class__.__module__,
Expand All @@ -78,7 +83,7 @@
)


def wrapper(
def _function_wrapper(
test,
truesocket_recording_dir=None,
strict_mode=False,
Expand All @@ -92,4 +97,54 @@
return test(*args, **kwargs)


mocketize = get_mocketize(wrapper)
_function_mocketize = get_mocketize(_function_wrapper)


def _class_decorator_factory(**options):
def decorator(cls):
orig_setup = getattr(cls, "setUp", lambda self, *a, **kw: None)
orig_td = getattr(cls, "tearDown", lambda self, *a, **kw: None)
use_add_cleanup = hasattr(cls, "addCleanup")

Check warning on line 107 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L104-L107

Added lines #L104 - L107 were not covered by tests

def setUp(self, *a, **kw):
ctx = Mocketizer(instance=self, **options)
ctx.enter()
if use_add_cleanup:
self.addCleanup(ctx.exit)

Check warning on line 113 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L109-L113

Added lines #L109 - L113 were not covered by tests
else:
self.__mocket_ctx = ctx
orig_setup(self, *a, **kw)

Check warning on line 116 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L115-L116

Added lines #L115 - L116 were not covered by tests

cls.setUp = functools.wraps(orig_setup)(setUp)

Check warning on line 118 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L118

Added line #L118 was not covered by tests

if not use_add_cleanup:

Check warning on line 120 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L120

Added line #L120 was not covered by tests

def tearDown(self, *a, **kw):
try:
orig_td(self, *a, **kw)

Check warning on line 124 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L122-L124

Added lines #L122 - L124 were not covered by tests
finally:
if hasattr(self, "__mocket_ctx"):
self.__mocket_ctx.exit()

Check warning on line 127 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L126-L127

Added lines #L126 - L127 were not covered by tests

cls.tearDown = functools.wraps(orig_td)(tearDown)

Check warning on line 129 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L129

Added line #L129 was not covered by tests

return cls

Check warning on line 131 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L131

Added line #L131 was not covered by tests

return decorator

Check warning on line 133 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L133

Added line #L133 was not covered by tests


def mocketize(*dargs, **dkwargs):
# bare @mocketize
if dargs and len(dargs) == 1 and callable(dargs[0]) and not dkwargs:
target = dargs[0]
if inspect.isclass(target):
return _class_decorator_factory()(target)

Check warning on line 141 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L141

Added line #L141 was not covered by tests
return _function_mocketize(target)

# @mocketize(...)
def real_decorator(target):
if inspect.isclass(target):
return _class_decorator_factory(**dkwargs)(target)

Check warning on line 147 in mocket/decorators/mocketizer.py

View check run for this annotation

Codecov / codecov/patch

mocket/decorators/mocketizer.py#L147

Added line #L147 was not covered by tests
return _function_mocketize(**dkwargs)(target)

return real_decorator