def test_StorePointers(self):
        """ Check that pointers are correctly stored. """
        fn = self.getOutputPath("pointers.sqlite")

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

        mapper = pwmapper.SqliteMapper(fn)
        # Insert a Complex
        c = Complex.createComplex()  # real = 1, imag = 1
        mapper.insert(c)
        # Insert an pwobj.Integer
        p1 = pwobj.Pointer(c)
        p1.setExtended('real')

        mapper.store(c)
        mapper.store(p1)

        self.assertAlmostEqual(c.real.get(), p1.get().get())

        p1.set(None)  # Reset value and check that is stored properly

        self.assertIsNone(p1._extended.get())
        mapper.store(p1)
        mapper.commit()

        mapper2 = pwmapper.SqliteMapper(fn, pw.Config.getDomain().getMapperDict())
        p2 = mapper2.selectByClass('Pointer')[0]

        # Check the mapper was properly stored when
        # set to None and the _extended property cleaned
        self.assertIsNone(p2.get())
Example #2
0
    def test_StepExecutor(self):
        """Test the list with several Complex"""
        fn = self.getOutputPath("protocol.sqlite")
        print("Writing to db: %s" % fn)

        # Discover objects and protocols
        mapperDict = Domain.getMapperDict()

        # Check that the protocol has associated package
        mapper = pwmapper.SqliteMapper(fn, mapperDict)
        prot = SleepingProtocol(mapper=mapper,
                                n=2,
                                workingDir=self.getOutputPath(''))
        domain = prot.getClassDomain()
        domain.printInfo()

        prot.setStepsExecutor(pwprot.StepExecutor(hostConfig=None))
        prot.run()
        mapper.commit()
        mapper.close()

        self.assertEqual(prot._steps[0].getStatus(), pwprot.STATUS_FINISHED)

        mapper2 = pwmapper.SqliteMapper(fn, mapperDict)
        prot2 = mapper2.selectById(prot.getObjId())

        self.assertEqual(prot.endTime.get(), prot2.endTime.get())
    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)
    def test_basicObjectOutput(self):
        """Test the list with several Complex"""
        fn = self.getOutputPath("protocol.sqlite")

        # Discover objects and protocols
        mapperDict = Domain.getMapperDict()

        mapper = pwmapper.SqliteMapper(fn, mapperDict)
        prot = ProtOutputTest(mapper=mapper,
                              n=2,
                              workingDir=self.getOutputPath(''))

        # Add and old style o, not in the outputs dictionary
        prot.output1 = MockObject()

        self.assertFalse(prot._useOutputList.get(),
                         "useOutputList wrongly initialized")

        outputs = [o for o in prot.iterOutputAttributes()]
        self.assertTrue(1, len(outputs))

        prot._stepsExecutor = pwprot.StepExecutor(hostConfig=None)
        prot.run()

        self.assertEqual(prot._steps[0].getStatus(), pwprot.STATUS_FINISHED)

        # Check there is an o
        self.assertOutput(prot)

        outputs = [o for o in prot.iterOutputAttributes()]

        # We are intentionally ignoring a protocol with o (EMObject)
        # That has been continued, We do not find a real case now.
        self.assertEqual(1,
                         len(outputs),
                         msg="Integer o not registered properly.")

        outputs = [o for o in prot.iterOutputAttributes(pwobj.Integer)]

        # Test passing a filter
        self.assertEqual(1,
                         len(outputs),
                         msg="Integer not matched when filtering outputs.")

        # Test with non existing class
        class NotRealClass:
            pass

        outputs = [o for o in prot.iterOutputAttributes(NotRealClass)]

        # Test passing a class
        self.assertEqual(0,
                         len(outputs),
                         msg="Filter by class in iterOutputAttributes does "
                         "not work.")

        self.assertTrue(prot._useOutputList.get(),
                        "useOutputList not activated")
    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))