code: def test_dag():
      r"""
      1 --- 4
      2 --- 6
        \  /
         5
       /
      3
      """
  
      dag = DAG()
      [dag.add_node(i) for i in range(1, 7)]
      dag.add_edge(1, 4)
      dag.add_edge(2, 6)
      dag.add_edge(2, 5)
      dag.add_edge(5, 6)
      dag.add_edge(3, 5)
  
      with pytest.raises(KeyError):
          dag.add_edge(1, 10)
      with pytest.raises(KeyError):
          dag.add_edge(10, 1)
  
      assert set(dag[2]) == {5, 6}
      assert list(dag.topological_iter()) == [3, 2, 5, 6, 1, 4]
  
      assert list(dag.dfs()) == [3, 2, 5, 6, 1, 4]
      assert list(dag.bfs()) == [1, 2, 3, 4, 5, 6]
  
      dag.add_edge(6, 1)
      dag.add_edge(1, 2)
  
      with pytest.raises(KeyError):
          for _ in dag.iter_predecessors(-1):
              pass
  
      with pytest.raises(KeyError):
          for _ in dag.iter_successors(-1):
              pass
  
      with pytest.raises(GraphContainsCycleError):
          _ = list(dag.topological_iter())
  
      dag.remove_edge(2, 5)
      assert dag.has_successor(2, 5) is False
      with pytest.raises(KeyError):
          dag.remove_edge(2, 5)
  
      rev_dag = dag.build_reversed()
      for n in dag:
          assert n in rev_dag
          assert (
              all(
                  rev_dag.has_successor(n, pred)
                  for pred in dag.predecessors(n)
              )
              is True
          )
  
      undigraph = dag.build_undirected()
      for n in dag:
          assert n in undigraph
          assert (
              all(
                  undigraph.has_predecessor(pred, n)
                  for pred in dag.predecessors(n)
              )
              is True
          )
          assert (
              all(
                  undigraph.has_successor(n, pred)
                  for pred in dag.predecessors(n)
              )
              is True
          )
  
      dag_copy = dag.copy()
      for n in dag:
          assert n in dag_copy
          assert (
              all(
                  dag_copy.has_successor(pred, n)
                  for pred in dag_copy.predecessors(n)
              )
              is True
          )
  
file_path: mars/mars/core/graph/tests/test_graph.py
imports: import pytest
  import numpy
  from ....None import dataframe
  from ....None import tensor
  from ....tests.core import flaky
  from ....utils import to_str
  from ..None import DAG, GraphContainsCycleError
label_desc: Use image node_id 1 for calling a global function with example usage: test_dag() without return types