Skip to content

Commit

Permalink
Merge pull request #7681 from readthedocs/faster-tests
Browse files Browse the repository at this point in the history
Tests: mock trigger_build
  • Loading branch information
stsewd committed Nov 23, 2020
2 parents 5234f41 + 5cc63d9 commit 8479e72
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 31 deletions.
13 changes: 1 addition & 12 deletions 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:

"""
Expand Down
1 change: 1 addition & 0 deletions readthedocs/rtd_tests/tests/test_build_forms.py
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions readthedocs/rtd_tests/tests/test_privacy_urls.py
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down
9 changes: 6 additions & 3 deletions readthedocs/rtd_tests/tests/test_project.py
@@ -1,23 +1,24 @@
import datetime
import json
from unittest import mock
from unittest.mock import patch

import pytest
from django.contrib.auth.models import User
from django.forms.models import model_to_dict
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
Expand Down Expand Up @@ -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):
Expand Down
29 changes: 15 additions & 14 deletions 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
Expand All @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions readthedocs/rtd_tests/tests/test_sync_versions.py
Expand Up @@ -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']

Expand Down Expand Up @@ -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': [
Expand Down Expand Up @@ -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': [
Expand Down Expand Up @@ -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']

Expand Down Expand Up @@ -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']

Expand Down
6 changes: 4 additions & 2 deletions 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
Expand All @@ -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):
Expand Down Expand Up @@ -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']

Expand Down

0 comments on commit 8479e72

Please sign in to comment.