diff --git a/aurweb/packages/search.py b/aurweb/packages/search.py index c0740cda883254858888723547d4779c1ba56383..d5e00110f5d86ca108e26d0a8e48539ebcf2e36d 100644 --- a/aurweb/packages/search.py +++ b/aurweb/packages/search.py @@ -136,7 +136,10 @@ class PackageSearch: self._join_user() self._join_keywords() keywords = set(k.lower() for k in keywords) - self.query = self.query.filter(PackageKeyword.Keyword.in_(keywords)) + self.query = self.query.filter(PackageKeyword.Keyword.in_(keywords)).group_by( + models.Package.Name + ) + return self def _search_by_maintainer(self, keywords: str) -> orm.Query: diff --git a/aurweb/routers/packages.py b/aurweb/routers/packages.py index a4aac4966b1a2424bff4bf98c90481e6783a3fe1..6a943dbfb52b6eb7443f838a07f3ebe290fefd34 100644 --- a/aurweb/routers/packages.py +++ b/aurweb/routers/packages.py @@ -93,22 +93,18 @@ async def packages_get( search.sort_by(sort_by, sort_order) # Insert search results into the context. - results = ( - search.results() - .with_entities( - models.Package.ID, - models.Package.Name, - models.Package.PackageBaseID, - models.Package.Version, - models.Package.Description, - models.PackageBase.Popularity, - models.PackageBase.NumVotes, - models.PackageBase.OutOfDateTS, - models.User.Username.label("Maintainer"), - models.PackageVote.PackageBaseID.label("Voted"), - models.PackageNotification.PackageBaseID.label("Notify"), - ) - .group_by(models.Package.Name) + results = search.results().with_entities( + models.Package.ID, + models.Package.Name, + models.Package.PackageBaseID, + models.Package.Version, + models.Package.Description, + models.PackageBase.Popularity, + models.PackageBase.NumVotes, + models.PackageBase.OutOfDateTS, + models.User.Username.label("Maintainer"), + models.PackageVote.PackageBaseID.label("Voted"), + models.PackageNotification.PackageBaseID.label("Notify"), ) packages = results.limit(per_page).offset(offset) diff --git a/test/test_packages_routes.py b/test/test_packages_routes.py index bf17996399529bf06749b770d1a979253f4d7b35..f9cea694b58f77446d0eb22ce1d42a0f6e029b1b 100644 --- a/test/test_packages_routes.py +++ b/test/test_packages_routes.py @@ -740,6 +740,23 @@ def test_packages_search_by_keywords(client: TestClient, packages: list[Package] rows = root.xpath('//table[@class="results"]/tbody/tr') assert len(rows) == 1 + # Now let's add another keyword to the same package + with db.begin(): + db.create( + PackageKeyword, PackageBase=package.PackageBase, Keyword="testKeyword2" + ) + + # And request packages with both keywords, we should still get 1 result. + with client as request: + response = request.get( + "/packages", params={"SeB": "k", "K": "testKeyword testKeyword2"} + ) + assert response.status_code == int(HTTPStatus.OK) + + root = parse_root(response.text) + rows = root.xpath('//table[@class="results"]/tbody/tr') + assert len(rows) == 1 + def test_packages_search_by_maintainer( client: TestClient, maintainer: User, package: Package