-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the behaviour of Prefix table: allow sorting (#5452)
--------- Co-authored-by: Timizuo <ebideritimizuo@gmail.com> Co-authored-by: Hanlin Miao <46973263+HanlinMiao@users.noreply.github.com> Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
- Loading branch information
1 parent
f76ff6c
commit f038c8f
Showing
3 changed files
with
45 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Changed the behavior of Prefix table: now they are sortable, and after sorting is applied, all hierarchy indentations are removed. |
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,42 @@ | ||
from django.test import TestCase | ||
|
||
from nautobot.core.models.querysets import count_related | ||
from nautobot.dcim.models.locations import Location | ||
from nautobot.ipam.models import Prefix | ||
from nautobot.ipam.tables import PrefixTable | ||
|
||
|
||
class PrefixTableTestCase(TestCase): | ||
def _validate_sorted_queryset_same_with_table_queryset(self, queryset, table_class, field_name): | ||
with self.subTest(f"Assert sorting {table_class.__name__} on '{field_name}'"): | ||
table = table_class(queryset, order_by=field_name) | ||
table_queryset_data = table.data.data.values_list("pk", flat=True) | ||
sorted_queryset = queryset.order_by(field_name).values_list("pk", flat=True) | ||
self.assertEqual(list(table_queryset_data), list(sorted_queryset)) | ||
|
||
def test_prefix_table_sort(self): | ||
"""Assert TreeNode model table are orderable.""" | ||
# Due to MySQL's lack of support for combining 'LIMIT' and 'ORDER BY' in a single query, | ||
# hence this approach. | ||
pk_list = Prefix.objects.all().values_list("pk", flat=True)[:20] | ||
pk_list = [str(pk) for pk in pk_list] | ||
queryset = Prefix.objects.filter(pk__in=pk_list) | ||
|
||
# Assets model names | ||
table_avail_fields = ["tenant", "vlan", "namespace"] | ||
for table_field_name in table_avail_fields: | ||
self._validate_sorted_queryset_same_with_table_queryset(queryset, PrefixTable, table_field_name) | ||
self._validate_sorted_queryset_same_with_table_queryset(queryset, PrefixTable, f"-{table_field_name}") | ||
|
||
# Assert `prefix` | ||
table_queryset_data = PrefixTable(queryset, order_by="prefix").data.data.values_list("pk", flat=True) | ||
prefix_queryset = queryset.order_by("network", "prefix_length").values_list("pk", flat=True) | ||
self.assertEqual(list(table_queryset_data), list(prefix_queryset)) | ||
table_queryset_data = PrefixTable(queryset, order_by="-prefix").data.data.values_list("pk", flat=True) | ||
prefix_queryset = queryset.order_by("-network", "-prefix_length").values_list("pk", flat=True) | ||
self.assertEqual(list(table_queryset_data), list(prefix_queryset)) | ||
|
||
# Assets `location_count` | ||
location_count_queryset = queryset.annotate(location_count=count_related(Location, "prefixes")).all() | ||
self._validate_sorted_queryset_same_with_table_queryset(location_count_queryset, PrefixTable, "location_count") | ||
self._validate_sorted_queryset_same_with_table_queryset(location_count_queryset, PrefixTable, "-location_count") |