[geneagrapher] 03/226: Small changes and unit tests for the GGraph.Node class. This resolves ticket #4.

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 94f03b62b76900216967ccef0adb95a6fee7c11c
Author: David Alber <alber.david at gmail.com>
Date:   Sun Apr 6 04:18:31 2008 +0000

    Small changes and unit tests for the GGraph.Node class. This resolves ticket #4.
---
 src/GGraph.py | 43 +++++++++++++++++++++++-------
 src/tests.py  | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 118 insertions(+), 11 deletions(-)

diff --git a/src/GGraph.py b/src/GGraph.py
index cbdfc06..c943f4e 100644
--- a/src/GGraph.py
+++ b/src/GGraph.py
@@ -4,7 +4,7 @@ class Record:
     """
     def __init__(self, name, institution, year, id):
         """
-        Construct a Record object.
+        Record class constructor.
         
         Parameters:
             name: string containing mathematician's name
@@ -19,13 +19,13 @@ class Record:
         self.id = id
         
         # Verify we got the types wanted.
-        if type(self.name) is not type('str'):
+        if not isinstance(self.name, str):
             raise TypeError("Unexpected parameter type: expected string value for 'name'")
-        if type(self.institution) is not type('str'):
+        if not isinstance(self.institution, str):
             raise TypeError("Unexpected parameter type: expected string value for 'institution'")
-        if type(self.year) is not type(1):
+        if not isinstance(self.year, int):
             raise TypeError("Unexpected parameter type: expected integer value for 'year'")
-        if type(self.id) is not type(1):
+        if not isinstance(self.id, int):
             raise TypeError("Unexpected parameter type: expected integer value for 'id'")
 
     def __cmp__(self, r2):
@@ -48,20 +48,36 @@ class Record:
     
 
 class Node:
-    "Node in the graph."
+    """
+    Container class storing a node in the graph.
+    """
     def __init__(self, record, ancestors):
+        """
+        Node class constructor.
+        
+        Parameters:
+            record: instance of the Record class
+            ancestors: list of Node objects containing this node's genealogical ancestors
+        """
+        
         self.record = record
         self.ancestors = ancestors
         self.already_printed = False
 
+        # Verify parameter types.
+        if not isinstance(self.record, Record):
+            raise TypeError("Unexpected parameter type: expected Record object for 'record'")
+        if not isinstance(self.ancestors, list):
+            raise TypeError("Unexpected parameter type: expected list object for 'ancestors'")
+        
     def __str__(self):
-        if self.record.institution != '':
-            if self.record.year > -1:
+        if self.record.hasInstitution():
+            if self.record.hasYear():
                 return self.record.name.encode('iso-8859-1', 'replace') + ' \\n' + self.record.institution.encode('iso-8859-1', 'replace') + ' (' + str(self.record.year) + ')'
             else:
                 return self.record.name.encode('iso-8859-1', 'replace') + ' \\n' + self.record.institution.encode('iso-8859-1', 'replace')
         else:
-            if self.record.year > -1:
+            if self.record.hasYear():
                 return self.record.name.encode('iso-8859-1', 'replace') + ' \\n(' + str(self.record.year) + ')'
             else:
                 return self.record.name.encode('iso-8859-1', 'replace')
@@ -70,9 +86,18 @@ class Node:
         return self.record.__cmp__(n2.record)
 
     def addAncestor(self, ancestor):
+        """
+        Append an ancestor id to the ancestor list.
+        """
+        # Verify we were passed an int.
+        if not isinstance(ancestor, int):
+            raise TypeError("Unexpected parameter type: expected int for 'ancestor'")
         self.ancestors.append(ancestor)
 
     def id(self):
+        """
+        Accessor method to retrieve the id of this node's record.
+        """
         return self.record.id
 
 
diff --git a/src/tests.py b/src/tests.py
index 7e54180..51e1840 100644
--- a/src/tests.py
+++ b/src/tests.py
@@ -6,9 +6,8 @@ class TestRecordMethods(unittest.TestCase):
     """
     Unit tests for the GGraph.Record class.
     """
