def create_system_tables(db): from dsaw.db import systemTables system_tables = systemTables() for table in system_tables.itertables(): db.createTable(table) continue return
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): 'dsaw.db.Psycopg2: normal table' db = self.dbManager() # init system tables db.createSystemTables() from dsaw.db.WithID import WithID class Table1(WithID): name = 'table1' import dsaw.db greeting = dsaw.db.varchar(name='greeting', length=100) db.createTable(Table1) # insert record row = Table1() row.greeting = 'hello' row.id = 'first' db.insertRow(row) # fetch it rows = db.fetchall(Table1, where="id='%s'" % row.id) self.assertEqual(len(rows), 1) row1 = rows[0] self.assertEqual(row1.greeting, row.greeting) db.dropTable(Table1) # remove system tables db.destroySystemTables() return
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
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
def test3b(self): 'dsaw.db.Psycopg2: table with reference. catch dangling reference' 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) tables = [User, Greeting] # init system tables db.createSystemTables() # for table in tables: db.createTable(table) # create a user user = User() user.id = user.username = '******' db.insertRow(user) # create a greeting greeting = Greeting() greeting.who = user greeting.greeting = 'hello' greeting.id = 'greeting1' db.insertRow(greeting) # delete user would leave a dangling reference self.assertRaises(db.RecordStillReferred, db.deleteRecord, user) # tables.reverse() for table in tables: db.dropTable(table) # db.destroySystemTables() return
def test3(self): 'dsaw.db.Psycopg2: table with reference' db = self.dbManager() 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) tables = [User, Greeting] # init system tables db.createSystemTables() # for table in tables: db.createTable(table) # create a user user = User() user.username = '******' user.id = 'bob1' db.insertRow(user) # create a greeting greeting = Greeting() greeting.who = user greeting.greeting = 'hello' greeting.id = 'greeting1' db.insertRow(greeting) # tables.reverse() for table in tables: db.dropTable(table) # db.destroySystemTables() return
def test3a(self): 'dsaw.db.Psycopg2: table with reference. catch non-existing reference' 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) tables = [User, Greeting] # init system tables db.createSystemTables() # for table in tables: db.createTable(table) # create a greeting greeting = Greeting() greeting.id = 'greeting1' greeting.who = 'a' greeting.greeting = 'hello' self.assertRaises(db.IntegrityError, db.insertRow, greeting) # tables.reverse() for table in tables: db.dropTable(table) # db.destroySystemTables() return