Repository: Nitrate/Nitrate

Issue:
Remove dependency mock
Use `unittest.mock` instead.


Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -46,7 +46,6 @@ def get_long_description():
         'coverage',
         'factory_boy',
         'flake8',
-        'mock',
         'pytest',
         'pytest-cov',
         'pytest-django',


Test patch:
diff --git a/src/tests/core/test_commands.py b/src/tests/core/test_commands.py
--- a/src/tests/core/test_commands.py
+++ b/src/tests/core/test_commands.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 
 from django.contrib.auth.models import Group
-from mock import patch
+from unittest.mock import patch
 from django import test
 from django.core.management import call_command
 
diff --git a/src/tests/core/test_core.py b/src/tests/core/test_core.py
--- a/src/tests/core/test_core.py
+++ b/src/tests/core/test_core.py
@@ -2,8 +2,7 @@
 
 import sys
 import unittest
-from mock import patch
-from mock import Mock
+from unittest.mock import patch, Mock
 
 from django import test
 from django.conf import settings
diff --git a/src/tests/core/test_files.py b/src/tests/core/test_files.py
--- a/src/tests/core/test_files.py
+++ b/src/tests/core/test_files.py
@@ -11,7 +11,7 @@
 from django.test import RequestFactory
 from django.urls import reverse
 from django.conf import settings
-from mock import patch
+from unittest.mock import patch
 
 from tcms.core.files import able_to_delete_attachment
 from tcms.management.models import TestAttachment
diff --git a/src/tests/issuetracker/test_services.py b/src/tests/issuetracker/test_services.py
--- a/src/tests/issuetracker/test_services.py
+++ b/src/tests/issuetracker/test_services.py
@@ -3,7 +3,7 @@
 import unittest
 
 from django import test
-from mock import Mock
+from unittest.mock import Mock
 
 from tcms.issuetracker import services
 from tcms.issuetracker.services import IssueTrackerService
diff --git a/src/tests/issuetracker/test_task.py b/src/tests/issuetracker/test_task.py
--- a/src/tests/issuetracker/test_task.py
+++ b/src/tests/issuetracker/test_task.py
@@ -2,7 +2,7 @@
 
 import unittest
 
-from mock import patch, Mock
+from unittest.mock import patch, Mock
 from tcms.issuetracker.task import bugzilla_external_track
 
 
diff --git a/src/tests/test_auth.py b/src/tests/test_auth.py
--- a/src/tests/test_auth.py
+++ b/src/tests/test_auth.py
@@ -3,7 +3,7 @@
 import datetime
 
 from hashlib import sha1
-from mock import patch
+from unittest.mock import patch
 
 from django.conf import settings
 from django.contrib.auth.models import User
diff --git a/src/tests/testcases/test_models.py b/src/tests/testcases/test_models.py
--- a/src/tests/testcases/test_models.py
+++ b/src/tests/testcases/test_models.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 
 from datetime import timedelta
-from mock import patch
+from unittest.mock import patch
 
 from django.contrib.auth.models import User
 from django.core import mail
diff --git a/src/tests/testcases/test_views.py b/src/tests/testcases/test_views.py
--- a/src/tests/testcases/test_views.py
+++ b/src/tests/testcases/test_views.py
@@ -7,8 +7,7 @@
 from bs4 import BeautifulSoup
 from datetime import datetime, timedelta
 from operator import attrgetter, itemgetter
-
-import mock
+from unittest.mock import patch
 
 from django import test
 from django.contrib.auth.models import User
@@ -62,13 +61,13 @@ def test_get_plan_id_from_post_request(self):
         pk = plan_from_request_or_none(request, pk_enough=True)
         self.assertEqual(1, pk)
 
-    @mock.patch('tcms.testcases.views.get_object_or_404')
+    @patch('tcms.testcases.views.get_object_or_404')
     def test_get_plan_object_from_get_request(self, get_object_or_404):
         request = self.factory.get('/uri', data={'from_plan': 1})
         plan = plan_from_request_or_none(request)
         self.assertEqual(get_object_or_404.return_value, plan)
 
-    @mock.patch('tcms.testcases.views.get_object_or_404')
+    @patch('tcms.testcases.views.get_object_or_404')
     def test_get_plan_object_from_post_request(self, get_object_or_404):
         request = self.factory.post('/uri', data={'from_plan': 1})
         plan = plan_from_request_or_none(request)
@@ -84,14 +83,14 @@ def test_missing_plan_id_in_post_request(self):
         plan = plan_from_request_or_none(request)
         self.assertIsNone(plan)
 
-    @mock.patch('tcms.testcases.views.get_object_or_404')
+    @patch('tcms.testcases.views.get_object_or_404')
     def test_nonexisting_plan_id_from_get_request(self, get_object_or_404):
         get_object_or_404.side_effect = Http404
 
         request = self.factory.get('/uri', data={'from_plan': 1})
         self.assertRaises(Http404, plan_from_request_or_none, request)
 
-    @mock.patch('tcms.testcases.views.get_object_or_404')
+    @patch('tcms.testcases.views.get_object_or_404')
     def test_nonexisting_plan_id_from_post_request(self, get_object_or_404):
         get_object_or_404.side_effect = Http404
 
