From 788fd736254f36e68d29cfedb86aaf58cfa15aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 09:04:58 +0200 Subject: [PATCH 01/47] ISSUE #150 * Migrate `minos.common.MinosRepository.select` return from `list[MinosRepositoryEntry]` to `AsyncIterator[MinosRepositoryEntry]`. --- minos/common/model/aggregate.py | 2 +- minos/common/repository/abc.py | 14 +++-- minos/common/repository/memory.py | 11 ++-- minos/common/repository/pg.py | 55 ++++++++++++------- .../test_repository/test_memory.py | 37 ++++++------- tests/test_common/test_repository/test_pg.py | 12 ++-- 6 files changed, 73 insertions(+), 58 deletions(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index e1f59611..52b9288d 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -88,7 +88,7 @@ async def get_one( _repository = cls._build_repository(_config) # noinspection PyTypeChecker - entries = await _repository.select(aggregate_name=cls.classname, aggregate_id=id) + entries = [v async for v in _repository.select(aggregate_name=cls.classname, aggregate_id=id)] if not len(entries): raise MinosRepositoryAggregateNotFoundException(f"Not found any entries for the {repr(id)} id.") diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index 1529b867..77d0a4b8 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -16,6 +16,7 @@ ) from typing import ( TYPE_CHECKING, + AsyncIterator, NoReturn, Optional, Union, @@ -33,7 +34,9 @@ ) if TYPE_CHECKING: - from ..model import Aggregate + from ..model import ( + Aggregate, + ) class MinosRepository(ABC, MinosSetup): @@ -127,7 +130,7 @@ async def select( id_gt: Optional[int] = None, id_le: Optional[int] = None, id_ge: Optional[int] = None, - ) -> list[MinosRepositoryEntry]: + ) -> AsyncIterator[MinosRepositoryEntry]: """Perform a selection query of entries stored in to the repository. :param aggregate_id: Aggregate identifier. @@ -146,7 +149,7 @@ async def select( """ await self.setup() - return await self._select( + generator = self._select( aggregate_id=aggregate_id, aggregate_name=aggregate_name, version=version, @@ -160,7 +163,10 @@ async def select( id_le=id_le, id_ge=id_ge, ) + # noinspection PyTypeChecker + async for entry in generator: + yield entry @abstractmethod - async def _select(self, *args, **kwargs) -> list[MinosRepositoryEntry]: + async def _select(self, *args, **kwargs) -> AsyncIterator[MinosRepositoryEntry]: """Perform a selection query of entries stored in to the repository.""" diff --git a/minos/common/repository/memory.py b/minos/common/repository/memory.py index bd6ff35c..63d3ac78 100644 --- a/minos/common/repository/memory.py +++ b/minos/common/repository/memory.py @@ -13,10 +13,9 @@ count, ) from typing import ( - TYPE_CHECKING, + AsyncIterator, NoReturn, Optional, - Union, ) from .abc import ( @@ -26,9 +25,6 @@ MinosRepositoryEntry, ) -if TYPE_CHECKING: - from ..model import Aggregate - class MinosInMemoryRepository(MinosRepository): """Memory-based implementation of the repository class in ``minos``.""" @@ -96,7 +92,7 @@ async def _select( id_ge: Optional[int] = None, *args, **kwargs - ) -> list[MinosRepositoryEntry]: + ) -> AsyncIterator[MinosRepositoryEntry]: # noinspection DuplicatedCode def _fn_filter(entry: MinosRepositoryEntry) -> bool: @@ -128,4 +124,5 @@ def _fn_filter(entry: MinosRepositoryEntry) -> bool: iterable = iter(self._storage) iterable = filter(_fn_filter, iterable) - return list(iterable) + for item in iterable: + yield item diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 6575332e..7609654c 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -6,8 +6,9 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ from typing import ( - Any, - Optional, + AsyncIterator, + Generator, + NoReturn, ) import aiopg @@ -50,8 +51,8 @@ async def _setup(self): await self._create_events_table() async def _create_events_table(self): - await self._submit_sql(_CREATE_ACTION_ENUM_QUERY, fetch=False) - await self._submit_sql(_CREATE_TABLE_QUERY, fetch=False) + await self._submit_sql(_CREATE_ACTION_ENUM_QUERY) + await self._submit_sql(_CREATE_TABLE_QUERY) async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: params = { @@ -60,36 +61,48 @@ async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: "aggregate_name": entry.aggregate_name, "data": entry.data, } - response = await self._submit_sql(_INSERT_VALUES_QUERY, params) - entry.id = response[0][0] - entry.aggregate_id = response[0][1] - entry.version = response[0][2] + response = await self._submit_and_fetchone_sql(_INSERT_VALUES_QUERY, params) + entry.id = response[0] + entry.aggregate_id = response[1] + entry.version = response[2] return entry async def _select( - self, aggregate_id: int = None, aggregate_name: str = None, *args, **kwargs, - ) -> list[MinosRepositoryEntry]: + self, + aggregate_id: int = None, + aggregate_name: str = None, + *args, + **kwargs, + ) -> AsyncIterator[MinosRepositoryEntry]: if aggregate_id is None and aggregate_name is None: - response = await self._submit_sql(_SELECT_ALL_ENTRIES_QUERY) - entries = [MinosRepositoryEntry(*row) for row in response] + async for row in self._submit_and_iter_sql(_SELECT_ALL_ENTRIES_QUERY): + yield MinosRepositoryEntry(*row) else: - params = (aggregate_id, aggregate_name) - response = await self._submit_sql(_SELECT_ENTRIES_QUERY, params) - entries = [MinosRepositoryEntry(aggregate_id, aggregate_name, *row) for row in response] + async for row in self._submit_and_iter_sql(_SELECT_ENTRIES_QUERY, (aggregate_id, aggregate_name)): + yield MinosRepositoryEntry(aggregate_id, aggregate_name, *row) + + async def _submit_and_fetchone_sql(self, *args, **kwargs) -> tuple: + return await self._submit_and_iter_sql(*args, **kwargs).__anext__() - return entries + async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> Generator[tuple, None, None]: + async with self._connection() as connect: + async with connect.cursor() as cursor: + await cursor.execute(query, *args, **kwargs) + async for row in cursor: + yield row - async def _submit_sql(self, query: str, *args, fetch: bool = True, **kwargs) -> Optional[list[tuple[Any, ...]]]: + async def _submit_sql(self, query: str, *args, **kwargs) -> NoReturn: async with self._connection() as connect: async with connect.cursor() as cursor: await cursor.execute(query, *args, **kwargs) - if not fetch: - return None - return await cursor.fetchall() def _connection(self): return aiopg.connect( - host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, + host=self.host, + port=self.port, + dbname=self.database, + user=self.user, + password=self.password, ) diff --git a/tests/test_common/test_repository/test_memory.py b/tests/test_common/test_repository/test_memory.py index 6915b731..0609a4e2 100644 --- a/tests/test_common/test_repository/test_memory.py +++ b/tests/test_common/test_repository/test_memory.py @@ -27,7 +27,7 @@ async def test_insert(self): expected = [ MinosRepositoryEntry(1, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.INSERT) ] - self.assertEqual(expected, await repository.select()) + self.assertEqual(expected, [v async for v in repository.select()]) async def test_update(self): async with MinosInMemoryRepository() as repository: @@ -35,14 +35,13 @@ async def test_update(self): expected = [ MinosRepositoryEntry(0, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.UPDATE) ] - self.assertEqual(expected, await repository.select()) + self.assertEqual(expected, [v async for v in repository.select()]) async def test_delete(self): async with MinosInMemoryRepository() as repository: - repository = MinosInMemoryRepository() await repository.delete(MinosRepositoryEntry(0, "example.Car", 1, bytes())) expected = [MinosRepositoryEntry(0, "example.Car", 1, bytes(), 1, MinosRepositoryAction.DELETE)] - self.assertEqual(expected, await repository.select()) + self.assertEqual(expected, [v async for v in repository.select()]) async def test_select(self): repository = await self._build_repository() @@ -55,18 +54,18 @@ async def test_select(self): MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"), 7, MinosRepositoryAction.INSERT), ] - self.assertEqual(expected, await repository.select()) + self.assertEqual(expected, [v async for v in repository.select()]) async def test_select_empty(self): repository = MinosInMemoryRepository() - self.assertEqual([], await repository.select()) + self.assertEqual([], [v async for v in repository.select()]) async def test_select_id(self): repository = await self._build_repository() expected = [ MinosRepositoryEntry(1, "example.Car", 2, bytes("bar", "utf-8"), 2, MinosRepositoryAction.UPDATE), ] - self.assertEqual(expected, await repository.select(id=2)) + self.assertEqual(expected, [v async for v in repository.select(id=2)]) async def test_select_id_lt(self): repository = await self._build_repository() @@ -76,7 +75,7 @@ async def test_select_id_lt(self): MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), MinosRepositoryEntry(1, "example.Car", 3, bytes("foobar", "utf-8"), 4, MinosRepositoryAction.UPDATE), ] - self.assertEqual(expected, await repository.select(id_lt=5)) + self.assertEqual(expected, [v async for v in repository.select(id_lt=5)]) async def test_select_id_gt(self): repository = await self._build_repository() @@ -85,7 +84,7 @@ async def test_select_id_gt(self): MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"), 7, MinosRepositoryAction.INSERT), ] - self.assertEqual(expected, await repository.select(id_gt=4)) + self.assertEqual(expected, [v async for v in repository.select(id_gt=4)]) async def test_select_id_le(self): repository = await self._build_repository() @@ -95,7 +94,7 @@ async def test_select_id_le(self): MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), MinosRepositoryEntry(1, "example.Car", 3, bytes("foobar", "utf-8"), 4, MinosRepositoryAction.UPDATE), ] - self.assertEqual(expected, await repository.select(id_le=4)) + self.assertEqual(expected, [v async for v in repository.select(id_le=4)]) async def test_select_id_ge(self): repository = await self._build_repository() @@ -104,7 +103,7 @@ async def test_select_id_ge(self): MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"), 7, MinosRepositoryAction.INSERT), ] - self.assertEqual(expected, await repository.select(id_ge=5)) + self.assertEqual(expected, [v async for v in repository.select(id_ge=5)]) async def test_select_aggregate_id(self): repository = await self._build_repository() @@ -112,21 +111,21 @@ async def test_select_aggregate_id(self): MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), ] - self.assertEqual(expected, await repository.select(aggregate_id=2)) + self.assertEqual(expected, [v async for v in repository.select(aggregate_id=2)]) async def test_select_aggregate_name(self): repository = await self._build_repository() expected = [ MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"), 7, MinosRepositoryAction.INSERT), ] - self.assertEqual(expected, await repository.select(aggregate_name="example.MotorCycle")) + self.assertEqual(expected, [v async for v in repository.select(aggregate_name="example.MotorCycle")]) async def test_select_version(self): repository = await self._build_repository() expected = [ MinosRepositoryEntry(1, "example.Car", 4, bytes(), 5, MinosRepositoryAction.DELETE), ] - self.assertEqual(expected, await repository.select(version=4)) + self.assertEqual(expected, [v async for v in repository.select(version=4)]) async def test_select_version_lt(self): repository = await self._build_repository() @@ -135,7 +134,7 @@ async def test_select_version_lt(self): MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"), 7, MinosRepositoryAction.INSERT), ] - self.assertEqual(expected, await repository.select(version_lt=2)) + self.assertEqual(expected, [v async for v in repository.select(version_lt=2)]) async def test_select_version_gt(self): repository = await self._build_repository() @@ -145,7 +144,7 @@ async def test_select_version_gt(self): MinosRepositoryEntry(1, "example.Car", 4, bytes(), 5, MinosRepositoryAction.DELETE), MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), ] - self.assertEqual(expected, await repository.select(version_gt=1)) + self.assertEqual(expected, [v async for v in repository.select(version_gt=1)]) async def test_select_version_le(self): repository = await self._build_repository() @@ -154,7 +153,7 @@ async def test_select_version_le(self): MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"), 7, MinosRepositoryAction.INSERT), ] - self.assertEqual(expected, await repository.select(version_le=1)) + self.assertEqual(expected, [v async for v in repository.select(version_le=1)]) async def test_select_version_ge(self): repository = await self._build_repository() @@ -164,7 +163,7 @@ async def test_select_version_ge(self): MinosRepositoryEntry(1, "example.Car", 4, bytes(), 5, MinosRepositoryAction.DELETE), MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), ] - self.assertEqual(expected, await repository.select(version_ge=2)) + self.assertEqual(expected, [v async for v in repository.select(version_ge=2)]) async def test_select_combine(self): repository = await self._build_repository() @@ -173,7 +172,7 @@ async def test_select_combine(self): MinosRepositoryEntry(1, "example.Car", 4, bytes(), 5, MinosRepositoryAction.DELETE), MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), ] - self.assertEqual(expected, await repository.select(aggregate_name="example.Car", id_ge=4)) + self.assertEqual(expected, [v async for v in repository.select(aggregate_name="example.Car", id_ge=4)]) @staticmethod async def _build_repository(): diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 36a8713c..e1790bfd 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -74,7 +74,7 @@ async def test_insert(self): expected = [ MinosRepositoryEntry(1, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.INSERT) ] - self.assertEqual(expected, await repository.select()) + self.assertEqual(expected, [v async for v in repository.select()]) async def test_update(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: @@ -82,13 +82,13 @@ async def test_update(self): expected = [ MinosRepositoryEntry(0, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.UPDATE) ] - self.assertEqual(expected, await repository.select()) + self.assertEqual(expected, [v async for v in repository.select()]) async def test_delete(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: await repository.delete(MinosRepositoryEntry(0, "example.Car", 1, bytes())) expected = [MinosRepositoryEntry(0, "example.Car", 1, bytes(), 1, MinosRepositoryAction.DELETE)] - self.assertEqual(expected, await repository.select()) + self.assertEqual(expected, [v async for v in repository.select()]) async def test_select(self): repository = await self._build_repository() @@ -101,11 +101,11 @@ async def test_select(self): MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"), 7, MinosRepositoryAction.INSERT), ] - self.assertEqual(expected, await repository.select()) + self.assertEqual(expected, [v async for v in repository.select()]) async def test_select_empty(self): repository = PostgreSqlMinosRepository(**self.repository_db) - self.assertEqual([], await repository.select()) + self.assertEqual([], [v async for v in repository.select()]) async def test_select_filtered(self): repository = await self._build_repository() @@ -113,7 +113,7 @@ async def test_select_filtered(self): MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), ] - self.assertEqual(expected, await repository.select(aggregate_name="example.Car", aggregate_id=2)) + self.assertEqual(expected, [v async for v in repository.select(aggregate_name="example.Car", aggregate_id=2)]) async def _build_repository(self): repository = PostgreSqlMinosRepository(**self.repository_db) From cf43271a5bc22008748e002c457e5a1a6d264ed9 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 30 Apr 2021 07:10:30 +0000 Subject: [PATCH 02/47] Restyled by black --- minos/common/model/aggregate.py | 20 +++++--------------- minos/common/repository/abc.py | 16 ++++------------ minos/common/repository/memory.py | 16 ++++------------ minos/common/repository/pg.py | 20 ++++---------------- tests/test_common/test_repository/test_pg.py | 12 +++--------- 5 files changed, 20 insertions(+), 64 deletions(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index 52b9288d..ddc9c7af 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -5,21 +5,15 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations -from operator import ( - attrgetter, -) +from operator import attrgetter from typing import ( NoReturn, Optional, ) -from ..configuration import ( - MinosConfig, -) +from ..configuration import MinosConfig from ..exceptions import ( MinosRepositoryAggregateNotFoundException, MinosRepositoryDeletedAggregateException, @@ -27,16 +21,12 @@ MinosRepositoryManuallySetAggregateVersionException, MinosRepositoryNonProvidedException, ) -from ..meta import ( - self_or_classmethod, -) +from ..meta import self_or_classmethod from ..repository import ( MinosRepository, MinosRepositoryAction, ) -from .abc import ( - MinosModel, -) +from .abc import MinosModel class Aggregate(MinosModel): diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index 77d0a4b8..9082a629 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -6,9 +6,7 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations from abc import ( ABC, @@ -22,21 +20,15 @@ Union, ) -from ..configuration import ( - MinosConfig, -) -from ..setup import ( - MinosSetup, -) +from ..configuration import MinosConfig +from ..setup import MinosSetup from .entries import ( MinosRepositoryAction, MinosRepositoryEntry, ) if TYPE_CHECKING: - from ..model import ( - Aggregate, - ) + from ..model import Aggregate class MinosRepository(ABC, MinosSetup): diff --git a/minos/common/repository/memory.py b/minos/common/repository/memory.py index 63d3ac78..3ede9f5e 100644 --- a/minos/common/repository/memory.py +++ b/minos/common/repository/memory.py @@ -5,25 +5,17 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations -from itertools import ( - count, -) +from itertools import count from typing import ( AsyncIterator, NoReturn, Optional, ) -from .abc import ( - MinosRepository, -) -from .entries import ( - MinosRepositoryEntry, -) +from .abc import MinosRepository +from .entries import MinosRepositoryEntry class MinosInMemoryRepository(MinosRepository): diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 7609654c..1ee51c2a 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -13,12 +13,8 @@ import aiopg -from .abc import ( - MinosRepository, -) -from .entries import ( - MinosRepositoryEntry, -) +from .abc import MinosRepository +from .entries import MinosRepositoryEntry class PostgreSqlMinosRepository(MinosRepository): @@ -68,11 +64,7 @@ async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: return entry async def _select( - self, - aggregate_id: int = None, - aggregate_name: str = None, - *args, - **kwargs, + self, aggregate_id: int = None, aggregate_name: str = None, *args, **kwargs, ) -> AsyncIterator[MinosRepositoryEntry]: if aggregate_id is None and aggregate_name is None: async for row in self._submit_and_iter_sql(_SELECT_ALL_ENTRIES_QUERY): @@ -98,11 +90,7 @@ async def _submit_sql(self, query: str, *args, **kwargs) -> NoReturn: def _connection(self): return aiopg.connect( - host=self.host, - port=self.port, - dbname=self.database, - user=self.user, - password=self.password, + host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, ) diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index e1790bfd..92c61522 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -15,15 +15,9 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import ( - PostgresAsyncTestCase, -) -from tests.aggregate_classes import ( - Car, -) -from tests.utils import ( - BASE_PATH, -) +from minos.common.testing import PostgresAsyncTestCase +from tests.aggregate_classes import Car +from tests.utils import BASE_PATH class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From 004219e0d53a7119b4bc5f151c214f09523f8d7f Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 30 Apr 2021 07:10:43 +0000 Subject: [PATCH 03/47] Restyled by isort --- minos/common/model/aggregate.py | 20 +++++++++++++++----- minos/common/repository/abc.py | 12 +++++++++--- minos/common/repository/memory.py | 16 ++++++++++++---- minos/common/repository/pg.py | 8 ++++++-- tests/test_common/test_repository/test_pg.py | 12 +++++++++--- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index ddc9c7af..52b9288d 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -5,15 +5,21 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) -from operator import attrgetter +from operator import ( + attrgetter, +) from typing import ( NoReturn, Optional, ) -from ..configuration import MinosConfig +from ..configuration import ( + MinosConfig, +) from ..exceptions import ( MinosRepositoryAggregateNotFoundException, MinosRepositoryDeletedAggregateException, @@ -21,12 +27,16 @@ MinosRepositoryManuallySetAggregateVersionException, MinosRepositoryNonProvidedException, ) -from ..meta import self_or_classmethod +from ..meta import ( + self_or_classmethod, +) from ..repository import ( MinosRepository, MinosRepositoryAction, ) -from .abc import MinosModel +from .abc import ( + MinosModel, +) class Aggregate(MinosModel): diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index 9082a629..2c823148 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -6,7 +6,9 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) from abc import ( ABC, @@ -20,8 +22,12 @@ Union, ) -from ..configuration import MinosConfig -from ..setup import MinosSetup +from ..configuration import ( + MinosConfig, +) +from ..setup import ( + MinosSetup, +) from .entries import ( MinosRepositoryAction, MinosRepositoryEntry, diff --git a/minos/common/repository/memory.py b/minos/common/repository/memory.py index 3ede9f5e..63d3ac78 100644 --- a/minos/common/repository/memory.py +++ b/minos/common/repository/memory.py @@ -5,17 +5,25 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) -from itertools import count +from itertools import ( + count, +) from typing import ( AsyncIterator, NoReturn, Optional, ) -from .abc import MinosRepository -from .entries import MinosRepositoryEntry +from .abc import ( + MinosRepository, +) +from .entries import ( + MinosRepositoryEntry, +) class MinosInMemoryRepository(MinosRepository): diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 1ee51c2a..f87e2a3e 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -13,8 +13,12 @@ import aiopg -from .abc import MinosRepository -from .entries import MinosRepositoryEntry +from .abc import ( + MinosRepository, +) +from .entries import ( + MinosRepositoryEntry, +) class PostgreSqlMinosRepository(MinosRepository): diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 92c61522..e1790bfd 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -15,9 +15,15 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import PostgresAsyncTestCase -from tests.aggregate_classes import Car -from tests.utils import BASE_PATH +from minos.common.testing import ( + PostgresAsyncTestCase, +) +from tests.aggregate_classes import ( + Car, +) +from tests.utils import ( + BASE_PATH, +) class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From a9980ce77eade33ce800b9d9ff7f8e4cf3e04573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 09:17:16 +0200 Subject: [PATCH 04/47] ISSUE #153 * Generate unique identifiers for database testing in order to avoid collisions. --- minos/common/testing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/minos/common/testing.py b/minos/common/testing.py index 5b5746f3..1d33fffe 100644 --- a/minos/common/testing.py +++ b/minos/common/testing.py @@ -16,6 +16,7 @@ Any, NoReturn, ) +from uuid import uuid4 import aiopg @@ -28,6 +29,7 @@ class PostgresAsyncTestCase(unittest.IsolatedAsyncioTestCase): CONFIG_FILE_PATH: Path def setUp(self) -> None: + self._uuid = uuid4() self._config = MinosConfig(self.CONFIG_FILE_PATH) self._meta_repository_db = self._config.repository._asdict() @@ -40,7 +42,7 @@ def setUp(self) -> None: self._meta_commands_queue_db.pop("records") self._meta_commands_queue_db.pop("retry") - self._test_db = {"database": "test_db", "user": "test_user"} + self._test_db = {"database": f"test_db_{self._uuid.hex}", "user": f"test_user_{self._uuid.hex}"} self.repository_db = self._meta_repository_db | self._test_db self.events_queue_db = self._meta_events_queue_db | self._test_db From d654dab70c53d099e9b244c15f01cdc8668fc06d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 09:35:35 +0200 Subject: [PATCH 05/47] ISSUE #153 * Minor change. --- minos/common/testing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/minos/common/testing.py b/minos/common/testing.py index 1d33fffe..5b811745 100644 --- a/minos/common/testing.py +++ b/minos/common/testing.py @@ -16,7 +16,9 @@ Any, NoReturn, ) -from uuid import uuid4 +from uuid import ( + uuid4, +) import aiopg From 642883a97d2dbdfedaf4c72c15b707befd84bf71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 09:46:03 +0200 Subject: [PATCH 06/47] Revert "Merge remote-tracking branch 'origin/issue-153-unique-database' into issue-150-lazy-repository-select" This reverts commit c28649f5d97a1173ed6bb1a20a3a5253f2f75b12, reversing changes made to 7f4ecd8b259bb39d3f6aa007a09fdad7f173efe9. --- minos/common/testing.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/minos/common/testing.py b/minos/common/testing.py index 5b811745..5b5746f3 100644 --- a/minos/common/testing.py +++ b/minos/common/testing.py @@ -16,9 +16,6 @@ Any, NoReturn, ) -from uuid import ( - uuid4, -) import aiopg @@ -31,7 +28,6 @@ class PostgresAsyncTestCase(unittest.IsolatedAsyncioTestCase): CONFIG_FILE_PATH: Path def setUp(self) -> None: - self._uuid = uuid4() self._config = MinosConfig(self.CONFIG_FILE_PATH) self._meta_repository_db = self._config.repository._asdict() @@ -44,7 +40,7 @@ def setUp(self) -> None: self._meta_commands_queue_db.pop("records") self._meta_commands_queue_db.pop("retry") - self._test_db = {"database": f"test_db_{self._uuid.hex}", "user": f"test_user_{self._uuid.hex}"} + self._test_db = {"database": "test_db", "user": "test_user"} self.repository_db = self._meta_repository_db | self._test_db self.events_queue_db = self._meta_events_queue_db | self._test_db From 4cbf90dea884893fc4235ccf8741a14b302256e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 09:17:16 +0200 Subject: [PATCH 07/47] ISSUE #153 * Generate unique identifiers for database testing in order to avoid collisions. --- minos/common/testing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/minos/common/testing.py b/minos/common/testing.py index 5b5746f3..1d33fffe 100644 --- a/minos/common/testing.py +++ b/minos/common/testing.py @@ -16,6 +16,7 @@ Any, NoReturn, ) +from uuid import uuid4 import aiopg @@ -28,6 +29,7 @@ class PostgresAsyncTestCase(unittest.IsolatedAsyncioTestCase): CONFIG_FILE_PATH: Path def setUp(self) -> None: + self._uuid = uuid4() self._config = MinosConfig(self.CONFIG_FILE_PATH) self._meta_repository_db = self._config.repository._asdict() @@ -40,7 +42,7 @@ def setUp(self) -> None: self._meta_commands_queue_db.pop("records") self._meta_commands_queue_db.pop("retry") - self._test_db = {"database": "test_db", "user": "test_user"} + self._test_db = {"database": f"test_db_{self._uuid.hex}", "user": f"test_user_{self._uuid.hex}"} self.repository_db = self._meta_repository_db | self._test_db self.events_queue_db = self._meta_events_queue_db | self._test_db From a401fd3e82f98f091c547c527b965caf2b88b1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 09:35:35 +0200 Subject: [PATCH 08/47] ISSUE #153 * Minor change. --- minos/common/testing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/minos/common/testing.py b/minos/common/testing.py index 1d33fffe..5b811745 100644 --- a/minos/common/testing.py +++ b/minos/common/testing.py @@ -16,7 +16,9 @@ Any, NoReturn, ) -from uuid import uuid4 +from uuid import ( + uuid4, +) import aiopg From 36a028abf319dc168f04bad5ccde1b4cc73e672f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 11:44:04 +0200 Subject: [PATCH 09/47] ISSUE #150 * Commented test section. --- .../test_common/test_model/test_aggregate.py | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/test_common/test_model/test_aggregate.py b/tests/test_common/test_model/test_aggregate.py index 87793a9f..b01983ad 100644 --- a/tests/test_common/test_model/test_aggregate.py +++ b/tests/test_common/test_model/test_aggregate.py @@ -141,21 +141,21 @@ async def test_update(self): self.assertEqual(Car(1, 2, 3, "red"), car) self.assertEqual(car, await Car.get_one(car.id)) - await car.update(doors=5) - self.assertEqual(Car(1, 3, 5, "red"), car) - self.assertEqual(car, await Car.get_one(car.id)) - - await car.delete() - with self.assertRaises(MinosRepositoryDeletedAggregateException): - await Car.get_one(car.id) - - car = await Car.create(doors=3, color="blue") - await Car.update(car.id, color="red") - self.assertEqual(Car(2, 2, 3, "red"), await Car.get_one(car.id)) - - await Car.delete(car.id) - with self.assertRaises(MinosRepositoryDeletedAggregateException): - await Car.get_one(car.id) + # await car.update(doors=5) + # self.assertEqual(Car(1, 3, 5, "red"), car) + # self.assertEqual(car, await Car.get_one(car.id)) + # + # await car.delete() + # with self.assertRaises(MinosRepositoryDeletedAggregateException): + # await Car.get_one(car.id) + # + # car = await Car.create(doors=3, color="blue") + # await Car.update(car.id, color="red") + # self.assertEqual(Car(2, 2, 3, "red"), await Car.get_one(car.id)) + # + # await Car.delete(car.id) + # with self.assertRaises(MinosRepositoryDeletedAggregateException): + # await Car.get_one(car.id) if __name__ == "__main__": From 7ddfc118e8905cdc8f4c87bf387a703ba0579eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 11:48:51 +0200 Subject: [PATCH 10/47] ISSUE #150 * Trying to kill the bug. --- minos/common/repository/pg.py | 4 ++- .../test_common/test_model/test_aggregate.py | 30 +++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index f87e2a3e..900044d1 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -78,7 +78,9 @@ async def _select( yield MinosRepositoryEntry(aggregate_id, aggregate_name, *row) async def _submit_and_fetchone_sql(self, *args, **kwargs) -> tuple: - return await self._submit_and_iter_sql(*args, **kwargs).__anext__() + async for entry in self._submit_and_iter_sql(*args, **kwargs): + return entry + raise Exception async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> Generator[tuple, None, None]: async with self._connection() as connect: diff --git a/tests/test_common/test_model/test_aggregate.py b/tests/test_common/test_model/test_aggregate.py index b01983ad..87793a9f 100644 --- a/tests/test_common/test_model/test_aggregate.py +++ b/tests/test_common/test_model/test_aggregate.py @@ -141,21 +141,21 @@ async def test_update(self): self.assertEqual(Car(1, 2, 3, "red"), car) self.assertEqual(car, await Car.get_one(car.id)) - # await car.update(doors=5) - # self.assertEqual(Car(1, 3, 5, "red"), car) - # self.assertEqual(car, await Car.get_one(car.id)) - # - # await car.delete() - # with self.assertRaises(MinosRepositoryDeletedAggregateException): - # await Car.get_one(car.id) - # - # car = await Car.create(doors=3, color="blue") - # await Car.update(car.id, color="red") - # self.assertEqual(Car(2, 2, 3, "red"), await Car.get_one(car.id)) - # - # await Car.delete(car.id) - # with self.assertRaises(MinosRepositoryDeletedAggregateException): - # await Car.get_one(car.id) + await car.update(doors=5) + self.assertEqual(Car(1, 3, 5, "red"), car) + self.assertEqual(car, await Car.get_one(car.id)) + + await car.delete() + with self.assertRaises(MinosRepositoryDeletedAggregateException): + await Car.get_one(car.id) + + car = await Car.create(doors=3, color="blue") + await Car.update(car.id, color="red") + self.assertEqual(Car(2, 2, 3, "red"), await Car.get_one(car.id)) + + await Car.delete(car.id) + with self.assertRaises(MinosRepositoryDeletedAggregateException): + await Car.get_one(car.id) if __name__ == "__main__": From 7a9b5862d83250fc9bc00e635e475b6e6cc6255f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 11:55:36 +0200 Subject: [PATCH 11/47] ISSUE #150 * Minor change. --- minos/common/repository/pg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 900044d1..7799cf1d 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -80,9 +80,9 @@ async def _select( async def _submit_and_fetchone_sql(self, *args, **kwargs) -> tuple: async for entry in self._submit_and_iter_sql(*args, **kwargs): return entry - raise Exception + raise IndexError("Given query did not returned any entry.") - async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> Generator[tuple, None, None]: + async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> AsyncIterator[tuple]: async with self._connection() as connect: async with connect.cursor() as cursor: await cursor.execute(query, *args, **kwargs) From a693af78701947e9ac8ca838944b331d438cda1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 11:57:37 +0200 Subject: [PATCH 12/47] ISSUE #150 * commented line. --- minos/common/repository/pg.py | 1 - tests/test_common/test_repository/test_pg.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 7799cf1d..1ce7281c 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -7,7 +7,6 @@ """ from typing import ( AsyncIterator, - Generator, NoReturn, ) diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index e1790bfd..400021b1 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -62,8 +62,8 @@ async def test_aggregate(self): await car.update(color="red") await car.update(doors=5) - another = await Car.get_one(car.id, _repository=repository) - self.assertEqual(car, another) + # another = await Car.get_one(car.id, _repository=repository) + # self.assertEqual(car, another) await car.delete() From b54efe90024e35d4b65569911bcf7949b3148d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:02:00 +0200 Subject: [PATCH 13/47] ISSUE #150 * commented line (2). --- tests/test_common/test_repository/test_pg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 400021b1..58bfddc0 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -59,9 +59,9 @@ async def test_setup(self): async def test_aggregate(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: car = await Car.create(doors=3, color="blue", _repository=repository) - await car.update(color="red") - await car.update(doors=5) - + # await car.update(color="red") + # await car.update(doors=5) + # # another = await Car.get_one(car.id, _repository=repository) # self.assertEqual(car, another) From 1d680a0b78a57e42634fdca2dd799c8ddb8e11db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:05:17 +0200 Subject: [PATCH 14/47] ISSUE #150 * commented line (3). --- tests/test_common/test_repository/test_pg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 58bfddc0..b597bd44 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -65,7 +65,7 @@ async def test_aggregate(self): # another = await Car.get_one(car.id, _repository=repository) # self.assertEqual(car, another) - await car.delete() + # await car.delete() async def test_insert(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: From 7cb58ac383f2c1df2ec48c82ee2b59bd8fe7d222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:08:21 +0200 Subject: [PATCH 15/47] ISSUE #150 * Destroy state. --- tests/test_common/test_repository/test_pg.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index b597bd44..45b1007a 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -66,6 +66,7 @@ async def test_aggregate(self): # self.assertEqual(car, another) # await car.delete() + repository, car = None, None async def test_insert(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: From 2bd3c3118ff552cd9457a35d2e1c70b5ca8ce413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:16:03 +0200 Subject: [PATCH 16/47] ISSUE #150 * Minor change (3). --- tests/test_common/test_repository/test_pg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 45b1007a..6db9fe3b 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -58,7 +58,8 @@ async def test_setup(self): async def test_aggregate(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: - car = await Car.create(doors=3, color="blue", _repository=repository) + pass + # car = await Car.create(doors=3, color="blue", _repository=repository) # await car.update(color="red") # await car.update(doors=5) # @@ -66,7 +67,6 @@ async def test_aggregate(self): # self.assertEqual(car, another) # await car.delete() - repository, car = None, None async def test_insert(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: From 031329af3dbb20d29b9230e90904d22f10ff8e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:18:37 +0200 Subject: [PATCH 17/47] ISSUE #150 * Minor change (4). --- tests/test_common/test_repository/test_pg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 6db9fe3b..4fc73dec 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -58,7 +58,7 @@ async def test_setup(self): async def test_aggregate(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: - pass + await Car.create(doors=3, color="blue", _repository=repository) # car = await Car.create(doors=3, color="blue", _repository=repository) # await car.update(color="red") # await car.update(doors=5) From b8fa8e6a0974ba660aeabb7534e13373ecf28aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:23:56 +0200 Subject: [PATCH 18/47] ISSUE #150 * Minor change (5). --- minos/common/model/aggregate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index 52b9288d..d9d16742 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -53,7 +53,7 @@ def __init__( super().__init__(id, version, *args, **kwargs) self._broker = _broker - self._repository = _repository + self._repository = None @classmethod async def get( From da0faf6ecb5a19a7467ac5598659cb01018b19d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:29:12 +0200 Subject: [PATCH 19/47] ISSUE #150 * Minor change (6). --- minos/common/model/aggregate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index d9d16742..52b9288d 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -53,7 +53,7 @@ def __init__( super().__init__(id, version, *args, **kwargs) self._broker = _broker - self._repository = None + self._repository = _repository @classmethod async def get( From 66fa06dcd881b2356d304278d9966c46d7d1569c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:30:11 +0200 Subject: [PATCH 20/47] ISSUE #150 * Minor change (7). --- minos/common/model/aggregate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index 52b9288d..5ad6e82e 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -130,7 +130,7 @@ async def create( if _repository is None: _repository = cls._build_repository(_config) - instance = cls(0, 0, *args, _broker=_broker, _repository=_repository, **kwargs) + instance = cls(0, 0, *args, _broker=_broker, **kwargs) entry = await _repository.insert(instance) From a34202eae0c626715ac6cd0c5e83959b3bb32061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:36:35 +0200 Subject: [PATCH 21/47] ISSUE #150 * Minor change (8). --- minos/common/repository/abc.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index 2c823148..c7b12992 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -17,7 +17,6 @@ from typing import ( TYPE_CHECKING, AsyncIterator, - NoReturn, Optional, Union, ) @@ -63,7 +62,7 @@ async def __aenter__(self) -> MinosRepository: async def __aexit__(self, exc_type, exc_value, exc_traceback): pass - async def insert(self, entry: Union[Aggregate, MinosRepositoryEntry]) -> NoReturn: + async def insert(self, entry: Union[Aggregate, MinosRepositoryEntry]) -> MinosRepositoryEntry: """Store new insertion entry into de repository. :param entry: Entry to be stored. @@ -77,7 +76,7 @@ async def insert(self, entry: Union[Aggregate, MinosRepositoryEntry]) -> NoRetur entry.action = MinosRepositoryAction.INSERT return await self._submit(entry) - async def update(self, entry: Union[Aggregate, MinosRepositoryEntry]) -> NoReturn: + async def update(self, entry: Union[Aggregate, MinosRepositoryEntry]) -> MinosRepositoryEntry: """Store new update entry into de repository. :param entry: Entry to be stored. @@ -91,7 +90,7 @@ async def update(self, entry: Union[Aggregate, MinosRepositoryEntry]) -> NoRetur entry.action = MinosRepositoryAction.UPDATE return await self._submit(entry) - async def delete(self, entry: Union[Aggregate, MinosRepositoryEntry]) -> NoReturn: + async def delete(self, entry: Union[Aggregate, MinosRepositoryEntry]) -> MinosRepositoryEntry: """Store new deletion entry into de repository. :param entry: Entry to be stored. From 400562a77116e359276da295959d14c383a5d684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:36:58 +0200 Subject: [PATCH 22/47] ISSUE #150 * Minor change (9). --- minos/common/model/aggregate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index 5ad6e82e..52b9288d 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -130,7 +130,7 @@ async def create( if _repository is None: _repository = cls._build_repository(_config) - instance = cls(0, 0, *args, _broker=_broker, **kwargs) + instance = cls(0, 0, *args, _broker=_broker, _repository=_repository, **kwargs) entry = await _repository.insert(instance) From 0d3f84064392785fe79e5e30b9d08968b838dc39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:38:34 +0200 Subject: [PATCH 23/47] ISSUE #150 * Minor change (10). --- minos/common/model/aggregate.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index 52b9288d..e145faf8 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -132,10 +132,10 @@ async def create( instance = cls(0, 0, *args, _broker=_broker, _repository=_repository, **kwargs) - entry = await _repository.insert(instance) + await _repository.insert(instance) - instance.id = entry.aggregate_id - instance.version = entry.version + # instance.id = entry.aggregate_id + # instance.version = entry.version return instance From f0e45c4ce04e276e34371f9167a4660e6caa2486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:46:26 +0200 Subject: [PATCH 24/47] ISSUE #150 * Minor change (11). --- minos/common/model/aggregate.py | 6 +++--- tests/test_common/test_repository/test_pg.py | 17 ++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index e145faf8..52b9288d 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -132,10 +132,10 @@ async def create( instance = cls(0, 0, *args, _broker=_broker, _repository=_repository, **kwargs) - await _repository.insert(instance) + entry = await _repository.insert(instance) - # instance.id = entry.aggregate_id - # instance.version = entry.version + instance.id = entry.aggregate_id + instance.version = entry.version return instance diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 4fc73dec..e1790bfd 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -58,15 +58,14 @@ async def test_setup(self): async def test_aggregate(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: - await Car.create(doors=3, color="blue", _repository=repository) - # car = await Car.create(doors=3, color="blue", _repository=repository) - # await car.update(color="red") - # await car.update(doors=5) - # - # another = await Car.get_one(car.id, _repository=repository) - # self.assertEqual(car, another) - - # await car.delete() + car = await Car.create(doors=3, color="blue", _repository=repository) + await car.update(color="red") + await car.update(doors=5) + + another = await Car.get_one(car.id, _repository=repository) + self.assertEqual(car, another) + + await car.delete() async def test_insert(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: From 15271b30d798b5900dc7552175f9217e35c3842c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:51:04 +0200 Subject: [PATCH 25/47] ISSUE #150 * Minor change (12). --- minos/common/repository/pg.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 1ce7281c..384dab4e 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -39,6 +39,7 @@ def __init__( self.database = database self.user = user self.password = password + self.__connection = None async def _setup(self): """Setup miscellaneous repository thing. @@ -53,6 +54,11 @@ async def _create_events_table(self): await self._submit_sql(_CREATE_ACTION_ENUM_QUERY) await self._submit_sql(_CREATE_TABLE_QUERY) + async def __aexit__(self, exc_type, exc_value, exc_traceback): + if self.__connection is not None: + await self.__connection.close() + pass + async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: params = { "action": entry.action.value, @@ -82,21 +88,22 @@ async def _submit_and_fetchone_sql(self, *args, **kwargs) -> tuple: raise IndexError("Given query did not returned any entry.") async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> AsyncIterator[tuple]: - async with self._connection() as connect: - async with connect.cursor() as cursor: - await cursor.execute(query, *args, **kwargs) - async for row in cursor: - yield row + async with self._connection.cursor() as cursor: + await cursor.execute(query, *args, **kwargs) + async for row in cursor: + yield row async def _submit_sql(self, query: str, *args, **kwargs) -> NoReturn: - async with self._connection() as connect: - async with connect.cursor() as cursor: - await cursor.execute(query, *args, **kwargs) + async with self._connection.cursor() as cursor: + await cursor.execute(query, *args, **kwargs) + @property def _connection(self): - return aiopg.connect( - host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, - ) + if self.__connection is None: + self.__connection = await aiopg.connect( + host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, + ) + return self.__connection _CREATE_ACTION_ENUM_QUERY = """ From c251932e5df5f01ec71109236a9a018126b3fd6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 12:52:20 +0200 Subject: [PATCH 26/47] ISSUE #150 * Minor change (13). --- minos/common/repository/pg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 384dab4e..bc578057 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -88,17 +88,17 @@ async def _submit_and_fetchone_sql(self, *args, **kwargs) -> tuple: raise IndexError("Given query did not returned any entry.") async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> AsyncIterator[tuple]: - async with self._connection.cursor() as cursor: + async with (await self._connection).cursor() as cursor: await cursor.execute(query, *args, **kwargs) async for row in cursor: yield row async def _submit_sql(self, query: str, *args, **kwargs) -> NoReturn: - async with self._connection.cursor() as cursor: + async with (await self._connection).cursor() as cursor: await cursor.execute(query, *args, **kwargs) @property - def _connection(self): + async def _connection(self): if self.__connection is None: self.__connection = await aiopg.connect( host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, From 9b8a9c6b1670eb6638f65ca1435a1181051c873c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 13:04:00 +0200 Subject: [PATCH 27/47] ISSUE #150 * Minor change (14). --- minos/common/repository/pg.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index bc578057..02b9a03e 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -39,7 +39,7 @@ def __init__( self.database = database self.user = user self.password = password - self.__connection = None + self._pool = None async def _setup(self): """Setup miscellaneous repository thing. @@ -55,9 +55,8 @@ async def _create_events_table(self): await self._submit_sql(_CREATE_TABLE_QUERY) async def __aexit__(self, exc_type, exc_value, exc_traceback): - if self.__connection is not None: - await self.__connection.close() - pass + if self._pool is not None: + self._pool.close() async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: params = { @@ -88,22 +87,22 @@ async def _submit_and_fetchone_sql(self, *args, **kwargs) -> tuple: raise IndexError("Given query did not returned any entry.") async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> AsyncIterator[tuple]: - async with (await self._connection).cursor() as cursor: + if self._pool is None: + self._pool = await aiopg.create_pool( + host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, + ) + with (await self._pool.cursor()) as cursor: await cursor.execute(query, *args, **kwargs) async for row in cursor: yield row async def _submit_sql(self, query: str, *args, **kwargs) -> NoReturn: - async with (await self._connection).cursor() as cursor: - await cursor.execute(query, *args, **kwargs) - - @property - async def _connection(self): - if self.__connection is None: - self.__connection = await aiopg.connect( + if self._pool is None: + self._pool = await aiopg.create_pool( host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, ) - return self.__connection + with (await self._pool.cursor()) as cursor: + await cursor.execute(query, *args, **kwargs) _CREATE_ACTION_ENUM_QUERY = """ From 34f27f62e49a6f7dd0524c52fdb0b16b91841efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 13:12:18 +0200 Subject: [PATCH 28/47] ISSUE #150 * Minor change (15). --- minos/common/repository/pg.py | 1 + 1 file changed, 1 insertion(+) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 02b9a03e..0b171214 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -57,6 +57,7 @@ async def _create_events_table(self): async def __aexit__(self, exc_type, exc_value, exc_traceback): if self._pool is not None: self._pool.close() + await self._pool.wait_closed() async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: params = { From 79dfa9d7d20e6bbbef074a17b75eb57313b9bae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 13:24:10 +0200 Subject: [PATCH 29/47] ISSUE #150 * Minor change (16). --- minos/common/repository/pg.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 0b171214..95bbb1f7 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -11,6 +11,7 @@ ) import aiopg +from aiopg import Pool from .abc import ( MinosRepository, @@ -39,7 +40,7 @@ def __init__( self.database = database self.user = user self.password = password - self._pool = None + self.__pool = None async def _setup(self): """Setup miscellaneous repository thing. @@ -55,9 +56,9 @@ async def _create_events_table(self): await self._submit_sql(_CREATE_TABLE_QUERY) async def __aexit__(self, exc_type, exc_value, exc_traceback): - if self._pool is not None: - self._pool.close() - await self._pool.wait_closed() + if self.__pool is not None: + self.__pool.close() + await self.__pool.wait_closed() async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: params = { @@ -88,22 +89,22 @@ async def _submit_and_fetchone_sql(self, *args, **kwargs) -> tuple: raise IndexError("Given query did not returned any entry.") async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> AsyncIterator[tuple]: - if self._pool is None: - self._pool = await aiopg.create_pool( - host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, - ) - with (await self._pool.cursor()) as cursor: + with (await (await self._pool).cursor()) as cursor: await cursor.execute(query, *args, **kwargs) async for row in cursor: yield row async def _submit_sql(self, query: str, *args, **kwargs) -> NoReturn: - if self._pool is None: - self._pool = await aiopg.create_pool( + with (await (await self._pool).cursor()) as cursor: + await cursor.execute(query, *args, **kwargs) + + @property + async def _pool(self) -> Pool: + if self.__pool is None: + self.__pool = await aiopg.create_pool( host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, ) - with (await self._pool.cursor()) as cursor: - await cursor.execute(query, *args, **kwargs) + return self.__pool _CREATE_ACTION_ENUM_QUERY = """ From f41adfd278a6d69878ebdf95ce67cac633e96afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 13:24:58 +0200 Subject: [PATCH 30/47] ISSUE #150 * Minor change (17). --- minos/common/repository/pg.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 95bbb1f7..301dd1a2 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -84,9 +84,7 @@ async def _select( yield MinosRepositoryEntry(aggregate_id, aggregate_name, *row) async def _submit_and_fetchone_sql(self, *args, **kwargs) -> tuple: - async for entry in self._submit_and_iter_sql(*args, **kwargs): - return entry - raise IndexError("Given query did not returned any entry.") + return await self._submit_and_iter_sql(*args, **kwargs).__anext__() async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> AsyncIterator[tuple]: with (await (await self._pool).cursor()) as cursor: From aa885020c6860fdfffcdd36701a4775f5101afaf Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 30 Apr 2021 11:25:15 +0000 Subject: [PATCH 31/47] Restyled by black --- minos/common/model/aggregate.py | 20 +++++--------------- minos/common/repository/abc.py | 12 +++--------- minos/common/repository/memory.py | 16 ++++------------ minos/common/repository/pg.py | 8 ++------ tests/test_common/test_repository/test_pg.py | 12 +++--------- 5 files changed, 17 insertions(+), 51 deletions(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index 52b9288d..ddc9c7af 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -5,21 +5,15 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations -from operator import ( - attrgetter, -) +from operator import attrgetter from typing import ( NoReturn, Optional, ) -from ..configuration import ( - MinosConfig, -) +from ..configuration import MinosConfig from ..exceptions import ( MinosRepositoryAggregateNotFoundException, MinosRepositoryDeletedAggregateException, @@ -27,16 +21,12 @@ MinosRepositoryManuallySetAggregateVersionException, MinosRepositoryNonProvidedException, ) -from ..meta import ( - self_or_classmethod, -) +from ..meta import self_or_classmethod from ..repository import ( MinosRepository, MinosRepositoryAction, ) -from .abc import ( - MinosModel, -) +from .abc import MinosModel class Aggregate(MinosModel): diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index c7b12992..8b2c0961 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -6,9 +6,7 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations from abc import ( ABC, @@ -21,12 +19,8 @@ Union, ) -from ..configuration import ( - MinosConfig, -) -from ..setup import ( - MinosSetup, -) +from ..configuration import MinosConfig +from ..setup import MinosSetup from .entries import ( MinosRepositoryAction, MinosRepositoryEntry, diff --git a/minos/common/repository/memory.py b/minos/common/repository/memory.py index 63d3ac78..3ede9f5e 100644 --- a/minos/common/repository/memory.py +++ b/minos/common/repository/memory.py @@ -5,25 +5,17 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations -from itertools import ( - count, -) +from itertools import count from typing import ( AsyncIterator, NoReturn, Optional, ) -from .abc import ( - MinosRepository, -) -from .entries import ( - MinosRepositoryEntry, -) +from .abc import MinosRepository +from .entries import MinosRepositoryEntry class MinosInMemoryRepository(MinosRepository): diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 301dd1a2..8644a316 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -13,12 +13,8 @@ import aiopg from aiopg import Pool -from .abc import ( - MinosRepository, -) -from .entries import ( - MinosRepositoryEntry, -) +from .abc import MinosRepository +from .entries import MinosRepositoryEntry class PostgreSqlMinosRepository(MinosRepository): diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index e1790bfd..92c61522 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -15,15 +15,9 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import ( - PostgresAsyncTestCase, -) -from tests.aggregate_classes import ( - Car, -) -from tests.utils import ( - BASE_PATH, -) +from minos.common.testing import PostgresAsyncTestCase +from tests.aggregate_classes import Car +from tests.utils import BASE_PATH class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From df3100cf36f3369f766a4ae58454ef406f0e4ffc Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 30 Apr 2021 11:25:19 +0000 Subject: [PATCH 32/47] Restyled by isort --- minos/common/model/aggregate.py | 20 +++++++++++++++----- minos/common/repository/abc.py | 12 +++++++++--- minos/common/repository/memory.py | 16 ++++++++++++---- minos/common/repository/pg.py | 12 +++++++++--- tests/test_common/test_repository/test_pg.py | 12 +++++++++--- 5 files changed, 54 insertions(+), 18 deletions(-) diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index ddc9c7af..52b9288d 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -5,15 +5,21 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) -from operator import attrgetter +from operator import ( + attrgetter, +) from typing import ( NoReturn, Optional, ) -from ..configuration import MinosConfig +from ..configuration import ( + MinosConfig, +) from ..exceptions import ( MinosRepositoryAggregateNotFoundException, MinosRepositoryDeletedAggregateException, @@ -21,12 +27,16 @@ MinosRepositoryManuallySetAggregateVersionException, MinosRepositoryNonProvidedException, ) -from ..meta import self_or_classmethod +from ..meta import ( + self_or_classmethod, +) from ..repository import ( MinosRepository, MinosRepositoryAction, ) -from .abc import MinosModel +from .abc import ( + MinosModel, +) class Aggregate(MinosModel): diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index 8b2c0961..c7b12992 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -6,7 +6,9 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) from abc import ( ABC, @@ -19,8 +21,12 @@ Union, ) -from ..configuration import MinosConfig -from ..setup import MinosSetup +from ..configuration import ( + MinosConfig, +) +from ..setup import ( + MinosSetup, +) from .entries import ( MinosRepositoryAction, MinosRepositoryEntry, diff --git a/minos/common/repository/memory.py b/minos/common/repository/memory.py index 3ede9f5e..63d3ac78 100644 --- a/minos/common/repository/memory.py +++ b/minos/common/repository/memory.py @@ -5,17 +5,25 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) -from itertools import count +from itertools import ( + count, +) from typing import ( AsyncIterator, NoReturn, Optional, ) -from .abc import MinosRepository -from .entries import MinosRepositoryEntry +from .abc import ( + MinosRepository, +) +from .entries import ( + MinosRepositoryEntry, +) class MinosInMemoryRepository(MinosRepository): diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 8644a316..3de6986a 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -11,10 +11,16 @@ ) import aiopg -from aiopg import Pool +from aiopg import ( + Pool, +) -from .abc import MinosRepository -from .entries import MinosRepositoryEntry +from .abc import ( + MinosRepository, +) +from .entries import ( + MinosRepositoryEntry, +) class PostgreSqlMinosRepository(MinosRepository): diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 92c61522..e1790bfd 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -15,9 +15,15 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import PostgresAsyncTestCase -from tests.aggregate_classes import Car -from tests.utils import BASE_PATH +from minos.common.testing import ( + PostgresAsyncTestCase, +) +from tests.aggregate_classes import ( + Car, +) +from tests.utils import ( + BASE_PATH, +) class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From 3f0a773350d768da6ff91c4ce74e9be47c0ee56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 13:38:05 +0200 Subject: [PATCH 33/47] ISSUE #150 * Add `destroy()` method and move context manager logic to the `MinosSetup` class. --- minos/common/repository/abc.py | 7 ------- minos/common/repository/pg.py | 2 +- minos/common/setup.py | 30 +++++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index c7b12992..64a32aa8 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -55,13 +55,6 @@ def from_config(cls, *args, config: MinosConfig = None, **kwargs) -> Optional[Mi # noinspection PyProtectedMember return cls(*args, **config.repository._asdict(), **kwargs) - async def __aenter__(self) -> MinosRepository: - await self.setup() - return self - - async def __aexit__(self, exc_type, exc_value, exc_traceback): - pass - async def insert(self, entry: Union[Aggregate, MinosRepositoryEntry]) -> MinosRepositoryEntry: """Store new insertion entry into de repository. diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 301dd1a2..a476218a 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -55,7 +55,7 @@ async def _create_events_table(self): await self._submit_sql(_CREATE_ACTION_ENUM_QUERY) await self._submit_sql(_CREATE_TABLE_QUERY) - async def __aexit__(self, exc_type, exc_value, exc_traceback): + async def _destroy(self) -> NoReturn: if self.__pool is not None: self.__pool.close() await self.__pool.wait_closed() diff --git a/minos/common/setup.py b/minos/common/setup.py index 8edf80a5..263ed348 100644 --- a/minos/common/setup.py +++ b/minos/common/setup.py @@ -9,18 +9,27 @@ abstractmethod, ) from typing import ( + Generic, NoReturn, + TypeVar, ) +T = TypeVar("T") -class MinosSetup(object): + +class MinosSetup(Generic[T]): """Minos setup base class.""" def __init__(self, *args, already_setup: bool = False, **kwargs): self.already_setup = already_setup + self.already_destroyed = False + + async def __aenter__(self) -> T: + await self.setup() + return self async def setup(self) -> NoReturn: - """Setup miscellaneous repository thing. + """Setup miscellaneous repository things. :return: This method does not return anything. """ @@ -30,5 +39,20 @@ async def setup(self) -> NoReturn: @abstractmethod async def _setup(self) -> NoReturn: - """Setup miscellaneous repository thing.""" + """Setup miscellaneous repository things.""" raise NotImplementedError + + async def __aexit__(self, exc_type, exc_value, exc_traceback): + await self.destroy() + + async def destroy(self) -> NoReturn: + """Destroy miscellaneous repository things. + + :return: This method does not return anything. + """ + if not self.already_destroyed: + await self._destroy() + self.already_destroyed = True + + async def _destroy(self) -> NoReturn: + """Destroy miscellaneous repository things.""" From 09bbcfdacf605fd86c8469ac7047ca3e1996de0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 14:12:06 +0200 Subject: [PATCH 34/47] ISSUE #150 * Expose `PostgreSqlMinosDataBase` and minor improvements. --- minos/common/__init__.py | 3 ++ minos/common/database.py | 97 +++++++++++++++++++++++++++++++++++ minos/common/repository/pg.py | 63 +++-------------------- 3 files changed, 108 insertions(+), 55 deletions(-) create mode 100644 minos/common/database.py diff --git a/minos/common/__init__.py b/minos/common/__init__.py index 964ad316..f68dd851 100644 --- a/minos/common/__init__.py +++ b/minos/common/__init__.py @@ -114,3 +114,6 @@ MinosStorage, MinosStorageLmdb, ) +from .database import ( + PostgreSqlMinosDataBase, +) diff --git a/minos/common/database.py b/minos/common/database.py new file mode 100644 index 00000000..eee8ff7a --- /dev/null +++ b/minos/common/database.py @@ -0,0 +1,97 @@ +""" +Copyright (C) 2021 Clariteia SL + +This file is part of minos framework. + +Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. +""" +from abc import ( + ABC, +) +from typing import ( + AsyncIterator, + NoReturn, +) + +import aiopg +from aiopg import ( + Pool, +) + +from .setup import ( + MinosSetup, +) + + +class PostgreSqlMinosDataBase(ABC, MinosSetup): + """PostgreSql Minos Database base class.""" + + def __init__( + self, + host: str = None, + port: int = None, + database: str = None, + user: str = None, + password: str = None, + *args, + **kwargs, + ): + super().__init__(*args, **kwargs) + self.host = host + self.port = port + self.database = database + self.user = user + self.password = password + self._pool = None + + async def _destroy(self) -> NoReturn: + if self._pool is not None: + self._pool.close() + await self._pool.wait_closed() + + async def submit_query_and_fetchone(self, *args, **kwargs) -> tuple: + """Submit a SQL query and gets the first response. + + :param args: Additional positional arguments. + :param kwargs: Additional named arguments. + :return: This method does not return anything. + """ + return await self.submit_query_and_iter(*args, **kwargs).__anext__() + + async def submit_query_and_iter(self, *args, **kwargs) -> AsyncIterator[tuple]: + """Submit a SQL query and return an asynchronous iterator. + + :param args: Additional positional arguments. + :param kwargs: Additional named arguments. + :return: This method does not return anything. + """ + with (await (await self.pool).cursor()) as cursor: + await cursor.execute(*args, **kwargs) + async for row in cursor: + yield row + + async def submit_query(self, *args, **kwargs) -> NoReturn: + """Submit a SQL query. + + :param args: Additional positional arguments. + :param kwargs: Additional named arguments. + :return: This method does not return anything. + """ + with (await (await self.pool).cursor()) as cursor: + await cursor.execute(*args, **kwargs) + + @property + async def pool(self) -> Pool: + """Get the connections pool. + + :return: A ``Pool`` object. + """ + if self._pool is None: + self._pool = await aiopg.create_pool( + host=self.host, + port=self.port, + dbname=self.database, + user=self.user, + password=self.password, + ) + return self._pool diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 4113c41f..26f9cb8c 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -7,14 +7,11 @@ """ from typing import ( AsyncIterator, - NoReturn, ) -import aiopg -from aiopg import ( - Pool, +from ..database import ( + PostgreSqlMinosDataBase, ) - from .abc import ( MinosRepository, ) @@ -23,27 +20,9 @@ ) -class PostgreSqlMinosRepository(MinosRepository): +class PostgreSqlMinosRepository(MinosRepository, PostgreSqlMinosDataBase): """PostgreSQL-based implementation of the repository class in ``minos``.""" - def __init__( - self, - host: str = None, - port: int = None, - database: str = None, - user: str = None, - password: str = None, - *args, - **kwargs, - ): - super().__init__(*args, **kwargs) - self.host = host - self.port = port - self.database = database - self.user = user - self.password = password - self.__pool = None - async def _setup(self): """Setup miscellaneous repository thing. @@ -54,13 +33,8 @@ async def _setup(self): await self._create_events_table() async def _create_events_table(self): - await self._submit_sql(_CREATE_ACTION_ENUM_QUERY) - await self._submit_sql(_CREATE_TABLE_QUERY) - - async def _destroy(self) -> NoReturn: - if self.__pool is not None: - self.__pool.close() - await self.__pool.wait_closed() + await self.submit_query(_CREATE_ACTION_ENUM_QUERY) + await self.submit_query(_CREATE_TABLE_QUERY) async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: params = { @@ -69,7 +43,7 @@ async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: "aggregate_name": entry.aggregate_name, "data": entry.data, } - response = await self._submit_and_fetchone_sql(_INSERT_VALUES_QUERY, params) + response = await self.submit_query_and_fetchone(_INSERT_VALUES_QUERY, params) entry.id = response[0] entry.aggregate_id = response[1] entry.version = response[2] @@ -79,33 +53,12 @@ async def _select( self, aggregate_id: int = None, aggregate_name: str = None, *args, **kwargs, ) -> AsyncIterator[MinosRepositoryEntry]: if aggregate_id is None and aggregate_name is None: - async for row in self._submit_and_iter_sql(_SELECT_ALL_ENTRIES_QUERY): + async for row in self.submit_query_and_iter(_SELECT_ALL_ENTRIES_QUERY): yield MinosRepositoryEntry(*row) else: - async for row in self._submit_and_iter_sql(_SELECT_ENTRIES_QUERY, (aggregate_id, aggregate_name)): + async for row in self.submit_query_and_iter(_SELECT_ENTRIES_QUERY, (aggregate_id, aggregate_name)): yield MinosRepositoryEntry(aggregate_id, aggregate_name, *row) - async def _submit_and_fetchone_sql(self, *args, **kwargs) -> tuple: - return await self._submit_and_iter_sql(*args, **kwargs).__anext__() - - async def _submit_and_iter_sql(self, query: str, *args, **kwargs) -> AsyncIterator[tuple]: - with (await (await self._pool).cursor()) as cursor: - await cursor.execute(query, *args, **kwargs) - async for row in cursor: - yield row - - async def _submit_sql(self, query: str, *args, **kwargs) -> NoReturn: - with (await (await self._pool).cursor()) as cursor: - await cursor.execute(query, *args, **kwargs) - - @property - async def _pool(self) -> Pool: - if self.__pool is None: - self.__pool = await aiopg.create_pool( - host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, - ) - return self.__pool - _CREATE_ACTION_ENUM_QUERY = """ DO From 20c3ef414a411d46920aac46ab8368ed4494cb2e Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 30 Apr 2021 12:12:18 +0000 Subject: [PATCH 35/47] Restyled by black --- minos/common/__init__.py | 8 ++------ minos/common/database.py | 18 ++++-------------- minos/common/model/aggregate.py | 20 +++++--------------- minos/common/repository/abc.py | 12 +++--------- minos/common/repository/memory.py | 16 ++++------------ minos/common/repository/pg.py | 18 +++++------------- minos/common/setup.py | 4 +--- tests/test_common/test_repository/test_pg.py | 12 +++--------- 8 files changed, 27 insertions(+), 81 deletions(-) diff --git a/minos/common/__init__.py b/minos/common/__init__.py index f68dd851..d2ebab18 100644 --- a/minos/common/__init__.py +++ b/minos/common/__init__.py @@ -107,13 +107,9 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from .setup import ( - MinosSetup, -) +from .setup import MinosSetup from .storage import ( MinosStorage, MinosStorageLmdb, ) -from .database import ( - PostgreSqlMinosDataBase, -) +from .database import PostgreSqlMinosDataBase diff --git a/minos/common/database.py b/minos/common/database.py index eee8ff7a..eb68fa7d 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -5,22 +5,16 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import ( - ABC, -) +from abc import ABC from typing import ( AsyncIterator, NoReturn, ) import aiopg -from aiopg import ( - Pool, -) +from aiopg import Pool -from .setup import ( - MinosSetup, -) +from .setup import MinosSetup class PostgreSqlMinosDataBase(ABC, MinosSetup): @@ -88,10 +82,6 @@ async def pool(self) -> Pool: """ if self._pool is None: self._pool = await aiopg.create_pool( - host=self.host, - port=self.port, - dbname=self.database, - user=self.user, - password=self.password, + host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password, ) return self._pool diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index 52b9288d..ddc9c7af 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -5,21 +5,15 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations -from operator import ( - attrgetter, -) +from operator import attrgetter from typing import ( NoReturn, Optional, ) -from ..configuration import ( - MinosConfig, -) +from ..configuration import MinosConfig from ..exceptions import ( MinosRepositoryAggregateNotFoundException, MinosRepositoryDeletedAggregateException, @@ -27,16 +21,12 @@ MinosRepositoryManuallySetAggregateVersionException, MinosRepositoryNonProvidedException, ) -from ..meta import ( - self_or_classmethod, -) +from ..meta import self_or_classmethod from ..repository import ( MinosRepository, MinosRepositoryAction, ) -from .abc import ( - MinosModel, -) +from .abc import MinosModel class Aggregate(MinosModel): diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index 64a32aa8..f3d2754b 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -6,9 +6,7 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations from abc import ( ABC, @@ -21,12 +19,8 @@ Union, ) -from ..configuration import ( - MinosConfig, -) -from ..setup import ( - MinosSetup, -) +from ..configuration import MinosConfig +from ..setup import MinosSetup from .entries import ( MinosRepositoryAction, MinosRepositoryEntry, diff --git a/minos/common/repository/memory.py b/minos/common/repository/memory.py index 63d3ac78..3ede9f5e 100644 --- a/minos/common/repository/memory.py +++ b/minos/common/repository/memory.py @@ -5,25 +5,17 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations -from itertools import ( - count, -) +from itertools import count from typing import ( AsyncIterator, NoReturn, Optional, ) -from .abc import ( - MinosRepository, -) -from .entries import ( - MinosRepositoryEntry, -) +from .abc import MinosRepository +from .entries import MinosRepositoryEntry class MinosInMemoryRepository(MinosRepository): diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 26f9cb8c..35915eac 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -5,19 +5,11 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from typing import ( - AsyncIterator, -) - -from ..database import ( - PostgreSqlMinosDataBase, -) -from .abc import ( - MinosRepository, -) -from .entries import ( - MinosRepositoryEntry, -) +from typing import AsyncIterator + +from ..database import PostgreSqlMinosDataBase +from .abc import MinosRepository +from .entries import MinosRepositoryEntry class PostgreSqlMinosRepository(MinosRepository, PostgreSqlMinosDataBase): diff --git a/minos/common/setup.py b/minos/common/setup.py index 263ed348..3421ca66 100644 --- a/minos/common/setup.py +++ b/minos/common/setup.py @@ -5,9 +5,7 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import ( - abstractmethod, -) +from abc import abstractmethod from typing import ( Generic, NoReturn, diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index e1790bfd..92c61522 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -15,15 +15,9 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import ( - PostgresAsyncTestCase, -) -from tests.aggregate_classes import ( - Car, -) -from tests.utils import ( - BASE_PATH, -) +from minos.common.testing import PostgresAsyncTestCase +from tests.aggregate_classes import Car +from tests.utils import BASE_PATH class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From e76409b6bb29726c85bd1e392cc89e659333ff32 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 30 Apr 2021 12:12:20 +0000 Subject: [PATCH 36/47] Restyled by isort --- minos/common/__init__.py | 8 ++++++-- minos/common/database.py | 12 +++++++++--- minos/common/model/aggregate.py | 20 +++++++++++++++----- minos/common/repository/abc.py | 12 +++++++++--- minos/common/repository/memory.py | 16 ++++++++++++---- minos/common/repository/pg.py | 18 +++++++++++++----- minos/common/setup.py | 4 +++- tests/test_common/test_repository/test_pg.py | 12 +++++++++--- 8 files changed, 76 insertions(+), 26 deletions(-) diff --git a/minos/common/__init__.py b/minos/common/__init__.py index d2ebab18..047c39e0 100644 --- a/minos/common/__init__.py +++ b/minos/common/__init__.py @@ -25,6 +25,9 @@ MinosConfig, MinosConfigAbstract, ) +from .database import ( + PostgreSqlMinosDataBase, +) from .exceptions import ( EmptyMinosModelSequenceException, MinosAttributeValidationException, @@ -107,9 +110,10 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from .setup import MinosSetup +from .setup import ( + MinosSetup, +) from .storage import ( MinosStorage, MinosStorageLmdb, ) -from .database import PostgreSqlMinosDataBase diff --git a/minos/common/database.py b/minos/common/database.py index eb68fa7d..1f4f4ad3 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -5,16 +5,22 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import ABC +from abc import ( + ABC, +) from typing import ( AsyncIterator, NoReturn, ) import aiopg -from aiopg import Pool +from aiopg import ( + Pool, +) -from .setup import MinosSetup +from .setup import ( + MinosSetup, +) class PostgreSqlMinosDataBase(ABC, MinosSetup): diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index ddc9c7af..52b9288d 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -5,15 +5,21 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) -from operator import attrgetter +from operator import ( + attrgetter, +) from typing import ( NoReturn, Optional, ) -from ..configuration import MinosConfig +from ..configuration import ( + MinosConfig, +) from ..exceptions import ( MinosRepositoryAggregateNotFoundException, MinosRepositoryDeletedAggregateException, @@ -21,12 +27,16 @@ MinosRepositoryManuallySetAggregateVersionException, MinosRepositoryNonProvidedException, ) -from ..meta import self_or_classmethod +from ..meta import ( + self_or_classmethod, +) from ..repository import ( MinosRepository, MinosRepositoryAction, ) -from .abc import MinosModel +from .abc import ( + MinosModel, +) class Aggregate(MinosModel): diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index f3d2754b..64a32aa8 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -6,7 +6,9 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) from abc import ( ABC, @@ -19,8 +21,12 @@ Union, ) -from ..configuration import MinosConfig -from ..setup import MinosSetup +from ..configuration import ( + MinosConfig, +) +from ..setup import ( + MinosSetup, +) from .entries import ( MinosRepositoryAction, MinosRepositoryEntry, diff --git a/minos/common/repository/memory.py b/minos/common/repository/memory.py index 3ede9f5e..63d3ac78 100644 --- a/minos/common/repository/memory.py +++ b/minos/common/repository/memory.py @@ -5,17 +5,25 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) -from itertools import count +from itertools import ( + count, +) from typing import ( AsyncIterator, NoReturn, Optional, ) -from .abc import MinosRepository -from .entries import MinosRepositoryEntry +from .abc import ( + MinosRepository, +) +from .entries import ( + MinosRepositoryEntry, +) class MinosInMemoryRepository(MinosRepository): diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 35915eac..26f9cb8c 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -5,11 +5,19 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from typing import AsyncIterator - -from ..database import PostgreSqlMinosDataBase -from .abc import MinosRepository -from .entries import MinosRepositoryEntry +from typing import ( + AsyncIterator, +) + +from ..database import ( + PostgreSqlMinosDataBase, +) +from .abc import ( + MinosRepository, +) +from .entries import ( + MinosRepositoryEntry, +) class PostgreSqlMinosRepository(MinosRepository, PostgreSqlMinosDataBase): diff --git a/minos/common/setup.py b/minos/common/setup.py index 3421ca66..263ed348 100644 --- a/minos/common/setup.py +++ b/minos/common/setup.py @@ -5,7 +5,9 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import abstractmethod +from abc import ( + abstractmethod, +) from typing import ( Generic, NoReturn, diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 92c61522..e1790bfd 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -15,9 +15,15 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import PostgresAsyncTestCase -from tests.aggregate_classes import Car -from tests.utils import BASE_PATH +from minos.common.testing import ( + PostgresAsyncTestCase, +) +from tests.aggregate_classes import ( + Car, +) +from tests.utils import ( + BASE_PATH, +) class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From f4f920aa9be43e9df0ce902905476ef1282e2d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 14:14:19 +0200 Subject: [PATCH 37/47] ISSUE #150 * Minor change. --- minos/common/__init__.py | 2 +- minos/common/database.py | 2 +- minos/common/repository/pg.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/minos/common/__init__.py b/minos/common/__init__.py index 047c39e0..c139e117 100644 --- a/minos/common/__init__.py +++ b/minos/common/__init__.py @@ -26,7 +26,7 @@ MinosConfigAbstract, ) from .database import ( - PostgreSqlMinosDataBase, + PostgreSqlMinosDatabase, ) from .exceptions import ( EmptyMinosModelSequenceException, diff --git a/minos/common/database.py b/minos/common/database.py index 1f4f4ad3..e11d9115 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -23,7 +23,7 @@ ) -class PostgreSqlMinosDataBase(ABC, MinosSetup): +class PostgreSqlMinosDatabase(ABC, MinosSetup): """PostgreSql Minos Database base class.""" def __init__( diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 26f9cb8c..88b2a682 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -10,7 +10,7 @@ ) from ..database import ( - PostgreSqlMinosDataBase, + PostgreSqlMinosDatabase, ) from .abc import ( MinosRepository, @@ -20,7 +20,7 @@ ) -class PostgreSqlMinosRepository(MinosRepository, PostgreSqlMinosDataBase): +class PostgreSqlMinosRepository(MinosRepository, PostgreSqlMinosDatabase): """PostgreSQL-based implementation of the repository class in ``minos``.""" async def _setup(self): From 57e83487aec4cc2b3d140e1c709690addb3ad4ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 15:02:55 +0200 Subject: [PATCH 38/47] ISSUE #150 * Simplify code. --- minos/common/database.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/minos/common/database.py b/minos/common/database.py index e11d9115..91eb2db0 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -65,7 +65,8 @@ async def submit_query_and_iter(self, *args, **kwargs) -> AsyncIterator[tuple]: :param kwargs: Additional named arguments. :return: This method does not return anything. """ - with (await (await self.pool).cursor()) as cursor: + pool = await self.pool + with await pool.cursor() as cursor: await cursor.execute(*args, **kwargs) async for row in cursor: yield row @@ -77,7 +78,8 @@ async def submit_query(self, *args, **kwargs) -> NoReturn: :param kwargs: Additional named arguments. :return: This method does not return anything. """ - with (await (await self.pool).cursor()) as cursor: + pool = await self.pool + with await pool.cursor() as cursor: await cursor.execute(*args, **kwargs) @property From 9f60258d9314b6cad80ab7a94e52bf4c250614a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Fri, 30 Apr 2021 16:50:50 +0200 Subject: [PATCH 39/47] ISSUE #150 * Add tests for database. --- minos/common/database.py | 11 +---- tests/test_common/test_database.py | 78 ++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 tests/test_common/test_database.py diff --git a/minos/common/database.py b/minos/common/database.py index 91eb2db0..f3082432 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -26,16 +26,7 @@ class PostgreSqlMinosDatabase(ABC, MinosSetup): """PostgreSql Minos Database base class.""" - def __init__( - self, - host: str = None, - port: int = None, - database: str = None, - user: str = None, - password: str = None, - *args, - **kwargs, - ): + def __init__(self, host: str, port: int, database: str, user: str, password: str, *args, **kwargs): super().__init__(*args, **kwargs) self.host = host self.port = port diff --git a/tests/test_common/test_database.py b/tests/test_common/test_database.py new file mode 100644 index 00000000..7fa28fe6 --- /dev/null +++ b/tests/test_common/test_database.py @@ -0,0 +1,78 @@ +""" +Copyright (C) 2021 Clariteia SL + +This file is part of minos framework. + +Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. +""" +import unittest +from typing import ( + NoReturn, +) + +import aiopg +from aiopg import ( + Pool, +) + +from minos.common import ( + PostgreSqlMinosDatabase, +) +from minos.common.testing import ( + PostgresAsyncTestCase, +) +from tests.utils import ( + BASE_PATH, +) + + +class _PostgreSqlMinosDatabase(PostgreSqlMinosDatabase): + async def _setup(self) -> NoReturn: + pass + + +class TestPostgreSqlMinosDatabase(PostgresAsyncTestCase): + CONFIG_FILE_PATH = BASE_PATH / "test_config.yml" + + def test_constructor(self): + database = _PostgreSqlMinosDatabase(**self.repository_db) + self.assertEqual(self.repository_db["host"], database.host) + self.assertEqual(self.repository_db["port"], database.port) + self.assertEqual(self.repository_db["database"], database.database) + self.assertEqual(self.repository_db["user"], database.user) + self.assertEqual(self.repository_db["password"], database.password) + + async def test_pool(self): + async with _PostgreSqlMinosDatabase(**self.repository_db) as database: + self.assertIsInstance(await database.pool, Pool) + + async def test_submit_query(self): + async with _PostgreSqlMinosDatabase(**self.repository_db) as database: + await database.submit_query("CREATE TABLE foo (id INT NOT NULL);") + + async with aiopg.connect(**self.repository_db) as connection: + async with connection.cursor() as cursor: + await cursor.execute("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'foo');") + self.assertTrue((await cursor.fetchone())[0]) + + async def test_submit_query_and_fetchone(self): + async with _PostgreSqlMinosDatabase(**self.repository_db) as database: + await database.submit_query("CREATE TABLE foo (id INT NOT NULL);") + await database.submit_query("INSERT INTO foo (id) VALUES (3), (4), (5);") + + observed = await database.submit_query_and_fetchone("SELECT * FROM foo;") + + self.assertEqual((3,), observed) + + async def test_submit_query_and_iter(self): + async with _PostgreSqlMinosDatabase(**self.repository_db) as database: + await database.submit_query("CREATE TABLE foo (id INT NOT NULL);") + await database.submit_query("INSERT INTO foo (id) VALUES (3), (4), (5);") + + observed = [v async for v in database.submit_query_and_iter("SELECT * FROM foo;")] + + self.assertEqual([(3,), (4,), (5,)], observed) + + +if __name__ == "__main__": + unittest.main() From da52fe6c729b6a15a20e6d4db8746972d96c75cd Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 30 Apr 2021 14:51:27 +0000 Subject: [PATCH 40/47] Restyled by black --- minos/common/__init__.py | 8 ++----- minos/common/database.py | 12 +++-------- minos/common/model/aggregate.py | 20 +++++------------- minos/common/repository/abc.py | 12 +++-------- minos/common/repository/memory.py | 16 ++++---------- minos/common/repository/pg.py | 18 +++++----------- minos/common/setup.py | 4 +--- tests/test_common/test_database.py | 22 ++++++-------------- tests/test_common/test_repository/test_pg.py | 12 +++-------- 9 files changed, 32 insertions(+), 92 deletions(-) diff --git a/minos/common/__init__.py b/minos/common/__init__.py index c139e117..2778ba03 100644 --- a/minos/common/__init__.py +++ b/minos/common/__init__.py @@ -25,9 +25,7 @@ MinosConfig, MinosConfigAbstract, ) -from .database import ( - PostgreSqlMinosDatabase, -) +from .database import PostgreSqlMinosDatabase from .exceptions import ( EmptyMinosModelSequenceException, MinosAttributeValidationException, @@ -110,9 +108,7 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from .setup import ( - MinosSetup, -) +from .setup import MinosSetup from .storage import ( MinosStorage, MinosStorageLmdb, diff --git a/minos/common/database.py b/minos/common/database.py index f3082432..078cb502 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -5,22 +5,16 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import ( - ABC, -) +from abc import ABC from typing import ( AsyncIterator, NoReturn, ) import aiopg -from aiopg import ( - Pool, -) +from aiopg import Pool -from .setup import ( - MinosSetup, -) +from .setup import MinosSetup class PostgreSqlMinosDatabase(ABC, MinosSetup): diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index 52b9288d..ddc9c7af 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -5,21 +5,15 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations -from operator import ( - attrgetter, -) +from operator import attrgetter from typing import ( NoReturn, Optional, ) -from ..configuration import ( - MinosConfig, -) +from ..configuration import MinosConfig from ..exceptions import ( MinosRepositoryAggregateNotFoundException, MinosRepositoryDeletedAggregateException, @@ -27,16 +21,12 @@ MinosRepositoryManuallySetAggregateVersionException, MinosRepositoryNonProvidedException, ) -from ..meta import ( - self_or_classmethod, -) +from ..meta import self_or_classmethod from ..repository import ( MinosRepository, MinosRepositoryAction, ) -from .abc import ( - MinosModel, -) +from .abc import MinosModel class Aggregate(MinosModel): diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index 64a32aa8..f3d2754b 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -6,9 +6,7 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations from abc import ( ABC, @@ -21,12 +19,8 @@ Union, ) -from ..configuration import ( - MinosConfig, -) -from ..setup import ( - MinosSetup, -) +from ..configuration import MinosConfig +from ..setup import MinosSetup from .entries import ( MinosRepositoryAction, MinosRepositoryEntry, diff --git a/minos/common/repository/memory.py b/minos/common/repository/memory.py index 63d3ac78..3ede9f5e 100644 --- a/minos/common/repository/memory.py +++ b/minos/common/repository/memory.py @@ -5,25 +5,17 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations -from itertools import ( - count, -) +from itertools import count from typing import ( AsyncIterator, NoReturn, Optional, ) -from .abc import ( - MinosRepository, -) -from .entries import ( - MinosRepositoryEntry, -) +from .abc import MinosRepository +from .entries import MinosRepositoryEntry class MinosInMemoryRepository(MinosRepository): diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 88b2a682..1fdbd723 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -5,19 +5,11 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from typing import ( - AsyncIterator, -) - -from ..database import ( - PostgreSqlMinosDatabase, -) -from .abc import ( - MinosRepository, -) -from .entries import ( - MinosRepositoryEntry, -) +from typing import AsyncIterator + +from ..database import PostgreSqlMinosDatabase +from .abc import MinosRepository +from .entries import MinosRepositoryEntry class PostgreSqlMinosRepository(MinosRepository, PostgreSqlMinosDatabase): diff --git a/minos/common/setup.py b/minos/common/setup.py index 263ed348..3421ca66 100644 --- a/minos/common/setup.py +++ b/minos/common/setup.py @@ -5,9 +5,7 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import ( - abstractmethod, -) +from abc import abstractmethod from typing import ( Generic, NoReturn, diff --git a/tests/test_common/test_database.py b/tests/test_common/test_database.py index 7fa28fe6..3fb6b735 100644 --- a/tests/test_common/test_database.py +++ b/tests/test_common/test_database.py @@ -6,24 +6,14 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ import unittest -from typing import ( - NoReturn, -) +from typing import NoReturn import aiopg -from aiopg import ( - Pool, -) - -from minos.common import ( - PostgreSqlMinosDatabase, -) -from minos.common.testing import ( - PostgresAsyncTestCase, -) -from tests.utils import ( - BASE_PATH, -) +from aiopg import Pool + +from minos.common import PostgreSqlMinosDatabase +from minos.common.testing import PostgresAsyncTestCase +from tests.utils import BASE_PATH class _PostgreSqlMinosDatabase(PostgreSqlMinosDatabase): diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index e1790bfd..92c61522 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -15,15 +15,9 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import ( - PostgresAsyncTestCase, -) -from tests.aggregate_classes import ( - Car, -) -from tests.utils import ( - BASE_PATH, -) +from minos.common.testing import PostgresAsyncTestCase +from tests.aggregate_classes import Car +from tests.utils import BASE_PATH class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From d1cc8e696e4b1fb92b105497a6c7a17f8a314005 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 30 Apr 2021 14:51:39 +0000 Subject: [PATCH 41/47] Restyled by isort --- minos/common/__init__.py | 8 ++++++-- minos/common/database.py | 12 ++++++++--- minos/common/model/aggregate.py | 20 ++++++++++++++----- minos/common/repository/abc.py | 12 ++++++++--- minos/common/repository/memory.py | 16 +++++++++++---- minos/common/repository/pg.py | 18 ++++++++++++----- minos/common/setup.py | 4 +++- tests/test_common/test_database.py | 21 ++++++++++++++------ tests/test_common/test_repository/test_pg.py | 12 ++++++++--- 9 files changed, 91 insertions(+), 32 deletions(-) diff --git a/minos/common/__init__.py b/minos/common/__init__.py index 2778ba03..c139e117 100644 --- a/minos/common/__init__.py +++ b/minos/common/__init__.py @@ -25,7 +25,9 @@ MinosConfig, MinosConfigAbstract, ) -from .database import PostgreSqlMinosDatabase +from .database import ( + PostgreSqlMinosDatabase, +) from .exceptions import ( EmptyMinosModelSequenceException, MinosAttributeValidationException, @@ -108,7 +110,9 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from .setup import MinosSetup +from .setup import ( + MinosSetup, +) from .storage import ( MinosStorage, MinosStorageLmdb, diff --git a/minos/common/database.py b/minos/common/database.py index 078cb502..f3082432 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -5,16 +5,22 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import ABC +from abc import ( + ABC, +) from typing import ( AsyncIterator, NoReturn, ) import aiopg -from aiopg import Pool +from aiopg import ( + Pool, +) -from .setup import MinosSetup +from .setup import ( + MinosSetup, +) class PostgreSqlMinosDatabase(ABC, MinosSetup): diff --git a/minos/common/model/aggregate.py b/minos/common/model/aggregate.py index ddc9c7af..52b9288d 100644 --- a/minos/common/model/aggregate.py +++ b/minos/common/model/aggregate.py @@ -5,15 +5,21 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) -from operator import attrgetter +from operator import ( + attrgetter, +) from typing import ( NoReturn, Optional, ) -from ..configuration import MinosConfig +from ..configuration import ( + MinosConfig, +) from ..exceptions import ( MinosRepositoryAggregateNotFoundException, MinosRepositoryDeletedAggregateException, @@ -21,12 +27,16 @@ MinosRepositoryManuallySetAggregateVersionException, MinosRepositoryNonProvidedException, ) -from ..meta import self_or_classmethod +from ..meta import ( + self_or_classmethod, +) from ..repository import ( MinosRepository, MinosRepositoryAction, ) -from .abc import MinosModel +from .abc import ( + MinosModel, +) class Aggregate(MinosModel): diff --git a/minos/common/repository/abc.py b/minos/common/repository/abc.py index f3d2754b..64a32aa8 100644 --- a/minos/common/repository/abc.py +++ b/minos/common/repository/abc.py @@ -6,7 +6,9 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) from abc import ( ABC, @@ -19,8 +21,12 @@ Union, ) -from ..configuration import MinosConfig -from ..setup import MinosSetup +from ..configuration import ( + MinosConfig, +) +from ..setup import ( + MinosSetup, +) from .entries import ( MinosRepositoryAction, MinosRepositoryEntry, diff --git a/minos/common/repository/memory.py b/minos/common/repository/memory.py index 3ede9f5e..63d3ac78 100644 --- a/minos/common/repository/memory.py +++ b/minos/common/repository/memory.py @@ -5,17 +5,25 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) -from itertools import count +from itertools import ( + count, +) from typing import ( AsyncIterator, NoReturn, Optional, ) -from .abc import MinosRepository -from .entries import MinosRepositoryEntry +from .abc import ( + MinosRepository, +) +from .entries import ( + MinosRepositoryEntry, +) class MinosInMemoryRepository(MinosRepository): diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 1fdbd723..88b2a682 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -5,11 +5,19 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from typing import AsyncIterator - -from ..database import PostgreSqlMinosDatabase -from .abc import MinosRepository -from .entries import MinosRepositoryEntry +from typing import ( + AsyncIterator, +) + +from ..database import ( + PostgreSqlMinosDatabase, +) +from .abc import ( + MinosRepository, +) +from .entries import ( + MinosRepositoryEntry, +) class PostgreSqlMinosRepository(MinosRepository, PostgreSqlMinosDatabase): diff --git a/minos/common/setup.py b/minos/common/setup.py index 3421ca66..263ed348 100644 --- a/minos/common/setup.py +++ b/minos/common/setup.py @@ -5,7 +5,9 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import abstractmethod +from abc import ( + abstractmethod, +) from typing import ( Generic, NoReturn, diff --git a/tests/test_common/test_database.py b/tests/test_common/test_database.py index 3fb6b735..254bcaf7 100644 --- a/tests/test_common/test_database.py +++ b/tests/test_common/test_database.py @@ -6,14 +6,23 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ import unittest -from typing import NoReturn +from typing import ( + NoReturn, +) import aiopg -from aiopg import Pool - -from minos.common import PostgreSqlMinosDatabase -from minos.common.testing import PostgresAsyncTestCase -from tests.utils import BASE_PATH +from aiopg import ( + Pool, +) +from minos.common import ( + PostgreSqlMinosDatabase, +) +from minos.common.testing import ( + PostgresAsyncTestCase, +) +from tests.utils import ( + BASE_PATH, +) class _PostgreSqlMinosDatabase(PostgreSqlMinosDatabase): diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 92c61522..e1790bfd 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -15,9 +15,15 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import PostgresAsyncTestCase -from tests.aggregate_classes import Car -from tests.utils import BASE_PATH +from minos.common.testing import ( + PostgresAsyncTestCase, +) +from tests.aggregate_classes import ( + Car, +) +from tests.utils import ( + BASE_PATH, +) class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From bacc018bebc200734ede3284b3452ff1311872f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Mon, 3 May 2021 09:28:43 +0200 Subject: [PATCH 42/47] ISSUE #148 * Improve `minos.common.MinosSetup` behaviour with a better handling of `already_*` flags. --- minos/common/setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/minos/common/setup.py b/minos/common/setup.py index 263ed348..c928e156 100644 --- a/minos/common/setup.py +++ b/minos/common/setup.py @@ -36,6 +36,7 @@ async def setup(self) -> NoReturn: if not self.already_setup: await self._setup() self.already_setup = True + self.already_destroyed = False @abstractmethod async def _setup(self) -> NoReturn: @@ -53,6 +54,7 @@ async def destroy(self) -> NoReturn: if not self.already_destroyed: await self._destroy() self.already_destroyed = True + self.already_setup = False async def _destroy(self) -> NoReturn: """Destroy miscellaneous repository things.""" From f7293f4ada90a8f341c64533aeb8d71343bc79a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Mon, 3 May 2021 09:29:39 +0200 Subject: [PATCH 43/47] ISSUE #148 * Improve `minos.common.PostgreSqlMinosDatabase` behaviour allowing destroying the pool properly. --- minos/common/database.py | 1 + 1 file changed, 1 insertion(+) diff --git a/minos/common/database.py b/minos/common/database.py index f3082432..4f567a79 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -39,6 +39,7 @@ async def _destroy(self) -> NoReturn: if self._pool is not None: self._pool.close() await self._pool.wait_closed() + self._pool = None async def submit_query_and_fetchone(self, *args, **kwargs) -> tuple: """Submit a SQL query and gets the first response. From cece7dab0cdfb1fbf88f3e2724f6127adb339a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Mon, 3 May 2021 09:32:13 +0200 Subject: [PATCH 44/47] ISSUE #148 * Add `created_at` column to `MinosRepositoryEntry`. * Rename `events` table as `aggregate_event`. --- minos/common/repository/entries.py | 34 ++++-- minos/common/repository/pg.py | 58 +++++----- .../test_repository/test_entries.py | 54 ++++++++- .../test_repository/test_memory.py | 4 +- tests/test_common/test_repository/test_pg.py | 103 +++++++++++------- 5 files changed, 176 insertions(+), 77 deletions(-) diff --git a/minos/common/repository/entries.py b/minos/common/repository/entries.py index c4b3c5f3..5922e895 100644 --- a/minos/common/repository/entries.py +++ b/minos/common/repository/entries.py @@ -9,6 +9,9 @@ annotations, ) +from datetime import ( + datetime, +) from enum import ( Enum, ) @@ -24,7 +27,9 @@ ) if TYPE_CHECKING: - from ..model import Aggregate + from ..model import ( + Aggregate, + ) class MinosRepositoryAction(Enum): @@ -48,7 +53,7 @@ def value_of(cls, value: str) -> Optional[MinosRepositoryAction]: class MinosRepositoryEntry(object): """Class that represents an entry (or row) on the events repository database which stores the aggregate changes.""" - __slots__ = "id", "action", "aggregate_id", "aggregate_name", "version", "data" + __slots__ = "aggregate_id", "aggregate_name", "version", "data", "id", "action", "created_at" # noinspection PyShadowingBuiltins def __init__( @@ -59,20 +64,22 @@ def __init__( data: Union[bytes, memoryview] = bytes(), id: Optional[int] = None, action: Optional[Union[str, MinosRepositoryAction]] = None, + created_at: Optional[datetime] = None, ): if isinstance(data, memoryview): data = data.tobytes() if action is not None and isinstance(action, str): action = MinosRepositoryAction.value_of(action) - self.id = id - self.action = action - self.aggregate_id = aggregate_id self.aggregate_name = aggregate_name self.version = version self.data = data + self.id = id + self.action = action + self.created_at = created_at + @classmethod def from_aggregate(cls, aggregate: Aggregate) -> MinosRepositoryEntry: """Build a new instance from an ``Aggregate``. @@ -80,6 +87,7 @@ def from_aggregate(cls, aggregate: Aggregate) -> MinosRepositoryEntry: :param aggregate: The aggregate instance. :return: A new ``MinosRepositoryEntry`` instance. """ + # noinspection PyTypeChecker return cls(aggregate.id, aggregate.classname, aggregate.version, aggregate.avro_bytes) def __eq__(self, other: "MinosRepositoryEntry") -> bool: @@ -89,12 +97,20 @@ def __hash__(self) -> int: return hash(tuple(self)) def __iter__(self) -> Iterable: - # noinspection PyRedundantParentheses - yield from (self.id, self.action, self.aggregate_name, self.version, self.data) + yield from ( + self.aggregate_id, + self.aggregate_name, + self.version, + self.data, + self.id, + self.action, + self.created_at, + ) def __repr__(self): return ( - f"{type(self).__name__}(id={repr(self.id)}, action={repr(self.action)}, " + f"{type(self).__name__}(" f"aggregate_id={repr(self.aggregate_id)}, aggregate_name={repr(self.aggregate_name)}, " - f"version={repr(self.version)}, data={repr(self.data)})" + f"version={repr(self.version)}, data={repr(self.data)}, " + f"id={repr(self.id)}, action={repr(self.action)}, created_at={repr(self.created_at)})" ) diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 88b2a682..303886ee 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -44,9 +44,7 @@ async def _submit(self, entry: MinosRepositoryEntry) -> MinosRepositoryEntry: "data": entry.data, } response = await self.submit_query_and_fetchone(_INSERT_VALUES_QUERY, params) - entry.id = response[0] - entry.aggregate_id = response[1] - entry.version = response[2] + entry.id, entry.aggregate_id, entry.version, entry.created_at = response return entry async def _select( @@ -78,48 +76,52 @@ async def _select( """.strip() _CREATE_TABLE_QUERY = """ -CREATE TABLE IF NOT EXISTS events ( +CREATE TABLE IF NOT EXISTS aggregate_event ( id BIGSERIAL PRIMARY KEY, action ACTION_TYPE NOT NULL, aggregate_id BIGINT NOT NULL, aggregate_name TEXT NOT NULL, version INT NOT NULL, data BYTEA NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), UNIQUE (aggregate_id, aggregate_name, version) ); """.strip() _INSERT_VALUES_QUERY = """ -INSERT INTO events (id, action, aggregate_id, aggregate_name, version, data) -VALUES (default, - %(action)s, - ( - CASE %(aggregate_id)s - WHEN 0 THEN ( - SELECT (CASE COUNT(*) WHEN 0 THEN 1 ELSE MAX(aggregate_id) + 1 END) - FROM events - WHERE aggregate_name = %(aggregate_name)s - ) - ELSE %(aggregate_id)s END - ), - %(aggregate_name)s, - ( - SELECT (CASE COUNT(*) WHEN 0 THEN 1 ELSE MAX(version) + 1 END) - FROM events - WHERE aggregate_id = %(aggregate_id)s - AND aggregate_name = %(aggregate_name)s +INSERT INTO aggregate_event (id, action, aggregate_id, aggregate_name, version, data, created_at) +VALUES ( + default, + %(action)s, + ( + CASE %(aggregate_id)s + WHEN 0 THEN ( + SELECT (CASE COUNT(*) WHEN 0 THEN 1 ELSE MAX(aggregate_id) + 1 END) + FROM aggregate_event + WHERE aggregate_name = %(aggregate_name)s + ) + ELSE %(aggregate_id)s END ), - %(data)s) -RETURNING id, aggregate_id, version; + %(aggregate_name)s, + ( + SELECT (CASE COUNT(*) WHEN 0 THEN 1 ELSE MAX(version) + 1 END) + FROM aggregate_event + WHERE aggregate_id = %(aggregate_id)s + AND aggregate_name = %(aggregate_name)s + ), + %(data)s, + default +) +RETURNING id, aggregate_id, version, created_at; """.strip() _SELECT_ENTRIES_QUERY = """ -SELECT version, data, id, action -FROM events +SELECT version, data, id, action, created_at +FROM aggregate_event WHERE aggregate_id = %s AND aggregate_name = %s; """.strip() _SELECT_ALL_ENTRIES_QUERY = """ -SELECT aggregate_id, aggregate_name, version, data, id, action -FROM events +SELECT aggregate_id, aggregate_name, version, data, id, action, created_at +FROM aggregate_event """.strip() diff --git a/tests/test_common/test_repository/test_entries.py b/tests/test_common/test_repository/test_entries.py index af8da851..c47d2649 100644 --- a/tests/test_common/test_repository/test_entries.py +++ b/tests/test_common/test_repository/test_entries.py @@ -6,12 +6,18 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ import unittest +from datetime import ( + datetime, +) from minos.common import ( MinosRepositoryAction, MinosRepositoryEntry, MinosRepositoryUnknownActionException, ) +from tests.aggregate_classes import ( + Car, +) class TestMinosRepositoryAction(unittest.TestCase): @@ -34,15 +40,36 @@ def test_constructor(self): self.assertEqual(bytes("car", "utf-8"), entry.data) self.assertEqual(None, entry.id) self.assertEqual(None, entry.action) + self.assertEqual(None, entry.created_at) def test_constructor_extended(self): - entry = MinosRepositoryEntry(1234, "example.Car", 0, bytes("car", "utf-8"), 5678, MinosRepositoryAction.INSERT) + entry = MinosRepositoryEntry( + aggregate_id=1234, + aggregate_name="example.Car", + version=0, + data=bytes("car", "utf-8"), + id=5678, + action=MinosRepositoryAction.INSERT, + created_at=datetime(2020, 10, 13, 8, 45, 32), + ) self.assertEqual(1234, entry.aggregate_id) self.assertEqual("example.Car", entry.aggregate_name) self.assertEqual(0, entry.version) self.assertEqual(bytes("car", "utf-8"), entry.data) self.assertEqual(5678, entry.id) self.assertEqual(MinosRepositoryAction.INSERT, entry.action) + self.assertEqual(datetime(2020, 10, 13, 8, 45, 32), entry.created_at) + + def test_from_aggregate(self): + car = Car(1, 1, 3, "blue") + entry = MinosRepositoryEntry.from_aggregate(car) + self.assertEqual(car.id, entry.aggregate_id) + self.assertEqual(car.classname, entry.aggregate_name) + self.assertEqual(car.version, entry.version) + self.assertIsInstance(entry.data, bytes) + self.assertEqual(None, entry.id) + self.assertEqual(None, entry.action) + self.assertEqual(None, entry.created_at) def test_id_set(self): entry = MinosRepositoryEntry(1234, "example.Car", 0, bytes("car", "utf-8")) @@ -56,6 +83,31 @@ def test_id_action(self): entry.action = MinosRepositoryAction.INSERT self.assertEqual(MinosRepositoryAction.INSERT, entry.action) + def test_equals(self): + a = MinosRepositoryEntry(1234, "example.Car", 0, bytes("car", "utf-8")) + b = MinosRepositoryEntry(1234, "example.Car", 0, bytes("car", "utf-8")) + self.assertEqual(a, b) + + def test_hash(self): + entry = MinosRepositoryEntry(1234, "example.Car", 0, bytes("car", "utf-8")) + self.assertIsInstance(hash(entry), int) + + def test_repr(self): + entry = MinosRepositoryEntry( + aggregate_id=1234, + aggregate_name="example.Car", + version=0, + data=bytes("car", "utf-8"), + id=5678, + action=MinosRepositoryAction.INSERT, + created_at=datetime(2020, 10, 13, 8, 45, 32), + ) + expected = ( + "MinosRepositoryEntry(aggregate_id=1234, aggregate_name='example.Car', version=0, data=b'car', id=5678, " + "action=, created_at=datetime.datetime(2020, 10, 13, 8, 45, 32))" + ) + self.assertEqual(expected, repr(entry)) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_common/test_repository/test_memory.py b/tests/test_common/test_repository/test_memory.py index 0609a4e2..0fb8e2e7 100644 --- a/tests/test_common/test_repository/test_memory.py +++ b/tests/test_common/test_repository/test_memory.py @@ -33,14 +33,14 @@ async def test_update(self): async with MinosInMemoryRepository() as repository: await repository.update(MinosRepositoryEntry(0, "example.Car", 1, bytes("foo", "utf-8"))) expected = [ - MinosRepositoryEntry(0, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.UPDATE) + MinosRepositoryEntry(1, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.UPDATE) ] self.assertEqual(expected, [v async for v in repository.select()]) async def test_delete(self): async with MinosInMemoryRepository() as repository: await repository.delete(MinosRepositoryEntry(0, "example.Car", 1, bytes())) - expected = [MinosRepositoryEntry(0, "example.Car", 1, bytes(), 1, MinosRepositoryAction.DELETE)] + expected = [MinosRepositoryEntry(1, "example.Car", 1, bytes(), 1, MinosRepositoryAction.DELETE)] self.assertEqual(expected, [v async for v in repository.select()]) async def test_select(self): diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index e1790bfd..f6042be5 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -7,8 +7,15 @@ """ import unittest +from datetime import ( + datetime, +) +from typing import ( + NoReturn, +) import aiopg + from minos.common import ( MinosRepository, MinosRepositoryAction, @@ -41,7 +48,7 @@ def test_constructor(self): async def test_setup(self): async with aiopg.connect(**self.repository_db) as connection: async with connection.cursor() as cursor: - template = "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'events');" + template = "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'aggregate_event');" await cursor.execute(template.format(**self.repository_db)) response = (await cursor.fetchone())[0] self.assertFalse(response) @@ -51,7 +58,7 @@ async def test_setup(self): async with aiopg.connect(**self.repository_db) as connection: async with connection.cursor() as cursor: - template = "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'events');" + template = "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'aggregate_event');" await cursor.execute(template.format(**self.repository_db)) response = (await cursor.fetchone())[0] self.assertTrue(response) @@ -74,59 +81,81 @@ async def test_insert(self): expected = [ MinosRepositoryEntry(1, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.INSERT) ] - self.assertEqual(expected, [v async for v in repository.select()]) + observed = [v async for v in repository.select()] + self._assert_equal_entries(expected, observed) async def test_update(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: await repository.update(MinosRepositoryEntry(0, "example.Car", 1, bytes("foo", "utf-8"))) expected = [ - MinosRepositoryEntry(0, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.UPDATE) + MinosRepositoryEntry(1, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.UPDATE) ] - self.assertEqual(expected, [v async for v in repository.select()]) + observed = [v async for v in repository.select()] + self._assert_equal_entries(expected, observed) async def test_delete(self): async with PostgreSqlMinosRepository(**self.repository_db) as repository: await repository.delete(MinosRepositoryEntry(0, "example.Car", 1, bytes())) - expected = [MinosRepositoryEntry(0, "example.Car", 1, bytes(), 1, MinosRepositoryAction.DELETE)] - self.assertEqual(expected, [v async for v in repository.select()]) + expected = [MinosRepositoryEntry(1, "example.Car", 1, bytes(), 1, MinosRepositoryAction.DELETE)] + observed = [v async for v in repository.select()] + self._assert_equal_entries(expected, observed) async def test_select(self): - repository = await self._build_repository() - expected = [ - MinosRepositoryEntry(1, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.INSERT), - MinosRepositoryEntry(1, "example.Car", 2, bytes("bar", "utf-8"), 2, MinosRepositoryAction.UPDATE), - MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), - MinosRepositoryEntry(1, "example.Car", 3, bytes("foobar", "utf-8"), 4, MinosRepositoryAction.UPDATE), - MinosRepositoryEntry(1, "example.Car", 4, bytes(), 5, MinosRepositoryAction.DELETE), - MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), - MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"), 7, MinosRepositoryAction.INSERT), - ] - self.assertEqual(expected, [v async for v in repository.select()]) + async with (await self._build_repository()) as repository: + expected = [ + MinosRepositoryEntry(1, "example.Car", 1, bytes("foo", "utf-8"), 1, MinosRepositoryAction.INSERT), + MinosRepositoryEntry(1, "example.Car", 2, bytes("bar", "utf-8"), 2, MinosRepositoryAction.UPDATE), + MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), + MinosRepositoryEntry(1, "example.Car", 3, bytes("foobar", "utf-8"), 4, MinosRepositoryAction.UPDATE), + MinosRepositoryEntry(1, "example.Car", 4, bytes(), 5, MinosRepositoryAction.DELETE), + MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), + MinosRepositoryEntry( + 1, "example.MotorCycle", 1, bytes("one", "utf-8"), 7, MinosRepositoryAction.INSERT + ), + ] + observed = [v async for v in repository.select()] + self._assert_equal_entries(expected, observed) async def test_select_empty(self): - repository = PostgreSqlMinosRepository(**self.repository_db) - self.assertEqual([], [v async for v in repository.select()]) + async with PostgreSqlMinosRepository(**self.repository_db) as repository: + expected = [] + observed = [v async for v in repository.select()] + self._assert_equal_entries(expected, observed) async def test_select_filtered(self): - repository = await self._build_repository() - expected = [ - MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), - MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), - ] - self.assertEqual(expected, [v async for v in repository.select(aggregate_name="example.Car", aggregate_id=2)]) + async with (await self._build_repository()) as repository: + expected = [ + MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"), 3, MinosRepositoryAction.INSERT), + MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"), 6, MinosRepositoryAction.UPDATE), + ] + observed = [v async for v in repository.select(aggregate_name="example.Car", aggregate_id=2)] + self._assert_equal_entries(expected, observed) async def _build_repository(self): - repository = PostgreSqlMinosRepository(**self.repository_db) - await repository._setup() - await repository.insert(MinosRepositoryEntry(1, "example.Car", 1, bytes("foo", "utf-8"))) - await repository.update(MinosRepositoryEntry(1, "example.Car", 2, bytes("bar", "utf-8"))) - await repository.insert(MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"))) - await repository.update(MinosRepositoryEntry(1, "example.Car", 3, bytes("foobar", "utf-8"))) - await repository.delete(MinosRepositoryEntry(1, "example.Car", 4)) - await repository.update(MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"))) - await repository.insert(MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"))) - - return repository + async with PostgreSqlMinosRepository(**self.repository_db) as repository: + await repository.insert(MinosRepositoryEntry(1, "example.Car", 1, bytes("foo", "utf-8"))) + await repository.update(MinosRepositoryEntry(1, "example.Car", 2, bytes("bar", "utf-8"))) + await repository.insert(MinosRepositoryEntry(2, "example.Car", 1, bytes("hello", "utf-8"))) + await repository.update(MinosRepositoryEntry(1, "example.Car", 3, bytes("foobar", "utf-8"))) + await repository.delete(MinosRepositoryEntry(1, "example.Car", 4)) + await repository.update(MinosRepositoryEntry(2, "example.Car", 2, bytes("bye", "utf-8"))) + await repository.insert(MinosRepositoryEntry(1, "example.MotorCycle", 1, bytes("one", "utf-8"))) + return repository + + def _assert_equal_entries( + self, expected: list[MinosRepositoryEntry], observed: list[MinosRepositoryEntry] + ) -> NoReturn: + self.assertEqual(len(expected), len(observed)) + + for e, o in zip(expected, observed): + self.assertEqual(type(e), type(o)) + self.assertEqual(e.aggregate_id, o.aggregate_id) + self.assertEqual(e.aggregate_name, o.aggregate_name) + self.assertEqual(e.version, o.version) + self.assertEqual(e.data, o.data) + self.assertEqual(e.id, o.id) + self.assertEqual(e.action, o.action) + self.assertIsInstance(o.created_at, datetime) if __name__ == "__main__": From 3f7b790d8ae1d195c28fc158a296b2d5350625f9 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 3 May 2021 07:33:10 +0000 Subject: [PATCH 45/47] Restyled by black --- minos/common/database.py | 12 +++-------- minos/common/repository/entries.py | 20 +++++-------------- minos/common/repository/pg.py | 16 ++++----------- minos/common/setup.py | 4 +--- .../test_repository/test_entries.py | 8 ++------ tests/test_common/test_repository/test_pg.py | 20 +++++-------------- 6 files changed, 20 insertions(+), 60 deletions(-) diff --git a/minos/common/database.py b/minos/common/database.py index 4f567a79..4e029e7f 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -5,22 +5,16 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import ( - ABC, -) +from abc import ABC from typing import ( AsyncIterator, NoReturn, ) import aiopg -from aiopg import ( - Pool, -) +from aiopg import Pool -from .setup import ( - MinosSetup, -) +from .setup import MinosSetup class PostgreSqlMinosDatabase(ABC, MinosSetup): diff --git a/minos/common/repository/entries.py b/minos/common/repository/entries.py index 5922e895..784fb1b6 100644 --- a/minos/common/repository/entries.py +++ b/minos/common/repository/entries.py @@ -5,16 +5,10 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import ( - annotations, -) +from __future__ import annotations -from datetime import ( - datetime, -) -from enum import ( - Enum, -) +from datetime import datetime +from enum import Enum from typing import ( TYPE_CHECKING, Iterable, @@ -22,14 +16,10 @@ Union, ) -from ..exceptions import ( - MinosRepositoryUnknownActionException, -) +from ..exceptions import MinosRepositoryUnknownActionException if TYPE_CHECKING: - from ..model import ( - Aggregate, - ) + from ..model import Aggregate class MinosRepositoryAction(Enum): diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 303886ee..6b9000f5 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -5,19 +5,11 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from typing import ( - AsyncIterator, -) +from typing import AsyncIterator -from ..database import ( - PostgreSqlMinosDatabase, -) -from .abc import ( - MinosRepository, -) -from .entries import ( - MinosRepositoryEntry, -) +from ..database import PostgreSqlMinosDatabase +from .abc import MinosRepository +from .entries import MinosRepositoryEntry class PostgreSqlMinosRepository(MinosRepository, PostgreSqlMinosDatabase): diff --git a/minos/common/setup.py b/minos/common/setup.py index c928e156..77140fc0 100644 --- a/minos/common/setup.py +++ b/minos/common/setup.py @@ -5,9 +5,7 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import ( - abstractmethod, -) +from abc import abstractmethod from typing import ( Generic, NoReturn, diff --git a/tests/test_common/test_repository/test_entries.py b/tests/test_common/test_repository/test_entries.py index c47d2649..a516bb42 100644 --- a/tests/test_common/test_repository/test_entries.py +++ b/tests/test_common/test_repository/test_entries.py @@ -6,18 +6,14 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ import unittest -from datetime import ( - datetime, -) +from datetime import datetime from minos.common import ( MinosRepositoryAction, MinosRepositoryEntry, MinosRepositoryUnknownActionException, ) -from tests.aggregate_classes import ( - Car, -) +from tests.aggregate_classes import Car class TestMinosRepositoryAction(unittest.TestCase): diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index f6042be5..303b63e0 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -7,12 +7,8 @@ """ import unittest -from datetime import ( - datetime, -) -from typing import ( - NoReturn, -) +from datetime import datetime +from typing import NoReturn import aiopg @@ -22,15 +18,9 @@ MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import ( - PostgresAsyncTestCase, -) -from tests.aggregate_classes import ( - Car, -) -from tests.utils import ( - BASE_PATH, -) +from minos.common.testing import PostgresAsyncTestCase +from tests.aggregate_classes import Car +from tests.utils import BASE_PATH class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From 755f20ecdc5979bafceefa29be8011672a9cb78b Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 3 May 2021 07:33:11 +0000 Subject: [PATCH 46/47] Restyled by isort --- minos/common/database.py | 12 ++++++++--- minos/common/repository/entries.py | 16 ++++++++++---- minos/common/repository/pg.py | 16 ++++++++++---- minos/common/setup.py | 4 +++- .../test_repository/test_entries.py | 8 +++++-- tests/test_common/test_repository/test_pg.py | 21 +++++++++++++------ 6 files changed, 57 insertions(+), 20 deletions(-) diff --git a/minos/common/database.py b/minos/common/database.py index 4e029e7f..4f567a79 100644 --- a/minos/common/database.py +++ b/minos/common/database.py @@ -5,16 +5,22 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import ABC +from abc import ( + ABC, +) from typing import ( AsyncIterator, NoReturn, ) import aiopg -from aiopg import Pool +from aiopg import ( + Pool, +) -from .setup import MinosSetup +from .setup import ( + MinosSetup, +) class PostgreSqlMinosDatabase(ABC, MinosSetup): diff --git a/minos/common/repository/entries.py b/minos/common/repository/entries.py index 784fb1b6..68b31670 100644 --- a/minos/common/repository/entries.py +++ b/minos/common/repository/entries.py @@ -5,10 +5,16 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from __future__ import annotations +from __future__ import ( + annotations, +) -from datetime import datetime -from enum import Enum +from datetime import ( + datetime, +) +from enum import ( + Enum, +) from typing import ( TYPE_CHECKING, Iterable, @@ -16,7 +22,9 @@ Union, ) -from ..exceptions import MinosRepositoryUnknownActionException +from ..exceptions import ( + MinosRepositoryUnknownActionException, +) if TYPE_CHECKING: from ..model import Aggregate diff --git a/minos/common/repository/pg.py b/minos/common/repository/pg.py index 6b9000f5..303886ee 100644 --- a/minos/common/repository/pg.py +++ b/minos/common/repository/pg.py @@ -5,11 +5,19 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from typing import AsyncIterator +from typing import ( + AsyncIterator, +) -from ..database import PostgreSqlMinosDatabase -from .abc import MinosRepository -from .entries import MinosRepositoryEntry +from ..database import ( + PostgreSqlMinosDatabase, +) +from .abc import ( + MinosRepository, +) +from .entries import ( + MinosRepositoryEntry, +) class PostgreSqlMinosRepository(MinosRepository, PostgreSqlMinosDatabase): diff --git a/minos/common/setup.py b/minos/common/setup.py index 77140fc0..c928e156 100644 --- a/minos/common/setup.py +++ b/minos/common/setup.py @@ -5,7 +5,9 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ -from abc import abstractmethod +from abc import ( + abstractmethod, +) from typing import ( Generic, NoReturn, diff --git a/tests/test_common/test_repository/test_entries.py b/tests/test_common/test_repository/test_entries.py index a516bb42..c47d2649 100644 --- a/tests/test_common/test_repository/test_entries.py +++ b/tests/test_common/test_repository/test_entries.py @@ -6,14 +6,18 @@ Minos framework can not be copied and/or distributed without the express permission of Clariteia SL. """ import unittest -from datetime import datetime +from datetime import ( + datetime, +) from minos.common import ( MinosRepositoryAction, MinosRepositoryEntry, MinosRepositoryUnknownActionException, ) -from tests.aggregate_classes import Car +from tests.aggregate_classes import ( + Car, +) class TestMinosRepositoryAction(unittest.TestCase): diff --git a/tests/test_common/test_repository/test_pg.py b/tests/test_common/test_repository/test_pg.py index 303b63e0..79dbd2bb 100644 --- a/tests/test_common/test_repository/test_pg.py +++ b/tests/test_common/test_repository/test_pg.py @@ -7,20 +7,29 @@ """ import unittest -from datetime import datetime -from typing import NoReturn +from datetime import ( + datetime, +) +from typing import ( + NoReturn, +) import aiopg - from minos.common import ( MinosRepository, MinosRepositoryAction, MinosRepositoryEntry, PostgreSqlMinosRepository, ) -from minos.common.testing import PostgresAsyncTestCase -from tests.aggregate_classes import Car -from tests.utils import BASE_PATH +from minos.common.testing import ( + PostgresAsyncTestCase, +) +from tests.aggregate_classes import ( + Car, +) +from tests.utils import ( + BASE_PATH, +) class TestPostgreSqlMinosRepository(PostgresAsyncTestCase): From b0d121e97e7975cc49c71297db5c6e5a7cdb9e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Mon, 3 May 2021 09:49:55 +0200 Subject: [PATCH 47/47] ISSUE #155 * Add `snapshot` entry on `minos.common.MinosConfig`. --- minos/common/configuration/config.py | 27 +++++++++++++++++-- .../test_configuration/test_config.py | 9 +++++++ tests/test_config.yml | 6 +++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/minos/common/configuration/config.py b/minos/common/configuration/config.py index e67f6439..7ef5da6d 100644 --- a/minos/common/configuration/config.py +++ b/minos/common/configuration/config.py @@ -30,12 +30,11 @@ EVENT = collections.namedtuple("Event", "name controller action") COMMAND = collections.namedtuple("Command", "name controller action") SERVICE = collections.namedtuple("Service", "name") - EVENTS = collections.namedtuple("Events", "broker items queue") COMMANDS = collections.namedtuple("Commands", "broker items queue") REST = collections.namedtuple("Rest", "broker endpoints") - REPOSITORY = collections.namedtuple("Repository", "database user password host port") +SNAPSHOT = collections.namedtuple("Snapshot", "database user password host port") _ENVIRONMENT_MAPPER = { "commands.queue.host": "MINOS_COMMANDS_QUEUE_HOST", @@ -57,6 +56,11 @@ "repository.database": "MINOS_REPOSITORY_DATABASE", "repository.user": "MINOS_REPOSITORY_USER", "repository.password": "MINOS_REPOSITORY_PASSWORD", + "snapshot.host": "MINOS_SNAPSHOT_HOST", + "snapshot.port": "MINOS_SNAPSHOT_PORT", + "snapshot.database": "MINOS_SNAPSHOT_DATABASE", + "snapshot.user": "MINOS_SNAPSHOT_USER", + "snapshot.password": "MINOS_SNAPSHOT_PASSWORD", } _PARAMETERIZED_MAPPER = { @@ -79,6 +83,11 @@ "repository.database": "repository_database", "repository.user": "repository_user", "repository.password": "repository_password", + "snapshot.host": "snapshot_host", + "snapshot.port": "snapshot_port", + "snapshot.database": "snapshot_database", + "snapshot.user": "snapshot_user", + "snapshot.password": "snapshot_password", } _default: t.Optional[MinosConfigAbstract] = None @@ -311,3 +320,17 @@ def repository(self) -> REPOSITORY: host=self._get("repository.host"), port=int(self._get("repository.port")), ) + + @property + def snapshot(self) -> SNAPSHOT: + """Get the snapshot config. + + :return: A ``SNAPSHOT`` NamedTuple instance. + """ + return SNAPSHOT( + database=self._get("snapshot.database"), + user=self._get("snapshot.user"), + password=self._get("snapshot.password"), + host=self._get("snapshot.host"), + port=int(self._get("snapshot.port")), + ) diff --git a/tests/test_common/test_configuration/test_config.py b/tests/test_common/test_configuration/test_config.py index c6122520..2a11ae15 100644 --- a/tests/test_common/test_configuration/test_config.py +++ b/tests/test_common/test_configuration/test_config.py @@ -86,6 +86,15 @@ def test_config_repository(self): self.assertEqual("localhost", repository.host) self.assertEqual(5432, repository.port) + def test_config_snapshot(self): + config = MinosConfig(path=self.config_file_path, with_environment=False) + snapshot = config.snapshot + self.assertEqual("order_db", snapshot.database) + self.assertEqual("minos", snapshot.user) + self.assertEqual("min0s", snapshot.password) + self.assertEqual("localhost", snapshot.host) + self.assertEqual(5432, snapshot.port) + @mock.patch.dict(os.environ, {"MINOS_REPOSITORY_DATABASE": "foo"}) def test_overwrite_with_environment(self): config = MinosConfig(path=self.config_file_path) diff --git a/tests/test_config.yml b/tests/test_config.yml index a1cd1430..239d8a02 100644 --- a/tests/test_config.yml +++ b/tests/test_config.yml @@ -15,6 +15,12 @@ repository: password: min0s host: localhost port: 5432 +snapshot: + database: order_db + user: minos + password: min0s + host: localhost + port: 5432 events: broker: localhost port: 9092