def test_transaction(): with dbkit.connect(sqlite3, ':memory:'): dbkit.execute(SCHEMA) # First, make sure the normal case behaves correctly. assert dbkit.context()._depth == 0 with dbkit.transaction(): assert dbkit.context()._depth == 1 dbkit.execute(TEST_DATA) assert dbkit.context()._depth == 0 assert dbkit.query_value(GET_COUNTER, ('foo',)) == 42 assert dbkit.query_value(GET_COUNTER, ('bar',)) is None # Now, ensure transactions are rolled back in case of exceptions. exception_caught = False try: with dbkit.transaction(): dbkit.execute(UPDATE_COUNTER, (13, 'foo')) raise dbkit.AbortTransaction() assert False, "Should've raised an exception." except dbkit.AbortTransaction: exception_caught = True assert exception_caught value = dbkit.query_value(GET_COUNTER, ('foo',)) assert value == 42
def get_counter(counter): """ Get the value of a counter. """ print query_value( 'SELECT value FROM counters WHERE counter = ?', (counter,), default=0)
def test_unpooled_disconnect(self): # Test rollback of connection. try: self.assertEqual(self.ctx.mdr.depth, 0) with self.ctx: try: with dbkit.transaction(): self.assertEqual(self.ctx.mdr.depth, 1) self.assertTrue(self.ctx.mdr.conn is not None) self.assertEqual( dbkit.query_value(GET_COUNTER, ('foo',)), 42) raise self.ctx.OperationalError("Simulating disconnect") except self.ctx.OperationalError: self.assertEqual(self.ctx.mdr.depth, 0) self.assertTrue(self.ctx.mdr.conn is None) raise self.fail("Should've raised OperationalError") except self.ctx.OperationalError as exc: self.assertEqual(self.ctx.mdr.depth, 0) self.assertTrue(self.ctx.mdr.conn is None) self.assertEqual(str(exc), "Simulating disconnect") # Test reconnect. As we're running this all against an in-memory DB, # everything in it will have been throttled, thus the only query we can # do is query the list of tables, which will be empty. with self.ctx: self.assertEqual(len(list(dbkit.query_column(LIST_TABLES))), 0) self.assertTrue(self.ctx.mdr.conn is not None)
def get_page_count(): """ """ n_pastes = dbkit.query_value("SELECT COUNT(*) FROM pastes") if n_pastes == 0: return 0 return (n_pastes / constants.PASTES_PER_PAGE) + 1
def get_page_count(): """ Get number of pages needed to list all pastes. """ return utils.to_page_count( dbkit.query_value(""" SELECT COUNT(*) FROM pastes """), utils.get_page_length())
def test_transaction_decorator(self): @dbkit.transactional def update_counter_and_fail(name, value): dbkit.execute(UPDATE_COUNTER, (value, name)) raise dbkit.AbortTransaction() with self.ctx: exception_caught = False try: update_counter_and_fail('foo', 13) except dbkit.AbortTransaction: exception_caught = True self.assertTrue(exception_caught) self.assertEqual(dbkit.query_value(GET_COUNTER, ('foo',)), 42)
def test_transaction(self): with dbkit.connect(sqlite3, ':memory:'): dbkit.execute(SCHEMA) # First, make sure the normal case behaves correctly. self.assertEqual(dbkit.context()._depth, 0) with dbkit.transaction(): self.assertEqual(dbkit.context()._depth, 1) dbkit.execute(TEST_DATA) self.assertEqual(dbkit.context()._depth, 0) self.assertEqual(dbkit.query_value(GET_COUNTER, ('foo',)), 42) self.assertTrue(dbkit.query_value(GET_COUNTER, ('bar',)) is None) # Now, ensure transactions are rolled back in case of exceptions. exception_caught = False try: with dbkit.transaction(): dbkit.execute(UPDATE_COUNTER, (13, 'foo')) raise dbkit.AbortTransaction() self.fail("Should've raised an exception.") except dbkit.AbortTransaction: exception_caught = True self.assertTrue(exception_caught) self.assertEqual(dbkit.query_value(GET_COUNTER, ('foo',)), 42)
def test_transaction_decorator(): @dbkit.transactional def update_counter_and_fail(name, value): dbkit.execute(UPDATE_COUNTER, (value, name)) raise dbkit.AbortTransaction() with setup(): exception_caught = False try: update_counter_and_fail('foo', 13) except dbkit.AbortTransaction: exception_caught = True assert exception_caught value = dbkit.query_value(GET_COUNTER, ('foo',)) assert value == 42
def test_unpooled_disconnect(): ctx = setup() # Test rollback of connection. try: with ctx: try: with dbkit.transaction(): assert ctx._mdr.depth == 1 assert ctx._mdr.conn is not None assert dbkit.query_value(GET_COUNTER, ('foo',)) == 42 raise ctx.OperationalError("Simulating disconnect") except: assert ctx._mdr.depth == 0 assert ctx._mdr.conn is None raise assert False, "Should've raised OperationalError" except ctx.OperationalError, exc: assert ctx._mdr.depth == 0 assert ctx._mdr.conn is None assert exc.message == "Simulating disconnect"
def save_name(name): if query_value("SELECT n FROM greeted WHERE name = %s", (name,), 0) == 0: execute("INSERT INTO greeted (name, n) VALUES (%s, 1)", (name,)) else: execute("UPDATE greeted SET n = n + 1 WHERE name = %s", (name,))
def get_digest_ha1(self, username): return dbkit.query_value(""" SELECT password FROM users WHERE username = ? """, (username,))