test_reciprocity.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import pytest
  2. import networkx as nx
  3. class TestReciprocity:
  4. # test overall reicprocity by passing whole graph
  5. def test_reciprocity_digraph(self):
  6. DG = nx.DiGraph([(1, 2), (2, 1)])
  7. reciprocity = nx.reciprocity(DG)
  8. assert reciprocity == 1.0
  9. # test empty graph's overall reciprocity which will throw an error
  10. def test_overall_reciprocity_empty_graph(self):
  11. with pytest.raises(nx.NetworkXError):
  12. DG = nx.DiGraph()
  13. nx.overall_reciprocity(DG)
  14. # test for reciprocity for a list of nodes
  15. def test_reciprocity_graph_nodes(self):
  16. DG = nx.DiGraph([(1, 2), (2, 3), (3, 2)])
  17. reciprocity = nx.reciprocity(DG, [1, 2])
  18. expected_reciprocity = {1: 0.0, 2: 0.6666666666666666}
  19. assert reciprocity == expected_reciprocity
  20. # test for reciprocity for a single node
  21. def test_reciprocity_graph_node(self):
  22. DG = nx.DiGraph([(1, 2), (2, 3), (3, 2)])
  23. reciprocity = nx.reciprocity(DG, 2)
  24. assert reciprocity == 0.6666666666666666
  25. # test for reciprocity for an isolated node
  26. def test_reciprocity_graph_isolated_nodes(self):
  27. with pytest.raises(nx.NetworkXError):
  28. DG = nx.DiGraph([(1, 2)])
  29. DG.add_node(4)
  30. nx.reciprocity(DG, 4)