-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ - perf: attempt to increase performance of DestructionList related …
…API endpoints
- Loading branch information
1 parent
d5df99f
commit 9ba0080
Showing
8 changed files
with
224 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
backend/src/openarchiefbeheer/accounts/tests/test_managers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from django.contrib.auth.models import Group, Permission | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.test import TestCase | ||
|
||
from openarchiefbeheer.destruction.models import DestructionList | ||
from ..models import User | ||
from .factories import UserFactory | ||
|
||
|
||
class UserQuerySetTests(TestCase): | ||
def test_annotate_permissions(self): | ||
content_type = ContentType.objects.get_for_model(DestructionList) | ||
|
||
group = Group.objects.create() | ||
group_permission = Permission.objects.create( | ||
codename="group_permission", content_type=content_type | ||
) | ||
group.permissions.add(group_permission) | ||
|
||
user_permission = Permission.objects.create( | ||
codename="user_permission", content_type=content_type | ||
) | ||
|
||
users = UserFactory.create_batch(3) | ||
for user in users: | ||
user.user_permissions.add(user_permission) | ||
user.groups.add(group) | ||
|
||
with self.assertNumQueries(1): | ||
ls = list( # List to force QuerySet evaluation. | ||
User.objects.annotate_permissions().values_list( | ||
"user_permission_codenames", flat=True | ||
) | ||
) | ||
|
||
self.assertIn("user_permission", ls[0]) | ||
self.assertIn("user_permission", ls[1]) | ||
self.assertIn("user_permission", ls[2]) | ||
|
||
with self.assertNumQueries(1): | ||
ls = list( # List to force QuerySet evaluation. | ||
User.objects.annotate_permissions().values_list( | ||
"group_permission_codenames", flat=True | ||
) | ||
) | ||
|
||
self.assertIn("group_permission", ls[0]) | ||
self.assertIn("group_permission", ls[1]) | ||
self.assertIn("group_permission", ls[2]) | ||
|
||
|
||
class UserManagerTests(TestCase): | ||
def test_create_superuser(self): | ||
user = User.objects.create_superuser("god", "[email protected]", "praisejebus") | ||
self.assertIsNotNone(user.pk) | ||
self.assertTrue(user.is_staff) | ||
self.assertTrue(user.is_superuser) | ||
self.assertEqual(user.username, "god") | ||
self.assertEqual(user.email, "[email protected]") | ||
self.assertTrue(user.check_password("praisejebus")) | ||
self.assertNotEqual(user.password, "praisejebus") | ||
|
||
def test_create_user(self): | ||
user = User.objects.create_user("infidel") | ||
self.assertIsNotNone(user.pk) | ||
self.assertFalse(user.is_superuser) | ||
self.assertFalse(user.is_staff) | ||
self.assertFalse(user.has_usable_password()) |
22 changes: 0 additions & 22 deletions
22
backend/src/openarchiefbeheer/accounts/tests/test_user_manager.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.db.models import Manager, Prefetch, QuerySet | ||
|
||
from openarchiefbeheer.accounts.models import User | ||
|
||
|
||
class DestructionListQuerySet(QuerySet): | ||
def annotate_user_permissions(self): | ||
""" | ||
Adds `user_permission_codenames` and `group_permission_codenames` as `ArrayField` to the `author`, | ||
`assignee`, and `assignees__user` in the current QuerySet containing the codenames of the applicable | ||
permissions. `user_permissions` and `permissions` are prefetched to minimize queries. | ||
This is used to avoid n+1 issues with nested `UserSerializer`. | ||
""" | ||
from openarchiefbeheer.destruction.models import DestructionListAssignee | ||
|
||
return self.prefetch_related( | ||
Prefetch( | ||
"author", | ||
queryset=User.objects.annotate_permissions(), | ||
), | ||
Prefetch( | ||
"assignee", | ||
queryset=User.objects.annotate_permissions(), | ||
), | ||
Prefetch( | ||
"assignees", | ||
queryset=DestructionListAssignee.objects.prefetch_related( | ||
Prefetch( | ||
"user", | ||
queryset=User.objects.annotate_permissions(), | ||
) | ||
).order_by("pk"), | ||
), | ||
) | ||
|
||
|
||
class DestructionListManager(Manager.from_queryset(DestructionListQuerySet)): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
backend/src/openarchiefbeheer/destruction/tests/test_managers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django.contrib.auth.models import Group, Permission | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.test import TestCase | ||
|
||
from openarchiefbeheer.destruction.models import DestructionList | ||
from openarchiefbeheer.destruction.tests.factories import ( | ||
DestructionListAssigneeFactory, | ||
DestructionListFactory, | ||
) | ||
|
||
|
||
class DestructionListQuerySetTests(TestCase): | ||
def test_annotate_user_permissions(self): | ||
content_type = ContentType.objects.get_for_model(DestructionList) | ||
|
||
group = Group.objects.create() | ||
group_permission = Permission.objects.create( | ||
codename="group_permission", content_type=content_type | ||
) | ||
group.permissions.add(group_permission) | ||
|
||
user_permission = Permission.objects.create( | ||
codename="user_permission", content_type=content_type | ||
) | ||
|
||
assignees = DestructionListAssigneeFactory.create_batch(3) | ||
author = assignees[0].user | ||
author.user_permissions.add(user_permission) | ||
destruction_lists = DestructionListFactory.create_batch(100, author=author) | ||
for destruction_list in destruction_lists: | ||
destruction_list.assignees.set(assignees) | ||
|
||
with self.assertNumQueries(11): | ||
ls = list( # List to force QuerySet evaluation. | ||
DestructionList.objects.filter( | ||
author=author | ||
).annotate_user_permissions() | ||
) | ||
for i in range(0, len(ls)): | ||
self.assertIn("user_permission", ls[i].author.user_permission_codenames) |