def __createSet(self, SetClass, template, suffix, **kwargs): """ Create a set and set the filename using the suffix. If the file exists, it will be delete. """ setFn = self._getPath(template % suffix) # Close the connection to the database if # it is open before deleting the file cleanPath(setFn) SqliteDb.closeConnection(setFn) setObj = SetClass(filename=setFn, **kwargs) return setObj
def __createSet(self, SetClass, template, suffix): """ Create a set and set the filename using the suffix. If the file exists, it will be delete. """ setFn = self._getPath(template % suffix) # Close the connection to the database if # it is open before deleting the file cleanPath(setFn) SqliteDb.closeConnection(setFn) setObj = SetClass(filename=setFn) return setObj
def test_SqliteDb(self): """ Test the SqliteDb class that is used by the sqlite mappers. """ db = SqliteDb() db._createConnection(self.modelGoldSqlite, timeout=1000) tables = ['Objects', 'Relations'] self.assertEqual(tables, db.getTables()) # Test getting the version, for the gold file it should be 0 self.assertEqual(0, db.getVersion()) db.close()
def test_SqliteDb(self): """ Test the SqliteDb class that is used by the sqlite mappers. """ db = SqliteDb() db._createConnection(self.modelGoldSqlite, timeout=1000) tables = ['Objects', 'Relations'] self.assertEqual(tables, db.getTables()) # Test getting the version, for the gold file it should be 0 self.assertEqual(0, db.getVersion()) db.close()
def test_SqliteDb(self): """ Test the SqliteDb class that is used by the sqlite mappers. """ from pyworkflow.mapper.sqlite_db import SqliteDb db = SqliteDb() db._createConnection(self.modelGoldSqlite, timeout=1000) tables = ["Objects", "Relations"] self.assertEqual(tables, db.getTables()) db.close()
def test_SqliteMapper(self): fn = self.getOutputPath("basic.sqlite") fnGoldCopy = self.getOutputPath('gold.sqlite') fnGold = self.modelGoldSqlite print ">>> Using db: ", fn print " gold: ", fnGold print " gold copy: ", fnGoldCopy pwutils.copyFile(fnGold, fnGoldCopy) mapper = SqliteMapper(fn) # Insert a Complex c = Complex.createComplex() # real = 1, imag = 1 mapper.insert(c) # Insert an Integer i = Integer(1) mapper.insert(i) # Insert two Boolean b = Boolean(False) b2 = Boolean(True) mapper.insert(b) mapper.insert(b2) #Test storing pointers p = Pointer() p.set(c) mapper.insert(p) # Store csv list strList = ['1', '2', '3'] csv = CsvList() csv += strList mapper.insert(csv) # Test normal List iList = List() mapper.insert(iList) # Insert the list when empty i1 = Integer(4) i2 = Integer(3) iList.append(i1) iList.append(i2) mapper.update(iList) # now update with some items inside pList = PointerList() p1 = Pointer() p1.set(b) p2 = Pointer() p2.set(b2) pList.append(p1) pList.append(p2) mapper.store(pList) # Test to add relations relName = 'testRelation' creator = c mapper.insertRelation(relName, creator, i, b) mapper.insertRelation(relName, creator, i, b2) mapper.insertRelation(relName, creator, b, p) mapper.insertRelation(relName, creator, b2, p) # Save changes to file mapper.commit() self.assertEqual(1, mapper.db.getVersion()) # Intentionally keep gold.sqlite as version 0 to check # backward compatibility db = SqliteDb() db._createConnection(fnGold, timeout=1000) print "Checking old version is properly read" self.assertEqual(0, db.getVersion()) colNamesGold = [u'id', u'parent_id', u'name', u'classname', u'value', u'label', u'comment', u'object_parent_id', u'object_child_id', u'creation'] colNames = [col[1] for col in db.getTableColumns('Relations')] self.assertEqual(colNamesGold, colNames) # Reading test mapper2 = SqliteMapper(fnGoldCopy, globals()) print "Checking that Relations table is updated and version to 1" self.assertEqual(1, mapper2.db.getVersion()) # Check that the new column is properly added after updated to version 1 colNamesGold += [u'object_parent_extended', u'object_child_extended'] colNames = [col[1] for col in mapper2.db.getTableColumns('Relations')] self.assertEqual(colNamesGold, colNames) l = mapper2.selectByClass('Integer')[0] self.assertEqual(l.get(), 1) c2 = mapper2.selectByClass('Complex')[0] self.assertTrue(c.equalAttributes(c2)) b = mapper2.selectByClass('Boolean')[0] self.assertTrue(not b.get()) p = mapper2.selectByClass('Pointer')[0] self.assertEqual(c, p.get()) csv2 = mapper2.selectByClass('CsvList')[0] self.assertTrue(list.__eq__(csv2, strList)) # Iterate over all objects allObj = mapper2.selectAll() iterAllObj = mapper2.selectAll(iterate=True) for a1, a2 in zip(allObj, iterAllObj): # Note compare the scalar objects, which have a well-defined comparison if isinstance(a1, Scalar): self.assertEqual(a1, a2) # Test relations childs = mapper2.getRelationChilds(relName, i) parents = mapper2.getRelationParents(relName, p) # In this case both childs and parent should be the same for c, p in zip(childs, parents): self.assertEqual(c, p, "Childs of object i, should be the parents of object p") relations = mapper2.getRelationsByCreator(creator) for row in relations: print row
def test_SqliteMapper(self): fn = self.getOutputPath("basic.sqlite") fnGoldCopy = self.getOutputPath('gold.sqlite') fnGold = self.modelGoldSqlite print ">>> Using db: ", fn print " gold: ", fnGold print " gold copy: ", fnGoldCopy pwutils.copyFile(fnGold, fnGoldCopy) mapper = SqliteMapper(fn) # Insert a Complex c = Complex.createComplex() # real = 1, imag = 1 mapper.insert(c) # Insert an Integer i = Integer(1) mapper.insert(i) # Insert two Boolean b = Boolean(False) b2 = Boolean(True) mapper.insert(b) mapper.insert(b2) #Test storing pointers p = Pointer() p.set(c) mapper.insert(p) # Store csv list strList = ['1', '2', '3'] csv = CsvList() csv += strList mapper.insert(csv) # Test normal List iList = List() mapper.insert(iList) # Insert the list when empty i1 = Integer(4) i2 = Integer(3) iList.append(i1) iList.append(i2) mapper.update(iList) # now update with some items inside pList = PointerList() p1 = Pointer() p1.set(b) p2 = Pointer() p2.set(b2) pList.append(p1) pList.append(p2) mapper.store(pList) # Test to add relations relName = 'testRelation' creator = c mapper.insertRelation(relName, creator, i, b) mapper.insertRelation(relName, creator, i, b2) mapper.insertRelation(relName, creator, b, p) mapper.insertRelation(relName, creator, b2, p) # Save changes to file mapper.commit() self.assertEqual(1, mapper.db.getVersion()) # Intentionally keep gold.sqlite as version 0 to check # backward compatibility db = SqliteDb() db._createConnection(fnGold, timeout=1000) print "Checking old version is properly read" self.assertEqual(0, db.getVersion()) colNamesGold = [ u'id', u'parent_id', u'name', u'classname', u'value', u'label', u'comment', u'object_parent_id', u'object_child_id', u'creation' ] colNames = [col[1] for col in db.getTableColumns('Relations')] self.assertEqual(colNamesGold, colNames) # Reading test mapper2 = SqliteMapper(fnGoldCopy, globals()) print "Checking that Relations table is updated and version to 1" self.assertEqual(1, mapper2.db.getVersion()) # Check that the new column is properly added after updated to version 1 colNamesGold += [u'object_parent_extended', u'object_child_extended'] colNames = [col[1] for col in mapper2.db.getTableColumns('Relations')] self.assertEqual(colNamesGold, colNames) l = mapper2.selectByClass('Integer')[0] self.assertEqual(l.get(), 1) c2 = mapper2.selectByClass('Complex')[0] self.assertTrue(c.equalAttributes(c2)) b = mapper2.selectByClass('Boolean')[0] self.assertTrue(not b.get()) p = mapper2.selectByClass('Pointer')[0] self.assertEqual(c, p.get()) csv2 = mapper2.selectByClass('CsvList')[0] self.assertTrue(list.__eq__(csv2, strList)) # Iterate over all objects allObj = mapper2.selectAll() iterAllObj = mapper2.selectAll(iterate=True) for a1, a2 in zip(allObj, iterAllObj): # Note compare the scalar objects, which have a well-defined comparison if isinstance(a1, Scalar): self.assertEqual(a1, a2) # Test relations childs = mapper2.getRelationChilds(relName, i) parents = mapper2.getRelationParents(relName, p) # In this case both childs and parent should be the same for c, p in zip(childs, parents): self.assertEqual( c, p, "Childs of object i, should be the parents of object p") relations = mapper2.getRelationsByCreator(creator) for row in relations: print row