Skip to content

Instantly share code, notes, and snippets.

@quantumew
Created February 25, 2016 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantumew/0d0e0baaa4b2e15f20fb to your computer and use it in GitHub Desktop.
Save quantumew/0d0e0baaa4b2e15f20fb to your computer and use it in GitHub Desktop.
from tests.route_tester import RouteTester
class ExampleRoute(Base):
def __init__(self, test, test_client):
Base.__init__(self, test, test_client)
self.route = "/example/{variable1}/route"
import flask
import json
class RouteTester(object):
def __init__(self, test, test_client):
self.test_client = test_client
self.test = test
self._endpoint = None
self.params = None
self.status_code = None
self.response = None
self.route = None
@property
def endpoint(self):
if not self._endpoint:
self._endpoint = self.route.format(**self.params)
return self._endpoint
def route_params(self, **params):
"""
Sets the route parameters
Arg:
params(dict): Names and values of route parameters.
Returns:
self(route_tester.base.Base)
"""
self.params = params
return self
def expect(self, status_code, response=None):
"""
Sets the expected status_code and response. To be asserted.
Arg:
status_code(int): status code of response expected.
response(object): expected decoded response.
Returns:
self(route_test.base.Base)
"""
self.status_code = status_code
self.response = response
return self
def get(self, data=None):
"""
Performs a GET request on the test client and asserts the response.
Arg:
data(None or dict): data for GET request.
"""
data = self._encode(data)
response = self.test_client.get(self.endpoint, data=data)
self._assert(response)
def post(self, data):
"""
Performs a POST request on the test client and asserts the response.
Arg:
data(dict): data for POST request.
"""
data = self._encode(data)
response = self.test_client.post(self.endpoint, data=data)
self._assert(response)
def put(self, data):
"""
Performs a PUT request on the test client and asserts the response.
Arg:
data(dict): data for PUT request.
"""
data = self._encode(data)
response = self.test_client.put(self.endpoint, data=data)
self._assert(response)
def delete(self, data=None):
"""
Performs a DELETE request on the test client and asserts the response.
Arg:
data(None or dict): data for DELETE request.
"""
data = self._encode(data)
response = self.test_client.delete(self.endpoint, data=data)
self._assert(response)
def _assert(self, actual_response):
if self.status_code is not None:
self.test.assertEqual(self.status_code, actual_response.status_code)
if self.response is not None:
actual = json.loads(actual_response.data)
self.test.assertEqual(self.response, actual)
def _encode(self, data):
if data is not None:
data = json.dumps(data)
return data
from tests.example_route import ExampleRoute
class Tester(object):
def __init__(self, test, test_client):
self.test = test
self.test_client = test_client
def example_route(self):
return ExampleRoute(self.test, self.test_client)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment