From a9d8efcf6cd74b3d2a75b10b26b45296a318504e Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Mon, 11 Aug 2014 19:12:32 -0400 Subject: [PATCH] Add a test fixture for translatable strings Change-Id: I96aec806392672ff69a3af6c3e0a04a621656f78 --- doc/source/api.rst | 5 ++++ oslo/i18n/fixture.py | 65 +++++++++++++++++++++++++++++++++++++++++++ tests/test_fixture.py | 38 +++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 oslo/i18n/fixture.py create mode 100644 tests/test_fixture.py diff --git a/doc/source/api.rst b/doc/source/api.rst index 441cde4..4c46c62 100644 --- a/doc/source/api.rst +++ b/doc/source/api.rst @@ -31,3 +31,8 @@ oslo.i18n.log .. automodule:: oslo.i18n.log :members: +oslo.i18n.fixture +================= + +.. automodule:: oslo.i18n.fixture + :members: diff --git a/oslo/i18n/fixture.py b/oslo/i18n/fixture.py new file mode 100644 index 0000000..4a91a2f --- /dev/null +++ b/oslo/i18n/fixture.py @@ -0,0 +1,65 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +"""Test fixtures for working with oslo.i18n. + +""" + +import fixtures +import six + +from oslo.i18n import _message + + +class Translation(fixtures.Fixture): + """Fixture for managing translatable strings. + + This class provides methods for creating translatable strings + using both lazy translation and immediate translation. It can be + used to generate the different types of messages returned from + oslo.i18n to test code that may need to know about the type to + handle them differently (for example, error handling in WSGI apps, + or logging). + + Use this class to generate messages instead of toggling the global + lazy flag and using the regular translation factory. + + """ + + def __init__(self, domain='test-domain'): + """Initialize the fixture. + + :param domain: The translation domain. This is not expected to + coincide with an actual set of message + catalogs, but it can. + :type domain: str + """ + self.domain = domain + + def lazy(self, msg): + """Return a lazily translated message. + + :param msg: Input message string. May optionally include + positional or named string interpolation markers. + :type msg: str or unicode + + """ + return _message.Message(msg, domain=self.domain) + + def immediate(self, msg): + """Return a string as though it had been translated immediately. + + :param msg: Input message string. May optionally include + positional or named string interpolation markers. + :type msg: str or unicode + + """ + return six.text_type(msg) diff --git a/tests/test_fixture.py b/tests/test_fixture.py new file mode 100644 index 0000000..66f9994 --- /dev/null +++ b/tests/test_fixture.py @@ -0,0 +1,38 @@ +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from oslotest import base as test_base +import six + +from oslo.i18n import _message +from oslo.i18n import fixture + + +class FixtureTest(test_base.BaseTestCase): + + def setUp(self): + super(FixtureTest, self).setUp() + self.trans_fixture = self.useFixture(fixture.Translation()) + + def test_lazy(self): + msg = self.trans_fixture.lazy('this is a lazy message') + self.assertIsInstance(msg, _message.Message) + self.assertEqual(msg.msgid, 'this is a lazy message') + + def test_immediate(self): + msg = self.trans_fixture.immediate('this is a lazy message') + # Python 2.6 does not have assertNotIsInstance + self.assertFalse(isinstance(msg, _message.Message)) + self.assertIsInstance(msg, six.text_type) + self.assertEqual(msg, u'this is a lazy message')