Commit eb82afea authored by Temujin's avatar Temujin

additional tests, required docs

parent 5970f441
...@@ -15,11 +15,10 @@ class FamilyTree: ...@@ -15,11 +15,10 @@ class FamilyTree:
""" """
Tree setup procedure: Tree setup procedure:
1 get all the nodes and edges, check if they're valid. 1 get all the nodes and edges
2 Make sure there's only one root. 2 Find and set the root
3 Find and set the root 3 run set_generations() method
5 run set_generations() method 4 done
6 done
""" """
ordinal = ["first", "second", "third", "fourth", "fifth", ordinal = ["first", "second", "third", "fourth", "fifth",
...@@ -361,6 +360,7 @@ def savefile(filename, tree): ...@@ -361,6 +360,7 @@ def savefile(filename, tree):
if x is not tree.root: if x is not tree.root:
parent = x.get_parent() parent = x.get_parent()
saveF.write("child " + parent.get_name() + " " + x.get_name() + "\n") saveF.write("child " + parent.get_name() + " " + x.get_name() + "\n")
saveF.close()
def command_line(): def command_line():
...@@ -450,19 +450,5 @@ def command_line(): ...@@ -450,19 +450,5 @@ def command_line():
print("Error: no such command") print("Error: no such command")
def main():
# test for opening and saving files
ntree = openfile("chandelier")
ntree.find_root()
ntree.set_generations()
print("The root of the tree is {}.".format(ntree.root))
for key, node in ntree.family_tree.items():
print("{}: {}".format(node, node.get_generation()))
savefile("test", ntree)
command_line()
if __name__ == "__main__": if __name__ == "__main__":
main() command_line()
"""Unittest for family_tree.""" """Unittest for family_tree."""
import sys import sys
import os import os
sys.path.insert(0, os.path.abspath( sys.path.insert(0, os.path.abspath(
...@@ -8,6 +7,36 @@ import family_tree as f ...@@ -8,6 +7,36 @@ import family_tree as f
import unittest import unittest
savefile_content = """
family test_save
count 6
name A
age 69
sex True
name B
age 69
sex True
name C
age 69
sex False
name D
age 69
sex True
name E
age 666
sex False
name F
age 420
sex False
links 5
child A B
child A C
child B D
child B E
child D F
""".strip()
class TestFamilyTree(unittest.TestCase): class TestFamilyTree(unittest.TestCase):
"""For testing family tree.""" """For testing family tree."""
...@@ -94,7 +123,7 @@ class TestFamilyTree(unittest.TestCase): ...@@ -94,7 +123,7 @@ class TestFamilyTree(unittest.TestCase):
self.assertEqual(self.ftree.relationship(A, D), "grandson") self.assertEqual(self.ftree.relationship(A, D), "grandson")
self.assertEqual(self.ftree.relationship(D, A), "grandfather") self.assertEqual(self.ftree.relationship(D, A), "grandfather")
# great grandparent/child # great grand parent/child
self.assertEqual(self.ftree.relationship(F, A), "greatgrandfather") self.assertEqual(self.ftree.relationship(F, A), "greatgrandfather")
self.assertEqual(self.ftree.relationship(A, F), "greatgranddaughter") self.assertEqual(self.ftree.relationship(A, F), "greatgranddaughter")
...@@ -111,6 +140,20 @@ class TestFamilyTree(unittest.TestCase): ...@@ -111,6 +140,20 @@ class TestFamilyTree(unittest.TestCase):
self.assertEqual(self.ftree.relationship(C, F), "grandniece") self.assertEqual(self.ftree.relationship(C, F), "grandniece")
self.assertEqual(self.ftree.relationship(F, C), "grandaunt") self.assertEqual(self.ftree.relationship(F, C), "grandaunt")
def test_save(self):
f.savefile("test_save", self.ftree)
savefile = open("test_save.ft", "r")
content = "".join(savefile.readlines()).strip()
self.assertEqual(content, savefile_content)
savefile.close()
def test_load(self):
tree = f.openfile("../chandelier")
self.assertFalse(len(tree.family_tree) == 0)
for k, v in tree.family_tree.items():
self.assertFalse(k is None)
self.assertFalse(v is None)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
family test_save
count 6
name A
age 69
sex True
name B
age 69
sex True
name C
age 69
sex False
name D
age 69
sex True
name E
age 666
sex False
name F
age 420
sex False
links 5
child A B
child A C
child B D
child B E
child D F
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment