From 5d1b1d4bf3621ed61241acdbd6e81978c8f04d30 Mon Sep 17 00:00:00 2001 From: Gabriel Schowe Date: Mon, 23 Sep 2024 09:06:12 +0200 Subject: [PATCH] Add: working unit tests, Refractor --- pontos/github/api/packages.py | 121 ++++++++++++++---------------- tests/github/api/test_packages.py | 23 +++++- 2 files changed, 76 insertions(+), 68 deletions(-) diff --git a/pontos/github/api/packages.py b/pontos/github/api/packages.py index 283e46722..f561f4074 100644 --- a/pontos/github/api/packages.py +++ b/pontos/github/api/packages.py @@ -35,45 +35,6 @@ async def exists( response = await self._client.get(api) return response.is_success - async def packages( - self, organization: str, package_type: str - ) -> AsyncIterator[Package]: - """ - Get information about organization packages - - https://docs.github.com/en/rest/reference/packages#list-packages-for-an-organization - - Args: - organization: GitHub organization to use - package_type: Type of the packages to list - - - Raises: - `httpx.HTTPStatusError`: If there was an error in the request - - Returns: - An async iterator yielding packages information - - Example: - .. code-block:: python - - from pontos.github.api import GitHubAsyncRESTApi - - async with GitHubAsyncRESTApi(token) as api: - async for package in api.packages.packages( - organization="foo", - package_type="container", - ): - print(package) - """ - api = f"/orgs/{organization}/packages/{package_type}" - params = {"per_page": "100"} - async for response in self._client.get_all(api, params=params): - response.raise_for_status() - - for package in response.json(): - yield Package.from_dict(package) - async def package( self, organization: str, package_type: PackageType, package_name: str ) -> Package: @@ -113,26 +74,24 @@ async def package( response.raise_for_status() return Package.from_dict(response.json()) - async def package_versions( - self, organization: str, package_type: PackageType, package_name: str - ) -> AsyncIterator[PackageVersion]: + async def packages( + self, organization: str, package_type: str + ) -> AsyncIterator[Package]: """ - Get information about package versions - - - https://docs.github.com/en/rest/reference/packages#list-package-versions-for-an-organization + Get information about organization packages + https://docs.github.com/en/rest/reference/packages#list-packages-for-an-organization Args: organization: GitHub organization to use - package_type: Type of the package to get - package_name: Name of the package to get + package_type: Type of the packages to list + Raises: `httpx.HTTPStatusError`: If there was an error in the request Returns: - An async iterator yielding package versions + An async iterator yielding packages information Example: .. code-block:: python @@ -140,21 +99,19 @@ async def package_versions( from pontos.github.api import GitHubAsyncRESTApi async with GitHubAsyncRESTApi(token) as api: - async for package in api.packages.package_versions( + async for package in api.packages.packages( organization="foo", package_type="container", - package_name="bar", ): print(package) """ - api = f"/orgs/{organization}/packages/{package_type}/{package_name}/versions" - async for response in self._client.get_all(api): + api = f"/orgs/{organization}/packages/{package_type}" + params = {"per_page": "100"} + async for response in self._client.get_all(api, params=params): response.raise_for_status() - versions = response.json() - if versions: - for version in versions: - yield PackageVersion.from_dict(version) + for package in response.json(): + yield Package.from_dict(package) async def package_version( self, @@ -201,6 +158,48 @@ async def package_version( raise GitHubApiError(response) return PackageVersion.from_dict(response.json()) + async def package_versions( + self, organization: str, package_type: PackageType, package_name: str + ) -> AsyncIterator[PackageVersion]: + """ + Get information about package versions + + + https://docs.github.com/en/rest/reference/packages#list-package-versions-for-an-organization + + + Args: + organization: GitHub organization to use + package_type: Type of the package to get + package_name: Name of the package to get + + Raises: + `httpx.HTTPStatusError`: If there was an error in the request + + Returns: + An async iterator yielding package versions + + Example: + .. code-block:: python + + from pontos.github.api import GitHubAsyncRESTApi + + async with GitHubAsyncRESTApi(token) as api: + async for package in api.packages.package_versions( + organization="foo", + package_type="container", + package_name="bar", + ): + print(package) + """ + api = f"/orgs/{organization}/packages/{package_type}/{package_name}/versions" + async for response in self._client.get_all(api): + response.raise_for_status() + versions = response.json() + if versions: + for version in versions: + yield PackageVersion.from_dict(version) + async def package_version_tags( self, organization: str, @@ -246,7 +245,7 @@ async def package_version_tags( if not response.is_success: raise GitHubApiError(response) resp = response.json() - return resp["metadata"][package_type]["tags"] + return resp["tags"] async def delete_package( self, organization: str, package_type: PackageType, package_name: str @@ -356,16 +355,10 @@ async def delete_package_with_tag( async for package_version in self.package_versions( organization, package_type, package_name ): - print("id") if tag in await self.package_version_tags( organization, package_type, package_name, package_version.id ): api = f"/orgs/{organization}/packages/{package_type}/{package_name}/versions/{package_version.id}" response = await self._client.delete(api) - print("deleted") if not response.is_success: raise GitHubApiError(response) - api = f"/orgs/{organization}/packages/{package_type}/{package_name}/versions/tags/{tag}" - response = await self._client.delete(api) - if not response.is_success: - raise GitHubApiError(response) diff --git a/tests/github/api/test_packages.py b/tests/github/api/test_packages.py index 795751eb1..a6b38766c 100644 --- a/tests/github/api/test_packages.py +++ b/tests/github/api/test_packages.py @@ -176,7 +176,7 @@ async def test_package_version_tags(self): ) self.client.get.assert_awaited_once_with( - "/orgs/foo/packages/container/bar/versions/1/tags" + "/orgs/foo/packages/container/bar/versions/1" ) self.assertEqual(tags, ["latest", "stable"]) @@ -241,12 +241,27 @@ async def test_delete_package_with_tag(self): response = create_response(is_success=True) self.client.delete.return_value = response + package_version_response = create_response() + package_version_response.json.return_value = [PACKAGE_VERSION] + self.client.get_all.return_value = AsyncIteratorMock([package_version_response]) + + tags_response = create_response() + tags_response.json.return_value = {"tags": ["latest", "stable"]} + self.client.get.return_value = tags_response + await self.api.delete_package_with_tag( organization="foo", package_type=PackageType.CONTAINER, package_name="bar", - tag="latest", + tag="latest" ) - self.client.delete.assert_awaited_once_with( - "/orgs/foo/packages/container/bar/versions/tags/latest" + + self.client.get_all.assert_called_once_with( + "/orgs/foo/packages/container/bar/versions" + ) + self.client.get.assert_awaited_once_with( + "/orgs/foo/packages/container/bar/versions/1" ) + self.client.delete.assert_awaited_once_with( + "/orgs/foo/packages/container/bar/versions/1" + ) \ No newline at end of file