1 """Test the core table functionality."""
3 from attest import Tests, Assert
4 from django.http import Http404
5 from django.core.paginator import Paginator
6 import django_tables2 as tables
7 from django_tables2 import utils
13 class UnsortedTable(tables.Table):
15 alpha = tables.Column()
16 beta = tables.Column()
19 class SortedTable(UnsortedTable):
25 {'i': 2, 'alpha': 'b', 'beta': 'b'},
26 {'i': 1, 'alpha': 'a', 'beta': 'c'},
27 {'i': 3, 'alpha': 'c', 'beta': 'a'},
33 """Test defining tables by declaration."""
34 class GeoAreaTable(tables.Table):
35 name = tables.Column()
36 population = tables.Column()
38 assert len(GeoAreaTable.base_columns) == 2
39 assert 'name' in GeoAreaTable.base_columns
40 assert not hasattr(GeoAreaTable, 'name')
42 class CountryTable(GeoAreaTable):
43 capital = tables.Column()
45 assert len(CountryTable.base_columns) == 3
46 assert 'capital' in CountryTable.base_columns
48 # multiple inheritance
49 class AddedMixin(tables.Table):
50 added = tables.Column()
52 class CityTable(GeoAreaTable, AddedMixin):
53 mayor = tables.Column()
55 assert len(CityTable.base_columns) == 4
56 assert 'added' in CityTable.base_columns
61 class TestTable(tables.Table):
64 Assert({}) == TestTable([]).attrs
66 class TestTable2(tables.Table):
69 Assert({"a": "b"}) == TestTable2([]).attrs
71 class TestTable3(tables.Table):
73 Assert({}) == TestTable3([]).attrs
74 Assert({"a": "b"}) == TestTable3([], attrs={"a": "b"}).attrs
76 class TestTable4(tables.Table):
79 Assert({"c": "d"}) == TestTable4([], attrs={"c": "d"}).attrs
83 def datasource_untouched():
84 """Ensure that data that is provided to the table (the datasource) is not
85 modified by table operations.
87 original_data = copy.deepcopy(MEMORY_DATA)
89 table = UnsortedTable(MEMORY_DATA)
92 assert MEMORY_DATA == Assert(original_data)
94 table = UnsortedTable(MEMORY_DATA)
95 table.order_by = 'beta'
97 assert MEMORY_DATA == Assert(original_data)
102 # fallback to Table.Meta
103 Assert(('alpha', )) == SortedTable([], order_by=None).order_by == SortedTable([]).order_by
105 # values of order_by are wrapped in tuples before being returned
106 Assert(SortedTable([], order_by='alpha').order_by) == ('alpha', )
107 Assert(SortedTable([], order_by=('beta',)).order_by) == ('beta', )
110 table = SortedTable([])
112 Assert(()) == table.order_by == SortedTable([], order_by=[]).order_by
114 table = SortedTable([])
116 Assert(()) == table.order_by == SortedTable([], order_by=()).order_by
118 table = SortedTable([])
120 Assert(()) == table.order_by == SortedTable([], order_by='').order_by
123 table = UnsortedTable([])
124 table.order_by = 'alpha'
125 Assert(('alpha', )) == UnsortedTable([], order_by='alpha').order_by == table.order_by
127 table = SortedTable([])
128 table.order_by = 'alpha'
129 Assert(('alpha', )) == SortedTable([], order_by='alpha').order_by == table.order_by
131 # let's check the data
132 table = SortedTable(MEMORY_DATA, order_by='beta')
133 Assert(3) == table.rows[0]['i']
135 # allow fallback to Table.Meta.order_by
136 table = SortedTable(MEMORY_DATA)
137 Assert(1) == table.rows[0]['i']
139 # column's can't be sorted if they're not allowed to be
140 class TestTable(tables.Table):
141 a = tables.Column(sortable=False)
144 table = TestTable([], order_by='a')
145 Assert(table.order_by) == ()
147 table = TestTable([], order_by='b')
148 Assert(table.order_by) == ('b', )
150 # sorting disabled by default
151 class TestTable(tables.Table):
152 a = tables.Column(sortable=True)
158 table = TestTable([], order_by='a')
159 Assert(table.order_by) == ('a', )
161 table = TestTable([], order_by='b')
162 Assert(table.order_by) == ()
164 table = TestTable([], sortable=True, order_by='b')
165 Assert(table.order_by) == ('b', )
170 class SimpleTable(tables.Table):
171 visible = tables.Column(visible=True)
172 hidden = tables.Column(visible=False)
174 # The columns container supports the len() builtin
175 assert len(SimpleTable([]).columns) == 1
179 def column_accessor():
180 class SimpleTable(UnsortedTable):
181 col1 = tables.Column(accessor='alpha.upper.isupper')
182 col2 = tables.Column(accessor='alpha.upper')
183 table = SimpleTable(MEMORY_DATA)
185 Assert(row['col1']) is True
186 Assert(row['col2']) == 'B'
190 def exclude_columns():
192 Defining ``Table.Meta.exclude`` or providing an ``exclude`` argument when
193 instantiating a table should have the same effect -- exclude those columns
194 from the table. It should have the same effect as not defining the
197 # Table(..., exclude=...)
198 table = UnsortedTable([], exclude=("i"))
199 Assert([c.name for c in table.columns]) == ["alpha", "beta"]
201 # Table.Meta: exclude=...
202 class PartialTable(UnsortedTable):
204 exclude = ("alpha", )
205 table = PartialTable([])
206 Assert([c.name for c in table.columns]) == ["i", "beta"]
208 # Inheritence -- exclude in parent, add in child
209 class AddonTable(PartialTable):
210 added = tables.Column()
211 table = AddonTable([])
212 Assert([c.name for c in table.columns]) == ["i", "beta", "added"]
214 # Inheritence -- exclude in child
215 class ExcludeTable(UnsortedTable):
216 added = tables.Column()
218 exclude = ("alpha", )
219 table = ExcludeTable([])
220 Assert([c.name for c in table.columns]) == ["i", "beta", "added"]
225 class BookTable(tables.Table):
226 name = tables.Column()
228 # create some sample data
231 data.append({"name": "Book No. %d" % i})
232 books = BookTable(data)
235 paginator = Paginator(books.rows, 10)
236 assert paginator.num_pages == 10
237 page = paginator.page(1)
238 assert page.has_previous() is False
239 assert page.has_next() is True
241 # integrated paginator
242 books.paginate(page=1)
243 Assert(hasattr(books, "page")) is True
245 books.paginate(page=1, per_page=10)
246 Assert(len(list(books.page.object_list))) == 10
249 Assert(books.paginator.num_pages) == 10
250 Assert(books.page.has_previous()) is False
251 Assert(books.page.has_next()) is True
253 # exceptions are converted into 404s
254 with Assert.raises(Http404) as error:
255 books.paginate(Paginator, page=9999, per_page=10)
256 books.paginate(Paginator, page='abc', per_page=10)
261 class TestTable(tables.Table):
264 table = TestTable([])
265 Assert(table.empty_text) is None
267 class TestTable(tables.Table):
271 empty_text = 'nothing here'
273 table = TestTable([])
274 Assert(table.empty_text) == 'nothing here'
276 table = TestTable([], empty_text='still nothing')
277 Assert(table.empty_text) == 'still nothing'