Make SettingDeleted tests compatible with Python 2.6 and Django 1.2

Using assertRaises as a context manager only became possible in Python
2.7 or when using Django 1.3 (thanks to unittest2).  This change lets
the tests pass when using Python 2.6 and Django 1.2.

Also, test for the existence of assertRaisesRegexp and use it if it
exists, otherwise use assertRaises.
This commit is contained in:
Eric Davis 2011-09-17 11:13:15 -07:00
parent 39af53a8c8
commit c88fbd6c75

View file

@ -15,8 +15,7 @@ from analytical.tests.utils import (
class SettingDeletedTestCase(TestCase):
@override_settings(USER_ID=SETTING_DELETED)
def test_deleted_setting_raises_exception(self):
with self.assertRaises(AttributeError):
getattr(settings, "USER_ID")
self.assertRaises(AttributeError, getattr, settings, "USER_ID")
@override_settings(USER_ID=1)
def test_only_disable_within_context_manager(self):
@ -26,8 +25,7 @@ class SettingDeletedTestCase(TestCase):
self.assertEqual(settings.USER_ID, 1)
with override_settings(USER_ID=SETTING_DELETED):
with self.assertRaises(AttributeError):
getattr(settings, "USER_ID")
self.assertRaises(AttributeError, getattr, settings, "USER_ID")
self.assertEqual(settings.USER_ID, 1)
@ -36,9 +34,13 @@ class SettingDeletedTestCase(TestCase):
"""
Make sure using get_required_setting fails in the right place.
"""
with self.assertRaisesRegexp(AnalyticalException, "USER_ID setting: not found"):
user_id = get_required_setting("USER_ID", "\d+", "invalid USER_ID")
# only available with python >= 2.7 or Django >= 1.3
if hasattr(self, 'assertRaisesRegexp'):
with self.assertRaisesRegexp(AnalyticalException, "^USER_ID setting: not found$"):
user_id = get_required_setting("USER_ID", "\d+", "invalid USER_ID")
else:
self.assertRaises(AnalyticalException,
get_required_setting, "USER_ID", "\d+", "invalid USER_ID")
class InternalIpTestCase(TestCase):