소스 검색

Remove access to config.py from sqlite and factory

All the functionality is the same, but the access to config
parameters is done over a Config instance or explicit parameter
in methods
oz123 10 년 전
부모
커밋
457eeb53ed
4개의 변경된 파일38개의 추가작업 그리고 59개의 파일을 삭제
  1. 18 30
      pwman/data/drivers/sqlite.py
  2. 4 7
      pwman/data/factory.py
  3. 14 18
      pwman/tests/db_tests.py
  4. 2 4
      pwman/tests/test_tools.py

+ 18 - 30
pwman/data/drivers/sqlite.py

@@ -26,44 +26,32 @@ from pwman.data.database import __DB_FORMAT__
 from pwman.data.nodes import NewNode
 from pwman.util.crypto_engine import CryptoEngine
 import sqlite3 as sqlite
-import pwman.util.config as config
 import itertools
 
 
-def check_db_version():
-    """
-    check the data base version query the right table
-    """
-    filename = config.get_value('Database', 'filename')
-    con = sqlite.connect(filename)
-    cur = con.cursor()
-    cur.execute("PRAGMA TABLE_INFO(DBVERSION)")
-    row = cur.fetchone()
-    if not row:
-        return "0.3"  # pragma: no cover
-    try:
-        return row[-2]
-    except IndexError:  # pragma: no cover
-        raise DatabaseException("Something seems fishy with the DB")
-
-
 class SQLiteDatabaseNewForm(Database):
     """SQLite Database implementation"""
 
-    def __init__(self, filename=None, dbformat=__DB_FORMAT__):
+    @classmethod
+    def check_db_version(cls, fname):
+        """
+        check the data base version.
+        """
+        con = sqlite.connect(fname)
+        cur = con.cursor()
+        cur.execute("PRAGMA TABLE_INFO(DBVERSION)")
+        row = cur.fetchone()
+        if not row:
+            return "0.3"  # pragma: no cover
+        try:
+            return row[-2]
+        except IndexError:  # pragma: no cover
+            raise DatabaseException("Something seems fishy with the DB")
+
+    def __init__(self, filename, dbformat=__DB_FORMAT__):
         """Initialise SQLitePwmanDatabase instance."""
-        #Database.__init__(self)
         super(SQLiteDatabaseNewForm, self).__init__()
-        # error handling is implemented in config.get_value
-        # so there's no need to try... except here...
-        if not filename:
-            self._filename = config.get_value('Database', 'filename')
-        else:
-            self._filename = filename
-
-        if not self._filename:
-            raise DatabaseException(("SQLite: missing config parameter:"
-                                    " filename"))
+        self._filename = filename
         self.dbformat = dbformat
 
     def _open(self):

+ 4 - 7
pwman/data/factory.py

@@ -43,9 +43,9 @@ class FactoryException(Exception):
         return self.message
 
 
-def check_db_version(type):
-    if type == "SQLite":
-        ver = sqlite.check_db_version()
+def check_db_version(ftype, filename):
+    if ftype == "SQLite":
+        ver = sqlite.SQLiteDatabaseNewForm.check_db_version(filename)
         try:
             return float(ver.strip("\'"))
         except ValueError:
