[tor-commits] [stem/master] Order dependent unit test failure

atagar at torproject.org atagar at torproject.org
Tue Jan 22 19:04:59 UTC 2019


commit 6618180b2e5be525cc451b3f745caf4b0dd51527
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Jan 21 11:49:25 2019 -0800

    Order dependent unit test failure
    
    Though these passed for me a few times for me undefinied ordering finally
    caught up with me. Simple to fix. Correcting some pydocs too.
    
      ======================================================================
      FAIL: test_new_header_attribute
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/usr/lib/python3.5/unittest/mock.py", line 1157, in patched
          return func(*args, **keywargs)
        File "/home/atagar/Desktop/stem/test/unit/descriptor/bandwidth_file.py", line 162, in test_new_header_attribute
          self.assertEqual(EXPECTED_NEW_HEADER_CONTENT, str(desc))
      AssertionError: '1410723598\nversion=1.1.0\nnew_header=neat stuff\n=====' != '1410723598\nnew_header=neat stuff\nversion=1.1.0\n====='
        1410723598
      + new_header=neat stuff
        version=1.1.0
      - new_header=neat stuff
        =====
    
      ----------------------------------------------------------------------
---
 stem/descriptor/__init__.py            | 18 ++++++++++++------
 stem/descriptor/bandwidth_file.py      | 10 ++++++++--
 stem/util/__init__.py                  |  2 ++
 test/unit/descriptor/bandwidth_file.py | 18 ++++++++++++------
 4 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index f926c45a..005830be 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -9,11 +9,14 @@ Package for parsing and processing descriptor data.
 ::
 
   parse_file - Parses the descriptors in a file.
-  create - Creates a new custom descriptor.
   create_signing_key - Cretes a signing key that can be used for creating descriptors.
 
   Descriptor - Common parent for all descriptor file types.
-    |- from_str - provides a parsed descriptor for the given string
+    | |- content - creates the text of a new descriptor
+    | |- create - creates a new descriptor
+    | +- from_str - provides a parsed descriptor for the given string
+    |
+    |- type_annotation - provides our @type annotation
     |- get_path - location of the descriptor on disk if it came from a file
     |- get_archive_path - location of the descriptor within the archive it came from
     |- get_bytes - similar to str(), but provides our original bytes content
@@ -109,17 +112,20 @@ except ImportError:
 
 __all__ = [
   'bandwidth_file',
+  'certificate',
   'export',
-  'reader',
-  'remote',
   'extrainfo_descriptor',
-  'server_descriptor',
+  'hidden_service_descriptor',
   'microdescriptor',
   'networkstatus',
+  'reader',
+  'remote',
   'router_status_entry',
+  'server_descriptor',
   'tordnsel',
-  'parse_file',
+
   'Descriptor',
+  'parse_file',
 ]
 
 UNSEEKABLE_MSG = """\
diff --git a/stem/descriptor/bandwidth_file.py b/stem/descriptor/bandwidth_file.py
index b51e9f82..78e2d05e 100644
--- a/stem/descriptor/bandwidth_file.py
+++ b/stem/descriptor/bandwidth_file.py
@@ -25,6 +25,12 @@ from stem.descriptor import (
   Descriptor,
 )
 
+try:
+  # added in python 2.7
+  from collections import OrderedDict
+except ImportError:
+  from stem.util.ordereddict import OrderedDict
+
 HEADER_DIV = b'====='
 
 
@@ -91,7 +97,7 @@ def _parse_file(descriptor_file, validate = False, **kwargs):
 
 
 def _parse_header(descriptor, entries):
-  header = {}
+  header = OrderedDict()
   content = io.BytesIO(descriptor.get_bytes())
 
   content.readline()  # skip the first line, which should be the timestamp
@@ -226,7 +232,7 @@ class BandwidthFile(Descriptor):
     if sign:
       raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
 
-    header = dict(attr) if attr is not None else {}
+    header = OrderedDict(attr) if attr is not None else OrderedDict()
     timestamp = header.pop('timestamp', str(int(time.time())))
     content = header.pop('content', [])
     version = header.get('version', HEADER_DEFAULT.get('version'))
diff --git a/stem/util/__init__.py b/stem/util/__init__.py
index 7fff7dbe..bff894dc 100644
--- a/stem/util/__init__.py
+++ b/stem/util/__init__.py
@@ -17,10 +17,12 @@ __all__ = [
   'lru_cache',
   'ordereddict',
   'proc',
+  'str_tools',
   'system',
   'term',
   'test_tools',
   'tor_tools',
+
   'datetime_to_unix',
 ]
 
diff --git a/test/unit/descriptor/bandwidth_file.py b/test/unit/descriptor/bandwidth_file.py
index 003f9fe8..6150a7e7 100644
--- a/test/unit/descriptor/bandwidth_file.py
+++ b/test/unit/descriptor/bandwidth_file.py
@@ -11,6 +11,12 @@ from stem.descriptor.bandwidth_file import BandwidthFile
 from test.unit.descriptor import get_resource
 
 try:
+  # added in python 2.7
+  from collections import OrderedDict
+except ImportError:
+  from stem.util.ordereddict import OrderedDict
+
+try:
   # added in python 3.3
   from unittest.mock import Mock, patch
 except ImportError:
@@ -144,11 +150,11 @@ class TestBandwidthFile(unittest.TestCase):
     Exercise the example in our content method's pydoc.
     """
 
-    content = BandwidthFile.content({
-      'timestamp': '12345',
-      'version': '1.2.0',
-      'content': [],
-    })
+    content = BandwidthFile.content(OrderedDict([
+      ('timestamp', '12345'),
+      ('version', '1.2.0'),
+      ('content', []),
+    ]))
 
     self.assertEqual(b'12345\nversion=1.2.0\n=====', content)
 
@@ -158,7 +164,7 @@ class TestBandwidthFile(unittest.TestCase):
     Include an unrecognized header field.
     """
 
-    desc = BandwidthFile.create({'version': '1.1.0', 'new_header': 'neat stuff'})
+    desc = BandwidthFile.create(OrderedDict([('version', '1.1.0'), ('new_header', 'neat stuff')]))
     self.assertEqual(EXPECTED_NEW_HEADER_CONTENT, str(desc))
     self.assertEqual('1.1.0', desc.version)
     self.assertEqual({'version': '1.1.0', 'new_header': 'neat stuff'}, desc.header)





More information about the tor-commits mailing list