Skip to content

Commit

Permalink
Added table_factory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuluPro committed Jan 17, 2018
1 parent 6f8892e commit 37cbd46
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 3 additions & 4 deletions django_tables2/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,15 +622,14 @@ class Table(TableBase):
# Table = DeclarativeColumnsMetaclass(str('Table'), (TableBase, ), {})


def table_factory(model, table=Table, fields=None, exclude=None,
localized_columns=None):
def table_factory(model, table=Table, fields=None, exclude=None, localize=None):
attrs = {'model': model}
if fields is not None:
attrs['fields'] = fields
if exclude is not None:
attrs['exclude'] = exclude
if localized_columns is not None:
attrs['localized_columns'] = localized_columns
if localize is not None:
attrs['localize'] = localize
# If parent form class already has an inner Meta, the Meta we're
# creating needs to inherit from the parent's inner meta.
parent = (object,)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,28 @@ def test_factory(self):
Table = tables.table_factory(Person)
table = Table(persons)
self.assertIsInstance(table, tables.Table)
self.assertEqual(Table.__name__, 'PersonTable')

def test_factory_fields_argument(self):
fields = ('username',)
Table = tables.table_factory(Person, fields=fields)
self.assertEqual(Table.Meta.fields, fields)
self.assertEqual(Table._meta.fields, fields)

def test_factory_exclude_argument(self):
exclude = ('username',)
Table = tables.table_factory(Person, exclude=exclude)
self.assertEqual(Table.Meta.exclude, exclude)
self.assertEqual(Table._meta.exclude, exclude)

def test_factory_localize_argument(self):
localize = ('username',)
Table = tables.table_factory(Person, localize=localize)
self.assertEqual(Table.Meta.localize, localize)
self.assertEqual(Table._meta.localize, localize)

def test_factory_with_meta(self):
localize = ('username',)
class Meta:
fields = ('first_name',)
Table = tables.table_factory(Person, localize=localize)

0 comments on commit 37cbd46

Please sign in to comment.