def test_drop_table(self): db = SQLiteDBMS(get_temp_filename()) fr = db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')],['id']) fr.result() db.drop_table('TEST').result() self.assertRaises(DBException, db.drop_table('TEST').result)
def test_table_exists(self): db = SQLiteDBMS(get_temp_filename()) self.assertFalse(db.table_exists('TEST')) db = SQLiteDBMS(get_temp_filename()) db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')], ['id']) self.assertTrue(db.table_exists('TEST'))
def test_simple_db(self): db = SQLiteDBMS(get_temp_filename()) db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')]).result() db.execute('INSERT INTO TEST VALUES (1,"a")').result() self.assertIn((1, 'a'), db.select('SELECT * from TEST')) self.assertEqual((1, 'a'), db.select_one('SELECT * from TEST'))
def test_performance_with_multiple_cursors(self): raise SkipTest( 'This test is very specific to my workstation and was written just' ' to make sure that my changes did not break the performance of a' ' critical part of the framework.' '' 'It is specific to my workstation because of the hard-coded' ' ONE_CURSOR_TIME value, which should be updated in each environment' ' by making the dbms._query_handler implementation look like:' '' 'return self.cursor.execute(query, parameters)') # I measured the performance of doing 10000 UPDATE calls with the same # cursor in dbms._query_handler(). It took: ONE_CURSOR_TIME = 0.710026979446 # Now I'm testing the same thing with multiple cursors (which is the way # it should always have been). db = SQLiteDBMS(get_temp_filename()) db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')]).result() db.execute('INSERT INTO TEST VALUES (1, "a")').result() start_time = time.time() for i in xrange(10000): result = db.execute('UPDATE TEST SET data = ? WHERE id = ?', ('%s' % i, 1)).result() self.assertEqual(result.rowcount, 1) spent_time = time.time() - start_time self.assertLessEqual(spent_time, ONE_CURSOR_TIME * 1.1)
def test_simple_db(self): db = SQLiteDBMS(get_temp_filename()) db.create_table('TEST', set([('id', 'INT'), ('data', 'TEXT')])).result() db.execute('INSERT INTO TEST VALUES (1,"a")').result() self.assertIn(('1', 'a'), db.select('SELECT * from TEST')) self.assertEqual(('1', 'a'), db.select_one('SELECT * from TEST'))
def test_drop_table(self): db = SQLiteDBMS(get_temp_filename()) fr = db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')], ['id']) fr.result() db.drop_table('TEST').result() self.assertRaises(DBException, db.drop_table('TEST').result)
def test_performance_with_multiple_cursors(self): raise SkipTest('This test is very specific to my workstation and was written just' ' to make sure that my changes did not break the performance of a' ' critical part of the framework.' '' 'It is specific to my workstation because of the hard-coded' ' ONE_CURSOR_TIME value, which should be updated in each environment' ' by making the dbms._query_handler implementation look like:' '' 'return self.cursor.execute(query, parameters)') # I measured the performance of doing 10000 UPDATE calls with the same # cursor in dbms._query_handler(). It took: ONE_CURSOR_TIME = 0.710026979446 # Now I'm testing the same thing with multiple cursors (which is the way # it should always have been). db = SQLiteDBMS(get_temp_filename()) db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')]).result() db.execute('INSERT INTO TEST VALUES (1, "a")').result() start_time = time.time() for i in xrange(10000): result = db.execute('UPDATE TEST SET data = ? WHERE id = ?', ('%s' % i, 1)).result() self.assertEqual(result.rowcount, 1) spent_time = time.time() - start_time self.assertLessEqual(spent_time, ONE_CURSOR_TIME * 1.1)
def test_table_exists(self): db = SQLiteDBMS(get_temp_filename()) self.assertFalse(db.table_exists('TEST')) db = SQLiteDBMS(get_temp_filename()) db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')],['id']) self.assertTrue(db.table_exists('TEST'))
def test_update_update_rowcount(self): db = SQLiteDBMS(get_temp_filename()) db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')]).result() db.execute('INSERT INTO TEST VALUES (1, "a")').result() result = db.execute('UPDATE TEST SET data = ? WHERE id = ?', ('b', 1)).result() self.assertEqual(result.rowcount, 1) # There was a bug here where the same cursor instance was used as a result # for two (or more) UPDATE calls, which will override the rowcount value # # This lead to race conditions like: # # https://github.com/andresriancho/w3af/issues/16171 # result1 = db.execute('UPDATE TEST SET data = ? WHERE id = ?', ('c', 1)).result() result2 = db.execute('UPDATE TEST SET data = ? WHERE id = ?', ('nope', 3)).result() self.assertEqual(result1.rowcount, 1) self.assertEqual(result2.rowcount, 0)
def test_simple_db_with_pk(self): db = SQLiteDBMS(get_temp_filename()) fr = db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')],['id']) fr.result() self.assertEqual([], db.select('SELECT * from TEST'))
def test_close_twice(self): db = SQLiteDBMS(get_temp_filename()) db.close() db.close()
def test_simple_db_with_pk(self): db = SQLiteDBMS(get_temp_filename()) fr = db.create_table('TEST', [('id', 'INT'), ('data', 'TEXT')], ['id']) fr.result() self.assertEqual([], db.select('SELECT * from TEST'))
def test_select_non_exist_table(self): db = SQLiteDBMS(get_temp_filename()) self.assertRaises(NoSuchTableException, db.select, 'SELECT * from TEST')
def test_close_twice(self): db = SQLiteDBMS(get_temp_filename()) db.close() self.assertRaises(AssertionError, db.close)