code: def test_query():
      def _test_one(g):
          assert g.num_nodes() == 10
          assert g.num_edges() == 20
  
          for i in range(10):
              assert g.has_nodes(i)
          assert not g.has_nodes(11)
          assert F.allclose(
              g.has_nodes([0, 2, 10, 11]), F.tensor([1, 1, 0, 0])
          )
  
          src, dst = edge_pair_input()
          for u, v in zip(src, dst):
              assert g.has_edges_between(u, v)
          assert not g.has_edges_between(0, 0)
          assert F.allclose(
              g.has_edges_between([0, 0, 3], [0, 9, 8]),
              F.tensor([0, 1, 1]),
          )
          assert set(F.asnumpy(g.predecessors(9))) == set([0, 5, 7, 4])
          assert set(F.asnumpy(g.successors(2))) == set([7, 3])
  
          assert g.edge_ids(4, 4) == 5
          assert F.allclose(
              g.edge_ids([4, 0], [4, 9]), F.tensor([5, 0])
          )
  
          src, dst = g.find_edges([3, 6, 5])
          assert F.allclose(src, F.tensor([5, 7, 4]))
          assert F.allclose(dst, F.tensor([9, 9, 4]))
  
          src, dst, eid = g.in_edges(9, form="all")
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(
              [(0, 9, 0), (5, 9, 3), (7, 9, 6), (4, 9, 7)]
          )
          src, dst, eid = g.in_edges(
              [9, 0, 8], form="all"
          )  # test node#0 has no in edges
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(
              [
                  (0, 9, 0),
                  (5, 9, 3),
                  (7, 9, 6),
                  (4, 9, 7),
                  (3, 8, 9),
                  (7, 8, 12),
              ]
          )
  
          src, dst, eid = g.out_edges(0, form="all")
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set([(0, 9, 0), (0, 6, 1), (0, 4, 4)])
          src, dst, eid = g.out_edges(
              [0, 4, 8], form="all"
          )  # test node#8 has no out edges
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(
              [
                  (0, 9, 0),
                  (0, 6, 1),
                  (0, 4, 4),
                  (4, 3, 2),
                  (4, 4, 5),
                  (4, 9, 7),
                  (4, 1, 8),
              ]
          )
  
          src, dst, eid = g.edges("all", "eid")
          t_src, t_dst = edge_pair_input()
          t_tup = list(zip(t_src, t_dst, list(range(20))))
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(t_tup)
          assert list(F.asnumpy(eid)) == list(range(20))
  
          src, dst, eid = g.edges("all", "srcdst")
          t_src, t_dst = edge_pair_input()
          t_tup = list(zip(t_src, t_dst, list(range(20))))
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(t_tup)
          assert list(F.asnumpy(src)) == sorted(list(F.asnumpy(src)))
  
          assert g.in_degrees(0) == 0
          assert g.in_degrees(9) == 4
          assert F.allclose(g.in_degrees([0, 9]), F.tensor([0, 4]))
          assert g.out_degrees(8) == 0
          assert g.out_degrees(9) == 1
          assert F.allclose(g.out_degrees([8, 9]), F.tensor([0, 1]))
  
          assert np.array_equal(
              F.sparse_to_numpy(g.adj_external(transpose=True)),
              scipy_coo_input().toarray().T,
          )
          assert np.array_equal(
              F.sparse_to_numpy(g.adj_external(transpose=False)),
              scipy_coo_input().toarray(),
          )
  
      def _test(g):
          # test twice to see whether the cached format works or not
          _test_one(g)
          _test_one(g)
  
      def _test_csr_one(g):
          assert g.num_nodes() == 10
          assert g.num_edges() == 20
  
          for i in range(10):
              assert g.has_nodes(i)
          assert not g.has_nodes(11)
          assert F.allclose(
              g.has_nodes([0, 2, 10, 11]), F.tensor([1, 1, 0, 0])
          )
  
          src, dst = edge_pair_input(sort=True)
          for u, v in zip(src, dst):
              assert g.has_edges_between(u, v)
          assert not g.has_edges_between(0, 0)
          assert F.allclose(
              g.has_edges_between([0, 0, 3], [0, 9, 8]),
              F.tensor([0, 1, 1]),
          )
          assert set(F.asnumpy(g.predecessors(9))) == set([0, 5, 7, 4])
          assert set(F.asnumpy(g.successors(2))) == set([7, 3])
  
          # src = [0 0 0 1 1 2 2 3 3 4 4 4 4 5 5 6 7 7 7 9]
          # dst = [4 6 9 3 5 3 7 5 8 1 3 4 9 1 9 6 2 8 9 2]
          # eid = [0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
          assert g.edge_ids(4, 4) == 11
          assert F.allclose(
              g.edge_ids([4, 0], [4, 9]), F.tensor([11, 2])
          )
  
          src, dst = g.find_edges([3, 6, 5])
          assert F.allclose(src, F.tensor([1, 2, 2]))
          assert F.allclose(dst, F.tensor([3, 7, 3]))
  
          src, dst, eid = g.in_edges(9, form="all")
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(
              [(0, 9, 2), (5, 9, 14), (7, 9, 18), (4, 9, 12)]
          )
          src, dst, eid = g.in_edges(
              [9, 0, 8], form="all"
          )  # test node#0 has no in edges
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(
              [
                  (0, 9, 2),
                  (5, 9, 14),
                  (7, 9, 18),
                  (4, 9, 12),
                  (3, 8, 8),
                  (7, 8, 17),
              ]
          )
  
          src, dst, eid = g.out_edges(0, form="all")
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set([(0, 9, 2), (0, 6, 1), (0, 4, 0)])
          src, dst, eid = g.out_edges(
              [0, 4, 8], form="all"
          )  # test node#8 has no out edges
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(
              [
                  (0, 9, 2),
                  (0, 6, 1),
                  (0, 4, 0),
                  (4, 3, 10),
                  (4, 4, 11),
                  (4, 9, 12),
                  (4, 1, 9),
              ]
          )
  
          src, dst, eid = g.edges("all", "eid")
          t_src, t_dst = edge_pair_input(sort=True)
          t_tup = list(zip(t_src, t_dst, list(range(20))))
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(t_tup)
          assert list(F.asnumpy(eid)) == list(range(20))
  
          src, dst, eid = g.edges("all", "srcdst")
          t_src, t_dst = edge_pair_input(sort=True)
          t_tup = list(zip(t_src, t_dst, list(range(20))))
          tup = list(
              zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid))
          )
          assert set(tup) == set(t_tup)
          assert list(F.asnumpy(src)) == sorted(list(F.asnumpy(src)))
  
          assert g.in_degrees(0) == 0
          assert g.in_degrees(9) == 4
          assert F.allclose(g.in_degrees([0, 9]), F.tensor([0, 4]))
          assert g.out_degrees(8) == 0
          assert g.out_degrees(9) == 1
          assert F.allclose(g.out_degrees([8, 9]), F.tensor([0, 1]))
  
          assert np.array_equal(
              F.sparse_to_numpy(g.adj_external(transpose=True)),
              scipy_coo_input().toarray().T,
          )
          assert np.array_equal(
              F.sparse_to_numpy(g.adj_external(transpose=False)),
              scipy_coo_input().toarray(),
          )
  
      def _test_csr(g):
          # test twice to see whether the cached format works or not
          _test_csr_one(g)
          _test_csr_one(g)
  
      def _test_edge_ids():
          g = gen_by_mutation()
          eids = g.edge_ids([4, 0], [4, 9])
          assert eids.shape[0] == 2
          eid = g.edge_ids(4, 4)
          assert isinstance(eid, numbers.Number)
          with pytest.raises(DGLError):
              eids = g.edge_ids([9, 0], [4, 9])
  
          with pytest.raises(DGLError):
              eid = g.edge_ids(4, 5)
  
          g.add_edges(0, 4)
          eids = g.edge_ids([0, 0], [4, 9])
          eid = g.edge_ids(0, 4)
  
      _test(gen_by_mutation())
      _test(dgl.graph(elist_input()))
      _test(dgl.from_scipy(scipy_coo_input()))
      _test_csr(dgl.from_scipy(scipy_csr_input()))
      _test_edge_ids()
  
imports: import math
  import numbers
  import backend
  import dgl
  import networkx
  import numpy
  import pytest
  import scipy.sparse
  from dgl import DGLError
label_desc: Use image node_id 7 for calling a global function with example usage: test_query() without return types