示例#1
0
class SQLiteTest(TestCase):
    """Test the sqlite.py connection code.

    Establishes a connection to a new, empty in-memory SQLite DB before each
    test.
    """

    def setUp(self):
        self.db = SQLiteDB(":memory:")

    def test_create_insert_select(self):
        """Create a table, insert a row and read it again, the basic DB
        actions.
        """
        test_value = "hello"
        self.db.Manipulation("CREATE TABLE test (test VARCHAR)")()
        self.db.Manipulation("INSERT INTO test VALUES(?)")(test_value)
        select = self.db.Select("SELECT * FROM test")
        self.assertEqual(select()[0][0], test_value)

    def test_reopen(self):
        """ Try what happens if the connection is lost (do a close) and see
        that it is reopened again.
        """
        self.db.close()
        # This should not throw an exception!
        self.db.Manipulation("CREATE TABLE test (test VARCHAR)")()

    def test_show(self):
        """ Check the show test_query function.
        """
        self.assertEqual(self.db.show("test", [1, 2]), "test [1, 2]")

    def test_connect_error(self):
        """ Test that it is not possible to open a connection again, without
        closing it first.
        """
        # Do something to create the connection.
        self.db.Manipulation("CREATE TABLE test (test VARCHAR)")()
        # Now try to create the connection again, without closing it first.
        with self.assertRaises(RuntimeError):
            self.db._connect()  # pylint: disable=protected-access

    def test_close_error(self):
        """ Add a faulty close function and check that the DB instance can
        handle that.
        """
        self.db._connection = (  # pylint: disable=protected-access
            _CloseErrorConnection())
        self.db.close()  # should not raise an error!
示例#2
0
 def setUp(self):
     self.db = SQLiteDB(":memory:")
示例#3
0
 def setUp(self):
     self.db = SQLiteDB(":memory:")
示例#4
0
# -*- coding: utf-8 -*-
"""
DBQuery example., using SQLite.

Shows the use of Manipulation, Select and SelectOne.
"""
from random import randrange

from dbquery import SQLiteDB

if __name__ == '__main__':
    # Create a SQLite connection to an in-memory database.
    db = SQLiteDB(':memory:')

    # Create a Manipulation which can create a table.
    create_world_table = db.Manipulation('CREATE TABLE world (hello INTEGER)')
    # Execute the SQL.
    create_world_table()

    # Create a new Manipulation which can insert data.
    insert_into_world = db.Manipulation('INSERT INTO world VALUES (?)')
    # Insert some random values.
    for _ in range(3):
        row_count = insert_into_world(randrange(100))
        print('Inserted rows: ', row_count)

    # Create a Select which can read those values.
    get_hello = db.Select('SELECT hello FROM world')
    # Print values.
    for hello_value in get_hello():
        print(hello_value[0])
示例#5
0
class SQLiteTest(TestCase):
    """Test the sqlite.py connection code.

    Establishes a connection to a new, empty in-memory SQLite DB before each
    test.
    """
    def setUp(self):
        self.db = SQLiteDB(":memory:")

    def test_create_insert_select(self):
        """Create a table, insert a row and read it again, the basic DB
        actions.
        """
        test_value = "hello"
        self.db.Manipulation("CREATE TABLE test (test VARCHAR)")()
        self.db.Manipulation("INSERT INTO test VALUES(?)")(test_value)
        select = self.db.Select("SELECT * FROM test")
        self.assertEqual(select()[0][0], test_value)

    def test_reopen(self):
        """ Try what happens if the connection is lost (do a close) and see
        that it is reopened again.
        """
        self.db.close()
        # This should not throw an exception!
        self.db.Manipulation("CREATE TABLE test (test VARCHAR)")()

    def test_show(self):
        """ Check the show test_query function.
        """
        self.assertEqual(self.db.show("test", [1, 2]), "test [1, 2]")

    def test_connect_error(self):
        """ Test that it is not possible to open a connection again, without
        closing it first.
        """
        # Do something to create the connection.
        self.db.Manipulation("CREATE TABLE test (test VARCHAR)")()
        # Now try to create the connection again, without closing it first.
        with self.assertRaises(RuntimeError):
            self.db._connect()

    def test_close_error(self):
        """ Add a faulty close function and check that the DB instance can
        handle that.
        """
        self.db._connection = _CloseErrorConnection()
        self.db.close()  # should not raise an error!