From 54332026039dc52bc1d35515b68f5e6e8136d2b9 Mon Sep 17 00:00:00 2001 From: thenav56 Date: Fri, 10 Nov 2023 11:48:48 +0545 Subject: [PATCH] Config test media location for each pytest worker --- apps/analysis_framework/models.py | 3 +++ apps/tabular/tests/test_unit.py | 3 ++- deep/settings.py | 3 ++- deep/tests/test_case.py | 12 ++++++++++++ utils/graphene/tests.py | 15 ++++----------- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/apps/analysis_framework/models.py b/apps/analysis_framework/models.py index 7c95fa99ca..88a583d120 100644 --- a/apps/analysis_framework/models.py +++ b/apps/analysis_framework/models.py @@ -16,6 +16,9 @@ class AnalysisFrameworkTag(models.Model): description = models.TextField(blank=True) icon = models.FileField(upload_to='af-tag-icon/', max_length=255) + def __str__(self): + return self.title + class AnalysisFramework(UserResource): """ diff --git a/apps/tabular/tests/test_unit.py b/apps/tabular/tests/test_unit.py index 05329dfd7b..9ee2c0a106 100644 --- a/apps/tabular/tests/test_unit.py +++ b/apps/tabular/tests/test_unit.py @@ -3,10 +3,10 @@ from tempfile import NamedTemporaryFile from deep.tests import TestCase, TEST_MEDIA_ROOT +from utils.common import makedirs from gallery.models import File from geo.models import GeoArea, Region, AdminLevel - from project.models import Project from tabular.tasks import auto_detect_and_update_fields @@ -371,6 +371,7 @@ def test_sheet_option_change_data_row_index(self): assert len(field.actual_data) == 9 def initialize_data_and_basic_test(self, csv_data): + makedirs(TEST_MEDIA_ROOT) file = NamedTemporaryFile('w', dir=TEST_MEDIA_ROOT, delete=False) self.files.append(file.name) diff --git a/deep/settings.py b/deep/settings.py index cc13ee4294..a78d952f0a 100644 --- a/deep/settings.py +++ b/deep/settings.py @@ -122,6 +122,7 @@ HTTP_PROTOCOL = env('DEEP_HTTPS') # See if we are inside a test environment (pytest) +PYTEST_XDIST_WORKER = env('PYTEST_XDIST_WORKER') TESTING = any([ arg in sys.argv for arg in [ 'test', @@ -130,7 +131,7 @@ '/usr/local/lib/python3.6/dist-packages/py/test.py', ] # Provided by pytest-xdist -]) or env('PYTEST_XDIST_WORKER') is not None +]) or PYTEST_XDIST_WORKER is not None TEST_RUNNER = 'snapshottest.django.TestRunner' TEST_DIR = os.path.join(BASE_DIR, 'deep/test_files') diff --git a/deep/tests/test_case.py b/deep/tests/test_case.py index 7cae8e650b..f9339948db 100644 --- a/deep/tests/test_case.py +++ b/deep/tests/test_case.py @@ -1,4 +1,5 @@ import os +import shutil import autofixture from rest_framework import ( test, @@ -22,6 +23,9 @@ TEST_MEDIA_ROOT = 'rest-media-temp' +if settings.PYTEST_XDIST_WORKER: + TEST_MEDIA_ROOT = f'rest-media-temp/{settings.PYTEST_XDIST_WORKER}' + TEST_EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' TEST_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage' TEST_CACHES = { @@ -48,6 +52,13 @@ TEST_AUTH_PASSWORD_VALIDATORS = [] +def clean_up_test_media_files(path): + try: + shutil.rmtree(path, ignore_errors=True) + except FileNotFoundError: + pass + + @override_settings( DEBUG=True, EMAIL_BACKEND=TEST_EMAIL_BACKEND, @@ -86,6 +97,7 @@ def setUp(self): def tearDown(self): super().tearDown() _set_middleware_current_request(None) + clean_up_test_media_files(os.path.join(settings.BASE_DIR, TEST_MEDIA_ROOT)) for file_path in self.deep_test_files_path: if os.path.isfile(file_path): os.unlink(file_path) diff --git a/utils/graphene/tests.py b/utils/graphene/tests.py index 861ac6ec70..da45c12c2c 100644 --- a/utils/graphene/tests.py +++ b/utils/graphene/tests.py @@ -1,7 +1,6 @@ import os import json import pytz -import shutil import inspect import datetime from enum import Enum @@ -27,6 +26,7 @@ TEST_AUTH_PASSWORD_VALIDATORS, TEST_EMAIL_BACKEND, TEST_FILE_STORAGE, + clean_up_test_media_files, ) from analysis_framework.models import AnalysisFramework, AnalysisFrameworkRole @@ -35,15 +35,8 @@ User = get_user_model() TEST_MEDIA_ROOT = 'media-temp' - - -def clean_up_test_media_files(): - try: - # NOTE: CI will clean itself - if os.environ.get('CI', '').lower() != 'true': - shutil.rmtree(os.path.join(settings.BASE_DIR, TEST_MEDIA_ROOT), ignore_errors=True) - except FileNotFoundError: - pass +if settings.PYTEST_XDIST_WORKER: + TEST_MEDIA_ROOT = f'media-temp/{settings.PYTEST_XDIST_WORKER}' @override_settings( @@ -66,7 +59,7 @@ class GraphQLTestCase(BaseGraphQLTestCase): @classmethod def tearDownClass(cls): # clear the temporary media files - clean_up_test_media_files() + clean_up_test_media_files(os.path.join(settings.BASE_DIR, TEST_MEDIA_ROOT)) super().tearDownClass() def _setup_premailer_patcher(self, mock):