a8ae3d058cbf91613b12996588bd4652bdcb1114
[django-tables2.git] / tests / rows.py
1 """Test the core table functionality."""
2 from attest import Tests, Assert
3 import django_tables2 as tables
4 from django_tables2 import utils
5
6
7 rows = Tests()
8
9
10 @rows.test
11 def bound_rows():
12     class SimpleTable(tables.Table):
13         name = tables.Column()
14
15     data = [
16         {'name': 'Bradley'},
17         {'name': 'Chris'},
18         {'name': 'Peter'},
19     ]
20
21     table = SimpleTable(data)
22
23     # iteration
24     records = []
25     for row in table.rows:
26         records.append(row.record)
27     Assert(records) == data
28
29
30 @rows.test
31 def bound_row():
32     class SimpleTable(tables.Table):
33         name = tables.Column()
34         occupation = tables.Column()
35         age = tables.Column()
36
37     record = {'name': 'Bradley', 'age': 20, 'occupation': 'programmer'}
38
39     table = SimpleTable([record])
40     row = table.rows[0]
41
42     # integer indexing into a row
43     Assert(row[0]) == record['name']
44     Assert(row[1]) == record['occupation']
45     Assert(row[2]) == record['age']
46
47     with Assert.raises(IndexError) as error:
48         row[3]
49
50     # column name indexing into a row
51     Assert(row['name'])       == record['name']
52     Assert(row['occupation']) == record['occupation']
53     Assert(row['age'])        == record['age']
54
55     with Assert.raises(KeyError) as error:
56         row['gamma']