Last change
on this file since 10351 was
10349,
checked in by uli, 12 years ago
|
Use fetchall instead manually creating lists.
|
File size:
1.2 KB
|
Rev | Line | |
---|
[10344] | 1 | # tests for db.py module |
---|
| 2 | import os |
---|
| 3 | import shutil |
---|
| 4 | import sqlite3 |
---|
| 5 | import tempfile |
---|
| 6 | import unittest |
---|
| 7 | from waeup.cas.db import create_db |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | class DBTests(unittest.TestCase): |
---|
| 11 | def setUp(self): |
---|
| 12 | self.workdir = tempfile.mkdtemp() |
---|
| 13 | |
---|
| 14 | def tearDown(self): |
---|
| 15 | shutil.rmtree(self.workdir) |
---|
| 16 | |
---|
| 17 | def test_create_db(self): |
---|
| 18 | # we can create a database |
---|
| 19 | db_path = os.path.join(self.workdir, 'sample-cas.db') |
---|
| 20 | create_db(db_path) |
---|
| 21 | assert os.path.isfile(db_path) |
---|
| 22 | conn = sqlite3.connect(db_path) |
---|
| 23 | assert conn is not None |
---|
| 24 | conn.close() |
---|
| 25 | |
---|
| 26 | def test_create_db_exists(self): |
---|
| 27 | # an already existing db will be left untouched |
---|
| 28 | db_path = os.path.join(self.workdir, 'sample-cas.db') |
---|
| 29 | create_db(db_path) |
---|
| 30 | conn = sqlite3.connect(db_path) |
---|
| 31 | cursor = conn.cursor() |
---|
| 32 | cursor.execute('''CREATE TABLE mytest (name text, forname text)''') |
---|
| 33 | cursor.execute('''INSERT INTO mytest VALUES ("Foo", "Bar")''') |
---|
| 34 | conn.commit() |
---|
| 35 | conn.close() |
---|
| 36 | |
---|
| 37 | create_db(db_path) |
---|
| 38 | conn = sqlite3.connect(db_path) |
---|
| 39 | cursor = conn.cursor() |
---|
[10349] | 40 | cursor.execute('''SELECT * FROM mytest''') |
---|
| 41 | assert cursor.fetchall() == [('Foo', 'Bar')] |
---|
Note: See
TracBrowser for help on using the repository browser.