@@ -61,10 +61,7 @@ def create(dbtype, version=None, filename=None):
     """
     if dbtype == "SQLite":
         from pwman.data.drivers import sqlite
-        if filename:
-            db = sqlite.SQLiteDatabaseNewForm(filename)
-        elif version >= 0.4:
-            db = sqlite.SQLiteDatabaseNewForm()
+        db = sqlite.SQLiteDatabaseNewForm(filename)
     elif dbtype == "Postgresql":  # pragma: no cover
         try:
             from pwman.data.drivers import postgresql

+ 14 - 18
pwman/tests/db_tests.py

@@ -76,6 +76,8 @@ PwmanCliNew, OSX = get_ui_platform(sys.platform)
 from .test_tools import (SetupTester, DummyCallback2,
                          DummyCallback3, DummyCallback4)
 
+testdb = os.path.join(os.path.dirname(__file__), "test.pwman.db")
+
 
 class DBTests(unittest.TestCase):
 
@@ -85,8 +87,8 @@ class DBTests(unittest.TestCase):
         "test that the right db instance was created"
         dbver = __DB_FORMAT__
         self.dbtype = config.get_value("Database", "type")
-        self.db = factory.create(self.dbtype, dbver)
-        self.tester = SetupTester(dbver)
+        self.db = factory.create(self.dbtype, dbver, testdb)
+        self.tester = SetupTester(dbver, testdb)
         self.tester.create()
 
     def test_1_db_created(self):
@@ -181,6 +183,7 @@ class TestDBFalseConfig(unittest.TestCase):
         self.fname1 = default_config['Database'].pop('filename')
         self.fname = config._conf['Database'].pop('filename')
 
+    @unittest.skip("Legacy test. Database and file should always be given")
     def test_db_missing_conf_parameter(self):
         self.assertRaises(DatabaseException, factory.create,
                           'SQLite', __DB_FORMAT__)
@@ -210,8 +213,8 @@ class CLITests(unittest.TestCase):
     def setUp(self):
         "test that the right db instance was created"
         self.dbtype = config.get_value("Database", "type")
-        self.db = factory.create(self.dbtype, __DB_FORMAT__)
-        self.tester = SetupTester(__DB_FORMAT__)
+        self.db = factory.create(self.dbtype, __DB_FORMAT__, testdb)
+        self.tester = SetupTester(__DB_FORMAT__, testdb)
         self.tester.create()
 
     def test_input(self):
@@ -335,7 +338,7 @@ class CLITests(unittest.TestCase):
 
             def __init__(self):
                 self.idx = -1
-                self.ans = ['4', 'some fucking notes','X']
+                self.ans = ['4', 'some fucking notes', 'X']
 
             def __call__(self, msg):
                 self.idx += 1
@@ -370,22 +373,15 @@ class CLITests(unittest.TestCase):
         self.assertTrue(self.tester.cli.do_exit(''))
 
 
-class FakeSqlite(object):
-
-    def check_db_version(self):
-        return ""
-
-
 class FactoryTest(unittest.TestCase):
 
     def test_factory_check_db_ver(self):
-        self.assertEquals(factory.check_db_version('SQLite'), 0.5)
+        self.assertEquals(factory.check_db_version('SQLite', testdb), 0.5)
 
     def test_factory_check_db_file(self):
-        orig_sqlite = getattr(factory, 'sqlite')
-        factory.sqlite = FakeSqlite()
-        self.assertEquals(factory.check_db_version('SQLite'), 0.3)
-        factory.sqlite = orig_sqlite
+        factory.create('SQLite', version='0.3', filename='baz.db')
+        self.assertEquals(factory.check_db_version('SQLite', 'baz.db'), 0.3)
+        os.unlink('baz.db')
 
     def test_factory_create(self):
         db = factory.create('SQLite', filename='foo.db')
@@ -403,8 +399,8 @@ class ConfigTest(unittest.TestCase):
         "test that the right db instance was created"
         dbver = 0.4
         self.dbtype = config.get_value("Database", "type")
-        self.db = factory.create(self.dbtype, dbver)
-        self.tester = SetupTester(dbver)
+        self.db = factory.create(self.dbtype, dbver, testdb)
+        self.tester = SetupTester(dbver, testdb)
         self.tester.create()
         self.orig_config = config._conf.copy()
         self.orig_config['Encryption'] = {'algorithm': 'AES'}

+ 2 - 4
pwman/tests/test_tools.py

@@ -78,8 +78,6 @@ class SetupTester(object):
 
     def create(self):
         dbtype = config.get_value("Database", "type")
-        if self.filename:
-            db = factory.create(dbtype, self.dbver, self.filename)
-        else:
-            db = factory.create(dbtype, self.dbver)
+        db = factory.create(dbtype, self.dbver, self.filename)
+
         self.cli = PwmanCliNew(db, self.xselpath, DummyCallback)