Skip to content

Commit

Permalink
Remove hard requirement for py.test by vendorizing assertRaises
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Sep 29, 2014
1 parent 826fa32 commit ae338e0
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
10 changes: 5 additions & 5 deletions disqusapi/tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import mock
import os
import pytest
import socket
import unittest

import disqusapi
from disqusapi.compat import xrange
from disqusapi.tests_compat import TestCase

extra_interface = {
"reserved": {
Expand Down Expand Up @@ -70,7 +69,7 @@ def read(self):
return self.body


class DisqusAPITest(unittest.TestCase):
class DisqusAPITest(TestCase):
API_SECRET = 'b' * 64
API_PUBLIC = 'c' * 64
HOST = os.environ.get('DISQUS_API_HOST', disqusapi.HOST)
Expand Down Expand Up @@ -156,17 +155,18 @@ def test_endpoint(self):

def test_update_interface_legacy(self):
api = disqusapi.DisqusAPI(self.API_SECRET, self.API_PUBLIC)
with pytest.raises(disqusapi.InterfaceNotDefined):
with self.assertRaises(disqusapi.InterfaceNotDefined):
api.interface.update(extra_interface)

def test_invalid_method(self):
api = disqusapi.DisqusAPI(self.API_SECRET, self.API_PUBLIC)
with pytest.raises(disqusapi.InvalidHTTPMethod):
with self.assertRaises(disqusapi.InvalidHTTPMethod):
api.get('posts.list', method='lol', forum='disqus')

def test_update_interface(self):
api = disqusapi.DisqusAPI(self.API_SECRET, self.API_PUBLIC)
api.update_interface(extra_interface)

if __name__ == '__main__':
import unittest
unittest.main()
90 changes: 90 additions & 0 deletions disqusapi/tests_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import unittest
import sys

if sys.version_info < (2, 7):
# Stolen from unittest2
import re

class _AssertRaisesBaseContext(object):

def __init__(self, expected, test_case, callable_obj=None,
expected_regex=None):
self.expected = expected
self.failureException = test_case.failureException
if callable_obj is not None:
try:
self.obj_name = callable_obj.__name__
except AttributeError:
self.obj_name = str(callable_obj)
else:
self.obj_name = None
if isinstance(expected_regex, basestring):
expected_regex = re.compile(expected_regex)
self.expected_regex = expected_regex

class _AssertRaisesContext(_AssertRaisesBaseContext):
"""A context manager used to implement TestCase.assertRaises* methods."""

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, tb):
if exc_type is None:
try:
exc_name = self.expected.__name__
except AttributeError:
exc_name = str(self.expected)
raise self.failureException(
"%s not raised" % (exc_name,))
if not issubclass(exc_type, self.expected):
# let unexpected exceptions pass through
return False
self.exception = exc_value # store for later retrieval
if self.expected_regex is None:
return True

expected_regex = self.expected_regex
if not expected_regex.search(str(exc_value)):
raise self.failureException(
'%r does not match %r' %
(expected_regex.pattern, str(exc_value)))
return True

class TestCase(unittest.TestCase):
def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
"""Fail unless an exception of class excClass is thrown
by callableObj when invoked with arguments args and keyword
arguments kwargs. If a different type of exception is
thrown, it will not be caught, and the test case will be
deemed to have suffered an error, exactly as for an
unexpected exception.
If called with callableObj omitted or None, will return a
context object used like this::
with self.assertRaises(SomeException):
do_something()
The context manager keeps a reference to the exception as
the 'exception' attribute. This allows you to inspect the
exception after the assertion::
with self.assertRaises(SomeException) as cm:
do_something()
the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)
"""
if callableObj is None:
return _AssertRaisesContext(excClass, self)
try:
callableObj(*args, **kwargs)
except excClass:
return

if hasattr(excClass, '__name__'):
excName = excClass.__name__
else:
excName = str(excClass)
raise self.failureException("%s not raised" % excName)
else:
TestCase = unittest.TestCase

0 comments on commit ae338e0

Please sign in to comment.