|
| 1 | +import unittest |
| 2 | +from unittest.mock import ( |
| 3 | + AsyncMock, |
| 4 | + call, |
| 5 | +) |
| 6 | +from uuid import ( |
| 7 | + UUID, |
| 8 | +) |
| 9 | + |
| 10 | +from minos.saga import ( |
| 11 | + SagaExecution, |
| 12 | + SagaExecutionRepository, |
| 13 | +) |
| 14 | +from tests.utils import ( |
| 15 | + ADD_ORDER, |
| 16 | + SagaTestCase, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class _SagaExecutionRepository(SagaExecutionRepository): |
| 21 | + async def _store(self, execution: SagaExecution) -> None: |
| 22 | + """For testing purposes.""" |
| 23 | + |
| 24 | + async def _load(self, uuid: UUID) -> SagaExecution: |
| 25 | + """For testing purposes.""" |
| 26 | + |
| 27 | + async def _delete(self, key: UUID) -> None: |
| 28 | + """For testing purposes.""" |
| 29 | + |
| 30 | + |
| 31 | +class TestSagaExecutionRepository(SagaTestCase): |
| 32 | + async def test_store(self): |
| 33 | + mock = AsyncMock() |
| 34 | + repository = _SagaExecutionRepository() |
| 35 | + repository._store = mock |
| 36 | + execution = SagaExecution.from_definition(ADD_ORDER) |
| 37 | + |
| 38 | + await repository.store(execution) |
| 39 | + |
| 40 | + self.assertEqual([call(execution)], mock.call_args_list) |
| 41 | + |
| 42 | + async def test_load(self): |
| 43 | + execution = SagaExecution.from_definition(ADD_ORDER) |
| 44 | + repository = _SagaExecutionRepository() |
| 45 | + mock = AsyncMock(return_value=execution) |
| 46 | + repository._load = mock |
| 47 | + |
| 48 | + observed = await repository.load(execution.uuid) |
| 49 | + |
| 50 | + self.assertEqual(execution, observed) |
| 51 | + self.assertEqual([call(execution.uuid)], mock.call_args_list) |
| 52 | + |
| 53 | + async def test_load_from_str(self): |
| 54 | + execution = SagaExecution.from_definition(ADD_ORDER) |
| 55 | + repository = _SagaExecutionRepository() |
| 56 | + mock = AsyncMock(return_value=execution) |
| 57 | + repository._load = mock |
| 58 | + |
| 59 | + observed = await repository.load(str(execution.uuid)) |
| 60 | + |
| 61 | + self.assertEqual(execution, observed) |
| 62 | + self.assertEqual([call(execution.uuid)], mock.call_args_list) |
| 63 | + |
| 64 | + async def test_delete(self): |
| 65 | + execution = SagaExecution.from_definition(ADD_ORDER) |
| 66 | + repository = _SagaExecutionRepository() |
| 67 | + mock = AsyncMock(return_value=execution) |
| 68 | + repository._delete = mock |
| 69 | + |
| 70 | + await repository.delete(execution) |
| 71 | + |
| 72 | + self.assertEqual([call(execution.uuid)], mock.call_args_list) |
| 73 | + |
| 74 | + async def test_delete_from_uuid(self): |
| 75 | + execution = SagaExecution.from_definition(ADD_ORDER) |
| 76 | + repository = _SagaExecutionRepository() |
| 77 | + mock = AsyncMock(return_value=execution) |
| 78 | + repository._delete = mock |
| 79 | + |
| 80 | + await repository.delete(execution.uuid) |
| 81 | + |
| 82 | + self.assertEqual([call(execution.uuid)], mock.call_args_list) |
| 83 | + |
| 84 | + async def test_delete_from_str(self): |
| 85 | + execution = SagaExecution.from_definition(ADD_ORDER) |
| 86 | + repository = _SagaExecutionRepository() |
| 87 | + mock = AsyncMock(return_value=execution) |
| 88 | + repository._delete = mock |
| 89 | + |
| 90 | + await repository.delete(str(execution.uuid)) |
| 91 | + |
| 92 | + self.assertEqual([call(execution.uuid)], mock.call_args_list) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + unittest.main() |
0 commit comments