예제 #1
0
파일: counters.py 프로젝트: justinfay/dbkit
def set_counter(counter, value):
    """
    Set a counter.
    """
    execute(
        'REPLACE INTO counters (counter, value) VALUES (?, ?)',
        (counter, value))
예제 #2
0
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
예제 #3
0
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
예제 #4
0
파일: counters.py 프로젝트: justinfay/dbkit
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))
예제 #5
0
파일: counters.py 프로젝트: justinfay/dbkit
def delete_counter(counter):
    """
    Delete a counter.
    """
    execute(
        'DELETE FROM counters WHERE counter = ?',
        (counter,))
예제 #6
0
 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
예제 #7
0
 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)
예제 #8
0
 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)
예제 #9
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']
예제 #10
0
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']
예제 #11
0
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
예제 #12
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'])
예제 #13
0
파일: db.py 프로젝트: justinfay/pastetron
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()
예제 #14
0
파일: db.py 프로젝트: justinfay/pastetron
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()
예제 #15
0
파일: db.py 프로젝트: kgaughan/pastetron
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
예제 #16
0
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
예제 #17
0
    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)
예제 #18
0
파일: notary.py 프로젝트: justinfay/dbkit
def save_note(project_id, note):
    dbkit.execute("""
        INSERT INTO notes (project_id, note) VALUES (?, ?)
        """, (project_id, note))
    return dbkit.last_row_id()
예제 #19
0
파일: notary.py 프로젝트: justinfay/dbkit
def add_project(project):
    slug = slugify(project)
    dbkit.execute("""
        INSERT INTO projects (project, slug, overview) VALUES (?, ?, '')
        """, (project, slug))
    return (dbkit.last_row_id(), slug)
예제 #20
0
파일: pools.py 프로젝트: justinfay/dbkit
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,))
예제 #21
0
 def update_counter_and_fail(name, value):
     dbkit.execute(UPDATE_COUNTER, (value, name))
     raise dbkit.AbortTransaction()