소스 검색

Add testing for new config class

oz123 10 년 전
부모
커밋
a5bb2f20e3
3개의 변경된 파일88개의 추가작업 그리고 5개의 파일을 삭제
  1. 86 0
      pwman/tests/test_config.py
  2. 2 0
      pwman/tests/test_pwman.py
  3. 0 5
      pwman/util/config.py

+ 86 - 0
pwman/tests/test_config.py

@@ -0,0 +1,86 @@
+#============================================================================
+# This file is part of Pwman3.
+#
+# Pwman3 is free software; you can redistribute it 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) 2014 Oz Nahum Tiram <nahumoz@gmail.com>
+#============================================================================
+
+import os
+import sys
+import unittest
+from pwman.util.config import Config, ConfigException, default_config
+if sys.version_info.major > 2:
+    from configparser import NoSectionError
+else:
+    from ConfigParser import NoSectionError
+
+# TODO: Drop support for none AES Encryption,
+# So the whole section [Encryption] can be dropped.
+with open('testfile.conf', 'w') as f:
+    f.write("""
+[Encryption]
+algorithm = Blowfish
+
+[Global]
+xsel = /usr/bin/xsel
+colors = yes
+umask = 0100
+cls_timeout = 5
+
+[Database]
+type = SQLite
+""")
+
+
+class TestConfig(unittest.TestCase):
+
+    def setUp(self):
+        self.conf = Config(filename='testfile.conf', defaults=default_config)
+
+    def test_has_defaults(self):
+        self.assertTrue(self.conf.parser.has_section('Readline'))
+
+    def test_has_blowfish(self):
+        self.assertEqual('Blowfish', self.conf.get_value('Encryption',
+                                                         'algorithm'))
+
+    def test_has_user_history(self):
+        self.assertEqual(os.path.expanduser('~/.pwman/history'),
+                         self.conf.get_value('Readline', 'history'))
+
+    def test_has_user_db(self):
+        self.assertEqual(os.path.expanduser('~/.pwman/pwman.db'),
+                         self.conf.get_value('Database', 'filename'))
+
+    def test_wrong_config(self):
+        with open('wrong_conf.conf', 'w') as f:
+            f.write("""
+[Encryption
+algorithm = Blowfish
+""")
+        self.assertRaises(ConfigException, Config, 'wrong_conf.conf')
+
+    def test_set_value(self):
+        self.conf.set_value('Global', 'colors', 'no')
+        self.assertEqual('no', self.conf.get_value('Global', 'colors'))
+
+    def test_set_value_wrong(self):
+        self.assertRaises(NoSectionError,
+                          self.conf.set_value, *('Error', 'colors', 'no'))
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2)
+    os.remove('wrong_conf.conf')
+    os.remove('testfile.conf')

+ 2 - 0
pwman/tests/test_pwman.py

@@ -26,6 +26,7 @@ from .db_tests import (DBTests, SetupTester, CLITests, ConfigTest,
 
 #from .crypto_tests import CryptoTest
 from .test_crypto_engine import CryptoEngineTest
+from .test_config import TestConfig
 
 if 'win' not in sys.platform:
     from .test_complete_ui import (Ferrum, NEW_DB_PATH)
@@ -51,6 +52,7 @@ def suite():
     suite.addTest(loader.loadTestsFromTestCase(FactoryTest))
     suite.addTest(loader.loadTestsFromTestCase(TestDBFalseConfig))
     suite.addTest(loader.loadTestsFromTestCase(CryptoEngineTest))
+    suite.addTest(loader.loadTestsFromTestCase(TestConfig))
     #if 'win' not in sys.platform:
     #    suite.addTest(loader.loadTestsFromTestCase(Ferrum))
     return suite

+ 0 - 5
pwman/util/config.py

@@ -59,11 +59,6 @@ _file = None
 _conf = dict()
 _defaults = dict()
 
-"""
-Add a global wide defaults, without regarding any section!
-"""
-defaults = {}
-
 
 class Config(object):