123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import sys
- import unittest
- import os
- import os.path
- from pwman.data import factory
- from pwman.data.database import DatabaseException
- from pwman.data.drivers.sqlite import SQLite
- from pwman.data.drivers.postgresql import PostgresqlDatabase
- from pwman.data.database import __DB_FORMAT__
- from .test_tools import (SetupTester)
- testdb = os.path.abspath(os.path.join(os.path.dirname(__file__),
- "test.pwman.db"))
- _saveconfig = False
- class TestFactory(unittest.TestCase):
- @classmethod
- def tearDownClass(cls):
- SetupTester().clean()
- def setUp(self):
- "test that the right db instance was created"
- self.dbtype = 'sqlite'
- self.db = factory.createdb('sqlite:///'+testdb, __DB_FORMAT__)
- self.tester = SetupTester(__DB_FORMAT__, testdb)
- self.tester.create()
- def test_factory_check_db_ver(self):
- self.assertEqual(factory.check_db_version('sqlite://'+testdb), 0.6)
- def test_factory_check_db_file(self):
- fn = os.path.join(os.path.dirname(__file__), 'baz.db')
- db = factory.createdb('sqlite:///'+os.path.abspath(fn), 0.3)
- db._open()
- self.assertEqual(factory.check_db_version('sqlite:///'+fn), 0.3)
- os.unlink(fn)
- def test_factory_create(self):
- fn = os.path.join(os.path.dirname(__file__), 'foo.db')
- db = factory.createdb('sqlite://'+os.path.abspath(fn), 0.6)
- db._open()
- self.assertTrue(os.path.exists(fn))
- db.close()
- os.unlink(fn)
- self.assertIsInstance(db, SQLite)
- self.assertRaises(DatabaseException, factory.createdb, *('UNKNOWN',
- 0.6))
- def test_factory_createdb(self):
- db = factory.createdb("sqlite:///test.db", 0.6)
- self.assertIsInstance(db, SQLite)
- del db
- db = factory.createdb("postgresql:///pwman", 0.6)
- self.assertIsInstance(db, PostgresqlDatabase)
- del db
- db = factory.createdb("sqlite:///test.db", 0.7)
- self.assertIsInstance(db, SQLite)
- del db
- db = factory.createdb("postgresql:///pwman", 0.7)
- self.assertIsInstance(db, PostgresqlDatabase)
- if __name__ == '__main__':
-
- sys.path.insert(0, os.getcwd())
-
-
-
- SetupTester().clean()
- unittest.main(verbosity=1, failfast=True)
|