From 27afab461df39ab20bb15b828f48083e12121d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sat, 20 Nov 2021 14:45:42 +0100 Subject: [PATCH] Add more tests for db.search --- src/runboat/db.py | 15 ++++++--------- tests/test_db.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/runboat/db.py b/src/runboat/db.py index b1331d2..a0d4edf 100644 --- a/src/runboat/db.py +++ b/src/runboat/db.py @@ -13,9 +13,6 @@ class BuildListener(Protocol): ... -NoPr = 0 - - class BuildsDb: """An in-memory database of builds. @@ -213,17 +210,17 @@ class BuildsDb: params.append(branch) where.append("pr IS NULL") if pr is not None: - if pr == NoPr: - where.append("pr IS NULL") - else: - where.append("pr=?") - params.append(pr) + where.append("pr=?") + params.append(pr) if name: where.append("name=?") params.append(name) if where: query += "WHERE " + " AND ".join(where) - query += "ORDER BY repo, target_branch, pr, created DESC" + query += ( + " ORDER BY" + " repo, target_branch DESC, COALESCE(pr, 999999) DESC, created DESC" + ) rows = self._con.execute(query, params).fetchall() for row in rows: yield self._build_from_row(row) diff --git a/tests/test_db.py b/tests/test_db.py index e59e5d4..2a0d89e 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -11,6 +11,7 @@ def _make_build( status: BuildStatus | None = None, init_status: BuildInitStatus | None = None, repo: str | None = None, + target_branch: str | None = None, pr: int | None = None, ) -> Build: name = name or "build-a" @@ -18,7 +19,7 @@ def _make_build( name=name, deployment_name=name + "-odoo", repo=repo or "oca/mis-builder", - target_branch="15.0", + target_branch=target_branch or "15.0", pr=pr or None, git_commit="0d35a10f161b410f2baa3d416a338d191b6dabc0", image="ghcr.io/oca/oca-ci:py3.8-odoo15.0", @@ -81,6 +82,18 @@ def test_search() -> None: assert list(db.search("oca/repo1")) == [build1] +def test_search_by_branch_and_pr() -> None: + db = BuildsDb() + db.add(build1 := _make_build(name="b1", target_branch="15.0", pr=None)) + db.add(build2 := _make_build(name="b2", target_branch="15.0", pr=1)) + # Searching on branch returns build for branch (and not pull requests). + assert list(db.search(branch="15.0")) == [build1] + # Searching on target_branch returns builds for branch and pull requests to branch. + assert list(db.search(target_branch="15.0")) == [build1, build2] + # Search on pr. + assert list(db.search(pr=1)) == [build2] + + def test_count_by_status() -> None: db = BuildsDb() db.add(_make_build(name="b1", status=BuildStatus.started))