Asure not too many queries are run

This commit is contained in:
2020-05-21 20:00:01 +02:00
parent c17b558d14
commit 9f0b6881dd
2 changed files with 15 additions and 2 deletions

View File

@@ -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
"""
_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():
if callable(v) and not k in kwargs:
if len(signature(v).parameters) == 1:
result = v(_next_pk(model))
result = v(next_pk())
else:
result = v()
defaults[k] = result
@@ -62,7 +70,7 @@ def _create_with_defaults(model: Type[T], kwargs: dict, **defaults) -> T:
return model.objects.create(**{**defaults, **kwargs})
def _next_pk(model: Type[T]) -> int:
def _query_next_pk(model: Type[T]) -> int:
try:
return model.objects.order_by('-pk').first().pk + 1
except AttributeError:

View File

@@ -34,3 +34,8 @@ class VideoTestCase(TestCase):
self.assertEqual(video3.description, 'Description 3')
self.assertIsInstance(video3.created_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)