Better upload handling in temporary dirs
This commit is contained in:
@@ -0,0 +1 @@
|
||||
from .base import UploadMixin
|
||||
|
||||
22
tests/videodinges/base.py
Normal file
22
tests/videodinges/base.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import tempfile
|
||||
from unittest import TestCase
|
||||
|
||||
from django.test import override_settings
|
||||
|
||||
|
||||
class UploadMixin(TestCase):
|
||||
base_upload_dir: str
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
super().setUpClass()
|
||||
cls.base_upload_dir = tempfile.mkdtemp(suffix='-videodinges-tests')
|
||||
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
media_root = tempfile.mkdtemp(suffix='-' + self.__class__.__name__, dir=self.base_upload_dir)
|
||||
self.media_root_override_settings = override_settings(MEDIA_ROOT=media_root)
|
||||
self.media_root_override_settings.enable()
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.media_root_override_settings.disable()
|
||||
super().tearDown()
|
||||
@@ -1,10 +1,8 @@
|
||||
""" Module generating useful models in 1 place """
|
||||
import tempfile
|
||||
from typing import Type, TypeVar
|
||||
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
import django.db.models
|
||||
from django.test import override_settings
|
||||
|
||||
from videodinges import models
|
||||
|
||||
@@ -30,10 +28,6 @@ def create(model: Type[T], **kwargs) -> T:
|
||||
return models.Transcoding.objects.create(**{**defaults, **kwargs})
|
||||
|
||||
if model is models.Upload:
|
||||
return _upload(**kwargs)
|
||||
|
||||
@override_settings(MEDIA_ROOT=tempfile.mkdtemp())
|
||||
def _upload(**kwargs):
|
||||
file = SimpleUploadedFile('some_file.txt', b'some contents') \
|
||||
if 'file' not in kwargs else None
|
||||
return models.Upload.objects.create(**{**dict(file=file), **kwargs})
|
||||
|
||||
@@ -2,7 +2,7 @@ from django.db.utils import IntegrityError
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from tests.videodinges import factories
|
||||
from tests.videodinges import factories, UploadMixin
|
||||
from videodinges.models import Transcoding, Video, qualities, transcoding_types, Upload
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class TranscodingTestCase(TestCase):
|
||||
self.assertEqual(transcoding.url, 'https://some_url')
|
||||
|
||||
|
||||
class CreateTranscodingTestCase(TestCase):
|
||||
class CreateTranscodingTestCase(UploadMixin, TestCase):
|
||||
|
||||
def test_upload_and_url_cannot_both_be_filled(self):
|
||||
video = factories.create(Video)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from tests.videodinges import UploadMixin
|
||||
from videodinges.models import Upload
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.test import override_settings
|
||||
|
||||
import tempfile
|
||||
|
||||
@override_settings(MEDIA_ROOT=tempfile.mkdtemp())
|
||||
class UploadTestCase(TestCase):
|
||||
class UploadTestCase(UploadMixin, TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
Upload.objects.create(file=SimpleUploadedFile('some_file.txt', b'some contents'))
|
||||
|
||||
def test_model_is_created(self):
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from tests.videodinges import factories
|
||||
from tests.videodinges import factories, UploadMixin
|
||||
from videodinges.models import Upload
|
||||
|
||||
class UploadTestCase(TestCase):
|
||||
class UploadTestCase(UploadMixin, TestCase):
|
||||
def test_model_is_created(self):
|
||||
upload = factories.create(Upload)
|
||||
self.assertEqual(upload.file.name, 'some_file.txt')
|
||||
|
||||
@@ -3,12 +3,13 @@ from django.http import HttpResponse
|
||||
from django.test import TestCase, Client
|
||||
from django.urls import reverse
|
||||
|
||||
from tests.videodinges import factories
|
||||
from tests.videodinges import factories, UploadMixin
|
||||
from videodinges import models
|
||||
|
||||
|
||||
class VideoTestCase(TestCase):
|
||||
class VideoTestCase(UploadMixin, TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.client = Client()
|
||||
|
||||
def test_video_view_renders_properly(self):
|
||||
|
||||
Reference in New Issue
Block a user