From 60bf798db1cb1541337ab2657f251b428f550093 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Tue, 27 Nov 2018 23:31:32 +0500 Subject: [PATCH] Use unittest.mock instead of external module. (#4893) * Use unittest.mock instead of external module. * Avoid assert_called and assert_called_once, for Python <3.6 compatibility --- setup.py | 1 - wagtail/admin/tests/test_contentstate.py | 2 +- wagtail/admin/tests/test_edit_handlers.py | 2 +- wagtail/admin/tests/test_pages_views.py | 2 +- wagtail/api/v2/tests/test_documents.py | 2 +- wagtail/api/v2/tests/test_images.py | 2 +- wagtail/api/v2/tests/test_pages.py | 2 +- wagtail/contrib/frontend_cache/tests.py | 2 +- wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py | 7 ++++--- wagtail/contrib/routable_page/tests.py | 3 ++- wagtail/core/tests/test_rich_text.py | 3 ++- wagtail/documents/tests/test_views.py | 2 +- wagtail/embeds/tests.py | 2 +- wagtail/images/tests/test_image_operations.py | 2 +- wagtail/search/tests/test_elasticsearch2_backend.py | 2 +- wagtail/search/tests/test_elasticsearch5_backend.py | 2 +- wagtail/search/tests/test_elasticsearch6_backend.py | 2 +- wagtail/search/tests/test_index_functions.py | 6 +++--- 18 files changed, 24 insertions(+), 22 deletions(-) diff --git a/setup.py b/setup.py index 05337d36f..153f319fb 100755 --- a/setup.py +++ b/setup.py @@ -40,7 +40,6 @@ install_requires = [ # Testing dependencies testing_extras = [ # Required for running the tests - 'mock>=1.0.0', 'python-dateutil>=2.2', 'pytz>=2014.7', 'elasticsearch>=1.0.0,<3.0', diff --git a/wagtail/admin/tests/test_contentstate.py b/wagtail/admin/tests/test_contentstate.py index 800a63452..3cd6bdef7 100644 --- a/wagtail/admin/tests/test_contentstate.py +++ b/wagtail/admin/tests/test_contentstate.py @@ -1,7 +1,7 @@ import json +from unittest.mock import patch from django.test import TestCase -from mock import patch from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter from wagtail.embeds.models import Embed diff --git a/wagtail/admin/tests/test_edit_handlers.py b/wagtail/admin/tests/test_edit_handlers.py index 46d5a06a6..049b4270d 100644 --- a/wagtail/admin/tests/test_edit_handlers.py +++ b/wagtail/admin/tests/test_edit_handlers.py @@ -1,6 +1,6 @@ from datetime import date +from unittest import mock -import mock from django import forms from django.contrib.auth.models import AnonymousUser from django.core import checks diff --git a/wagtail/admin/tests/test_pages_views.py b/wagtail/admin/tests/test_pages_views.py index 7629497fb..988173402 100644 --- a/wagtail/admin/tests/test_pages_views.py +++ b/wagtail/admin/tests/test_pages_views.py @@ -2,8 +2,8 @@ import datetime import logging import os from itertools import chain +from unittest import mock -import mock from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.models import Group, Permission diff --git a/wagtail/api/v2/tests/test_documents.py b/wagtail/api/v2/tests/test_documents.py index 1756e23f1..5032c58b9 100644 --- a/wagtail/api/v2/tests/test_documents.py +++ b/wagtail/api/v2/tests/test_documents.py @@ -1,6 +1,6 @@ import json +from unittest import mock -import mock from django.test import TestCase from django.test.utils import override_settings from django.urls import reverse diff --git a/wagtail/api/v2/tests/test_images.py b/wagtail/api/v2/tests/test_images.py index 8c4443288..6197425a9 100644 --- a/wagtail/api/v2/tests/test_images.py +++ b/wagtail/api/v2/tests/test_images.py @@ -1,6 +1,6 @@ import json +from unittest import mock -import mock from django.test import TestCase from django.test.utils import override_settings from django.urls import reverse diff --git a/wagtail/api/v2/tests/test_pages.py b/wagtail/api/v2/tests/test_pages.py index 8510b0459..60c2be9a2 100644 --- a/wagtail/api/v2/tests/test_pages.py +++ b/wagtail/api/v2/tests/test_pages.py @@ -1,6 +1,6 @@ import collections import json -import mock +from unittest import mock from django.contrib.contenttypes.models import ContentType from django.test import TestCase diff --git a/wagtail/contrib/frontend_cache/tests.py b/wagtail/contrib/frontend_cache/tests.py index ad80f9ac7..0f25faf25 100644 --- a/wagtail/contrib/frontend_cache/tests.py +++ b/wagtail/contrib/frontend_cache/tests.py @@ -1,6 +1,6 @@ +from unittest import mock from urllib.error import HTTPError, URLError -import mock from django.core.exceptions import ImproperlyConfigured from django.test import TestCase from django.test.utils import override_settings diff --git a/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py b/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py index f199175f8..7a739eff0 100644 --- a/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py +++ b/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py @@ -1,4 +1,5 @@ -import mock +from unittest import mock + from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from django.test import TestCase @@ -189,7 +190,7 @@ class TestCreateView(TestCase, WagtailTestUtils): mock_form_fields_exclude.return_value = ['123'] self.get() - mock_form_fields_exclude.assert_called() + self.assertTrue(mock_form_fields_exclude.called) m.assert_called_with(Book, exclude=mock_form_fields_exclude.return_value) @@ -317,7 +318,7 @@ class TestEditView(TestCase, WagtailTestUtils): mock_form_fields_exclude.return_value = ['123'] self.get(1) - mock_form_fields_exclude.assert_called() + self.assertTrue(mock_form_fields_exclude.called) m.assert_called_with(Book, exclude=mock_form_fields_exclude.return_value) diff --git a/wagtail/contrib/routable_page/tests.py b/wagtail/contrib/routable_page/tests.py index e7be048af..beff1100f 100644 --- a/wagtail/contrib/routable_page/tests.py +++ b/wagtail/contrib/routable_page/tests.py @@ -1,4 +1,5 @@ -import mock +from unittest import mock + from django.test import RequestFactory, TestCase from django.urls.exceptions import NoReverseMatch diff --git a/wagtail/core/tests/test_rich_text.py b/wagtail/core/tests/test_rich_text.py index 3f25ae0ae..3e6118b12 100644 --- a/wagtail/core/tests/test_rich_text.py +++ b/wagtail/core/tests/test_rich_text.py @@ -1,6 +1,7 @@ +from unittest.mock import patch + from bs4 import BeautifulSoup from django.test import TestCase -from mock import patch from wagtail.core.models import Page from wagtail.core.rich_text import RichText, expand_db_html diff --git a/wagtail/documents/tests/test_views.py b/wagtail/documents/tests/test_views.py index dda7ffa95..571116653 100644 --- a/wagtail/documents/tests/test_views.py +++ b/wagtail/documents/tests/test_views.py @@ -1,7 +1,7 @@ import os.path import unittest +from unittest import mock -import mock from django.conf import settings from django.core.files.base import ContentFile from django.test import TestCase diff --git a/wagtail/embeds/tests.py b/wagtail/embeds/tests.py index f396557f3..c28a1ae39 100644 --- a/wagtail/embeds/tests.py +++ b/wagtail/embeds/tests.py @@ -1,6 +1,7 @@ import json import unittest import urllib.request +from unittest.mock import patch from urllib.error import URLError from bs4 import BeautifulSoup @@ -8,7 +9,6 @@ from django import template from django.core.exceptions import ValidationError from django.test import TestCase, override_settings from django.urls import reverse -from mock import patch from wagtail.core import blocks from wagtail.core.rich_text import expand_db_html diff --git a/wagtail/images/tests/test_image_operations.py b/wagtail/images/tests/test_image_operations.py index 66f01187e..3da48af9c 100644 --- a/wagtail/images/tests/test_image_operations.py +++ b/wagtail/images/tests/test_image_operations.py @@ -1,7 +1,7 @@ from io import BytesIO +from unittest.mock import Mock, patch from django.test import TestCase, override_settings -from mock import Mock, patch from wagtail.core import hooks from wagtail.images import image_operations diff --git a/wagtail/search/tests/test_elasticsearch2_backend.py b/wagtail/search/tests/test_elasticsearch2_backend.py index 180f7a465..b86c3838f 100644 --- a/wagtail/search/tests/test_elasticsearch2_backend.py +++ b/wagtail/search/tests/test_elasticsearch2_backend.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- import datetime import json +from unittest import mock -import mock from django.db.models import Q from django.test import TestCase from elasticsearch.serializer import JSONSerializer diff --git a/wagtail/search/tests/test_elasticsearch5_backend.py b/wagtail/search/tests/test_elasticsearch5_backend.py index 6102d7fb0..1dc094431 100644 --- a/wagtail/search/tests/test_elasticsearch5_backend.py +++ b/wagtail/search/tests/test_elasticsearch5_backend.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- import datetime import json +from unittest import mock -import mock from django.db.models import Q from django.test import TestCase from elasticsearch.serializer import JSONSerializer diff --git a/wagtail/search/tests/test_elasticsearch6_backend.py b/wagtail/search/tests/test_elasticsearch6_backend.py index c72ef08bf..805601a94 100644 --- a/wagtail/search/tests/test_elasticsearch6_backend.py +++ b/wagtail/search/tests/test_elasticsearch6_backend.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- import datetime import json +from unittest import mock -import mock from django.db.models import Q from django.test import TestCase from elasticsearch.serializer import JSONSerializer diff --git a/wagtail/search/tests/test_index_functions.py b/wagtail/search/tests/test_index_functions.py index fe5743792..3f0194a63 100644 --- a/wagtail/search/tests/test_index_functions.py +++ b/wagtail/search/tests/test_index_functions.py @@ -1,6 +1,6 @@ from datetime import date +from unittest import mock -import mock from django.test import TestCase, override_settings from wagtail.core.models import Page @@ -150,7 +150,7 @@ class TestSignalHandlers(TestCase, WagtailTestUtils): obj.title = "Updated test" obj.save() - backend().add.assert_called_once() + self.assertEqual(backend().add.call_count, 1) indexed_object = backend().add.call_args[0][0] self.assertEqual(indexed_object.title, "Updated test") @@ -169,7 +169,7 @@ class TestSignalHandlers(TestCase, WagtailTestUtils): obj.publication_date = date(2001, 10, 19) obj.save(update_fields=['title']) - backend().add.assert_called_once() + self.assertEqual(backend().add.call_count, 1) indexed_object = backend().add.call_args[0][0] self.assertEqual(indexed_object.title, "Updated test") self.assertEqual(indexed_object.publication_date, date(2017, 10, 18))