[tor-commits] [stem/master] Using a common mock controller for unit tests

atagar at torproject.org atagar at torproject.org
Tue May 6 01:21:13 UTC 2014


commit f44f094fba5ce45c03c83b4e0b40746728105597
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun May 4 14:38:08 2014 -0700

    Using a common mock controller for unit tests
    
    A couple of our test modules need a mock controller, so having them each use
    the same mock to avoid duplicating it.
---
 test/unit/interpretor/__init__.py     |   29 +++++++++++++++++++++++++++++
 test/unit/interpretor/autocomplete.py |   24 +++---------------------
 test/unit/interpretor/help.py         |   28 +---------------------------
 3 files changed, 33 insertions(+), 48 deletions(-)

diff --git a/test/unit/interpretor/__init__.py b/test/unit/interpretor/__init__.py
index c49e07f..8063dfc 100644
--- a/test/unit/interpretor/__init__.py
+++ b/test/unit/interpretor/__init__.py
@@ -7,3 +7,32 @@ __all__ = [
   "autocomplete",
   "help",
 ]
+
+try:
+  # added in python 3.3
+  from unittest.mock import Mock
+except ImportError:
+  from mock import Mock
+
+GETINFO_NAMES = """
+info/names -- List of GETINFO options, types, and documentation.
+ip-to-country/* -- Perform a GEOIP lookup
+md/id/* -- Microdescriptors by ID
+""".strip()
+
+GETCONF_NAMES = """
+ExitNodes RouterList
+ExitPolicy LineList
+ExitPolicyRejectPrivate Boolean
+""".strip()
+
+
+CONTROLLER = Mock()
+
+CONTROLLER.get_info.side_effect = lambda arg, _: {
+  'info/names': GETINFO_NAMES,
+  'config/names': GETCONF_NAMES,
+  'events/names': 'BW DEBUG INFO NOTICE',
+  'features/names': 'VERBOSE_NAMES EXTENDED_EVENTS',
+  'signal/names': 'RELOAD HUP SHUTDOWN',
+}[arg]
diff --git a/test/unit/interpretor/autocomplete.py b/test/unit/interpretor/autocomplete.py
index 579f177..6541da3 100644
--- a/test/unit/interpretor/autocomplete.py
+++ b/test/unit/interpretor/autocomplete.py
@@ -2,24 +2,14 @@ import unittest
 
 from stem.interpretor.autocomplete import _get_commands, Autocompleter
 
+from test.unit.interpretor import CONTROLLER
+
 try:
   # added in python 3.3
   from unittest.mock import Mock
 except ImportError:
   from mock import Mock
 
-GETINFO_NAMES = """
-info/names -- List of GETINFO options, types, and documentation.
-ip-to-country/* -- Perform a GEOIP lookup
-md/id/* -- Microdescriptors by ID
-""".strip()
-
-GETCONF_NAMES = """
-ExitNodes RouterList
-ExitPolicy LineList
-ExitPolicyRejectPrivate Boolean
-""".strip()
-
 
 class TestAutocompletion(unittest.TestCase):
   def test_autocomplete_results_from_config(self):
@@ -54,15 +44,7 @@ class TestAutocompletion(unittest.TestCase):
 
     # Now check where we should be able to determine tor's capabilities.
 
-    controller.get_info.side_effect = lambda arg, _: {
-      'info/names': GETINFO_NAMES,
-      'config/names': GETCONF_NAMES,
-      'events/names': 'BW DEBUG INFO NOTICE',
-      'features/names': 'VERBOSE_NAMES EXTENDED_EVENTS',
-      'signal/names': 'RELOAD HUP SHUTDOWN',
-    }[arg]
-
-    commands = _get_commands(controller)
+    commands = _get_commands(CONTROLLER)
 
     expected = (
       'GETINFO info/names',
diff --git a/test/unit/interpretor/help.py b/test/unit/interpretor/help.py
index 51123e0..ff178a2 100644
--- a/test/unit/interpretor/help.py
+++ b/test/unit/interpretor/help.py
@@ -2,33 +2,7 @@ import unittest
 
 from stem.interpretor.help import response, _normalize
 
-try:
-  # added in python 3.3
-  from unittest.mock import Mock
-except ImportError:
-  from mock import Mock
-
-GETINFO_NAMES = """
-info/names -- List of GETINFO options, types, and documentation.
-ip-to-country/* -- Perform a GEOIP lookup
-md/id/* -- Microdescriptors by ID
-""".strip()
-
-GETCONF_NAMES = """
-ExitNodes RouterList
-ExitPolicy LineList
-ExitPolicyRejectPrivate Boolean
-""".strip()
-
-
-CONTROLLER = Mock()
-
-CONTROLLER.get_info.side_effect = lambda arg, _: {
-  'info/names': GETINFO_NAMES,
-  'config/names': GETCONF_NAMES,
-  'events/names': 'BW DEBUG INFO NOTICE',
-  'features/names': 'VERBOSE_NAMES EXTENDED_EVENTS',
-}[arg]
+from test.unit.interpretor import CONTROLLER
 
 
 class TestHelpResponses(unittest.TestCase):





More information about the tor-commits mailing list