From c88fbd6c7551cd6bafc273e4538338f6f58495a0 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Sat, 17 Sep 2011 11:13:15 -0700 Subject: [PATCH] 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. --- analytical/tests/test_utils.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/analytical/tests/test_utils.py b/analytical/tests/test_utils.py index c0aceca..8225cee 100644 --- a/analytical/tests/test_utils.py +++ b/analytical/tests/test_utils.py @@ -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):