提交 3f70dafb 编写于 作者: T Torsten Werner

implement first test with database access

* Implement a new test class: DBDakTestCase in db_test.py.
* First test: dbtest_fingerprint.py is prefixed with dbtest_ instead of
  test_ because we do not have a test database on franck (yet).
Signed-off-by: NTorsten Werner <twerner@debian.org>
上级 650daaa7
from base_test import DakTestCase, fixture
from daklib.config import Config
from daklib.dbconn import DBConn
from sqlalchemy import create_engine
from sqlalchemy.exc import SADeprecationWarning
import pickle
import warnings
# suppress some deprecation warnings in squeeze related to sqlalchemy
warnings.filterwarnings('ignore', \
"The SQLAlchemy PostgreSQL dialect has been renamed from 'postgres' to 'postgresql'.*", \
SADeprecationWarning)
class DBDakTestCase(DakTestCase):
def setUp(self):
cnf = Config()
if cnf["DB::Host"]:
# TCP/IP
connstr = "postgres://%s" % cnf["DB::Host"]
if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
connstr += ":%s" % cnf["DB::Port"]
connstr += "/%s" % cnf["DB::Name"]
else:
# Unix Socket
connstr = "postgres:///%s" % cnf["DB::Name"]
if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
connstr += "?port=%s" % cnf["DB::Port"]
pickle_file = open(fixture('db-metadata.pkl'), 'r')
self.metadata = pickle.load(pickle_file)
self.metadata.ddl_listeners = pickle.load(pickle_file)
pickle_file.close()
self.metadata.bind = create_engine(connstr)
self.metadata.create_all()
self.session = DBConn().session()
def tearDown(self):
#pass
self.session.close()
#self.metadata.drop_all()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import unittest
def suite():
suite = unittest.TestSuite()
for _, _, files in os.walk('.'):
for name in filter(is_test, files):
tests = unittest.defaultTestLoader.loadTestsFromName(name[:-3])
suite.addTests(tests)
return suite
def is_test(filename):
return filename.startswith('dbtest_') and filename.endswith('.py')
if __name__ == "__main__":
unittest.main(defaultTest="suite")
#!/usr/bin/env python
from db_test import DBDakTestCase
from daklib.dbconn import Fingerprint
import unittest
class FingerprintTestCase(DBDakTestCase):
def test_mini(self):
fingerprint = Fingerprint()
fingerprint.fingerprint = 'deadbeefdeadbeef'
self.session.add(fingerprint)
self.session.commit
fingerprint = self.session.query(Fingerprint).one()
self.assertEqual('deadbeefdeadbeef', fingerprint.fingerprint)
if __name__ == '__main__':
unittest.main()
......@@ -22,3 +22,11 @@ Dir
{
Root "tests/fixtures/ftp/";
};
DB
{
Name "test_projectb";
Host "";
Port -1;
};
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册