Skip to content

Commit

Permalink
Amend tests for #316, and some double to single quote conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Apr 23, 2016
1 parent 7dd258b commit c692885
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 190 deletions.
7 changes: 4 additions & 3 deletions django_tables2/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8
from __future__ import unicode_literals

from django.core.paginator import EmptyPage, PageNotAnInteger


Expand Down Expand Up @@ -37,13 +38,13 @@ def configure(self, table):
if order_by:
table.order_by = order_by
if self.paginate:
if hasattr(self.paginate, "items"):
if hasattr(self.paginate, 'items'):
kwargs = dict(self.paginate)
else:
kwargs = {}
# extract some options from the request
for arg in ("page", "per_page"):
name = getattr(table, "prefixed_%s_field" % arg)
for arg in ('page', 'per_page'):
name = getattr(table, 'prefixed_%s_field' % arg)
try:
kwargs[arg] = int(self.request.GET[name])
except (ValueError, KeyError):
Expand Down
30 changes: 15 additions & 15 deletions tests/columns/test_booleancolumn.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# coding: utf-8
from __future__ import unicode_literals

import pytest
from django.db import models

import django_tables2 as tables
import pytest

from ..app.models import Occupation, Person
from ..utils import attrs
Expand All @@ -21,7 +21,7 @@ class Table(tables.Table):
class Meta:
model = BoolModel

column = Table.base_columns["field"]
column = Table.base_columns['field']
assert type(column) == tables.BooleanColumn
assert column.empty_values != ()

Expand All @@ -37,33 +37,33 @@ class Table(tables.Table):
class Meta:
model = NullBoolModel

column = Table.base_columns["field"]
column = Table.base_columns['field']
assert type(column) == tables.BooleanColumn
assert column.empty_values == ()


def test_treat_none_different_from_false():
class Table(tables.Table):
col = tables.BooleanColumn(null=False, default="---")
col = tables.BooleanColumn(null=False, default='---')

table = Table([{"col": None}])
assert table.rows[0]["col"] == "---"
table = Table([{'col': None}])
assert table.rows[0].get_cell('col') == '---'


def test_treat_none_as_false():
class Table(tables.Table):
col = tables.BooleanColumn(null=True)

table = Table([{"col": None}])
assert table.rows[0]["col"] == '<span class="false">✘</span>'
table = Table([{'col': None}])
assert table.rows[0].get_cell('col') == '<span class="false">✘</span>'


def test_span_attrs():
class Table(tables.Table):
col = tables.BooleanColumn(attrs={"span": {"key": "value"}})
col = tables.BooleanColumn(attrs={'span': {'key': 'value'}})

table = Table([{"col": True}])
assert attrs(table.rows[0]["col"]) == {"class": "true", "key": "value"}
table = Table([{'col': True}])
assert attrs(table.rows[0].get_cell('col')) == {'class': 'true', 'key': 'value'}


def test_boolean_field_choices_with_real_model_instances():
Expand All @@ -86,8 +86,8 @@ class Meta:

table = Table([BoolModel(field=True), BoolModel(field=False)])

assert table.rows[0]['field'] == '<span class="true">✔</span>'
assert table.rows[1]['field'] == '<span class="false">✘</span>'
assert table.rows[0].get_cell('field') == '<span class="true">✔</span>'
assert table.rows[1].get_cell('field') == '<span class="false">✘</span>'


@pytest.mark.django_db
Expand All @@ -108,5 +108,5 @@ class Meta:
Person(first_name='True', last_name='False', occupation=model_false)
])

assert table.rows[0]['boolean'] == '<span class="true">✔</span>'
assert table.rows[1]['boolean'] == '<span class="false">✘</span>'
assert table.rows[0].get_cell('boolean') == '<span class="true">✔</span>'
assert table.rows[1].get_cell('boolean') == '<span class="false">✘</span>'
58 changes: 39 additions & 19 deletions tests/columns/test_checkboxcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,42 @@

def test_new_attrs_should_be_supported():
class TestTable(tables.Table):
col1 = tables.CheckBoxColumn(attrs=dict(th__input={"th_key": "th_value"},
td__input={"td_key": "td_value"}))
col2 = tables.CheckBoxColumn(attrs=dict(input={"key": "value"}))

table = TestTable([{"col1": "data", "col2": "data"}])
assert attrs(table.columns["col1"].header) == {"type": "checkbox", "th_key": "th_value"}
assert attrs(table.rows[0]["col1"]) == {"type": "checkbox", "td_key": "td_value", "value": "data", "name": "col1"}
assert attrs(table.columns["col2"].header) == {"type": "checkbox", "key": "value"}
assert attrs(table.rows[0]["col2"]) == {"type": "checkbox", "key": "value", "value": "data", "name": "col2"}
col1 = tables.CheckBoxColumn(attrs=dict(th__input={'th_key': 'th_value'},
td__input={'td_key': 'td_value'}))
col2 = tables.CheckBoxColumn(attrs=dict(input={'key': 'value'}))

