From 30b5a975b763843bc7ff27d5515f33f97f986bce Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Sat, 16 Jul 2022 03:24:23 +1000 Subject: [PATCH] Remove use of mock (#1228) As of Python 3.3, mock is now included in the standard library, and since the lowest version of Python supported is below that, we can remove the dependency. Switch to using unittest.mock everywhere, cleaning up some imports as a side benefit. --- requirements/test.txt | 2 +- tests/unit/__init__.py | 4 ++-- tests/unit/api/test_control.py | 5 ++--- tests/unit/api/test_tasks.py | 3 +-- tests/unit/api/test_workers.py | 3 +-- tests/unit/test_command.py | 3 +-- tests/unit/utils/test_broker.py | 3 +-- tests/unit/views/test_dashboard.py | 7 +------ 8 files changed, 10 insertions(+), 20 deletions(-) diff --git a/requirements/test.txt b/requirements/test.txt index 932a8957f..8b1378917 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -1 +1 @@ -mock + diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py index 07bcd17ab..ef66fe098 100644 --- a/tests/unit/__init__.py +++ b/tests/unit/__init__.py @@ -1,3 +1,4 @@ +from unittest.mock import patch from urllib.parse import urlencode import tornado.testing @@ -6,7 +7,6 @@ from tornado.concurrent import Future import celery -import mock from flower.app import Flower from flower.urls import handlers @@ -37,4 +37,4 @@ def post(self, url, **kwargs): return self.fetch(url, method='POST', **kwargs) def mock_option(self, name, value): - return mock.patch.object(options.mockable(), name, value) + return patch.object(options.mockable(), name, value) diff --git a/tests/unit/api/test_control.py b/tests/unit/api/test_control.py index a83d9ad6a..aa7bac0ec 100644 --- a/tests/unit/api/test_control.py +++ b/tests/unit/api/test_control.py @@ -1,5 +1,4 @@ -from mock import MagicMock -import mock +from unittest.mock import MagicMock, patch from flower.api.control import ControlHandler from tests.unit import AsyncHTTPTestCase @@ -174,7 +173,7 @@ def test_terminate_signal(self): class ControlAuthTests(WorkerControlTests): def test_auth(self): - with mock.patch.object(options.mockable(), 'basic_auth', ['user1:password1']): + with patch.object(options.mockable(), 'basic_auth', ['user1:password1']): app = self._app.capp app.control.broadcast = MagicMock() r = self.post('/api/worker/shutdown/test', body={}) diff --git a/tests/unit/api/test_tasks.py b/tests/unit/api/test_tasks.py index c38278dc7..dcee141da 100644 --- a/tests/unit/api/test_tasks.py +++ b/tests/unit/api/test_tasks.py @@ -1,4 +1,4 @@ -from mock import Mock, patch +from unittest.mock import Mock, patch, PropertyMock from datetime import datetime, timedelta from celery.result import AsyncResult @@ -16,7 +16,6 @@ class ApplyTests(AsyncHTTPTestCase): def test_apply(self): - from mock import patch, PropertyMock import json result = 'result' diff --git a/tests/unit/api/test_workers.py b/tests/unit/api/test_workers.py index 6b62c13ab..831fa0cfd 100644 --- a/tests/unit/api/test_workers.py +++ b/tests/unit/api/test_workers.py @@ -1,6 +1,5 @@ import json - -import mock +from unittest import mock from flower.api.control import ControlHandler from flower.inspector import Inspector diff --git a/tests/unit/test_command.py b/tests/unit/test_command.py index 4e90e63ae..2e7b98c7b 100644 --- a/tests/unit/test_command.py +++ b/tests/unit/test_command.py @@ -5,7 +5,6 @@ import subprocess from unittest.mock import Mock, patch -import mock from prometheus_client import Histogram from flower.command import apply_options, warn_about_celery_args_used_in_flower_command, apply_env_options @@ -49,7 +48,7 @@ def test_autodiscovery(self): - create flower command """ celery_app = self._get_celery_app() - with mock.patch.object(celery_app, '_autodiscover_tasks') as autodiscover: + with patch.object(celery_app, '_autodiscover_tasks') as autodiscover: celery_app.autodiscover_tasks() self.get_app(capp=celery_app) diff --git a/tests/unit/utils/test_broker.py b/tests/unit/utils/test_broker.py index f495af46b..53990c7a2 100644 --- a/tests/unit/utils/test_broker.py +++ b/tests/unit/utils/test_broker.py @@ -1,6 +1,5 @@ import unittest - -from mock import MagicMock +from unittest.mock import MagicMock from flower.utils import broker from flower.utils.broker import RabbitMQ, Redis, RedisBase, RedisSocket, Broker, RedisSentinel diff --git a/tests/unit/views/test_dashboard.py b/tests/unit/views/test_dashboard.py index 571257daa..de673c2c8 100644 --- a/tests/unit/views/test_dashboard.py +++ b/tests/unit/views/test_dashboard.py @@ -1,17 +1,12 @@ import time import unittest +from unittest.mock import patch import sys from tests.unit import AsyncHTTPTestCase from tests.unit.utils import task_succeeded_events, task_failed_events from tests.unit.utils import HtmlTableParser -if sys.version_info >= (2, 7): - from mock import patch -else: - from unittest.mock import patch - - from celery.events import Event from celery.utils import uuid