@@ -352,7 +351,7 @@ def test_fail_to_remove_if_component_not_exist(self):
         data = json.loads(resp.content)
         self.assertIn('Cannot remove component', data['response'])
 
-    @mock.patch('tcms.testcases.models.TestCase.remove_component')
+    @patch('tcms.testcases.models.TestCase.remove_component')
     def test_case_remove_component_fails(self, remove_component):
         remove_component.side_effect = Exception
 
@@ -682,8 +681,8 @@ def test_remove_tags_from_cases(self):
             TestCaseTag.objects.filter(
                 case=self.case_3.pk, tag=self.tag_python.pk).exists())
 
-    @mock.patch('tcms.testcases.models.TestCase.remove_tag',
-                side_effect=ValueError('value error'))
+    @patch('tcms.testcases.models.TestCase.remove_tag',
+           side_effect=ValueError('value error'))
     def test_ensure_response_if_error_happens_when_remove_tag(self, remove_tag):
         # This test does not care about what tags are removed from which cases
         response = self.client.post(
@@ -1535,7 +1534,7 @@ def test_create_new_plan_case_rel_sort_key_for_link(self):
                           orig_plan=self.orphan_plan,
                           copy_case=False)
 
-    @mock.patch('tcms.testplans.models.TestPlan.get_case_sortkey')
+    @patch('tcms.testplans.models.TestPlan.get_case_sortkey')
     def test_clone_to_same_plan(self, get_case_sortkey):
         # Make it easier to assert the new sort key.
         get_case_sortkey.return_value = 100
@@ -1783,7 +1782,7 @@ def test_invalid_arguments(self):
         data = json.loads(resp.content)
         self.assertIn('Cannot add component', data['response'])
 
-    @mock.patch('tcms.testcases.models.TestCase.add_component')
+    @patch('tcms.testcases.models.TestCase.add_component')
     def test_failed_to_add_component(self, add_component):
         add_component.side_effect = ValueError
 
@@ -1933,7 +1932,7 @@ def test_invalid_input_for_adding_an_issue(self):
             ['Invalid issue tracker that does not exist.'],
             error_messages)
 
-    @mock.patch('tcms.testcases.models.TestCase.add_issue')
+    @patch('tcms.testcases.models.TestCase.add_issue')
     def test_fail_if_case_add_issue_fails(self, add_issue):
         add_issue.side_effect = Exception('Something wrong')
 
@@ -2437,14 +2436,14 @@ def setUpTestData(cls):
         cls.case_run_1.notes = 'Some notes'
         cls.case_run_1.save()
 
-        with mock.patch('django.utils.timezone.now') as mock_now:
+        with patch('django.utils.timezone.now') as mock_now:
             cls.submit_date = datetime(2020, 1, 22, 19, 47, 30)
             mock_now.return_value = cls.submit_date
             add_comment(
                 cls.tester, 'testruns.testcaserun', [cls.case_run_1.pk],
                 'first comment')
 
-        with mock.patch('django.utils.timezone.now') as mock_now:
+        with patch('django.utils.timezone.now') as mock_now:
             cls.submit_date_later = cls.submit_date + timedelta(minutes=10)
             mock_now.return_value = cls.submit_date_later
             add_comment(
diff --git a/src/tests/testplans/test_forms.py b/src/tests/testplans/test_forms.py
--- a/src/tests/testplans/test_forms.py
+++ b/src/tests/testplans/test_forms.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 
 import unittest
-from mock import Mock
+from unittest.mock import Mock
 
 from tcms.testplans.forms import UploadedHTMLFile
 
diff --git a/src/tests/testplans/test_models.py b/src/tests/testplans/test_models.py
--- a/src/tests/testplans/test_models.py
+++ b/src/tests/testplans/test_models.py
@@ -2,7 +2,7 @@
 
 from django.conf import settings
 from django import test
-from mock import patch
+from unittest.mock import patch
 
 from tcms.testplans.helpers import email
 from tcms.testplans.models import _disconnect_signals, TestPlan
diff --git a/src/tests/testruns/test_views.py b/src/tests/testruns/test_views.py
--- a/src/tests/testruns/test_views.py
+++ b/src/tests/testruns/test_views.py
@@ -7,7 +7,7 @@
 from http import HTTPStatus
 from operator import attrgetter
 
-from mock import patch
+from unittest.mock import patch
 from xml.etree import ElementTree
 
 from django.db.models import Max
diff --git a/src/tests/xmlrpc/test_testcase.py b/src/tests/xmlrpc/test_testcase.py
--- a/src/tests/xmlrpc/test_testcase.py
+++ b/src/tests/xmlrpc/test_testcase.py
@@ -7,7 +7,7 @@
 from django_comments.models import Comment
 from django.db.models import Max, Min
 from django import test
-from mock import patch, PropertyMock
+from unittest.mock import patch, PropertyMock
 
 from tcms.core.utils import checksum
 from tcms.issuetracker.models import Issue
diff --git a/src/tests/xmlrpc/test_user.py b/src/tests/xmlrpc/test_user.py
--- a/src/tests/xmlrpc/test_user.py
+++ b/src/tests/xmlrpc/test_user.py
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-from mock import patch
+from unittest.mock import patch
 
 from django.contrib.auth.models import User
 from django.test import TestCase