1 # -*- coding: utf-8 -*-
2 """Test the core table functionality."""
3 from attest import Tests, Assert
4 from django_attest import TransactionTestContext
5 from django.test.client import RequestFactory
6 from django.template import Context, Template
7 from django.core.exceptions import ImproperlyConfigured
8 import django_tables2 as tables
9 from django_tables2 import utils, A
10 from .testapp.models import Person
11 from django.utils.translation import ugettext_lazy
12 from django.utils.translation import ugettext
20 class SimpleTable(tables.Table):
21 name = tables.Column()
22 Assert(SimpleTable([]).columns['name'].sortable) is True
24 class SimpleTable(tables.Table):
25 name = tables.Column()
29 Assert(SimpleTable([]).columns['name'].sortable) is False
31 class SimpleTable(tables.Table):
32 name = tables.Column()
36 Assert(SimpleTable([]).columns['name'].sortable) is True
42 Tests different types of values for the ``verbose_name`` property of a
45 class TranslationTable(tables.Table):
46 normal = tables.Column(verbose_name=ugettext("Normal"))
47 lazy = tables.Column(verbose_name=ugettext("Lazy"))
49 table = TranslationTable([])
50 Assert("Normal") == table.columns["normal"].header
51 Assert("Lazy") == table.columns["lazy"].header
57 Ensures that the sequence of columns is configurable.
59 class TestTable(tables.Table):
63 Assert(["a", "b", "c"]) == TestTable([]).columns.names()
64 Assert(["b", "a", "c"]) == TestTable([], sequence=("b", "a", "c")).columns.names()
66 class TestTable2(TestTable):
68 sequence = ("b", "a", "c")
69 Assert(["b", "a", "c"]) == TestTable2([]).columns.names()
70 Assert(["a", "b", "c"]) == TestTable2([], sequence=("a", "b", "c")).columns.names()
72 # BAD, all columns must be specified, or must use "..."
73 with Assert.raises(ValueError):
74 class TestTable3(TestTable):
77 with Assert.raises(ValueError):
78 TestTable([], sequence=("a", ))
80 # GOOD, using a single "..." allows you to only specify some columns. The
81 # remaining columns are ordered based on their definition order
82 class TestTable4(TestTable):
85 Assert(["a", "b", "c"]) == TestTable4([]).columns.names()
86 Assert(["a", "b", "c"]) == TestTable([], sequence=("...", )).columns.names()
88 class TestTable5(TestTable):
90 sequence = ("b", "...")
91 Assert(["b", "a", "c"]) == TestTable5([]).columns.names()
92 Assert(["b", "a", "c"]) == TestTable([], sequence=("b", "...")).columns.names()
94 class TestTable6(TestTable):
96 sequence = ("...", "b")
97 Assert(["a", "c", "b"]) == TestTable6([]).columns.names()
98 Assert(["a", "c", "b"]) == TestTable([], sequence=("...", "b")).columns.names()
100 class TestTable7(TestTable):
102 sequence = ("b", "...", "a")
103 Assert(["b", "c", "a"]) == TestTable7([]).columns.names()
104 Assert(["b", "c", "a"]) == TestTable([], sequence=("b", "...", "a")).columns.names()
106 # Let's test inheritence
107 class TestTable8(TestTable):
113 sequence = ("d", "...")
115 class TestTable9(TestTable):
120 Assert(["d", "a", "b", "c", "e", "f"]) == TestTable8([]).columns.names()
121 Assert(["d", "a", "b", "c", "e", "f"]) == TestTable9([], sequence=("d", "...")).columns.names()
125 linkcolumn.context(TransactionTestContext())
129 """Test LinkColumn"""
130 # test unicode values + headings
131 class UnicodeTable(tables.Table):
132 first_name = tables.LinkColumn('person', args=[A('pk')])
133 last_name = tables.LinkColumn('person', args=[A('pk')], verbose_name=u'äÚ¨´ˆÁ˜¨ˆ˜˘Ú…Ò˚ˆπ∆ˆ´')
136 {'pk': 1, 'first_name': u'Brädley', 'last_name': u'∆yers'},
137 {'pk': 2, 'first_name': u'Chr…s', 'last_name': u'DÒble'},
140 table = UnicodeTable(dataset)
141 request = RequestFactory().get('/some-url/')
142 template = Template('{% load django_tables2 %}{% render_table table %}')
143 html = template.render(Context({'request': request, 'table': table}))
145 Assert(u'Brädley' in html)
146 Assert(u'∆yers' in html)
147 Assert(u'Chr…s' in html)
148 Assert(u'DÒble' in html)
150 # Test handling queryset data with a null foreign key
154 def null_foreign_key():
158 class PersonTable(tables.Table):
159 first_name = tables.Column()
160 last_name = tables.Column()
161 occupation = tables.LinkColumn('occupation', args=[A('occupation.pk')])
163 Person.objects.create(first_name='bradley', last_name='ayers')
165 table = PersonTable(Person.objects.all())
169 columns = Tests([general, linkcolumn])