Add tests for Flask views

This commit is contained in:
f4lco
2019-01-27 18:55:42 +01:00
parent e4814cf8dc
commit 523cc8ba62
5 changed files with 259 additions and 104 deletions
+13
View File
@@ -0,0 +1,13 @@
# -*- encoding: utf-8 -*-
from xml.etree import ElementTree
def meal_names(response):
"""Extract meal names from OpenMensa XML.
By no means below parsing is robust or complete. Below just helps ensuring that the parser indeed returns proper XML
instead of arbitrary responses."""
root = ElementTree.fromstring(response)
namespace = {'om': 'http://openmensa.org/open-mensa-v2'}
nodes = root.findall('om:canteen/om:day/om:category/om:meal/om:name', namespace)
return [node.text for node in nodes]
+37
View File
@@ -0,0 +1,37 @@
# -*- encoding: utf-8 -*-
import httpretty
import os
import pytest
from stw_potsdam import canteen_api
@pytest.fixture
def api_offline():
"""Disallow all network requests."""
httpretty.enable(allow_net_connect=False)
yield
httpretty.disable()
httpretty.reset()
@pytest.fixture
def api_online_one_shot():
"""Allow a single API request, returning the contents of 'input.json'.
Subsequent API invocations will return with HTTP status code 500."""
def canned_menu(request, uri, response_headers):
path = os.path.join(os.path.dirname(__file__), 'resources', 'input.json')
with open(path) as f:
return 200, response_headers, f.read()
responses = [
httpretty.Response(body=canned_menu),
httpretty.Response(body='invalid', status=500),
]
httpretty.register_uri(httpretty.POST, canteen_api.URL, responses=responses)
httpretty.enable(allow_net_connect=False)
yield httpretty
httpretty.disable()
httpretty.reset()
+72
View File
@@ -0,0 +1,72 @@
# -*- encoding: utf-8 -*-
import pytest
from response_util import meal_names
from stw_potsdam import views
# noinspection PyUnresolvedReferences
# pytest fixtures are linked via parameter names of test methods
from stub_api import api_offline, api_online_one_shot
def test_health_check(client):
response = client.get('/health_check')
assert response.status_code == 200
assert response.data == 'OK'
def test_index(client):
response = client.get('/').json
canteen_url = response.get('griebnitzsee', None)
assert canteen_url, 'Known canteen in index response'
canteen = client.get(canteen_url)
assert canteen.status_code == 200, 'Canteen URL is reachable'
@pytest.mark.parametrize('url', ['/canteens/spam', '/canteens/spam/meta', '/canteens/spam/menu'])
def test_canteen_not_found(client, url):
response = client.get(url)
assert response.status_code == 404
assert "Canteen 'spam' not found" in response.data
@pytest.mark.xfail(strict=True)
def test_canteen_menu_api_unavailable(client, api_offline):
_request_check_meals(client)
def test_canteen_menu_request(client, api_online_one_shot):
_request_check_meals(client)
def test_canteen_menu_cached(client, api_online_one_shot):
_request_check_meals(client)
_request_check_meals(client)
@pytest.mark.xfail(strict=True)
def test_canteen_menu_second_request_indeed_fails(client, api_online_one_shot):
_request_check_meals(client)
views.cache.clear()
_request_check_meals(client)
def _request_check_meals(client):
response = client.get('/canteens/griebnitzsee/menu')
assert response.status_code == 200
meals = meal_names(response.data)
assert meals[0] == u"Gefüllter Germknödel \nmit Vanillesauce und Mohnzucker"
@pytest.fixture
def client():
views.app.config['TESTING'] = True
return views.app.test_client()
@pytest.fixture(autouse=True)
def clear_cache():
yield
views.cache.clear()