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

[tor-commits] [ooni-probe/master] Load NetTests from strings



commit 423d3d6882b8945be915162e54a914c49b393fd7
Author: Arturo Filastò <art@xxxxxxxxx>
Date:   Sun Jan 13 12:55:38 2013 +0100

    Load NetTests from strings
    
    This is achieved by converting the string to a StringIO
---
 ooni/nettest.py       |   26 +++++++++++++++++---------
 tests/test_nettest.py |   24 ++++++++++++++++++++----
 2 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/ooni/nettest.py b/ooni/nettest.py
index 5c2e8cf..c9e2ed3 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -9,6 +9,9 @@ from ooni.utils import log, checkForRoot, NotRootError
 from inspect import getmembers
 from StringIO import StringIO
 
+class NoTestCasesFound(Exception):
+    pass
+
 class NetTest(object):
     director = None
     method_prefix = 'test'
@@ -33,7 +36,7 @@ class NetTest(object):
         """
         raise NotImplementedError
 
-    def loadNetTest(self, net_test_object):
+    def loadNetTest(self, net_test_file):
         """
         Creates all the necessary test_cases (a list of tuples containing the
         NetTestCase (test_class, test_method))
@@ -50,23 +53,28 @@ class NetTest(object):
 
         Note: the inputs must be valid for test_classA and test_classB.
 
-        net_test_object:
-            is a file like object that will be used to generate the test_cases.
+        net_test_file:
+            is either a file path or a file like object that will be used to
+            generate the test_cases.
         """
+        test_cases = None
         try:
-            if os.path.isfile(net_test_object):
-                test_cases = self._loadNetTestFile(net_test_object)
+            if os.path.isfile(net_test_file):
+                test_cases = self._loadNetTestFile(net_test_file)
+            else:
+                net_test_file = StringIO(net_test_file)
+                raise TypeError("not a file path")
+
         except TypeError:
-            if isinstance(net_test_object, StringIO) or \
-                isinstance(net_test_object, str):
-                test_cases = self._loadNetTestString(net_test_object)
+            if hasattr(net_test_file, 'read'):
+                test_cases = self._loadNetTestFromFileObject(net_test_file)
 
         if not test_cases:
             raise NoTestCasesFound
 
         return test_cases
 
-    def _loadNetTestString(self, net_test_string):
+    def _loadNetTestFromFileObject(self, net_test_string):
         """
         Load NetTest from a string
         """
diff --git a/tests/test_nettest.py b/tests/test_nettest.py
index 0c91a9e..f2d4c6f 100644
--- a/tests/test_nettest.py
+++ b/tests/test_nettest.py
@@ -51,7 +51,6 @@ class DummyTestCase(NetTestCase):
         self.report['foo'] = 'foo'
 """
 
-
 #XXX you should actually implement this
 net_test_with_required_option = net_test_string
 
@@ -132,7 +131,25 @@ class TestNetTest(unittest.TestCase):
 
         os.unlink(net_test_file)
 
-    def test_load_net_test_from_string(self):
+    def test_load_net_test_from_str(self):
+        """
+        Given a file like object verify that the net test cases are properly
+        generated.
+        """
+        net_test_from_string = NetTest(net_test_string,
+                dummyOptions, DummyReporter())
+
+        test_methods = set()
+        for test_class, test_method in net_test_from_string.test_cases:
+            instance = test_class()
+            c = getattr(instance, test_method)
+            self.assertCallable(c)
+
+            test_methods.add(test_method)
+
+        self.assertEqual(set(['test_a', 'test_b']), test_methods)
+
+    def test_load_net_test_from_StringIO(self):
         """
         Given a file like object verify that the net test cases are properly
         generated.
@@ -199,8 +216,7 @@ class TestNetTest(unittest.TestCase):
             dummyOptionsWithFile, None)
 
         measurements = list(net_test.generateMeasurements())
-        self.assertEqual(len(measuremenets), 20)
-
+        self.assertEqual(len(measurements), 20)
 
     def dd_test_require_root_succeed(self):
         n = NetTest(StringIO(net_test_root_required),



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