From 7cba3615c822943e6f2a9b3b976f2135a4eaecdd Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Thu, 4 Nov 2021 23:30:12 -0400 Subject: [PATCH] more tests --- .coveragerc | 5 +++++ api.py | 1 + api_tests.py | 12 ++++++++++++ cli.py | 10 +++++++--- ghub.py | 15 +++++---------- ghub_tests.py | 41 ++++++++++++++++++++++++++++++----------- 6 files changed, 60 insertions(+), 24 deletions(-) diff --git a/.coveragerc b/.coveragerc index e79111d..129acfc 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,2 +1,7 @@ [html] directory = coverage_html_report + +[report] +show_missing = True +omit = + tasks* diff --git a/api.py b/api.py index c03a24f..35e1093 100644 --- a/api.py +++ b/api.py @@ -1,5 +1,6 @@ import httpx import os +import sys from urllib.parse import urljoin diff --git a/api_tests.py b/api_tests.py index 56804e9..caf1ead 100644 --- a/api_tests.py +++ b/api_tests.py @@ -1,5 +1,7 @@ import json import pytest +import httpx + from api import API from pytest_httpx import HTTPXMock @@ -11,6 +13,11 @@ def _load_fixture(name: str): with open(f"./tests/fixtures/{name}.json", "r") as f: return json.loads(f.read()) +def test_except(httpx_mock: HTTPXMock): + httpx_mock.add_response(method="GET", status_code=403) + with pytest.raises(httpx.HTTPStatusError): + API().get("boooger") + def test_get(httpx_mock: HTTPXMock): httpx_mock.add_response(method="GET", json=_load_fixture("user")) @@ -35,3 +42,8 @@ def test_get_with_pagination(httpx_mock: HTTPXMock): httpx_mock.add_response(method="GET", json=_load_fixture("repos_1")) repos = api.get_with_pagination("repos") assert len(repos) == 1 + + # test exit on dict + httpx_mock.add_response(method="GET", json=_load_fixture("user")) + user = api.get_with_pagination("user") + assert user['login'] == 'tyrelsouza' diff --git a/cli.py b/cli.py index 35f368b..fac48a2 100644 --- a/cli.py +++ b/cli.py @@ -10,9 +10,13 @@ def main(): raise Exception("Please set GITHUB_TOKEN") user_name = Prompt.ask("Username?") gh = GHub() - success = gh.load_user(user_name=user_name) - if success: - print(gh.repos_table()) + try: + success = gh.load_user(user_name=user_name) + if success: + print(gh.repos_table()) + except httpx.HTTPStatusError as e: + print(e) + if __name__ == "__main__": diff --git a/ghub.py b/ghub.py index 49e2235..c9edad8 100644 --- a/ghub.py +++ b/ghub.py @@ -9,18 +9,13 @@ class GHub: user = None repos = None - def load_user(self, user_name: str) -> bool: + def load_user(self, user_name: str)-> None: self.user_name = user_name self.api = API() - try: - self.user = self.api.get(f"/users/{user_name}") - self.repos = self.api.get_with_pagination( - f"https://api.github.com/users/{user_name}/repos" - ) - return True - except httpx.HTTPStatusError as e: - print(e) - return False + self.user = self.api.get(f"/users/{user_name}") + self.repos = self.api.get_with_pagination( + f"https://api.github.com/users/{user_name}/repos" + ) def repos_trimmed(self) -> list: for repo in self.repos: diff --git a/ghub_tests.py b/ghub_tests.py index bc7decc..52eeddb 100644 --- a/ghub_tests.py +++ b/ghub_tests.py @@ -1,25 +1,44 @@ import json import pytest from ghub import GHub +from pytest_httpx import HTTPXMock - -def _load_user(): - with open("./tests/fixtures/user.json", "r") as f: +def _load_fixture(name: str): + with open(f"./tests/fixtures/{name}.json", "r") as f: return json.loads(f.read()) - -def _load_repos(): - with open("./tests/fixtures/repos_1.json", "r") as f: - return json.loads(f.read()) - - def _fake_tyrel() -> GHub: gh = GHub() - gh.user = _load_user() - gh.repos = _load_repos() + gh.user = _load_fixture("user") + gh.repos = _load_fixture("repos_1") return gh def test_load_repos(): gh = _fake_tyrel() assert gh.repos[0]["git_url"] == "git://github.com/tyrelsouza/genealogy.git" + +def test_repos_trimmed(): + gh = _fake_tyrel() + repos = list(gh.repos_trimmed()) + assert repos[0] == ['genealogy', 'None', '0','0','0', '0'] + + +def test_get(httpx_mock: HTTPXMock): + httpx_mock.add_response(method="GET", json=_load_fixture("user")) + httpx_mock.add_response(method="GET", json=_load_fixture("repos_1")) + gh = GHub() + gh.load_user('tyrelsouza') + assert gh.user["login"] == "tyrelsouza" + assert len(gh.repos) == 1 + +def test_table(): + gh = _fake_tyrel() + table = gh.repos_table() + assert table.row_count == 1 + assert table.columns[0]._cells[0] == 'genealogy' + assert table.columns[1]._cells[0] == 'None' + assert table.columns[2]._cells[0] == '0' + assert table.columns[3]._cells[0] == '0' + assert table.columns[4]._cells[0] == '0' +