def test1(self): 'dsaw.db.Psycopg2: integer array type' db = self.dbManager() # declare tables from dsaw.db.WithID import WithID class IntegerArrayTest(WithID): name = 'integerarraytest' import dsaw.db arr = dsaw.db.integerArray(name='arr') db.registerTable(IntegerArrayTest) db.createAllTables() t1 = IntegerArrayTest() t1.id = 't1' t1.arr = [1, 2] db.insertRow(t1) t1.arr = [3, 4] db.updateRecord(t1) db.destroyAllTables() return
def test1(self): 'dsaw.db.Psycopg2: double type' db = self.dbManager() # declare tables from dsaw.db.WithID import WithID class DoubleTest(WithID): name = 'doubletest' import dsaw.db d = dsaw.db.double(name='d') r = dsaw.db.real(name='r') db.registerTable(DoubleTest) db.createAllTables() t1 = DoubleTest() t1.id = 't1' import math t1.r = t1.d = math.pi db.insertRow(t1) print t1.r, t1.d self.assertAlmostEqual(t1.r, math.pi, 5) self.assertNotAlmostEqual(t1.r, math.pi, 10) self.assertAlmostEqual(t1.d, math.pi, 10) t1.d = math.e db.updateRecord(t1) db.destroyAllTables() return
def findPendingTask(self, db, iworker=None): import journal debug = journal.debug('itask-findPendingTask') from ITask import ITask tasks = self.getReferences(db, ITask, 'beneficiary') if not tasks: return found = None for task in tasks: # if not the right task, skip if task.worker != iworker: continue if task.state == 'finished': raise RuntimeError, "Task %s for %s finished but apparently it is not giving the right results or the results of this task has been mistakenly removed." % (task.id, self.id) if task.state == 'failed': debug.log("Task %s for %s found. Which has failed before." % (task.id, self.id)) return task if task.state == 'cancelled': debug.log("Task %s for %s found. Which was cancelled." % (task.id, self.id)) # reopen the task task.state = 'created' db.updateRecord(task) found = task break found = task break return found
def test1(self): 'dsaw.db.Psycopg2: varchar array type' db = self.dbManager() # declare tables from dsaw.db.WithID import WithID class VarCharArrayTest(WithID): name = 'varchararraytest' import dsaw.db arr = dsaw.db.varcharArray(name='arr', length=10) db.registerTable(VarCharArrayTest) db.createAllTables() t1 = VarCharArrayTest() t1.id = 't1' t1.arr = ['a', 'b', 'c'] db.insertRow(t1) t1.arr = ['hello', 'world'] db.updateRecord(t1) db.destroyAllTables() return
def test3c(self): 'dsaw.db.Psycopg2: table with reference. updateRecord' db = self.dbManager() # declare tables from dsaw.db.WithID import WithID class User(WithID): name = 'users' import dsaw.db username = dsaw.db.varchar(name='username', length=100) class Greeting(WithID): name = 'greetings' import dsaw.db greeting = dsaw.db.varchar(name='greeting', length=100) who = dsaw.db.reference(name='who', table=User) db._tablemap.registerTable(User) db._tablemap.TableToObject(User) tables = [User, Greeting] for table in tables: db.registerTable(table) db.createAllTables() # create two users userbob = User() userbob.id = userbob.username = '******' db.insertRow(userbob) useralice = User() useralice.id = useralice.username = '******' db.insertRow(useralice) # create a greeting greeting = Greeting() greeting.who = userbob greeting.greeting = 'hello' greeting.id = 'greeting1' db.insertRow(greeting) # update greeting.who = useralice greeting.greeting = 'aloha' db.updateRecord(greeting) # fetch it greeting1 = db.query(Greeting).filter_by(id=greeting.id).one() self.assertEqual(greeting1.greeting, 'aloha') self.assertEqual(greeting1.who.id, useralice.id) db.destroyAllTables() return
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 test1(self): 'dsaw.db.Psycopg2: double array type' db = self.dbManager() # declare tables from dsaw.db.WithID import WithID class DoubleArrayTest(WithID): name = 'doublearraytest' import dsaw.db arr = dsaw.db.doubleArray(name='arr') m = dsaw.db.doubleArray(name='m', shape=(2, 3)) db.registerTable(DoubleArrayTest) db.createAllTables() t1 = DoubleArrayTest() t1.id = 't1' t1.arr = [1., 2.] db.insertRow(t1) t1.arr = [3., 4.] db.updateRecord(t1) t1.arr = [5., 6.] t1.m = [[0, 1, 2], [3, 4, 5]] self.assertEqual(t1.m.shape, (2, 3)) t1.m = [[0, 1. / 3, 2], [3, 4, 5]] db.updateRecord(t1) self.assert_(abs(t1.m[0, 1] - 1. / 3) < 1e-12) t1a = db.query(DoubleArrayTest).filter_by(id='t1').one() self.assert_(abs(t1a.m[0, 1] - 1. / 3) < 1e-12) db.destroyAllTables() return
def test1(self): 'dsaw.db.VersatileReference: ' import dsaw.db db = dsaw.db.connect(db='postgres:///test') db.autocommit(True) print 'declare tables' from dsaw.db.WithID import WithID from dsaw.db.GloballyReferrable import GloballyReferrable class Cylinder(WithID, GloballyReferrable): name = 'cylinders' radius = dsaw.db.real(name='radius') height = dsaw.db.real(name='height') class Sphere(WithID, GloballyReferrable): name = 'spheres' radius = dsaw.db.real(name='radius') class Scatterer(WithID): name = 'scatterers' shape = dsaw.db.versatileReference(name='shape') tables = [ Sphere, Cylinder, Scatterer, ] for table in tables: db.registerTable(table) db.createAllTables() print 'insert records' sphere1 = Sphere() sphere1.id = 'sphere1' sphere1.radius = 10. cylinder1 = Cylinder() cylinder1.id = 'cylinder1' cylinder1.radius = 5 cylinder1.height = 10 scatterer1 = Scatterer() scatterer1.id = 'scatterer1' scatterer1.shape = sphere1 rows = [ sphere1, cylinder1, scatterer1, ] for row in rows: db.insertRow(row) print 'dereference' shape1 = scatterer1.shape.dereference(db) self.assertEqual(shape1.__class__, sphere1.__class__) self.assertEqual(shape1.id, sphere1.id) print 'fetch from db' rows = db.fetchall(Scatterer, where="id='%s'" % scatterer1.id) self.assertEqual(len(rows), 1) scatterer1r = rows[0] self.assertEqual(scatterer1r.shape.id, sphere1.globalpointer.id) print "make sure we don't create dangling reference" self.assertRaises(db.RecordStillReferred, db.deleteRecord, sphere1) print 'updateRecord: switch to a different type of shape' scatterer1.shape = cylinder1 db.updateRecord(scatterer1) shape1 = scatterer1.shape.dereference(db) self.assertEqual(shape1.__class__, cylinder1.__class__) self.assertEqual(shape1.id, cylinder1.id) print 'updateRecord 2: switch to a different type of shape by using a string' scatterer1.shape = 'spheres###sphere1' db.updateRecord(scatterer1) shape1 = scatterer1.shape.dereference(db) self.assertEqual(shape1.__class__, sphere1.__class__) self.assertEqual(shape1.id, sphere1.id) print 'updateRecord 3: switch to None' scatterer1.shape = None db.updateRecord(scatterer1) shape1 = scatterer1.shape.dereference(db) self.assertEqual(shape1, None) print 'remove tables' db.destroyAllTables() return
def resetResultRetrievalStatus(self, db): entry = self.getResultRetrievalEntry(filename='', db=db) if entry: entry.status = 'failed' db.updateRecord(entry) return
deemed to have suffered an error, exactly as for an unexpected exception. """ try: callableObj(*args, **kwargs) except excClass: return else: if hasattr(excClass,'__name__'): excName = excClass.__name__ else: excName = str(excClass) raise Exception, "%s not raised" % excName print "make sure we don't create dangling reference" assertRaises(db.RecordStillReferred, db.deleteRecord, sphere1) print 'updateRecord: switch to a different type of shape' scatterer1.shape = cylinder1 db.updateRecord(scatterer1) shape1 = scatterer1.shape.dereference(db) assertEqual( shape1.__class__, cylinder1.__class__) assertEqual(shape1.id, cylinder1.id) print 'updateRecord 2: switch to a different type of shape by using a string' scatterer1.shape = 'sphere###sphere1' db.updateRecord(scatterer1) shape1 = scatterer1.shape.dereference(db) assertEqual(shape1.__class__, sphere1.__class__) assertEqual(shape1.id, sphere1.id) print 'remove tables' db.destroyAllTables()
def test1(self): 'dsaw.db.Reference: ' import dsaw.db db = dsaw.db.connect(db='postgres:///test') db.autocommit(True) print 'declare tables' from dsaw.db.WithID import WithID class Cylinder(WithID): name = 'cylinders' radius = dsaw.db.real(name='radius') height = dsaw.db.real(name='height') class Scatterer(WithID): name = 'scatterers' shape = dsaw.db.reference(name='shape', table=Cylinder) tables = [ Cylinder, Scatterer, ] for table in tables: db.registerTable(table) db.createAllTables() print 'insert records' cylinder1 = Cylinder() cylinder1.id = 'cylinder1' cylinder1.radius = 5 cylinder1.height = 10 scatterer1 = Scatterer() scatterer1.id = 'scatterer1' scatterer1.shape = cylinder1 rows = [ cylinder1, scatterer1, ] for row in rows: db.insertRow(row) print 'dereference' shape1 = scatterer1.shape.dereference(db) self.assertEqual(shape1.__class__, Cylinder) self.assertEqual(shape1.id, cylinder1.id) print "make sure we don't create dangling reference" self.assertRaises(db.RecordStillReferred, db.deleteRecord, cylinder1) print 'updateRecord 3: switch to None' scatterer1.shape = None db.updateRecord(scatterer1) shape1 = scatterer1.shape self.assertEqual(shape1, None) print 'remove tables' db.destroyAllTables() return