[geneagrapher] 165/226: - Modified option parsing error handling. - Added new end-to-end test to cover ggrapher function.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Sat Jul 11 17:11:00 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch master
in repository geneagrapher.

commit 8cf83c587f7a56d3b1c6ce442a149492ee814b43
Author: David Alber <alber.david at gmail.com>
Date:   Fri Nov 4 21:36:21 2011 -0700

    - Modified option parsing error handling.
    - Added new end-to-end test to cover ggrapher function.
---
 geneagrapher/geneagrapher.py       | 10 +++------
 tests/test_geneagrapher_methods.py | 42 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/geneagrapher/geneagrapher.py b/geneagrapher/geneagrapher.py
index af4f609..94425ec 100644
--- a/geneagrapher/geneagrapher.py
+++ b/geneagrapher/geneagrapher.py
@@ -24,7 +24,7 @@ class Geneagrapher:
         self.parser = OptionParser(version="{}".format(
             pkg_env[self.__module__.split('.')[0]][0].version))
 
-        self.parser.set_usage("%prog [options] ID ...")
+        self.parser.set_usage("%prog [options] ID [ID...]")
         self.parser.set_description('Create a Graphviz "dot" file for a mathematics genealogy, where ID is a record identifier from the Mathematics Genealogy Project. Multiple IDs may be passed.')
 
         self.parser.add_option("-f", "--file", dest="filename",
@@ -42,7 +42,7 @@ class Geneagrapher:
         (options, args) = self.parser.parse_args()
         
         if len(args) == 0:
-            raise SyntaxError("%s: error: no record IDs passed" % (self.parser.get_prog_name()))
+            self.parser.error("no record IDs given")
 
         self.get_ancestors = options.get_ancestors
         self.get_descendants = options.get_descendants
@@ -126,10 +126,6 @@ def ggrapher():
     """Function to run the Geneagrapher. This is the function called when
     the ggrapher script is run."""
     ggrapher = Geneagrapher()
-    try:
-        ggrapher.parse_input()
-    except SyntaxError, e:
-        print ggrapher.parser.get_usage()
-        print e
+    ggrapher.parse_input()
     ggrapher.build_graph()
     ggrapher.generate_dot_file()
diff --git a/tests/test_geneagrapher_methods.py b/tests/test_geneagrapher_methods.py
index dbc40d5..97cbe7b 100644
--- a/tests/test_geneagrapher_methods.py
+++ b/tests/test_geneagrapher_methods.py
@@ -24,8 +24,23 @@ class TestGeneagrapherMethods(unittest.TestCase):
     def test002_parse_empty(self):
         # Test parse_input() with no arguments.
         sys.argv = ['geneagrapher']
-        self.assertRaises(SyntaxError, self.ggrapher.parse_input)
-        
+
+        # Redirect stderr to capture output.
+        stderr = sys.stderr
+        stderr_intercept = StringIO.StringIO()
+        sys.stderr = stderr_intercept
+
+        expected = """Usage: geneagrapher [options] ID [ID...]
+
+geneagrapher: error: no record IDs given
+"""
+        try:
+            self.ggrapher.parse_input()
+        except SystemExit:
+            self.assertEquals(stderr_intercept.getvalue().decode('utf-8'), expected)
+
+        sys.stderr = stderr
+
     def test003_parse_default(self):
         # Test parse_input() with no options.
         sys.argv = ['geneagrapher', '3']
@@ -299,5 +314,28 @@ class TestGeneagrapherMethods(unittest.TestCase):
             self.assertEquals(fin.read().decode('utf-8'), expected)
         os.remove(outfname)
 
+    def test013_end_to_end_through_ggrapher_self_stdout(self):
+        # Complete test calling ggrapher getting no ancestors or descendants
+        # and writing the result to stdout.
+        sys.argv = ['geneagrapher', '30484']
+
+        # Redirect stdout to capture output.
+        stdout = sys.stdout
+        stdout_intercept = StringIO.StringIO()
+        sys.stdout = stdout_intercept
+        geneagrapher.ggrapher()
+        sys.stdout = stdout
+
+        expected = u"""digraph genealogy {
+    graph [charset="utf-8"];
+    node [shape=plaintext];
+    edge [style=bold];
+
+    30484 [label="Peter Chris Pappas \\nThe Pennsylvania State University (1982)"];
+
+}
+"""
+        self.assertEquals(stdout_intercept.getvalue().decode('utf-8'), expected)
+
 if __name__ == '__main__':
     unittest.main()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/geneagrapher.git



More information about the debian-science-commits mailing list