test(tables): Add validation for model table test classes (#22223)

Add test ensuring each model-backed table has a corresponding test case
inheriting from StandardTableTestCase with correct table attribute set.
Includes helper method to import table test classes by model.

Fixes #22110
This commit is contained in:
Martin Hauser 2026-05-18 18:30:44 +02:00 committed by GitHub
parent 8c506c84c8
commit f149ccb302
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 0 deletions

View File

@ -31,6 +31,10 @@ class RackRoleTableTestCase(TableTestCases.StandardTableTestCase):
table = RackRoleTable
class RackGroupTableTestCase(TableTestCases.StandardTableTestCase):
table = RackGroupTable
class RackTypeTableTestCase(TableTestCases.StandardTableTestCase):
table = RackTypeTable
@ -171,6 +175,10 @@ class CableTableTestCase(TableTestCases.StandardTableTestCase):
table = CableTable
class CableBundleTableTestCase(TableTestCases.StandardTableTestCase):
table = CableBundleTable
#
# Power
#

View File

@ -52,6 +52,7 @@ from netbox.tables import (
OrganizationalModelTable,
PrimaryModelTable,
)
from utilities.testing import TableTestCases
class FormClassesTestCase(TestCase):
@ -222,6 +223,15 @@ class TableClassesTestCase(TestCase):
model_name = model.__name__
return import_string(f'{app_label}.tables.{model_name}Table')
@staticmethod
def get_table_test_for_model(model):
"""
Import and return the table test class for a given model.
"""
app_label = model._meta.app_label
model_name = model.__name__
return import_string(f'{app_label}.tests.test_tables.{model_name}TableTestCase')
@staticmethod
def get_model_table_base_class(model):
"""
@ -255,6 +265,42 @@ class TableClassesTestCase(TestCase):
f"{table}.Meta does not inherit from {base_class}.Meta",
)
def test_model_table_test_classes(self):
"""
Check that each model-backed table has a standard table test case.
"""
for model in apps.get_models():
if self.get_model_table_base_class(model) is None:
continue
with self.subTest(model=model.__name__):
app_label = model._meta.app_label
model_name = model.__name__
try:
table = self.get_table_for_model(model)
except ImportError:
self.fail(
f"No table class found for {model_name} "
f"(expected {app_label}.tables.{model_name}Table)"
)
try:
table_test = self.get_table_test_for_model(model)
except ImportError:
self.fail(
f"No table test case found for {model_name} "
f"(expected {app_label}.tests.test_tables.{model_name}TableTestCase)"
)
self.assertTrue(
issubclass(table_test, TableTestCases.StandardTableTestCase),
f"{table_test} does not inherit from {TableTestCases.StandardTableTestCase}",
)
self.assertIs(
table_test.table,
table,
f"{table_test}.table is not set to {table}",
)
class SerializerClassesTestCase(TestCase):

View File

@ -14,6 +14,10 @@ class ClusterTableTestCase(TableTestCases.StandardTableTestCase):
table = ClusterTable
class VirtualMachineTypeTableTestCase(TableTestCases.StandardTableTestCase):
table = VirtualMachineTypeTable
class VirtualMachineTableTestCase(TableTestCases.StandardTableTestCase):
table = VirtualMachineTable