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

[tor-commits] [stem/master] Adding tests for the network status document examples



commit 78e09a971b7c242dc1d0a6b95718b78edd5cb32b
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Oct 9 07:56:33 2012 -0700

    Adding tests for the network status document examples
    
    Including unit tests for the header pydoc examples to check that they're
    runnable. I also changed the first example to show that the consensus file
    doesn't need to remain open when using that method.
---
 stem/descriptor/networkstatus.py               |   21 ++++++++--------
 test/mocking.py                                |    4 +++
 test/unit/descriptor/networkstatus/document.py |   30 +++++++++++++++++++++++-
 3 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index f6116c5..ae0f0c3 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -26,14 +26,15 @@ constructor. Router entries are assigned to its 'routers' attribute...
 
   from stem.descriptor.networkstatus import NetworkStatusDocument
   
-  with open('.tor/cached-consensus', 'r') as consensus_file:
-    # Reads the full consensus into memory twice (both for the parsed and
-    # unparsed contents).
-    
-    consensus = NetworkStatusDocument(consensus_file.read())
-    
-    for router in consensus.routers:
-      print router.nickname
+  # Reads the full consensus into memory twice (both for the parsed and
+  # unparsed contents).
+  
+  consensus_file = open('.tor/cached-consensus', 'r')
+  consensus = NetworkStatusDocument(consensus_file.read())
+  consensus_file.close()
+  
+  for router in consensus.routers:
+    print router.nickname
 
 * :func:`stem.descriptor.parse_file`
 
@@ -378,7 +379,7 @@ class _DocumentHeader(object):
   
   def _parse(self, entries, validate):
     for keyword, values in entries.items():
-      value, block_contents = values[0]
+      value, _ = values[0]
       line = "%s %s" % (keyword, value)
       
       # all known header fields can only appear once except
@@ -798,7 +799,7 @@ class DirectoryAuthority(stem.descriptor.Descriptor):
           raise ValueError("Authority %s shouldn't have a '%s' line:\n%s" % (type_label, keyword, content))
     
     for keyword, values in entries.items():
-      value, block_contents = values[0]
+      value, _ = values[0]
       line = "%s %s" % (keyword, value)
       
       # all known attributes can only appear at most once
diff --git a/test/mocking.py b/test/mocking.py
index 7c848ad..ae74166 100644
--- a/test/mocking.py
+++ b/test/mocking.py
@@ -10,6 +10,7 @@ calling :func:`test.mocking.revert_mocking`.
   revert_mocking - reverts any changes made by the mock function
   get_real_function - provides the non-mocked version of a function
   get_all_combinations - provides all combinations of attributes
+  support_with - makes object be compatable for use via the 'with' keyword
   
   Mocking Functions
     no_op           - does nothing
@@ -216,10 +217,13 @@ def support_with(obj):
   does nothing.
   
   :param object obj: object to support the 'with' keyword
+  
+  :returns: input object
   """
   
   obj.__dict__["__enter__"] = return_value(obj)
   obj.__dict__["__exit__"] = no_op()
+  return obj
 
 def mock(target, mock_call, target_module=None):
   """
diff --git a/test/unit/descriptor/networkstatus/document.py b/test/unit/descriptor/networkstatus/document.py
index 8009fbb..246f7aa 100644
--- a/test/unit/descriptor/networkstatus/document.py
+++ b/test/unit/descriptor/networkstatus/document.py
@@ -2,6 +2,8 @@
 Unit tests for the NetworkStatusDocument of stem.descriptor.networkstatus.
 """
 
+from __future__ import with_statement
+
 import datetime
 import unittest
 import StringIO
@@ -10,7 +12,7 @@ import stem.version
 from stem.descriptor import Flag
 from stem.descriptor.networkstatus import HEADER_STATUS_DOCUMENT_FIELDS, FOOTER_STATUS_DOCUMENT_FIELDS, DEFAULT_PARAMS, BANDWIDTH_WEIGHT_ENTRIES, DirectoryAuthority, NetworkStatusDocument, parse_file
 from stem.descriptor.router_status_entry import RouterStatusEntryV3, RouterStatusEntryMicroV3
-from test.mocking import get_router_status_entry_v3, get_router_status_entry_micro_v3, get_directory_authority, get_network_status_document, CRYPTO_BLOB, DOC_SIG
+from test.mocking import support_with, get_router_status_entry_v3, get_router_status_entry_micro_v3, get_directory_authority, get_network_status_document, CRYPTO_BLOB, DOC_SIG
 
 class TestNetworkStatusDocument(unittest.TestCase):
   def test_minimal_consensus(self):
@@ -79,6 +81,32 @@ class TestNetworkStatusDocument(unittest.TestCase):
     self.assertEqual([DOC_SIG], document.signatures)
     self.assertEqual([], document.get_unrecognized_lines())
   
+  def test_examples(self):
+    """
+    Run something similar to the examples in the header pydocs.
+    """
+    
+    # makes a consensus with a couple routers, both with the same nickname
+    
+    entry1 = get_router_status_entry_v3({'s': "Fast"})
+    entry2 = get_router_status_entry_v3({'s': "Valid"})
+    content = get_network_status_document(routers = (entry1, entry2), content = True)
+    
+    # first example: parsing via the NetworkStatusDocument constructor
+    
+    consensus_file = StringIO.StringIO(content)
+    consensus = NetworkStatusDocument(consensus_file.read())
+    consensus_file.close()
+    
+    for router in consensus.routers:
+      self.assertEqual('caerSidi', router.nickname)
+    
+    # second example: using parse_file
+    
+    with support_with(StringIO.StringIO(content)) as consensus_file:
+      for router in parse_file(consensus_file):
+        self.assertEqual('caerSidi', router.nickname)
+  
   def test_parse_file(self):
     """
     Try parsing a document via the parse_file() function.



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