Asure not too many queries are run
This commit is contained in:
@@ -51,10 +51,18 @@ def _create_with_defaults(model: Type[T], kwargs: dict, **defaults) -> T:
|
|||||||
:param defaults: default keyword arguments to use when not mentioned in kwargs
|
:param defaults: default keyword arguments to use when not mentioned in kwargs
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
_next_pk = 0
|
||||||
|
def next_pk():
|
||||||
|
# Queries next pk only one time during creation
|
||||||
|
nonlocal _next_pk
|
||||||
|
if _next_pk == 0:
|
||||||
|
_next_pk = _query_next_pk(model)
|
||||||
|
return _next_pk
|
||||||
|
|
||||||
for k, v in defaults.items():
|
for k, v in defaults.items():
|
||||||
if callable(v) and not k in kwargs:
|
if callable(v) and not k in kwargs:
|
||||||
if len(signature(v).parameters) == 1:
|
if len(signature(v).parameters) == 1:
|
||||||
result = v(_next_pk(model))
|
result = v(next_pk())
|
||||||
else:
|
else:
|
||||||
result = v()
|
result = v()
|
||||||
defaults[k] = result
|
defaults[k] = result
|
||||||
@@ -62,7 +70,7 @@ def _create_with_defaults(model: Type[T], kwargs: dict, **defaults) -> T:
|
|||||||
return model.objects.create(**{**defaults, **kwargs})
|
return model.objects.create(**{**defaults, **kwargs})
|
||||||
|
|
||||||
|
|
||||||
def _next_pk(model: Type[T]) -> int:
|
def _query_next_pk(model: Type[T]) -> int:
|
||||||
try:
|
try:
|
||||||
return model.objects.order_by('-pk').first().pk + 1
|
return model.objects.order_by('-pk').first().pk + 1
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
|||||||
@@ -34,3 +34,8 @@ class VideoTestCase(TestCase):
|
|||||||
self.assertEqual(video3.description, 'Description 3')
|
self.assertEqual(video3.description, 'Description 3')
|
||||||
self.assertIsInstance(video3.created_at, datetime)
|
self.assertIsInstance(video3.created_at, datetime)
|
||||||
self.assertIsInstance(video3.updated_at, datetime)
|
self.assertIsInstance(video3.updated_at, datetime)
|
||||||
|
|
||||||
|
def test_factory_runs_only_2_queries(self):
|
||||||
|
""" Factory should only use 2 queries: one for selecting primary key, and one for inserting record """
|
||||||
|
with self.assertNumQueries(2):
|
||||||
|
video = factories.create(Video)
|
||||||
|
|||||||
Reference in New Issue
Block a user