[tor-commits] [stem/master] First complete build of export.py tests.

atagar at torproject.org atagar at torproject.org
Sat Aug 4 19:19:49 UTC 2012

commit aafb7b429e7220c938da7a591d17ca1b2463aa09
Author: Erik <eislo at wesleyan.edu>
Date:   Tue Jul 31 12:35:09 2012 -0400

    First complete build of export.py tests.
    Everything should pass, and testing export_csv_file() promted some changes,
    which include the change from specifying a file for writing to providing
    a document object.  This code is now ready for review.
 stem/descriptor/export.py      |    9 +++++----
 test/mocking.py                |    2 +-
 test/unit/descriptor/export.py |   14 +++++++-------
 3 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/stem/descriptor/export.py b/stem/descriptor/export.py
index af62be5..1f94734 100644
--- a/stem/descriptor/export.py
+++ b/stem/descriptor/export.py
@@ -76,7 +76,7 @@ def export_csvs(descriptors, include_fields=[], exclude_fields=[], header=False)
   return temp_file.getvalue()
   # cStringIO files are closed automatically when the current scope is exited.
-def export_csv_file(descriptors, document, include_fields=[], exclude_fields=[], header=True):
+def export_csv_file(descriptors, document, include_fields=(), exclude_fields=(), header=True):
   Writes descriptor attributes to a csv file on disk.
@@ -89,7 +89,8 @@ def export_csv_file(descriptors, document, include_fields=[], exclude_fields=[],
   :param list include_fields: list of attribute fields to include in the csv line.
   :param list exclude_fields: list of attribute fields to exclude from csv line.
-  try:
+  if not hasattr(document, 'write'):
+    raise AttributeError("Provided %r object does not have a write method." % document)
+  else:
     document.write(export_csvs(descriptors, include_fields=include_fields, exclude_fields=exclude_fields, header=header))
-  except AttributeError:
-    print "A valid document object was not provided; could not write"
diff --git a/test/mocking.py b/test/mocking.py
index b2d9737..071514d 100644
--- a/test/mocking.py
+++ b/test/mocking.py
@@ -69,7 +69,7 @@ def return_for_args(args_to_return_value, kwarg_type=None, default=None):
   :param object kwarg_type: type of kwarg mapping to be used in unwraping these arguments.
   :param functor default: returns the value of this function if the args don't match something that we have, we raise a ValueError by default
   def _return_value(*args, **kwargs):
     # First handle the case in which we aren't expecting keyword args.
     if kwarg_type == None:
diff --git a/test/unit/descriptor/export.py b/test/unit/descriptor/export.py
index a785957..d006e85 100644
--- a/test/unit/descriptor/export.py
+++ b/test/unit/descriptor/export.py
@@ -136,20 +136,20 @@ class TestExport(unittest.TestCase):
     # Must use named tuples again for ret_vals dictionary.
     Fields = namedtuple('Fields', 'include_fields exclude_fields header')
-    ret_vals = {(descriptor, sample_file):sample_csv_string,
-      (descriptor, sample_file, Fields(include_fields=('address', 'onion_key'), exclude_fields=('address',), header=False)):sample_csv_string2}
+    ret_vals = {((descriptor,), Fields(include_fields=(), exclude_fields=(), header=True)):sample_csv_string,
+      ((descriptor,), Fields(include_fields=('address', 'onion_key'), exclude_fields=('address',), header=False)):sample_csv_string2}
     # TODO Ask Danner: mock it once then do both tests (not including assertRaises), or do separate mockings.
     #    the latter requires that we still include empty incl_fields and excl_fields parameters instead of
     #    letting them default to [].  Same for header.
     mocking.mock(export.export_csvs, mocking.return_for_args(ret_vals, kwarg_type=Fields))
-    export.export_csv_file(descriptor, sample_file)
+    export.export_csv_file((descriptor,), sample_file)
     self.assertEqual(sample_csv_string, sample_file.getvalue())
-    sample_file = cStringIO.StringIO
+    sample_file = cStringIO.StringIO()
-    export.export_csv_file(descriptor, sample_file, include_fields=('address', 'onion_key'), exclude_fields=('address',), header=False)
+    export.export_csv_file((descriptor,), sample_file, include_fields=('address', 'onion_key'), exclude_fields=('address',), header=False)
     self.assertEqual(sample_csv_string2, sample_file.getvalue())
     # Make sure error is Raised when necessary.
-    self.assertRaises(export.export_csv_file, (descriptor, sample_csv_string))
+    self.assertRaises(AttributeError, export.export_csv_file, (descriptor,), sample_csv_string)

More information about the tor-commits mailing list