Skip to content

Commit

Permalink
Fix: tests for the big fixed in this pr
Browse files Browse the repository at this point in the history
  • Loading branch information
lwasser committed May 7, 2024
1 parent fe3de9b commit c730c5c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
12 changes: 1 addition & 11 deletions src/pyosmeta/cli/update_review_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,7 @@
from pyosmeta.file_io import clean_export_yml, load_pickle
from pyosmeta.github_api import GitHubAPI
from pyosmeta.models import PersonModel


def get_clean_user(username: str) -> str:
"""A small helper that removes whitespace and ensures username is
lower case"""
# If the github username has spaces there is an error which might be
# that someone added a name after the username. Only return the github
# username
if len(username.split()) > 1:
username = username.split()[0]
return username.lower().strip()
from pyosmeta.utils_clean import get_clean_user


def main():
Expand Down
33 changes: 33 additions & 0 deletions src/pyosmeta/utils_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@
from typing import Any


def get_clean_user(username: str) -> str:
"""Cleans a GitHub username provided in a review issue by removing any
additional text after a space and converting to lowercase.
This function assumes that a valid username should not contain spaces. If a
space is detected, only the part before the first space is considered the
username. The resulting string is then trimmed of whitespace and converted
to lowercase.
Parameters
----------
username : str
The input username string which may contain extra text or spaces.
Returns
-------
str
The cleaned username in lowercase without any extra text or spaces.
Examples
--------
>>> get_clean_user("@githubusername")
'githubusername'
>>> get_clean_user("@githubusername name after text")
'githubusername'
"""

if len(username.split()) > 1:
username = username.split()[0]
return username.lower().strip()


def clean_date(source_date: str | None) -> datetime | str:
"""Cleans up a date string to a consistent datetime format.
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def test_get_token(mock_github_token):


def test_missing_token(mock_missing_github_token, tmpdir):
"""Test that a keyerror is raised when the token is missing."""
"""Test that a keyerror is raised when the token is missing.
If you have a token in your temporary environment, this will
fail and not return a keyerror."""
os.chdir(tmpdir)
github_api = GitHubAPI()

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/test_update_review_teams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def get_clean_user(username: str) -> str:
"""A small helper that removes whitespace and ensures username is
lower case"""
# If the github username has spaces there is an error which might be
# that someone added a name after the username. Only return the github
# username
if len(username.split()) > 1:
username = username.split()[0]
return username.lower().strip()
17 changes: 17 additions & 0 deletions tests/unit/test_utils_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
clean_date_accepted_key,
clean_markdown,
clean_name,
get_clean_user,
)


Expand Down Expand Up @@ -84,3 +85,19 @@ def test_clean_name(input_name, expected_output):
)
def test_clean_date_accepted_key(input_dict, expected_output):
assert clean_date_accepted_key(input_dict) == expected_output


@pytest.mark.parametrize(
"input_username, expected_output",
[
("githubusername", "githubusername"),
("githubusername name after text", "githubusername"),
("username (full name here)", "username"),
("githubusername", "githubusername"),
("githubusername extra text", "githubusername"),
("username (just the username)", "username"),
],
)
def test_get_clean_user(input_username, expected_output):
print(input_username)
assert get_clean_user(input_username) == expected_output

0 comments on commit c730c5c

Please sign in to comment.