1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
# Copyright 2001 by Tarjei Mikkelsen. All rights reserved.
# Revisions copyright 2018 by Maximilian Greil. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
# python unittest framework
"""Tests for Pathway module."""
import unittest
# modules to be tested
from Bio.Pathway import Reaction
from Bio.Pathway.Rep.Graph import Graph
from Bio.Pathway.Rep.MultiGraph import MultiGraph
class GraphTestCase(unittest.TestCase):
def test_Equals(self):
a = Graph(["a", "b", "c"])
a.add_edge("a", "b", "label1")
a.add_edge("b", "c", "label1")
a.add_edge("b", "a", "label2")
b = Graph(["a", "b", "c"])
self.assertNotEqual(a, b, "equal to similar nodes, no edges")
b.add_edge("a", "b", "label1")
self.assertNotEqual(a, b, "equal to similar nodes, edge subset")
b.add_edge("b", "c", "label1")
b.add_edge("b", "a", "label2")
self.assertEqual(a, b, "not equal to similar")
c = Graph(["a", "b", "c"])
c.add_edge("a", "b", "label2")
c.add_edge("b", "c", "label2")
c.add_edge("b", "a", "label1")
self.assertNotEqual(a, c, "equal to similar with different labels")
self.assertNotEqual(c, Graph(), "equal to empty graph")
self.assertEqual(Graph(), Graph(), "empty graph not equal to self")
def test_Nodes(self):
a = Graph()
self.assertEqual(a.nodes(), [], "default graph not empty")
a.add_node("a")
self.assertEqual(a.nodes(), ["a"], "one node not added")
a.add_node("a")
self.assertEqual(a.nodes(), ["a"], "duplicate node added")
a.add_node("b")
self.assertEqual(sorted(a.nodes()), ["a", "b"], "second node not added")
def test_Edges(self):
a = Graph(["a", "b", "c", "d"])
a.add_edge("a", "b", "label1")
self.assertEqual(a.child_edges("a"), [("b", "label1")])
a.add_edge("b", "a", "label2")
self.assertEqual(a.parent_edges("a"), [("b", "label2")])
a.add_edge("b", "c", "label3")
self.assertEqual(a.parent_edges("c"), [("b", "label3")])
self.assertEqual(sorted(a.children("b")), ["a", "c"], "incorrect children")
self.assertEqual(a.children("d"), [], "incorrect children for singleton")
self.assertEqual(a.parents("a"), ["b"], "incorrect parents")
def test_RemoveNode(self):
a = Graph(["a", "b", "c", "d", "e"])
a.add_edge("a", "e", "label1")
a.add_edge("b", "e", "label1")
a.add_edge("c", "e", "label2")
a.add_edge("d", "e", "label3")
a.add_edge("e", "d", "label4")
a.add_edge("a", "b", "label5")
a.remove_node("e")
b = Graph(["a", "b", "c", "d"])
b.add_edge("a", "b", "label5")
self.assertEqual(a, b) # , "incorrect node removal")
def testAdditionalFunctions(self):
a = Graph(["a", "b", "c"])
a.add_edge("a", "b", "label1")
a.add_edge("b", "c", "label1")
a.add_edge("b", "a", "label2")
self.assertEqual(str(a), "<Graph: 3 node(s), 3 edge(s), 2 unique label(s)>")
self.assertEqual(
repr(a),
"<Graph: ('a': ('b', 'label1'))('b': ('a', 'label2'),('c', 'label1'))('c': )>",
)
self.assertListEqual(a.edges("label1"), [("a", "b"), ("b", "c")])
self.assertListEqual(a.labels(), ["label1", "label2"])
class MultiGraphTestCase(unittest.TestCase):
def test_Equals(self):
a = MultiGraph(["a", "b", "c"])
a.add_edge("a", "b", "label1")
a.add_edge("b", "c", "label1")
a.add_edge("b", "a", "label2")
b = MultiGraph(["a", "b", "c"])
self.assertNotEqual(a, b, "equal to similar nodes, no edges")
b.add_edge("a", "b", "label1")
self.assertNotEqual(a, b, "equal to similar nodes, edge subset")
b.add_edge("b", "c", "label1")
b.add_edge("b", "a", "label2")
self.assertEqual(a, b, "not equal to similar")
c = MultiGraph(["a", "b", "c"])
c.add_edge("a", "b", "label2")
c.add_edge("b", "c", "label2")
c.add_edge("b", "a", "label1")
self.assertNotEqual(a, c, "equal to similar with different labels")
self.assertNotEqual(c, MultiGraph(), "equal to empty graph")
self.assertEqual(MultiGraph(), MultiGraph(), "empty graph not equal to self")
def test_Nodes(self):
a = MultiGraph()
self.assertEqual(a.nodes(), [], "default graph not empty")
a.add_node("a")
self.assertEqual(a.nodes(), ["a"], "one node not added")
a.add_node("a")
self.assertEqual(a.nodes(), ["a"], "duplicate node added")
a.add_node("b")
self.assertEqual(sorted(a.nodes()), ["a", "b"], "second node not added")
def test_Edges(self):
a = MultiGraph(["a", "b", "c", "d"])
a.add_edge("a", "b", "label1")
self.assertEqual(a.child_edges("a"), [("b", "label1")])
a.add_edge("a", "b", "label2")
self.assertEqual(sorted(a.child_edges("a")), [("b", "label1"), ("b", "label2")])
a.add_edge("b", "a", "label2")
self.assertEqual(a.parent_edges("a"), [("b", "label2")])
a.add_edge("b", "c", "label3")
self.assertEqual(a.parent_edges("c"), [("b", "label3")])
children = a.children("b")
children.sort()
self.assertEqual(children, ["a", "c"], "incorrect children")
self.assertEqual(a.children("d"), [], "incorrect children for singleton")
self.assertEqual(a.parents("a"), ["b"], "incorrect parents")
def test_RemoveNode(self):
a = MultiGraph(["a", "b", "c", "d", "e"])
a.add_edge("a", "e", "label1")
self.assertEqual(
repr(a), "<MultiGraph: ('a': ('e', 'label1'))('b': )('c': )('d': )('e': )>"
)
a.add_edge("b", "e", "label1")
a.add_edge("c", "e", "label2")
a.add_edge("d", "e", "label3")
a.add_edge("e", "d", "label4")
a.add_edge("a", "b", "label5")
self.assertEqual(
repr(a),
"<MultiGraph: ('a': ('b', 'label5'),('e', 'label1'))('b': ('e', 'label1'))('c': ('e', 'label2'))('d': ('e', 'label3'))('e': ('d', 'label4'))>",
)
a.remove_node("e")
self.assertEqual(
repr(a), "<MultiGraph: ('a': ('b', 'label5'))('b': )('c': )('d': )>"
)
b = MultiGraph(["a", "b", "c", "d"])
b.add_edge("a", "b", "label5")
self.assertEqual(
repr(b), "<MultiGraph: ('a': ('b', 'label5'))('b': )('c': )('d': )>"
)
self.assertEqual(repr(a), repr(b))
self.assertEqual(a, b) # , "incorrect node removal")
def testAdditionalFunctions(self):
a = MultiGraph(["a", "b", "c"])
a.add_edge("a", "b", "label1")
a.add_edge("b", "c", "label1")
a.add_edge("b", "a", "label2")
self.assertEqual(
str(a), "<MultiGraph: 3 node(s), 3 edge(s), 2 unique label(s)>"
)
self.assertListEqual(a.edges("label1"), [("a", "b"), ("b", "c")])
self.assertListEqual(a.labels(), ["label1", "label2"])
class ReactionTestCase(unittest.TestCase):
def setUp(self):
self.r_empty = Reaction()
self.r_prod = Reaction({"a": 1})
self.r_dest = Reaction({"a": -1})
self.r_1 = Reaction({"a": -1, "b": 1})
self.r_1i = Reaction({"a": -1, "b": 1, "c": 0})
self.r_2 = Reaction({"b": -1, "c": 1})
self.r_3 = Reaction({"a": -1, "d": 2})
self.r_4 = Reaction({"c": -1, "d": -1, "a": 1, "e": 2})
def test_eq(self):
self.assertEqual(self.r_1, self.r_1i) # , "not equal to similar")
self.assertNotEqual(self.r_3, self.r_4) # , "equal to different")
def test_rev(self):
self.assertEqual(
self.r_empty.reverse(), self.r_empty, "empty reversed not empty"
)
self.assertEqual(
self.r_prod.reverse(), self.r_dest, "reversed reaction not equal to similar"
)
self.assertEqual(
self.r_4.reverse(),
Reaction({"c": 1, "d": 1, "a": -1, "e": -2}),
"reversed reaction not equal to similar",
)
self.assertEqual(
self.r_3.reverse().reverse(), self.r_3, "double reversal not identity"
)
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2)
unittest.main(testRunner=runner)
|