tests/test_tablemodel.py
author Radek Brich <radek.brich@devl.cz>
Wed, 03 Sep 2014 08:57:24 +0200
changeset 108 11dac45bfba4
parent 100 3b2df86d8f94
permissions -rwxr-xr-x
Fix cursor.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
100
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
import sys
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
sys.path.insert(0, '..')
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
from tuikit.tableview import *
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
import unittest
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
class TestTableView(unittest.TestCase):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
    def setUp(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        self.abs_model = AbstractTableModel()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
        self.model = TableModel()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
    def test_initial(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
        self._test_empty_model(self.abs_model)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        self._test_empty_model(self.model)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
    def test_one(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
        self._insert_data()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        self._fill_data()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        self._insert_headers()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        self._test_empty_headers()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        self._fill_headers()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        self._remove_data()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
        self._clear_data()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
    def test_two(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        self._insert_headers()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
        self._insert_data()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
        self._test_empty_headers()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
        self._fill_headers()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        self._fill_data()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
    def _test_empty_model(self, model):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
        self.assertEqual(model.row_count(), 0)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
        self.assertEqual(model.column_count(), 0)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
        self.assertRaises(IndexError, model.get_row, 0)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
        self.assertRaises(IndexError, model.get_column, 0)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        self.assertRaises(IndexError, model.get_item, 0, 0)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
    def _insert_data(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        for _i in range(3):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
            self.model.insert_row(0)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
            self.model.insert_column(0)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
        self.assertEqual(self.model.row_count(), 3)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
        self.assertEqual(self.model.column_count(), 3)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
    def _fill_data(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        for row in range(3):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
            for col in range(3):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
                self.model.set_item(row, col, row*3 + col+1)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        self.assertEqual(self.model.get_row(0), [1,2,3])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
        self.assertEqual(self.model.get_row(1), [4,5,6])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
        self.assertEqual(self.model.get_row(2), [7,8,9])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
        self.assertEqual(self.model.get_column(0), [1,4,7])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
    def _insert_headers(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
        self.model.set_num_headers(2, 2)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
        self.assertEqual(self.model.row_header_count(), 2)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
        self.assertEqual(self.model.column_header_count(), 2)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
    def _test_empty_headers(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
        self.assertEqual(self.model.get_row_header(0, 0), None)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
        self.assertEqual(self.model.get_row_header(2, 1), None)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
        self.assertEqual(self.model.get_column_header(0, 0), None)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
        self.assertEqual(self.model.get_column_header(2, 1), None)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
    def _fill_headers(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
        for i in range(3):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
            for header in range(2):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
                self.model.set_column_header(i, header, header*3 + i + 1)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
                self.model.set_row_header(i, header, header*3 + i + 1)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
        self.assertEqual(self.model.get_column_header(1, 0), 2)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
        self.assertEqual(self.model.get_column_header(1, 1), 5)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
        self.assertEqual(self.model.get_row_header(1, 0), 2)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
        self.assertEqual(self.model.get_row_header(1, 1), 5)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
    def _remove_data(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
        self.model.remove_row(1)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
        self.assertEqual(self.model.get_row(0), [1,2,3])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
        self.assertEqual(self.model.get_row(1), [7,8,9])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
        self.assertEqual(self.model.get_column(0), [1,7])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
        self.model.remove_column(1)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
        self.assertEqual(self.model.get_row(0), [1,3])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
        self.assertEqual(self.model.get_row(1), [7,9])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
        self.assertEqual(self.model.get_column(1), [3,9])
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
        self.assertRaises(IndexError, self.model.get_row, 2)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
        self.assertRaises(IndexError, self.model.get_column, 2)
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
    def _clear_data(self):
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
        self.model.clear()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
if __name__ == '__main__':
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
    unittest.main()
3b2df86d8f94 Update TableView (old uncommitted work).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97