[tor-commits] [stem/master] Fixing python 3 compatability for remote descriptor unit tests

atagar at torproject.org atagar at torproject.org
Mon Oct 14 00:42:16 UTC 2013


commit a303b137a9ada7cc9a31a57ba2dcbeae89dd83a3
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Oct 13 12:55:24 2013 -0700

    Fixing python 3 compatability for remote descriptor unit tests
    
    ======================================================================
    ERROR: test_query_with_timeout
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/opt/python3.3/lib/python3.3/unittest/mock.py", line 1079, in patched
        arg = patching.__enter__()
      File "/opt/python3.3/lib/python3.3/unittest/mock.py", line 1132, in __enter__
        self.target = self.getter()
      File "/opt/python3.3/lib/python3.3/unittest/mock.py", line 1294, in <lambda>
        getter = lambda: _importer(target)
      File "/opt/python3.3/lib/python3.3/unittest/mock.py", line 988, in _importer
        thing = __import__(import_path)
    ImportError: No module named 'urllib2'
---
 test/unit/descriptor/remote.py |   15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/test/unit/descriptor/remote.py b/test/unit/descriptor/remote.py
index 4eda9e8..60a5c1a 100644
--- a/test/unit/descriptor/remote.py
+++ b/test/unit/descriptor/remote.py
@@ -6,6 +6,7 @@ import io
 import socket
 import unittest
 
+import stem.prereq
 import stem.descriptor.remote
 
 try:
@@ -14,6 +15,12 @@ try:
 except ImportError:
   from mock import patch
 
+# The urlopen() method is in a different location depending on if we're using
+# python 2.x or 3.x. The 2to3 converter accounts for this in imports, but not
+# mock annotations.
+
+URL_OPEN = 'urllib.request.urlopen' if stem.prereq.is_python_3() else 'urllib2.urlopen'
+
 # Output from requesting moria1's descriptor from itself...
 # % curl http://128.31.0.39:9131/tor/server/fp/9695DFC35FFEB861329B9F1AB04C46397020CE31
 
@@ -53,7 +60,7 @@ iO3EUE0AEYah2W9gdz8t+i3Dtr0zgqLS841GC/TyDKCm+MKmN8d098qnwK0NGF9q
 
 
 class TestDescriptorDownloader(unittest.TestCase):
-  @patch('urllib2.urlopen')
+  @patch(URL_OPEN)
   def test_query_download(self, urlopen_mock):
     """
     Check Query functionality when we successfully download a descriptor.
@@ -81,7 +88,7 @@ class TestDescriptorDownloader(unittest.TestCase):
 
     urlopen_mock.assert_called_once_with(expeced_url, timeout = None)
 
-  @patch('urllib2.urlopen')
+  @patch(URL_OPEN)
   def test_query_with_malformed_content(self, urlopen_mock):
     """
     Query with malformed descriptor content.
@@ -109,7 +116,7 @@ class TestDescriptorDownloader(unittest.TestCase):
 
     self.assertRaises(ValueError, query.run)
 
-  @patch('urllib2.urlopen')
+  @patch(URL_OPEN)
   def test_query_with_timeout(self, urlopen_mock):
     urlopen_mock.side_effect = socket.timeout('connection timed out')
 
@@ -128,7 +135,7 @@ class TestDescriptorDownloader(unittest.TestCase):
     )
     self.assertEqual(3, urlopen_mock.call_count)
 
-  @patch('urllib2.urlopen')
+  @patch(URL_OPEN)
   def test_can_iterate_multiple_times(self, urlopen_mock):
     urlopen_mock.return_value = io.BytesIO(TEST_DESCRIPTOR)
 





More information about the tor-commits mailing list