fix(fastapi): support by maintainer search with no keywords

In this case, package search should return orphaned packages.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-30 16:22:54 -07:00
parent a38e126f49
commit c28f1695ed
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
2 changed files with 30 additions and 3 deletions

View file

@ -90,9 +90,13 @@ class PackageSearch:
return self return self
def _search_by_maintainer(self, keywords: str) -> orm.Query: def _search_by_maintainer(self, keywords: str) -> orm.Query:
self.query = self.query.join( if keywords:
models.User, models.User.ID == models.PackageBase.MaintainerUID self.query = self.query.join(
).filter(models.User.Username == keywords) models.User, models.User.ID == models.PackageBase.MaintainerUID
).filter(models.User.Username == keywords)
else:
self.query = self.query.filter(
models.PackageBase.MaintainerUID.is_(None))
return self return self
def _search_by_comaintainer(self, keywords: str) -> orm.Query: def _search_by_comaintainer(self, keywords: str) -> orm.Query:

View file

@ -623,13 +623,36 @@ def test_packages_search_by_keywords(client: TestClient,
def test_packages_search_by_maintainer(client: TestClient, def test_packages_search_by_maintainer(client: TestClient,
maintainer: User, maintainer: User,
package: Package): package: Package):
# We should expect that searching by `package`'s maintainer
# returns `package` in the results.
with client as request: with client as request:
response = request.get("/packages", params={ response = request.get("/packages", params={
"SeB": "m", "SeB": "m",
"K": maintainer.Username "K": maintainer.Username
}) })
assert response.status_code == int(HTTPStatus.OK) assert response.status_code == int(HTTPStatus.OK)
root = parse_root(response.text)
rows = root.xpath('//table[@class="results"]/tbody/tr')
assert len(rows) == 1
# Search again by maintainer with no keywords given.
# This kind of search returns all orphans instead.
# In this first case, there are no orphan packages; assert that.
with client as request:
response = request.get("/packages", params={"SeB": "m"})
assert response.status_code == int(HTTPStatus.OK)
root = parse_root(response.text)
rows = root.xpath('//table[@class="results"]/tbody/tr')
assert len(rows) == 0
# Orphan `package`.
with db.begin():
package.PackageBase.Maintainer = None
# This time, we should get `package` returned, since it's now an orphan.
with client as request:
response = request.get("/packages", params={"SeB": "m"})
assert response.status_code == int(HTTPStatus.OK)
root = parse_root(response.text) root = parse_root(response.text)
rows = root.xpath('//table[@class="results"]/tbody/tr') rows = root.xpath('//table[@class="results"]/tbody/tr')
assert len(rows) == 1 assert len(rows) == 1