Skip to content

Commit

Permalink
test(api): Stop hardcoding filename, test resume validations
Browse files Browse the repository at this point in the history
Not hardcoding allows a single GitHub user to create
multiple resume files, which can then be tested.
  • Loading branch information
alexpovel committed Aug 14, 2022
1 parent 02370ab commit 476bf58
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
10 changes: 8 additions & 2 deletions ancv/web/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from http import HTTPStatus
from types import SimpleNamespace

import aiohttp
import gidgethub
Expand All @@ -22,6 +23,7 @@ async def get_resume(
session: aiohttp.ClientSession,
github: GitHubAPI,
stopwatch: Stopwatch,
filename: str = "resume.json",
size_limit: int = 1 * SIPrefix.MEGA,
) -> ResumeSchema:
log = LOGGER.bind(user=user, session=session)
Expand All @@ -33,7 +35,7 @@ async def get_resume(
raw_gist = await anext(gists)
except StopAsyncIteration:
raise ResumeLookupError(
f"No 'resume.json' file found in any gist of '{user}'."
f"No '{filename}' file found in any gist of '{user}'."
)
except gidgethub.BadRequest as e:
# `except `RateLimitExceeded` didn't work, it seems it's not correctly
Expand All @@ -52,8 +54,12 @@ async def get_resume(
log = log.bind(gist_url=gist.url)
log.info("Parsed gist of user.")

# https://peps.python.org/pep-0636/#matching-against-constants-and-enums :
obj = SimpleNamespace() # Direct kwargs passing isn't mypy-friendly.
obj.filename = filename

match gist:
case Gist(files={"resume.json": file}):
case Gist(files={obj.filename: file}):
log.info("Gist matched.")
break
case _:
Expand Down
36 changes: 32 additions & 4 deletions tests/web/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,57 @@ async def gh_api(client_session):


@pytest.mark.parametrize(
["username", "size_limit", "expectation"],
["username", "size_limit", "filename", "expectation"],
[
(
"alexpovel",
1 * SIPrefix.MEGA,
"resume.json",
does_not_raise(),
),
(
"alexpovel",
0,
pytest.raises(ResumeLookupError),
"resume.json",
pytest.raises(
ResumeLookupError,
match=r"^Resume file too large \(limit: 0 Bytes, got \d+\.\d+ kB\)\.$",
),
),
(
"alexpovel",
1 * SIPrefix.MEGA,
"resume.invalid-json",
pytest.raises(ResumeLookupError, match=r"^Got malformed JSON\.$"),
),
(
"alexpovel",
1 * SIPrefix.MEGA,
"resume.invalid-schema.json",
pytest.raises(
ResumeLookupError,
match=r"^Got legal JSON but wrong schema \(cf\. https://jsonresume\.org/schema/\)$",
),
),
],
)
@pytest.mark.asyncio
async def test_get_resume_size_limit(
async def test_get_resume_validations(
username: str,
client_session: aiohttp.ClientSession,
gh_api: GitHubAPI,
stopwatch: Stopwatch,
size_limit: int,
filename: str,
expectation: ContextManager,
) -> None:
api = await gh_api
with expectation:
await get_resume(username, client_session, api, stopwatch, size_limit)
await get_resume(
user=username,
session=client_session,
github=api,
stopwatch=stopwatch,
filename=filename,
size_limit=size_limit,
)

0 comments on commit 476bf58

Please sign in to comment.