Skip to content

Commit c98b148

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 c98b148

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

openwisp_controller/config/tests/test_admin.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,37 @@ def test_vpn_template_switch(self):
20182018
)
20192019
self.assertEqual(config.vpnclient_set.count(), 1)
20202020

2021+
# helper for asserting queries executed during template fetch for a device
2022+
def _verify_template_queries(self, config, count):
2023+
path = reverse(f'admin:{self.app_label}_device_change', args=[config.device.pk])
2024+
for i in range(count):
2025+
self._create_template(name=f'template-{i}')
2026+
2027+
with self.assertNumQueries(24):
2028+
# contains 22 queries for fetching normal device data
2029+
response = self.client.get(path)
2030+
# contains 2 queries, 1 for fetching organization
2031+
# and 1 for fetching templates
2032+
response = self.client.get(
2033+
reverse(
2034+
'admin:get_relevant_templates', args=[config.device.organization.pk]
2035+
)
2036+
)
2037+
self.assertEqual(response.status_code, 200)
2038+
2039+
# ensuring queries are consistent for different number of templates
2040+
def test_templates_fetch_queries_1(self):
2041+
config = self._create_config(organization=self._get_org())
2042+
self._verify_template_queries(config, 1)
2043+
2044+
def test_templates_fetch_queries_5(self):
2045+
config = self._create_config(organization=self._get_org())
2046+
self._verify_template_queries(config, 1)
2047+
2048+
def test_templates_fetch_queries_10(self):
2049+
config = self._create_config(organization=self._get_org())
2050+
self._verify_template_queries(config, 1)
2051+
20212052

20222053
class TestTransactionAdmin(
20232054
CreateConfigTemplateMixin,

0 commit comments

Comments
 (0)