Procházet zdrojové kódy

Add testing for mysql

oz123 před 10 roky
rodič
revize
7c1702c52c
2 změnil soubory, kde provedl 58 přidání a 5 odebrání
  1. 9 5
      pwman/data/drivers/mysql.py
  2. 49 0
      pwman/tests/test_mysql.py

+ 9 - 5
pwman/data/drivers/mysql.py

@@ -23,9 +23,8 @@
 
 """MySQL Database implementation."""
 from __future__ import print_function
-from pwman.data.database import Database
+from pwman.data.database import Database, __DB_FORMAT__
 import MySQLdb as mysql
-import pwman.util.config as config
 
 
 class MySQLDatabase(Database):
@@ -38,10 +37,10 @@ class MySQLDatabase(Database):
         if ':' in host:
             host, port = host.split(':')
             port = int(port)
+        con = mysql.connect(host=host, port=port, user=user, passwd=passwd,
+                            db=dburi.path.lstrip('/'))
+        cur = con.cursor()
         try:
-            con = mysql.connect(host=host, port=port, user=user, passwd=passwd,
-                                db=dburi.path.lstrip('/'))
-            cur = con.cursor()
             cur.execute("SELECT VERSION FROM DBVERSION")
             version = cur.fetchone()
             cur.close()
@@ -49,3 +48,8 @@ class MySQLDatabase(Database):
             return version[-1]
         except mysql.ProgrammingError:
             con.rollback()
+
+    def __init__(self, mysqluri, dbformat=__DB_FORMAT__):
+        self._mysqluri = mysqluri
+        self.dbversion = dbformat
+

+ 49 - 0
pwman/tests/test_mysql.py

@@ -0,0 +1,49 @@
+# ============================================================================
+# 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 unittest
+import sys
+from .test_crypto_engine import give_key, DummyCallback
+if sys.version_info.major > 2:  # pragma: no cover
+    from urllib.parse import urlparse
+else:  # pragma: no cover
+    from urlparse import urlparse
+import psycopg2 as pg
+from pwman.data.drivers.mysql import MySQLDatabase
+from pwman.util.crypto_engine import CryptoEngine
+
+
+class TestPostGresql(unittest.TestCase):
+
+    @classmethod
+    def setUpClass(self):
+        u = "postgresql://tester:123456@localhost/pwman"
+        u = urlparse(u)
+        # password required, for all hosts
+        # u = "postgresql://<user>:<pass>@localhost/pwman"
+        self.db = PostgresqlDatabase(u)
+        self.db._open()
+
+    @classmethod
+    def tearDownClass(self):
+        self.db._cur.execute("DROP TABLE LOOKUP")
+        self.db._cur.execute("DROP TABLE TAG")
+        self.db._cur.execute("DROP TABLE NODE")
+        self.db._cur.execute("DROP TABLE DBVERSION")
+        self.db._cur.execute("DROP TABLE CRYPTO")
+        self.db._con.commit()