class TestPyTableModel(unittest.TestCase): def setUp(self): self.model = PyTableModel([[1, 4], [2, 3]]) def test_init(self): self.model = PyTableModel() self.assertEqual(self.model.rowCount(), 0) def test_rowCount(self): self.assertEqual(self.model.rowCount(), 2) self.assertEqual(len(self.model), 2) def test_columnCount(self): self.assertEqual(self.model.columnCount(), 2) def test_data(self): mi = self.model.index(0, 0) self.assertEqual(self.model.data(mi), '1') self.assertEqual(self.model.data(mi, Qt.EditRole), 1) def test_editable(self): editable_model = PyTableModel([[0]], editable=True) self.assertFalse(int(self.model.flags(self.model.index(0, 0)) & Qt.ItemIsEditable)) self.assertTrue(int(editable_model.flags(editable_model.index(0, 0)) & Qt.ItemIsEditable)) def test_sort(self): self.model.sort(1) self.assertEqual(self.model.index(0, 0).data(Qt.EditRole), 2) def test_setHeaderLabels(self): self.model.setHorizontalHeaderLabels(['Col 1', 'Col 2']) self.assertEqual(self.model.headerData(1, Qt.Horizontal), 'Col 2') self.assertEqual(self.model.headerData(1, Qt.Vertical), 2) def test_removeRows(self): self.model.removeRows(0, 1) self.assertEqual(len(self.model), 1) self.assertEqual(self.model[0][1], 3) def test_removeColumns(self): self.model.removeColumns(0, 1) self.assertEqual(self.model.columnCount(), 1) self.assertEqual(self.model[1][0], 3) def test_insertRows(self): self.model.insertRows(0, 1) self.assertEqual(self.model[1][0], 1) def test_insertColumns(self): self.model.insertColumns(0, 1) self.assertEqual(self.model[0], ['', 1, 4]) def test_wrap(self): self.model.wrap([[0]]) self.assertEqual(self.model.rowCount(), 1) self.assertEqual(self.model.columnCount(), 1) def test_init_wrap_empty(self): # pylint: disable=protected-access t = [] model = PyTableModel(t) self.assertIs(model._table, t) t.append([1, 2, 3]) self.assertEqual(list(model), [[1, 2, 3]]) def test_clear(self): self.model.clear() self.assertEqual(self.model.rowCount(), 0) def test_append(self): self.model.append([5, 6]) self.assertEqual(self.model[2][1], 6) self.assertEqual(self.model.rowCount(), 3) def test_extend(self): self.model.extend([[5, 6]]) self.assertEqual(self.model[2][1], 6) self.assertEqual(self.model.rowCount(), 3) def test_insert(self): self.model.insert(0, [5, 6]) self.assertEqual(self.model[0][1], 6) self.assertEqual(self.model.rowCount(), 3) def test_remove(self): self.model.remove([2, 3]) self.assertEqual(self.model.rowCount(), 1) def test_other_roles(self): self.model.append([2, 3]) self.model.setData(self.model.index(2, 0), Qt.AlignCenter, Qt.TextAlignmentRole) del self.model[1] self.assertTrue(Qt.AlignCenter & self.model.data(self.model.index(1, 0), Qt.TextAlignmentRole))
class TestPyTableModel(unittest.TestCase): def setUp(self): self.model = PyTableModel([[1, 4], [2, 3]]) def test_init(self): self.model = PyTableModel() self.assertEqual(self.model.rowCount(), 0) def test_rowCount(self): self.assertEqual(self.model.rowCount(), 2) self.assertEqual(len(self.model), 2) def test_columnCount(self): self.assertEqual(self.model.columnCount(), 2) def test_data(self): mi = self.model.index(0, 0) self.assertEqual(self.model.data(mi), '1') self.assertEqual(self.model.data(mi, Qt.EditRole), 1) def test_editable(self): editable_model = PyTableModel([[0]], editable=True) self.assertFalse(self.model.flags(self.model.index(0, 0)) & Qt.ItemIsEditable) self.assertTrue(editable_model.flags(editable_model.index(0, 0)) & Qt.ItemIsEditable) def test_sort(self): self.model.sort(1) self.assertEqual(self.model.index(0, 0).data(Qt.EditRole), 2) def test_setHeaderLabels(self): self.model.setHorizontalHeaderLabels(['Col 1', 'Col 2']) self.assertEqual(self.model.headerData(1, Qt.Horizontal), 'Col 2') self.assertEqual(self.model.headerData(1, Qt.Vertical), 2) def test_removeRows(self): self.model.removeRows(0, 1) self.assertEqual(len(self.model), 1) self.assertEqual(self.model[0][1], 3) def test_removeColumns(self): self.model.removeColumns(0, 1) self.assertEqual(self.model.columnCount(), 1) self.assertEqual(self.model[1][0], 3) def test_insertRows(self): self.model.insertRows(0, 1) self.assertEqual(self.model[1][0], 1) def test_insertColumns(self): self.model.insertColumns(0, 1) self.assertEqual(self.model[0], ['', 1, 4]) def test_wrap(self): self.model.wrap([[0]]) self.assertEqual(self.model.rowCount(), 1) self.assertEqual(self.model.columnCount(), 1) def test_init_wrap_empty(self): # pylint: disable=protected-access t = [] model = PyTableModel(t) self.assertIs(model._table, t) t.append([1, 2, 3]) self.assertEqual(list(model), [[1, 2, 3]]) def test_clear(self): self.model.clear() self.assertEqual(self.model.rowCount(), 0) def test_append(self): self.model.append([5, 6]) self.assertEqual(self.model[2][1], 6) self.assertEqual(self.model.rowCount(), 3) def test_extend(self): self.model.extend([[5, 6]]) self.assertEqual(self.model[2][1], 6) self.assertEqual(self.model.rowCount(), 3) def test_insert(self): self.model.insert(0, [5, 6]) self.assertEqual(self.model[0][1], 6) self.assertEqual(self.model.rowCount(), 3) def test_remove(self): self.model.remove([2, 3]) self.assertEqual(self.model.rowCount(), 1) def test_other_roles(self): self.model.append([2, 3]) self.model.setData(self.model.index(2, 0), Qt.AlignCenter, Qt.TextAlignmentRole) del self.model[1] self.assertTrue(Qt.AlignCenter & self.model.data(self.model.index(1, 0), Qt.TextAlignmentRole)) def test_set_item_signals(self): def p(*s): return [[x] for x in s] def assert_changed(startrow, stoprow, ncolumns): start, stop = changed[-1][:2] self.assertEqual(start.row(), startrow) self.assertEqual(stop.row(), stoprow) self.assertEqual(start.column(), 0) self.assertEqual(stop.column(), ncolumns) self.model.wrap(p(0, 1, 2, 3, 4, 5)) aboutinserted = QSignalSpy(self.model.rowsAboutToBeInserted) inserted = QSignalSpy(self.model.rowsInserted) aboutremoved = QSignalSpy(self.model.rowsAboutToBeRemoved) removed = QSignalSpy(self.model.rowsRemoved) changed = QSignalSpy(self.model.dataChanged) # Insert rows self.model[2:4] = p(6, 7, 8, 9, 10) + [[11, 2]] self.assertEqual(list(self.model), p(0, 1, 6, 7, 8, 9, 10) + [[11, 2]] + p(4, 5)) self.assertEqual(len(changed), 1) assert_changed(2, 3, 1) self.assertEqual(aboutinserted[-1][1:], [4, 7]) self.assertEqual(inserted[-1][1:], [4, 7]) self.assertEqual(len(aboutremoved), 0) self.assertEqual(len(removed), 0) # Remove rows self.model[2:8] = p(2, 3) self.assertEqual(list(self.model), p(0, 1, 2, 3, 4, 5)) self.assertEqual(len(changed), 2) # one is from before assert_changed(2, 3, 0) self.assertEqual(aboutremoved[-1][1:], [4, 7]) self.assertEqual(removed[-1][1:], [4, 7]) self.assertEqual(len(inserted), 1) # from before self.assertEqual(len(aboutinserted), 1) # from before # Change rows self.model[-5:-3] = p(19, 20) self.assertEqual(list(self.model), p(0, 19, 20, 3, 4, 5)) self.assertEqual(len(changed), 3) # two are from before assert_changed(1, 2, 0) self.assertEqual(len(inserted), 1) # from before self.assertEqual(len(aboutinserted), 1) # from before self.assertEqual(len(removed), 1) # from before self.assertEqual(len(aboutremoved), 1) # from before # Insert without change self.model[3:3] = p(21, 22) self.assertEqual(list(self.model), p(0, 19, 20, 21, 22, 3, 4, 5)) self.assertEqual(len(changed), 3) #from before self.assertEqual(inserted[-1][1:], [3, 4]) self.assertEqual(aboutinserted[-1][1:], [3, 4]) self.assertEqual(len(removed), 1) # from before self.assertEqual(len(aboutremoved), 1) # from before # Remove without change self.model[3:5] = [] self.assertEqual(list(self.model), p(0, 19, 20, 3, 4, 5)) self.assertEqual(len(changed), 3) #from before self.assertEqual(removed[-1][1:], [3, 4]) self.assertEqual(aboutremoved[-1][1:], [3, 4]) self.assertEqual(len(inserted), 2) # from before self.assertEqual(len(aboutinserted), 2) # from before # Remove all self.model[:] = [] self.assertEqual(list(self.model), []) self.assertEqual(len(changed), 3) #from before self.assertEqual(removed[-1][1:], [0, 5]) self.assertEqual(aboutremoved[-1][1:], [0, 5]) self.assertEqual(len(inserted), 2) # from before self.assertEqual(len(aboutinserted), 2) # from before # Add to empty self.model[:] = p(0, 1, 2, 3) self.assertEqual(list(self.model), p(0, 1, 2, 3)) self.assertEqual(len(changed), 3) #from before self.assertEqual(inserted[-1][1:], [0, 3]) self.assertEqual(inserted[-1][1:], [0, 3]) self.assertEqual(len(removed), 3) # from before self.assertEqual(len(aboutremoved), 3) # from before