diff options
Diffstat (limited to 'silx/gui/hdf5/test/test_hdf5.py')
-rw-r--r-- | silx/gui/hdf5/test/test_hdf5.py | 99 |
1 files changed, 69 insertions, 30 deletions
diff --git a/silx/gui/hdf5/test/test_hdf5.py b/silx/gui/hdf5/test/test_hdf5.py index 8e375f2..44c4456 100644 --- a/silx/gui/hdf5/test/test_hdf5.py +++ b/silx/gui/hdf5/test/test_hdf5.py @@ -26,7 +26,7 @@ __authors__ = ["V. Valls"] __license__ = "MIT" -__date__ = "22/09/2017" +__date__ = "20/02/2018" import time @@ -40,6 +40,7 @@ from silx.gui import qt from silx.gui.test.utils import TestCaseQt from silx.gui import hdf5 from silx.io import commonh5 +import weakref try: import h5py @@ -69,6 +70,14 @@ class TestHdf5TreeModel(TestCaseQt): if h5py is None: self.skipTest("h5py is not available") + def waitForPendingOperations(self, model): + for i in range(10): + if not model.hasPendingOperations(): + break + self.qWait(10) + else: + raise RuntimeError("Still waiting for a pending operation") + @contextmanager def h5TempFile(self): # create tmp file @@ -96,7 +105,9 @@ class TestHdf5TreeModel(TestCaseQt): # clean up index = model.index(0, 0, qt.QModelIndex()) h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) - h5File.close() + ref = weakref.ref(model) + model = None + self.qWaitForDestroy(ref) def testAppendBadFilename(self): model = hdf5.Hdf5TreeModel() @@ -104,32 +115,35 @@ class TestHdf5TreeModel(TestCaseQt): def testInsertFilename(self): with self.h5TempFile() as filename: - model = hdf5.Hdf5TreeModel() - self.assertEquals(model.rowCount(qt.QModelIndex()), 0) - model.insertFile(filename) - self.assertEquals(model.rowCount(qt.QModelIndex()), 1) - # clean up - index = model.index(0, 0, qt.QModelIndex()) - h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) - h5File.close() + try: + model = hdf5.Hdf5TreeModel() + self.assertEquals(model.rowCount(qt.QModelIndex()), 0) + model.insertFile(filename) + self.assertEquals(model.rowCount(qt.QModelIndex()), 1) + # clean up + index = model.index(0, 0, qt.QModelIndex()) + h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) + self.assertIsNotNone(h5File) + finally: + ref = weakref.ref(model) + model = None + self.qWaitForDestroy(ref) def testInsertFilenameAsync(self): with self.h5TempFile() as filename: - model = hdf5.Hdf5TreeModel() - self.assertEquals(model.rowCount(qt.QModelIndex()), 0) - model.insertFileAsync(filename) - index = model.index(0, 0, qt.QModelIndex()) - self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5LoadingItem.Hdf5LoadingItem) - time.sleep(0.1) - self.qapp.processEvents() - time.sleep(0.1) - index = model.index(0, 0, qt.QModelIndex()) - self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5Item.Hdf5Item) - self.assertEquals(model.rowCount(qt.QModelIndex()), 1) - # clean up - index = model.index(0, 0, qt.QModelIndex()) - h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) - h5File.close() + try: + model = hdf5.Hdf5TreeModel() + self.assertEquals(model.rowCount(qt.QModelIndex()), 0) + model.insertFileAsync(filename) + index = model.index(0, 0, qt.QModelIndex()) + self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5LoadingItem.Hdf5LoadingItem) + self.waitForPendingOperations(model) + index = model.index(0, 0, qt.QModelIndex()) + self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5Item.Hdf5Item) + finally: + ref = weakref.ref(model) + model = None + self.qWaitForDestroy(ref) def testInsertObject(self): h5 = commonh5.File("/foo/bar/1.mock", "w") @@ -156,6 +170,10 @@ class TestHdf5TreeModel(TestCaseQt): index = model.index(0, 0, qt.QModelIndex()) node1 = model.nodeFromIndex(index) model.synchronizeH5pyObject(h5) + # Now h5 was loaded from it's filename + # Another ref is owned by the model + h5.close() + index = model.index(0, 0, qt.QModelIndex()) node2 = model.nodeFromIndex(index) self.assertIsNot(node1, node2) @@ -168,7 +186,12 @@ class TestHdf5TreeModel(TestCaseQt): # clean up index = model.index(0, 0, qt.QModelIndex()) h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) - h5File.close() + self.assertIsNotNone(h5File) + h5File = None + # delete the model + ref = weakref.ref(model) + model = None + self.qWaitForDestroy(ref) def testFileMoveState(self): model = hdf5.Hdf5TreeModel() @@ -206,15 +229,17 @@ class TestHdf5TreeModel(TestCaseQt): model.dropMimeData(mimeData, qt.Qt.CopyAction, 0, 0, qt.QModelIndex()) self.assertEquals(model.rowCount(qt.QModelIndex()), 1) # after sync - time.sleep(0.1) - self.qapp.processEvents() - time.sleep(0.1) + self.waitForPendingOperations(model) index = model.index(0, 0, qt.QModelIndex()) self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5Item.Hdf5Item) # clean up index = model.index(0, 0, qt.QModelIndex()) h5File = model.data(index, role=hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) - h5File.close() + self.assertIsNotNone(h5File) + h5File = None + ref = weakref.ref(model) + model = None + self.qWaitForDestroy(ref) def getRowDataAsDict(self, model, row): displayed = {} @@ -503,7 +528,9 @@ class TestH5Node(TestCaseQt): @classmethod def tearDownClass(cls): + ref = weakref.ref(cls.model) cls.model = None + cls.qWaitForDestroy(ref) cls.h5File.close() shutil.rmtree(cls.tmpDirectory) super(TestH5Node, cls).tearDownClass() @@ -696,6 +723,18 @@ class TestHdf5TreeView(TestCaseQt): view = hdf5.Hdf5TreeView() view._createContextMenu(qt.QPoint(0, 0)) + def testSelection_OriginalModel(self): + tree = commonh5.File("/foo/bar/1.mock", "w") + item = tree.create_group("a/b/c/d") + item.create_group("e").create_group("f") + + view = hdf5.Hdf5TreeView() + view.findHdf5TreeModel().insertH5pyObject(tree) + view.setSelectedH5Node(item) + + selected = list(view.selectedH5Nodes())[0] + self.assertIs(item, selected.h5py_object) + def testSelection_Simple(self): tree = commonh5.File("/foo/bar/1.mock", "w") item = tree.create_group("a/b/c/d") |