Skip to content

Commit

Permalink
More delicious code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Arcangeli committed Jan 3, 2014
1 parent bf22b37 commit ac580fd
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Pyramid Duh
===========
:Build: |build|_ |coverage|_
:Documentation: http://pyramid_duh.readthedocs.org/en/latest/
:Source: https://github.com/stevearc/pyramid_duh

.. |build| image:: https://travis-ci.org/stevearc/pyramid_duh.png?branch=master
Expand Down
2 changes: 1 addition & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pyramid Duh - Tools you Want
============================

.. toctree::
:maxdepth: 2
:maxdepth: 1
:glob:

topics/request_parameters
Expand Down
5 changes: 3 additions & 2 deletions doc/topics/subpath.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,16 @@ to get it that indexing the subpath?

.. code-block:: python
@view_config(context=MyCtxt, name='foobar', subpath=('post', 'id/\*'))
@view_config(context=MyCtxt, name='foobar', subpath=('post', 'id/*'))
def my_view(request):
id = request.named_subpaths['id']
# do things
Ooooooooooooooooooooooo

Yeah, and it does PCRE as well. In case you need that. Check out the docs on
:meth:`~pyramid_duh.view.match` for details on matching and flags.
:class:`~pyramid_duh.view.SubpathPredicate` for all of the formats, and
:meth:`~pyramid_duh.view.match` for details on match flags.

How Does I Do?
--------------
Expand Down
70 changes: 67 additions & 3 deletions pyramid_duh/tests/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import json
import unittest
from pyramid.httpexceptions import HTTPBadRequest
from mock import MagicMock
from pyramid.testing import DummyRequest
from pyramid_duh.compat import is_bytes, is_string
from pyramid_duh.params import argify, _param
from pyramid_duh.compat import is_bytes, is_string, string_type
from pyramid_duh.params import argify, _param, includeme


class ParamContainer(object):
Expand Down Expand Up @@ -75,6 +76,21 @@ def test_unicode_json_body(self):
self.assertEquals(field, 'myfield')
self.assertTrue(is_string(field, strict=True))

def test_unicode_param_explicit(self):
""" Specifying type=unicode checks arg type before returning it """
request = DummyRequest()
request.params = {'field': 'myfield'}
field = _param(request, 'field', type=string_type)
self.assertEquals(field, 'myfield')
self.assertTrue(is_string(field, strict=True))

def test_unicode_param_bad_type(self):
""" Raise exception if unicode param as incorrect type """
request = DummyRequest()
request.params = {'field': 4}
with self.assertRaises(HTTPBadRequest):
_param(request, 'field', type=string_type)

def test_str_param(self):
""" Pull binary string param off of request object """
request = DummyRequest()
Expand Down Expand Up @@ -140,6 +156,13 @@ def test_dict_json_body(self):
field = _param(request, 'field', type=dict)
self.assertEquals(field, {'a': 'b'})

def test_dict_param_bad_type(self):
""" If dict param isn't a dict, raise exception """
request = DummyRequest()
request.params = {'field': json.dumps(['a', 'b'])}
with self.assertRaises(HTTPBadRequest):
_param(request, 'field', type=dict)

def test_set_param(self):
""" Pull encoded sets off of request object """
request = DummyRequest()
Expand Down Expand Up @@ -174,6 +197,14 @@ def test_datetime_json_body(self):
field = _param(request, 'field', type=datetime)
self.assertEquals(time.mktime(field.timetuple()), now)

def test_timedelta_param(self):
""" Pull timedelta off of request object """
request = DummyRequest()
diff = 3600
request.params = {'field': diff}
field = _param(request, 'field', type=datetime.timedelta)
self.assertEquals(field, datetime.timedelta(seconds=diff))

