Use lambda for dependencies
so they won't be created when dependency is already provided
This commit is contained in:
@@ -14,10 +14,8 @@ def create(model: Type[T], **kwargs) -> T:
|
|||||||
return _create_with_defaults(models.Video, kwargs, title='Title', slug='slug', description='Description')
|
return _create_with_defaults(models.Video, kwargs, title='Title', slug='slug', description='Description')
|
||||||
|
|
||||||
if model is models.Transcoding:
|
if model is models.Transcoding:
|
||||||
video = create(models.Video, title='Title', slug='slug', description='Description') \
|
|
||||||
if 'video' not in kwargs else None
|
|
||||||
defaults = dict(
|
defaults = dict(
|
||||||
video=video,
|
video=lambda: create(models.Video),
|
||||||
quality=models.qualities[0].name,
|
quality=models.qualities[0].name,
|
||||||
type=str(models.transcoding_types[0]),
|
type=str(models.transcoding_types[0]),
|
||||||
)
|
)
|
||||||
@@ -36,8 +34,16 @@ def create(model: Type[T], **kwargs) -> T:
|
|||||||
def _create_with_defaults(model: Type[T], kwargs: dict, **defaults) -> T:
|
def _create_with_defaults(model: Type[T], kwargs: dict, **defaults) -> T:
|
||||||
"""
|
"""
|
||||||
Return created django model instance.
|
Return created django model instance.
|
||||||
|
When providing lambda as default item, the result of the lambda will be taken.
|
||||||
|
The lambda will ONLY be executed when not provided in kwargs.
|
||||||
|
|
||||||
:param model: django model to create
|
:param model: django model to create
|
||||||
:param kwargs: keyword arguments to fill the model
|
:param kwargs: keyword arguments to fill the model
|
||||||
:param defaults: default keyword arguments to use when not mentioned in kwargs
|
:param defaults: default keyword arguments to use when not mentioned in kwargs
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
for k, v in defaults.items():
|
||||||
|
if callable(v) and not k in kwargs:
|
||||||
|
defaults[k] = v()
|
||||||
|
|
||||||
return model.objects.create(**{**defaults, **kwargs})
|
return model.objects.create(**{**defaults, **kwargs})
|
||||||
|
|||||||
@@ -24,3 +24,14 @@ class TranscodingTestCase(TestCase):
|
|||||||
self.assertEqual(transcoding.quality, '720p')
|
self.assertEqual(transcoding.quality, '720p')
|
||||||
self.assertEqual(transcoding.type, 'video/mp4')
|
self.assertEqual(transcoding.type, 'video/mp4')
|
||||||
self.assertEqual(transcoding.url, 'http://another_url')
|
self.assertEqual(transcoding.url, 'http://another_url')
|
||||||
|
|
||||||
|
def test_does_not_create_video_when_providing_one(self):
|
||||||
|
transcoding = factories.create(
|
||||||
|
Transcoding,
|
||||||
|
quality='720p',
|
||||||
|
type='video/mp4',
|
||||||
|
url='http://another_url',
|
||||||
|
video=factories.create(Video, slug='yet-another-video-slug')
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEquals(Video.objects.all().count(), 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user