diff --git a/app/__init__.py b/app/__init__.py index 5e96ca1..e69de29 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1 +0,0 @@ -import dramatiq diff --git a/app/app.py b/app/app.py new file mode 100644 index 0000000..5668592 --- /dev/null +++ b/app/app.py @@ -0,0 +1,8 @@ +import requests + +# TODO add error handling logic for request +def count_words(url): + response = requests.get(url, timeout=5) + word_count = len(response.text.split(" ")) + print(f"There are {word_count} words in {url}") + return word_count diff --git a/tasks.py b/tasks.py index f2bc15c..b98864b 100644 --- a/tasks.py +++ b/tasks.py @@ -12,10 +12,18 @@ def start_app(docs=False, extra=""): print(f"Building docs. Docs value: {extra}") +@task +def run_unit_tests(c): + """Triggers a local unittest run for the app.""" + # Pytest will hide color output when it thinks it's running + # outside of a terminal therefore pty=True is used. + c.run("python3 -m pytest -vvv tests/unit", pty=True) + + @task def fix_eof(c): """Fixes any missing newlines for end of files.""" - c.run("pre-commit run end-of-file-fixer --all-files") + c.run("pre-commit run end-of-file-fixer --all-files", pty=True) @task diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_app.py b/tests/unit/test_app.py new file mode 100644 index 0000000..9e584d9 --- /dev/null +++ b/tests/unit/test_app.py @@ -0,0 +1,16 @@ +from unittest.mock import MagicMock, patch + +from app.app import count_words + + +@patch("app.app.requests") +def test_count_words(mock_requests): + mock_response = MagicMock() + mock_response.text = "example word count split" + mock_requests.get.return_value = mock_response + + wc = count_words("http://anyurl.com") + assert wc == 4 + + +# TODO add test coverage for failure