table = TestTable([{'col1': 'data', 'col2': 'data'}])
assert attrs(table.columns['col1'].header) == {'type': 'checkbox', 'th_key': 'th_value'}
assert attrs(table.rows[0].get_cell('col1')) == {
'type': 'checkbox',
'td_key': 'td_value',
'value': 'data',
'name': 'col1'
}
assert attrs(table.columns['col2'].header) == {'type': 'checkbox', 'key': 'value'}
assert attrs(table.rows[0].get_cell('col2')) == {
'type': 'checkbox',
'key': 'value',
'value': 'data',
'name': 'col2'
}


def test_column_is_checked():
class TestTable(tables.Table):
col = tables.CheckBoxColumn(attrs={"name": "col"}, checked='is_selected')
col = tables.CheckBoxColumn(attrs={'name': 'col'}, checked='is_selected')

table = TestTable([
{'col': '1', 'is_selected': True},
{'col': '2', 'is_selected': False}
])
assert attrs(table.rows[0]["col"]) == {"type": "checkbox", "value": "1", "name": "col", "checked": "checked"}
assert attrs(table.rows[1]["col"]) == {"type": "checkbox", "value": "2", "name": "col"}
assert attrs(table.rows[0].get_cell('col')) == {
'type': 'checkbox',
'value': '1',
'name': 'col',
'checked': 'checked'
}
assert attrs(table.rows[1].get_cell('col')) == {'type': 'checkbox', 'value': '2', 'name': 'col'}


def test_column_is_not_checked_for_non_existing_column():
Expand All @@ -40,8 +55,8 @@ class TestTable(tables.Table):
{'col': '1', 'is_selected': True},
{'col': '2', 'is_selected': False}
])
assert attrs(table.rows[0]["col"]) == {"type": "checkbox", "value": "1", "name": "col"}
assert attrs(table.rows[1]["col"]) == {"type": "checkbox", "value": "2", "name": "col"}
assert attrs(table.rows[0].get_cell('col')) == {'type': 'checkbox', 'value': '1', 'name': 'col'}
assert attrs(table.rows[1].get_cell('col')) == {'type': 'checkbox', 'value': '2', 'name': 'col'}


def test_column_is_alway_checked():
Expand All @@ -52,17 +67,22 @@ class TestTable(tables.Table):
{'col': 1, 'foo': 'bar'},
{'col': 2, 'foo': 'baz'}
])
assert attrs(table.rows[0]['col'])['checked'] == 'checked'
assert attrs(table.rows[1]['col'])['checked'] == 'checked'
assert attrs(table.rows[0].get_cell('col'))['checked'] == 'checked'
assert attrs(table.rows[1].get_cell('col'))['checked'] == 'checked'


def test_column_is_checked_callback():
def is_selected(value, record):
return value == '1'

class TestTable(tables.Table):
col = tables.CheckBoxColumn(attrs={"name": "col"}, checked=is_selected)
col = tables.CheckBoxColumn(attrs={'name': 'col'}, checked=is_selected)

table = TestTable([{'col': '1'}, {'col': '2'}])
assert attrs(table.rows[0]["col"]) == {"type": "checkbox", "value": "1", "name": "col", "checked": "checked"}
assert attrs(table.rows[1]["col"]) == {"type": "checkbox", "value": "2", "name": "col"}
assert attrs(table.rows[0].get_cell('col')) == {
'type': 'checkbox',
'value': '1',
'name': 'col',
'checked': 'checked'
}
assert attrs(table.rows[1].get_cell('col')) == {'type': 'checkbox', 'value': '2', 'name': 'col'}
38 changes: 19 additions & 19 deletions tests/columns/test_datecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,45 @@

def test_should_handle_explicit_format():
class TestTable(tables.Table):
date = tables.DateColumn(format="D b Y")
date = tables.DateColumn(format='D b Y')

class Meta:
default = "—"
default = '—'

table = TestTable([{"date": date(2012, 9, 11)},
{"date": None}])
assert table.rows[0]["date"] == "Tue sep 2012"
assert table.rows[1]["date"] == "—"
table = TestTable([{'date': date(2012, 9, 11)},
{'date': None}])
assert table.rows[0].get_cell('date') == 'Tue sep 2012'
assert table.rows[1].get_cell('date') == '—'


def test_should_handle_long_format(settings):
settings.DATE_FORMAT = "D Y b"
settings.DATE_FORMAT = 'D Y b'

class TestTable(tables.Table):
date = tables.DateColumn(short=False)

class Meta:
default = "—"
default = '—'

table = TestTable([{"date": date(2012, 9, 11)},
{"date": None}])
assert table.rows[0]["date"] == "Tue 2012 sep"
assert table.rows[1]["date"] == "—"
table = TestTable([{'date': date(2012, 9, 11)},
{'date': None}])
assert table.rows[0].get_cell('date') == 'Tue 2012 sep'
assert table.rows[1].get_cell('date') == '—'


