def test_insertColumns(self): """Test the insertColumns() method of MinimalTableModel.""" model = MinimalTableModel() model.insertRows(0, 1) self.assertEqual(model.columnCount(), 1) index = model.index(0, 0) model.setData(index, 'a') self.assertTrue(model.insertColumns(0, 1)) self.assertEqual(model.columnCount(), 2) def check_data(expecteds): for column, expected in enumerate(expecteds): index = model.index(0, column) if expected is not None: self.assertEqual(model.data(index), expected) else: self.assertIsNone(model.data(index)) check_data([None, 'a']) index = model.index(0, 0) model.setData(index, 'b') self.assertTrue(model.insertColumns(1, 1)) self.assertEqual(model.columnCount(), 3) check_data(['b', None, 'a']) index = model.index(0, 1) model.setData(index, 'c') self.assertTrue(model.insertColumns(3, 1)) self.assertEqual(model.columnCount(), 4) check_data(['b', 'c', 'a', None])
def test_batch_set_data(self): """Test the batch_set_data() method of MinimalTableModel.""" model = MinimalTableModel() n_rows = 3 model.insertRows(0, n_rows) n_columns = 3 model.insertColumns(0, n_columns) background = n_rows * n_columns * ['0xdeadbeef'] indices = list() for row in range(n_rows): for column in range(n_columns): indices.append(model.index(row, column)) def _handle_data_changed(top_left, bottom_right, roles): self.assertEqual(top_left, indices[0]) self.assertEqual(bottom_right, indices[-1]) self.assertTrue(Qt.EditRole in roles) self.assertTrue(Qt.DisplayRole in roles) model.dataChanged.connect(_handle_data_changed) self.assertTrue(model.batch_set_data(indices, background)) for row in range(n_rows): for column in range(n_columns): index = model.index(row, column) self.assertEqual(model.data(index), '0xdeadbeef')
def test_removeColumns(self): """Test the removeColumns() method of MinimalTableModel.""" model = MinimalTableModel() self.assertFalse(model.removeColumns(-1, 1)) self.assertFalse(model.removeColumns(0, 1)) model.insertRows(0, 1) model.insertColumns(0, 4) data = ['a', 'b', 'c', 'd', 'e'] for column, value in enumerate(data): index = model.index(0, column) model.setData(index, value) self.assertTrue(model.removeColumns(4, 1)) self.assertEqual(model.columnCount(), 4) def check_data(expecteds): for column, expected in enumerate(expecteds): index = model.index(0, column) self.assertEqual(model.data(index), expected) check_data(['a', 'b', 'c', 'd']) self.assertTrue(model.removeColumns(0, 1)) self.assertEqual(model.columnCount(), 3) check_data(['b', 'c', 'd']) self.assertTrue(model.removeColumns(1, 1)) self.assertEqual(model.columnCount(), 2) check_data(['b', 'd'])
def test_removeRows(self): """Test the removeRows() method of MinimalTableModel.""" model = MinimalTableModel() self.assertFalse(model.removeRows(-1, 1)) self.assertFalse(model.removeRows(0, 1)) data = ['a', 'b', 'c', 'd', 'e'] model.insertRows(0, len(data)) for row, value in enumerate(data): index = model.index(row, 0) model.setData(index, value) self.assertTrue(model.removeRows(1, 2)) self.assertEqual(model.rowCount(), 3) def check_data(expecteds): for row, expected in enumerate(expecteds): index = model.index(row, 0) self.assertEqual(model.data(index), expected) check_data(['a', 'd', 'e']) self.assertTrue(model.removeRows(2, 1)) self.assertEqual(model.rowCount(), 2) check_data(['a', 'd']) self.assertTrue(model.removeRows(0, 1)) self.assertEqual(model.rowCount(), 1) check_data(['d']) self.assertTrue(model.removeRows(0, 1)) self.assertEqual(model.rowCount(), 0)
def test_setData(self): """Test the setData() method of MinimalTableModel.""" model = MinimalTableModel() model.insertRows(0, 1) index = model.index(0, 0) self.assertTrue(model.setData(index, 'a')) self.assertEqual(model.data(index), 'a')
def test_flags(self): """Test the flags() method of MinimalTableModel.""" model = MinimalTableModel() model.insertColumns(0, 1) model.insertRows(0, 1) index = model.index(0, 0) flags = model.flags(index) self.assertEqual( flags, Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
def test_reset_model(self): """Test the reset_model() method of MinimalTableModel.""" model = MinimalTableModel() data = [['a', 'b', 'c'], ['d', 'e', 'f']] model.reset_model(data) for row, row_data in enumerate(data): for column, value in enumerate(row_data): index = model.index(row, column) self.assertEqual(model.data(index), value)
def test_row_data(self): """Test the row_data() method of MinimalTableModel.""" model = MinimalTableModel() model.insertRows(0, 1) n_columns = 3 model.insertColumns(0, n_columns - 1) data = ['a', 'b', 'c'] for column in range(n_columns): index = model.index(0, column) model.setData(index, data[column]) self.assertEqual(model.row_data(0), data)
def test_clear(self): """Test the clear() method of MinimalTableModel.""" model = MinimalTableModel() model.insertColumns(0, 1) model.insertRows(0, 1) index = model.index(0, 0) model.setData(index, 23) self.assertEqual(model.rowCount(), 1) self.assertEqual(model.columnCount(), 1) model.clear() self.assertEqual(model.rowCount(), 0) self.assertEqual(model.columnCount(), 0)