-
     def test001_init(self):
-        # Test constructor.
+        # Test the constructor.
         record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
         self.assertEqual(record.name, "Carl Friedrich Gauss")
         self.assertEqual(record.institution, "Universitaet Helmstedt")
@@ -64,7 +63,90 @@ class TestRecordMethods(unittest.TestCase):
         # Verify hasYear() method returns False when the conditions are right.
         record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", -1, 18231)
         self.assert_(not record.hasYear())
+
+class TestNodeMethods(unittest.TestCase):
+    """
+    Unit tests for the GGraph.Node class.
+    """
+    def setUp(self):
+        self.record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+    
+    def test001_init(self):
+        # Test the constructor.
+        node = GGraph.Node(self.record, [])
+        self.assertEquals(node.record, self.record)
+        self.assertEquals(node.ancestors, [])
+        
+    def test002_init_bad_record(self):
+        # Test the constructor for a case where the record passed is not a Record
+        # object.
+        self.assertRaises(TypeError, GGraph.Node, 1, [])
+        
+    def test003_init_bad_ancestor_list(self):
+        # Test the constructor for a case where the ancestor list is not a list.
+        self.assertRaises(TypeError, GGraph.Node, self.record, 1)
+        
+    def test004_str_full(self):
+        # Test __str__() method for Node with complete record.
+        node = GGraph.Node(self.record, [])
+        nodestr = node.__str__()
+        nodestrexpt = "Carl Friedrich Gauss \\nUniversitaet Helmstedt (1799)"
+        self.assertEquals(nodestr, nodestrexpt)
+
+    def test005_str_no_year(self):
+        # Test __str__() method for Node containing record without year.
+        record = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", -1, 18231)
+        node = GGraph.Node(record, [])
+        nodestr = node.__str__()
+        nodestrexpt = "Carl Friedrich Gauss \\nUniversitaet Helmstedt"
+        self.assertEquals(nodestr, nodestrexpt)
+
+    def test006_str_no_inst(self):
+        # Test __str__() method for Node containing record without institution.
+        record = GGraph.Record("Carl Friedrich Gauss", "", 1799, 18231)
+        node = GGraph.Node(record, [])
+        nodestr = node.__str__()
+        nodestrexpt = "Carl Friedrich Gauss \\n(1799)"
+        self.assertEquals(nodestr, nodestrexpt)
+
+    def test007_str_no_inst_no_id(self):
+        # Test __str__() method for Node containing record without institution
+        # or year.
+        record = GGraph.Record("Carl Friedrich Gauss", "", -1, 18231)
+        node = GGraph.Node(record, [])
+        nodestr = node.__str__()
+        nodestrexpt = "Carl Friedrich Gauss"
+        self.assertEquals(nodestr, nodestrexpt)
+
+    def test008_cmp_equal(self):
+        # Test comparison method for Nodes with identical records.
+        record2 = GGraph.Record("Carl Friedrich Gauss", "Universitaet Helmstedt", 1799, 18231)
+        node1 = GGraph.Node(self.record, [])
+        node2 = GGraph.Node(record2, [])
+        self.assert_(node1 == node2)
+
+    def test009_cmp_unequal(self):
+        # Test comparison method for Nodes with different records.
+        record2 = GGraph.Record("Leonhard Euler", "Universitaet Basel", 1726, 38586)
+        node1 = GGraph.Node(self.record, [])
+        node2 = GGraph.Node(record2, [])
+        self.assert_(node1 < node2)
+
+    def test010_add_ancestor(self):
+        # Test the addAncestor() method.
+        node = GGraph.Node(self.record, [])
+        node.addAncestor(5)
+        self.assertEquals(node.ancestors, [5])
+
+    def test011_add_ancestor_bad_type(self):
+        # Test the addAncestor() method for a case where the parameter type is incorrect.
+        node = GGraph.Node(self.record, [])
+        self.assertRaises(TypeError, node.addAncestor, '5')
         
+    def test012_get_id(self):
+        node = GGraph.Node(self.record, [])
+        self.assertEquals(node.id(), 18231)
+
 
 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