from chrysacase import ChrysalideTestCase from pychrysalide.glibext import ObjectStorage, SerializableObject import gi import os import tempfile class TestObjectStorage(ChrysalideTestCase): """TestCase for analysis.storage.""" @classmethod def setUpClass(cls): super(TestObjectStorage, cls).setUpClass() _, cls._tmp_filename = tempfile.mkstemp() cls.log('Using temporary filename "%s"' % cls._tmp_filename) @classmethod def tearDownClass(cls): super(TestObjectStorage, cls).tearDownClass() cls.log('Delete filename "%s"' % cls._tmp_filename) os.unlink(cls._tmp_filename) def testGenericStorage(self): """Store and load basic objects.""" class SimpleObject(gi._gi.GObject, SerializableObject): def __init__(self, b=None): super().__init__() self._b = b def _load(self, storage, fd): assert(self._b is None) self._b = os.read(fd, 1)[0] return True def _store(self, storage, fd): os.write(fd, bytes([ self._b ])) return True def __eq__(self, other): return self._b == other._b # Store storage = ObjectStorage('TestStorage', 0, 'my-storage-hash') self.assertIsNotNone(storage) so = SimpleObject(0x23) pos = storage.store_object('simple', so) self.assertIsNotNone(pos) status = storage.store(self._tmp_filename) self.assertTrue(status) # Reload storage2 = ObjectStorage.load(self._tmp_filename) so2 = storage2.load_object('simple', pos) self.assertEqual(so, so2)