Skip to content

Commit

Permalink
Add: Add tests for async GitHub branches API
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Oct 25, 2022
1 parent 7d37f78 commit 2f852da
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pontos/github/api/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def delete(self, repo: str, branch: str):
branch: Branch to be deleted
Raises:
HTTPError if the request was invalid
HTTPStatusError if the request was invalid
"""
api = f"/repos/{repo}/git/refs/{branch}"
response = await self._client.delete(api)
Expand All @@ -59,10 +59,11 @@ async def protection_rules(self, repo: str, branch: str) -> JSON:
branch: Get protection rules for this branch
Raises:
HTTPError if the request was invalid
HTTPStatusError if the request was invalid
"""
api = f"/repos/{repo}/branches/{branch}/protection"
response = await self._client.get(api)
response.raise_for_status()
return response.json()


Expand Down
83 changes: 81 additions & 2 deletions tests/github/api/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,97 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=too-many-lines

import unittest
from pathlib import Path
from unittest.mock import MagicMock, patch

from httpx import HTTPStatusError, Response

from pontos.github.api import GitHubRESTApi
from pontos.github.api.branch import GitHubAsyncRESTBranches
from pontos.github.api.client import GitHubAsyncRESTClient
from tests import AsyncMock, IsolatedAsyncioTestCase
from tests.github.api import default_request

here = Path(__file__).parent


class GitHubAsyncRESTBranchesTestCase(IsolatedAsyncioTestCase):
def setUp(self) -> None:
self.client = AsyncMock(spec=GitHubAsyncRESTClient)
self.api = GitHubAsyncRESTBranches(self.client)

async def test_exists(self):
response = MagicMock(spec=Response, is_success=True)
self.client.get.return_value = response

self.assertTrue(await self.api.exists("foo/bar", "baz"))
self.client.get.assert_awaited_once_with("/repos/foo/bar/branches/baz")

async def test_not_exists(self):
response = MagicMock(spec=Response, is_success=False)
self.client.get.return_value = response

self.assertFalse(await self.api.exists("foo/bar", "baz"))
self.client.get.assert_awaited_once_with("/repos/foo/bar/branches/baz")

async def test_delete_branch(self):
response = MagicMock(spec=Response)
self.client.delete.return_value = response

await self.api.delete("foo/bar", "baz")

self.client.delete.assert_awaited_once_with(
"/repos/foo/bar/git/refs/baz"
)

async def test_delete_branch_failure(self):
response = MagicMock(spec=Response)
error = HTTPStatusError("404", request=MagicMock(), response=response)
response.raise_for_status.side_effect = error

self.client.delete.return_value = response

with self.assertRaises(HTTPStatusError):
await self.api.delete("foo/bar", "baz")

self.client.delete.assert_awaited_once_with(
"/repos/foo/bar/git/refs/baz"
)

async def test_protection_rules(self):
rules = {
"required_status_checks": {},
"enforce_admins": {},
"required_pull_request_reviews": {},
}
response = MagicMock(spec=Response)
response.json.return_value = rules

self.client.get.return_value = response

data = await self.api.protection_rules("foo/bar", "baz")

self.client.get.assert_awaited_once_with(
"/repos/foo/bar/branches/baz/protection"
)
self.assertEqual(data, rules)

async def test_protection_rules_failure(self):
response = MagicMock(spec=Response)
error = HTTPStatusError("404", request=MagicMock(), response=response)
response.raise_for_status.side_effect = error

self.client.get.return_value = response

with self.assertRaises(HTTPStatusError):
await self.api.protection_rules("foo/bar", "baz")

self.client.get.assert_awaited_once_with(
"/repos/foo/bar/branches/baz/protection"
)


class GitHubBranchTestCase(unittest.TestCase):
@patch("pontos.github.api.api.httpx.get")
def test_branch_exists(self, requests_mock: MagicMock):
Expand Down

0 comments on commit 2f852da

Please sign in to comment.