test_graph.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_ip_union(self, trp):
  76. """
  77. Test graph in-place 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_addition(self, trp):
  88. """
  89. Test graph addition.
  90. """
  91. gr1 = SimpleGraph()
  92. gr2 = SimpleGraph()
  93. gr1.add(trp[0:3])
  94. gr2.add(trp[2:6])
  95. gr3 = gr1 + gr2
  96. assert len(gr3) == 5
  97. assert trp[0] in gr3
  98. assert trp[4] in gr3
  99. def test_ip_addition(self, trp):
  100. """
  101. Test graph in-place addition.
  102. """
  103. gr1 = SimpleGraph()
  104. gr2 = SimpleGraph()
  105. gr1.add(trp[0:3])
  106. gr2.add(trp[2:6])
  107. gr1 += gr2
  108. assert len(gr1) == 5
  109. assert trp[0] in gr1
  110. assert trp[4] in gr1
  111. def test_subtraction(self, trp):
  112. """
  113. Test graph addition.
  114. """
  115. gr1 = SimpleGraph()
  116. gr2 = SimpleGraph()
  117. gr1.add(trp[0:4])
  118. gr2.add(trp[2:6])
  119. gr3 = gr1 - gr2
  120. assert len(gr3) == 1
  121. assert trp[0] in gr3
  122. assert trp[1] in gr3
  123. assert trp[2] not in gr3
  124. assert trp[3] not in gr3
  125. assert trp[4] not in gr3
  126. gr3 = gr2 - gr1
  127. assert len(gr3) == 2
  128. assert trp[0] not in gr3
  129. assert trp[1] not in gr3
  130. assert trp[2] not in gr3
  131. assert trp[3] not in gr3
  132. assert trp[4] in gr3
  133. assert trp[5] in gr3
  134. def test_ip_subtraction(self, trp):
  135. """
  136. Test graph in-place addition.
  137. """
  138. gr1 = SimpleGraph()
  139. gr2 = SimpleGraph()
  140. gr1.add(trp[0:4])
  141. gr2.add(trp[2:6])
  142. gr1 -= gr2
  143. assert len(gr1) == 1
  144. assert trp[0] in gr1
  145. assert trp[1] in gr1
  146. assert trp[2] not in gr1
  147. assert trp[3] not in gr1
  148. assert trp[4] not in gr1
  149. def test_intersect(self, trp):
  150. """
  151. Test graph intersextion.
  152. """
  153. gr1 = SimpleGraph()
  154. gr2 = SimpleGraph()
  155. gr1.add(trp[0:4])
  156. gr2.add(trp[2:6])
  157. gr3 = gr1 & gr2
  158. assert len(gr3) == 2
  159. assert trp[2] in gr3
  160. assert trp[3] in gr3
  161. assert trp[0] not in gr3
  162. assert trp[5] not in gr3
  163. def test_ip_intersect(self, trp):
  164. """
  165. Test graph intersextion.
  166. """
  167. gr1 = SimpleGraph()
  168. gr2 = SimpleGraph()
  169. gr1.add(trp[0:4])
  170. gr2.add(trp[2:6])
  171. gr1 &= gr2
  172. assert len(gr1) == 2
  173. assert trp[2] in gr1
  174. assert trp[3] in gr1
  175. assert trp[0] not in gr1
  176. assert trp[5] not in gr1
  177. def test_xor(self, trp):
  178. """
  179. Test graph intersextion.
  180. """
  181. gr1 = SimpleGraph()
  182. gr2 = SimpleGraph()
  183. gr1.add(trp[0:4])
  184. gr2.add(trp[2:6])
  185. gr3 = gr1 ^ gr2
  186. assert len(gr3) == 3
  187. assert trp[2] not in gr3
  188. assert trp[3] not in gr3
  189. assert trp[0] in gr3
  190. assert trp[5] in gr3
  191. def test_ip_xor(self, trp):
  192. """
  193. Test graph intersextion.
  194. """
  195. gr1 = SimpleGraph()
  196. gr2 = SimpleGraph()
  197. gr1.add(trp[0:4])
  198. gr2.add(trp[2:6])
  199. gr1 ^= gr2
  200. assert len(gr1) == 3
  201. assert trp[2] not in gr1
  202. assert trp[3] not in gr1
  203. assert trp[0] in gr1
  204. assert trp[5] in gr1