Exemplo n.º 1
0
    def test1(self):
        'dsaw.db.pickler: basic table'
        self.removeDB()

        user1 = User()
        user1.id = 1
        user1.username = '******'

        db = self.dbManager()
        db.createTable(User)
        db.insertRow(user1)
        db.commit()

        from dsaw.db.Pickler import Pickler
        outdir = 'pickle-test1-out'
        if os.path.exists(outdir):
            shutil.rmtree(outdir)
        pickler = Pickler(db, outdir)
        pickler.dump(User)

        import pickle
        pkl = os.path.join(outdir, User.getTableName())
        tablename, fields, records = pickle.load(open(pkl))

        self.assertEqual(tablename, User.getTableName())
        self.assertEqual(fields, tuple(user1.getColumnNames()))
        self.assertEqual(records[0][0],
                         user1.getColumnValue(user1.getColumnNames()[0]))

        # resolve order
        resolve_order = open(
            os.path.join(outdir, Pickler.resolve_order_filename)).read()
        resolve_order = resolve_order.splitlines()
        self.assertEqual(resolve_order, ['users'])
        return
    def test2(self):
        'add, delete two users'
        db = self.db
        
        # insert records
        userbob = User()
        userbob.id = userbob.username = '******'
        db.insertRow(userbob)
        
        useralice = User()
        useralice.id = useralice.username = '******'
        db.insertRow(useralice)

        group = Group()
        group.id = 'group2'
        db.insertRow(group)
        db.commit()

        refset = group.users
        refset.add(userbob, db)
        refset.add(useralice, db)

        users = refset.dereference(db)
        self.assertEqual(len(users), 2)

        refset.delete(useralice, db)
        refset.delete(userbob, db)
        return
    def test5(self):
        'insert'
        db = self.db
        
        # insert records
        userbob = User()
        userbob.id = userbob.username = '******'
        db.insertRow(userbob)
        
        useralice = User()
        useralice.id = useralice.username = '******'
        db.insertRow(useralice)

        group = Group()
        group.id = 'group5'
        db.insertRow(group)
        db.commit()

        refset = group.users

        # bob
        refset.add(userbob, db=db)

        # alice, bob
        refset.insert(useralice, before=userbob, db=db)
        #
        self.assertEqual(refset.getElement(index=0, db=db).id, useralice.id)
        self.assertEqual(refset.getElement(index=1, db=db).id, userbob.id)
        return
Exemplo n.º 4
0
    def test3(self):
        'dsaw.db.pickler: table with a column being versatile reference'
        self.removeDB()

        # create tables and insert rows
        tables = [global_pointer, Cylinder, Sample]
        cyl1 = Cylinder()
        cyl1.id = 3
        sample1 = Sample()
        sample1.shape = cyl1
        sample1.id = 5
        records = [
            cyl1,
            sample1,
        ]

        db = self.dbManager()
        for t in tables:
            db.createTable(t)
        for r in records:
            db.insertRow(r)
        db.commit()

        # pickle
        from dsaw.db.Pickler import Pickler
        outdir = 'pickle-test3-out'
        if os.path.exists(outdir):
            shutil.rmtree(outdir)
        pickler = Pickler(db, outdir)
        pickler.dump(tables=tables)

        # load and compare
        import pickle
        #  Cylinder
        pkl = os.path.join(outdir, Cylinder.getTableName())
        tablename, fields, records = pickle.load(open(pkl))

        self.assertEqual(tablename, Cylinder.getTableName())
        self.assertEqual(fields, tuple(cyl1.getColumnNames()))
        self.assertEqual(records[0][0], cyl1.globalpointer.id)

        #  Sample
        pkl = os.path.join(outdir, Sample.getTableName())
        tablename, fields, records = pickle.load(open(pkl))

        self.assertEqual(tablename, Sample.getTableName())
        self.assertEqual(fields, tuple(sample1.getColumnNames()))
        self.assertEqual(records[0][1], sample1.id)
        self.assertEqual(records[0][0], cyl1.globalpointer.id)

        # resolve order
        resolve_order = open(
            os.path.join(outdir, Pickler.resolve_order_filename)).read()
        resolve_order = resolve_order.splitlines()
        self.assertEqual(resolve_order,
                         ['global_pointers', 'cylinders', 'samples'])
        return
