Skip to content

Commit 4f85589

Browse files
committed
[tests] Added assertion test for device template queries
Added a test to assert the queries executed during templates fetch on device change page. Previous count of this fetch combined with `get_relevant_templates` was 26 due to template fetch query being duplicated. With new changes the count is down to 24. Signed-off-by: DragnEmperor <[email protected]>
1 parent a845721 commit 4f85589

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

openwisp_controller/config/tests/test_admin.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest.mock import patch
55
from uuid import uuid4
66

7+
import django
78
from django.contrib.admin.models import LogEntry
89
from django.contrib.auth import get_user_model
910
from django.core.exceptions import ValidationError
@@ -2018,6 +2019,41 @@ def test_vpn_template_switch(self):
20182019
)
20192020
self.assertEqual(config.vpnclient_set.count(), 1)
20202021

2022+
# helper for asserting queries executed during template fetch for a device
2023+
def _verify_template_queries(self, config, count):
2024+
path = reverse(f'admin:{self.app_label}_device_change', args=[config.device.pk])
2025+
for i in range(count):
2026+
self._create_template(name=f'template-{i}')
2027+
expected_count = 24
2028+
if django.VERSION < (5, 2):
2029+
# In django version < 5.2, there is an extra SAVEPOINT query
2030+
# leading to extra RELEASE SAVEPOINT query, thus 2 extra queries
2031+
expected_count += 2
2032+
with self.assertNumQueries(expected_count):
2033+
# contains 22 queries for fetching normal device data
2034+
response = self.client.get(path)
2035+
# contains 2 queries, 1 for fetching organization
2036+
# and 1 for fetching templates
2037+
response = self.client.get(
2038+
reverse(
2039+
'admin:get_relevant_templates', args=[config.device.organization.pk]
2040+
)
2041+
)
2042+
self.assertEqual(response.status_code, 200)
2043+
2044+
# ensuring queries are consistent for different number of templates
2045+
def test_templates_fetch_queries_1(self):
2046+
config = self._create_config(organization=self._get_org())
2047+
self._verify_template_queries(config, 1)
2048+
2049+
def test_templates_fetch_queries_5(self):
2050+
config = self._create_config(organization=self._get_org())
2051+
self._verify_template_queries(config, 1)
2052+
2053+
def test_templates_fetch_queries_10(self):
2054+
config = self._create_config(organization=self._get_org())
2055+
self._verify_template_queries(config, 1)
2056+
20212057

20222058
class TestTransactionAdmin(
20232059
CreateConfigTemplateMixin,

0 commit comments

Comments
 (0)