From 2e3e68ad669068d3b4f05f6ed3cfc7b4eea22081 Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Sun, 31 Dec 2017 03:20:54 +0900 Subject: [PATCH] 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 --- horizon/test/helpers.py | 15 +++++++++++++++ horizon/test/unit/tables/test_tables.py | 20 ++++++++++---------- openstack_dashboard/test/helpers.py | 19 +++---------------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/horizon/test/helpers.py b/horizon/test/helpers.py index 9a0dadee54..27fd158a43 100644 --- a/horizon/test/helpers.py +++ b/horizon/test/helpers.py @@ -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) diff --git a/horizon/test/unit/tables/test_tables.py b/horizon/test/unit/tables/test_tables.py index 67b0813814..a2ef8b168e 100644 --- a/horizon/test/unit/tables/test_tables.py +++ b/horizon/test/unit/tables/test_tables.py @@ -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) diff --git a/openstack_dashboard/test/helpers.py b/openstack_dashboard/test/helpers.py index 81fa972da8..36a3b7cdac 100644 --- a/openstack_dashboard/test/helpers.py +++ b/openstack_dashboard/test/helpers.py @@ -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: "" +# 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)