MAINT: Make exception handling tests compatible between Python 2 and 3

Python 3 removes the `.message` attribute, so use `str` instead.

Also, the divide by zero message has changed slightly between versions,
so just check for the exception type, instead of also checking the message.
This commit is contained in:
Eddie Hebert
2014-01-06 14:15:10 -05:00
parent d06f35623a
commit 9326a732a4
2 changed files with 8 additions and 16 deletions
+5 -16
View File
@@ -57,14 +57,9 @@ class ExceptionTestCase(TestCase):
**self.zipline_test_config
)
with self.assertRaises(ZeroDivisionError) as ctx:
with self.assertRaises(ZeroDivisionError):
output, _ = drain_zipline(self, zipline)
self.assertEqual(
ctx.exception.message,
'integer division or modulo by zero'
)
def test_tranform_exception(self):
exc_tnfm = StatefulTransform(ExceptionTransform)
self.zipline_test_config['transforms'] = [exc_tnfm]
@@ -76,7 +71,7 @@ class ExceptionTestCase(TestCase):
with self.assertRaises(AssertionError) as ctx:
output, _ = drain_zipline(self, zipline)
self.assertEqual(ctx.exception.message,
self.assertEqual(str(ctx.exception),
'An assertion message')
def test_exception_in_handle_data(self):
@@ -96,7 +91,7 @@ class ExceptionTestCase(TestCase):
with self.assertRaises(Exception) as ctx:
output, _ = drain_zipline(self, zipline)
self.assertEqual(ctx.exception.message,
self.assertEqual(str(ctx.exception),
'Algo exception in handle_data')
def test_zerodivision_exception_in_handle_data(self):
@@ -113,12 +108,9 @@ class ExceptionTestCase(TestCase):
**self.zipline_test_config
)
with self.assertRaises(ZeroDivisionError) as ctx:
with self.assertRaises(ZeroDivisionError):
output, _ = drain_zipline(self, zipline)
self.assertEqual(ctx.exception.message,
'integer division or modulo by zero')
def test_set_portfolio(self):
"""
Are we protected against overwriting an algo's portfolio?
@@ -136,8 +128,5 @@ class ExceptionTestCase(TestCase):
**self.zipline_test_config
)
with self.assertRaises(AttributeError) as ctx:
with self.assertRaises(AttributeError):
output, _ = drain_zipline(self, zipline)
self.assertEqual(ctx.exception.message,
"can't set attribute")
+3
View File
@@ -95,6 +95,9 @@ class ExceptionSource(object):
def next(self):
5 / 0
def __next__(self):
5 / 0
class ExceptionTransform(object):