From fc2d39034b7ed3a5afbb6467302ae1cd431d57ad Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Wed, 19 Nov 2014 11:58:20 -0500 Subject: [PATCH] Add ClearRequestContext fixture Add a fixture class for use in tests to provide a way to flush the cached RequestContext without exposing how that is stored. bp/graduate-oslo-context Change-Id: I27dd50a61d364e1698823629e2676c10ced50af3 --- doc/source/api/autoindex.rst | 1 + doc/source/api/oslo_context.fixture.rst | 7 ++++++ oslo_context/fixture.py | 30 +++++++++++++++++++++++++ oslo_context/tests/test_context.py | 11 ++------- oslo_context/tests/test_fixture.py | 30 +++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 doc/source/api/oslo_context.fixture.rst create mode 100644 oslo_context/fixture.py create mode 100644 oslo_context/tests/test_fixture.py diff --git a/doc/source/api/autoindex.rst b/doc/source/api/autoindex.rst index eaaf790..1d5d077 100644 --- a/doc/source/api/autoindex.rst +++ b/doc/source/api/autoindex.rst @@ -2,4 +2,5 @@ :maxdepth: 1 oslo_context.context.rst + oslo_context.fixture.rst oslo_context.tests.test_context.rst diff --git a/doc/source/api/oslo_context.fixture.rst b/doc/source/api/oslo_context.fixture.rst new file mode 100644 index 0000000..58ef47f --- /dev/null +++ b/doc/source/api/oslo_context.fixture.rst @@ -0,0 +1,7 @@ +The :mod:`oslo_context.fixture` Module +====================================== + +.. automodule:: oslo_context.fixture + :members: + :undoc-members: + :show-inheritance: diff --git a/oslo_context/fixture.py b/oslo_context/fixture.py new file mode 100644 index 0000000..51822e8 --- /dev/null +++ b/oslo_context/fixture.py @@ -0,0 +1,30 @@ +# 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. + +import fixtures + +from oslo_context import context + + +class ClearRequestContext(fixtures.Fixture): + """Clears any cached RequestContext at the end of a test case.""" + + def setUp(self): + super(ClearRequestContext, self).setUp() + self.addCleanup(self._remove_cached_context) + + def _remove_cached_context(self): + """Remove the thread-local context stored in the module.""" + try: + del context._request_store.context + except AttributeError: + pass diff --git a/oslo_context/tests/test_context.py b/oslo_context/tests/test_context.py index 655895d..97e3ca1 100644 --- a/oslo_context/tests/test_context.py +++ b/oslo_context/tests/test_context.py @@ -16,20 +16,14 @@ from oslotest import base as test_base from oslo_context import context +from oslo_context import fixture class ContextTest(test_base.BaseTestCase): def setUp(self): super(ContextTest, self).setUp() - self.addCleanup(self._remove_cached_context) - - def _remove_cached_context(self): - """Remove the thread-local context stored in the module.""" - try: - del context._request_store.context - except AttributeError: - pass + self.useFixture(fixture.ClearRequestContext()) def test_context(self): ctx = context.RequestContext() @@ -57,7 +51,6 @@ class ContextTest(test_base.BaseTestCase): def test_store_current(self): # By default a new context is stored. - self._remove_cached_context() ctx = context.RequestContext() self.assertIs(context.get_current(), ctx) diff --git a/oslo_context/tests/test_fixture.py b/oslo_context/tests/test_fixture.py new file mode 100644 index 0000000..7a5b3cc --- /dev/null +++ b/oslo_context/tests/test_fixture.py @@ -0,0 +1,30 @@ +# 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 + +from oslo_context import context +from oslo_context import fixture + + +class ClearRequestContextTest(test_base.BaseTestCase): + + # def setUp(self): + # super(ContextTest, self).setUp() + # self.useFixture(fixture.ClearRequestContext()) + + def test_store_current(self): + # By default a new context is stored. + ctx = context.RequestContext() + self.assertIs(context.get_current(), ctx) + fixture.ClearRequestContext()._remove_cached_context() + self.assertIsNone(context.get_current())