Browse Source

add config test

tangs 5 years ago
parent
commit
1cf9895b78
5 changed files with 48 additions and 4 deletions
  1. 6 0
      config.json
  2. 6 3
      config.py
  3. 22 0
      config_test.py
  4. 11 0
      timing_test.py
  5. 3 1
      upload.py

+ 6 - 0
config.json

@@ -0,0 +1,6 @@
+{
+    "redis_host": "local.pc",
+    "redis_port": ":6370",
+    "redis_db": 1,
+    "redis_password": "password"
+}

+ 6 - 3
config.py

@@ -15,10 +15,12 @@ class Config:
 
     def read_local_config(self):
         try:
-            f = open(self.path)
+            f = open(self.path, encoding='utf-8')
             self.local_config = json.load(f)
+            f.close()
         except Exception as e:
             logging.error("[CONFIG] read config with path: %s error: %s", self.path, e)
+        return self.local_config
 
     def read_redis_config(self):
         if self.local_config is None:
@@ -31,9 +33,10 @@ class Config:
 
         try:
             r = redis.Redis(host, port, db=db, password=password)
-            config = r.get('config').decode('utf8')
+            config = r.get('config').decode('utf-8')
             if config is None or config is "":
                 return
             self.redis_config = json.loads(config)
         except Exception as e:
-            print(e)
+            logging.error("[CONFIG] read redis config with local_config(%s) error: %s", self.local_config, e)
+        return self.redis_config

+ 22 - 0
config_test.py

@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+# -*- coding:utf-8 -*-
+
+import unittest
+import config
+
+class ConfigTest(unittest.TestCase):
+    def test_read_local_config(self):
+        path = 'config.json'
+        conf = config.Config(path)
+        local_config = conf.read_local_config()
+        self.assertIsNotNone(local_config)
+        self.assertEqual(local_config['redis_host'], 'local.pc')
+        self.assertEqual(local_config['redis_port'], ':6370')
+        self.assertEqual(local_config['redis_db'], 1)
+        self.assertEqual(local_config['redis_password'], 'password')
+
+    def test_read_redis_config(self):
+        pass
+
+if __name__ == '__main__':
+    unittest.main()

+ 11 - 0
timing_test.py

@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+# -*- coding:utf-8 -*-
+
+import unittest
+
+class TimingTest(unittest.TestCase):
+    pass
+
+
+if __name__ == '__main__':
+    pass

+ 3 - 1
upload.py

@@ -133,5 +133,7 @@ class Handle:
     def sleep():
         time.sleep(1)
 
-    def ftp_upload(self):
+
+class Uploader:
+    def __init__(self):
         pass