def test_should_handle_short_format(settings):
settings.SHORT_DATE_FORMAT = "b Y D"
settings.SHORT_DATE_FORMAT = 'b Y D'

class TestTable(tables.Table):
date = tables.DateColumn(short=True)

class Meta:
default = "—"
default = '—'

table = TestTable([{"date": date(2012, 9, 11)},
{"date": None}])
assert table.rows[0]["date"] == "sep 2012 Tue"
assert table.rows[1]["date"] == "—"
table = TestTable([{'date': date(2012, 9, 11)},
{'date': None}])
assert table.rows[0].get_cell('date') == 'sep 2012 Tue'
assert table.rows[1].get_cell('date') == '—'


def test_should_be_used_for_datefields():
Expand All @@ -68,4 +68,4 @@ class Table(tables.Table):
class Meta:
model = DateModel

assert type(Table.base_columns["field"]) == tables.DateColumn
assert type(Table.base_columns['field']) == tables.DateColumn
34 changes: 17 additions & 17 deletions tests/columns/test_datetimecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,46 @@ def dt():
# If the version of Django has timezone support, convert from naive to
# UTC, the test project uses Australia/Brisbane so regardless the
# output from the column should be the same.
dt = pytz.timezone("Australia/Brisbane").localize(dt)
dt = pytz.timezone('Australia/Brisbane').localize(dt)
yield dt


def test_should_handle_explicit_format(dt):
class TestTable(tables.Table):
date = tables.DateTimeColumn(format="D b Y")
date = tables.DateTimeColumn(format='D b Y')

class Meta:
default = "—"
default = '—'

table = TestTable([{"date": dt}, {"date": None}])
assert table.rows[0]["date"] == "Tue sep 2012"
assert table.rows[1]["date"] == "—"
table = TestTable([{'date': dt}, {'date': None}])
assert table.rows[0].get_cell('date') == 'Tue sep 2012'
assert table.rows[1].get_cell('date') == '—'


def test_should_handle_long_format(dt, settings):
class TestTable(tables.Table):
date = tables.DateTimeColumn(short=False)

class Meta:
default = "—"
default = '—'

settings.DATETIME_FORMAT = "D Y b A f"
table = TestTable([{"date": dt}, {"date": None}])
assert table.rows[0]["date"] == "Tue 2012 sep PM 12:30"
assert table.rows[1]["date"] == "—"
settings.DATETIME_FORMAT = 'D Y b A f'
table = TestTable([{'date': dt}, {'date': None}])
assert table.rows[0].get_cell('date') == 'Tue 2012 sep PM 12:30'
assert table.rows[1].get_cell('date') == '—'


def test_should_handle_short_format(dt, settings):
class TestTable(tables.Table):
date = tables.DateTimeColumn(short=True)

class Meta:
default = "—"
default = '—'

settings.SHORT_DATETIME_FORMAT = "b Y D A f"
table = TestTable([{"date": dt}, {"date": None}])
assert table.rows[0]["date"] == "sep 2012 Tue PM 12:30"
assert table.rows[1]["date"] == "—"
settings.SHORT_DATETIME_FORMAT = 'b Y D A f'
table = TestTable([{'date': dt}, {'date': None}])
assert table.rows[0].get_cell('date') == 'sep 2012 Tue PM 12:30'
assert table.rows[1].get_cell('date') == '—'


def test_should_be_used_for_datetimefields():
Expand All @@ -83,4 +83,4 @@ class Table(tables.Table):
class Meta:
model = DateTimeModel

assert type(Table.base_columns["field"]) == tables.DateTimeColumn
assert type(Table.base_columns['field']) == tables.DateTimeColumn
12 changes: 6 additions & 6 deletions tests/columns/test_emailcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ def test_should_turn_email_address_into_hyperlink():
class Table(tables.Table):
email = tables.EmailColumn()

table = Table([{"email": "[email protected]"}])
assert table.rows[0]["email"] == '<a href="mailto:[email protected]">[email protected]</a>'
table = Table([{'email': '[email protected]'}])
assert table.rows[0].get_cell('email') == '<a href="mailto:[email protected]">[email protected]</a>'


def test_should_render_default_for_blank():
class Table(tables.Table):
email = tables.EmailColumn(default="---")
email = tables.EmailColumn(default='---')

table = Table([{"email": ""}])
assert table.rows[0]["email"] == '---'
table = Table([{'email': ''}])
assert table.rows[0].get_cell('email') == '---'


def test_should_be_used_for_datetimefields():
Expand All @@ -39,4 +39,4 @@ class Table(tables.Table):
class Meta:
model = EmailModel

assert type(Table.base_columns["field"]) == tables.EmailColumn
assert type(Table.base_columns['field']) == tables.EmailColumn
Loading

0 comments on commit c692885

Please sign in to comment.