Skip to content

Commit

Permalink
Don't enter a transaction in MockOSDBMixin when starting a request
Browse files Browse the repository at this point in the history
In OpenSearch we're currently not using transactions. To accurately mock this with
the MySQL backend we instead enter a transaction in each method call.
  • Loading branch information
chrisburr committed Jan 16, 2025
1 parent 4d40913 commit 2a46eea
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions diracx-testing/src/diracx/testing/mock_osdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,22 @@ async def client_context(self) -> AsyncIterator[None]:
yield

async def __aenter__(self):
await self._sql_db.__aenter__()
"""Enter the request context.
This is a no-op as the real OpenSearch class doesn't use transactions.
Instead we enter a transaction in each method that needs it.
"""
return self

async def __aexit__(self, exc_type, exc_value, traceback):
await self._sql_db.__aexit__(exc_type, exc_value, traceback)
pass

async def create_index_template(self) -> None:
async with self._sql_db.engine.begin() as conn:
await conn.run_sync(self._sql_db.metadata.create_all)

async def upsert(self, doc_id, document) -> None:
async with self:
async with self._sql_db:
values = {}
for key, value in document.items():
if key in self.fields:
Expand All @@ -106,7 +110,7 @@ async def search(
per_page: int = 100,
page: int | None = None,
) -> tuple[int, list[dict[Any, Any]]]:
async with self:
async with self._sql_db:
# Apply selection
if parameters:
columns = []
Expand Down Expand Up @@ -150,7 +154,8 @@ async def search(
return results

async def ping(self):
return await self._sql_db.ping()
async with self._sql_db:
return await self._sql_db.ping()


def fake_available_osdb_implementations(name, *, real_available_implementations):
Expand Down

0 comments on commit 2a46eea

Please sign in to comment.