Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active August 29, 2015 14:06
Show Gist options
  • Save dogweather/345f738229c2f78a97d5 to your computer and use it in GitHub Desktop.
Save dogweather/345f738229c2f78a97d5 to your computer and use it in GitHub Desktop.
The controller code as originally written, with a small test suite
import pytest
def product_index(request):
limit = request.GET.get('limit')
offset = request.GET.get('offset')
if limit and limit.isdigit():
limit = int(limit)
else:
limit = 10
if offset and offset.isdigit():
offset = int(offset)
else:
offset = 0
return limit, offset
class MockRequest:
def __init__(self):
self.GET = {}
def test_pytest_is_correctly_installed():
assert True == True
def test_default_values_when_no_parameters_given():
request = MockRequest()
(limit, offset) = product_index(request)
assert limit == 10
assert offset == 0
def test_values_coerced_to_int_when_string_params_given():
request = MockRequest()
request.GET['limit'] = '20'
request.GET['offset'] = '50'
(limit, offset) = product_index(request)
assert limit == 20
assert offset == 50
def test_default_returned_when_param_is_unparseable():
request = MockRequest()
request.GET['limit'] = 'qwerty'
request.GET['offset'] = '50'
(limit, offset) = product_index(request)
assert limit == 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment