Switch horizon UT from mox to mock

This completes mock migration in horizon UT.

IsA and IsHttpRequest are moved to horizon.test.helpers
as they are genric enough and IsA is needed in a horizon UT.

Part of blueprint mock-framework-in-unit-tests
Change-Id: I702551fe900224c9ebbbf3ef815206be035b6d14
This commit is contained in:
Akihiro Motoki 2017-12-31 03:20:54 +09:00
parent 73a0bbd43e
commit 2e3e68ad66
3 changed files with 28 additions and 26 deletions

View File

@ -360,3 +360,18 @@ class update_settings(django_test_utils.override_settings):
copied.update(new_value)
kwargs[key] = copied
super(update_settings, self).__init__(**kwargs)
class IsA(object):
"""Class to compare param is a specified class."""
def __init__(self, cls):
self.cls = cls
def __eq__(self, other):
return isinstance(other, self.cls)
class IsHttpRequest(IsA):
"""Class to compare param is django.http.HttpRequest."""
def __init__(self):
super(IsHttpRequest, self).__init__(http.HttpRequest)

View File

@ -27,7 +27,6 @@ from django.urls import reverse
from django.utils.translation import ungettext_lazy
import mock
from mox3.mox import IsA
import six
from horizon import exceptions
@ -390,9 +389,6 @@ class DisabledActionsTable(tables.DataTable):
class DataTableTests(test.TestCase):
use_mox = True
def test_table_instantiation(self):
"""Tests everything that happens when the table is instantiated."""
self.table = MyTable(self.request, TEST_DATA)
@ -1334,17 +1330,21 @@ class DataTableTests(test.TestCase):
req = self.factory.post('/my_url/', {'action': action_string})
self.table = MyTable(req, TEST_DATA)
self.mox.StubOutWithMock(self.table, 'get_object_display')
self.table.get_object_display(IsA(FakeObject)).AndReturn(None)
self.mox.ReplayAll()
with mock.patch.object(
self.table,
'get_object_display',
return_value=None) as mock_get_object_display:
self.assertEqual(('my_table', 'toggle', '1'),
self.table.parse_action(action_string))
handled = self.table.maybe_handle()
self.assertEqual(('my_table', 'toggle', '1'),
self.table.parse_action(action_string))
handled = self.table.maybe_handle()
self.assertEqual(302, handled.status_code)
self.assertEqual("/my_url/", handled["location"])
self.assertEqual(u"Downed Item: 1",
list(req._messages)[0].message)
mock_get_object_display.assert_called_once_with(
test.IsA(FakeObject))
def test_table_column_can_be_selected(self):
self.table = MyTableSelectable(self.request, TEST_DATA_6)

View File

@ -26,7 +26,6 @@ import unittest
from django.conf import settings
from django.contrib.messages.storage import default_storage
from django.core.handlers import wsgi
from django import http as http_request
from django.test.client import RequestFactory
from django import urls
from django.utils import http
@ -66,7 +65,10 @@ LOG = logging.getLogger(__name__)
# Makes output of failing mox tests much easier to read.
wsgi.WSGIRequest.__repr__ = lambda self: "<class 'django.http.HttpRequest'>"
# Shortcuts to avoid importing horizon_helpers and for backward compatibility.
update_settings = horizon_helpers.update_settings
IsA = horizon_helpers.IsA
IsHttpRequest = horizon_helpers.IsHttpRequest
def create_stubs(stubs_to_create=None):
@ -762,18 +764,3 @@ def mock_factory(r):
mocked = mock_obj_to_dict(r)
mocked.configure_mock(**r)
return mocked
class IsA(object):
"""Class to compare param is a specified class."""
def __init__(self, cls):
self.cls = cls
def __eq__(self, other):
return isinstance(other, self.cls)
class IsHttpRequest(IsA):
"""Class to compare param is django.http.HttpRequest."""
def __init__(self):
super(IsHttpRequest, self).__init__(http_request.HttpRequest)