def test_connection_state(self): conn = test_db.get_conn() self.assertFalse(test_db.is_closed()) test_db.close() self.assertTrue(test_db.is_closed()) conn = test_db.get_conn() self.assertFalse(test_db.is_closed())
def test_context_multithreaded(self): conn = test_db.get_conn() evt = threading.Event() evt2 = threading.Event() def create(): with test_db.execution_context() as ctx: database = ctx.database self.assertEqual(database.execution_context_depth(), 1) evt2.set() evt.wait() self.assertNotEqual(conn, ctx.connection) User.create(username='******') create_t = threading.Thread(target=create) create_t.daemon = True create_t.start() evt2.wait() self.assertEqual(test_db.execution_context_depth(), 0) evt.set() create_t.join() self.assertEqual(test_db.execution_context_depth(), 0) self.assertEqual(User.select().count(), 1)
def test_context_ext(self): with test_db.execution_context(): with test_db.execution_context() as inner_ctx: with test_db.execution_context(): User.create(username="******") self.assertEqual(test_db.execution_context_depth(), 3) conn = test_db.get_conn() self.assertEqual(conn, inner_ctx.connection) self.assertTrue(User.select().where(User.username == "huey").exists()) self.assertEqual(test_db.execution_context_depth(), 0)
def test_context_ext(self): with test_db.execution_context(): with test_db.execution_context() as inner_ctx: with test_db.execution_context(): User.create(username='******') self.assertEqual(test_db.execution_context_depth(), 3) conn = test_db.get_conn() self.assertEqual(conn, inner_ctx.connection) self.assertTrue( User.select().where(User.username == 'huey').exists()) self.assertEqual(test_db.execution_context_depth(), 0)
def test_outer_loop_inner_commit(self): # By default we are in autocommit mode (isolation_level=None). self.assertEqual(test_db.get_conn().isolation_level, None) for username in ['u1', 'u2', 'u3']: User.create(username=username) for user in User.select(): Blog.create(user=user, title='b-%s' % user.username) # These statements are auto-committed. new_db = self.new_connection() count = new_db.execute_sql('select count(*) from blog;').fetchone() self.assertEqual(count[0], 3) self.assertEqual(Blog.select().count(), 3) blog_titles = [b.title for b in Blog.select().order_by(Blog.title)] self.assertEqual(blog_titles, ['b-u1', 'b-u2', 'b-u3']) self.assertEqual(Blog.delete().execute(), 3) # If we disable autocommit, we need to explicitly call begin(). test_db.set_autocommit(False) test_db.begin() for user in User.select(): Blog.create(user=user, title='b-%s' % user.username) # These statements have not been committed. new_db = self.new_connection() count = new_db.execute_sql('select count(*) from blog;').fetchone() self.assertEqual(count[0], 0) self.assertEqual(Blog.select().count(), 3) blog_titles = [b.title for b in Blog.select().order_by(Blog.title)] self.assertEqual(blog_titles, ['b-u1', 'b-u2', 'b-u3']) test_db.commit() count = new_db.execute_sql('select count(*) from blog;').fetchone() self.assertEqual(count[0], 3)
def reset_encoding(self, encoding): test_db.close() conn = test_db.get_conn() conn.set_client_encoding(encoding)