Skip to content

Commit

Permalink
Use contextlib.ExitStack for managing multiple patchers
Browse files Browse the repository at this point in the history
  • Loading branch information
saltie2193 committed Oct 10, 2024
1 parent 7234aab commit ada33c9
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions backend/app/tests/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random
import string
from collections.abc import Generator
from contextlib import contextmanager
from contextlib import ExitStack, contextmanager
from unittest.mock import patch

from fastapi.testclient import TestClient
Expand Down Expand Up @@ -36,14 +36,10 @@ def patch_password_hashing(*modules: str) -> Generator[None, None, None]:
:param modules: list of modules to patch.
:return:
"""
patchers = []
for module in modules:
patcher_p = patch(f"{module}.pwd_context.verify", lambda x, y: x == y)
patcher_h = patch(f"{module}.pwd_context.hash", lambda x: x)
patcher_p.start()
patcher_h.start()

patchers.extend((patcher_p, patcher_h))
yield
for patcher in patchers:
patcher.stop()
with ExitStack() as stack:
for module in modules:
stack.enter_context(
patch(f"{module}.pwd_context.verify", lambda x, y: x == y)
)
stack.enter_context(patch(f"{module}.pwd_context.hash", lambda x: x))
yield

0 comments on commit ada33c9

Please sign in to comment.