Skip to content

Commit

Permalink
Paginate merge endpoint to return paginated jobs by state
Browse files Browse the repository at this point in the history
  • Loading branch information
dfitchett committed Jan 2, 2024
1 parent 6bc1438 commit f083ad9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion domain-ee/ee-ep-merge-app/src/python_src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async def get_merge_request_by_job_id(job_id: UUID):
async def get_merge_jobs(state: Annotated[list[JobState], Query()] = JobState.incomplete(),
page: int = 1,
size: int = 10):
jobs, total = job_store.query(state, page, size)
jobs, total = job_store.query(states=state, offset=page, limit=size)
logging.info(f"event=getMergeJobs "
f"total={total} "
f"page={sanitize(page)} "
Expand Down
5 changes: 3 additions & 2 deletions domain-ee/ee-ep-merge-app/src/python_src/service/job_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ def init(self) -> list[MergeJob]:
def clear(self):
self.db.clear(MergeJob)

def query(self, states: list[schema.JobState], offset, limit) -> list[MergeJob]:
def query(self, states: list[schema.JobState] = schema.JobState.incomplete(), offset: int = 1, limit: int = 10) -> \
list[MergeJob]:
return self.db.query(MergeJob,
MergeJob.state.in_(states),
MergeJob.updated_at,
MergeJob.state.in_(states) if states else True,
offset,
limit)

Expand Down
43 changes: 43 additions & 0 deletions domain-ee/ee-ep-merge-app/tests/service/test_job_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pytest
from model.merge_job import MergeJob
from schema.merge_job import JobState
from src.python_src.service.job_store import JobStore

DEFAULT_STATES = JobState.incomplete()
DEFAULT_OFFSET = 1
DEFAULT_LIMIT = 10


def test_init(db):
job_store = JobStore(db)
Expand Down Expand Up @@ -48,3 +54,40 @@ def test_update_merge_job(db, merge_job):
assert updated_merge_job.ep400_claim_id == merge_job.ep400_claim_id
assert updated_merge_job.state == merge_job.state
db.update.assert_called_once()


@pytest.mark.parametrize("states,offset,limit",
[
pytest.param(None, None, None, id="defaults"),
pytest.param([JobState.PENDING], 1, None, id="first page, default 10 items"),
pytest.param([JobState.PENDING], None, 1, id="first page, default offset, 1 item"),
pytest.param([JobState.PENDING], 1, 10, id="last page, 1 item"),
pytest.param([JobState.PENDING], 1, 2, id="first page, 2 items"),
pytest.param([JobState.PENDING], 6, 2, id="last page, 2 items"),
])
def test_query(db, merge_job, states: list, offset: int, limit: int):
job_store = JobStore(db)
kwargs = {}
if states:
kwargs["states"] = states
if offset:
kwargs['offset'] = offset
if limit:
kwargs['limit'] = limit

job_store.query(**kwargs)

query_args = db.query.call_args[0]
actual_model = query_args[0]
actual_order_by = query_args[1]
actual_filter = query_args[2]
actual_offset = query_args[3]
actual_limit = query_args[4]

assert actual_model == MergeJob
assert actual_order_by == MergeJob.updated_at
assert actual_filter.compare(MergeJob.state.in_(states if states else DEFAULT_STATES))
assert actual_offset == offset if offset else DEFAULT_OFFSET
assert actual_limit == limit if limit else DEFAULT_LIMIT

db.query.assert_called_once()

0 comments on commit f083ad9

Please sign in to comment.