def test_connection_commit_by_key(self): with ContextManager() as context: with connection(key=context.key) as db: # Setup tables self.drop_tables(db) self.create_tables(db, fill=True) with connection(key=context.key) as db: try: exists = db.check_table('doctest_t1') self.assertEqual(exists, True, 'Table must exist, but was not found.') finally: # Drop tables self.drop_tables(db) db = connection(key=context.key) exists = db.check_table('doctest_t1') self.assertEqual(exists, False, 'Table must don''t exist, but was found.') db2 = connection() exists = db2.check_table('doctest_t1') self.assertEqual(exists, False, 'Table must don''t exist, but was found.') db = connection() exists = db.check_table('doctest_t1') self.assertEqual(exists, False, 'Table must don''t exist, but was found.')
def test_connection_commit_by_key(self): with ContextManager() as context: with connection(key=context.key) as db: # Setup tables self.drop_tables(db) self.create_tables(db, fill=True) with connection(key=context.key) as db: try: exists = db.check_table('doctest_t1') self.assertEqual(exists, True, 'Table must exist, but was not found.') finally: # Drop tables self.drop_tables(db) db = connection(key=context.key) exists = db.check_table('doctest_t1') self.assertEqual(exists, False, 'Table must don' 't exist, but was found.') db2 = connection() exists = db2.check_table('doctest_t1') self.assertEqual(exists, False, 'Table must don' 't exist, but was found.') db = connection() exists = db.check_table('doctest_t1') self.assertEqual(exists, False, 'Table must don' 't exist, but was found.')
def test_threaded_connections(self): import threading class myThread(threading.Thread): def __init__(self, threadID, name, counter, key): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.key = key def run(self): print("Starting " + self.name) database_operations(self.key) print("Exiting " + self.name) def database_operations(key): # with connection(key=key) as db: with connection() as db: exists = db.check_table('doctest_t1') # self.assertEqual(exists, True, 'Table must exist, but was not found.') with ContextManager() as context: with connection(key=context.key) as db: # Setup tables self.drop_tables(db) self.create_tables(db, fill=True) threads = [] # Create new threads for i in range(500): newThread = myThread(i, "Thread-" + str(i), i, key=context.key) threads.append(newThread) # Start new Threads for t in threads: sleep(0.007) t.start() # Wait for all threads to complete for t in threads: t.join() # Drop tables with connection(key=context.key) as db: self.drop_tables(db) print "Exiting Main Thread \n" while True: sleep(0.006)
def db_command(i): with ContextManager() as context: print("ContextManager ID: " + str(i)) with connection() as db: print("Into of ContextManager ID: " + str(i)) # Setup tables db.execute("SELECT pg_sleep(1);") print("End od ContextManager ID: " + str(i))
def get_conn(db_config=None): global db_initialized if not db_initialized: if db_config: init_db_pool(db_config) else: raise DatabaseError("Database haven't! been initialized") return pypgwrap.connection()
def test_connection_auto_commit(self): import code import sys with connection() as db: if sys.argv.count('--interact'): db.log = sys.stdout code.interact(local=locals()) else: self.drop_tables(db) self.create_tables(db, fill=True) with connection() as db: try: exists = db.check_table('doctest_t1') self.assertEqual(exists, True, 'Table must exist, but was not found. Auto-commit fail.') finally: self.drop_tables(db)
def test_connection_auto_commit(self): import code import sys with connection() as db: if sys.argv.count('--interact'): db.log = sys.stdout code.interact(local=locals()) else: self.drop_tables(db) self.create_tables(db, fill=True) with connection() as db: try: exists = db.check_table('doctest_t1') self.assertEqual( exists, True, 'Table must exist, but was not found. Auto-commit fail.') finally: self.drop_tables(db)
def test_basic_functions(self): import code import doctest import sys db = connection() if sys.argv.count('--interact'): db.log = sys.stdout code.interact(local=locals()) else: try: # Setup tables self.drop_tables(db) self.create_tables(db, fill=True) # Run tests doctest.testmod(optionflags=doctest.ELLIPSIS) finally: # Drop tables self.drop_tables(db) self.assertEqual(True, True)
def database_operations(key): # with connection(key=key) as db: with connection() as db: exists = db.check_table('doctest_t1')