diff options
author | Doug Hellmann <doug@doughellmann.com> | 2015-01-06 17:18:37 -0500 |
---|---|---|
committer | Doug Hellmann <doug@doughellmann.com> | 2015-01-06 17:25:32 -0500 |
commit | 118aeb0ceb10f80a365db6cd724d9d2720b132f9 (patch) | |
tree | 241db3a29c70e465ccb6801355e8952380449bdd | |
parent | 0ce52c22b6929dcb0b5922ab226dbaae6c96dc81 (diff) | |
download | oslo-i18n-118aeb0ceb10f80a365db6cd724d9d2720b132f9.tar.gz |
Add a fixture to let tests manage the lazy flag1.3.0
We do not want to expose the stored lazy flag, but we do want to allow
applications to force it to a certain state and then restore it to its
original test for testing. This new fixture provides a way to do that.
Change-Id: Ica05fd8fc7e687da69d61ea84bf1ceae65db93ce
-rw-r--r-- | oslo_i18n/fixture.py | 24 | ||||
-rw-r--r-- | oslo_i18n/tests/test_fixture.py | 40 |
2 files changed, 62 insertions, 2 deletions
diff --git a/oslo_i18n/fixture.py b/oslo_i18n/fixture.py index 076c708..e62fc8c 100644 --- a/oslo_i18n/fixture.py +++ b/oslo_i18n/fixture.py @@ -16,6 +16,7 @@ import fixtures import six +from oslo_i18n import _lazy from oslo_i18n import _message @@ -63,3 +64,26 @@ class Translation(fixtures.Fixture): """ return six.text_type(msg) + + +class ToggleLazy(fixtures.Fixture): + """Fixture to toggle lazy translation on or off for a test.""" + + def __init__(self, enabled): + """Force lazy translation on or off. + + :param enabled: Flag controlling whether to enable or disable + lazy translation, passed to :func:`~oslo_i18n.enable_lazy`. + :type enabled: bool + """ + super(ToggleLazy, self).__init__() + self._enabled = enabled + self._original_value = _lazy.USE_LAZY + + def setUp(self): + super(ToggleLazy, self).setUp() + self.addCleanup(self._restore_original) + _lazy.enable_lazy(self._enabled) + + def _restore_original(self): + _lazy.enable_lazy(self._original_value) diff --git a/oslo_i18n/tests/test_fixture.py b/oslo_i18n/tests/test_fixture.py index aca994f..b5e7f53 100644 --- a/oslo_i18n/tests/test_fixture.py +++ b/oslo_i18n/tests/test_fixture.py @@ -15,14 +15,15 @@ from oslotest import base as test_base import six +from oslo_i18n import _lazy from oslo_i18n import _message from oslo_i18n import fixture -class FixtureTest(test_base.BaseTestCase): +class TranslationFixtureTest(test_base.BaseTestCase): def setUp(self): - super(FixtureTest, self).setUp() + super(TranslationFixtureTest, self).setUp() self.trans_fixture = self.useFixture(fixture.Translation()) def test_lazy(self): @@ -36,3 +37,38 @@ class FixtureTest(test_base.BaseTestCase): self.assertFalse(isinstance(msg, _message.Message)) self.assertIsInstance(msg, six.text_type) self.assertEqual(msg, u'this is a lazy message') + + +class ToggleLazyFixtureText(test_base.BaseTestCase): + + def test_on_on(self): + _lazy.USE_LAZY = True + f = fixture.ToggleLazy(True) + f.setUp() + self.assertTrue(_lazy.USE_LAZY) + f._restore_original() + self.assertTrue(_lazy.USE_LAZY) + + def test_on_off(self): + _lazy.USE_LAZY = True + f = fixture.ToggleLazy(False) + f.setUp() + self.assertFalse(_lazy.USE_LAZY) + f._restore_original() + self.assertTrue(_lazy.USE_LAZY) + + def test_off_on(self): + _lazy.USE_LAZY = False + f = fixture.ToggleLazy(True) + f.setUp() + self.assertTrue(_lazy.USE_LAZY) + f._restore_original() + self.assertFalse(_lazy.USE_LAZY) + + def test_off_off(self): + _lazy.USE_LAZY = False + f = fixture.ToggleLazy(False) + f.setUp() + self.assertFalse(_lazy.USE_LAZY) + f._restore_original() + self.assertFalse(_lazy.USE_LAZY) |