def test_graph_with_one_node_returns_only_one_node(self) -> None: """Test if empty graph with one node returns it correctly.""" node = _get_random_node() graph = Graph() graph.add_node(node) self.assertEqual([node], graph.nodes)
def test_adding_edge_when_no_nodes_does_not_add_an_adge(self) -> None: """Test if adding an edge to empty graph fails as expected.""" source_id = _get_random_string() target_id = _get_random_string() graph = Graph() graph.add_edge(source_id=source_id, target_id=target_id) self.assertEqual([], graph.edges)
def test_adding_edge_between_nodes_works(self) -> None: """Test adding an edge.""" source = _get_random_node() target = _get_random_node() graph = Graph() graph.add_node(source) graph.add_node(target) graph.add_edge(source_id=source.id, target_id=target.id) edges = graph.edges self.assertEqual(1, len(edges)) edge = edges[0] self.assertEqual(source, edge._source) self.assertEqual(target, edge._target)
def test_collapsing_all_groups(self) -> None: """Test that no collapsing will happen when all groups are expanded.""" source = self._get_source_graph() expected = Graph() expected.add_node(_create_simple_node("input", [])) expected.add_node(_create_group_node("a")) expected.add_node(_create_simple_node("e", ["e"])) expected.add_node(_create_simple_node("output", [])) expected.add_edge("input", "node_group_a") expected.add_edge("node_group_a", "output") expected.add_edge("input", "e") expected.add_edge("e", "output") collapser = Collapser([]) collapsed = collapser.collapse(graph=source) self.assertEqual(expected, collapsed)
def _get_source_graph(self) -> Graph: """Return a graph with a well-known contents.""" graph = Graph() graph.add_node(_create_simple_node("input", [])) graph.add_node(_create_simple_node("a:b", ["a"])) graph.add_node(_create_simple_node("a:b:c1", ["a", "a:b"])) graph.add_node(_create_simple_node("a:b:c2", ["a", "a:b"])) graph.add_node(_create_simple_node("a:b:c:d", ["a", "a:b", "a:b:c"])) graph.add_node(_create_simple_node("e", ["e"])) graph.add_node(_create_simple_node("output", [])) graph.add_edge("input", "a:b") graph.add_edge("a:b", "a:b:c1") graph.add_edge("a:b", "a:b:c2") graph.add_edge("a:b:c1", "a:b:c:d") graph.add_edge("a:b:c2", "a:b:c:d") graph.add_edge("a:b:c:d", "output") graph.add_edge("input", "e") graph.add_edge("e", "output") return graph
def test_comparing_to_something_else_fails(self) -> None: """Test if comparing Graph to something other fails.""" with self.assertRaises(NotImplementedError): Graph() == 1
def test_empty_graph_has_no_edges(self) -> None: """Test if empty graph has no edges.""" graph = Graph() self.assertEqual([], graph.edges)