-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from ghinks/feat/test-reviewers
Feat/test reviewers
- Loading branch information
Showing
11 changed files
with
295 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Asynch testing notes | ||
## | ||
## references | ||
- [reviewers github api docs](https://docs.github.com/en/rest/pulls/reviews?apiVersion=2022-11-28) | ||
- [github api docs](https://docs.github.com/en/rest/reference/pulls#reviews) | ||
|
||
### sample response | ||
```json | ||
[ | ||
{ | ||
"id": 80, | ||
"node_id": "MDE3OlB1bGxSZXF1ZXN0UmV2aWV3ODA=", | ||
"user": { | ||
"login": "octocat", | ||
"id": 1, | ||
"node_id": "MDQ6VXNlcjE=", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"html_url": "https://github.com/octocat", | ||
"followers_url": "https://api.github.com/users/octocat/followers", | ||
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | ||
"organizations_url": "https://api.github.com/users/octocat/orgs", | ||
"repos_url": "https://api.github.com/users/octocat/repos", | ||
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/octocat/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"body": "Here is the body for the review.", | ||
"state": "APPROVED", | ||
"html_url": "https://github.com/octocat/Hello-World/pull/12#pullrequestreview-80", | ||
"pull_request_url": "https://api.github.com/repos/octocat/Hello-World/pulls/12", | ||
"_links": { | ||
"html": { | ||
"href": "https://github.com/octocat/Hello-World/pull/12#pullrequestreview-80" | ||
}, | ||
"pull_request": { | ||
"href": "https://api.github.com/repos/octocat/Hello-World/pulls/12" | ||
} | ||
}, | ||
"submitted_at": "2019-11-17T17:43:43Z", | ||
"commit_id": "ecdd80bb57125d7ba9641ffaa4d7d2c19d3f3091", | ||
"author_association": "COLLABORATOR" | ||
} | ||
] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
[ | ||
{ | ||
"id": 80, | ||
"node_id": "MDE3OlB1bGxSZXF1ZXN0UmV2aWV3ODA=", | ||
"user": { | ||
"login": "octocat", | ||
"id": 1, | ||
"node_id": "MDQ6VXNlcjE=", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"html_url": "https://github.com/octocat", | ||
"followers_url": "https://api.github.com/users/octocat/followers", | ||
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | ||
"organizations_url": "https://api.github.com/users/octocat/orgs", | ||
"repos_url": "https://api.github.com/users/octocat/repos", | ||
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/octocat/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"body": "Here is the body for the review.", | ||
"state": "APPROVED", | ||
"html_url": "https://github.com/octocat/Hello-World/pull/12#pullrequestreview-80", | ||
"pull_request_url": "https://api.github.com/repos/octocat/Hello-World/pulls/12", | ||
"_links": { | ||
"html": { | ||
"href": "https://github.com/octocat/Hello-World/pull/12#pullrequestreview-80" | ||
}, | ||
"pull_request": { | ||
"href": "https://api.github.com/repos/octocat/Hello-World/pulls/12" | ||
} | ||
}, | ||
"submitted_at": "2019-11-17T17:43:43Z", | ||
"commit_id": "ecdd80bb57125d7ba9641ffaa4d7d2c19d3f3091", | ||
"author_association": "COLLABORATOR" | ||
} | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
from typing import Callable | ||
from unittest.mock import patch | ||
|
||
import aiohttp | ||
import pytest | ||
from aioresponses import aioresponses | ||
|
||
from pr_reviews.queries.get_reviewers_rest import fetch | ||
|
||
|
||
@pytest.fixture | ||
def read_reviews_file() -> str: | ||
# assume the reviews_response.json file is in the tests/fixtures directory | ||
with Path("tests/fixtures/reviews_response.json").open("r") as file: | ||
return json.dumps(json.load(file)) | ||
|
||
@pytest.fixture | ||
def get_reviews_url() -> callable: | ||
def _get_reviews_url(owner: str, repo: str, pull_number: int) -> str: | ||
return f"https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/reviews" | ||
return _get_reviews_url | ||
|
||
|
||
@pytest.fixture | ||
def mock_aioresponse() -> aioresponses: | ||
with aioresponses() as m: | ||
yield m | ||
|
||
@pytest.mark.asyncio | ||
@patch.dict(os.environ, {"GITHUB_TOKEN": "test_token"}) | ||
async def test_get_reviewers_success(read_reviews_file: str, | ||
get_reviews_url: Callable[[],str], | ||
mock_aioresponse: aioresponses) -> None: | ||
url = get_reviews_url("expressjs", "express", 1) | ||
mock_aioresponse.get( | ||
url, | ||
status=200, | ||
payload=read_reviews_file, | ||
) | ||
async with aiohttp.ClientSession() as client: | ||
response = await fetch(client, url) | ||
assert response == read_reviews_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import asyncio | ||
import unittest | ||
|
||
import aiohttp | ||
from aioresponses import aioresponses | ||
|
||
from pr_reviews.queries.get_reviewers_rest import fetch | ||
from tests.utils import get_reviews_url, read_reviews_file | ||
|
||
|
||
class TestFetch(unittest.TestCase): | ||
@aioresponses() | ||
def test_fetch_json(self, mocked: aioresponses) -> None: | ||
url = get_reviews_url("expressjs", "express", 1) | ||
payload = read_reviews_file() | ||
mocked.get(url, status=200, payload=payload) | ||
|
||
async def run_test() -> None: | ||
async with aiohttp.ClientSession() as session: | ||
result = await fetch(session,url) | ||
assert result == payload | ||
|
||
asyncio.run(run_test()) |
26 changes: 26 additions & 0 deletions
26
tests/get_reviewers/test_get_reviewers_rest_fetch_batch.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import asyncio | ||
import unittest | ||
|
||
from aioresponses import aioresponses | ||
|
||
from pr_reviews.queries.get_reviewers_rest import fetch_batch | ||
from tests.utils import get_reviews_url, read_reviews_file | ||
|
||
|
||
class TestFetchBatch(unittest.TestCase): | ||
@aioresponses() | ||
def test_fetch_json(self, mocked: aioresponses) -> None: | ||
payload = read_reviews_file() | ||
urls =[] | ||
for pull_number in range(2): | ||
url = get_reviews_url("expressjs", | ||
"express", | ||
pull_number) | ||
urls.append(url) | ||
mocked.get(url, status=200, payload=payload) | ||
|
||
async def run_test() -> None: | ||
result = await fetch_batch(urls) | ||
assert result == [payload, payload] | ||
|
||
asyncio.run(run_test()) |
31 changes: 31 additions & 0 deletions
31
tests/get_reviewers/test_get_reviewers_rest_pull_requests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import unittest | ||
|
||
from aioresponses import aioresponses | ||
|
||
from pr_reviews.queries.get_reviewers_rest import ( | ||
get_reviewers_for_pull_requests, | ||
) | ||
from tests.utils import get_reviews_url, read_reviews_file | ||
|
||
|
||
class TestGetReviewers(unittest.TestCase): | ||
OWNER = "expressjs" | ||
REPO = "express" | ||
PULL_REQUESTS: tuple[int, ...] = (1, 2) | ||
@aioresponses() | ||
def test_get_reviewers(self, mocked: aioresponses) -> None: | ||
payload = read_reviews_file() | ||
urls =[] | ||
for pull_number in self.PULL_REQUESTS: | ||
url = get_reviews_url(self.OWNER, | ||
self.REPO, | ||
pull_number) | ||
urls.append(url) | ||
mocked.get(url, status=200, payload=payload) | ||
|
||
results = get_reviewers_for_pull_requests(self.OWNER, | ||
self.REPO, | ||
self.PULL_REQUESTS) | ||
assert results[0]["id"] == 1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Python | ||
import json | ||
from pathlib import Path | ||
|
||
|
||
def read_reviews_file() -> str: | ||
# assume the reviews_response.json file is in the tests/fixtures directory | ||
with Path("tests/fixtures/reviews_response.json").open("r") as file: | ||
return json.load(file) | ||
|
||
def get_reviews_url(owner: str, repo: str, pull_number: int) -> str: | ||
return f"https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/reviews" |