Skip to content

Commit 57ad684

Browse files
author
Sergio García Prado
committed
Merge remote-tracking branch 'origin/0.7.x' into 0.8.0
# Conflicts: # packages/core/minos-microservice-aggregate/minos/aggregate/__init__.py # packages/core/minos-microservice-aggregate/minos/aggregate/testing/snapshots/repositories/testcases.py # packages/core/minos-microservice-aggregate/poetry.lock # packages/core/minos-microservice-aggregate/pyproject.toml # packages/core/minos-microservice-common/minos/common/__init__.py # packages/core/minos-microservice-common/pyproject.toml # packages/core/minos-microservice-networks/minos/networks/__init__.py # packages/core/minos-microservice-networks/poetry.lock # packages/core/minos-microservice-networks/pyproject.toml # packages/plugins/minos-database-aiopg/minos/plugins/aiopg/__init__.py # packages/plugins/minos-database-aiopg/poetry.lock # packages/plugins/minos-database-aiopg/pyproject.toml
2 parents 6072046 + 1b33fee commit 57ad684

File tree

15 files changed

+259
-109
lines changed

15 files changed

+259
-109
lines changed

packages/core/minos-microservice-aggregate/minos/aggregate/queries.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ def _evaluate(self, value: Model) -> bool:
146146
return self._get_field(value) in self.parameter
147147

148148

149+
class _ContainsCondition(_SimpleCondition):
150+
def _evaluate(self, value: Model) -> bool:
151+
return self.parameter in self._get_field(value)
152+
153+
149154
class _LikeCondition(_SimpleCondition):
150155
def _evaluate(self, value: Model) -> bool:
151156
return bool(self._pattern.fullmatch(self._get_field(value)))
@@ -205,6 +210,7 @@ class Condition:
205210
EQUAL = _EqualCondition
206211
NOT_EQUAL = _NotEqualCondition
207212
IN = _InCondition
213+
CONTAINS = _ContainsCondition
208214
LIKE = _LikeCondition
209215

210216

packages/core/minos-microservice-aggregate/minos/aggregate/testing/snapshots/repositories/testcases.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ class SnapshotRepositoryTestCase(MinosTestCase, ABC):
5252

5353
snapshot_repository: SnapshotRepository
5454

55+
class NumbersList(Entity):
56+
"""For testing purposes"""
57+
58+
numbers: list[int]
59+
60+
class Number(Entity):
61+
"""For testing purposes"""
62+
63+
value: int
64+
5565
class Owner(Entity):
5666
"""For testing purposes"""
5767

@@ -380,6 +390,52 @@ async def test_find_by_uuid(self):
380390
]
381391
self.assertEqual(expected, observed)
382392

393+
async def test_find_contains(self):
394+
a = FieldDiffContainer([FieldDiff("numbers", list[int], [1, 2, 3])])
395+
b = FieldDiffContainer([FieldDiff("numbers", list[int], [4, 5, 6])])
396+
c = FieldDiffContainer([FieldDiff("numbers", list[int], [3, 8, 9])])
397+
await self.delta_repository.create(
398+
DeltaEntry(name=self.NumbersList.classname, data=a.avro_bytes, uuid=self.uuid_1)
399+
)
400+
await self.delta_repository.create(
401+
DeltaEntry(name=self.NumbersList.classname, data=b.avro_bytes, uuid=self.uuid_2)
402+
)
403+
await self.delta_repository.create(
404+
DeltaEntry(name=self.NumbersList.classname, data=c.avro_bytes, uuid=self.uuid_3)
405+
)
406+
await self.synchronize()
407+
408+
condition = Condition.CONTAINS("numbers", 3)
409+
410+
iterable = self.snapshot_repository.find(self.NumbersList, condition, ordering=Ordering.ASC("updated_at"))
411+
observed = [v async for v in iterable]
412+
413+
expected = [
414+
await self.snapshot_repository.get(self.NumbersList, self.uuid_1),
415+
await self.snapshot_repository.get(self.NumbersList, self.uuid_3),
416+
]
417+
self.assertEqual(expected, observed)
418+
419+
async def test_find_equal(self):
420+
a = FieldDiffContainer([FieldDiff("value", int, 1)])
421+
b = FieldDiffContainer([FieldDiff("value", int, 2)])
422+
c = FieldDiffContainer([FieldDiff("value", int, 1)])
423+
await self.delta_repository.create(DeltaEntry(name=self.Number.classname, data=a.avro_bytes, uuid=self.uuid_1))
424+
await self.delta_repository.create(DeltaEntry(name=self.Number.classname, data=b.avro_bytes, uuid=self.uuid_2))
425+
await self.delta_repository.create(DeltaEntry(name=self.Number.classname, data=c.avro_bytes, uuid=self.uuid_3))
426+
await self.synchronize()
427+
428+
condition = Condition.EQUAL("value", 1)
429+
430+
iterable = self.snapshot_repository.find(self.Number, condition, ordering=Ordering.ASC("updated_at"))
431+
observed = [v async for v in iterable]
432+
433+
expected = [
434+
await self.snapshot_repository.get(self.Number, self.uuid_1),
435+
await self.snapshot_repository.get(self.Number, self.uuid_3),
436+
]
437+
self.assertEqual(expected, observed)
438+
383439
async def test_find_with_transaction(self):
384440
await self.populate_and_synchronize()
385441
condition = Condition.IN("uuid", [self.uuid_2, self.uuid_3])

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class _Text(DeclarativeModel):
2121
value: str
2222

