Browse Source

Add unit tests

Andrew Morgan 4 years ago
parent
commit
5c39549c23
4 changed files with 27 additions and 7 deletions
  1. 1 0
      setup.py
  2. 2 2
      sydent/sydent.py
  3. 22 0
      tests/__init__.py
  4. 2 5
      tests/test_start.py

+ 1 - 0
setup.py

@@ -52,6 +52,7 @@ setup(
         "sortedcontainers>=2.1.0",
         "six>=1.10",
         "pyyaml>=3.11",
+        "mock>=3.0.5",
     ],
     # make sure we package the sql files
     include_package_data=True,

+ 2 - 2
sydent/sydent.py

@@ -127,10 +127,10 @@ CONFIG_DEFAULTS = {
 
 
 class Sydent:
-    def __init__(self, reactor=twisted.internet.reactor):
+    def __init__(self, reactor=twisted.internet.reactor, config=None):
         self.reactor = reactor
         self.config_file = os.environ.get('SYDENT_CONF', "sydent.conf")
-        self.cfg = parse_config(self.config_file)
+        self.cfg = config if config else parse_config(self.config_file)
 
         log_format = (
             "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s"

+ 22 - 0
tests/__init__.py

@@ -0,0 +1,22 @@
+from twisted.test.proto_helpers import MemoryReactorClock
+from sydent.sydent import Sydent
+from sydent.sydent import CONFIG_DEFAULTS
+
+
+def make_sydent(config={}):
+    """Create a new sydent
+
+    Args:
+        config (dict): any configuration variables for overriding the default sydent
+            config
+    """
+    # Override default config with provided dict contents
+    config = CONFIG_DEFAULTS
+    for section in config.keys():
+        if section not in config:
+            config[section] = {}
+        for option in section.keys():
+            config[section][option] = config[section][option]
+
+    reactor = MemoryReactorClock()
+    return Sydent(reactor, config=config)

+ 2 - 5
tests/test_start.py

@@ -1,12 +1,9 @@
 from twisted.trial import unittest
-from twisted.test.proto_helpers import MemoryReactorClock
-from sydent.sydent import Sydent
+from . import make_sydent
 
 
 class StartupTestCase(unittest.TestCase):
     """Test that sydent started up correctly"""
     def test_start(self):
-        reactor = MemoryReactorClock()
-        sydent = Sydent(reactor)
-
+        sydent = make_sydent()
         sydent.run()