more tests

This commit is contained in:
Tyrel Souza 2021-11-04 23:30:12 -04:00
parent cfc2ccb89b
commit 7cba3615c8
6 changed files with 60 additions and 24 deletions

View File

@ -1,2 +1,7 @@
[html]
directory = coverage_html_report
[report]
show_missing = True
omit =
tasks*

1
api.py
View File

@ -1,5 +1,6 @@
import httpx
import os
import sys
from urllib.parse import urljoin

View File

@ -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'

10
cli.py
View File

@ -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__":

15
ghub.py
View File

@ -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:

View File

@ -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'