2323

24+
class _ListInt(DeclarativeModel):
25+
value: list[int]
26+
27+
2428
class TestCondition(unittest.TestCase):
2529
def test_hash(self):
2630
self.assertIsInstance(hash(Condition.EQUAL("value", 3)), int)
@@ -126,6 +130,13 @@ def test_condition_in(self):
126130
self.assertFalse(condition.evaluate(_Number(42)))
127131
self.assertTrue(condition.evaluate(_Number(56)))
128132

133+
def test_condition_contains(self):
134+
condition = Condition.CONTAINS("value", 1)
135+
self.assertEqual("_ContainsCondition('value', 1)", repr(condition))
136+
137+
self.assertFalse(condition.evaluate(_ListInt([42, 3, -5])))
138+
self.assertTrue(condition.evaluate(_ListInt([1, 2, 3])))
139+
129140
def test_condition_like(self):
130141
condition = Condition.LIKE("value", "a%[^ou]")
131142
self.assertEqual("_LikeCondition('value', 'a%[^ou]')", repr(condition))

packages/core/minos-microservice-aggregate/tests/test_aggregate/test_snapshots/test_repositories/test_database.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,44 @@ async def test_find_by_uuid(self):
330330
):
331331
await super().test_find_by_uuid()
332332

333+
async def test_find_contains(self):
334+
entities = [
335+
SnapshotRepositoryTestCase.NumbersList([1, 2, 3], uuid=self.uuid_2),
336+
SnapshotRepositoryTestCase.NumbersList([3, 8, 9], uuid=self.uuid_3),
337+
]
338+
with patch.object(DatabaseClient, "fetch_one", return_value=(9999,)):
339+
with patch.object(
340+
DatabaseClient,
341+
"fetch_all",
342+
side_effect=[
343+
FakeAsyncIterator(
344+
[tuple(SnapshotEntry.from_entity(entity).as_raw().values()) for entity in entities]
345+
),
346+
FakeAsyncIterator([tuple(SnapshotEntry.from_entity(entities[0]).as_raw().values())]),
347+
FakeAsyncIterator([tuple(SnapshotEntry.from_entity(entities[1]).as_raw().values())]),
348+
],
349+
):
350+
await super().test_find_contains()
351+
352+
async def test_find_equal(self):
353+
entities = [
354+
SnapshotRepositoryTestCase.Number(1, uuid=self.uuid_2),
355+
SnapshotRepositoryTestCase.Number(1, uuid=self.uuid_3),
356+
]
357+
with patch.object(DatabaseClient, "fetch_one", return_value=(9999,)):
358+
with patch.object(
359+
DatabaseClient,
360+
"fetch_all",
361+
side_effect=[
362+
FakeAsyncIterator(
363+
[tuple(SnapshotEntry.from_entity(entity).as_raw().values()) for entity in entities]
364+
),
365+
FakeAsyncIterator([tuple(SnapshotEntry.from_entity(entities[0]).as_raw().values())]),
366+
FakeAsyncIterator([tuple(SnapshotEntry.from_entity(entities[1]).as_raw().values())]),
367+
],
368+
):
369+
await super().test_find_equal()
370+
333371
async def test_find_with_transaction(self):
334372
entities = [
335373
SnapshotRepositoryTestCase.Car(3, "blue", uuid=self.uuid_2, version=4),

0 commit comments

Comments
 (0)