def test_remove_doubles(self): old_db = os.path.join(os.path.dirname(__file__), 'testfiles/openport-0.9.1.db') old_db_tmp = os.path.join(os.path.dirname(__file__), 'testfiles/tmp/openport-0.9.1.db') shutil.copy(old_db, old_db_tmp) db_handler = dbhandler.DBHandler(old_db_tmp) def filter_shares_by_local_port(shares, local_port): return [share for share in shares if share.local_port==local_port] all_shares = db_handler.get_all_shares() filtered = filter_shares_by_local_port(all_shares, 22) self.assertTrue(len(filtered) > 1) self.assertEqual(2, len(filtered)) new_share = Share(local_port=22, active=True) db_handler.add_share(new_share) all_shares = db_handler.get_all_shares() filtered = filter_shares_by_local_port(all_shares, 22) self.assertEqual(1, len(filtered)) session = db_handler.get_share_by_local_port(22) self.assertNotEqual(None, session)
def init_db_test(): try: dbh = dbhandler.DBHandler(self.test_db) dbh.init_db(True) except Exception as e: global errors errors.append(e)
def test_alembic(self): old_db = os.path.join(os.path.dirname(__file__), 'testfiles/openport-0.9.1.db') old_db_tmp = os.path.join(os.path.dirname(__file__), 'testfiles/tmp/openport-0.9.1.db') shutil.copy(old_db, old_db_tmp) db_handler = dbhandler.DBHandler(old_db_tmp) session = db_handler.get_share_by_local_port(22) self.assertNotEqual(None, session)
def test_concurrency(self): dbhandler2 = dbhandler.DBHandler(self.test_db) share = Share() share.local_port = 2224 saved_share = self.dbhandler.add_share(share) retrieved_share = dbhandler2.get_share(saved_share.id) try: self.assertEqual(retrieved_share.local_port, share.local_port) finally: dbhandler2.close()
def setUp(self): self.os_interaction = getInstance() self.db_file = os.path.join(os.path.dirname(__file__), 'testfiles', 'tmp', 'tmp_openport.db') if os.path.exists(self.db_file): try: os.remove(self.db_file) except: sleep(3) os.remove(self.db_file) os.chdir(os.path.dirname(os.path.dirname(__file__))) self.db_handler = dbhandler.DBHandler(self.db_file)
def setUp(self): print self._testMethodName logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.WARN) dbhandler.TIMEOUT = 3 self.test_db = os.path.join(os.path.dirname(__file__), 'testfiles', 'tmp', 'db_test.db') if os.path.exists(self.test_db): os.remove(self.test_db) set_log_level(logging.DEBUG) self.dbhandler = dbhandler.DBHandler(self.test_db) self.dbhandler.init_db(False)
def test_concurrency_2(self): dbhandler2 = dbhandler.DBHandler(self.test_db) try: share = Share(active=True) share.local_port = 2224 share2 = Share(active=True) share2.local_port = 2225 saved_share = self.dbhandler.add_share(share) saved_share2 = dbhandler2.add_share(share2) retrieved_share2 = self.dbhandler.get_share_by_local_port(2225) retrieved_share = self.dbhandler.get_share_by_local_port(2224) self.assertEqual(retrieved_share.local_port, share.local_port) self.assertEqual(retrieved_share2.local_port, share2.local_port) finally: dbhandler2.close()
def test_stress_test(self): share = Share() dbhandler2 = dbhandler.DBHandler(self.test_db) try: errors = [] for i in range(100): try: share.local_port = i saved_share = self.dbhandler.add_share(share) retrieved_share = dbhandler2.get_share(saved_share.id) self.assertEqual(retrieved_share.local_port, share.local_port) saved_share = dbhandler2.add_share(share) retrieved_share = self.dbhandler.get_share(saved_share.id) self.assertEqual(retrieved_share.local_port, share.local_port) except AssertionError: print 'error on i:%s' % i errors.append(i) self.assertEqual([], errors) finally: dbhandler2.close()