Skip to content

Commit

Permalink
Implement batch queries & update
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Dec 11, 2024
1 parent fa677bc commit 8d636a4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
3 changes: 2 additions & 1 deletion ebl/dossiers/application/dossiers_repository.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Sequence
from abc import ABC, abstractmethod

from ebl.dossiers.domain.dossier_record import (
Expand All @@ -7,7 +8,7 @@

class DossiersRepository(ABC):
@abstractmethod
def fetch(self, id: str) -> DossierRecord: ...
def query_by_ids(self, ids: Sequence[str]) -> DossierRecord: ...

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.

@abstractmethod
def create(self, dossier_record: DossierRecord) -> str: ...

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.
14 changes: 6 additions & 8 deletions ebl/dossiers/infrastructure/mongo_dossiers_repository.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Sequence
from marshmallow import Schema, fields, post_load, EXCLUDE
from pymongo.database import Database
from ebl.mongo_collection import MongoCollection
Expand Down Expand Up @@ -48,14 +49,11 @@ def make_record(self, data, **kwargs):

class MongoDossiersRepository(DossiersRepository):
def __init__(self, database: Database):
self._dossier = MongoCollection(database, COLLECTION)
self._collection = MongoCollection(database, COLLECTION)

def fetch(self, id: str) -> DossierRecord:
query = {"_id": id}
record_data = self._dossier.find_one(query)
if not record_data:
raise ValueError(f"No dossier record found for the id: {id}")
return DossierRecordSchema().load(record_data)
def query_by_ids(self, ids: Sequence[str]) -> Sequence[DossierRecord]:
cursor = self._collection.find_many({"_id": {"$in": ids}})
return DossierRecordSchema(many=True).load(cursor)

def create(self, dossier_record: DossierRecord) -> str:
return self._dossier.insert_one(DossierRecordSchema().dump(dossier_record))
return self._collection.insert_one(DossierRecordSchema().dump(dossier_record))
5 changes: 3 additions & 2 deletions ebl/dossiers/web/dossier_records.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from falcon import Request, Response
from ebl.errors import NotFoundError
from marshmallow import EXCLUDE

from ebl.dossiers.application.dossiers_repository import DossiersRepository
from ebl.dossiers.infrastructure.mongo_dossiers_repository import (
Expand All @@ -13,9 +14,9 @@ def __init__(self, _dossiersRepository: DossiersRepository):

def on_get(self, req: Request, resp: Response) -> None:
try:
dossiers = self._dossiersRepository.fetch(req.params["id"])
dossiers = self._dossiersRepository.query_by_ids(req.params["ids"].split(","))
except ValueError as error:
raise NotFoundError(
f"No dossier records matching {str(req.params)} found."
) from error
resp.media = DossierRecordSchema().dump([dossiers], many=True)
resp.media = DossierRecordSchema(unknown=EXCLUDE, many=True).dump(dossiers)
28 changes: 24 additions & 4 deletions ebl/tests/dossiers/test_dossiers_route.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import falcon
import pytest

from ebl.dossiers.domain.dossier_record import DossierRecord
from ebl.tests.factories.dossier import (
DossierRecordFactory,
Expand All @@ -18,11 +17,32 @@ def dossier_record() -> DossierRecord:
return DossierRecordFactory.build()


@pytest.fixture
def another_dossier_record() -> DossierRecord:
return DossierRecordFactory.build()


@pytest.fixture
def unrelated_dossier_record() -> DossierRecord:
return DossierRecordFactory.build()


def test_fetch_dossier_record_route(
dossier_record, dossiers_repository: DossiersRepository, client
dossier_record,
another_dossier_record,
unrelated_dossier_record,
dossiers_repository: DossiersRepository,
client,
) -> None:
dossiers_repository.create(dossier_record)
get_result = client.simulate_get("/dossiers", params={"id": dossier_record.id})
dossiers_repository.create(another_dossier_record)
dossiers_repository.create(unrelated_dossier_record)
get_result = client.simulate_get(
"/dossiers",
params={"ids": ",".join([dossier_record.id, another_dossier_record.id])},
)

assert get_result.status == falcon.HTTP_OK
assert get_result.json[0] == DossierRecordSchema().dump(dossier_record)
assert sorted(get_result.json, key=lambda r: r["_id"]) == DossierRecordSchema(
many=True
).dump(sorted([dossier_record, another_dossier_record], key=lambda r: r.id))

0 comments on commit 8d636a4

Please sign in to comment.