Skip to content

Commit 74b480d

Browse files
author
Sergio García Prado
committed
ISSUE #99
* Add support for handling of not found cases to `LmdbDatabaseClient`.
1 parent 4394cb5 commit 74b480d

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

packages/core/minos-microservice-common/minos/common/database/mixins.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from collections.abc import (
2+
AsyncIterator,
3+
)
14
from contextlib import (
25
suppress,
36
)
47
from typing import (
5-
AsyncIterator,
8+
Any,
69
Generic,
710
Optional,
811
TypeVar,
@@ -101,7 +104,7 @@ def _get_generic_operation_factory(self) -> Optional[type[GenericDatabaseOperati
101104
raise TypeError(f"{type(self)!r} must contain a {DatabaseOperationFactory!r} as generic value.")
102105
return operation_factory_cls
103106

104-
async def execute_on_database_and_fetch_one(self, operation: DatabaseOperation) -> tuple:
107+
async def execute_on_database_and_fetch_one(self, operation: DatabaseOperation) -> Any:
105108
"""Submit an Operation and get the first response.
106109
107110
:param operation: The operation to be executed.

packages/core/minos-microservice-saga/minos/saga/executions/repositories/database/impl.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from minos.common import (
1313
DatabaseMixin,
14+
ProgrammingException,
1415
)
1516

1617
from ....exceptions import (
@@ -46,8 +47,9 @@ async def _delete(self, uuid: UUID) -> None:
4647
async def _load(self, uuid: UUID) -> SagaExecution:
4748
operation = self.database_operation_factory.build_load(uuid)
4849

49-
value = await self.execute_on_database_and_fetch_one(operation)
50-
if value is None:
50+
try:
51+
value = await self.execute_on_database_and_fetch_one(operation)
52+
except ProgrammingException:
5153
raise SagaExecutionNotFoundException(f"The execution identified by {uuid} was not found.")
5254
execution = SagaExecution.from_raw(value)
5355
return execution

packages/core/minos-microservice-saga/tests/test_saga/test_executions/test_repositories/test_database/test_impl.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from minos.common import (
77
DatabaseClient,
8+
ProgrammingException,
89
)
910
from minos.saga import (
1011
DatabaseSagaExecutionRepository,
@@ -39,15 +40,15 @@ async def test_load_from_str(self):
3940
await super().test_load_from_str()
4041

4142
async def test_load_raises(self):
42-
with patch.object(DatabaseClient, "fetch_one", side_effect=[None]):
43+
with patch.object(DatabaseClient, "fetch_one", side_effect=[ProgrammingException("")]):
4344
await super().test_load_raises()
4445

4546
async def test_delete(self):
46-
with patch.object(DatabaseClient, "fetch_one", side_effect=[None]):
47+
with patch.object(DatabaseClient, "fetch_one", side_effect=[ProgrammingException("")]):
4748
await super().test_delete()
4849

4950
async def test_delete_from_str(self):
50-
with patch.object(DatabaseClient, "fetch_one", side_effect=[None]):
51+
with patch.object(DatabaseClient, "fetch_one", side_effect=[ProgrammingException("")]):
5152
await super().test_delete_from_str()
5253

5354

packages/core/minos-microservice-saga/tests/test_saga/test_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
DatabaseClient,
1818
DatabaseClientPool,
1919
NotProvidedException,
20+
ProgrammingException,
2021
)
2122
from minos.networks import (
2223
REQUEST_HEADERS_CONTEXT_VAR,
@@ -109,7 +110,7 @@ async def test_run_with_pause_on_memory(self):
109110

110111
self.assertEqual(SagaStatus.Finished, execution.status)
111112
with self.assertRaises(SagaExecutionNotFoundException):
112-
with patch.object(DatabaseClient, "fetch_one", side_effect=[None]):
113+
with patch.object(DatabaseClient, "fetch_one", side_effect=[ProgrammingException("")]):
113114
await self.manager.storage.load(execution.uuid)
114115

115116
observed = self.broker_publisher.messages
@@ -190,7 +191,7 @@ async def test_run_with_pause_on_disk(self):
190191
self.assertEqual(SagaStatus.Finished, execution.status)
191192

192193
with self.assertRaises(SagaExecutionNotFoundException):
193-
with patch.object(DatabaseClient, "fetch_one", side_effect=[None]):
194+
with patch.object(DatabaseClient, "fetch_one", side_effect=[ProgrammingException("")]):
194195
await self.manager.storage.load(self.uuid)
195196

196197
observed = self.broker_publisher.messages

packages/plugins/minos-database-lmdb/minos/plugins/lmdb/clients.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
LmdbDatabaseOperationType,
2828
)
2929

30+
not_found_sentinel = object()
31+
3032

3133
class LmdbDatabaseClient(DatabaseClient):
3234
"""Lmdb Database Client class."""
@@ -52,7 +54,7 @@ def __init__(
5254
self._protocol = protocol
5355
self._tables = {}
5456

55-
self._prefetched = None
57+
self._prefetched = not_found_sentinel
5658

5759
self._environment = None
5860

@@ -79,8 +81,10 @@ async def _reset(self, **kwargs) -> None:
7981
self._environment.sync()
8082

8183
async def _fetch_all(self, *args, **kwargs) -> Any:
84+
if self._prefetched is not_found_sentinel:
85+
return
8286
prefetched = self._prefetched
83-
self._prefetched = None
87+
self._prefetched = not_found_sentinel
8488
yield prefetched
8589

8690
async def _execute(self, operation: DatabaseOperation) -> None:
@@ -109,11 +113,9 @@ def _create(self, table: str, key: str, value: Any, **kwargs) -> None:
109113
def _read(self, table: str, key: str, **kwargs):
110114
table = self._get_table(table)
111115
with self._environment.begin(db=table) as transaction:
112-
value_binary = transaction.get(key.encode())
113-
if value_binary is not None:
114-
value = self._protocol.decode(value_binary)
115-
else:
116-
value = None
116+
value = transaction.get(key.encode(), default=not_found_sentinel)
117+
if value is not not_found_sentinel:
118+
value = self._protocol.decode(value)
117119

118120
self._prefetched = value
119121

packages/plugins/minos-database-lmdb/tests/test_lmdb/test_clients.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from minos.common import (
88
DatabaseClient,
99
DatabaseOperation,
10+
ProgrammingException,
1011
)
1112
from minos.plugins.lmdb import (
1213
LmdbDatabaseClient,
@@ -146,7 +147,8 @@ async def test_execute_delete(self):
146147
self.assertEqual("Text Second Value", await client.fetch_one())
147148

148149
await client.execute(read_op_2)
149-
self.assertEqual(None, await client.fetch_one())
150+
with self.assertRaises(ProgrammingException):
151+
self.assertEqual(None, await client.fetch_one())
150152

151153
async def test_execute_update(self):
152154
create_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.CREATE, "TestOne", "first", "Text Value")

packages/plugins/minos-database-lmdb/tests/test_lmdb/test_factories/test_saga/test_factories.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def test_build_store(self):
2525
self.assertEqual(LmdbDatabaseOperationType.CREATE, operation.type_)
2626

2727
def test_build_load(self):
28-
pass
2928
factory = LmdbSagaExecutionDatabaseOperationFactory()
3029

3130
operation = factory.build_load(uuid4())

0 commit comments

Comments
 (0)