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
This commit is contained in:
Doug Hellmann 2014-11-19 11:58:20 -05:00
parent 9152a0d38b
commit fc2d39034b
5 changed files with 70 additions and 9 deletions

View File

@ -2,4 +2,5 @@
:maxdepth: 1
oslo_context.context.rst
oslo_context.fixture.rst
oslo_context.tests.test_context.rst

View File

@ -0,0 +1,7 @@
The :mod:`oslo_context.fixture` Module
======================================
.. automodule:: oslo_context.fixture
:members:
:undoc-members:
:show-inheritance:

30
oslo_context/fixture.py Normal file
View File

@ -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

View File

@ -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)

View File

@ -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())