Skip to content

Commit

Permalink
feat: async testing
Browse files Browse the repository at this point in the history
- ruff lint, mostly annotations
  • Loading branch information
ghinks committed Dec 26, 2024
1 parent 59a3796 commit 88f423f
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions tests/test_get_reviewers_rest.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
import json
import os
from typing import Callable, Any
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():
def read_reviews_file() -> str:
# assume the reviews_response.json file is in the tests/fixtures directory
with open("tests/fixtures/reviews_response.json") as file:
return json.load(file)
with Path("tests/fixtures/reviews_response.json").open("r") as file:
return json.dumps(json.load(file))

@pytest.fixture
def get_reviews_url():
def get_reviews_url() -> callable:
def _get_reviews_url(owner: str, repo: str, pull_number: int) -> str:
url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/reviews"
return url
return f"https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/reviews"
return _get_reviews_url


@pytest.fixture
def mock_aioresponse():
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, get_reviews_url, mock_aioresponse):
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
payload=read_reviews_file,
)
async with aiohttp.ClientSession() as client:
response = await fetch(client, url)
Expand Down

0 comments on commit 88f423f

Please sign in to comment.