Selaa lähdekoodia

Re-organize testing tools

oz123 10 vuotta sitten
vanhempi
commit
b59da7ea1b
3 muutettua tiedostoa jossa 96 lisäystä ja 86 poistoa
  1. 3 79
      pwman/tests/db_tests.py
  2. 7 7
      pwman/tests/test_converter.py
  3. 86 0
      pwman/tests/test_tools.py

+ 3 - 79
pwman/tests/db_tests.py

@@ -22,10 +22,9 @@ from pwman.data.tags import TagNew
 from pwman.data import factory
 from pwman.data.drivers.sqlite import DatabaseException
 from pwman.util import config
-from pwman.util.callback import Callback
 from pwman.util.generator import leetlist
 from pwman.util.crypto import CryptoEngine, CryptoBadKeyException
-from pwman import which, default_config, set_xsel
+from pwman import default_config, set_xsel
 from pwman.ui import get_ui_platform
 from pwman.ui.base import get_pass_conf
 from pwman.ui.tools import CMDLoop, CliMenuItem
@@ -72,83 +71,8 @@ _saveconfig = False
 PwmanCliNew, OSX = get_ui_platform(sys.platform)
 
 
-class DummyCallback(Callback):
-
-    def getsecret(self, question):
-        return u'12345'
-
-    def getnewsecret(self, question):
-        return u'12345'
-
-
-class DummyCallback2(Callback):
-
-    def getinput(self, question):
-        return u'newsecret'
-
-    def getsecret(self, question):
-        return u'wrong'
-
-    def getnewsecret(self, question):
-        return u'newsecret'
-
-
-class DummyCallback3(Callback):
-
-    def getinput(self, question):
-        return u'newsecret'
-
-    def getsecret(self, question):
-        return u'12345'
-
-    def getnewsecret(self, question):
-        return u'newsecret'
-
-
-class DummyCallback4(Callback):
-
-    def getinput(self, question):
-        return u'newsecret'
-
-    def getsecret(self, question):
-        return u'newsecret'
-
-    def getnewsecret(self, question):
-        return u'newsecret'
-
-
-default_config['Database'] = {'type': 'SQLite',
-                              'filename':
-                              os.path.join(os.path.dirname(__file__),
-                                           "test.pwman.db")
-                              }
-
-
-class SetupTester(object):
-
-    def __init__(self, dbver=None):
-        config.set_defaults(default_config)
-        if not OSX:
-            self.xselpath = which("xsel")
-            config.set_value("Global", "xsel", self.xselpath)
-        else:
-            self.xselpath = "xsel"
-
-        self.dbver = dbver
-
-    def clean(self):
-        if os.path.exists(config.get_value('Database', 'filename')):
-            os.remove(config.get_value('Database', 'filename'))
-
-        if os.path.exists(os.path.join(os.path.dirname(__file__),
-                                       'testing_config')):
-            os.remove(os.path.join(os.path.dirname(__file__),
-                                   'testing_config'))
-
-    def create(self):
-        dbtype = config.get_value("Database", "type")
-        db = factory.create(dbtype, self.dbver)
-        self.cli = PwmanCliNew(db, self.xselpath, DummyCallback)
+from test_tools import (SetupTester, DummyCallback2,
+                        DummyCallback3, DummyCallback4)
 
 
 class DBTests(unittest.TestCase):

+ 7 - 7
pwman/tests/test_converter.py

@@ -27,13 +27,13 @@ from pwman.data.nodes import Node
 from pwman.data.nodes import NewNode
 from pwman.util.crypto import CryptoEngine
 from pwman.data.tags import Tag
-from db_tests import node_factory 
+from db_tests import node_factory
 from pwman.util.callback import CLICallback
 import sqlite3 as sqlite
 import pwman.util.config as config
 from pwman import default_config
 import cPickle
-
+from test_tools import SetupTester
 
 class SQLiteDatabase(Database):
     """SQLite Database implementation"""
@@ -303,7 +303,7 @@ class SQLiteDatabase(Database):
             return keyrow[0]
 
 
-class CreateTestDataBase(object):
+class CreateTestDataBases(object):
 
     def __init__(self):
         config.set_defaults(default_config)
@@ -323,8 +323,8 @@ class CreateTestDataBase(object):
         password = 'Password'
         url = 'example.org'
         notes = 'some notes'
-        node = node_factory(username, password, url, notes, 
-                ['testing1', 'testing2']) 
+        node = node_factory(username, password, url, notes,
+                            ['testing1', 'testing2'])
         self.db.open()
         self.db.addnodes([node])
         idx_created = node._id
@@ -334,12 +334,12 @@ class CreateTestDataBase(object):
                           'url': url, 'notes': notes}.iteritems():
             self.assertEquals(attr, getattr(new_node, key))
         self.db.close()
-    
+
     def run(self):
         self.open_dbs()
         self.add_nodes_to_db1()
 
 
 if __name__ == '__main__':
-    tester = CreateTestDataBase()
+    tester = CreateTestDataBases()
     tester.run()

+ 86 - 0
pwman/tests/test_tools.py

@@ -0,0 +1,86 @@
+from pwman.data import factory
+from pwman.util import config
+from pwman import which, default_config
+from pwman.ui import get_ui_platform
+import os
+import os.path
+import sys
+from pwman.util.callback import Callback
+
+PwmanCliNew, OSX = get_ui_platform(sys.platform)
+
+
+class DummyCallback(Callback):
+
+    def getsecret(self, question):
+        return u'12345'
+
+    def getnewsecret(self, question):
+        return u'12345'
+
+
+class DummyCallback2(Callback):
+
+    def getinput(self, question):
+        return u'newsecret'
+
+    def getsecret(self, question):
+        return u'wrong'
+
+    def getnewsecret(self, question):
+        return u'newsecret'
+
+
+class DummyCallback3(Callback):
+
+    def getinput(self, question):
+        return u'newsecret'
+
+    def getsecret(self, question):
+        return u'12345'
+
+    def getnewsecret(self, question):
+        return u'newsecret'
+
+
+class DummyCallback4(Callback):
+
+    def getinput(self, question):
+        return u'newsecret'
+
+    def getsecret(self, question):
+        return u'newsecret'
+
+    def getnewsecret(self, question):
+        return u'newsecret'
+
+
+class SetupTester(object):
+
+    def __init__(self, dbver=None, filename=None):
+        config.set_defaults(default_config)
+        if not OSX:
+            self.xselpath = which("xsel")
+            config.set_value("Global", "xsel", self.xselpath)
+        else:
+            self.xselpath = "xsel"
+
+        self.dbver = dbver
+        self.filename = filename
+
+    def clean(self):
+        if os.path.exists(config.get_value('Database', 'filename')):
+            os.remove(config.get_value('Database', 'filename'))
+
+        if os.path.exists(os.path.join(os.path.dirname(__file__),
+                                       'testing_config')):
+            os.remove(os.path.join(os.path.dirname(__file__),
+                                   'testing_config'))
+
+    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)
+        self.cli = PwmanCliNew(db, self.xselpath, DummyCallback)