Skip to content

Commit

Permalink
Fix a couple context managers in the test fixtures that weren't prope…
Browse files Browse the repository at this point in the history
…rly handling an exception raised within a fixture.
  • Loading branch information
jonathangreen committed Jun 3, 2024
1 parent b188b50 commit 5282fb6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
12 changes: 8 additions & 4 deletions tests/fixtures/api_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")
Expand Down
15 changes: 8 additions & 7 deletions tests/fixtures/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 8 additions & 4 deletions tests/fixtures/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5282fb6

Please sign in to comment.