|
1 #!/usr/bin/env python3 |
|
2 |
|
3 import sys |
|
4 sys.path.insert(0, '..') |
|
5 |
|
6 from tuikit.tableview import * |
|
7 import unittest |
|
8 |
|
9 |
|
10 class TestTableView(unittest.TestCase): |
|
11 def setUp(self): |
|
12 self.abs_model = AbstractTableModel() |
|
13 self.model = TableModel() |
|
14 |
|
15 def test_initial(self): |
|
16 self._test_empty_model(self.abs_model) |
|
17 self._test_empty_model(self.model) |
|
18 |
|
19 def test_one(self): |
|
20 self._insert_data() |
|
21 self._fill_data() |
|
22 self._insert_headers() |
|
23 self._test_empty_headers() |
|
24 self._fill_headers() |
|
25 self._remove_data() |
|
26 self._clear_data() |
|
27 |
|
28 def test_two(self): |
|
29 self._insert_headers() |
|
30 self._insert_data() |
|
31 self._test_empty_headers() |
|
32 self._fill_headers() |
|
33 self._fill_data() |
|
34 |
|
35 def _test_empty_model(self, model): |
|
36 self.assertEqual(model.row_count(), 0) |
|
37 self.assertEqual(model.column_count(), 0) |
|
38 self.assertRaises(IndexError, model.get_row, 0) |
|
39 self.assertRaises(IndexError, model.get_column, 0) |
|
40 self.assertRaises(IndexError, model.get_item, 0, 0) |
|
41 |
|
42 def _insert_data(self): |
|
43 for _i in range(3): |
|
44 self.model.insert_row(0) |
|
45 self.model.insert_column(0) |
|
46 self.assertEqual(self.model.row_count(), 3) |
|
47 self.assertEqual(self.model.column_count(), 3) |
|
48 |
|
49 def _fill_data(self): |
|
50 for row in range(3): |
|
51 for col in range(3): |
|
52 self.model.set_item(row, col, row*3 + col+1) |
|
53 self.assertEqual(self.model.get_row(0), [1,2,3]) |
|
54 self.assertEqual(self.model.get_row(1), [4,5,6]) |
|
55 self.assertEqual(self.model.get_row(2), [7,8,9]) |
|
56 self.assertEqual(self.model.get_column(0), [1,4,7]) |
|
57 |
|
58 def _insert_headers(self): |
|
59 self.model.set_num_headers(2, 2) |
|
60 self.assertEqual(self.model.row_header_count(), 2) |
|
61 self.assertEqual(self.model.column_header_count(), 2) |
|
62 |
|
63 def _test_empty_headers(self): |
|
64 self.assertEqual(self.model.get_row_header(0, 0), None) |
|
65 self.assertEqual(self.model.get_row_header(2, 1), None) |
|
66 self.assertEqual(self.model.get_column_header(0, 0), None) |
|
67 self.assertEqual(self.model.get_column_header(2, 1), None) |
|
68 |
|
69 def _fill_headers(self): |
|
70 for i in range(3): |
|
71 for header in range(2): |
|
72 self.model.set_column_header(i, header, header*3 + i + 1) |
|
73 self.model.set_row_header(i, header, header*3 + i + 1) |
|
74 self.assertEqual(self.model.get_column_header(1, 0), 2) |
|
75 self.assertEqual(self.model.get_column_header(1, 1), 5) |
|
76 self.assertEqual(self.model.get_row_header(1, 0), 2) |
|
77 self.assertEqual(self.model.get_row_header(1, 1), 5) |
|
78 |
|
79 def _remove_data(self): |
|
80 self.model.remove_row(1) |
|
81 self.assertEqual(self.model.get_row(0), [1,2,3]) |
|
82 self.assertEqual(self.model.get_row(1), [7,8,9]) |
|
83 self.assertEqual(self.model.get_column(0), [1,7]) |
|
84 self.model.remove_column(1) |
|
85 self.assertEqual(self.model.get_row(0), [1,3]) |
|
86 self.assertEqual(self.model.get_row(1), [7,9]) |
|
87 self.assertEqual(self.model.get_column(1), [3,9]) |
|
88 self.assertRaises(IndexError, self.model.get_row, 2) |
|
89 self.assertRaises(IndexError, self.model.get_column, 2) |
|
90 |
|
91 def _clear_data(self): |
|
92 self.model.clear() |
|
93 |
|
94 |
|
95 if __name__ == '__main__': |
|
96 unittest.main() |
|
97 |