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

[tor-commits] [stem/master] Unit test for CollecTor index method



commit 97c23c831e131e849a433054bb6653c8165a78f7
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Dec 25 14:39:52 2017 -0800

    Unit test for CollecTor index method
    
    Initial method for the index method. Mocking CollecTor responses will not only
    be quicker, but let us exercise more scenarios.
---
 stem/descriptor/collector.py      |  2 +-
 test/unit/descriptor/collector.py | 24 +++++++++++++++++++++++-
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/stem/descriptor/collector.py b/stem/descriptor/collector.py
index 1184c56e..c6d70544 100644
--- a/stem/descriptor/collector.py
+++ b/stem/descriptor/collector.py
@@ -117,7 +117,7 @@ class Compression(object):
     elif self._module_name == 'lzma':
       decompressed = self._module.decompress(content)
     else:
-      raise ImportError("BUG: No implementation for %s decompression" % self)
+      raise ImportError('BUG: No implementation for %s decompression' % self)
 
     return stem.util.str_tools._to_unicode(decompressed)
 
diff --git a/test/unit/descriptor/collector.py b/test/unit/descriptor/collector.py
index f09d5b9d..01f8f6ee 100644
--- a/test/unit/descriptor/collector.py
+++ b/test/unit/descriptor/collector.py
@@ -2,9 +2,20 @@
 Unit tests for stem.descriptor.collector.
 """
 
+import io
 import unittest
 
-from stem.descriptor.collector import GZIP, BZ2, LZMA, url
+import stem.prereq
+
+from stem.descriptor.collector import GZIP, BZ2, LZMA, CollecTor, url
+
+try:
+  # added in python 3.3
+  from unittest.mock import Mock, patch
+except ImportError:
+  from mock import Mock, patch
+
+URL_OPEN = 'urllib.request.urlopen' if stem.prereq.is_python_3() else 'urllib2.urlopen'
 
 
 class TestCollector(unittest.TestCase):
@@ -14,3 +25,14 @@ class TestCollector(unittest.TestCase):
     self.assertEqual('https://collector.torproject.org/index/index.json.gz', url('index', compression = GZIP))
     self.assertEqual('https://collector.torproject.org/index/index.json.bz2', url('index', compression = BZ2))
     self.assertEqual('https://collector.torproject.org/index/index.json.xz', url('index', compression = LZMA))
+
+  @patch(URL_OPEN, Mock(return_value = io.BytesIO(b'{"index_created":"2017-12-25 21:06","build_revision":"56a303e","path":"https://collector.torproject.org"}')))
+  def test_index(self):
+    expected = {
+      'index_created': '2017-12-25 21:06',
+      'build_revision': '56a303e',
+      'path': 'https://collector.torproject.org'
+    }
+
+    collector = CollecTor(compression = None)
+    self.assertEqual(expected, collector.index())



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