Skip to content

Commit 3656a66

Browse files
author
Sergio García Prado
committed
ISSUE #346
* Increase coverage. * Fix bug related with `PostgresAsyncTestCase`.
1 parent 6bd6790 commit 3656a66

File tree

6 files changed

+158
-40
lines changed

6 files changed

+158
-40
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ def _get_pool_class(self, type_: str) -> type[Pool]:
9090
pool_cls = self._config.get_pools().get(type_)
9191

9292
if pool_cls is None:
93-
raise ValueError
93+
raise ValueError(
94+
f"There is not any provided {type!r} to build pools that matches the given type: {type_!r}"
95+
)
9496

9597
return pool_cls
9698

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

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import unittest
2+
import warnings
23
from abc import (
34
ABC,
45
)
56
from contextlib import (
67
suppress,
78
)
8-
from itertools import (
9-
starmap,
10-
)
119
from pathlib import (
1210
Path,
1311
)
@@ -77,21 +75,24 @@ def __getattr__(self, item: str) -> Any:
7775
# noinspection SqlNoDataSourceInspection
7876
class PostgresAsyncTestCase(MinosTestCase, ABC):
7977
def setUp(self):
80-
self._config = Config(self.get_config_file_path())
78+
self.base_config = Config(self.get_config_file_path())
8179
self._uuid = uuid4()
8280
self._test_db = {"database": f"test_db_{self._uuid.hex}", "user": f"test_user_{self._uuid.hex}"}
8381
super().setUp()
8482

8583
@property
8684
def repository_db(self) -> dict[str, Any]:
85+
warnings.warn("'repository_db' attribute has been deprecated.", DeprecationWarning)
8786
return self.config.get_database_by_name("aggregate") | self._test_db
8887

8988
@property
9089
def broker_queue_db(self) -> dict[str, Any]:
90+
warnings.warn("'broker_queue_db' attribute has been deprecated.", DeprecationWarning)
9191
return self.config.get_database_by_name("broker") | self._test_db
9292

9393
@property
9494
def snapshot_db(self) -> dict[str, Any]:
95+
warnings.warn("'snapshot_db' attribute has been deprecated.", DeprecationWarning)
9596
return self.config.get_database_by_name("aggregate") | self._test_db
9697

