Skip to content

Commit 38e03ed

Browse files
author
Sergio García Prado
authored
Merge pull request #357 from minos-framework/issue-346-add-pool-factory
#346 - Add `PoolFactory` class
2 parents 94dd238 + a040925 commit 38e03ed

File tree

88 files changed

+905
-642
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+905
-642
lines changed

.github/workflows/minos-microservice-common-tests.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ jobs:
2929
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
3030

3131
env:
32-
MINOS_BROKER_QUEUE_HOST: postgres
33-
MINOS_REPOSITORY_HOST: postgres
34-
MINOS_SNAPSHOT_HOST: postgres
32+
MINOS_DATABASES_DEFAULT_HOST: postgres
33+
MINOS_DATABASES_QUERY_HOST: postgres
3534

3635
steps:
3736
- name: Check out repository code

packages/core/minos-microservice-aggregate/minos/aggregate/entities/refs/resolvers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
)
1111
from typing import (
1212
Any,
13+
Optional,
1314
Union,
1415
)
1516
from uuid import (
@@ -19,6 +20,8 @@
1920
from minos.common import (
2021
Inject,
2122
Model,
23+
NotProvidedException,
24+
PoolFactory,
2225
)
2326
from minos.networks import (
2427
BrokerClient,
@@ -45,7 +48,18 @@ class RefResolver:
4548

4649
# noinspection PyUnusedLocal
4750
@Inject()
48-
def __init__(self, broker_pool: BrokerClientPool, **kwargs):
51+
def __init__(
52+
self,
53+
broker_pool: Optional[BrokerClientPool] = None,
54+
pool_factory: Optional[PoolFactory] = None,
55+
**kwargs,
56+
):
57+
if broker_pool is None and pool_factory is not None:
58+
broker_pool = pool_factory.get_pool("broker")
59+
60+
if not isinstance(broker_pool, BrokerClientPool):
61+
raise NotProvidedException(f"A {BrokerClientPool!r} instance is required. Obtained: {broker_pool}")
62+
4963
self.broker_pool = broker_pool
5064

5165
# noinspection PyUnusedLocal

packages/core/minos-microservice-aggregate/minos/aggregate/events/repositories/abc.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Lock,
3030
LockPool,
3131
NotProvidedException,
32+
PoolFactory,
3233
SetupMixin,
3334
)
3435
from minos.networks import (
@@ -74,12 +75,16 @@ def __init__(
7475
self,
7576
broker_publisher: BrokerPublisher,
7677
transaction_repository: TransactionRepository,
77-
lock_pool: LockPool,
78+
lock_pool: Optional[LockPool] = None,
79+
pool_factory: Optional[PoolFactory] = None,
7880
*args,
7981
**kwargs,
8082
):
8183
super().__init__(*args, **kwargs)
8284

85+
if lock_pool is None and pool_factory is not None:
86+
lock_pool = pool_factory.get_pool("lock")
87+
8388
if broker_publisher is None:
8489
raise NotProvidedException("A broker instance is required.")
8590

packages/core/minos-microservice-aggregate/minos/aggregate/events/repositories/pg.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from minos.common import (
2424
NULL_UUID,
2525
Config,
26-
PostgreSqlMinosDatabase,
26+
DatabaseMixin,
2727
)
2828

2929
from ...exceptions import (
@@ -37,12 +37,12 @@
3737
)
3838

3939

40-
class PostgreSqlEventRepository(PostgreSqlMinosDatabase, EventRepository):
40+
class PostgreSqlEventRepository(DatabaseMixin, EventRepository):
4141
"""PostgreSQL-based implementation of the event repository class in ``Minos``."""
4242

4343
@classmethod
44-
def _from_config(cls, *args, config: Config, **kwargs) -> Optional[EventRepository]:
45-
return cls(*args, **config.get_database_by_name("event"), **kwargs)
44+
def _from_config(cls, config: Config, **kwargs) -> Optional[EventRepository]:
45+
return super()._from_config(config, **config.get_database_by_name("event"), **kwargs)
4646

4747
async def _setup(self):
4848
"""Setup miscellaneous repository thing.

packages/core/minos-microservice-aggregate/minos/aggregate/snapshots/pg/abc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
from minos.common import (
1111
Config,
12-
PostgreSqlMinosDatabase,
12+
DatabaseMixin,
1313
)
1414

1515

16-
class PostgreSqlSnapshotSetup(PostgreSqlMinosDatabase):
16+
class PostgreSqlSnapshotSetup(DatabaseMixin):
1717
"""Minos Snapshot Setup Class"""
1818

1919
@classmethod

packages/core/minos-microservice-aggregate/minos/aggregate/transactions/repositories/abc.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from __future__ import (
2+
annotations,
3+
)
4+
15
from abc import (
26
ABC,
37
abstractmethod,
@@ -19,6 +23,7 @@
1923
Lock,
2024
LockPool,
2125
NotProvidedException,
26+
PoolFactory,
2227
SetupMixin,
2328
)
2429

@@ -36,9 +41,14 @@ class TransactionRepository(ABC, SetupMixin):
3641
"""Transaction Repository base class."""
3742

3843
@Inject()
39-
def __init__(self, lock_pool: LockPool, *args, **kwargs):
44+
def __init__(
45+
self, lock_pool: Optional[LockPool] = None, pool_factory: Optional[PoolFactory] = None, *args, **kwargs
46+
):
4047
super().__init__(*args, **kwargs)
4148

49+
if lock_pool is None and pool_factory is not None:
50+
lock_pool = pool_factory.get_pool("lock")
51+
4252
if lock_pool is None:
4353
raise NotProvidedException("A lock pool instance is required.")
4454

packages/core/minos-microservice-aggregate/minos/aggregate/transactions/repositories/pg.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from minos.common import (
1717
Config,
18-
PostgreSqlMinosDatabase,
18+
DatabaseMixin,
1919
)
2020

2121
from ...exceptions import (
@@ -29,12 +29,12 @@
2929
)
3030

3131

32-
class PostgreSqlTransactionRepository(PostgreSqlMinosDatabase, TransactionRepository):
32+
class PostgreSqlTransactionRepository(DatabaseMixin, TransactionRepository):
3333
"""PostgreSql Transaction Repository class."""
3434

3535
@classmethod
36-
def _from_config(cls, *args, config: Config, **kwargs) -> Optional[PostgreSqlTransactionRepository]:
37-
return cls(*args, **config.get_database_by_name("transaction"), **kwargs)
36+
def _from_config(cls, config: Config, **kwargs) -> Optional[PostgreSqlTransactionRepository]:
37+
return super()._from_config(config, **config.get_database_by_name("transaction"), **kwargs)
3838

3939
async def _setup(self):
4040
await self.submit_query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";', lock="uuid-ossp")

packages/core/minos-microservice-aggregate/tests/test_aggregate/test_aggregate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
)
1212
from tests.utils import (
1313
CONFIG_FILE_PATH,
14-
MinosTestCase,
14+
AggregateTestCase,
1515
Order,
1616
OrderAggregate,
1717
)
1818

1919

20-
class TestAggregate(MinosTestCase):
20+
class TestAggregate(AggregateTestCase):
2121
async def test_root(self):
2222
async with OrderAggregate.from_config(CONFIG_FILE_PATH) as aggregate:
2323
self.assertEqual(Order, aggregate.root)

packages/core/minos-microservice-aggregate/tests/test_aggregate/test_entities/test_models/test_root/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
current_datetime,
1919
)
2020
from tests.utils import (
21+
AggregateTestCase,
2122
Car,
22-
MinosTestCase,
2323
)
2424

2525

26-
class TestRootEntity(MinosTestCase):
26+
class TestRootEntity(AggregateTestCase):
2727
async def test_create(self):
2828
observed = await Car.create(doors=3, color="blue")
2929
expected = Car(

packages/core/minos-microservice-aggregate/tests/test_aggregate/test_entities/test_models/test_root/test_broker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
BrokerMessageV1,
1515
)
1616
from tests.utils import (
17+
AggregateTestCase,
1718
Car,
18-
MinosTestCase,
1919
Owner,
2020
)
2121

2222

23-
class TestRootEntityBroker(MinosTestCase):
23+
class TestRootEntityBroker(AggregateTestCase):
2424
async def test_create(self):
2525
car = await Car.create(doors=3, color="blue")
2626

0 commit comments

Comments
 (0)