From 9581cd0206c708bc47dd8bee914c6e49262c00f0 Mon Sep 17 00:00:00 2001 From: Brant Knudson Date: Wed, 13 Apr 2016 08:35:46 -0500 Subject: [PATCH] Remove mockpatch re-implementations Rather than carry implementations of the moved classes in mockpatch, just reference the symbols. Less maintenance for us. The tests are removed since fixtures has its own tests. Change-Id: Iab382a604321e09a01c3d795cd7bcd480dc249ce --- oslotest/mockpatch.py | 74 ++++----------------------- oslotest/tests/unit/test_mockpatch.py | 55 +++----------------- 2 files changed, 17 insertions(+), 112 deletions(-) diff --git a/oslotest/mockpatch.py b/oslotest/mockpatch.py index c4f9810..5ddd485 100644 --- a/oslotest/mockpatch.py +++ b/oslotest/mockpatch.py @@ -14,76 +14,22 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +"""Deprecated. + +This module is deprecated since version 1.13 and may be removed in version 2.0. +Use fixtures.Mock* classes instead. + +""" from debtcollector import removals import fixtures -from six.moves import mock removals.removed_module("oslotest.mockpatch", replacement="fixtures", version="1.13", removal_version="2.0", - message="Use fixtures.Mock* classes instead") + message="Use fixtures.Mock* classes instead.") -class _Base(fixtures.Fixture): - def setUp(self): - super(_Base, self).setUp() - _p = self._get_p() - self.addCleanup(_p.stop) - self.mock = _p.start() - - -class PatchObject(_Base): - """Deal with code around :func:`mock.patch.object`. - - .. py:attribute:: mock - - The mock as returned by :func:`mock.patch.object`. - - """ - - def __init__(self, obj, attr, new=mock.DEFAULT, **kwargs): - super(PatchObject, self).__init__() - self._get_p = lambda: mock.patch.object(obj, attr, new, **kwargs) - - -class Patch(_Base): - """Deal with code around :func:`mock.patch`. - - .. py:attribute:: mock - - The mock as returned by :func:`mock.patch`. - - """ - - def __init__(self, obj, new=mock.DEFAULT, **kwargs): - super(Patch, self).__init__() - self._get_p = lambda: mock.patch(obj, new, **kwargs) - - -class Multiple(_Base): - """Deal with code around :func:`mock.patch.multiple`. - - Pass name=value to replace obj.name with value. - - Pass name= :attr:`.DEFAULT` to replace obj.name with a - :class:`mock.MagicMock` instance. - - :param obj: Object or name containing values being mocked. - :type obj: str or object - :param kwargs: names and values of attributes of obj to be mocked. - - .. py:attribute:: mock - - A :class:`dict`, where each key matches a kwarg parameter and the value - is the passed-in value or :class:`mock.MagicMock`. - - """ - - DEFAULT = mock.DEFAULT - """Triggers a :class:`mock.MagicMock` to be created for the named - attribute.""" - - def __init__(self, obj, **kwargs): - super(Multiple, self).__init__() - self._get_p = lambda: mock.patch.multiple(obj, **kwargs) +PatchObject = fixtures.MockPatchObject +Patch = fixtures.MockPatch +Multiple = fixtures.MockPatchMultiple diff --git a/oslotest/tests/unit/test_mockpatch.py b/oslotest/tests/unit/test_mockpatch.py index fb3a476..dc1c247 100644 --- a/oslotest/tests/unit/test_mockpatch.py +++ b/oslotest/tests/unit/test_mockpatch.py @@ -13,55 +13,14 @@ # under the License. -from six.moves import mock - from oslotest import base from oslotest import mockpatch -class Foo(object): - def bar(self): - pass - - -def mocking_bar(self): - return 'mocked!' - - -class TestMockPatch(base.BaseTestCase): - def test_mock_patch_with_replacement(self): - self.useFixture(mockpatch.Patch('%s.Foo.bar' % (__name__), - mocking_bar)) - instance = Foo() - self.assertEqual(instance.bar(), 'mocked!') - - def test_mock_patch_without_replacement(self): - self.useFixture(mockpatch.Patch('%s.Foo.bar' % (__name__))) - instance = Foo() - self.assertIsInstance(instance.bar(), mock.MagicMock) - - -class TestMockMultiple(base.BaseTestCase): - def test_mock_multiple_with_replacement(self): - self.useFixture(mockpatch.Multiple('%s.Foo' % (__name__), - bar=mocking_bar)) - instance = Foo() - self.assertEqual(instance.bar(), 'mocked!') - - def test_mock_patch_without_replacement(self): - self.useFixture(mockpatch.Multiple('%s.Foo' % (__name__), - bar=mockpatch.Multiple.DEFAULT)) - instance = Foo() - self.assertIsInstance(instance.bar(), mock.MagicMock) - - -class TestMockPatchObject(base.BaseTestCase): - def test_mock_patch_object_with_replacement(self): - self.useFixture(mockpatch.PatchObject(Foo, 'bar', mocking_bar)) - instance = Foo() - self.assertEqual(instance.bar(), 'mocked!') - - def test_mock_patch_object_without_replacement(self): - self.useFixture(mockpatch.PatchObject(Foo, 'bar')) - instance = Foo() - self.assertIsInstance(instance.bar(), mock.MagicMock) +class TestMockPatchSymbols(base.BaseTestCase): + def test_reference(self): + # Applications expect these public symbols to be available until the + # deprecated module is removed. + self.assertTrue(mockpatch.PatchObject) + self.assertTrue(mockpatch.Patch) + self.assertTrue(mockpatch.Multiple)