Prechádzať zdrojové kódy

Add testing and new SQLite engine

oz123 10 rokov pred
rodič
commit
ce4fa7ca28
2 zmenil súbory, kde vykonal 63 pridanie a 6 odobranie
  1. 26 6
      pwman/data/drivers/sqlite.py
  2. 37 0
      pwman/tests/test_sqlite.py

+ 26 - 6
pwman/data/drivers/sqlite.py

@@ -16,7 +16,6 @@
 #============================================================================
 # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
 #============================================================================
-#============================================================================
 # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
 #============================================================================
 
@@ -358,6 +357,16 @@ class SQLiteDatabaseNewForm(Database):
 
 class SQLite(SQLiteDatabaseNewForm):
 
+    def __init__(self, filename, dbformat=0.6):
+        """Initialise SQLitePwmanDatabase instance."""
+        self._filename = filename
+        self.dbformat = dbformat
+
+    def _open(self):
+        self._con = sqlite.connect(self._filename)
+        self._cur = self._con.cursor()
+        self._create_tables()
+
     def _create_tables(self):
         self._cur.execute("CREATE TABLE NODE (ID INTEGER PRIMARY KEY "
                           "AUTOINCREMENT, "
@@ -373,15 +382,26 @@ class SQLite(SQLiteDatabaseNewForm):
         self._cur.execute("CREATE TABLE LOOKUP ("
                           "nodeid INTEGER NOT NULL, "
                           "tagid INTEGER NOT NULL, "
-                          "FOREIGN KEY nodeid REFERENCES NODE(ID),"
-                          "FOREIGN KEY tagid REFERENCES TAG(ID))")
+                          "FOREIGN KEY(nodeid) REFERENCES NODE(ID),"
+                          "FOREIGN KEY(tagid) REFERENCES TAG(ID))")
+
+        self._cur.execute("CREATE TABLE CRYPTO"
+                          "(SALT TEXT,"
+                          " DIGEST TEXT)")
 
-        self._cur.execute("CREATE TABLE KEY"
-                          "(THEKEY TEXT NOT NULL DEFAULT '')")
-        self._cur.execute("INSERT INTO KEY VALUES('')")
         # create a table to hold DB version info
         self._cur.execute("CREATE TABLE DBVERSION"
                           "(DBVERSION TEXT NOT NULL DEFAULT '%s')" %
                           self.dbformat)
         self._cur.execute("INSERT INTO DBVERSION VALUES('%s')" %
                           self.dbformat)
+        try:
+            self._con.commit()
+        except DatabaseException as e:  # pragma: no cover
+            self._con.rollback()
+            raise e
+
+    def fetch_crypto_info(self):
+        self._cur.execute("SELECT * FROM CRYPTO")
+        keyrow = self._cur.fetchone()
+        return keyrow[0]

+ 37 - 0
pwman/tests/test_sqlite.py

@@ -0,0 +1,37 @@
+#============================================================================
+# 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) 2012 Oz Nahum Tiram <nahumoz@gmail.com>
+#============================================================================
+import os
+import unittest
+from pwman.data.drivers.sqlite import SQLite
+
+
+class TestConfig(unittest.TestCase):
+    def setUp(self):
+        self.db = SQLite('test.db')
+
+    def test_create_tables(self):
+        # the method _open calls _create_tables
+        self.db._open()
+
+
+if __name__ == '__main__':
+    try:
+        unittest.main(verbosity=2)
+    except SystemExit:
+        os.remove('test.db')