Example #1
0
    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
Example #2
0
    def test5(self):
        'dsaw.db: make sure globalpointer is left blank when a record is created'

        db = self.dbManager()

        # declare tables
        from dsaw.db.WithID import WithID
        from dsaw.db.GloballyReferrable import GloballyReferrable
        class User(WithID, GloballyReferrable):
            name = 'users'
            import dsaw.db
            username = dsaw.db.varchar(name='username', length=100)

        # register and initialize
        tables = [User]
        for table in tables: db.registerTable(table)
        db.createAllTables()

        # insert a record
        bob = User()
        bob.id = bob.username = '******'
        db.insertRow(bob)

        # read
        bob1 = db.query(User).filter_by(id=bob.id).one()
        self.assertEqual(bob1.globalpointer, None)
        
        # destroy
        db.destroyAllTables()
        return
Example #3
0
    def setUp(self):
        import dsaw.db
        db = dsaw.db.connect(db='sqlite://')

        tables = [
            User,
            Role,
            Privilege,
            UserHasRole,
            RoleHasRole,
            RoleHasPrivilege,
        ]
        map(db.registerTable, tables)
        db.createAllTables()

        # users
        linjiao = User()
        linjiao.id = linjiao.username = '******'
        db.insertRow(linjiao)

        demo = User()
        demo.id = demo.username = '******'
        db.insertRow(demo)

        # roles
        vnfadmin = Role()
        vnfadmin.rolename = 'admin'
        vnfadmin.context = 'vnf'
        db.insertRow(vnfadmin)
        #
        vnfuser = Role()
        vnfuser.rolename = 'user'
        vnfuser.context = 'vnf'
        db.insertRow(vnfuser)
        #
        vnfguest = Role()
        vnfguest.rolename = 'guest'
        vnfguest.context = 'vnf'
        db.insertRow(vnfguest)

        # privileges
        writesample = Privilege()
        writesample.name = 'write'
        writesample.target = 'sample'
        db.insertRow(writesample)

        # linjiao is a vnf admin
        linjiao.assignRole(vnfadmin, db)

        # vnf admin can be vnf user
        vnfadmin.becomeMember(vnfuser, db)

        # demo is a guest
        demo.assignRole(vnfguest, db)

        # vnf user can write samples
        grant(writesample, vnfuser, db)

        self.db = db
        return
Example #4
0
    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
Example #5
0
    def test4(self):
        'dsaw.db: DBManager.getUniqueIdentifierStr and DBManager.fetchRecordUsingUniqueIdentifierStr'

        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)

        # register and initialize
        tables = [User]
        for table in tables: db.registerTable(table)
        db.createAllTables()

        # insert a record
        bob = User()
        bob.id = bob.username = '******'
        db.insertRow(bob)
        
        #
        uidstr = db.getUniqueIdentifierStr(bob)
        bob1 = db.fetchRecordUsingUniqueIdentifierStr(uidstr)
        assert bob1.id == bob.id
        assert bob1.username == bob.username
        
        # destroy
        db.destroyAllTables()
        return
Example #6
0
    def test1(self):
        'dsaw.db.ReferenceSet: '

        # create table
        from dsaw.db.WithID import WithID

        class Test(WithID):

            name = "test"
            import dsaw.db

        # add more columns
        import dsaw.db
        address = dsaw.db.varchar(name='address', length=16)
        Test.addColumn(address)

        #
        db = self.db

        # create table
        db.registerTable(Test)
        db.createAllTables()

        # create new record
        t = Test()
        t.id = '111'
        t.address = 'abc'
        db.insertRow(t)

        # load from db and compare
        t1 = db.query(Test).filter_by(id=t.id).one()
        self.assertEqual(t.address, t1.address)

        return
Example #7
0
    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
Example #9
0
    def setUp(self):
        db = self.db = self.dbManager()

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

        db.createAllTables()

        return
Example #10
0
    def setUp(self):
        import dsaw.db
        db = dsaw.db.connect(db='sqlite://')

        tables = [
            User, Role, Privilege,
            UserHasRole, RoleHasRole, RoleHasPrivilege,
            ]
        map(db.registerTable, tables)
        db.createAllTables()

        # users
        linjiao = User(); linjiao.id = linjiao.username = '******'
        db.insertRow(linjiao)

        demo = User(); demo.id = demo.username = '******'
        db.insertRow(demo)

        # roles
        vnfadmin = Role(); vnfadmin.rolename='admin'; vnfadmin.context='vnf'
        db.insertRow(vnfadmin)
        #
        vnfuser = Role(); vnfuser.rolename='user'; vnfuser.context='vnf'
        db.insertRow(vnfuser)
        #
        vnfguest = Role(); vnfguest.rolename='guest'; vnfguest.context='vnf'
        db.insertRow(vnfguest)

        # privileges
        writesample = Privilege(); writesample.name='write'; writesample.target='sample'
        db.insertRow(writesample)
        
        # linjiao is a vnf admin
        linjiao.assignRole(vnfadmin, db)

        # vnf admin can be vnf user
        vnfadmin.becomeMember(vnfuser, db)

        # demo is a guest
        demo.assignRole(vnfguest, db)

        # vnf user can write samples
        grant(writesample, vnfuser, db)

        self.db = db
        return
    def _test2(self):
        'dsaw.db.GloballyReferrable: establish global pointer automatically'
        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')

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

        db.createAllTables()

        print 'insert records'
        cylinder1 = Cylinder()
        cylinder1.id = 'cylinder1'
        cylinder1.radius = 5
        cylinder1.height = 10

        rows = [
            cylinder1,
        ]
        for row in rows:
            db.insertRow(row)

        self.assert_(cylinder1.globalpointer and cylinder1.globalpointer.id)

        print 'remove tables'
        db.destroyAllTables()
        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
    height = 2.0
    
class Sphere(WithID, GloballyReferrable):
    radius = 1.1
    
class Scatterer(WithID):
    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
Example #15
0
    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
    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