/
testcase.py
35 lines (26 loc) · 1.33 KB
/
testcase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from djblets.siteconfig.models import SiteConfiguration
from reviewboard.avatars import avatar_services
class AvatarServicesTestMixin(object):
"""A testcase mixin for resetting the state of avatar services.
The avatar service registry class will change the state of the
:py:attr:`SiteConfiguration.settings
<djblets.siteconfig.models.SiteConfiguration>` object, which will not be
automatically undone when the test-case finishes. Instead, we cache the
site configuration settings prior to running any test cases and reset the
site configuration settings to these cached settings after each test.
This mixin should be used in any test case class that deals with avatar
services.
"""
@classmethod
def setUpClass(cls):
"""Cache the SiteConfiguration settings object."""
super(AvatarServicesTestMixin, cls).setUpClass()
siteconfig = SiteConfiguration.objects.get_current()
cls._original_settings = siteconfig.settings.copy()
def tearDown(self):
"""Restore the SiteConfiguration settings object."""
super(AvatarServicesTestMixin, self).tearDown()
avatar_services.reset()
siteconfig = SiteConfiguration.objects.get_current()
siteconfig.settings = self._original_settings.copy()
siteconfig.save(update_fields=('settings',))