Atempt to create model factories
This commit is contained in:
40
tests/videodinges/factories.py
Normal file
40
tests/videodinges/factories.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
""" 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
|
||||||
|
|
||||||
|
T = TypeVar('T', bound=django.db.models.Model)
|
||||||
|
|
||||||
|
|
||||||
|
def create(model: Type[T], **kwargs) -> T:
|
||||||
|
if model is models.Video:
|
||||||
|
return models.Video.objects.create(**{**dict(title='Title', slug='slug', description='Description'), **kwargs})
|
||||||
|
|
||||||
|
if model is models.Transcoding:
|
||||||
|
video = create(models.Video, title='Title', slug='slug', description='Description')
|
||||||
|
return models.Transcoding.objects.create(
|
||||||
|
**{
|
||||||
|
**dict(
|
||||||
|
video=video,
|
||||||
|
quality=models.qualities[0].name,
|
||||||
|
type=str(models.transcoding_types[0]),
|
||||||
|
url='https://some_url',
|
||||||
|
),
|
||||||
|
**kwargs
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if model is models.Upload:
|
||||||
|
return _upload(**kwargs)
|
||||||
|
|
||||||
|
@override_settings(MEDIA_ROOT=tempfile.mkdtemp())
|
||||||
|
def _upload(**kwargs):
|
||||||
|
return models.Upload.objects.create(**{**dict(file=SimpleUploadedFile('some_file.txt', b'some contents')), **kwargs})
|
||||||
|
|
||||||
|
|
||||||
|
# TODO fix annoying dict notation to something more gentle.
|
||||||
0
tests/videodinges/test_factories/__init__.py
Normal file
0
tests/videodinges/test_factories/__init__.py
Normal file
12
tests/videodinges/test_factories/test_transcoding.py
Normal file
12
tests/videodinges/test_factories/test_transcoding.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
from videodinges.models import Transcoding
|
||||||
|
from tests.videodinges import factories
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
class TranscodingTestCase(TestCase):
|
||||||
|
def test_factory_returns_model(self):
|
||||||
|
transcoding = factories.create(Transcoding)
|
||||||
|
self.assertEqual(transcoding.video.slug, 'slug')
|
||||||
|
self.assertEqual(transcoding.quality, '360p')
|
||||||
|
self.assertEqual(transcoding.type, 'video/webm')
|
||||||
|
self.assertEqual(transcoding.url, 'https://some_url')
|
||||||
9
tests/videodinges/test_factories/test_upload.py
Normal file
9
tests/videodinges/test_factories/test_upload.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from tests.videodinges import factories
|
||||||
|
from videodinges.models import Upload
|
||||||
|
|
||||||
|
class UploadTestCase(TestCase):
|
||||||
|
def test_model_is_created(self):
|
||||||
|
upload = factories.create(Upload)
|
||||||
|
self.assertEqual(upload.file.name, 'some_file.txt')
|
||||||
13
tests/videodinges/test_factories/test_video.py
Normal file
13
tests/videodinges/test_factories/test_video.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
from videodinges.models import Video
|
||||||
|
from tests.videodinges import factories
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
class VideoTestCase(TestCase):
|
||||||
|
def test_factory_returns_model(self):
|
||||||
|
video = factories.create(Video)
|
||||||
|
self.assertEqual(video.slug, 'slug')
|
||||||
|
self.assertEqual(video.title, 'Title')
|
||||||
|
self.assertEqual(video.description, 'Description')
|
||||||
|
self.assertIsInstance(video.created_at, datetime)
|
||||||
|
self.assertIsInstance(video.updated_at, datetime)
|
||||||
Reference in New Issue
Block a user