diff --git a/tests/fixtures/api_admin.py b/tests/fixtures/api_admin.py index 9976ae5a9e..2a27738444 100644 --- a/tests/fixtures/api_admin.py +++ b/tests/fixtures/api_admin.py @@ -44,8 +44,10 @@ def request_context_with_admin(self, route, *args, **kwargs): flask.request.files = {} self.ctrl.db.session.begin_nested() flask.request.admin = admin - yield c - self.ctrl.db.session.commit() + try: + yield c + finally: + self.ctrl.db.session.commit() @contextmanager def request_context_with_library_and_admin(self, route, *args, **kwargs): @@ -57,8 +59,10 @@ def request_context_with_library_and_admin(self, route, *args, **kwargs): flask.request.files = {} self.ctrl.db.session.begin_nested() flask.request.admin = admin - yield c - self.ctrl.db.session.commit() + try: + yield c + finally: + self.ctrl.db.session.commit() @pytest.fixture(scope="function") diff --git a/tests/fixtures/flask.py b/tests/fixtures/flask.py index a44a92b8a6..485ae98c52 100644 --- a/tests/fixtures/flask.py +++ b/tests/fixtures/flask.py @@ -47,13 +47,14 @@ def test_request_context( flask.request.admin = admin # type: ignore[attr-defined] flask.request.form = ImmutableMultiDict() flask.request.files = ImmutableMultiDict() - yield c - - # Flush any changes that may have occurred during the request, then - # expire all objects to ensure that the next request will see the - # changes. - self.db.session.commit() - self.db.session.expire_all() + try: + yield c + finally: + # Flush any changes that may have occurred during the request, then + # expire all objects to ensure that the next request will see the + # changes. + self.db.session.commit() + self.db.session.expire_all() @contextmanager def test_request_context_system_admin( diff --git a/tests/fixtures/services.py b/tests/fixtures/services.py index 06556b1fa1..d8a6960932 100644 --- a/tests/fixtures/services.py +++ b/tests/fixtures/services.py @@ -34,8 +34,10 @@ def mock_services_container( from palace.manager.service import container container._container_instance = services_container - yield - container._container_instance = None + try: + yield + finally: + container._container_instance = None @dataclass @@ -197,8 +199,10 @@ def set_base_url(self, base_url: str | None) -> None: @contextmanager def wired(self) -> Generator[None, None, None]: wire_container(self.services) - yield - self.services.unwire() + try: + yield + finally: + self.services.unwire() @pytest.fixture