Browse Source

Continue work on postgresql driver

oz123 10 years ago
parent
commit
13a7b1b6fc
4 changed files with 49 additions and 6 deletions
  1. 2 0
      pwman/__init__.py
  2. 7 6
      pwman/data/drivers/postgresql.py
  3. 2 0
      pwman/data/factory.py
  4. 38 0
      pwman/tests/test_postgresql.py

+ 2 - 0
pwman/__init__.py

@@ -132,6 +132,8 @@ def get_conf_options(args, OSX):
 
 
 def get_db_version(config, dbtype, args):
+    # This method is seriously biased towards SQLite.
+    # TODO: make this more Postgresql\Network Database friendly
     if os.path.exists(config.get_value("Database", "filename")):
         dbver = factory.check_db_version(dbtype, config.get_value("Database",
                                                                   "filename"))

+ 7 - 6
pwman/data/drivers/postgresql.py

@@ -23,7 +23,7 @@
 import psycopg2 as pg
 import cPickle
 import pwman.util.config as config
-from pwman.data.database import Database, DatabaseException
+from pwman.data.database import Database, DatabaseException, __DB_FORMAT__
 
 
 class PostgresqlDatabase(Database):
@@ -62,18 +62,19 @@ class PostgresqlDatabase(Database):
         con = pg.connect("dbname=pwman user=%s" % user)
         cur = con.cursor()
         try:
-            cur.execute("SELECT DBVERSION from DBVERSION")
+            cur.execute("SELECT VERSION from DBVERSION")
             version = cur.fetchone()
             return version
         except pg.ProgrammingError:
             con.rollback()
             raise DatabaseException("Something seems fishy with the DB")
 
-    def __init__(self):
-        """Initialise PostgresqlDatabase instance."""
-        Database.__init__(self)
+    def __init__(self, pgsqluri, dbformat=__DB_FORMAT__):
+        """
+        Initialise PostgresqlDatabase instance.
+        """
+        self._pgsqluri = pgsqluri
 
-        self._tagidcache = {}
 
         config.add_defaults({"Database": {"server": "localhost",
                                           "port": "5432",

+ 2 - 0
pwman/data/factory.py

@@ -42,6 +42,8 @@ def check_db_version(ftype, filename):
         except ValueError:
             return 0.3
     # TODO: implement version checks for other supported DBs.
+    if ftype == "Postgresql":
+        ver = sqlite.PostgresqlDatabase.check_db_version(filename)
 
 
 def create(dbtype, version=None, filename=None):

+ 38 - 0
pwman/tests/test_postgresql.py

@@ -0,0 +1,38 @@
+# ============================================================================
+# This file is part of Pwman3.
+#
+# Pwman3 is free software; you can redistribute iut and/or modify
+# it under the terms of the GNU General Public License, version 2
+# as published by the Free Software Foundation;
+#
+# Pwman3 is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Pwman3; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+# ============================================================================
+# Copyright (C) 2015 Oz Nahum Tiram <nahumoz@gmail.com>
+# ============================================================================
+
+import os
+import unittest
+from pwman.data.drivers.sqlite import PostgresqlDatabase
+from pwman.data.nodes import Node
+from pwman.util.crypto_engine import CryptoEngine
+from .test_crypto_engine import give_key, DummyCallback
+
+
+class TestPostGresql(unittest.TestCase):
+
+    @classmethod
+    def setUpClass(self):
+        self.db = SQLite('test.db')
+        self.db._open()
+
+    @classmethod
+    def tearDownClass(self):
+        # TODO: DROP Test DATABASE
+        pass