[geneagrapher] 04/226: Built unit tests. This resolves ticket #5.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Sat Jul 11 17:10:30 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 b2a35c0f813af4d60ac7e51c4972bfee75d22c22
Author: David Alber <alber.david at gmail.com>
Date:   Tue Apr 8 05:26:27 2008 +0000

    Built unit tests. This resolves ticket #5.
---
 src/GGraph.py | 109 +++++++++++++++++++++++++++++++++++++++----------------
 src/tests.py  | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 192 insertions(+), 30 deletions(-)

diff --git a/src/GGraph.py b/src/GGraph.py
index c943f4e..a0fe54f 100644
--- a/src/GGraph.py
+++ b/src/GGraph.py
@@ -1,3 +1,9 @@
+class DuplicateNodeError(Exception):
+    def __init__(self, value):
+        self.value = value
+        def __str__(self):
+            return repr(self.value)
+
 class Record:
     """
     Container class storing record of a mathematician in the graph.
@@ -8,10 +14,10 @@ class Record:
         
         Parameters:
             name: string containing mathematician's name
-            institution: string containing mathematician's instituion (empty if
-                none)
+            institution: string containing mathematician's institution
+                (empty if none)
             year: integer containing year degree was earned
-            id: integer containing Math Genealogy Project id value.
+            id: integer containing Math Genealogy Project id value
         """
         self.name = name
         self.institution = institution
@@ -36,13 +42,15 @@ class Record:
     
     def hasInstitution(self):
         """
-        Return True if this record has an institution associated with it, else False.
+        Return True if this record has an institution associated with it,
+        else False.
         """
         return self.institution != ""
     
     def hasYear(self):
         """
-        Return True if this record has a year associated with it, else False.
+        Return True if this record has a year associated with it, else
+        False.
         """
         return self.year != -1
     
@@ -57,7 +65,8 @@ class Node:
         
         Parameters:
             record: instance of the Record class
