Example #1
0
class TestipyDBTests(unittest.TestCase):
    """
    Tests relating to database functionality.
    """
    def setUp(self):
        self.db_name = 'test.db'
        create_db(self.db_name)
        self.dbh = DBHelper(self.db_name)

    def tearDown(self):
        self.dbh.close_con()
        delete_db(self.db_name)

    def test_open_con(self):
        self.dbh.open_con()
        if not self.dbh.con:
            raise Exception("Test failed, no open connection.")

    def test_close_con(self):
        self.dbh.open_con()
        self.dbh.close_con()
        try:
            self.dbh.con.cursor()
            self.dbh.c.execute('SELECT SQLITE_VERSION()')
            raise Exception('Connection open when expected to be closed.')
        except sql.Error as e:
            if e.__str__() == 'Cannot operate on a closed database.':
                pass
            else:
                print("Error: %s" % e.args[0])
                raise Exception('Unexpected error.')

    def test_cursor(self):
        self.dbh.open_con()
        try:
            self.dbh.cursor()
            self.dbh.c.execute('SELECT SQLITE_VERSION()')
            if '<sqlite3.Cursor object at' in self.dbh.c.__str__():
                pass
            else:
                raise Exception("'<sqlite3.Cursor object at' not found in " +
                                self.dbh.c.__str__())
        except sql.Error as e:
            print("Error: %s" % e.args[0])
            raise Exception('Unexpected error.')

    def test_db_version(self):
        version = self.dbh.db_version()
        match = re.search('SQLite Version: \d.\d.\d.\d', version)
        if match:
            pass  # test passes
        else:
            print(traceback.format_exc())
            raise Exception('No sqlite3 version regex match.')

    def test_db_call_commit(self):  # this is silly.
        commands = [
            "ATTACH 'test.db' as test", 'DROP TABLE IF EXISTS test.test_table',
            '''CREATE TABLE IF NOT EXISTS test.test_table (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                date text NULL,
                name text NULL,
                description text NULL,
                pickled_obj text NULL
                );'''
        ]
        try:
            self.dbh.open_con()
            self.dbh.cursor()
            for command in commands:
                self.dbh.c.execute(command)
            self.dbh.con.commit()
        except sql.Error as e:
            print("Error: %s" % e.args[0])
            print(traceback.format_exc())
            raise Exception("SQL ERROR. TEST FAILED.")
        finally:
            self.dbh.close_con()
Example #2
0
class TestipyDBTests(unittest.TestCase):
    """
    Tests relating to database functionality.
    """

    def setUp(self):
        self.db_name = 'test.db'
        create_db(self.db_name)
        self.dbh = DBHelper(self.db_name)

    def tearDown(self):
        self.dbh.close_con()
        delete_db(self.db_name)

    def test_open_con(self):
        self.dbh.open_con()
        if not self.dbh.con:
            raise Exception("Test failed, no open connection.")

    def test_close_con(self):
        self.dbh.open_con()
        self.dbh.close_con()
        try:
            self.dbh.con.cursor()
            self.dbh.c.execute('SELECT SQLITE_VERSION()')
            raise Exception('Connection open when expected to be closed.')
        except sql.Error as e:
            if e.__str__() == 'Cannot operate on a closed database.':
                pass
            else:
                print("Error: %s" % e.args[0])
                raise Exception('Unexpected error.')

    def test_cursor(self):
        self.dbh.open_con()
        try:
            self.dbh.cursor()
            self.dbh.c.execute('SELECT SQLITE_VERSION()')
            if '<sqlite3.Cursor object at' in self.dbh.c.__str__():
                pass
            else:
                raise Exception("'<sqlite3.Cursor object at' not found in " + self.dbh.c.__str__())
        except sql.Error as e:
            print("Error: %s" % e.args[0])
            raise Exception('Unexpected error.')

    def test_db_version(self):
        version = self.dbh.db_version()
        match = re.search('SQLite Version: \d.\d.\d.\d', version)
        if match:
            pass # test passes
        else:
            print(traceback.format_exc())
            raise Exception('No sqlite3 version regex match.')

    def test_db_call_commit(self): # this is silly.
        commands = [
            "ATTACH 'test.db' as test",
            'DROP TABLE IF EXISTS test.test_table',
            '''CREATE TABLE IF NOT EXISTS test.test_table (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                date text NULL,
                name text NULL,
                description text NULL,
                pickled_obj text NULL
                );'''
        ]
        try:
            self.dbh.open_con()
            self.dbh.cursor()
            for command in commands:
                self.dbh.c.execute(command)
            self.dbh.con.commit()
        except sql.Error as e:
            print("Error: %s" % e.args[0])
            print(traceback.format_exc())
            raise Exception("SQL ERROR. TEST FAILED.")
        finally:
            self.dbh.close_con()