# tests for db.py module import os import shutil import sqlite3 import tempfile import unittest from waeup.cas.db import create_db class DBTests(unittest.TestCase): def setUp(self): self.workdir = tempfile.mkdtemp() def tearDown(self): shutil.rmtree(self.workdir) def test_create_db(self): # we can create a database db_path = os.path.join(self.workdir, 'sample-cas.db') create_db(db_path) assert os.path.isfile(db_path) conn = sqlite3.connect(db_path) assert conn is not None conn.close() def test_create_db_exists(self): # an already existing db will be left untouched db_path = os.path.join(self.workdir, 'sample-cas.db') create_db(db_path) conn = sqlite3.connect(db_path) cursor = conn.cursor() cursor.execute('''CREATE TABLE mytest (name text, forname text)''') cursor.execute('''INSERT INTO mytest VALUES ("Foo", "Bar")''') conn.commit() conn.close() create_db(db_path) conn = sqlite3.connect(db_path) cursor = conn.cursor() cursor.execute('''SELECT * FROM mytest''') assert cursor.fetchall() == [('Foo', 'Bar')]