Skip to content
Snippets Groups Projects
Commit 74cac7a5 authored by Valentin Lorentz's avatar Valentin Lorentz
Browse files

Make PPPTestCase methods more usable.

parent 7413dc75
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,17 @@ from webtest import TestApp
from unittest import TestCase
from ppp_datamodel.communication import Request, Response
def arg_to_dict(f):
"""Converts the first argument to the function to a dict
(assuming it's either a json-encoding string or a Request object)."""
def newf(self, obj, *args, **kwargs):
if isinstance(obj, Request):
obj = obj.as_dict()
elif isinstance(obj, str):
obj = json.loads(obj)
return f(self, obj, *args, **kwargs)
return newf
def PPPTestCase(app):
class _PPPTestCase(TestCase):
config_var = None
......@@ -30,23 +41,32 @@ def PPPTestCase(app):
del self.config_file
super(_PPPTestCase, self).tearDown()
@arg_to_dict
def request(self, obj):
if isinstance(obj, Request):
obj = obj.as_dict()
elif isinstance(obj, str):
obj = json.loads(obj)
j = self.app.post_json('/', obj).json
"""Make a request and return the answers."""
return list(map(Response.from_dict, j))
@arg_to_dict
def assertResponse(self, request, response):
"""Makes a request and asserts the response is the expected one."""
self.assertEqual(self.request(request), response)
@arg_to_dict
def assertResponsesIn(self, request, expected):
"""Makes a request and asserts the response is among a set
of expected ones."""
responses = self.request(request)
self.assertTrue(all(x in expected for x in responses),
'Not all of:\n%r\n are in:\n%r' % (responses, expected))
@arg_to_dict
def assertResponsesCount(self, request, count):
"""Makes a request and asserts the number of responses is
a given number."""
self.assertEqual(len(self.request(request)), count)
def assertStatusInt(self, request, status):
res = self.app.post_json('/', request, status='*')
@arg_to_dict
def assertStatusInt(self, obj, status):
"""Makes a request and asserts the HTTP status code is
the one that is expected."""
res = self.app.post_json('/', obj, status='*')
self.assertEqual(res.status_int, status)
return _PPPTestCase
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment