[tor-commits] [stem/master] Unit tests for the load_processed_files() function

atagar at torproject.org atagar at torproject.org
Mon Mar 26 00:10:01 UTC 2012


commit 5e6623ce92e0bee08df9af18aacdd30548d47ad7
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Mar 7 20:28:44 2012 -0800

    Unit tests for the load_processed_files() function
    
    Unit tests for stem.descriptor.reader's load_processed_files() function. This
    mocks the open call to simulate file reads and exercise a variety of good and
    malformed contents. This does not yet test for use cases where the file doesn't
    exists or fails to be read due to permissions, but those will be integ tests.
---
 run_tests.py                     |    2 +
 test/unit/descriptor/__init__.py |    6 ++
 test/unit/descriptor/reader.py   |   96 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+), 0 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index b59d3c3..7cf5117 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -17,6 +17,7 @@ import test.unit.connection.authentication
 import test.unit.connection.protocolinfo
 import test.unit.socket.control_line
 import test.unit.socket.control_message
+import test.unit.descriptor.reader
 import test.unit.util.conf
 import test.unit.util.enum
 import test.unit.util.system
@@ -80,6 +81,7 @@ UNIT_TESTS = (
   test.unit.util.enum.TestEnum,
   test.unit.util.conf.TestConf,
   test.unit.util.system.TestSystem,
+  test.unit.descriptor.reader.TestDescriptorReader,
   test.unit.version.TestVersion,
   test.unit.socket.control_message.TestControlMessage,
   test.unit.socket.control_line.TestControlLine,
diff --git a/test/unit/descriptor/__init__.py b/test/unit/descriptor/__init__.py
new file mode 100644
index 0000000..ddcd821
--- /dev/null
+++ b/test/unit/descriptor/__init__.py
@@ -0,0 +1,6 @@
+"""
+Unit tests for stem.descriptor.
+"""
+
+__all__ = ["reader"]
+
diff --git a/test/unit/descriptor/reader.py b/test/unit/descriptor/reader.py
new file mode 100644
index 0000000..cf11c23
--- /dev/null
+++ b/test/unit/descriptor/reader.py
@@ -0,0 +1,96 @@
+"""
+Unit tests for stem.descriptor.reader.
+"""
+
+import unittest
+import StringIO
+
+import stem.descriptor.reader
+import test.mocking as mocking
+
+class TestDescriptorReader(unittest.TestCase):
+  def tearDown(self):
+    mocking.revert_mocking()
+  
+  def test_load_processed_files(self):
+    """
+    Successful load of content.
+    """
+    
+    test_lines = (
+      "/dir/ 0",
+      "/dir/file 12345",
+      "/dir/file with spaces 7138743",
+      "  /dir/with extra space 12345   ",
+      "   \t   ",
+      "",
+      "/dir/after empty line 12345",
+    )
+    
+    expected_value = {
+      "/dir/": 0,
+      "/dir/file": 12345,
+      "/dir/file with spaces": 7138743,
+      "/dir/with extra space": 12345,
+      "/dir/after empty line": 12345,
+    }
+    
+    test_content = StringIO.StringIO("\n".join(test_lines))
+    mocking.support_with(test_content)
+    mocking.mock(open, mocking.return_value(test_content))
+    self.assertEquals(expected_value, stem.descriptor.reader.load_processed_files(""))
+  
+  def test_load_processed_files_empty(self):
+    """
+    Tests the load_processed_files() function with an empty file.
+    """
+    
+    test_content = StringIO.StringIO("")
+    mocking.support_with(test_content)
+    mocking.mock(open, mocking.return_value(test_content))
+    self.assertEquals({}, stem.descriptor.reader.load_processed_files(""))
+  
+  def test_load_processed_files_no_file(self):
+    """
+    Tests the load_processed_files() function content that is malformed because
+    it is missing the file path.
+    """
+    
+    test_content = StringIO.StringIO(" 12345")
+    mocking.support_with(test_content)
+    mocking.mock(open, mocking.return_value(test_content))
+    self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
+  
+  def test_load_processed_files_no_timestamp(self):
+    """
+    Tests the load_processed_files() function content that is malformed because
+    it is missing the timestamp.
+    """
+    
+    test_content = StringIO.StringIO("/dir/file ")
+    mocking.support_with(test_content)
+    mocking.mock(open, mocking.return_value(test_content))
+    self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
+
+  def test_load_processed_files_malformed_file(self):
+    """
+    Tests the load_processed_files() function content that is malformed because
+    it has an invalid file path.
+    """
+    
+    test_content = StringIO.StringIO("not_an_absolute_file 12345")
+    mocking.support_with(test_content)
+    mocking.mock(open, mocking.return_value(test_content))
+    self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
+
+  def test_load_processed_files_malformed_timestamp(self):
+    """
+    Tests the load_processed_files() function content that is malformed because
+    it has a non-numeric timestamp.
+    """
+    
+    test_content = StringIO.StringIO("/dir/file 123a")
+    mocking.support_with(test_content)
+    mocking.mock(open, mocking.return_value(test_content))
+    self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
+





More information about the tor-commits mailing list