Merge lp:~matiasb/u1-test-utils/improved-django-patch-settings into lp:u1-test-utils

Proposed by Matias Bordese
Status: Merged
Approved by: Natalia Bidart
Approved revision: 40
Merged at revision: 38
Proposed branch: lp:~matiasb/u1-test-utils/improved-django-patch-settings
Merge into: lp:u1-test-utils
Diff against target: 107 lines (+73/-9)
2 files modified
u1testutils/django.py (+28/-9)
u1testutils/selftests/unit/test_django.py (+45/-0)
To merge this branch: bzr merge lp:~matiasb/u1-test-utils/improved-django-patch-settings
Reviewer Review Type Date Requested Status
Natalia Bidart (community) Approve
Leo Arias (community) code review Approve
Review via email: mp+154418@code.launchpad.net

Commit message

Improved django patch_settings to provide start/stop methods.

Description of the change

Improved django patch_settings to provide start/stop methods (taken from SSO).

To post a comment you must log in.
39. By Matias Bordese

Added tests.

Revision history for this message
Leo Arias (elopio) wrote :

love it. Thanks!

review: Approve (code review)
Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Looks great, but a test is missing: when the setting being patched is not present (ie, the setting should not be set after the test)

review: Needs Fixing
40. By Matias Bordese

Added additional test.

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Looks great!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'u1testutils/django.py'
2--- u1testutils/django.py 2012-12-04 19:56:54 +0000
3+++ u1testutils/django.py 2013-03-20 17:16:24 +0000
4@@ -3,7 +3,6 @@
5 import re
6 import string
7 import time
8-from contextlib import contextmanager
9
10 from django.conf import settings
11 from django.test import TestCase
12@@ -29,17 +28,37 @@
13
14 return old_settings
15
16-
17-@contextmanager
18-def patch_settings(**kwargs):
19- old_settings = switch_settings(**kwargs)
20- try:
21- yield
22- finally:
23- switch_settings(**old_settings)
24 # end snippet
25
26
27+class patch_settings(object):
28+
29+ def __init__(self, **kwargs):
30+ super(patch_settings, self).__init__()
31+ self.marker = object()
32+ self.old_settings = {}
33+ self.kwargs = kwargs
34+
35+ def start(self):
36+ for setting, new_value in self.kwargs.items():
37+ old_value = getattr(settings, setting, self.marker)
38+ self.old_settings[setting] = old_value
39+ setattr(settings, setting, new_value)
40+
41+ def stop(self):
42+ for setting, old_value in self.old_settings.items():
43+ if old_value is self.marker:
44+ delattr(settings, setting)
45+ else:
46+ setattr(settings, setting, old_value)
47+
48+ def __enter__(self):
49+ self.start()
50+
51+ def __exit__(self, exc_type, exc_val, exc_tb):
52+ self.stop()
53+
54+
55 class CsrfMiddlewareEnabledTestCase(TestCase):
56 def setUp(self):
57 # make sure csrf middleware is enabled
58
59=== added file 'u1testutils/selftests/unit/test_django.py'
60--- u1testutils/selftests/unit/test_django.py 1970-01-01 00:00:00 +0000
61+++ u1testutils/selftests/unit/test_django.py 2013-03-20 17:16:24 +0000
62@@ -0,0 +1,45 @@
63+# -*- coding: utf-8 -*-
64+
65+# Copyright 2013 Canonical Ltd.
66+#
67+# This program is free software: you can redistribute it and/or modify it
68+# under the terms of the GNU Lesser General Public License version 3, as
69+# published by the Free Software Foundation.
70+#
71+# This program is distributed in the hope that it will be useful, but
72+# WITHOUT ANY WARRANTY; without even the implied warranties of
73+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
74+# PURPOSE. See the GNU Lesser General Public License for more details.
75+#
76+# You should have received a copy of the GNU General Public License along
77+# with this program. If not, see <http://www.gnu.org/licenses/
78+
79+import unittest
80+
81+from django.conf import settings
82+
83+from u1testutils.django import patch_settings
84+
85+
86+class PatchSettingsTestCase(unittest.TestCase):
87+
88+ def test_patch_settings_using_as_context_manager(self):
89+ assert not settings.DEBUG
90+ with patch_settings(DEBUG=True):
91+ self.assertTrue(settings.DEBUG)
92+ self.assertFalse(settings.DEBUG)
93+
94+ def test_patch_settings_start_stop(self):
95+ assert not settings.DEBUG
96+ p = patch_settings(DEBUG=True)
97+ p.start()
98+ self.assertTrue(settings.DEBUG)
99+ p.stop()
100+ self.assertFalse(settings.DEBUG)
101+
102+ def test_patch_settings_not_available_setting(self):
103+ marker = object()
104+ assert getattr(settings, 'NOT_AVAILABLE', marker) is marker
105+ with patch_settings(NOT_AVAILABLE=True):
106+ self.assertTrue(settings.NOT_AVAILABLE)
107+ self.assertIs(getattr(settings, 'NOT_AVAILABLE', marker), marker)

Subscribers

People subscribed via source and target branches

to all changes: