|
4 | 4 | from unittest.mock import patch |
5 | 5 | from uuid import uuid4 |
6 | 6 |
|
| 7 | +import django |
7 | 8 | from django.contrib.admin.models import LogEntry |
8 | 9 | from django.contrib.auth import get_user_model |
9 | 10 | from django.core.exceptions import ValidationError |
@@ -2018,6 +2019,41 @@ def test_vpn_template_switch(self): |
2018 | 2019 | ) |
2019 | 2020 | self.assertEqual(config.vpnclient_set.count(), 1) |
2020 | 2021 |
|
| 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 | + |
2021 | 2057 |
|
2022 | 2058 | class TestTransactionAdmin( |
2023 | 2059 | CreateConfigTemplateMixin, |
|
0 commit comments