Skip to content

Commit

Permalink
add integration test for requests/latest endpoint
Browse files Browse the repository at this point in the history
Generate duplicate requests for a repo/ref and query the requests/latest
API endpoint to determine the "latest". The latest request should always
be the last of the duplicate requests for each repo+ref combination

Signed-off-by: Taylor Madore <[email protected]>
  • Loading branch information
taylormadore committed Dec 5, 2023
1 parent 3e1f32d commit d6aef93
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/integration/test_get_latest_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Any

from cachito.common.utils import get_repo_name

from . import utils


def test_get_latest_request(api_client: utils.Client, test_env: dict[str, Any]) -> None:
"""
Generates requests and ensures that the requests/latest endpoint returns the most recent one.
For each package in "various_packages", a request and a duplicate request are generated.
After all requests have been completed for all packages, the requests/latest endpoint is
queried for each repo_name/ref combination. The request_id of the final duplicate request
for each package should match what is returned by the latest endpoint.
"""

def get_repo_name_and_ref_from_package(package: dict[str, str]) -> tuple[Any, str]:
return get_repo_name(package["repo"]), package["ref"]

latest_request_ids = {}
repeat_count = 2

# Generate the requests
for pkg_manager, package in test_env["various_packages"].items():
repo_name, ref = get_repo_name_and_ref_from_package(package)
for _ in range(repeat_count):
initial_response = api_client.create_new_request(
payload={
"repo": package["repo"],
"ref": package["ref"],
"pkg_managers": [pkg_manager],
},
)
completed_response = api_client.wait_for_complete_request(initial_response)
utils.assert_properly_completed_response(completed_response)
latest_request_ids[(repo_name, ref)] = completed_response.data["id"]

# Check that the latest is the latest
for package in test_env["various_packages"].values():
repo_name, ref = get_repo_name_and_ref_from_package(package)
latest_request = api_client.fetch_latest_request(repo_name=repo_name, ref=ref).json()

assert {
"id",
"repo",
"ref",
"updated",
}.issubset(latest_request)
assert latest_request_ids[(repo_name, ref)] == latest_request["id"]
12 changes: 12 additions & 0 deletions tests/integration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ def fetch_all_requests(self, query_params=None, all_pages=True):

return Response({"items": all_items}, None, resp.status_code)

def fetch_latest_request(self, **params) -> requests.Response:
"""
Fetch the latest request for a repo/ref from the Cachito API.
:param dict query_params: Request parameters and values (repo_name, ref)
:return: Object that contains response from the Cachito API
:rtype: Response
"""
resp = self.requests_session.get(f"{self._cachito_api_url}/requests/latest", params=params)
resp.raise_for_status()
return resp

def fetch_content_manifest(self, request_id):
"""
Fetch a contest manifest by request_id from the Cachito API.
Expand Down

0 comments on commit d6aef93

Please sign in to comment.