Browse Source

Replace create with createdb

oz123 10 years ago
parent
commit
39e1def31c

+ 29 - 32
pwman/data/factory.py

@@ -57,34 +57,34 @@ def check_db_version(dburi):
         ver = sqlite.PostgresqlDatabase.check_db_version(dburi)
 
 
-def create(dbtype, version=None, filename=None):
-    """
-    create(params) -> Database
-    Create a Database instance.
-    'type' can only be 'SQLite' at the moment
-    """
-    if dbtype == "sqlite":
-        from pwman.data.drivers import sqlite
-        if str(version) == '0.6':
-            db = sqlite.SQLite(filename)
-        else:
-            db = sqlite.SQLite(filename, dbformat=version)
-
-    elif dbtype == "postgresql":  # pragma: no cover
-        try:
-            from pwman.data.drivers import postgresql
-            db = postgresql.PostgresqlDatabase()
-        except ImportError:
-            raise DatabaseException("python-psycopg2 not installed")
-    elif dbtype == "mysql":  # pragma: no cover
-        try:
-            from pwman.data.drivers import mysql
-            db = mysql.MySQLDatabase()
-        except ImportError:
-            raise DatabaseException("python-mysqldb not installed")
-    else:
-        raise DatabaseException("Unknown database type specified")
-    return db
+#def create(dbtype, version=None, filename=None):
+#    """
+#    create(params) -> Database
+#    Create a Database instance.
+#    'type' can only be 'SQLite' at the moment
+#    """
+#    if dbtype == "sqlite":
+#        from pwman.data.drivers import sqlite
+#        if str(version) == '0.6':
+#            db = sqlite.SQLite(filename)
+#        else:
+#            db = sqlite.SQLite(filename, dbformat=version)
+#
+#    elif dbtype == "postgresql":  # pragma: no cover
+#        try:
+#            from pwman.data.drivers import postgresql
+#            db = postgresql.PostgresqlDatabase()
+#        except ImportError:
+#            raise DatabaseException("python-psycopg2 not installed")
+#    elif dbtype == "mysql":  # pragma: no cover
+#        try:
+#            from pwman.data.drivers import mysql
+#            db = mysql.MySQLDatabase()
+#        except ImportError:
+#            raise DatabaseException("python-mysqldb not installed")
+#    else:
+#        raise DatabaseException("Unknown database type specified")
+#    return db
 
 
 def createdb(dburi, version):
@@ -94,10 +94,7 @@ def createdb(dburi, version):
 
     if dbtype == "sqlite":
         from pwman.data.drivers import sqlite
-        if str(version) == '0.6':
-            db = sqlite.SQLite(filename)
-        else:
-            db = sqlite.SQLite(filename, dbformat=version)
+        db = sqlite.SQLite(filename, dbformat=version)
 
     elif dbtype == "postgresql":
         try:

+ 3 - 3
pwman/tests/test_base_ui.py

@@ -32,7 +32,8 @@ from pwman.data import factory
 from pwman.data.nodes import Node
 from pwman.ui import get_ui_platform
 
-testdb = os.path.join(os.path.dirname(__file__), "test-baseui.pwman.db")
+testdb = os.path.abspath(os.path.join(os.path.dirname(__file__),
+                                      "test-baseui.pwman.db"))
 
 
 class dummy_stdin(object):
@@ -61,8 +62,7 @@ class TestBaseUI(unittest.TestCase):
     def setUp(self):
         "test that the right db instance was created"
         dbver = __DB_FORMAT__
-        self.dbtype = 'sqlite'
-        self.db = factory.create(self.dbtype, dbver, testdb)
+        self.db = factory.createdb('sqlite://' + testdb, dbver)
         self.tester = SetupTester(dbver, testdb)
         self.tester.create()
 

+ 7 - 6
pwman/tests/test_factory.py

@@ -29,7 +29,8 @@ from pwman.data.database import __DB_FORMAT__
 from .test_tools import (SetupTester)
 
 PwmanCliNew, OSX = get_ui_platform(sys.platform)
-testdb = os.path.join(os.path.dirname(__file__), "test.pwman.db")
+testdb = os.path.abspath(os.path.join(os.path.dirname(__file__),
+                                      "test.pwman.db"))
 _saveconfig = False
 
 
@@ -42,7 +43,7 @@ class TestFactory(unittest.TestCase):
     def setUp(self):
         "test that the right db instance was created"
         self.dbtype = 'sqlite'
-        self.db = factory.create(self.dbtype, __DB_FORMAT__, testdb)
+        self.db = factory.createdb('sqlite:///'+testdb, __DB_FORMAT__)
         self.tester = SetupTester(__DB_FORMAT__, testdb)
         self.tester.create()
 
@@ -51,20 +52,20 @@ class TestFactory(unittest.TestCase):
 
     def test_factory_check_db_file(self):
         fn = os.path.join(os.path.dirname(__file__), 'baz.db')
-        db = factory.create('sqlite', filename=fn)
+        db = factory.createdb('sqlite:///'+os.path.abspath(fn), 0.3)
         db._open()
-        self.assertEqual(factory.check_db_version('sqlite://'+fn), 0.3)
+        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.create('sqlite', filename=fn)
+        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.create, 'UNKNOWN')
+        self.assertRaises(DatabaseException, factory.createdb, *('UNKNOWN',0.6))
 
     def test_factory_createdb(self):
         db = factory.createdb("sqlite:///test.db", 0.6)

+ 2 - 2
pwman/tests/test_importer.py

@@ -94,8 +94,8 @@ class TestImporter(unittest.TestCase):
         if os.path.exists('importdummy.db'):
             os.unlink('importdummy.db')
         args = Args(import_file=open('import_file.csv'), db='importdummy.db')
-        dbtype, dbver, fname = 'sqlite', 0.6, 'importdummy.db'
-        db = pwman.data.factory.create(dbtype, dbver, fname)
+        db = pwman.data.factory.createdb('sqlite:///' + os.getcwd() +
+                                         '/importdummy.db', 0.6)
         importer = Importer((args, '', db))
         importer.importer.run(callback=DummyCallback)
         args.import_file.close()

+ 2 - 1
pwman/tests/test_init.py

@@ -67,7 +67,8 @@ class TestInit(unittest.TestCase):
     def setUp(self):
         "test that the right db instance was created"
         self.dbtype = 'sqlite'
-        self.db = factory.create(self.dbtype, __DB_FORMAT__, testdb)
+        self.db = factory.createdb('sqlite://'+os.path.abspath(testdb),
+                                   __DB_FORMAT__)
         self.tester = SetupTester(__DB_FORMAT__, dburi=testdb)
         self.tester.create()