From 0bb23fa39ae82e97bf4a1db4fef611b1e43d0d15 Mon Sep 17 00:00:00 2001 From: Jan Pieter Waagmeester Date: Mon, 12 Mar 2018 18:47:53 +0100 Subject: [PATCH] Remove default addition of the column name to / class-attribute As mentioned in #370: https://github.com/jieter/django-tables2/pull/370#issuecomment-241012232 --- CHANGELOG.md | 14 ++++- django_tables2/columns/base.py | 11 ++-- django_tables2/tables.py | 15 ++++- docs/pages/column-attributes.rst | 6 +- tests/columns/test_general.py | 62 +++++++------------ tests/columns/test_linkcolumn.py | 45 ++++++-------- tests/columns/test_templatecolumn.py | 30 ++++----- tests/test_core.py | 7 +-- .../test_dynamically_add_show_hide_columns.py | 34 +++++----- tests/test_faq.py | 8 +-- tests/test_footer.py | 4 +- tests/test_templates.py | 7 +-- tests/test_views.py | 32 +++++----- 13 files changed, 134 insertions(+), 141 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62c20106..e679b86d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,18 @@ - Cleaned up templates to add consistancy in what is presented accross all templates. - Added bootstrap4.html template - Fixed translation inconsistancies. - - **breaking change** removed the `template` argument to the table constructor, use `template_name` instead. - - +### breaking changes + - Appearance of the paginators might be different from the current 1.x templates. Use a custom template if you need to keep the appearance the same. + - Removed the `template` argument to the table constructor, use `template_name` instead. + - Stopped adding column names to the class attribute of table cells (`` tags) by default. Previous behaviour can be restored by using this method on your custom table: +```python +class MyTable(tables.Table): + # columns + def get_column_class_names(self, classes_set, bound_column): + classes_set = super(MyTable, self).get_column_class_names(classes_set, bound_column) + classes_set.add(bound_column.name) + return classes_set +``` ## 1.21.2 (2018-03-26) - Moved table instantiation from `get_context_data` to `get_tables` [#554](https://github.com/jieter/django-tables2/pull/554) by [@sdolemelipone](https://github.com/sdolemelipone) diff --git a/django_tables2/columns/base.py b/django_tables2/columns/base.py index fede3fea..d07403d6 100644 --- a/django_tables2/columns/base.py +++ b/django_tables2/columns/base.py @@ -328,6 +328,7 @@ def attrs(self): # we take the value for 'cell' as the basis for both the th and td attrs cell_attrs = attrs.get('cell', {}) + # override with attrs defined specifically for th and td respectively. attrs['th'] = computed_values(attrs.get('th', cell_attrs), kwargs=kwargs) attrs['td'] = computed_values(attrs.get('td', cell_attrs), kwargs=kwargs) @@ -347,11 +348,10 @@ def attrs(self): def _get_cell_class(self, attrs): ''' - return a set of the classes from the class key in ``attrs``, augmented - with the column name (or anything else added by Table.get_column_class_names()). + Return a set of the classes from the class key in ``attrs``. ''' classes = attrs.get('class', None) - classes = set() if classes is None else {c for c in classes.split(' ') if c} + classes = set() if classes is None else set(classes.split(' ')) return self._table.get_column_class_names(classes, self) @@ -359,7 +359,8 @@ def get_td_class(self, td_attrs): ''' Returns the HTML class attribute for a data cell in this column ''' - return ' '.join(sorted(self._get_cell_class(td_attrs))) + classes = sorted(self._get_cell_class(td_attrs)) + return None if len(classes) == 0 else ' '.join(classes) def get_th_class(self, th_attrs): ''' @@ -376,7 +377,7 @@ def get_th_class(self, th_attrs): if self.order_by_alias.is_descending else ordering_class.get('ascending', 'asc')) - return ' '.join(sorted(classes)) + return None if len(classes) == 0 else ' '.join(classes) @property def default(self): diff --git a/django_tables2/tables.py b/django_tables2/tables.py index fcea4e18..50d25300 100644 --- a/django_tables2/tables.py +++ b/django_tables2/tables.py @@ -595,7 +595,7 @@ def get_column_class_names(self, classes_set, bound_column): Returns a set of HTML class names for cells (both td and th) of a **bound column** in this table. By default this returns the column class names defined in the table's - attributes, and additionally the bound column's name. + attributes. This method can be overridden to change the default behavior, for example to simply `return classes_set`. @@ -611,8 +611,19 @@ def get_column_class_names(self, classes_set, bound_column): Returns: A set of class names to be added to cells of this column + + If you want to add the column names to the list of classes for a column, + override this method in your custom table:: + + class MyTable(tables.Table): + ... + + def get_column_class_names(self, classes_set, bound_column): + classes_set = super(MyTable, self).get_column_class_names(classes_set, bound_column) + classes_set.add(bound_column.name) + + return classes_set ''' - classes_set.add(bound_column.name) return classes_set diff --git a/docs/pages/column-attributes.rst b/docs/pages/column-attributes.rst index b538f0c9..8833262b 100644 --- a/docs/pages/column-attributes.rst +++ b/docs/pages/column-attributes.rst @@ -17,12 +17,10 @@ Depending on the column, different elements are supported, however ``th``, ... name = tables.Column(attrs={'th': {'id': 'foo'}}) ... >>> # will render something like this: - '{snip}{snip}{snip}' + '{snip}{snip}{snip}' -For ``th`` and ``td``, the column name will be added as a class name. This makes -selecting the row for styling easier. Have a look at each column's API -reference to find which elements are supported. +Have a look at each column's API reference to find which elements are supported. If you need to add some extra attributes to column's tags rendered in the footer, use key name ``tf``, as described in section on :ref:`css`. diff --git a/tests/columns/test_general.py b/tests/columns/test_general.py index 8d16ccba..cf1819ff 100644 --- a/tests/columns/test_general.py +++ b/tests/columns/test_general.py @@ -256,18 +256,9 @@ class SimpleTable(tables.Table): table = SimpleTable([{'a': 'value'}]) root = parse(table.as_html(request)) - assert root.findall('.//thead/tr/th')[0].attrib == {'key': 'value', 'class': 'a orderable'} - assert root.findall('.//tbody/tr/td')[0].attrib == {'key': 'value', 'class': 'a'} - assert root.findall('.//tfoot/tr/td')[0].attrib == {'key': 'value', 'class': 'a'} - - def test_cells_are_automatically_given_column_name_as_class(self): - class SimpleTable(tables.Table): - a = tables.Column() - - table = SimpleTable([{'a': 'value'}]) - root = parse(table.as_html(request)) - assert root.findall('.//thead/tr/th')[0].attrib == {'class': 'a orderable'} - assert root.findall('.//tbody/tr/td')[0].attrib == {'class': 'a'} + assert root.findall('.//thead/tr/th')[0].attrib == {'key': 'value', 'class': 'orderable'} + assert root.findall('.//tbody/tr/td')[0].attrib == {'key': 'value'} + assert root.findall('.//tfoot/tr/td')[0].attrib == {'key': 'value'} def test_th_are_given_orderable_class_if_column_is_orderable(self): class SimpleTable(tables.Table): @@ -277,9 +268,9 @@ class SimpleTable(tables.Table): table = SimpleTable([{'a': 'value'}]) root = parse(table.as_html(request)) # return classes of an element as a set - classes = lambda x: set(x.attrib['class'].split()) - assert 'orderable' in classes(root.findall('.//thead/tr/th')[0]) - assert 'orderable' not in classes(root.findall('.//thead/tr/th')[1]) + classes = lambda x: set(x.attrib.get('class', '').split()) + self.assertIn('orderable', classes(root.findall('.//thead/tr/th')[0])) + self.assertNotIn('orderable', classes(root.findall('.//thead/tr/th')[1])) # Now try with an ordered table table = SimpleTable([], order_by='a') @@ -443,10 +434,10 @@ class Table(tables.Table): table = Table(Person.objects.all()) html = table.as_html(request) # cell should affect both and - assert '' in html - assert '' in html + self.assertIn('', html) + self.assertIn('', html) # td should only affect - assert '' in html + self.assertIn('', html) def test_computable_td_attrs_defined_in_column_class_attribute(self): '''Computable attrs for columns, using custom Column''' @@ -465,14 +456,8 @@ class Table(tables.Table): html = table.as_html(request) root = parse(html) - assert root.findall('.//tbody/tr/td')[0].attrib == { - 'data-test': '2', - 'class': 'last_name' - } - assert root.findall('.//tbody/tr/td')[1].attrib == { - 'data-test': '2', - 'class': 'last_name' - } + self.assertEqual(root.findall('.//tbody/tr/td')[0].attrib, {'data-test': '2'}) + self.assertEqual(root.findall('.//tbody/tr/td')[1].attrib, {'data-test': '2'}) def test_computable_td_attrs_defined_in_column_class_attribute_record(self): '''Computable attrs for columns, using custom column''' @@ -495,11 +480,10 @@ class Table(tables.Table): html = table.as_html(request) root = parse(html) - assert root.findall('.//tbody/tr/td')[0].attrib == { + self.assertEqual(root.findall('.//tbody/tr/td')[0].attrib, { 'data-first-name': 'Jan', - 'data-last-name': 'Pietersz.', - 'class': 'person' - } + 'data-last-name': 'Pietersz.' + }) def test_computable_column_td_attrs_record_header(self): ''' @@ -523,15 +507,15 @@ class Table(tables.Table): html = table.as_html(request) root = parse(html) - assert root.findall('.//thead/tr/th')[0].attrib == { - 'class': 'first_name orderable', + self.assertEqual(root.findall('.//thead/tr/th')[0].attrib, { + 'class': 'orderable', 'data-first-name': 'header', - } - assert root.findall('.//tbody/tr/td')[0].attrib == { - 'class': 'first_name status-Jan', + }) + self.assertEqual(root.findall('.//tbody/tr/td')[0].attrib, { + 'class': 'status-Jan', 'data-first-name': 'Jan', - } - assert root.findall('.//tbody/tr/td')[1].attrib == { - 'class': 'first_name status-Sjon', + }) + self.assertEqual(root.findall('.//tbody/tr/td')[1].attrib, { + 'class': 'status-Sjon', 'data-first-name': 'Sjon', - } + }) diff --git a/tests/columns/test_linkcolumn.py b/tests/columns/test_linkcolumn.py index 6b298434..7ac7dcc9 100644 --- a/tests/columns/test_linkcolumn.py +++ b/tests/columns/test_linkcolumn.py @@ -31,10 +31,10 @@ class UnicodeTable(tables.Table): 'table': UnicodeTable(dataset) })) - assert 'Brädley' in html - assert '∆yers' in html - assert 'Chr…s' in html - assert 'DÒble' in html + self.assertIn('Brädley', html) + self.assertIn('∆yers', html) + self.assertIn('Chr…s', html) + self.assertIn('DÒble', html) def test_link_text_custom_value(self): class CustomLinkTable(tables.Table): @@ -51,8 +51,8 @@ class CustomLinkTable(tables.Table): html = CustomLinkTable(dataset).as_html(build_request()) - assert 'foo::bar' in html - assert 'Doe John' in html + self.assertIn('foo::bar', html) + self.assertIn('Doe John', html) def test_link_text_escaping(self): class CustomLinkTable(tables.Table): @@ -68,10 +68,8 @@ class CustomLinkTable(tables.Table): html = CustomLinkTable(dataset).as_html(build_request()) - expected = 'edit'.format( - reverse('person', args=(1, )) - ) - assert expected in html + expected = 'edit'.format(reverse('person', args=(1, ))) + self.assertIn(expected, html) def test_null_foreign_key(self): class PersonTable(tables.Table): @@ -84,7 +82,7 @@ class PersonTable(tables.Table): table = PersonTable(Person.objects.all()) html = table.as_html(build_request()) - assert '—' in html + self.assertIn('—', html) def test_linkcolumn_non_field_based(self): '''Test for issue 257, non-field based columns''' @@ -96,10 +94,10 @@ class Table(tables.Table): html = Table(Person.objects.all()).as_html(build_request()) - expected = 'delete'.format( + expected = 'delete'.format( reverse('person_delete', kwargs={'pk': willem.pk}) ) - assert expected in html + self.assertIn(expected, html) def test_kwargs(self): class PersonTable(tables.Table): @@ -107,15 +105,15 @@ class PersonTable(tables.Table): table = PersonTable([{'a': 0}, {'a': 1}]) - assert reverse('occupation', kwargs={'pk': 0}) in table.rows[0].get_cell('a') - assert reverse('occupation', kwargs={'pk': 1}) in table.rows[1].get_cell('a') + self.assertIn(reverse('occupation', kwargs={'pk': 0}), table.rows[0].get_cell('a')) + self.assertIn(reverse('occupation', kwargs={'pk': 1}), table.rows[1].get_cell('a')) def test_html_escape_value(self): class PersonTable(tables.Table): name = tables.LinkColumn('escaping', kwargs={'pk': A('pk')}) table = PersonTable([{'name': '', 'pk': 1}]) - assert table.rows[0].get_cell('name') == '<brad>' + self.assertEqual(table.rows[0].get_cell('name'), '<brad>') def test_a_attrs_should_be_supported(self): class TestTable(tables.Table): @@ -123,10 +121,10 @@ class TestTable(tables.Table): attrs={'a': {'title': 'Occupation Title'}}) table = TestTable([{'col': 0}]) - assert attrs(table.rows[0].get_cell('col')) == { + self.assertEqual(attrs(table.rows[0].get_cell('col')), { 'href': reverse('occupation', kwargs={'pk': 0}), 'title': 'Occupation Title' - } + }) def test_td_attrs_should_be_supported(self): '''LinkColumn should support both and attrs''' @@ -143,16 +141,13 @@ class Table(tables.Table): table = Table(Person.objects.all()) a_tag = table.rows[0].get_cell('first_name') - assert 'href="{}"'.format(reverse('person', args=(person.pk, ))) in a_tag - assert 'style="color: red;"' in a_tag - assert person.first_name in a_tag + self.assertIn('href="{}"'.format(reverse('person', args=(person.pk, ))), a_tag) + self.assertIn('style="color: red;"', a_tag) + self.assertIn(person.first_name, a_tag) html = table.as_html(build_request()) - td_tag_1 = '' - td_tag_2 = '' - - assert td_tag_1 in html or td_tag_2 in html + self.assertIn('', html) def test_defaults(self): class Table(tables.Table): diff --git a/tests/columns/test_templatecolumn.py b/tests/columns/test_templatecolumn.py index f6291ccc..7cd08381 100644 --- a/tests/columns/test_templatecolumn.py +++ b/tests/columns/test_templatecolumn.py @@ -25,12 +25,12 @@ def get_top_pinned_data(self): table = TestOnlyPinnedTable([{'foo': 'bar'}]) for row in table.rows: - assert row.get_cell('foo') == 'value=bar' + self.assertEqual(row.get_cell('foo'), 'value=bar') template = Template('{% load django_tables2 %}{% render_table table %}') html = template.render(Context({'request': build_request(), 'table': table})) - assert 'value=bar' in html + self.assertIn('value=bar', html) def test_should_handle_context_on_table(self): class TestTable(tables.Table): @@ -40,47 +40,47 @@ class TestTable(tables.Table): extra_context={'label': 'label'}) table = TestTable([{'col': 'brad'}]) - assert table.rows[0].get_cell('col_code') == 'code:brad-' - assert table.rows[0].get_cell('col_name') == 'name:brad-empty\n' - assert table.rows[0].get_cell("col_context") == "label:brad-" + self.assertEqual(table.rows[0].get_cell('col_code'), 'code:brad-') + self.assertEqual(table.rows[0].get_cell('col_name'), 'name:brad-empty\n') + self.assertEqual(table.rows[0].get_cell('col_context'), 'label:brad-') table.context = Context({'foo': 'author'}) - assert table.rows[0].get_cell('col_code') == 'code:brad-author' - assert table.rows[0].get_cell('col_name') == 'name:brad-author\n' - assert table.rows[0].get_cell("col_context") == "label:brad-author" + self.assertEqual(table.rows[0].get_cell('col_code'), 'code:brad-author') + self.assertEqual(table.rows[0].get_cell('col_name'), 'name:brad-author\n') + self.assertEqual(table.rows[0].get_cell('col_context'), 'label:brad-author') # new table and render using the 'render_table' template tag. table = TestTable([{'col': 'brad'}]) template = Template('{% load django_tables2 %}{% render_table table %}') html = template.render(Context({'request': build_request(), 'table': table, 'foo': 'author'})) - assert 'name:brad-author\n' in html + self.assertIn('name:brad-author\n', html) def test_should_support_default(self): class Table(tables.Table): foo = tables.TemplateColumn('default={{ default }}', default='bar') table = Table([{}]) - assert table.rows[0].get_cell('foo') == 'default=bar' + self.assertEqual(table.rows[0].get_cell('foo'), 'default=bar') def test_should_support_value(self): class Table(tables.Table): foo = tables.TemplateColumn('value={{ value }}') table = Table([{'foo': 'bar'}]) - assert table.rows[0].get_cell('foo') == 'value=bar' + self.assertEqual(table.rows[0].get_cell('foo'), 'value=bar') template = Template('{% load django_tables2 %}{% render_table table %}') html = template.render(Context({'request': build_request(), 'table': table})) - assert 'value=bar' in html + self.assertIn('value=bar', html) def test_should_support_column(self): class Table(tables.Table): tcol = tables.TemplateColumn('column={{ column.name }}') table = Table([{'foo': 'bar'}]) - assert table.rows[0].get_cell('tcol') == 'column=tcol' + self.assertEqual(table.rows[0].get_cell('tcol'), 'column=tcol') def test_should_raise_when_called_without_template(self): with self.assertRaises(ValueError): @@ -95,7 +95,7 @@ class Table(tables.Table): track = tables.TemplateColumn('track: {{ value }}') table = Table([{'track': 'Beat it {Freestyle}'}]) - assert table.rows[0].get_cell('track') == 'track: Beat it {Freestyle}' + self.assertEqual(table.rows[0].get_cell('track'), 'track: Beat it {Freestyle}') def test_should_strip_tags_for_value(self): class Table(tables.Table): @@ -103,4 +103,4 @@ class Table(tables.Table): table = Table([{'track': 'Space Oddity'}]) - assert list(table.as_values()) == [['Track'], ['Space Oddity']] + self.assertEqual(list(table.as_values()), [['Track'], ['Space Oddity']]) diff --git a/tests/test_core.py b/tests/test_core.py index 6d14253b..d4f7ada8 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -119,7 +119,7 @@ class FlippedTweakedTableBase(tables.Table): self.assertIn('name', table.columns) self.assertTrue(table.tweaked) - def test_attrs(self): + def test_table_attrs(self): class TestTable(tables.Table): class Meta: attrs = {} @@ -635,7 +635,4 @@ class Meta: table = Table(MEMORY_DATA) html = table.as_html(request) td = parse(html).find('.//tbody/tr[1]/td[1]') - self.assertEqual(td.attrib, { - 'data-column-name': 'alpha', - 'class': 'alpha' - }) + self.assertEqual(td.attrib, {'data-column-name': 'alpha'}) diff --git a/tests/test_dynamically_add_show_hide_columns.py b/tests/test_dynamically_add_show_hide_columns.py index 0829b54f..fd0b91c6 100644 --- a/tests/test_dynamically_add_show_hide_columns.py +++ b/tests/test_dynamically_add_show_hide_columns.py @@ -34,14 +34,14 @@ class MyTable(tables.Table): name = tables.Column() # this is obvious: - assert list(MyTable(data).columns.columns.keys()) == ['name'] + self.assertEqual(list(MyTable(data).columns.columns.keys()), ['name']) - assert list(MyTable(data, extra_columns=[ + self.assertEqual(list(MyTable(data, extra_columns=[ ('country', tables.Column()) - ]).columns.columns.keys()) == ['name', 'country'] + ]).columns.columns.keys()), ['name', 'country']) # this new instance should not have the extra columns added to the first instance. - assert list(MyTable(data).columns.columns.keys()) == ['name'] + self.assertEqual(list(MyTable(data).columns.columns.keys()), ['name']) def test_sorting_on_dynamically_added_columns(self): class MyTable(tables.Table): @@ -52,8 +52,8 @@ class MyTable(tables.Table): ]) root = parse(table.as_html(build_request())) - assert root.find('.//tbody/tr/td[2]').text == 'Chile' - assert root.find('.//tbody/tr[4]/td[2]').text == 'Australia' + self.assertEqual(root.find('.//tbody/tr/td[2]').text, 'Chile') + self.assertEqual(root.find('.//tbody/tr[4]/td[2]').text, 'Australia') def test_dynamically_override_auto_generated_columns(self): for name, country in data: @@ -65,15 +65,15 @@ class Meta: model = Person fields = ('first_name', 'last_name') - assert list(MyTable(queryset).columns.columns.keys()) == ['first_name', 'last_name'] + self.assertEqual(list(MyTable(queryset).columns.columns.keys()), ['first_name', 'last_name']) table = MyTable(queryset, extra_columns=[ ('first_name', tables.Column(attrs={'td': {'style': 'color: red;'}})) ]) # we still should have two columns - assert list(table.columns.columns.keys()) == ['first_name', 'last_name'] + self.assertEqual(list(table.columns.columns.keys()), ['first_name', 'last_name']) # the attrs should be applied to the `first_name` column - assert table.columns['first_name'].attrs['td'] == {'class': 'first_name', 'style': 'color: red;'} + self.assertEqual(table.columns['first_name'].attrs['td'], {'style': 'color: red;', 'class': None}) def test_dynamically_add_column_with_sequence(self): class MyTable(tables.Table): @@ -109,18 +109,18 @@ def before_render(self, request): table = MyTable(data) request = build_request(user=User.objects.create(username='Bob')) html = table.as_html(request) - assert 'Name' in html - assert 'Country' not in html + self.assertIn('Name', html) + self.assertNotIn('Country', html) html = template.render(Context({'request': request, 'table': table})) - assert 'Name' in html - assert 'Country' not in html + self.assertIn('Name', html) + self.assertNotIn('Country', html) request = build_request(user=User.objects.create(username='Alice')) html = table.as_html(request) - assert 'Name' in html - assert 'Country' in html + self.assertIn('Name', html) + self.assertIn('Country', html) html = template.render(Context({'request': request, 'table': table})) - assert 'Name' in html - assert 'Country' in html + self.assertIn('Name', html) + self.assertIn('Country', html) diff --git a/tests/test_faq.py b/tests/test_faq.py index 2d08ecd4..8425347e 100644 --- a/tests/test_faq.py +++ b/tests/test_faq.py @@ -17,16 +17,16 @@ class CountryTable(tables.Table): counter = tables.TemplateColumn('{{ row_counter }}') name = tables.Column() - expected = '0' + expected = '0' table = CountryTable(TEST_DATA) html = table.as_html(build_request()) - assert expected in html + self.assertIn(expected, html) # the counter should start at zero the second time too table = CountryTable(TEST_DATA) html = table.as_html(build_request()) - assert expected in html + self.assertIn(expected, html) def test_row_footer_total(self): class CountryTable(tables.Table): @@ -41,4 +41,4 @@ class CountryTable(tables.Table): html = table.as_html(build_request()) columns = parse(html).findall('.//tfoot/tr')[-1].findall('td') - assert columns[1].text == 'Total: 77740000' + self.assertEqual(columns[1].text, 'Total: 77740000') diff --git a/tests/test_footer.py b/tests/test_footer.py index 6b59bf7b..692e2bb4 100644 --- a/tests/test_footer.py +++ b/tests/test_footer.py @@ -78,13 +78,13 @@ def render_footer(self, bound_column, table): class TestTable(tables.Table): name = tables.Column() country = tables.Column(footer='Total:') - population = SummingColumn() + population = SummingColumn(attrs={'tf': {'class': 'population_sum'}}) table = TestTable(MEMORY_DATA) html = table.as_html(build_request('/')) columns = parse(html).findall('.//tfoot/tr/td') - assert 'class' in columns[1].attrib + self.assertEqual(columns[2].attrib, {'class': 'population_sum'}) def test_footer_custom_attriubtes(self): class SummingColumn(tables.Column): diff --git a/tests/test_templates.py b/tests/test_templates.py index c5b4b65d..3008f31b 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -151,16 +151,13 @@ def assert_cond_localized_table(self, localizeit=None, expected=None): helper function for defining Table class conditionally ''' class TestTable(tables.Table): - name = tables.Column(verbose_name="my column", localize=localizeit) + name = tables.Column(verbose_name='my column', localize=localizeit) self.assert_table_localization(TestTable, expected) def assert_table_localization(self, TestTable, expected): html = TestTable(self.simple_test_data).as_html(build_request()) - self.assertIn( - '{0}'.format(self.expected_results[expected]), - html - ) + self.assertIn('{0}'.format(self.expected_results[expected]), html) def test_localization_check(self): self.assert_cond_localized_table(None, None) diff --git a/tests/test_views.py b/tests/test_views.py index 87f922b1..9c29001c 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -127,7 +127,7 @@ def get_queryset(self): return Region.objects.all().order_by('name') response, view = PaginateByDefinedOnView.as_view()(build_request('/')) - assert view.get_table().paginator.per_page == 2 + self.assertEqual(view.get_table().paginator.per_page, 2) def test_should_pass_kwargs_to_table_constructor(self): class PassKwargsView(SimpleView): @@ -138,10 +138,10 @@ def get_table(self, **kwargs): return super(PassKwargsView, self).get_table(**kwargs) response, view = SimpleView.as_view()(build_request('/')) - assert view.get_table().orderable is True + self.assertTrue(view.get_table().orderable) response, view = PassKwargsView.as_view()(build_request('/')) - assert view.get_table().orderable is False + self.assertFalse(view.get_table().orderable) def test_should_override_table_pagination(self): class PrefixedTable(SimpleTable): @@ -289,11 +289,11 @@ class View(tables.MultiTableMixin, TemplateView): html = response.rendered_content - assert 'table_0-sort=first_name' in html - assert 'table_1-sort=name' in html + self.assertIn('table_0-sort=first_name', html) + self.assertIn('table_1-sort=name', html) - assert 'Jan Pieter' in html - assert 'Zuid-Holland' in html + self.assertIn('Jan Pieter', html) + self.assertIn('Zuid-Holland', html) def test_supplying_instances(self): class View(tables.MultiTableMixin, TemplateView): @@ -308,11 +308,11 @@ class View(tables.MultiTableMixin, TemplateView): html = response.rendered_content - assert 'table_0-sort=first_name' in html - assert 'table_1-sort=name' in html + self.assertIn('table_0-sort=first_name', html) + self.assertIn('table_1-sort=name', html) - assert 'Jan Pieter' in html - assert 'Zuid-Holland' in html + self.assertIn('Jan Pieter', html) + self.assertIn('Zuid-Holland', html) def test_without_tables(self): class View(tables.MultiTableMixin, TemplateView): @@ -343,7 +343,7 @@ class View(tables.MultiTableMixin, TemplateView): response.render() html = response.rendered_content - assert '

Multiple tables using MultiTableMixin

' in html + self.assertIn('

Multiple tables using MultiTableMixin

', html) def test_length_mismatch(self): class View(tables.MultiTableMixin, TemplateView): @@ -370,8 +370,8 @@ class View(DispatchHookMixin, tables.MultiTableMixin, TemplateView): tableA, tableB = view.get_tables() - assert tableA.page.number == 1 - assert tableB.page.number == 3 + self.assertEqual(tableA.page.number, 1) + self.assertEqual(tableB.page.number, 3) def test_get_tables_data(self): class View(tables.MultiTableMixin, TemplateView): @@ -386,5 +386,5 @@ def get_tables_data(self): html = response.rendered_content - assert 'Jan Pieter' in html - assert 'Zuid-Holland' in html + self.assertIn('Jan Pieter', html) + self.assertIn('Zuid-Holland', html)