def test_Database_insert_update_record(self): db = self.db db('drop table if exists dzdb_test_table') cols = Columns() cols.append(Column('ID','CHAR',10)) cols.append(Column('NAME','CHAR',25)) cols.append(Column('AMOUNT','NUMERIC',10,2)) cols.append(Column('DTADD','DATE')) cols.append(Column('NOTES','TEXT')) db.create_table('dzdb_test_table',cols) dt = datetime(2005,01,02) db.insert_record('dzdb_test_table',cols,{'DTADD':dt,'ID':'1234','AMOUNT':50,'NOTES':'Testing'}) self.assertEqual(db('select * from dzdb_test_table').cursor.rowcount,1) db.insert_record('dzdb_test_table',cols,{'ID':'4321','AMOUNT':10,'NOTES':'Testing 2'}) self.assertEqual(db('select * from dzdb_test_table').cursor.rowcount,2) db.update_record('dzdb_test_table','ID',cols,{'ID':'4321','NOTES':"Updated"}) self.assertEqual(db('select * from dzdb_test_table').cursor._rows[1][4],"Updated") db('drop table dzdb_test_table') db('drop table if exists dz_test_contacts') db('create table dz_test_contacts (contactid integer PRIMARY KEY AUTO_INCREMENT,userid char(20) UNIQUE, key (userid),password char(16),email char(60), key (email))') db('insert into dz_test_contacts values (1,"testuser","pass","*****@*****.**")') self.assertEqual(db.lastrowid,1) db('insert into dz_test_contacts values (4,"2testuser","pass","*****@*****.**")') self.assertEqual(db.lastrowid,4) db('drop table dz_test_contacts')
def test_Database_create_drop_table(self): db = self.db db('drop table if exists dzdb_test_table') cols = Columns() cols.append(Column('ID', 'CHAR', 10)) cols.append(Column('NAME', 'CHAR', 25)) cols.append(Column('AMOUNT', 'NUMERIC', 10, 2)) cols.append(Column('DTADD', 'DATE')) cols.append(Column('NOTES', 'TEXT')) self.assert_(db.create_table('dzdb_test_table', cols) == 0) self.assert_('dzdb_test_table' in db.table_names()) db.drop_table('dzdb_test_table') self.assert_('dzdb_test_table' not in db.table_names())
def test_Columns(self): cols = Columns([Column('ID','CHAR',15),Column('Number','NUMERIC',15,1),Column('Comm','TEXT')]) self.assertEquals(cols.names(),['ID','Number','Comm']) cols = cols.drop(['ID']) self.assertEquals(cols.names(),['Number','Comm']) cols.append(Column('ID','CHAR',15)) cols = cols.keep(['ID']) self.assertEquals(cols.names(),['ID'])
def test_Columns(self): cols = Columns([ Column('ID', 'CHAR', 15), Column('Number', 'NUMERIC', 15, 1), Column('Comm', 'TEXT') ]) self.assertEquals(cols.names(), ['ID', 'Number', 'Comm']) cols = cols.drop(['ID']) self.assertEquals(cols.names(), ['Number', 'Comm']) cols.append(Column('ID', 'CHAR', 15)) cols = cols.keep(['ID']) self.assertEquals(cols.names(), ['ID'])
def test_Table_locate(self): db = self.db db('drop table if exists dzdb_test_table') cols = Columns([ Column('ID', 'NUMERIC', 10), Column('NAME', 'CHAR', 25), Column('AMOUNT', 'NUMERIC', 10, 2) ]) db.create_table('dzdb_test_table', cols) t = db.table('dzdb_test_table') from decimal import Decimal t.insert_record({'ID': 1, 'NAME': 'Andy', 'AMOUNT': Decimal('10')}) db.insert_record('dzdb_test_table', t.columns, { 'ID': 2, 'NAME': 'Alex', 'AMOUNT': Decimal('100.24') }) t = db.table('dzdb_test_table', 'ID') rec = t.locate(ID=1)[0] self.assert_(rec.ID == 1 and rec.NAME == 'Andy' and rec.AMOUNT == Decimal('10.00')) rec = t.locate(NAME='Alex')[0] self.assert_(rec.ID == 2 and rec.NAME == 'Alex' and rec.AMOUNT == Decimal('100.24')) db('drop table dzdb_test_table')
def test_Database_create_drop_table(self): db = self.db db('drop table if exists dzdb_test_table') cols = Columns() cols.append(Column('ID','CHAR',10)) cols.append(Column('NAME','CHAR',25)) cols.append(Column('AMOUNT','NUMERIC',10,2)) cols.append(Column('DTADD','DATE')) cols.append(Column('NOTES','TEXT')) self.assert_(db.create_table('dzdb_test_table',cols)==0) self.assert_('dzdb_test_table' in db.table_names()) db.drop_table('dzdb_test_table') self.assert_('dzdb_test_table' not in db.table_names())
def test_Database_insert_update_record(self): db = self.db db('drop table if exists dzdb_test_table') cols = Columns() cols.append(Column('ID', 'CHAR', 10)) cols.append(Column('NAME', 'CHAR', 25)) cols.append(Column('AMOUNT', 'NUMERIC', 10, 2)) cols.append(Column('DTADD', 'DATE')) cols.append(Column('NOTES', 'TEXT')) db.create_table('dzdb_test_table', cols) dt = datetime(2005, 01, 02) db.insert_record('dzdb_test_table', cols, { 'DTADD': dt, 'ID': '1234', 'AMOUNT': 50, 'NOTES': 'Testing' }) self.assertEqual( db('select * from dzdb_test_table').cursor.rowcount, 1) db.insert_record('dzdb_test_table', cols, { 'ID': '4321', 'AMOUNT': 10, 'NOTES': 'Testing 2' }) self.assertEqual( db('select * from dzdb_test_table').cursor.rowcount, 2) db.update_record('dzdb_test_table', 'ID', cols, { 'ID': '4321', 'NOTES': "Updated" }) self.assertEqual( db('select * from dzdb_test_table').cursor._rows[1][4], "Updated") db('drop table dzdb_test_table') db('drop table if exists dz_test_contacts') db('create table dz_test_contacts (contactid integer PRIMARY KEY AUTO_INCREMENT,userid char(20) UNIQUE, key (userid),password char(16),email char(60), key (email))' ) db('insert into dz_test_contacts values (1,"testuser","pass","*****@*****.**")' ) self.assertEqual(db.lastrowid, 1) db('insert into dz_test_contacts values (4,"2testuser","pass","*****@*****.**")' ) self.assertEqual(db.lastrowid, 4) db('drop table dz_test_contacts')