[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Cleaning up 'The Little Engine that Could' tutorial test



commit 478ba3a44f3291367eeaea9ba9ce0f4cab24f517
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Feb 23 11:49:02 2013 -0800

    Cleaning up 'The Little Engine that Could' tutorial test
    
    Of all our tests the tutorial example tests stick out as being... just
    aweful. They work I guess but their mix of heavy mocking and the example being
    tested is both damn confusing and unmaintainable.
    
    Breaking it up so the mocking is entirely separate from the tutorial code.
---
 docs/tutorial/the_little_relay_that_could.rst |    2 +-
 test/mocking.py                               |   12 ++++++---
 test/unit/tutorial.py                         |   32 +++++++++++++++---------
 3 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/docs/tutorial/the_little_relay_that_could.rst b/docs/tutorial/the_little_relay_that_could.rst
index 5f85f6e..e3f42d0 100644
--- a/docs/tutorial/the_little_relay_that_could.rst
+++ b/docs/tutorial/the_little_relay_that_could.rst
@@ -49,7 +49,7 @@ many bytes Tor has sent and received...
   from stem.control import Controller
   
   with Controller.from_port(control_port = 9051) as controller:
-    controller.authenticate() # provide the password here if you set one
+    controller.authenticate()  # provide the password here if you set one
 
     bytes_read = controller.get_info("traffic/read")
     bytes_written = controller.get_info("traffic/written")
diff --git a/test/mocking.py b/test/mocking.py
index 3ee8f08..9ed3fbb 100644
--- a/test/mocking.py
+++ b/test/mocking.py
@@ -204,14 +204,14 @@ NETWORK_STATUS_DOCUMENT_FOOTER = (
 
 
 def no_op():
-  def _no_op(*args):
+  def _no_op(*args, **kwargs):
     pass
 
   return _no_op
 
 
 def return_value(value):
-  def _return_value(*args):
+  def _return_value(*args, **kwargs):
     return value
 
   return _return_value
@@ -318,7 +318,7 @@ def support_with(obj):
   return obj
 
 
-def mock(target, mock_call, target_module=None):
+def mock(target, mock_call, target_module = None, is_static = False):
   """
   Mocks the given function, saving the initial implementation so it can be
   reverted later.
@@ -331,6 +331,7 @@ def mock(target, mock_call, target_module=None):
   :param function target: function to be mocked
   :param functor mock_call: mocking to replace the function with
   :param module target_module: module that this is mocking, this defaults to the inspected value
+  :param bool is_static: handles this like a static method of the target_module if True
   """
 
   if hasattr(target, "mock_id"):
@@ -349,7 +350,10 @@ def mock(target, mock_call, target_module=None):
 
   # mocks the function with this wrapper
 
-  setattr(target_module, target_function, mock_wrapper)
+  if is_static:
+    setattr(target_module, target_function, staticmethod(mock_wrapper))
+  else:
+    setattr(target_module, target_function, mock_wrapper)
 
 
 def mock_method(target_class, method_name, mock_call):
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 21db41b..7d79eb2 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -8,6 +8,7 @@ import StringIO
 import sys
 import unittest
 
+from stem.control import Controller
 from test import mocking
 
 
@@ -23,27 +24,34 @@ class TestTutorial(unittest.TestCase):
     sys.stdout = self.stdout_real
 
   def test_the_little_relay_that_could(self):
-    from stem.control import Controller
+    def tutorial_example():
+      from stem.control import Controller
+
+      with Controller.from_port(control_port = 9051) as controller:
+        controller.authenticate()  # provide the password here if you set one
+
+        bytes_read = controller.get_info("traffic/read")
+        bytes_written = controller.get_info("traffic/written")
+
+        print "My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written)
 
     controller = mocking.get_object(Controller, {
       'authenticate': mocking.no_op(),
       'close': mocking.no_op(),
       'get_info': mocking.return_for_args({
-        ('traffic/read',): '1234',
-        ('traffic/written',): '5678',
+        ('traffic/read',): '33406',
+        ('traffic/written',): '29649',
       }, is_method = True),
     })
 
-    controller.authenticate()
-
-    bytes_read = controller.get_info("traffic/read")
-    bytes_written = controller.get_info("traffic/written")
-
-    print "My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written)
-
-    controller.close()
+    mocking.mock(
+      Controller.from_port, mocking.return_value(controller),
+      target_module = Controller,
+      is_static = True,
+    )
 
-    self.assertEqual("My Tor relay has read 1234 bytes and written 5678.\n", self.stdout.getvalue())
+    tutorial_example()
+    self.assertEqual("My Tor relay has read 33406 bytes and written 29649.\n", self.stdout.getvalue())
 
   def test_mirror_mirror_on_the_wall(self):
     from stem.descriptor.server_descriptor import RelayDescriptor



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits