Verified Commit efd20ed2 authored by Mario Oenning's avatar Mario Oenning
Browse files

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



Add "by" parameter: keywords

Signed-off-by: Mario Oenning's avatarmoson-mo <mo-son@mailbox.org>
parent 5484e68b
......@@ -269,7 +269,7 @@ class RPCSearch(PackageSearch):
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":
super().__init__()
......@@ -372,11 +372,16 @@ class RPCSearch(PackageSearch):
)
return self
def _search_by_groups(self, keywords: str) -> orm.Query:
def _search_by_groups(self, keywords: str) -> "RPCSearch":
self._join_groups()
self.query = self.query.filter(Group.Name == keywords)
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":
"""Override inherited search_by. In this override, we reduce the
scope of what we handle within this function. We do not set `by`
......
......@@ -88,10 +88,17 @@ class RPC:
"replaces",
"groups",
"submitter",
"keywords",
}
# 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":
self.version = version
......
......@@ -938,6 +938,25 @@ def test_rpc_search_submitter(client: TestClient, user2: User, packages: list[Pa
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):
params = {"v": 5, "type": "search", "by": "fake", "arg": "big"}
with client as request:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment