-
Notifications
You must be signed in to change notification settings - Fork 3
Unit testing DataTable grids
Dave Lawrence edited this page Jul 30, 2026
·
5 revisions
Go through the "urls.py" and look for DatabaseTableView, copy/paste the URL names.
Helpers on URLTestCase (library/django_utils/unittest_utils.py):
-
_test_urls- hits URLs and checks the status code -
_test_datatable_urls- hits the URLs with?dataTableDefinition=1 -
_test_datatables_grid_urls_contains_objs- if your grid uses permissions, you can add some objects with a user permission, then verify the object appears on the grid for the user, and doesn't appear on the grid for a different user -
_test_jqgrid_urls_contains_objs- the same thing for legacy jqGrid grids -
_test_autocomplete_urls- autocomplete views
Call the file "test_urls.py" (some apps put it under a tests/utils/ subdir, eg
classification/tests/utils/test_urls.py), have the test class inherit from URLTestCase.
Below is an example from the "classification" app:
class Test(URLTestCase):
user_non_owner = None
user_owner = None
@classmethod
def setUpTestData(cls):
super().setUpTestData()
owner_username = f"test_user_{__file__}_owner"
non_owner_username = f"test_user_{__file__}_non_owner"
cls.user_owner = User.objects.get_or_create(username=owner_username)[0]
cls.user_non_owner = User.objects.get_or_create(username=non_owner_username)[0]
cls.lab = Lab.objects.get_or_create(name="Fake Lab", city="Adelaide", country=australia,
organization=organization, group_name="fake_org/fake_lab")[0]
cls.PRIVATE_GRID_LIST_URLS = [
("classification_datatables", {}, classification),
]
def testDataGridUrls(self):
""" Grids w/o permissions """
GRID_LIST_URLS = [
("classification_datatables", {}, 200),
("classification_upload_unmapped_datatable", {"GET_PARAMS": {"lab_id": self.lab.pk}}, 200),
("clinvar_export_batch_datatables", {}, 200),
("clinvar_exports_datatables", {}, 200),
("condition_text_datatable", {"lab_id": self.lab.pk}, 200),
("imported_allele_info_datatables", {}, 200),
]
self._test_urls(GRID_LIST_URLS, self.user_owner)
def testDataTablesGridListPermission(self):
self._test_datatables_grid_urls_contains_objs(self.PRIVATE_GRID_LIST_URLS, self.user_owner, True)
@prevent_request_warnings
def testDataTablesGridListNoPermission(self):
self._test_datatables_grid_urls_contains_objs(self.PRIVATE_GRID_LIST_URLS, self.user_non_owner, False)