feat(rpc): add "by" parameter - keywords

Add "by" parameter: keywords

Signed-off-by: moson-mo <mo-son@mailbox.org>
This commit is contained in:
moson-mo 2022-11-08 15:26:27 +01:00
parent 5484e68b42
commit efd20ed2c7
No known key found for this signature in database
GPG key ID: 4A4760AB4EE15296
3 changed files with 34 additions and 3 deletions

View file

@ -269,7 +269,7 @@ class RPCSearch(PackageSearch):
sanitization done for the PackageSearch `by` argument. sanitization done for the PackageSearch `by` argument.
""" """
keys_removed = ("b", "N", "B", "k", "c", "M") keys_removed = ("b", "N", "B", "c", "M")
def __init__(self) -> "RPCSearch": def __init__(self) -> "RPCSearch":
super().__init__() super().__init__()
@ -372,11 +372,16 @@ class RPCSearch(PackageSearch):
) )
return self return self
def _search_by_groups(self, keywords: str) -> orm.Query: def _search_by_groups(self, keywords: str) -> "RPCSearch":
self._join_groups() self._join_groups()
self.query = self.query.filter(Group.Name == keywords) self.query = self.query.filter(Group.Name == keywords)
return self return self
def _search_by_keywords(self, keywords: str) -> "RPCSearch":
self._join_keywords()
self.query = self.query.filter(PackageKeyword.Keyword == keywords)
return self
def search_by(self, by: str, keywords: str) -> "RPCSearch": def search_by(self, by: str, keywords: str) -> "RPCSearch":
"""Override inherited search_by. In this override, we reduce the """Override inherited search_by. In this override, we reduce the
scope of what we handle within this function. We do not set `by` scope of what we handle within this function. We do not set `by`

View file

@ -88,10 +88,17 @@ class RPC:
"replaces", "replaces",
"groups", "groups",
"submitter", "submitter",
"keywords",
} }
# A mapping of by aliases. # A mapping of by aliases.
BY_ALIASES = {"name-desc": "nd", "name": "n", "maintainer": "m", "submitter": "s"} BY_ALIASES = {
"name-desc": "nd",
"name": "n",
"maintainer": "m",
"submitter": "s",
"keywords": "k",
}
def __init__(self, version: int = 0, type: str = None) -> "RPC": def __init__(self, version: int = 0, type: str = None) -> "RPC":
self.version = version self.version = version

View file

@ -938,6 +938,25 @@ def test_rpc_search_submitter(client: TestClient, user2: User, packages: list[Pa
assert data.get("resultcount") == 0 assert data.get("resultcount") == 0
def test_rpc_search_keywords(client: TestClient, packages: list[Package]):
params = {"v": 5, "type": "search", "by": "keywords", "arg": "big-chungus"}
with client as request:
response = request.get("/rpc", params=params)
data = response.json()
# should get 2 packages
assert data.get("resultcount") == 1
names = list(sorted(r.get("Name") for r in data.get("results")))
expected_results = ["big-chungus"]
assert names == expected_results
# non-existent search
params["arg"] = "blah-blah"
response = request.get("/rpc", params=params)
data = response.json()
assert data.get("resultcount") == 0
def test_rpc_incorrect_by(client: TestClient): def test_rpc_incorrect_by(client: TestClient):
params = {"v": 5, "type": "search", "by": "fake", "arg": "big"} params = {"v": 5, "type": "search", "by": "fake", "arg": "big"}
with client as request: with client as request: