Use mock rather than mox

Switch to mock rather than mox. We should pick one or the other
mocking library, and mock is preferred.

Change-Id: I86ad9638da2f53189fbaea3fd9476356eb0c7ff5
This commit is contained in:
Brant Knudson
2015-06-13 08:17:20 -05:00
parent 75d4b16eaf
commit f249332bb6
3 changed files with 42 additions and 34 deletions

View File

@@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from oslotest import mockpatch
from keystoneclient import base from keystoneclient import base
from keystoneclient.tests.unit import utils from keystoneclient.tests.unit import utils
from keystoneclient.v2_0 import client from keystoneclient.v2_0 import client
@@ -40,9 +42,9 @@ class BaseTest(utils.TestCase):
auth_url='http://127.0.0.1:5000', auth_url='http://127.0.0.1:5000',
endpoint='http://127.0.0.1:5000') endpoint='http://127.0.0.1:5000')
self.client._adapter.get = self.mox.CreateMockAnything() self.useFixture(mockpatch.PatchObject(
self.client._adapter.get('/OS-KSADM/roles/1').AndRaise(AttributeError) self.client._adapter, 'get', side_effect=AttributeError,
self.mox.ReplayAll() autospec=True))
f = roles.Role(self.client.roles, {'id': 1, 'name': 'Member'}) f = roles.Role(self.client.roles, {'id': 1, 'name': 'Member'})
self.assertEqual(f.name, 'Member') self.assertEqual(f.name, 'Member')
@@ -95,67 +97,78 @@ class ManagerTest(utils.TestCase):
self.assertEqual(self.mgr.api, self.client) self.assertEqual(self.mgr.api, self.client)
def test_get(self): def test_get(self):
self.client.get = self.mox.CreateMockAnything() get_mock = self.useFixture(mockpatch.PatchObject(
self.client.get(self.url).AndReturn((None, self.body)) self.client, 'get', autospec=True, return_value=(None, self.body))
self.mox.ReplayAll() ).mock
rsrc = self.mgr._get(self.url, "hello") rsrc = self.mgr._get(self.url, "hello")
get_mock.assert_called_once_with(self.url)
self.assertEqual(rsrc.hi, 1) self.assertEqual(rsrc.hi, 1)
def test_post(self): def test_post(self):
self.client.post = self.mox.CreateMockAnything() post_mock = self.useFixture(mockpatch.PatchObject(
self.client.post(self.url, body=self.body).AndReturn((None, self.body)) self.client, 'post', autospec=True, return_value=(None, self.body))
self.client.post(self.url, body=self.body).AndReturn((None, self.body)) ).mock
self.mox.ReplayAll()
rsrc = self.mgr._post(self.url, self.body, "hello") rsrc = self.mgr._post(self.url, self.body, "hello")
post_mock.assert_called_once_with(self.url, body=self.body)
self.assertEqual(rsrc.hi, 1) self.assertEqual(rsrc.hi, 1)
post_mock.reset_mock()
rsrc = self.mgr._post(self.url, self.body, "hello", return_raw=True) rsrc = self.mgr._post(self.url, self.body, "hello", return_raw=True)
post_mock.assert_called_once_with(self.url, body=self.body)
self.assertEqual(rsrc["hi"], 1) self.assertEqual(rsrc["hi"], 1)
def test_put(self): def test_put(self):
self.client.put = self.mox.CreateMockAnything() put_mock = self.useFixture(mockpatch.PatchObject(
self.client.put(self.url, body=self.body).AndReturn((None, self.body)) self.client, 'put', autospec=True, return_value=(None, self.body))
self.client.put(self.url, body=self.body).AndReturn((None, self.body)) ).mock
self.mox.ReplayAll()
rsrc = self.mgr._put(self.url, self.body, "hello") rsrc = self.mgr._put(self.url, self.body, "hello")
put_mock.assert_called_once_with(self.url, body=self.body)
self.assertEqual(rsrc.hi, 1) self.assertEqual(rsrc.hi, 1)
put_mock.reset_mock()
rsrc = self.mgr._put(self.url, self.body) rsrc = self.mgr._put(self.url, self.body)
put_mock.assert_called_once_with(self.url, body=self.body)
self.assertEqual(rsrc.hello["hi"], 1) self.assertEqual(rsrc.hello["hi"], 1)
def test_patch(self): def test_patch(self):
self.client.patch = self.mox.CreateMockAnything() patch_mock = self.useFixture(mockpatch.PatchObject(
self.client.patch(self.url, body=self.body).AndReturn( self.client, 'patch', autospec=True,
(None, self.body)) return_value=(None, self.body))
self.client.patch(self.url, body=self.body).AndReturn( ).mock
(None, self.body))
self.mox.ReplayAll()
rsrc = self.mgr._patch(self.url, self.body, "hello") rsrc = self.mgr._patch(self.url, self.body, "hello")
patch_mock.assert_called_once_with(self.url, body=self.body)
self.assertEqual(rsrc.hi, 1) self.assertEqual(rsrc.hi, 1)
patch_mock.reset_mock()
rsrc = self.mgr._patch(self.url, self.body) rsrc = self.mgr._patch(self.url, self.body)
patch_mock.assert_called_once_with(self.url, body=self.body)
self.assertEqual(rsrc.hello["hi"], 1) self.assertEqual(rsrc.hello["hi"], 1)
def test_update(self): def test_update(self):
self.client.patch = self.mox.CreateMockAnything() patch_mock = self.useFixture(mockpatch.PatchObject(
self.client.put = self.mox.CreateMockAnything() self.client, 'patch', autospec=True,
self.client.patch( return_value=(None, self.body))
self.url, body=self.body, management=False).AndReturn((None, ).mock
self.body))
self.client.put(self.url, body=None, management=True).AndReturn( put_mock = self.useFixture(mockpatch.PatchObject(
(None, self.body)) self.client, 'put', autospec=True, return_value=(None, self.body))
self.mox.ReplayAll() ).mock
rsrc = self.mgr._update( rsrc = self.mgr._update(
self.url, body=self.body, response_key="hello", method="PATCH", self.url, body=self.body, response_key="hello", method="PATCH",
management=False) management=False)
patch_mock.assert_called_once_with(
self.url, management=False, body=self.body)
self.assertEqual(rsrc.hi, 1) self.assertEqual(rsrc.hi, 1)
rsrc = self.mgr._update( rsrc = self.mgr._update(
self.url, body=None, response_key="hello", method="PUT", self.url, body=None, response_key="hello", method="PUT",
management=True) management=True)
put_mock.assert_called_once_with(self.url, management=True, body=None)
self.assertEqual(rsrc.hi, 1) self.assertEqual(rsrc.hi, 1)