9798
def get_config(self) -> Config:
@@ -109,15 +110,15 @@ def get_injections(self) -> list[Union[InjectableMixin, type[InjectableMixin], s
109110
return [PoolFactory.from_config(self.config, default_classes={"database": DatabaseClientPool})]
110111

111112
async def asyncSetUp(self):
112-
pairs = self._drop_duplicates(
113-
[(db, self._test_db) for db in self._config.get_databases().values() if "database" in db]
114-
)
113+
await self._create_database(self.base_config.get_default_database(), self._test_db)
114+
await super().asyncSetUp()
115115

116-
for meta, test in pairs:
117-
await self._setup_database(meta, test)
116+
async def asyncTearDown(self):
117+
await super().asyncTearDown()
118+
await self._drop_database(self.base_config.get_default_database(), self._test_db)
118119

119-
async def _setup_database(self, meta: dict[str, Any], test: dict[str, Any]) -> None:
120-
await self._teardown_database(meta, test)
120+
async def _create_database(self, meta: dict[str, Any], test: dict[str, Any]) -> None:
121+
await self._drop_database(meta, test)
121122

122123
async with aiopg.connect(**meta) as connection:
123124
async with connection.cursor() as cursor:
@@ -127,28 +128,8 @@ async def _setup_database(self, meta: dict[str, Any], test: dict[str, Any]) -> N
127128
template = "CREATE DATABASE {database} WITH OWNER = {user};"
128129
await cursor.execute(template.format(**(meta | test)))
129130

130-
await super().asyncSetUp()
131-
132-
async def asyncTearDown(self):
133-
await super().asyncTearDown()
134-
135-
pairs = self._drop_duplicates(
136-
[(db, self._test_db) for db in self._config.get_databases().values() if "database" in db]
137-
)
138-
139-
for meta, test in pairs:
140-
await self._teardown_database(meta, test)
141-
142-
@staticmethod
143-
def _drop_duplicates(items: list[(dict, dict)]):
144-
items = starmap(lambda a, b: (tuple(a.items()), tuple(b.items())), items)
145-
items = set(items)
146-
items = starmap(lambda a, b: (dict(a), dict(b)), items)
147-
items = list(items)
148-
return items
149-
150131
@staticmethod
151-
async def _teardown_database(meta: dict[str, Any], test: dict[str, Any]) -> None:
132+
async def _drop_database(meta: dict[str, Any], test: dict[str, Any]) -> None:
152133
async with aiopg.connect(**meta) as connection:
153134
async with connection.cursor() as cursor:
154135
template = "DROP DATABASE IF EXISTS {database}"

packages/core/minos-microservice-common/tests/test_common/test_database/test_abc.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import unittest
2+
import warnings
23

34
import aiopg
45

56
from minos.common import (
67
DatabaseClientPool,
78
DatabaseMixin,
9+
NotProvidedException,
810
PoolFactory,
11+
PostgreSqlMinosDatabase,
912
)
1013
from minos.common.testing import (
1114
PostgresAsyncTestCase,
@@ -29,6 +32,11 @@ async def test_constructor_with_pool_factory(self):
2932
# noinspection PyUnresolvedReferences
3033
self.assertEqual(pool_factory.get_pool("database"), database.pool)
3134

35+
async def test_constructor_raises(self):
36+
with self.assertRaises(NotProvidedException):
37+
# noinspection PyArgumentEqualDefault
38+
DatabaseMixin(pool=None, pool_factory=None)
39+
3240
def test_database(self):
3341
pool = DatabaseClientPool.from_config(self.config)
3442
database = DatabaseMixin(pool)
@@ -113,5 +121,18 @@ async def test_submit_query_and_iter_locked(self):
113121
self.assertEqual([(3,), (4,), (5,)], observed)
114122

115123

124+
class TestPostgreSqlMinosDatabase(CommonTestCase, PostgresAsyncTestCase):
125+
def test_is_subclass(self):
126+
self.assertTrue(issubclass(PostgreSqlMinosDatabase, DatabaseMixin))
127+
128+
def test_warnings(self):
129+
pool = DatabaseClientPool.from_config(self.config)
130+
131+
with warnings.catch_warnings():
132+
warnings.simplefilter("ignore", DeprecationWarning)
133+
database = PostgreSqlMinosDatabase(pool)
134+
self.assertIsInstance(database, DatabaseMixin)
135+
136+
116137
if __name__ == "__main__":
117138
unittest.main()

packages/core/minos-microservice-common/tests/test_common/test_database/test_locks.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import warnings
23

34
import aiopg
45
from aiopg import (
@@ -8,6 +9,7 @@
89
from minos.common import (
910
DatabaseLock,
1011
Lock,
12+
PostgreSqlLock,
1113
)
1214
from minos.common.testing import (
1315
PostgresAsyncTestCase,
@@ -17,7 +19,7 @@
1719
)
1820

1921

20-
class TestPostgreSqlLock(CommonTestCase, PostgresAsyncTestCase):
22+
class TestDatabaseLock(CommonTestCase, PostgresAsyncTestCase):
2123
def test_base(self):
2224
self.assertTrue(issubclass(DatabaseLock, Lock))
2325

@@ -47,5 +49,18 @@ async def test_cursor(self):
4749
self.assertIsInstance(lock.cursor, Cursor)
4850

4951

52+
class TestPostgreSqlLock(CommonTestCase, PostgresAsyncTestCase):
53+
def test_is_subclass(self):
54+
self.assertTrue(issubclass(PostgreSqlLock, DatabaseLock))
55+
56+
async def test_warnings(self):
57+
wrapped_connection = aiopg.connect(**self.config.get_default_database())
58+
59+
with warnings.catch_warnings():
60+
warnings.simplefilter("ignore", DeprecationWarning)
61+
lock = PostgreSqlLock(wrapped_connection, "foo")
62+
self.assertIsInstance(lock, DatabaseLock)
63+
64+
5065
if __name__ == "__main__":
5166
unittest.main()

packages/core/minos-microservice-common/tests/test_common/test_database/test_pools.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import warnings
23
from unittest.mock import (
34
PropertyMock,
45
patch,
@@ -16,6 +17,8 @@
1617
DatabaseClientPool,
1718
DatabaseLock,
1819
DatabaseLockPool,
20+
PostgreSqlLockPool,
21+
PostgreSqlPool,
1922
)
2023
from minos.common.testing import (
2124
PostgresAsyncTestCase,
@@ -25,7 +28,7 @@
2528
)
2629

2730

28-
class TestPostgreSqlPool(CommonTestCase, PostgresAsyncTestCase):
31+
class TestDatabaseClientPool(CommonTestCase, PostgresAsyncTestCase):
2932
def setUp(self) -> None:
3033
super().setUp()
3134
self.pool = DatabaseClientPool.from_config(self.config)
@@ -80,7 +83,18 @@ def _side_effect(*args, **kwargs):
8083
self.assertIsInstance(connection, Connection)
8184

8285

83-
class TestPostgreSqlLockPool(CommonTestCase, PostgresAsyncTestCase):
86+
class TestPostgreSqlPool(CommonTestCase, PostgresAsyncTestCase):
87+
def test_is_subclass(self):
88+
self.assertTrue(issubclass(PostgreSqlPool, DatabaseClientPool))
89+
90+
def test_warnings(self):
91+
with warnings.catch_warnings():
92+
warnings.simplefilter("ignore", DeprecationWarning)
93+
pool = PostgreSqlPool.from_config(self.config)
94+
self.assertIsInstance(pool, DatabaseClientPool)
95+
96+
97+
class TestDatabaseLockPool(CommonTestCase, PostgresAsyncTestCase):
8498
def setUp(self) -> None:
8599
super().setUp()
86100
self.pool = DatabaseLockPool.from_config(self.config)
@@ -99,5 +113,16 @@ async def test_acquire(self):
99113
self.assertEqual("foo", lock.key)
100114

101115

116+
class TestPostgreSqlLockPool(CommonTestCase, PostgresAsyncTestCase):
117+
def test_is_subclass(self):
118+
self.assertTrue(issubclass(PostgreSqlLockPool, DatabaseLockPool))
119+
120+
def test_warnings(self):
121+
with warnings.catch_warnings():
122+
warnings.simplefilter("ignore", DeprecationWarning)
123+
pool = PostgreSqlLockPool.from_config(self.config)
124+
self.assertIsInstance(pool, DatabaseLockPool)
125+
126+
102127
if __name__ == "__main__":
103128
unittest.main()

packages/core/minos-microservice-common/tests/test_common/test_testing.py

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import unittest
2+
import warnings
23

34
from minos.common import (
45
Config,
56
DependencyInjector,
67
)
78
from minos.common.testing import (
89
MinosTestCase,
10+
PostgresAsyncTestCase,
911
)
1012
from tests.utils import (
1113
CONFIG_FILE_PATH,
1214
)
1315

1416

15-
class MyMinosTestCase(MinosTestCase):
16-
CONFIG_FILE_PATH = CONFIG_FILE_PATH
17-
18-
1917
class TestMinosTestCase(unittest.IsolatedAsyncioTestCase):
2018
def test_config(self):
2119
test_case = MyMinosTestCase()
@@ -28,5 +26,81 @@ def test_injector(self):
2826
self.assertIsInstance(test_case.injector, DependencyInjector)
2927

3028

29+
class MyMinosTestCase(MinosTestCase):
30+
CONFIG_FILE_PATH = CONFIG_FILE_PATH
31+
32+
33+
class TestPostgresAsyncTestCase(unittest.IsolatedAsyncioTestCase):
34+
def test_repository_db(self):
35+
test_case = MyPostgresAsyncTestCase()
36+
test_case.setUp()
37+
with warnings.catch_warnings():
38+
warnings.simplefilter("ignore", DeprecationWarning)
39+
self.assertEqual(
40+
{
41+
k: v
42+
for k, v in test_case.base_config.get_database_by_name("aggregate").items()
43+
if k not in {"database", "user"}
44+
},
45+
{k: v for k, v in test_case.repository_db.items() if k not in {"database", "user"}},
46+
)
47+
self.assertNotEqual(
48+
test_case.base_config.get_database_by_name("aggregate")["database"],
49+
test_case.repository_db["database"],
50+
)
51+
self.assertNotEqual(
52+
test_case.base_config.get_database_by_name("aggregate")["user"],
53+
test_case.repository_db["user"],
54+
)
55+
56+
def test_broker_queue_db(self):
57+
test_case = MyPostgresAsyncTestCase()
58+
test_case.setUp()
59+
with warnings.catch_warnings():
60+
warnings.simplefilter("ignore", DeprecationWarning)
61+
self.assertEqual(
62+
{
63+
k: v
64+
for k, v in test_case.base_config.get_database_by_name("broker").items()
65+
if k not in {"database", "user"}
66+
},
67+
{k: v for k, v in test_case.broker_queue_db.items() if k not in {"database", "user"}},
68+
)
69+
self.assertNotEqual(
70+
test_case.base_config.get_database_by_name("broker")["database"],
71+
test_case.broker_queue_db["database"],
72+
)
73+
self.assertNotEqual(
74+
test_case.base_config.get_database_by_name("broker")["user"],
75+
test_case.broker_queue_db["user"],
76+
)
77+
78+
def test_snapshot_db(self):
79+
test_case = MyPostgresAsyncTestCase()
80+
test_case.setUp()
81+
with warnings.catch_warnings():
82+
warnings.simplefilter("ignore", DeprecationWarning)
83+
self.assertEqual(
84+
{
85+
k: v
86+
for k, v in test_case.base_config.get_database_by_name("aggregate").items()
87+
if k not in {"database", "user"}
88+
},
89+
{k: v for k, v in test_case.snapshot_db.items() if k not in {"database", "user"}},
90+
)
91+
self.assertNotEqual(
92+
test_case.base_config.get_database_by_name("aggregate")["database"],
93+
test_case.broker_queue_db["database"],
94+
)
95+
self.assertNotEqual(
96+
test_case.base_config.get_database_by_name("aggregate")["user"],
97+
test_case.broker_queue_db["user"],
98+
)
99+
100+
101+
class MyPostgresAsyncTestCase(PostgresAsyncTestCase):
102+
CONFIG_FILE_PATH = CONFIG_FILE_PATH
103+
104+
31105
if __name__ == "__main__":
32106
unittest.main()

0 commit comments

Comments
 (0)