Skip to content

Commit 9d874c9

Browse files
author
Sergio García Prado
committed
ISSUE #346
* Improve tests.
1 parent e703077 commit 9d874c9

File tree

4 files changed

+38
-77
lines changed

4 files changed

+38
-77
lines changed

packages/core/minos-microservice-common/minos/common/testing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ def __getattr__(self, item):
6464

6565

6666
class PostgresAsyncTestCase(MinosTestCase):
67-
def __init__(self, *args, **kwargs):
68-
super().__init__(*args, **kwargs)
67+
def setUp(self):
6968

7069
self._uuid = uuid4()
7170
self._config = Config(self.CONFIG_FILE_PATH)
@@ -82,6 +81,8 @@ def __init__(self, *args, **kwargs):
8281
self.broker_queue_db = self._meta_broker_queue_db | self._test_db
8382
self.snapshot_db = self._meta_snapshot_db | self._test_db
8483

84+
super().setUp()
85+
8586
def get_config(self):
8687
return Config(
8788
self.CONFIG_FILE_PATH,

packages/core/minos-microservice-saga/tests/utils.py

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
PoolFactory,
2525
SetupMixin,
2626
)
27+
from minos.common.testing import MinosTestCase as MinosTestCaseBase
2728
from minos.networks import (
2829
BrokerClientPool,
2930
InMemoryBrokerPublisher,
@@ -41,53 +42,32 @@
4142
DB_PATH = BASE_PATH / "test_db.lmdb"
4243

4344

44-
class MinosTestCase(unittest.IsolatedAsyncioTestCase):
45-
def setUp(self) -> None:
46-
super().setUp()
45+
class MinosTestCase(MinosTestCaseBase):
46+
CONFIG_FILE_PATH = CONFIG_FILE_PATH
4747

48-
self.config = Config(CONFIG_FILE_PATH)
49-
50-
self.pool_factory = PoolFactory.from_config(
51-
CONFIG_FILE_PATH, default_classes={"broker": BrokerClientPool, "lock": FakeLockPool}
52-
)
53-
self.broker_publisher = InMemoryBrokerPublisher()
54-
self.broker_subscriber_builder = InMemoryBrokerSubscriberBuilder()
55-
self.transaction_repository = InMemoryTransactionRepository(lock_pool=self.pool_factory.get_pool("lock"))
56-
self.event_repository = InMemoryEventRepository(
57-
broker_publisher=self.broker_publisher,
58-
transaction_repository=self.transaction_repository,
59-
lock_pool=self.pool_factory.get_pool("lock"),
48+
def get_injections(self):
49+
pool_factory = PoolFactory.from_config(
50+
self.config, default_classes={"broker": BrokerClientPool, "lock": FakeLockPool}
6051
)
61-
self.snapshot_repository = InMemorySnapshotRepository(
62-
event_repository=self.event_repository, transaction_repository=self.transaction_repository
52+
broker_publisher = InMemoryBrokerPublisher()
53+
broker_subscriber_builder = InMemoryBrokerSubscriberBuilder()
54+
transaction_repository = InMemoryTransactionRepository(lock_pool=pool_factory.get_pool("lock"))
55+
event_repository = InMemoryEventRepository(
56+
broker_publisher=broker_publisher,
57+
transaction_repository=transaction_repository,
58+
lock_pool=pool_factory.get_pool("lock"),
6359
)
64-
65-
self.injector = DependencyInjector(
66-
self.config,
67-
[
68-
self.pool_factory,
69-
self.broker_publisher,
70-
self.broker_subscriber_builder,
71-
self.transaction_repository,
72-
self.event_repository,
73-
self.snapshot_repository,
74-
],
60+
snapshot_repository = InMemorySnapshotRepository(
61+
event_repository=event_repository, transaction_repository=transaction_repository
7562
)
76-
self.injector.wire_injections()
77-
78-
async def asyncSetUp(self):
79-
await super().asyncSetUp()
80-
81-
await self.injector.setup_injections()
82-
83-
async def asyncTearDown(self):
84-
await self.injector.destroy_injections()
85-
86-
await super().asyncTearDown()
87-
88-
def tearDown(self) -> None:
89-
self.injector.unwire_injections()
90-
super().tearDown()
63+
return [
64+
pool_factory,
65+
broker_publisher,
66+
broker_subscriber_builder,
67+
transaction_repository,
68+
event_repository,
69+
snapshot_repository,
70+
]
9171

9272

9373
class FakeBrokerPublisher(SetupMixin):

packages/plugins/minos-broker-kafka/tests/test_kafka/test_publisher.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
from minos.common import (
1515
Config,
16-
DatabaseClientPool,
16+
)
17+
from minos.common.testing import (
18+
PostgresAsyncTestCase,
1719
)
1820
from minos.networks import (
1921
BrokerMessage,
@@ -152,23 +154,13 @@ def test_build(self):
152154
self.assertEqual(common_config["port"], publisher.port)
153155

154156

155-
class TestPostgreSqlQueuedKafkaBrokerPublisher(unittest.IsolatedAsyncioTestCase):
156-
def setUp(self) -> None:
157-
super().setUp()
158-
self.database_pool = DatabaseClientPool.from_config(CONFIG_FILE_PATH)
159-
160-
async def asyncSetUp(self) -> None:
161-
await super().asyncSetUp()
162-
await self.database_pool.setup()
163-
164-
async def asyncTearDown(self) -> None:
165-
await self.database_pool.destroy()
166-
await super().asyncTearDown()
157+
class TestPostgreSqlQueuedKafkaBrokerPublisher(PostgresAsyncTestCase):
158+
CONFIG_FILE_PATH = CONFIG_FILE_PATH
167159

168160
def test_from_config(self):
169161
with warnings.catch_warnings():
170162
warnings.simplefilter("ignore", DeprecationWarning)
171-
publisher = PostgreSqlQueuedKafkaBrokerPublisher.from_config(CONFIG_FILE_PATH, pool=self.database_pool)
163+
publisher = PostgreSqlQueuedKafkaBrokerPublisher.from_config(CONFIG_FILE_PATH)
172164
self.assertIsInstance(publisher, PostgreSqlQueuedKafkaBrokerPublisher)
173165
self.assertIsInstance(publisher.impl, KafkaBrokerPublisher)
174166
self.assertIsInstance(publisher.queue, PostgreSqlBrokerPublisherQueue)

packages/plugins/minos-broker-kafka/tests/test_kafka/test_subscriber.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
from minos.common import (
2323
Config,
24-
DatabaseClientPool,
24+
)
25+
from minos.common.testing import (
26+
PostgresAsyncTestCase,
2527
)
2628
from minos.networks import (
2729
BrokerMessageV1,
@@ -251,28 +253,14 @@ def test_build(self):
251253
self.assertEqual(common_config["port"], subscriber.port)
252254

253255

254-
class TestPostgreSqlQueuedKafkaBrokerSubscriberBuilder(unittest.IsolatedAsyncioTestCase):
255-
def setUp(self) -> None:
256-
super().setUp()
257-
self.config = Config(CONFIG_FILE_PATH)
258-
self.database_pool = DatabaseClientPool.from_config(CONFIG_FILE_PATH)
259-
260-
async def asyncSetUp(self) -> None:
261-
await super().asyncSetUp()
262-
await self.database_pool.setup()
263-
264-
async def asyncTearDown(self) -> None:
265-
await self.database_pool.destroy()
266-
await super().asyncTearDown()
256+
class TestPostgreSqlQueuedKafkaBrokerSubscriberBuilder(PostgresAsyncTestCase):
257+
CONFIG_FILE_PATH = CONFIG_FILE_PATH
267258

268259
def test_build(self):
269260
with warnings.catch_warnings():
270261
warnings.simplefilter("ignore", DeprecationWarning)
271262
builder = (
272-
PostgreSqlQueuedKafkaBrokerSubscriberBuilder()
273-
.with_config(self.config)
274-
.with_topics({"one", "two"})
275-
.with_kwargs({"pool": self.database_pool})
263+
PostgreSqlQueuedKafkaBrokerSubscriberBuilder().with_config(self.config).with_topics({"one", "two"})
276264
)
277265

278266
subscriber = builder.build()

0 commit comments

Comments
 (0)