Add more tests for db.search

This commit is contained in:
Stéphane Bidoul 2021-11-20 14:45:42 +01:00
parent f173688935
commit 27afab461d
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92
2 changed files with 20 additions and 10 deletions

View file

@ -13,9 +13,6 @@ class BuildListener(Protocol):
... ...
NoPr = 0
class BuildsDb: class BuildsDb:
"""An in-memory database of builds. """An in-memory database of builds.
@ -213,9 +210,6 @@ class BuildsDb:
params.append(branch) params.append(branch)
where.append("pr IS NULL") where.append("pr IS NULL")
if pr is not None: if pr is not None:
if pr == NoPr:
where.append("pr IS NULL")
else:
where.append("pr=?") where.append("pr=?")
params.append(pr) params.append(pr)
if name: if name:
@ -223,7 +217,10 @@ class BuildsDb:
params.append(name) params.append(name)
if where: if where:
query += "WHERE " + " AND ".join(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() rows = self._con.execute(query, params).fetchall()
for row in rows: for row in rows:
yield self._build_from_row(row) yield self._build_from_row(row)

View file

@ -11,6 +11,7 @@ def _make_build(
status: BuildStatus | None = None, status: BuildStatus | None = None,
init_status: BuildInitStatus | None = None, init_status: BuildInitStatus | None = None,
repo: str | None = None, repo: str | None = None,
target_branch: str | None = None,
pr: int | None = None, pr: int | None = None,
) -> Build: ) -> Build:
name = name or "build-a" name = name or "build-a"
@ -18,7 +19,7 @@ def _make_build(
name=name, name=name,
deployment_name=name + "-odoo", deployment_name=name + "-odoo",
repo=repo or "oca/mis-builder", repo=repo or "oca/mis-builder",
target_branch="15.0", target_branch=target_branch or "15.0",
pr=pr or None, pr=pr or None,
git_commit="0d35a10f161b410f2baa3d416a338d191b6dabc0", git_commit="0d35a10f161b410f2baa3d416a338d191b6dabc0",
image="ghcr.io/oca/oca-ci:py3.8-odoo15.0", 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] 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: def test_count_by_status() -> None:
db = BuildsDb() db = BuildsDb()
db.add(_make_build(name="b1", status=BuildStatus.started)) db.add(_make_build(name="b1", status=BuildStatus.started))