def test_date_param(self):
""" Pull date off of request object as YYYY-mm-dd """
request = DummyRequest()
Expand Down Expand Up @@ -263,9 +294,21 @@ def test_object_param_simple(self):
self.assertEqual(field.alpha, data['alpha'])
self.assertEqual(field.beta, data['beta'])

def test_bad_format(self):
""" Raise exception if any error during type conversion """
request = DummyRequest()
request.params = {'field': 'abc'}
with self.assertRaises(HTTPBadRequest):
_param(request, 'field', type=int)

# pylint: disable=E1120,W0613,C0111
def test_include(self):
""" Including pyramid_duh.params should add param() as a req method """
config = MagicMock()
includeme(config)
config.add_request_method.assert_called_with(_param, name='param')


# pylint: disable=E1120,W0613,C0111
class TestArgify(unittest.TestCase):

""" Tests for the argify decorator """
Expand Down Expand Up @@ -303,6 +346,16 @@ def req(request, field='myfield'):
request.json_body = {}
req(context, request)

def test_default_override(self):
""" If keyword arg is present, use that value """
@argify
def req(request, field='myfield'):
self.assertEquals(field, 'otherfield')
context = object()
request = DummyRequest()
request.params = {'field': 'otherfield'}
req(context, request)

def test_bool(self):
""" Pull bool from request automatically """
@argify(field=bool)
Expand Down Expand Up @@ -367,3 +420,14 @@ def req(request, f1, f2=None, **kwargs):
'foobar': 'baz',
}
req(context, request)

def test_pass_context(self):
""" argify will pass on context, request arguments to view """
@argify
def base_req(context, request, field):
return context, request, field
context = object()
request = DummyRequest()
request.params = {'field': 'myfield'}
val = base_req(context, request)
self.assertEquals(val, (context, request, 'myfield'))
26 changes: 20 additions & 6 deletions pyramid_duh/tests/test_view.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# encoding: utf-8
""" Tests for view utilities """
from pyramid.httpexceptions import HTTPFound
import unittest
from mock import MagicMock
from pyramid.httpexceptions import HTTPFound
from pyramid.testing import DummyRequest
from pyramid_duh.view import SubpathPredicate, addslash
from pyramid_duh.params import argify
from pyramid_duh.view import SubpathPredicate, addslash, includeme


class TestSubpath(unittest.TestCase):
Expand Down Expand Up @@ -80,7 +81,7 @@ def test_named_subpaths_pcre_group(self):
'value': 'bar',
})

def test_pcre_flag_a(self):
def test_pcre_flag_a(self): # pragma: no cover
""" Can match ascii-only """
import re
if not hasattr(re, 'A'):
Expand Down Expand Up @@ -110,17 +111,30 @@ def test_optional_subpaths(self):
self.assertTrue(result)
self.assertTrue('opt' in self.request.named_subpaths)

# pylint: disable=C0111,E1101,E1121
def test_non_optional_subpaths(self):
""" If subpath not marked as optional, it is mandatory """
matcher = SubpathPredicate(('*', '*'), None)
self.request.subpath = ('foo',)
result = matcher(None, self.request)
self.assertFalse(result)

def test_include(self):
""" Including pyramid_duh.view adds the predicate method """
config = MagicMock()
includeme(config)
config.add_view_predicate.assert_called_with('subpath',
SubpathPredicate)


# pylint: disable=C0111,E1101,E1121
class TestAddslash(unittest.TestCase):

""" Tests for @addslash """

def test_addslash_redirect(self):
""" addslash causes redirect if path_url doesn't end in / """
@addslash
def myview(request):
def myview(request): # pragma: no cover
return 'foobar'
context = object()
request = DummyRequest()
Expand All @@ -132,7 +146,7 @@ def myview(request):
def test_addslash_redirect_query(self):
""" addslash keeps the query string """
@addslash
def myview(request):
def myview(request): # pragma: no cover
return 'foobar'
context = object()
request = DummyRequest()
Expand Down

0 comments on commit ac580fd

Please sign in to comment.