def test_remove_invalid_rows(self):
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        self.assertEqual(5, m.rowCount())
        self.assertRaises(ValueError, m.removeRows, -1, 1)   # -ve row
        self.assertRaises(ValueError, m.removeRows,  6, 1)   # row > n existing rows
        self.assertRaises(ValueError, m.removeRows,  0, -1)  # -ve count
    def test_set_invalid_rotation(self):
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        i = m.index(0, 0)
        self.assertRaises(ValueError, m.setData, i, 'not an integer', RotationRole)
        self.assertRaises(ValueError, m.setData, i, -1, RotationRole)
        self.assertRaises(ValueError, m.setData, i, 2, RotationRole)
    def test_set_invalid_rotation(self):
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        i = m.index(0, 0)
        self.assertRaises(ValueError, m.setData, i, 'not an integer',
                          RotationRole)
        self.assertRaises(ValueError, m.setData, i, -1, RotationRole)
        self.assertRaises(ValueError, m.setData, i, 2, RotationRole)
    def test_set_same_rect_does_not_modify(self):
        "Set box's rect with the same value as it currently has"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        mock_modified_changed = self._new_mock_modified_changed(m)
        m.setData(m.index(0, 0), m.data(m.index(0, 0), RectRole), RectRole)
        self.assertFalse(mock_modified_changed.called)
        self.assertFalse(m.is_modified)
    def test_set_rect(self):
        "Alter box's rect"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        i = m.index(0, 0)
        r = QRect(0, 0, 1, 1)
        m.setData(i, r, RectRole)
        self.assertEqual(r, m.data(i, RectRole))

        r = QRect(5, 5, 5, 5)
        m.setData(i, r, RectRole)
        self.assertEqual(r, m.data(i, RectRole))
    def test_remove_rows(self):
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        m.removeRows(0, 1)
        self.assertEqual(4, m.rowCount())
        expected = {"catalogNumber": "2", "scientificName": "B"}
        self.assertEqual(expected, m.data(m.index(0, 0), MetadataRole))
    def test_set_same_rect_does_not_modify(self):
        "Set box's rect with the same value as it currently has"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        mock_modified_changed = self._new_mock_modified_changed(m)
        m.setData(m.index(0, 0), m.data(m.index(0, 0), RectRole), RectRole)
        self.assertFalse(mock_modified_changed.called)
        self.assertFalse(m.is_modified)
    def test_display_role(self):
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        # First four characters only - remainder depend upon current template
        self.assertEqual('0001', m.data(m.index(0, 0), Qt.DisplayRole)[:4])
        self.assertEqual('0003', m.data(m.index(2, 0), Qt.DisplayRole)[:4])
        self.assertEqual('0004', m.data(m.index(3, 0), Qt.DisplayRole)[:4])
    def test_set_metadata_modifies(self):
        "Alter box's metadata"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        mock_modified_changed = self._new_mock_modified_changed(m)
        m.setData(m.index(0, 0), {'catalogNumber': None}, MetadataRole)
        mock_modified_changed.assert_called_once_with()
        self.assertTrue(m.is_modified)
    def test_set_rect_modifies(self):
        "Alter box's rect"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        mock_modified_changed = self._new_mock_modified_changed(m)
        m.setData(m.index(0, 0), QRect(0, 0, 1, 1), RectRole)
        mock_modified_changed.assert_called_once_with()
        self.assertTrue(m.is_modified)
    def test_modified_cleared(self):
        "Alter document and clear"
        with temp_directory_with_files(TESTDATA / 'shapes.inselect',
                                       TESTDATA / 'shapes.png') as tempdir:
            m = Model()
            m.from_document(InselectDocument.load(tempdir / 'shapes.inselect'))

            # Alter data
            mock_modified_changed = self._new_mock_modified_changed(m)
            m.setData(m.index(0, 0), -90, RotationRole)
            mock_modified_changed.assert_called_once_with()
            self.assertTrue(m.is_modified)

            # Clear
            m.clear()

            self.assertEqual(2, mock_modified_changed.call_count)
            self.assertFalse(m.is_modified)
    def test_set_metadata(self):
        "Alter box's metadata"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        i = m.index(0, 0)
        expected = {
            "catalogNumber": "1",
            "scientificName": "A",
        }
        self.assertEqual(expected, m.data(i, MetadataRole))

        m.setData(i, {'catalogNumber': '1234'}, MetadataRole)
        expected = {
            "catalogNumber": "1234",
            "scientificName": "A",
        }
        self.assertEqual(expected, m.data(i, MetadataRole))
    def test_set_rect_modifies(self):
        "Alter box's rect"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        mock_modified_changed = self._new_mock_modified_changed(m)
        m.setData(m.index(0, 0), QRect(0, 0, 1, 1), RectRole)
        mock_modified_changed.assert_called_once_with()
        self.assertTrue(m.is_modified)
    def test_set_rotation_modifies(self):
        "Alter box's rotation"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        mock_modified_changed = self._new_mock_modified_changed(m)
        m.setData(m.index(0, 0), 90, RotationRole)
        mock_modified_changed.assert_called_once_with()
        self.assertTrue(m.is_modified)
    def test_set_metadata_modifies(self):
        "Alter box's metadata"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        mock_modified_changed = self._new_mock_modified_changed(m)
        m.setData(m.index(0, 0), {'catalogNumber': None}, MetadataRole)
        mock_modified_changed.assert_called_once_with()
        self.assertTrue(m.is_modified)
    def test_set_same_rotation_does_not_modify(self):
        "Set box's rotation to the same value as it currently has"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        mock_modified_changed = self._new_mock_modified_changed(m)
        m.setData(m.index(0, 0), 0, RotationRole)
        self.assertFalse(mock_modified_changed.called)
        self.assertFalse(m.is_modified)
    def test_set_metadata(self):
        "Alter box's metadata"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        i = m.index(0, 0)
        expected = {
            "catalogNumber": "1",
            "scientificName": "A",
        }
        self.assertEqual(expected, m.data(i, MetadataRole))

        m.setData(i, {'catalogNumber': '1234'}, MetadataRole)
        expected = {
            "catalogNumber": "1234",
            "scientificName": "A",
        }
        self.assertEqual(expected, m.data(i, MetadataRole))
    def test_modified_cleared(self):
        "Alter document and clear"
        with temp_directory_with_files(TESTDATA / 'test_segment.inselect',
                                       TESTDATA / 'test_segment.png') as tempdir:
            m = Model()
            m.from_document(InselectDocument.load(tempdir / 'test_segment.inselect'))

            # Alter data
            mock_modified_changed = self._new_mock_modified_changed(m)
            m.setData(m.index(0, 0), -90, RotationRole)
            mock_modified_changed.assert_called_once_with()
            self.assertTrue(m.is_modified)

            # Clear
            m.clear()

            self.assertEqual(2, mock_modified_changed.call_count)
            self.assertFalse(m.is_modified)
    def test_set_invalid_rect(self):
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        self.assertRaises(ValueError, m.setData, m.index(0, 0), 'not a rect',
                          RectRole)
    def test_set_invalid_metadata(self):
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        self.assertRaises(ValueError, m.setData, m.index(0, 0), 'not a dict',
                          MetadataRole)
 def test_set_invalid_index(self):
     m = Model()
     self.assertIsNone(m.data(m.index(-1, -1)))
    def test_set_rotation(self):
        "Alter box's rotation"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        i = m.index(0, 0)
        self.assertEqual(0, m.data(i, RotationRole))

        m.setData(i, 180, RotationRole)
        self.assertEqual(180, m.data(i, RotationRole))

        # Rotation stored 0 <= v < 360
        m.setData(i, -90, RotationRole)
        self.assertEqual(270, m.data(i, RotationRole))
    def test_insert_row(self):
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))

        self.assertEqual(5, m.rowCount())
        m.insertRow(5)

        self.assertEqual(6, m.rowCount())

        i = m.index(5, 0)
        self.assertEqual({}, m.data(i, MetadataRole))
        self.assertEqual(0, m.data(i, RotationRole))
        self.assertEqual(QRect(0, 0, 0, 0), m.data(i, RectRole))
    def test_set_invalid_metadata(self):
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        self.assertRaises(ValueError, m.setData, m.index(0, 0), 'not a dict',
                          MetadataRole)
 def test_set_invalid_index(self):
     m = Model()
     self.assertIsNone(m.data(m.index(-1, -1)))
    def test_set_rect(self):
        "Alter box's rect"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        i = m.index(0, 0)
        r = QRect(0, 0, 1, 1)
        m.setData(i, r, RectRole)
        self.assertEqual(r, m.data(i, RectRole))

        r = QRect(5, 5, 5, 5)
        m.setData(i, r, RectRole)
        self.assertEqual(r, m.data(i, RectRole))
    def test_set_rotation(self):
        "Alter box's rotation"
        m = Model()
        m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))

        i = m.index(0, 0)
        self.assertEqual(0, m.data(i, RotationRole))

        m.setData(i, 180, RotationRole)
        self.assertEqual(180, m.data(i, RotationRole))

        # Rotation stored 0 <= v < 360
        m.setData(i, -90, RotationRole)
        self.assertEqual(270, m.data(i, RotationRole))
 def test_new_document_not_modified(self):
     "Open a document - expect it not show as modified"
     m = Model()
     m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))
     self.assertFalse(m.is_modified)
 def test_new_document_not_modified(self):
     "Open a document - expect it not show as modified"
     m = Model()
     m.from_document(InselectDocument.load(TESTDATA / 'shapes.inselect'))
     self.assertFalse(m.is_modified)