View File

@@ -17,7 +17,6 @@ import uuid
import fixtures import fixtures
import mock import mock
from mox3 import mox
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
import requests import requests
from requests_mock.contrib import fixture from requests_mock.contrib import fixture
@@ -43,7 +42,6 @@ class TestCase(testtools.TestCase):
def setUp(self): def setUp(self):
super(TestCase, self).setUp() super(TestCase, self).setUp()
self.mox = mox.Mox()
self.logger = self.useFixture(fixtures.FakeLogger(level=logging.DEBUG)) self.logger = self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
self.time_patcher = mock.patch.object(time, 'time', lambda: 1234) self.time_patcher = mock.patch.object(time, 'time', lambda: 1234)
self.time_patcher.start() self.time_patcher.start()
@@ -52,8 +50,6 @@ class TestCase(testtools.TestCase):
def tearDown(self): def tearDown(self):
self.time_patcher.stop() self.time_patcher.stop()
self.mox.UnsetStubs()
self.mox.VerifyAll()
super(TestCase, self).tearDown() super(TestCase, self).tearDown()
def stub_url(self, method, parts=None, base_url=None, json=None, **kwargs): def stub_url(self, method, parts=None, base_url=None, json=None, **kwargs):

View File

@@ -10,7 +10,6 @@ fixtures>=0.3.14
keyring>=2.1,!=3.3 keyring>=2.1,!=3.3
lxml>=2.3 lxml>=2.3
mock>=1.0 mock>=1.0
mox3>=0.7.0
oauthlib>=0.6 oauthlib>=0.6
oslosphinx>=2.5.0 # Apache-2.0 oslosphinx>=2.5.0 # Apache-2.0
oslotest>=1.5.1 # Apache-2.0 oslotest>=1.5.1 # Apache-2.0