From ca869c759cd07a4e6534dd6244070b1a04debd72 Mon Sep 17 00:00:00 2001 From: Alec Clowes Date: Sat, 26 Aug 2017 09:30:41 -0700 Subject: [PATCH 1/5] add management command tests --- yawn/management/tests/test_examples.py | 8 +++++++ yawn/management/tests/test_webserver.py | 31 +++++++++++++++++++++++++ yawn/management/tests/test_worker.py | 11 +++++++++ 3 files changed, 50 insertions(+) create mode 100644 yawn/management/tests/test_examples.py create mode 100644 yawn/management/tests/test_webserver.py create mode 100644 yawn/management/tests/test_worker.py diff --git a/yawn/management/tests/test_examples.py b/yawn/management/tests/test_examples.py new file mode 100644 index 0000000..863833c --- /dev/null +++ b/yawn/management/tests/test_examples.py @@ -0,0 +1,8 @@ +from django.core import management + +from yawn.workflow.models import Workflow + + +def test_examples(): + management.call_command('examples') + assert Workflow.objects.count() == 3 diff --git a/yawn/management/tests/test_webserver.py b/yawn/management/tests/test_webserver.py new file mode 100644 index 0000000..b971b25 --- /dev/null +++ b/yawn/management/tests/test_webserver.py @@ -0,0 +1,31 @@ +from unittest import mock + +from django.conf import settings +from django.core import management + +from yawn.management.commands import webserver + + +@mock.patch.object(webserver.WSGIApplication, 'run') +def test_webserver(mock_run): + management.call_command('webserver') + assert mock_run.call_count == 1 + + +def test_static_files(): + app = webserver.get_wsgi_application() + whitenoise = webserver.DefaultFileServer(app, settings) + whitenoise.application = mock.Mock() + + # an API request + whitenoise({'PATH_INFO': '/api/'}, None) + assert whitenoise.application.call_count == 1 + + # an unmatched file, goes to homepage + start_response = mock.Mock() + response = whitenoise({'PATH_INFO': '/', 'REQUEST_METHOD': 'GET'}, start_response) + assert 'index.html' in response.filelike.name + + # a real static file + response = whitenoise({'PATH_INFO': '/favicon.ico', 'REQUEST_METHOD': 'GET'}, start_response) + assert 'favicon.ico' in response.filelike.name diff --git a/yawn/management/tests/test_worker.py b/yawn/management/tests/test_worker.py new file mode 100644 index 0000000..d5f8200 --- /dev/null +++ b/yawn/management/tests/test_worker.py @@ -0,0 +1,11 @@ +from unittest import mock + +from django.core import management + +from yawn.management.commands import worker + + +@mock.patch.object(worker.Main, 'run') +def test_worker(mock_run): + management.call_command('worker') + assert mock_run.called From 8b8ea0ca7e2968ca84fa890d40227709d3fef51b Mon Sep 17 00:00:00 2001 From: Alec Clowes Date: Sat, 26 Aug 2017 09:53:06 -0700 Subject: [PATCH 2/5] add management command tests --- yawn/management/tests/test_webserver.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/yawn/management/tests/test_webserver.py b/yawn/management/tests/test_webserver.py index b971b25..4ccdc94 100644 --- a/yawn/management/tests/test_webserver.py +++ b/yawn/management/tests/test_webserver.py @@ -1,13 +1,19 @@ +import os +import subprocess + from unittest import mock from django.conf import settings from django.core import management +import yawn + from yawn.management.commands import webserver @mock.patch.object(webserver.WSGIApplication, 'run') def test_webserver(mock_run): + mock.patch('argparse._sys.argv', ['worker']) management.call_command('webserver') assert mock_run.call_count == 1 @@ -17,6 +23,11 @@ def test_static_files(): whitenoise = webserver.DefaultFileServer(app, settings) whitenoise.application = mock.Mock() + # make files to serve + for filename in ('index.html', 'favicon.ico'): + path = os.path.join(os.path.dirname(yawn.__file__), 'staticfiles', filename) + subprocess.check_call(['touch', path]) + # an API request whitenoise({'PATH_INFO': '/api/'}, None) assert whitenoise.application.call_count == 1 From 216081d19c8a118db6c978543a159fd995c0c842 Mon Sep 17 00:00:00 2001 From: Alec Clowes Date: Sat, 26 Aug 2017 10:03:45 -0700 Subject: [PATCH 3/5] fix argparse during testing --- yawn/management/tests/test_webserver.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/yawn/management/tests/test_webserver.py b/yawn/management/tests/test_webserver.py index 4ccdc94..b199079 100644 --- a/yawn/management/tests/test_webserver.py +++ b/yawn/management/tests/test_webserver.py @@ -1,4 +1,5 @@ import os +import sys import subprocess from unittest import mock @@ -13,9 +14,10 @@ @mock.patch.object(webserver.WSGIApplication, 'run') def test_webserver(mock_run): - mock.patch('argparse._sys.argv', ['worker']) - management.call_command('webserver') - assert mock_run.call_count == 1 + # pytest with coverage arguments causes gunicorn's argparse to fail + with mock.patch.object(sys, 'argv', ['worker']): + management.call_command('webserver') + assert mock_run.call_count == 1 def test_static_files(): From 0d1610414b62e934d9fe55e04b5638629e5fa31b Mon Sep 17 00:00:00 2001 From: Alec Clowes Date: Sat, 26 Aug 2017 10:14:50 -0700 Subject: [PATCH 4/5] files for whitenoise --- yawn/management/tests/test_webserver.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/yawn/management/tests/test_webserver.py b/yawn/management/tests/test_webserver.py index b199079..89415a9 100644 --- a/yawn/management/tests/test_webserver.py +++ b/yawn/management/tests/test_webserver.py @@ -21,15 +21,18 @@ def test_webserver(mock_run): def test_static_files(): - app = webserver.get_wsgi_application() - whitenoise = webserver.DefaultFileServer(app, settings) - whitenoise.application = mock.Mock() - # make files to serve + root = os.path.join(os.path.dirname(yawn.__file__), 'staticfiles/') + os.makedirs(root, exist_ok=True) for filename in ('index.html', 'favicon.ico'): - path = os.path.join(os.path.dirname(yawn.__file__), 'staticfiles', filename) + path = os.path.join(root, filename) subprocess.check_call(['touch', path]) + # setup whitenoise after the files exist + app = webserver.get_wsgi_application() + whitenoise = webserver.DefaultFileServer(app, settings) + whitenoise.application = mock.Mock() + # an API request whitenoise({'PATH_INFO': '/api/'}, None) assert whitenoise.application.call_count == 1 From 3c7c8c4fc5aa322c2fe6d2d64c826fc68c4d48f6 Mon Sep 17 00:00:00 2001 From: Alec Clowes Date: Sat, 26 Aug 2017 10:19:17 -0700 Subject: [PATCH 5/5] files for whitenoise --- yawn/management/tests/static/favicon.ico | 0 yawn/management/tests/static/index.html | 0 yawn/management/tests/test_webserver.py | 19 +++++-------------- 3 files changed, 5 insertions(+), 14 deletions(-) create mode 100644 yawn/management/tests/static/favicon.ico create mode 100644 yawn/management/tests/static/index.html diff --git a/yawn/management/tests/static/favicon.ico b/yawn/management/tests/static/favicon.ico new file mode 100644 index 0000000..e69de29 diff --git a/yawn/management/tests/static/index.html b/yawn/management/tests/static/index.html new file mode 100644 index 0000000..e69de29 diff --git a/yawn/management/tests/test_webserver.py b/yawn/management/tests/test_webserver.py index 89415a9..a7852e4 100644 --- a/yawn/management/tests/test_webserver.py +++ b/yawn/management/tests/test_webserver.py @@ -1,14 +1,11 @@ import os import sys -import subprocess from unittest import mock from django.conf import settings from django.core import management -import yawn - from yawn.management.commands import webserver @@ -21,17 +18,11 @@ def test_webserver(mock_run): def test_static_files(): - # make files to serve - root = os.path.join(os.path.dirname(yawn.__file__), 'staticfiles/') - os.makedirs(root, exist_ok=True) - for filename in ('index.html', 'favicon.ico'): - path = os.path.join(root, filename) - subprocess.check_call(['touch', path]) - - # setup whitenoise after the files exist - app = webserver.get_wsgi_application() - whitenoise = webserver.DefaultFileServer(app, settings) - whitenoise.application = mock.Mock() + root = os.path.join(settings.BASE_DIR, 'management/tests/static') + with mock.patch.object(settings, 'WHITENOISE_ROOT', root): + app = webserver.get_wsgi_application() + whitenoise = webserver.DefaultFileServer(app, settings) + whitenoise.application = mock.Mock() # an API request whitenoise({'PATH_INFO': '/api/'}, None)