Exemplo n.º 5
0
    def test2(self):
        'dsaw.db.pickler: table with a column being reference'
        self.removeDB()

        tables = [User, Simulation]
        user1 = User()
        user1.id = 1
        user1.username = '******'
        sim1 = Simulation()
        sim1.creator = user1
        sim1.id = 5
        records = [
            user1,
            sim1,
        ]

        db = self.dbManager()
        for t in tables:
            db.createTable(t)
        for r in records:
            db.insertRow(r)
        db.commit()

        from dsaw.db.Pickler import Pickler
        outdir = 'pickle-test2-out'
        if os.path.exists(outdir):
            shutil.rmtree(outdir)
        pickler = Pickler(db, outdir)
        pickler.dump(Simulation)

        # load and compare
        import pickle
        #  User
        pkl = os.path.join(outdir, User.getTableName())
        tablename, fields, records = pickle.load(open(pkl))

        self.assertEqual(tablename, User.getTableName())
        self.assertEqual(fields, tuple(user1.getColumnNames()))
        self.assertEqual(records[0][0],
                         user1.getColumnValue(user1.getColumnNames()[0]))

        #  Simulation
        pkl = os.path.join(outdir, Simulation.getTableName())
        tablename, fields, records = pickle.load(open(pkl))

        self.assertEqual(tablename, Simulation.getTableName())
        self.assertEqual(fields, tuple(sim1.getColumnNames()))
        self.assertEqual(records[0][0], sim1.id)
        self.assertEqual(records[0][1], user1.id)

        # resolve order
        resolve_order = open(
            os.path.join(outdir, Pickler.resolve_order_filename)).read()
        resolve_order = resolve_order.splitlines()
        self.assertEqual(resolve_order, ['users', 'simulations'])
        return
Exemplo n.º 6
0
    def markResultFileStatusAndErrorMessage(self, filename, db, status, message=None):
        message_len = _ComputationResultRetrievals.message.length
        
        gp = self.globalpointer
        gp = gp and gp.id
        if not gp:
            # no global address yet, means need new entry
            neednewentry = True
        else:
            # look for the entries
            where = "computation=%s and filename='%s'"  % (gp, filename)
            rs = db.query(_ComputationResultRetrievals).filter(where).all()
            db.commit()
            
            if len(rs) == 0:
                # no entry, need one
                neednewentry = True
                
            elif len(rs) > 1:
                # more than one entries, error
                raise RuntimeError, \
                      "multiple entries in table %s for computation %s(%s), filename %s" % (
                    _ComputationResultRetrievals.getTableName(),
                    self.getTableName(), self.id,
                    filename)
                    
            else:
                # found one entry
                neednewentry = False
                r = rs[0]
                
        if neednewentry:
            # create a new entry
            r = _ComputationResultRetrievals()
            r.computation = self
            #db.insertRow(r)
            r.status = status
            #db.updateRecord(r)
            r.filename = filename
            #db.updateRecord(r)
            if message is not None:
                r.message = message[:message_len]
            #db.updateRecord(r)
            db.insertRow(r)
            return

        # update entry
        r.status = status
        if message is not None:
            r.message = message[:message_len]
        db.updateRecord(r)
        return
    def test4(self):
        'get, set, delete by index'
        db = self.db
        
        # insert records
        userbob = User()
        userbob.id = userbob.username = '******'
        db.insertRow(userbob)
        
        useralice = User()
        useralice.id = useralice.username = '******'
        db.insertRow(useralice)

        group = Group()
        group.id = 'group4'
        db.insertRow(group)
        db.commit()

        refset = group.users

        # bob, alice
        refset.add(userbob, db=db)
        refset.add(useralice, db=db)

        user0 = refset.getElement(db=db, index=0)
        self.assertEqual(user0.id, userbob.id)

        # alice, alice
        refset.setElement(element=useralice, index=0, db=db)
        user0 = refset.getElement(db=db, index=0)
        self.assertEqual(user0.id, useralice.id)

        # alice, bob
        refset.setElement(element=userbob, index=1, db=db)
        user0 = refset.getElement(db=db, index=0)
        self.assertEqual(user0.id, useralice.id)
        user1 = refset.getElement(db=db, index=1)
        self.assertEqual(user1.id, userbob.id)

        # bob
        refset.delElement(index=0, db=db)
        user0 = refset.getElement(db=db, index=0)
        self.assertEqual(user0.id, userbob.id)
        
        return
Exemplo n.º 8
0
 def getResultRetrievalEntry(self, filename, db):
     gp = self.globalpointer
     gp = gp and gp.id
     if not gp: return 
     
     where = "computation=%s and filename='%s' "  % (gp, filename)
     rs = db.query(_ComputationResultRetrievals).filter(where).all()
     db.commit()
     
     if not rs: return
     
     if len(rs)>1:
         raise RuntimeError, \
               "multiple entries in table %s for computation %s(%s), filename %s" % (
             _ComputationResultRetrievals.getTableName(),
             self.getTableName(), self.id,
             filename)
     
     return rs[0]
    def test3(self):
        'get, set, delete by key'
        db = self.db
        
        # insert records
        userbob = User()
        userbob.id = userbob.username = '******'
        db.insertRow(userbob)
        
        useralice = User()
        useralice.id = useralice.username = '******'
        db.insertRow(useralice)

        group = Group()
        group.id = 'group3'
        db.insertRow(group)
        db.commit()

        refset = group.users
        refset.setElement(element=userbob, db=db, key='key1')
        refset.setElement(element=useralice, db=db, key='key2')

        user1 = refset.getElement(db=db, key='key1')
        self.assertEqual(user1.id, userbob.id)

        user2 = refset.getElement(db=db, key='key2')
        self.assertEqual(user2.id, useralice.id)

        refset.delElement(db=db, key='key1')
        self.assertEqual(len(refset.dereference(db)), 1)

        user2 = refset.getElement(db=db, key='key2')
        self.assertEqual(user2.id, useralice.id)

        user0 = refset.getElement(db=db, index=0)
        self.assertEqual(user0.id, useralice.id)

        refset.setElement(db=db, key='key1', element=userbob)

        user1 = refset.getElement(db=db, index=1)
        self.assertEqual(user1.id, userbob.id)
        return
