def test_removeFromLists(self):
        """ Check that lists are properly stored after removing some elements.
        """
        fn = self.getOutputPath("lists.sqlite")

        print(">>> Using db: ", fn)

        # Let's create a Mapper to store a simple List containing two integers
        mapper = pwmapper.SqliteMapper(fn,
                                       pw.Config.getDomain().getMapperDict())
        iList = pwobj.List()
        i1 = pwobj.Integer(4)
        i2 = pwobj.Integer(3)
        iList.append(i1)
        iList.append(i2)
        # Store the list and commit changes to db, then close db.
        mapper.store(iList)
        mapper.commit()
        mapper.close()

        # Now let's open again the db with a different connection
        # and load the previously stored list
        mapper2 = pwmapper.SqliteMapper(fn,
                                        pw.Config.getDomain().getMapperDict())
        iList2 = mapper2.selectByClass('List')[0]
        # Let's do some basic checks
        self.assertEqual(iList2.getSize(), 2)
        self.assertTrue(pwobj.Integer(4) in iList2)
        self.assertTrue(pwobj.Integer(3) in iList2)

        # Now remove one of the integers in the list
        # check consistency in the list elements
        iList2.remove(pwobj.Integer(4))
        self.assertEqual(iList2.getSize(), 1)
        self.assertTrue(pwobj.Integer(4) not in iList2)
        self.assertTrue(pwobj.Integer(3) in iList2)
        # Store once again the new list with one element
        mapper2.store(iList2)
        mapper2.commit()
        mapper2.close()

        # Open the db and load the list once again
        mapper3 = pwmapper.SqliteMapper(fn,
                                        pw.Config.getDomain().getMapperDict())
        iList3 = mapper3.selectByClass('List')[0]
        # Check the same consistency before it was stored
        self.assertEqual(iList3.getSize(), 1)
        self.assertTrue(pwobj.Integer(4) not in iList3)
        self.assertTrue(pwobj.Integer(3) in iList3)
Esempio n. 2
0
 def __init__(self, text=None, value=None, icon=None, tag=None, **kwargs):
     """Constructor for the Menu config item.
     Arguments:
       text: text to be displayed
       value: internal value associated with the item.
       icon: display an icon with the item
       tag: put some tags to items
     **args: pass other options to base class.
     """
     self.text = pwobj.String(text)
     self.value = pwobj.String(value)
     self.icon = pwobj.String(icon)
     self.tag = pwobj.String(tag)
     self.childs = pwobj.List()
     self.openItem = pwobj.Boolean(kwargs.get('openItem', False))
Esempio n. 3
0
 def __init__(self, text=None, value=None, icon=None, tag=None, **kwargs):
     """Constructor for the Menu config item.
     Arguments:
       text: text to be displayed
       value: internal value associated with the item.
       icon: display an icon with the item
       tag: put some tags to items
     **args: pass other options to base class.
     """
     self.text = text
     self.value = value
     self.icon = icon
     self.tag = tag
     self.shortCut = kwargs.get('shortCut', None)
     self.childs = pwobj.List()
     self.openItem = kwargs.get('openItem', False)
    def test_SqliteMapper(self):
        fn = self.getOutputPath("basic.sqlite")
        mapper = pwmapper.SqliteMapper(fn)

        # Insert a Float
        f = pwobj.Float(5.4)
        mapper.insert(f)

        # Insert an pwobj.Integer
        i = pwobj.Integer(1)
        mapper.insert(i)

        # Insert two pwobj.Boolean
        b = pwobj.Boolean(False)
        b2 = pwobj.Boolean(True)
        mapper.insert(b)
        mapper.insert(b2)

        # Test storing pointers
        p = pwobj.Pointer(b)
        mapper.insert(p)

        # Store csv list
        strList = ['1', '2', '3']
        csv = pwobj.CsvList()
        csv += strList
        mapper.insert(csv)

        # Test normal List
        iList = pwobj.List()
        mapper.insert(iList)  # Insert the list when empty
        i1 = pwobj.Integer(4)
        i2 = pwobj.Integer(3)
        iList.append(i1)
        iList.append(i2)
        mapper.update(iList)  # now update with some items inside

        pList = pwobj.PointerList()
        p1 = pwobj.Pointer(b)
        # p1.set(b)
        p2 = pwobj.Pointer(b2)
        # p2.set(b2)
        pList.append(p1)
        pList.append(p2)
        mapper.store(pList)

        # Test to add relations
        relName = 'testRelation'
        creator = f
        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())
        mapper.close()

        # TODO: Maybe some mapper test for backward compatibility can be
        # include in scipion-em, where we already have defined datasets
        # and reference old sqlite files

        # Test using SqliteDb class
        db = pwmapper.SqliteDb()
        db._createConnection(fn, timeout=1000)
        tables = ['Objects', 'Relations']
        self.assertEqual(tables, db.getTables())
        # Test getting the version, for the gold file it should be 0
        self.assertEqual(1, db.getVersion())
        db.close()

        # Reading test
        mapper2 = pwmapper.SqliteMapper(fn, pw.Config.getDomain().getMapperDict())
        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'id', u'parent_id', u'name', u'classname',
                        u'value', u'label', u'comment', u'object_parent_id',
                        u'object_child_id', u'creation',
                        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)

        f2 = mapper2.selectByClass('Float')[0]
        self.assertEqual(f, f2.get())

        b = mapper2.selectByClass('Boolean')[0]
        self.assertTrue(not b.get())

        p = mapper2.selectByClass('Pointer')[0]
        self.assertEqual(b.get(), 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, pwobj.Scalar):
                self.assertEqual(a1, a2)

        # Test select all batch approach
        allBatch = mapper2.selectAllBatch()

        # 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(dict(row))