def set_counter(counter, value): """ Set a counter. """ execute( 'REPLACE INTO counters (counter, value) VALUES (?, ?)', (counter, value))
def test_bad_drop_table(): with setup() as ctx: try: dbkit.execute("DROP TABLE kownturs") assert False, "Should have triggered an exception." except ctx.OperationalError: pass
def setup(): """Creates a context fit for testing.""" with dbkit.connect(sqlite3, ':memory:') as ctx: dbkit.execute(SCHEMA) with dbkit.transaction(): dbkit.execute(TEST_DATA) return ctx
def increment_counter(counter, by): """ Modify the value of a counter by a certain amount. """ execute( 'UPDATE counters SET value = value + ? WHERE counter = ?', (by, counter))
def delete_counter(counter): """ Delete a counter. """ execute( 'DELETE FROM counters WHERE counter = ?', (counter,))
def test_bad_drop_table(self): with self.ctx: try: dbkit.execute("DROP TABLE kownturs") self.fail("Should have triggered an exception.") except self.ctx.OperationalError: pass
def setUp(self): """Creates a context fit for testing.""" self.ctx = dbkit.connect(sqlite3, ':memory:') with self.ctx: dbkit.execute(SCHEMA) with dbkit.transaction(): dbkit.execute(TEST_DATA) self.assertEqual(self.ctx.last_row_count, 1)
def test_bad_query(self): with self.pool.connect() as ctx: try: dbkit.execute("some query") self.fail("Should've got an OperationalError") except ctx.OperationalError: # An operational error will close the connection it occurs on. self.assertEqual(self.pool._allocated, 0)
def test_create_table(): with dbkit.connect(sqlite3, ':memory:'): result = dbkit.query_column(LIST_TABLES) assert isinstance(result, types.GeneratorType) assert len(list(result)) == 0 dbkit.execute(SCHEMA) result = dbkit.query_column(LIST_TABLES) assert isinstance(result, types.GeneratorType) assert list(result) == [u'counters']
def test_create_table(): with dbkit.connect(sqlite3, ':memory:'): result = dbkit.query_column(LIST_TABLES) assert hasattr(result, '__iter__') assert len(list(result)) == 0 dbkit.execute(SCHEMA) result = dbkit.query_column(LIST_TABLES) assert hasattr(result, '__iter__') assert list(result) == [u'counters']
def test_bad_query(): with POOL.connect() as ctx: try: assert POOL._allocated == 1 dbkit.execute("some query") assert False, "Should've got an OperationalError" except ctx.OperationalError: # An operational error will close the connection it occurs on. assert POOL._allocated == 0
def test_create_table(self): with dbkit.connect(sqlite3, ':memory:'): result = dbkit.query_column(LIST_TABLES) self.assertTrue(hasattr(result, '__iter__')) self.assertEqual(len(list(result)), 0) dbkit.execute(SCHEMA) result = dbkit.query_column(LIST_TABLES) self.assertTrue(hasattr(result, '__iter__')) self.assertEqual(list(result), [u'counters'])
def add_paste(poster, title, body, fmt): """ Create a new paste. """ dbkit.execute(""" INSERT INTO pastes ( poster, title, body, syntax ) VALUES ( ?, ?, ?, ? ) """, (poster, title, body, fmt)) return dbkit.last_row_id()
def add_comment(paste_id, poster, body): """ Attach a comment to a paste. """ dbkit.execute(""" INSERT INTO comments ( paste_id, poster, body ) VALUES ( ?, ?, ? ) """, (paste_id, poster, body)) return dbkit.last_row_id()
def add_paste(poster, title, chunks): """ Create a new paste. """ dbkit.execute(""" INSERT INTO pastes (poster, title) VALUES (?, ?) """, (poster, title)) paste_id = dbkit.last_row_id() for body, syntax in chunks: dbkit.execute(""" INSERT INTO chunks (paste_id, body, syntax) VALUES (?, ?, ?) """, (paste_id, body, syntax)) return paste_id
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 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 save_note(project_id, note): dbkit.execute(""" INSERT INTO notes (project_id, note) VALUES (?, ?) """, (project_id, note)) return dbkit.last_row_id()
def add_project(project): slug = slugify(project) dbkit.execute(""" INSERT INTO projects (project, slug, overview) VALUES (?, ?, '') """, (project, slug)) return (dbkit.last_row_id(), slug)
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 update_counter_and_fail(name, value): dbkit.execute(UPDATE_COUNTER, (value, name)) raise dbkit.AbortTransaction()