-            ancestors: list of Node objects containing this node's genealogical ancestors
+            ancestors: list of Node objects containing this node's
+                genealogical ancestors
         """
         
         self.record = record
@@ -102,37 +111,81 @@ class Node:
 
 
 class Graph:
-    def __init__(self, head, displayHead=True):
+    """
+    Class storing the representation of a genealogy graph.
+    """
+    def __init__(self, head=None):
+        """
+        Graph class constructor.
+        
+        Parameters:
+            head: a Node object representing the tree head (can be
+                omitted to create an empty graph)
+        """
         self.head = head
-        self.nodes = {head.id(): head}
-        self.displayHead = displayHead
         
+        # Verify type of head is what we expect.
+        if self.head is not None and not isinstance(self.head, Node):
+            raise TypeError("Unexpected parameter type: expected Node object for 'head'")
+
+        if self.head is not None:
+            self.nodes = {head.id(): head}
+        else:
+            self.nodes = {}
+
     def hasNode(self, id):
+        """
+        Check if the graph contains a node with the given id.
+        """
         return self.nodes.has_key(id)
 
     def getNode(self, id):
-        return self.nodes[id]
+        """
+        Return the node in the graph with given id. Returns
+        None if no such node exists.
+        """
+        if self.hasNode(id):
+            return self.nodes[id]
+        else:
+            return None
 
     def getNodeList(self):
+        """
+        Return a list of the nodes in the graph.
+        """
         return self.nodes.keys()
 
     def addNode(self, name, institution, year, id, ancestors):
-        record = Record(name, institution, year, id)
-        node = Node(record, ancestors)
-        self.nodes[id] = node
+        """
+        Add a new node to the graph if a matching node is not already
+        present.
+        """
+        if not self.hasNode(id):
+            record = Record(name, institution, year, id)
+            node = Node(record, ancestors)
+            self.nodes[id] = node
+            if self.head is None:
+                self.head = node
+        else:
+            msg = "node with id %d already exists" % (id)
+            raise DuplicateNodeError(msg)
 
     def generateDotFile(self):
-        if self.displayHead:
-            queue = [self.head.id()]
-        else:
-            queue = self.head.ancestors
+        """
+        Return a string that contains the content of the Graphviz dotfile
+        format for this graph.
+        """
+        if self.head is None:
+            return ""
+
+        queue = [self.head.id()]
         edges = ""
         dotfile = ""
         
         dotfile += """digraph genealogy {
-        graph [charset="iso-8859-1"];
-        node [shape=plaintext];
-	edge [style=bold];\n\n"""
+    graph [charset="iso-8859-1"];
+    node [shape=plaintext];
+    edge [style=bold];\n\n"""
 
         while len(queue) > 0:
             node_id = queue.pop()
@@ -147,22 +200,18 @@ class Graph:
             queue += node.ancestors
         
             # Print this node's information.
-            dotfile += "\t" + str(node_id) + " [label=\" " + str(node) + "\"];\n"
+            nodestr = "    %d [label=\"%s\"];" % (node_id, node)
+            dotfile += nodestr
 
             # Store the connection information for this node.
             for advisor in node.ancestors:
-                edges += "\n\t" + str(advisor) + " -> " + str(node_id) + ";"
+                edgestr = "\n    %s -> %d;" % (advisor, node_id)
+                edges += edgestr
+                
+            dotfile += "\n"
 
         # Now print the connections between the nodes.
         dotfile += edges
 
         dotfile += "\n}\n"
         return dotfile
-
-    def printDotFile(self):
-        print self.generateDotFile()
-
-    def writeDotFile(self, filename):
-        outfile = open(filename, 'w')
-        outfile.write(self.generateDotFile())
-        outfile.close()
diff --git a/src/tests.py b/src/tests.py
index 51e1840..8b66e52 100644
--- a/src/tests.py
+++ b/src/tests.py
@@ -147,6 +147,119 @@ class TestNodeMethods(unittest.TestCase):
         node = GGraph.Node(self.record, [])
         self.assertEquals(node.id(), 18231)
 
+class TestGraphMethods(unittest.TestCase):
+    """
+    Unit tests for the GGraph.Graph class.
+    """
+    def setUp(self):
+        self.record1 = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+        self.node1 = GGraph.Node(self.record1, [])
+        self.graph1 = GGraph.Graph(self.node1)
+    
+    def test001_init_empty(self):
+        # Test the constructor.
+        graph = GGraph.Graph()
+        self.assertEquals(graph.head, None)
+        
+    def test002_init(self):
+        # Test the constructor.
+        self.assert_(self.graph1.head == self.node1)
+        self.assertEquals(self.graph1.nodes.keys(), [18231])
+        self.assertEquals(self.graph1.nodes[18231], self.node1)
+        
+    def test003_init_bad_head(self):
+        # Test the constructor when passed a bad type for the head parameter.
+        self.assertRaises(TypeError, GGraph.Graph, 3)
+        
+    def test004_has_node_true(self):
+        # Test the hasNode() method for a True case.
+        self.assertEquals(self.graph1.hasNode(18231), True)
+        
+    def test005_has_node_false(self):
+        # Test the hasNode() method for a False case.
+        self.assertEquals(self.graph1.hasNode(1), False)
+        
+    def test006_get_node(self):
+        # Test the getNode() method.
+        node = self.graph1.getNode(18231)
+        self.assert_(node == self.node1)
+        
+    def test007_get_node_not_found(self):
+        # Test the getNode() method for a case where the node does not exist.
+        node = self.graph1.getNode(1)
+        self.assertEquals(node, None)
+        
+    def test008_get_node_list(self):
+        # Test the getNodeList() method.
+        self.assertEquals(self.graph1.getNodeList(), [18231])
+        
+    def test008_get_node_list_empty(self):
+        # Test the getNodeList() method for an empty graph.
+        graph = GGraph.Graph()
+        self.assertEquals(graph.getNodeList(), [])
+        
+    def test009_add_node(self):
+        # Test the addNode() method.
+        self.graph1.addNode("Leonhard Euler", "Universitaet Basel", 1726, 38586, [])
+        self.assertEquals([38586, 18231], self.graph1.getNodeList())
+
+    def test010_add_node_head(self):
+        # Test the addNode() method when no head exists.
+        graph = GGraph.Graph()
+        self.assertEquals(graph.head, None)
+        graph.addNode("Leonhard Euler", "Universitaet Basel", 1726, 38586, [])
+        self.assertEquals(graph.head, graph.getNode(38586))
+
+    def test011_add_node_already_present(self):
+        self.graph1.addNode("Leonhard Euler", "Universitaet Basel", 1726, 38586, [])
+        self.assertEquals([38586, 18231], self.graph1.getNodeList())
+        self.assertRaises(GGraph.DuplicateNodeError, self.graph1.addNode, "Leonhard Euler", "Universitaet Basel", 1726, 38586, [])
+
+    def test012_generate_dot_file(self):
+        # Test the generateDotFile() method.
+        dotfileexpt = """digraph genealogy {
+    graph [charset="iso-8859-1"];
+    node [shape=plaintext];
+    edge [style=bold];
+
+    18231 [label="Carl Friedrich Gauss \\nUniversitaet Helmstedt (1799)"];
+
+}
+"""    
+        dotfile = self.graph1.generateDotFile()
+        self.assertEquals(dotfile, dotfileexpt)
+        
+    def test013_generate_dot_file(self):
+        # Test the generateDotFile() method.
+        graph = GGraph.Graph()
+        graph.addNode("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231, [18230])
+        graph.addNode("Johann Friedrich Pfaff", "Georg-August-Universitaet Goettingen", 1786, 18230, [66476])
+        graph.addNode("Abraham Gotthelf Kaestner", "Universitaet Leipzig", 1739, 66476, [57670])
+        graph.addNode("Christian August Hausen", "Martin-Luther-Universitaet Halle-Wittenberg", 1713, 57670, [72669])
+        graph.addNode("Johann Christoph Wichmannshausen", "Universitaet Leipzig", 1685, 72669, [21235])
+        graph.addNode("Otto Mencke", "Universitaet Leipzig", 1665, 21235, [])
+        
+        dotfileexpt = """digraph genealogy {
+    graph [charset="iso-8859-1"];
+    node [shape=plaintext];
+    edge [style=bold];
+
+    18231 [label="Carl Friedrich Gauss \\nUniversitaet Helmstedt (1799)"];
+    18230 [label="Johann Friedrich Pfaff \\nGeorg-August-Universitaet Goettingen (1786)"];
+    66476 [label="Abraham Gotthelf Kaestner \\nUniversitaet Leipzig (1739)"];
+    57670 [label="Christian August Hausen \\nMartin-Luther-Universitaet Halle-Wittenberg (1713)"];
+    72669 [label="Johann Christoph Wichmannshausen \\nUniversitaet Leipzig (1685)"];
+    21235 [label="Otto Mencke \\nUniversitaet Leipzig (1665)"];
+
+    18230 -> 18231;
+    66476 -> 18230;
+    57670 -> 66476;
+    72669 -> 57670;
+    21235 -> 72669;
+}
+"""
+        dotfile = graph.generateDotFile()
+        self.assertEquals(dotfile, dotfileexpt)
 
 if __name__ == '__main__':
     unittest.main()
\ No newline at end of file

-- 
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