From a0de0a4ccff779a768442ae4f1d1d6036de75194 Mon Sep 17 00:00:00 2001 From: F-G Fernandez <26927750+frgfm@users.noreply.github.com> Date: Mon, 11 Dec 2023 13:30:24 +0100 Subject: [PATCH] test: Adds unittests --- src/tests/endpoints/test_repos.py | 26 +++++++++ src/tests/test_services.py | 97 +++++++++++++++++++++++++++++-- 2 files changed, 119 insertions(+), 4 deletions(-) diff --git a/src/tests/endpoints/test_repos.py b/src/tests/endpoints/test_repos.py index aa321e7..418712c 100644 --- a/src/tests/endpoints/test_repos.py +++ b/src/tests/endpoints/test_repos.py @@ -379,3 +379,29 @@ async def test_add_repo_to_waitlist( assert response.status_code == status_code, print(response.json()) if isinstance(status_detail, str): assert response.json()["detail"] == status_detail + + +@pytest.mark.parametrize( + ("user_idx", "repo_id", "status_code", "status_detail"), + [ + (None, 12345, 401, "Not authenticated"), + (0, 100, 404, "Not Found"), + ], +) +@pytest.mark.asyncio() +async def test_parse_guidelines_from_github( + async_client: AsyncClient, + guideline_session: AsyncSession, + user_idx: Union[int, None], + repo_id: int, + status_code: int, + status_detail: Union[str, None], +): + auth = None + if isinstance(user_idx, int): + auth = await pytest.get_token(USER_TABLE[user_idx]["id"], USER_TABLE[user_idx]["scope"].split()) + + response = await async_client.post(f"/repos/{repo_id}/parse", headers=auth) + assert response.status_code == status_code, print(response.json()) + if isinstance(status_detail, str): + assert response.json()["detail"] == status_detail diff --git a/src/tests/test_services.py b/src/tests/test_services.py index 10ae0c9..9201906 100644 --- a/src/tests/test_services.py +++ b/src/tests/test_services.py @@ -2,6 +2,7 @@ from fastapi import HTTPException from app.services.github import GitHubClient +from app.services.utils import execute_in_parallel @pytest.mark.parametrize( @@ -105,20 +106,108 @@ async def test_githubclient_list_pulls(repo_name, status_code, status_detail): github_client.list_pulls(repo_name) +@pytest.mark.parametrize( + ("repo_name", "issue_number", "status_code", "status_detail"), + [ + ("frgfm/hola", 1, 404, "Not Found"), + ("frgfm/torch-cam", 181, 200, None), + ], +) +@pytest.mark.asyncio() +async def test_githubclient_list_comments_from_issue(repo_name, issue_number, status_code, status_detail): + github_client = GitHubClient() + if status_code // 100 == 2: + response = github_client.list_comments_from_issue(issue_number, repo_name) + assert isinstance(response, list) + assert all(isinstance(elt, dict) for elt in response) + else: + with pytest.raises(HTTPException): + github_client.list_comments_from_issue(issue_number, repo_name) + + +@pytest.mark.parametrize( + ("repo_name", "pull_number", "status_code", "status_detail"), + [ + ("frgfm/hola", 1, 404, "Not Found"), + ("frgfm/Holocron", 279, 200, None), + ], +) +@pytest.mark.asyncio() +async def test_githubclient_list_reviews_from_pull(repo_name, pull_number, status_code, status_detail): + github_client = GitHubClient() + if status_code // 100 == 2: + response = github_client.list_reviews_from_pull(repo_name, pull_number) + assert isinstance(response, list) + assert all(isinstance(elt, dict) for elt in response) + else: + with pytest.raises(HTTPException): + github_client.list_reviews_from_pull(repo_name, pull_number) + + +@pytest.mark.parametrize( + ("repo_name", "pull_number", "status_code", "status_detail"), + [ + ("frgfm/hola", 1, 404, "Not Found"), + ("frgfm/Holocron", 279, 200, None), + ], +) +@pytest.mark.asyncio() +async def test_githubclient_list_review_comments_from_pull(repo_name, pull_number, status_code, status_detail): + github_client = GitHubClient() + if status_code // 100 == 2: + response = github_client.list_review_comments_from_pull(pull_number, repo_name) + assert isinstance(response, list) + assert all(isinstance(elt, dict) for elt in response) + else: + with pytest.raises(HTTPException): + github_client.list_review_comments_from_pull(pull_number, repo_name) + + @pytest.mark.parametrize( ("repo_name", "status_code", "status_detail"), [ ("frgfm/hola", 404, "Not Found"), - ("frgfm/torch-cam", 200, None), + ("frgfm/Holocron", 200, None), ], ) @pytest.mark.asyncio() -async def test_githubclient_list_review_comments(repo_name, status_code, status_detail): +async def test_githubclient_fetch_reviews_from_repo(repo_name, status_code, status_detail): github_client = GitHubClient() if status_code // 100 == 2: - response = github_client.list_review_comments(repo_name) + response = github_client.fetch_reviews_from_repo(repo_name, num_pulls=1) assert isinstance(response, list) assert all(isinstance(elt, dict) for elt in response) else: with pytest.raises(HTTPException): - github_client.list_review_comments(repo_name) + github_client.fetch_reviews_from_repo(repo_name, num_pulls=1) + + +@pytest.mark.parametrize( + ("repo_name", "status_code", "status_detail"), + [ + ("frgfm/hola", 404, "Not Found"), + ("frgfm/Holocron", 200, None), + ], +) +@pytest.mark.asyncio() +async def test_githubclient_fetch_pull_comments_from_repo(repo_name, status_code, status_detail): + github_client = GitHubClient() + if status_code // 100 == 2: + response = github_client.fetch_pull_comments_from_repo(repo_name, num_pulls=1) + assert isinstance(response, list) + assert all(isinstance(elt, dict) for elt in response) + else: + with pytest.raises(HTTPException): + github_client.fetch_pull_comments_from_repo(repo_name, num_pulls=1) + + +@pytest.mark.parametrize( + ("func", "arr", "output"), + [ + (lambda x: x**2, [1, 2, 3], [1, 4, 9]), + ], +) +@pytest.mark.asyncio() +async def test_execute_in_parallel(func, arr, output): + assert list(execute_in_parallel(func, arr, num_threads=1)) == output + assert list(execute_in_parallel(func, arr, num_threads=2)) == output