Skip to content

Commit 9c56c0c

Browse files
author
Sergio García Prado
committed
ISSUE #471
* Fix bug related with `Conditions.CONTAINS` and `aiopg` * Fix minor bugs.
1 parent 7f60295 commit 9c56c0c

File tree

4 files changed

+97
-3
lines changed
  • packages
    • core/minos-microservice-aggregate
    • plugins/minos-database-aiopg
      • minos/plugins/aiopg/factories/aggregate/snapshots
      • tests/test_aiopg/test_factories/test_aggregate/test_snapshots

4 files changed

+97
-3
lines changed

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
@@ -50,6 +50,16 @@ class SnapshotRepositoryTestCase(MinosTestCase, ABC):
5050

5151
snapshot_repository: SnapshotRepository
5252

53+
class NumbersList(RootEntity):
54+
"""For testing purposes"""
55+
56+
numbers: list[int]
57+
58+
class Number(RootEntity):
59+
"""For testing purposes"""
60+
61+
value: int
62+
5363
class Owner(RootEntity):
5464
"""For testing purposes"""
5565

@@ -386,6 +396,52 @@ async def test_find_by_uuid(self):
386396
]
387397
self.assertEqual(expected, observed)
388398

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

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_root_entity(entity).as_raw().values()) for entity in entities]
345+
),
346+
FakeAsyncIterator([tuple(SnapshotEntry.from_root_entity(entities[0]).as_raw().values())]),
347+
FakeAsyncIterator([tuple(SnapshotEntry.from_root_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_root_entity(entity).as_raw().values()) for entity in entities]
364+
),
365+
FakeAsyncIterator([tuple(SnapshotEntry.from_root_entity(entities[0]).as_raw().values())]),
366+
FakeAsyncIterator([tuple(SnapshotEntry.from_root_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),

packages/plugins/minos-database-aiopg/minos/plugins/aiopg/factories/aggregate/snapshots/queries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ def _build_condition_contains(self, condition: _ContainsCondition) -> Composable
216216
raise ValueError(f"Cannot use 'contains' over non-list field '{field}'")
217217
else:
218218
name = self.generate_random_str()
219-
self._parameters[name] = parameter
219+
self._parameters[name] = Json(parameter)
220220

221221
field = Literal("{{{}}}".format(field.replace(".", ",")))
222222
name = Placeholder(name)
223-
return SQL("(data#>{name} IN {field}::jsonb)").format(field=field, name=name)
223+
return SQL("(data#>{field} @> {name}::jsonb)").format(field=field, name=name)
224224

225225
def _build_ordering(self, ordering: _Ordering) -> Composable:
226226
field = ordering.by

packages/plugins/minos-database-aiopg/tests/test_aiopg/test_factories/test_aggregate/test_snapshots/test_queries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ async def test_build_contains(self):
283283
with patch.object(AiopgSnapshotQueryDatabaseOperationBuilder, "generate_random_str", side_effect=["hello"]):
284284
observed = AiopgSnapshotQueryDatabaseOperationBuilder(self.classname, condition).build()
285285

286-
expected_query = SQL(" WHERE ").join([self.base_select, SQL("(data#>%(hello)s IN '{numbers}'::jsonb)")])
286+
expected_query = SQL(" WHERE ").join([self.base_select, SQL("(data#>'{numbers}' @> %(hello)s::jsonb)")])
287287
expected_parameters = {"hello": 1} | self.base_parameters
288288

289289
self.assertEqual(await self._flatten_query(expected_query), await self._flatten_query(observed[0]))

0 commit comments

Comments
 (0)