Skip to content

Commit

Permalink
Config test media location for each pytest worker
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 authored and AdityaKhatri committed Nov 28, 2023
1 parent 507b8b0 commit 18414fe
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
3 changes: 3 additions & 0 deletions apps/analysis_framework/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
3 changes: 2 additions & 1 deletion apps/tabular/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion deep/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,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',
Expand All @@ -128,7 +129,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')

Expand Down
12 changes: 12 additions & 0 deletions deep/tests/test_case.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import autofixture
from rest_framework import (
test,
Expand All @@ -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 = {
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 4 additions & 11 deletions utils/graphene/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import json
import pytz
import shutil
import inspect
import datetime
from enum import Enum
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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):
Expand Down

0 comments on commit 18414fe

Please sign in to comment.