[tor-commits] [stem/master] Ensure the 'compare_flags' test gives reliable results.

atagar at torproject.org atagar at torproject.org
Thu Jul 9 15:35:27 UTC 2015


commit 1b7b1369579e04292221b2bfce6ad4b3d3c85d2e
Author: cypherpunks <cypherpunks at torproject.org>
Date:   Thu Jul 9 16:10:06 2015 +0200

    Ensure the 'compare_flags' test gives reliable results.
    
    The call to queries.items() causes the issue by returning an unordered set of
    key/value pairs. When the call returns the queries in an unexpected order, the
    mocked Query.run() function assigns the network status documents in the wrong
    order to the authorities. Using an ordered dictionary fixes the problem.
    
    This commit closes ticket 16299.
    
    Other small changes are a PEP8 fix, a grammar fix, and the use of
    assertItemsEqual instead of assertEquals. According to the Python
    documentation [0], assertItemsEqual is the Python 2.7 equivalent of
    assertCountEqual in Python 3.
    
    [0] https://docs.python.org/2.7/library/unittest.html#unittest.TestCase.assertItemsEqual
---
 docs/_static/example/compare_flags.py |   11 ++++++++---
 test/unit/tutorial_examples.py        |    2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/docs/_static/example/compare_flags.py b/docs/_static/example/compare_flags.py
index 1043af1..df317a8 100644
--- a/docs/_static/example/compare_flags.py
+++ b/docs/_static/example/compare_flags.py
@@ -1,9 +1,14 @@
+from collections import OrderedDict
 from stem.descriptor import DocumentHandler, remote
 
 # Query all authority votes asynchronously.
 
-downloader = remote.DescriptorDownloader(document_handler = DocumentHandler.DOCUMENT)
-queries = {}
+downloader = remote.DescriptorDownloader(document_handler=DocumentHandler.DOCUMENT)
+
+# An ordered dictionary ensures queries are finished in the order they were
+# added.
+
+queries = OrderedDict()
 
 for name, authority in remote.get_authorities().items():
   if authority.v3ident is None:
@@ -23,7 +28,7 @@ all_fingerprints = set()
 for vote in votes.values():
   all_fingerprints.update(vote.routers.keys())
 
-# Finally, compare moria1's votes to maatuska.
+# Finally, compare moria1's votes to maatuska's votes.
 
 for fingerprint in all_fingerprints:
   moria1_vote = votes['moria1'].routers.get(fingerprint)
diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py
index c102c1a..a8382a3 100644
--- a/test/unit/tutorial_examples.py
+++ b/test/unit/tutorial_examples.py
@@ -140,7 +140,7 @@ class TestTutorialExamples(unittest.TestCase):
     if stem.prereq.is_python_3():
       self.assertCountEqual(expected.splitlines(), actual.splitlines())
     else:
-      self.assertEquals(sorted(expected.splitlines()), sorted(actual.splitlines()))
+      self.assertItemsEqual(expected.splitlines(), actual.splitlines())
 
   @patch('sys.stdout', new_callable = StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)



More information about the tor-commits mailing list