Exemplo n.º 10
0
    def testMixed(self):
        'dsaw.db.ReferenceSet: mixed'
        db = self.db

        # insert records
        userbob = User()
        userbob.id = userbob.username = '******'
        db.insertRow(userbob)

        useralice = User()
        useralice.id = useralice.username = '******'
        db.insertRow(useralice)
        db.commit()

        group = Group()
        group.id = 'groupMixed'
        db.insertRow(group)
        db.commit()

        refset = group.users
        print 'referenceset instance: %s' % refset
        users = refset.dereference(db)
        self.assertEqual(len(users), 0)
        print 'users: %s' % (users, )

        print '> add one user'
        refset.add(userbob, db, name='1')
        users = refset.dereference(db)
        self.assertEqual(len(users), 1)

        print '> delete one user'
        refset.delete(userbob, db)
        users = refset.dereference(db)
        self.assertEqual(len(users), 0)

        print '> add two users'
        refset.add(userbob, db)
        refset.add(useralice, db)
        users = refset.dereference(db)
        self.assertEqual(len(users), 2)
        ids = [row.id for label, row in users]
        self.assert_('bobMixed' in ids)
        self.assert_('aliceMixed' in ids)

        print '> remove all users'
        refset.clear(db)
        users = refset.dereference(db)
        self.assertEqual(len(users), 0)

        print '> getElement and setElement'
        refset.setElement('1', userbob, db)
        refset.setElement('2', useralice, db)
        self.assertEqual(refset.getElement('1', db).id, userbob.id)

        users = refset.dereference(db)
        self.assertEqual(len(users), 2)

        ids = [row.id for label, row in users]
        self.assert_('bobMixed' in ids)
        self.assert_('aliceMixed' in ids)

        refset.setElement('2', userbob, db)
        users = refset.dereference(db)
        self.assertEqual(len(users), 2)

        ids = [row.id for label, row in users]
        self.assert_('bobMixed' in ids)
        self.assert_('aliceMixed' not in ids)

        print '> delElement'
        deleted = refset.delElement('1', db)
        self.assertEqual(deleted.id, 'bobMixed')
        self.assertEqual(len(refset.dereference(db)), 1)
        self.assertRaises(KeyError, refset.delElement, '1', db)

        print '> remove all users'
        refset.clear(db)
        users = refset.dereference(db)
        self.assertEqual(len(users), 0)

        return
    def test1(self):
        'dsaw.db.ReferenceSet: '
        db = self.dbManager()

        # declare tables
        from dsaw.db.GloballyReferrable import GloballyReferrable
        from dsaw.db.WithID import WithID

        class User(GloballyReferrable, WithID):

            name = "users"

            username = '******'
            password = "******"

        class Group(GloballyReferrable, WithID):

            name = "groups"

            #            id = dsaw.db.varchar(name="id", length=30)
            #            id.constraints = "PRIMARY KEY"

            import dsaw.db
            users = dsaw.db.referenceSet(name='users')

        tables = [
            User,
            Group,
        ]
        for table in tables:
            db.registerTable(table)

        db.createAllTables()

        # insert records
        userbob = User()
        userbob.id = userbob.username = '******'
        db.insertRow(userbob)

        useralice = User()
        useralice.id = useralice.username = '******'
        db.insertRow(useralice)
        db.commit()

        group = Group()
        group.id = 'group1'
        db.insertRow(group)
        db.commit()

        refset = group.users
        print 'referenceset instance: %s' % refset
        users = refset.dereference(db)
        self.assertEqual(len(users), 0)
        print 'users: %s' % (users, )

        print '> add one user'
        refset.add(userbob, db)
        users = refset.dereference(db)
        self.assertEqual(len(users), 1)

        print '> delete one user'
        refset.delete(userbob, db)
        users = refset.dereference(db)
        self.assertEqual(len(users), 0)

        print '> add two users'
        refset.add(userbob, db)
        refset.add(useralice, db)
        users = refset.dereference(db)
        self.assertEqual(len(users), 2)
        ids = [row.id for label, row in users]
        self.assert_('bob' in ids)
        self.assert_('alice' in ids)

        print '> remove all users'
        refset.clear(db)
        users = refset.dereference(db)
        self.assertEqual(len(users), 0)

        # finish  and clean up
        db.destroyAllTables()
        return