From 5cc63d9cf478d5809f4f5ae8470929a66701a10a Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Thu, 19 Nov 2020 13:32:27 -0500 Subject: [PATCH] Tests: mock trigger_build Final take, same as https://github.com/readthedocs/readthedocs.org/pull/7677. Everything should be fast now! --- readthedocs/rtd_tests/base.py | 13 +-------- .../rtd_tests/tests/test_build_forms.py | 1 + .../rtd_tests/tests/test_privacy_urls.py | 2 ++ readthedocs/rtd_tests/tests/test_project.py | 9 ++++-- .../rtd_tests/tests/test_project_views.py | 29 ++++++++++--------- .../rtd_tests/tests/test_sync_versions.py | 5 ++++ readthedocs/rtd_tests/tests/test_views.py | 6 ++-- 7 files changed, 34 insertions(+), 31 deletions(-) diff --git a/readthedocs/rtd_tests/base.py b/readthedocs/rtd_tests/base.py index ebc575f4ab4..22d9a2a9574 100644 --- a/readthedocs/rtd_tests/base.py +++ b/readthedocs/rtd_tests/base.py @@ -1,27 +1,16 @@ -# -*- coding: utf-8 -*- """Base classes and mixins for unit tests.""" import logging from collections import OrderedDict +from unittest.mock import patch from django.contrib.auth.models import AnonymousUser from django.contrib.messages.storage.fallback import FallbackStorage from django.contrib.sessions.middleware import SessionMiddleware from django.test import RequestFactory, TestCase -from unittest.mock import patch - log = logging.getLogger(__name__) -@patch('readthedocs.projects.views.private.trigger_build', lambda x: None) -@patch('readthedocs.projects.views.private.trigger_build', lambda x: None) -class MockBuildTestCase(TestCase): - - """Mock build triggers for test cases.""" - - pass - - class RequestFactoryTestMixin: """ diff --git a/readthedocs/rtd_tests/tests/test_build_forms.py b/readthedocs/rtd_tests/tests/test_build_forms.py index 16773d8e841..5c075fd93ef 100644 --- a/readthedocs/rtd_tests/tests/test_build_forms.py +++ b/readthedocs/rtd_tests/tests/test_build_forms.py @@ -90,6 +90,7 @@ def test_can_update_privacy_level(self): self.assertTrue(form.is_valid()) self.assertEqual(version.privacy_level, PRIVATE) + @mock.patch('readthedocs.builds.forms.trigger_build', mock.MagicMock()) @mock.patch('readthedocs.projects.tasks.clean_project_resources') def test_resources_are_deleted_when_version_is_inactive(self, clean_project_resources): version = get( diff --git a/readthedocs/rtd_tests/tests/test_privacy_urls.py b/readthedocs/rtd_tests/tests/test_privacy_urls.py index 93c0c0977c9..203e7d30c9c 100644 --- a/readthedocs/rtd_tests/tests/test_privacy_urls.py +++ b/readthedocs/rtd_tests/tests/test_privacy_urls.py @@ -253,6 +253,7 @@ def is_admin(self): # ## Private Project Testing ### +@mock.patch('readthedocs.projects.views.private.trigger_build', mock.MagicMock()) class PrivateProjectAdminAccessTest(PrivateProjectMixin, TestCase): response_data = { @@ -291,6 +292,7 @@ def is_admin(self): return True +@mock.patch('readthedocs.projects.views.private.trigger_build', mock.MagicMock()) class PrivateProjectUserAccessTest(PrivateProjectMixin, TestCase): response_data = { diff --git a/readthedocs/rtd_tests/tests/test_project.py b/readthedocs/rtd_tests/tests/test_project.py index 31294b99e7b..e56f680761e 100644 --- a/readthedocs/rtd_tests/tests/test_project.py +++ b/readthedocs/rtd_tests/tests/test_project.py @@ -1,5 +1,7 @@ import datetime import json +from unittest import mock +from unittest.mock import patch import pytest from django.contrib.auth.models import User @@ -7,17 +9,16 @@ from django.test import TestCase from django.utils import timezone from django_dynamic_fixture import get -from unittest.mock import patch from rest_framework.reverse import reverse from readthedocs.builds.constants import ( BUILD_STATE_CLONING, BUILD_STATE_FINISHED, BUILD_STATE_TRIGGERED, - LATEST, EXTERNAL, + LATEST, + TAG, ) -from readthedocs.builds.constants import TAG from readthedocs.builds.models import Build, Version from readthedocs.oauth.services import GitHubService, GitLabService from readthedocs.projects.constants import GITHUB_BRAND, GITLAB_BRAND @@ -256,6 +257,8 @@ def test_git_service_class_gitlab(self): self.pip.save() self.assertEqual(self.pip.git_service_class(), GitLabService) + +@mock.patch('readthedocs.projects.forms.trigger_build', mock.MagicMock()) class TestProjectTranslations(ProjectMixin, TestCase): def test_translations(self): diff --git a/readthedocs/rtd_tests/tests/test_project_views.py b/readthedocs/rtd_tests/tests/test_project_views.py index 2c873ae2f4c..0718e75b6df 100644 --- a/readthedocs/rtd_tests/tests/test_project_views.py +++ b/readthedocs/rtd_tests/tests/test_project_views.py @@ -1,4 +1,5 @@ from datetime import timedelta +from unittest import mock from allauth.account.models import EmailAddress from django.contrib.auth.models import User @@ -9,7 +10,6 @@ from django.utils import timezone from django.views.generic.base import ContextMixin from django_dynamic_fixture import get, new -from unittest import mock from readthedocs.builds.constants import EXTERNAL from readthedocs.builds.models import Build, Version @@ -19,16 +19,12 @@ from readthedocs.projects.exceptions import ProjectSpamError from readthedocs.projects.models import Domain, Project from readthedocs.projects.views.mixins import ProjectRelationMixin -from readthedocs.projects.views.public import ProjectBadgeView from readthedocs.projects.views.private import ImportWizardView -from readthedocs.rtd_tests.base import ( - MockBuildTestCase, - RequestFactoryTestMixin, - WizardTestCase, -) +from readthedocs.projects.views.public import ProjectBadgeView +from readthedocs.rtd_tests.base import RequestFactoryTestMixin, WizardTestCase -@mock.patch('readthedocs.core.utils.trigger_build', mock.MagicMock()) +@mock.patch('readthedocs.projects.tasks.update_docs_task', mock.MagicMock()) class TestProfileMiddleware(RequestFactoryTestMixin, TestCase): wizard_class_slug = 'import_wizard_view' @@ -85,7 +81,7 @@ def test_profile_middleware_banned(self): self.assertEqual(resp['location'], '/') -@mock.patch('readthedocs.core.utils.trigger_build', mock.MagicMock()) +@mock.patch('readthedocs.projects.tasks.update_docs_task', mock.MagicMock()) class TestBasicsForm(WizardTestCase): wizard_class_slug = 'import_wizard_view' @@ -168,7 +164,7 @@ def test_form_missing(self): self.assertWizardFailure(resp, 'repo_type') -@mock.patch('readthedocs.core.utils.trigger_build', mock.MagicMock()) +@mock.patch('readthedocs.projects.tasks.update_docs_task', mock.MagicMock()) class TestAdvancedForm(TestBasicsForm): def setUp(self): @@ -278,7 +274,8 @@ def test_form_spam_ban_user(self, mocked_validator): self.assertTrue(self.user.profile.banned) -class TestImportDemoView(MockBuildTestCase): +@mock.patch('readthedocs.projects.views.private.trigger_build', mock.MagicMock()) +class TestImportDemoView(TestCase): """Test project import demo view.""" fixtures = ['test_data', 'eric'] @@ -391,7 +388,8 @@ def test_import_demo_imported_duplicate(self): ) -class TestPublicViews(MockBuildTestCase): +@mock.patch('readthedocs.core.utils.trigger_build', mock.MagicMock()) +class TestPublicViews(TestCase): def setUp(self): self.pip = get(Project, slug='pip', privacy_level=PUBLIC) self.external_version = get( @@ -424,7 +422,8 @@ def test_project_versions_only_shows_internal_versons(self): self.assertNotIn(self.external_version, response.context['inactive_versions']) -class TestPrivateViews(MockBuildTestCase): +@mock.patch('readthedocs.core.utils.trigger_build', mock.MagicMock()) +class TestPrivateViews(TestCase): def setUp(self): self.user = new(User, username='eric') self.user.set_password('test') @@ -518,7 +517,9 @@ def test_integration_create_generic_webhook(self, attach_webhook): attach_webhook.assert_not_called() -class TestPrivateMixins(MockBuildTestCase): +@mock.patch('readthedocs.core.utils.trigger_build', mock.MagicMock()) +@mock.patch('readthedocs.projects.tasks.update_docs_task', mock.MagicMock()) +class TestPrivateMixins(TestCase): def setUp(self): self.project = get(Project, slug='kong') diff --git a/readthedocs/rtd_tests/tests/test_sync_versions.py b/readthedocs/rtd_tests/tests/test_sync_versions.py index eee0c67da1a..6def6025898 100644 --- a/readthedocs/rtd_tests/tests/test_sync_versions.py +++ b/readthedocs/rtd_tests/tests/test_sync_versions.py @@ -19,6 +19,7 @@ @mock.patch('readthedocs.core.utils.trigger_build', mock.MagicMock()) +@mock.patch('readthedocs.api.v2.views.model_views.trigger_build', mock.MagicMock()) class TestSyncVersions(TestCase): fixtures = ['eric', 'test_data'] @@ -797,6 +798,7 @@ def test_automation_rules_are_triggered_for_new_versions(self, run_automation_ru {'0.8', '0.8.1'}, ) + @mock.patch('readthedocs.builds.automation_actions.trigger_build', mock.MagicMock()) def test_automation_rule_activate_version(self): version_post_data = { 'tags': [ @@ -828,6 +830,7 @@ def test_automation_rule_activate_version(self): new_tag = self.pip.versions.get(verbose_name='new_tag') self.assertTrue(new_tag.active) + @mock.patch('readthedocs.builds.automation_actions.trigger_build', mock.MagicMock()) def test_automation_rule_set_default_version(self): version_post_data = { 'tags': [ @@ -923,6 +926,7 @@ def test_automation_rule_dont_delete_default_version(self): self.assertTrue(self.pip.versions.filter(slug=version_slug).exists()) @mock.patch('readthedocs.core.utils.trigger_build', mock.MagicMock()) +@mock.patch('readthedocs.api.v2.views.model_views.trigger_build', mock.MagicMock()) class TestStableVersion(TestCase): fixtures = ['eric', 'test_data'] @@ -1432,6 +1436,7 @@ def test_user_defined_stable_version_branch_with_tags(self): @mock.patch('readthedocs.core.utils.trigger_build', mock.MagicMock()) +@mock.patch('readthedocs.api.v2.views.model_views.trigger_build', mock.MagicMock()) class TestLatestVersion(TestCase): fixtures = ['eric', 'test_data'] diff --git a/readthedocs/rtd_tests/tests/test_views.py b/readthedocs/rtd_tests/tests/test_views.py index f9fbb5e8159..954703a95a4 100644 --- a/readthedocs/rtd_tests/tests/test_views.py +++ b/readthedocs/rtd_tests/tests/test_views.py @@ -1,13 +1,13 @@ import csv +from unittest import mock from urllib.parse import urlsplit -from unittest import mock from django.contrib.auth.models import User +from django.core.cache import cache from django.test import TestCase from django.urls import reverse from django.utils import timezone from django_dynamic_fixture import get, new -from django.core.cache import cache from readthedocs.builds.constants import EXTERNAL, LATEST from readthedocs.builds.models import Build, Version @@ -17,6 +17,7 @@ from readthedocs.projects.models import Feature, Project +@mock.patch('readthedocs.projects.forms.trigger_build', mock.MagicMock()) class Testmaker(TestCase): def setUp(self): @@ -225,6 +226,7 @@ def test_project_admins_can_delete_subprojects_that_they_are_not_admin_of( ) +@mock.patch('readthedocs.projects.tasks.update_docs_task', mock.MagicMock()) class BuildViewTests(TestCase): fixtures = ['eric', 'test_data']