def test_normalize_config(self):
     dt = Datatable([], '/')
     dt.configure()
     self.assertEqual(dt.config['hidden_columns'], [])
     self.assertEqual(dt.config['search_fields'], [])
     self.assertEqual(dt.config['unsortable_columns'], [])
     self.assertEqual(dt.config['search'], set())
     self.assertEqual(dt.config['start_offset'], 0)
     self.assertEqual(dt.config['page_length'], 25)
     self.assertEqual(dt.config['ordering'], None)
    def test_get_processor_method_returns_direct_callable(self):
        def fake_callback():
            pass

        column = Column('Fake', sources=[], processor=fake_callback)

        # Test no callback given
        dt = Datatable([], '/')
        f = dt.get_processor_method(column, i=0)
        self.assertEqual(f, fake_callback)
예제 #3
0
 def test_normalize_config(self):
     dt = Datatable([], "/")
     dt.configure()
     self.assertEqual(dt.config["hidden_columns"], [])
     self.assertEqual(dt.config["search_fields"], [])
     self.assertEqual(dt.config["unsortable_columns"], [])
     self.assertEqual(dt.config["search"], set())
     self.assertEqual(dt.config["start_offset"], 0)
     self.assertEqual(dt.config["page_length"], 25)
     self.assertEqual(dt.config["ordering"], None)
    def test_preload_record_data_calls_view(self):
        obj1 = ExampleModel.objects.create(name='test name 1')
        queryset = ExampleModel.objects.all()

        class Dummy(object):
            def preload_record_data(self, obj):
                raise Exception('We did it')

        dt = Datatable(queryset, '/', callback_target=Dummy())
        with self.assertRaises(Exception) as cm:
            dt.get_records()
        self.assertEqual(str(cm.exception), 'We did it')
    def test_get_processor_method(self):
        class Dummy(object):
            def fake_callback(self):
                pass

        view = Dummy()

        # Test no callback given
        dt = Datatable([], '/')
        f = dt.get_processor_method(Column('Fake', sources=['fake']), i=0)
        self.assertEqual(f, None)

        class DT(Datatable):
            def fake_callback(self):
                pass

        column = Column('Fake', sources=['fake'], processor='fake_callback')

        # Test callback found on self
        dt = DT([], '/')
        f = dt.get_processor_method(column, i=0)
        self.assertEqual(f, dt.fake_callback)

        # Test callback found on callback_target
        dt = Datatable([], '/', callback_target=view)
        f = dt.get_processor_method(column, i=0)
        self.assertEqual(f, view.fake_callback)
    def test_get_records_populates_cache(self):
        ExampleModel.objects.create(name='test name')
        queryset = ExampleModel.objects.all()

        dt = Datatable(queryset, '/')
        dt.get_records()
        self.assertIsNotNone(dt._records)
        records = dt._records

        # _records doesn't change when run again
        dt.get_records()
        self.assertEqual(dt._records, records)
    def test_get_ordering_splits(self):
        # Verify empty has blank db-backed list and virtual list
        dt = Datatable([], '/')
        dt.configure()
        self.assertEqual(dt.get_ordering_splits(), ([], []))

        class DT(Datatable):
            fake = TextColumn('Fake', sources=['get_absolute_url'])

            class Meta:
                model = ExampleModel
                columns = ['name', 'fake']

        # Verify a fake field name ends up separated from the db-backed field
        dt = DT([],
                '/',
                query_config={
                    'order[0][column]': '0',
                    'order[0][dir]': 'asc'
                })  # iSortingCols': '1',
        dt.configure()
        self.assertEqual(dt.get_ordering_splits(), (['name'], []))

        # Verify ['name', 'fake'] ordering sends 'name' to db sort list, but keeps 'fake' in manual
        # sort list.
        dt = DT(
            [],
            '/',
            query_config={
                'order[0][column]': '0',
                'order[0][dir]': 'asc',
                'order[1][column]': '1',
                'order[1][dir]': 'asc'
            })  # 'iSortingCols': '2',
        dt.configure()
        self.assertEqual(dt.get_ordering_splits(), (['name'], ['fake']))

        # Verify a fake field name as the sort column correctly finds no db sort fields
        dt = DT([],
                '/',
                query_config={
                    'order[0][column]': '1',
                    'order[0][dir]': 'asc'
                })  # 'iSortingCols': '1',
        dt.configure()
        self.assertEqual(dt.get_ordering_splits(), ([], ['fake']))

        # Verify ['fake', 'name'] ordering sends both fields to manual sort list
        dt = DT(
            [],
            '/',
            query_config={
                'order[0][column]': '1',
                'order[0][dir]': 'asc',
                'order[1][column]': '0',
                'order[1][dir]': 'asc'
            })  # 'iSortingCols': '2',
        dt.configure()
        self.assertEqual(dt.get_ordering_splits(), ([], ['fake', 'name']))
    def test_get_processor_method_finds_implied_callback(self):
        class DummyNamed(object):
            def get_column_fake_data(self):
                pass

        class DummyIndexed(object):
            def get_column_0_data(self):
                pass

        class DummyBoth(object):
            def get_column_fake_data(self):
                pass

            def get_column_0_data(self):
                pass

        column = Column('Fake', sources=[])
        column.name = 'fake'

        # Test implied named callback found first
        view = DummyNamed()
        dt = Datatable([], '/', callback_target=view)
        f = dt.get_processor_method(column, i=0)
        self.assertEqual(f, view.get_column_fake_data)

        # Test implied named callback found first
        view = DummyIndexed()
        dt = Datatable([], '/', callback_target=view)
        f = dt.get_processor_method(column, i=0)
        self.assertEqual(f, view.get_column_0_data)

        # Test implied named callback found first
        view = DummyBoth()
        dt = Datatable([], '/', callback_target=view)
        f = dt.get_processor_method(column, i=0)
        self.assertEqual(f, view.get_column_fake_data)

        class DTNamed(Datatable):
            def get_column_fake_data(self):
                pass

        class DTIndexed(Datatable):
            def get_column_0_data(self):
                pass

        class DTBoth(Datatable):
            def get_column_fake_data(self):
                pass

            def get_column_0_data(self):
                pass

        # Test implied named callback found first
        dt = DTNamed([], '/')
        f = dt.get_processor_method(column, i=0)
        self.assertEqual(f, dt.get_column_fake_data)

        # Test implied named callback found first
        dt = DTIndexed([], '/')
        f = dt.get_processor_method(column, i=0)
        self.assertEqual(f, dt.get_column_0_data)

        # Test implied named callback found first
        dt = DTBoth([], '/')
        f = dt.get_processor_method(column, i=0)
        self.assertEqual(f, dt.get_column_fake_data)
 def test_get_object_pk(self):
     obj1 = ExampleModel.objects.create(name='test name 1')
     queryset = ExampleModel.objects.all()
     dt = Datatable(queryset, '/')
     self.assertEqual(dt.get_object_pk(obj1), obj1.pk)