Skip to content

Unit testing DataTable grids

Dave Lawrence edited this page Oct 12, 2023 · 4 revisions

Go through the "urls.py" and look for DatabaseTableView, copy/paste the URL names.

_test_datatable_urls - Hits the URLS and ?dataTableDefinition=1

_test_datatables_grid_list_urls - If you 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

Call the file "test_urls.py", have the test class inherit from URLTestCase below is an example from "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_list_urls(self.PRIVATE_GRID_LIST_URLS, self.user_owner, True)

    @prevent_request_warnings
    def testDataTablesGridListNoPermission(self):
        self._test_datatables_grid_list_urls(self.PRIVATE_GRID_LIST_URLS, self.user_non_owner, False)
Clone this wiki locally