diff --git a/beeref/fileio/sql.py b/beeref/fileio/sql.py index ea65abc..518b794 100644 --- a/beeref/fileio/sql.py +++ b/beeref/fileio/sql.py @@ -170,7 +170,13 @@ class SQLiteIO: item.filename)) item.save_id = self.cursor.lastrowid pixmap = item.pixmap_to_bytes() - name = '%04d.png' % item.save_id + + if item.filename: + basename = os.path.splitext(os.path.basename(item.filename))[0] + name = '%04d-%s.png' % (item.save_id, basename) + else: + name = '%04d.png' % item.save_id + self.ex( 'INSERT INTO sqlar (item_id, name, mode, sz, data) ' 'VALUES (?, ?, 644, ?, ?)', diff --git a/tests/base.py b/tests/base.py index b92a92f..25036e8 100644 --- a/tests/base.py +++ b/tests/base.py @@ -1,10 +1,20 @@ +import os.path from unittest import TestCase from PyQt6 import QtWidgets +root = os.path.dirname(__file__) +imgfilename3x3 = os.path.join(root, 'assets', 'test3x3.png') +with open(imgfilename3x3, 'rb') as f: + imgdata3x3 = f.read() + + class BeeTestCase(TestCase): + imgfilename3x3 = imgfilename3x3 + imgdata3x3 = imgdata3x3 + @classmethod def setUpClass(cls): cls.app = QtWidgets.QApplication([]) diff --git a/tests/fileio/test_sql.py b/tests/fileio/test_sql.py index f7a16ed..652a097 100644 --- a/tests/fileio/test_sql.py +++ b/tests/fileio/test_sql.py @@ -84,7 +84,7 @@ class SQLiteIOWriteTestCase(BeeTestCase): metamock.assert_called_once() def test_inserts_new_item(self): - item = BeePixmapItem(QtGui.QImage(), filename='bee.png') + item = BeePixmapItem(QtGui.QImage(), filename='bee.jpg') item.setScale(1.3) item.setPos(44, 55) item.pixmap_to_bytes = MagicMock(return_value=b'abc') @@ -93,15 +93,29 @@ class SQLiteIOWriteTestCase(BeeTestCase): assert item.save_id == 1 result = self.io.fetchone( - 'SELECT pos_x, pos_y, scale, filename, sqlar.data, type ' + 'SELECT pos_x, pos_y, scale, filename, type, ' + 'sqlar.data, sqlar.name ' 'FROM items ' 'INNER JOIN sqlar on sqlar.item_id = items.id') assert result[0] == 44.0 assert result[1] == 55.0 assert result[2] == 1.3 - assert result[3] == 'bee.png' - assert result[4] == b'abc' - assert result[5] == 'pixmap' + assert result[3] == 'bee.jpg' + assert result[4] == 'pixmap' + assert result[5] == b'abc' + assert result[6] == '0001-bee.png' + + def test_inserts_new_item_without_filename(self): + item = BeePixmapItem(QtGui.QImage()) + self.scene.addItem(item) + self.io.write() + + assert item.save_id == 1 + result = self.io.fetchone( + 'SELECT filename, sqlar.name FROM items ' + 'INNER JOIN sqlar on sqlar.item_id = items.id') + assert result[0] is None + assert result[1] == '0001.png' def test_updates_existing_item(self): item = BeePixmapItem(QtGui.QImage(), filename='bee.png') @@ -165,11 +179,6 @@ class SQLiteIOReadTestCase(BeeTestCase): self.scene = BeeGraphicsScene(None) def test_reads_readonly(self): - root = os.path.dirname(__file__) - imgfilename = os.path.join(root, '..', 'assets', 'test3x3.png') - with open(imgfilename, 'rb') as f: - imgdata = f.read() - with tempfile.TemporaryDirectory() as dirname: fname = os.path.join(dirname, 'test.bee') io = SQLiteIO(fname, self.scene, create_new=True) @@ -178,7 +187,7 @@ class SQLiteIOReadTestCase(BeeTestCase): 'VALUES (?, ?, ?, ?, ?) ', ('pixmap', 22.2, 33.3, 3.4, 'bee.png')) io.ex('INSERT INTO sqlar (item_id, data) VALUES (?, ?)', - (1, imgdata)) + (1, self.imgdata3x3)) io.connection.commit() del(io) diff --git a/tests/test_items.py b/tests/test_items.py index 6bc0067..c898c75 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -1,7 +1,5 @@ from unittest.mock import patch, PropertyMock -import os.path - from PyQt6 import QtGui, QtWidgets from beeref.items import BeePixmapItem @@ -11,16 +9,15 @@ from .base import BeeTestCase class BeePixmapItemTestCase(BeeTestCase): def test_init(self): - root = os.path.dirname(__file__) - filename = os.path.join(root, 'assets', 'test3x3.png') - item = BeePixmapItem(QtGui.QImage(filename), filename) + item = BeePixmapItem( + QtGui.QImage(self.imgfilename3x3), self.imgfilename3x3) assert item.width == 3 assert item.height == 3 assert item.scale_factor == 1 assert item.flags() == ( QtWidgets.QGraphicsItem.GraphicsItemFlags.ItemIsMovable | QtWidgets.QGraphicsItem.GraphicsItemFlags.ItemIsSelectable) - assert item.filename == filename + assert item.filename == self.imgfilename3x3 def test_set_scale(self): item = BeePixmapItem(QtGui.QImage()) diff --git a/tests/test_scene.py b/tests/test_scene.py index 009d8c3..e09ac2f 100644 --- a/tests/test_scene.py +++ b/tests/test_scene.py @@ -24,6 +24,9 @@ class BeeGraphicsSceneNormalizeTestCase(BeeTestCase): item1.setScale.assert_called_once_with(1.5) item2.setScale.assert_called_once_with(0.75) + def test_normalize_height_when_no_items(self): + self.scene.normalize_height() + def test_normalize_width(self): item1 = MagicMock(width=100, scale_factor=1) item2 = MagicMock(width=200, scale_factor=3) @@ -35,6 +38,9 @@ class BeeGraphicsSceneNormalizeTestCase(BeeTestCase): item1.setScale.assert_called_once_with(1.5) item2.setScale.assert_called_once_with(0.75) + def test_normalize_width_when_no_items(self): + self.scene.normalize_width() + def test_normalize_size(self): item1 = MagicMock(width=100, height=200, scale_factor=1) item2 = MagicMock(width=400, height=100, scale_factor=3) @@ -45,3 +51,6 @@ class BeeGraphicsSceneNormalizeTestCase(BeeTestCase): item1.setScale.assert_called_once_with(math.sqrt(1.5)) item2.setScale.assert_called_once_with(math.sqrt(0.75)) + + def test_normalize_size_when_no_items(self): + self.scene.normalize_size()