test_graph.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import pytest
  2. from rdflib import Graph, Namespace, URIRef
  3. from lakesuperior.model.graph.graph import SimpleGraph, Imr
  4. @pytest.fixture(scope='class')
  5. def trp():
  6. return (
  7. (URIRef('urn:s:0'), URIRef('urn:p:0'), URIRef('urn:o:0')),
  8. # Exact same as [0].
  9. (URIRef('urn:s:0'), URIRef('urn:p:0'), URIRef('urn:o:0')),
  10. # NOTE: s and o are in reversed order.
  11. (URIRef('urn:o:0'), URIRef('urn:p:0'), URIRef('urn:s:0')),
  12. (URIRef('urn:s:0'), URIRef('urn:p:1'), URIRef('urn:o:0')),
  13. (URIRef('urn:s:0'), URIRef('urn:p:1'), URIRef('urn:o:1')),
  14. (URIRef('urn:s:1'), URIRef('urn:p:1'), URIRef('urn:o:1')),
  15. (URIRef('urn:s:1'), URIRef('urn:p:2'), URIRef('urn:o:2')),
  16. )
  17. @pytest.mark.usefixtures('trp')
  18. class TestGraphOps:
  19. """
  20. Test various graph operations.
  21. """
  22. def test_len(self, trp):
  23. """
  24. Test the length of a graph with and without duplicates.
  25. """
  26. gr = SimpleGraph()
  27. assert len(gr) == 0
  28. gr.add((trp[0],))
  29. assert len(gr) == 1
  30. gr.add((trp[1],)) # Same values
  31. assert len(gr) == 1
  32. gr.add((trp[2],))
  33. assert len(gr) == 2
  34. gr.add(trp)
  35. assert len(gr) == 6
  36. def test_dup(self, trp):
  37. """
  38. Test operations with duplicate triples.
  39. """
  40. gr = SimpleGraph()
  41. #import pdb; pdb.set_trace()
  42. gr.add((trp[0],))
  43. assert trp[1] in gr
  44. assert trp[2] not in gr
  45. def test_remove(self, trp):
  46. """
  47. Test adding and removing triples.
  48. """
  49. gr = SimpleGraph()
  50. gr.add(trp)
  51. gr.remove(trp[0])
  52. assert len(gr) == 5
  53. assert trp[0] not in gr
  54. assert trp[1] not in gr
  55. # This is the duplicate triple.
  56. gr.remove(trp[1])
  57. assert len(gr) == 5
  58. # This is the triple in reverse order.
  59. gr.remove(trp[2])
  60. assert len(gr) == 4
  61. gr.remove(trp[4])
  62. assert len(gr) == 3
  63. def test_union(self, trp):
  64. """
  65. Test graph union.
  66. """
  67. gr1 = SimpleGraph()
  68. gr2 = SimpleGraph()
  69. gr1.add(trp[0:3])
  70. gr2.add(trp[2:6])
  71. gr3 = gr1 | gr2
  72. assert len(gr3) == 5
  73. assert trp[0] in gr3
  74. assert trp[4] in gr3
  75. def test_iunion(self, trp):
  76. """
  77. Test graph union.
  78. """
  79. gr1 = SimpleGraph()
  80. gr2 = SimpleGraph()
  81. gr1.add(trp[0:3])
  82. gr2.add(trp[2:6])
  83. gr1 |= gr2
  84. assert len(gr1) == 5
  85. assert trp[0] in gr1
  86. assert trp[4] in gr1
  87. def test_intersect(self, trp):
  88. """
  89. Test graph union.
  90. """
  91. gr1 = SimpleGraph()
  92. gr2 = SimpleGraph()
  93. gr1.add(trp[0:4])
  94. gr2.add(trp[2:6])
  95. gr3 = gr1 & gr2
  96. assert len(gr3) == 2
  97. assert trp[2] in gr3
  98. assert trp[3] in gr3
  99. assert trp[0] not in gr3
  100. assert trp[5] not in gr3