1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
# vim: tabstop=4 expandtab autoindent shiftwidth=4 fileencoding=utf-8
"""
Provides Nose and Django test case assert functions
"""
from django.test.testcases import TransactionTestCase
from django.core import mail
import re
## Python
from nose import tools
for t in dir(tools):
if t.startswith('assert_'):
vars()[t] = getattr(tools, t)
## Django
caps = re.compile('([A-Z])')
def pep8(name):
return caps.sub(lambda m: '_' + m.groups()[0].lower(), name)
class Dummy(TransactionTestCase):
def nop():
pass
_t = Dummy('nop')
for at in [ at for at in dir(_t)
if at.startswith('assert') and not '_' in at ]:
pepd = pep8(at)
vars()[pepd] = getattr(_t, at)
del Dummy
del _t
del pep8
## New
def assert_code(response, status_code, msg_prefix=''):
"""Asserts the response was returned with the given status code
"""
if msg_prefix:
msg_prefix = '%s: ' % msg_prefix
assert response.status_code == status_code, \
'Response code was %d (expected %d)' % \
(response.status_code, status_code)
def assert_ok(response, msg_prefix=''):
"""Asserts the response was returned with status 200 (OK)
"""
return assert_code(response, 200, msg_prefix=msg_prefix)
def assert_mail_count(count, msg=None):
"""Assert the number of emails sent.
The message here tends to be long, so allow for replacing the whole
thing instead of prefixing.
"""
if msg is None:
msg = ', '.join([e.subject for e in mail.outbox])
msg = '%d != %d %s' % (len(mail.outbox), count, msg)
assert_equals(len(mail.outbox), count, msg)
# EOF
|