test_0_0_graph.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. import pdb
  2. import pytest
  3. from shutil import rmtree
  4. from rdflib import Graph, Namespace, URIRef
  5. from lakesuperior.model.rdf.graph import Graph
  6. from lakesuperior.store.ldp_rs.lmdb_store import LmdbStore
  7. @pytest.fixture(scope='class')
  8. def store():
  9. """
  10. Test LMDB store.
  11. This store has a different life cycle than the one used for tests in higher
  12. levels of the stack and is not bootstrapped (i.e. starts completely empty).
  13. """
  14. env_path = '/tmp/test_lmdbstore'
  15. # Remove previous test DBs
  16. rmtree(env_path, ignore_errors=True)
  17. store = LmdbStore(env_path)
  18. yield store
  19. store.close()
  20. store.destroy()
  21. @pytest.fixture(scope='class')
  22. def trp():
  23. return (
  24. (URIRef('urn:s:0'), URIRef('urn:p:0'), URIRef('urn:o:0')),
  25. # Exact same as [0].
  26. (URIRef('urn:s:0'), URIRef('urn:p:0'), URIRef('urn:o:0')),
  27. # NOTE: s and o are in reversed order.
  28. (URIRef('urn:o:0'), URIRef('urn:p:0'), URIRef('urn:s:0')),
  29. (URIRef('urn:s:0'), URIRef('urn:p:1'), URIRef('urn:o:0')),
  30. (URIRef('urn:s:0'), URIRef('urn:p:1'), URIRef('urn:o:1')),
  31. (URIRef('urn:s:1'), URIRef('urn:p:1'), URIRef('urn:o:1')),
  32. (URIRef('urn:s:1'), URIRef('urn:p:2'), URIRef('urn:o:2')),
  33. )
  34. @pytest.mark.usefixtures('trp')
  35. @pytest.mark.usefixtures('store')
  36. class TestGraphInit:
  37. """
  38. Test initialization of graphs with different base data sets.
  39. """
  40. def test_empty(self, store):
  41. """
  42. Test creation of an empty graph.
  43. """
  44. # No transaction needed to init an empty graph.
  45. gr = Graph(store)
  46. # len() should not need a DB transaction open.
  47. assert len(gr) == 0
  48. def test_init_triples(self, trp, store):
  49. """
  50. Test creation using a Python set.
  51. """
  52. with store.txn_ctx():
  53. gr = Graph(store, data=set(trp))
  54. assert len(gr) == 6
  55. for t in trp:
  56. assert t in gr
  57. @pytest.mark.usefixtures('trp')
  58. @pytest.mark.usefixtures('store')
  59. class TestGraphLookup:
  60. """
  61. Test triple lookup.
  62. """
  63. def test_lookup_all_unbound(self, trp, store):
  64. """
  65. Test lookup ? ? ? (all unbound)
  66. """
  67. with store.txn_ctx():
  68. gr = Graph(store, data=set(trp))
  69. flt_gr = gr.lookup((None, None, None))
  70. assert len(flt_gr) == 6
  71. assert trp[0] in flt_gr
  72. assert trp[2] in flt_gr
  73. assert trp[3] in flt_gr
  74. assert trp[4] in flt_gr
  75. assert trp[5] in flt_gr
  76. assert trp[6] in flt_gr
  77. def test_lookup_s(self, trp, store):
  78. """
  79. Test lookup s ? ?
  80. """
  81. with store.txn_ctx():
  82. gr = Graph(store, data=set(trp))
  83. flt_gr = gr.lookup((URIRef('urn:s:0'), None, None))
  84. assert len(flt_gr) == 3
  85. assert trp[0] in flt_gr
  86. assert trp[3] in flt_gr
  87. assert trp[4] in flt_gr
  88. assert trp[2] not in flt_gr
  89. assert trp[5] not in flt_gr
  90. assert trp[6] not in flt_gr
  91. # Test for empty results.
  92. empty_flt_gr = gr.lookup((URIRef('urn:s:8'), None, None))
  93. assert len(empty_flt_gr) == 0
  94. def test_lookup_p(self, trp, store):
  95. """
  96. Test lookup ? p ?
  97. """
  98. with store.txn_ctx():
  99. gr = Graph(store, data=set(trp))
  100. flt_gr = gr.lookup((None, URIRef('urn:p:0'), None))
  101. assert len(flt_gr) == 2
  102. assert trp[0] in flt_gr
  103. assert trp[2] in flt_gr
  104. assert trp[3] not in flt_gr
  105. assert trp[4] not in flt_gr
  106. assert trp[5] not in flt_gr
  107. assert trp[6] not in flt_gr
  108. # Test for empty results.
  109. empty_flt_gr = gr.lookup((None, URIRef('urn:p:8'), None))
  110. assert len(empty_flt_gr) == 0
  111. def test_lookup_o(self, trp, store):
  112. """
  113. Test lookup ? ? o
  114. """
  115. with store.txn_ctx():
  116. gr = Graph(store, data=set(trp))
  117. flt_gr = gr.lookup((None, None, URIRef('urn:o:1')))
  118. assert len(flt_gr) == 2
  119. assert trp[4] in flt_gr
  120. assert trp[5] in flt_gr
  121. assert trp[0] not in flt_gr
  122. assert trp[2] not in flt_gr
  123. assert trp[3] not in flt_gr
  124. assert trp[6] not in flt_gr
  125. # Test for empty results.
  126. empty_flt_gr = gr.lookup((None, None, URIRef('urn:o:8')))
  127. assert len(empty_flt_gr) == 0
  128. def test_lookup_sp(self, trp, store):
  129. """
  130. Test lookup s p ?
  131. """
  132. with store.txn_ctx():
  133. gr = Graph(store, data=set(trp))
  134. flt_gr = gr.lookup((URIRef('urn:s:0'), URIRef('urn:p:1'), None))
  135. assert len(flt_gr) == 2
  136. assert trp[3] in flt_gr
  137. assert trp[4] in flt_gr
  138. assert trp[0] not in flt_gr
  139. assert trp[2] not in flt_gr
  140. assert trp[5] not in flt_gr
  141. assert trp[6] not in flt_gr
  142. # Test for empty results.
  143. empty_flt_gr = gr.lookup((URIRef('urn:s:0'), URIRef('urn:p:2'), None))
  144. assert len(empty_flt_gr) == 0
  145. def test_lookup_so(self, trp, store):
  146. """
  147. Test lookup s ? o
  148. """
  149. with store.txn_ctx():
  150. gr = Graph(store, data=set(trp))
  151. flt_gr = gr.lookup((URIRef('urn:s:0'), None, URIRef('urn:o:0')))
  152. assert len(flt_gr) == 2
  153. assert trp[0] in flt_gr
  154. assert trp[3] in flt_gr
  155. assert trp[2] not in flt_gr
  156. assert trp[4] not in flt_gr
  157. assert trp[5] not in flt_gr
  158. assert trp[6] not in flt_gr
  159. # Test for empty results.
  160. empty_flt_gr = gr.lookup((URIRef('urn:s:0'), None, URIRef('urn:o:2')))
  161. assert len(empty_flt_gr) == 0
  162. def test_lookup_po(self, trp, store):
  163. """
  164. Test lookup ? p o
  165. """
  166. with store.txn_ctx():
  167. gr = Graph(store, data=set(trp))
  168. flt_gr = gr.lookup((None, URIRef('urn:p:1'), URIRef('urn:o:1')))
  169. assert len(flt_gr) == 2
  170. assert trp[4] in flt_gr
  171. assert trp[5] in flt_gr
  172. assert trp[0] not in flt_gr
  173. assert trp[2] not in flt_gr
  174. assert trp[3] not in flt_gr
  175. assert trp[6] not in flt_gr
  176. # Test for empty results.
  177. empty_flt_gr = gr.lookup((None, URIRef('urn:p:1'), URIRef('urn:o:2')))
  178. assert len(empty_flt_gr) == 0
  179. def test_lookup_spo(self, trp, store):
  180. """
  181. Test lookup s p o
  182. """
  183. with store.txn_ctx():
  184. gr = Graph(store, data=set(trp))
  185. flt_gr = gr.lookup(
  186. (URIRef('urn:s:1'), URIRef('urn:p:1'), URIRef('urn:o:1'))
  187. )
  188. assert len(flt_gr) == 1
  189. assert trp[5] in flt_gr
  190. assert trp[0] not in flt_gr
  191. assert trp[2] not in flt_gr
  192. assert trp[3] not in flt_gr
  193. assert trp[4] not in flt_gr
  194. assert trp[6] not in flt_gr
  195. # Test for empty results.
  196. empty_flt_gr = gr.lookup(
  197. (URIRef('urn:s:1'), URIRef('urn:p:1'), URIRef('urn:o:2'))
  198. )
  199. assert len(empty_flt_gr) == 0
  200. @pytest.mark.usefixtures('trp')
  201. @pytest.mark.usefixtures('store')
  202. class TestGraphSlicing:
  203. """
  204. Test triple lookup.
  205. """
  206. # TODO
  207. pass
  208. @pytest.mark.usefixtures('trp')
  209. @pytest.mark.usefixtures('store')
  210. class TestGraphOps:
  211. """
  212. Test various graph operations.
  213. """
  214. def test_len(self, trp, store):
  215. """
  216. Test the length of a graph with and without duplicates.
  217. """
  218. with store.txn_ctx():
  219. gr = Graph(store)
  220. assert len(gr) == 0
  221. gr.add((trp[0],))
  222. assert len(gr) == 1
  223. gr.add((trp[1],)) # Same values
  224. assert len(gr) == 1
  225. gr.add((trp[2],))
  226. assert len(gr) == 2
  227. gr.add(trp)
  228. assert len(gr) == 6
  229. def test_dup(self, trp, store):
  230. """
  231. Test operations with duplicate triples.
  232. """
  233. with store.txn_ctx():
  234. gr = Graph(store)
  235. gr.add((trp[0],))
  236. assert trp[1] in gr
  237. assert trp[2] not in gr
  238. def test_remove(self, trp, store):
  239. """
  240. Test adding and removing triples.
  241. """
  242. with store.txn_ctx():
  243. gr = Graph(store)
  244. gr.add(trp)
  245. gr.remove(trp[0])
  246. assert len(gr) == 5
  247. assert trp[0] not in gr
  248. assert trp[1] not in gr
  249. # This is the duplicate triple.
  250. gr.remove(trp[1])
  251. assert len(gr) == 5
  252. # This is the triple in reverse order.
  253. gr.remove(trp[2])
  254. assert len(gr) == 4
  255. gr.remove(trp[4])
  256. assert len(gr) == 3
  257. def test_union(self, trp, store):
  258. """
  259. Test graph union.
  260. """
  261. with store.txn_ctx():
  262. gr1 = Graph(store, data={*trp[:3]})
  263. gr2 = Graph(store, data={*trp[2:6]})
  264. gr3 = gr1 | gr2
  265. assert len(gr3) == 5
  266. assert trp[0] in gr3
  267. assert trp[4] in gr3
  268. def test_ip_union(self, trp, store):
  269. """
  270. Test graph in-place union.
  271. """
  272. with store.txn_ctx():
  273. gr1 = Graph(store, data={*trp[:3]})
  274. gr2 = Graph(store, data={*trp[2:6]})
  275. gr1 |= gr2
  276. assert len(gr1) == 5
  277. assert trp[0] in gr1
  278. assert trp[4] in gr1
  279. def test_addition(self, trp, store):
  280. """
  281. Test graph addition.
  282. """
  283. with store.txn_ctx():
  284. gr1 = Graph(store, data={*trp[:3]})
  285. gr2 = Graph(store, data={*trp[2:6]})
  286. gr3 = gr1 + gr2
  287. assert len(gr3) == 5
  288. assert trp[0] in gr3
  289. assert trp[4] in gr3
  290. def test_ip_addition(self, trp, store):
  291. """
  292. Test graph in-place addition.
  293. """
  294. with store.txn_ctx():
  295. gr1 = Graph(store, data={*trp[:3]})
  296. gr2 = Graph(store, data={*trp[2:6]})
  297. gr1 += gr2
  298. assert len(gr1) == 5
  299. assert trp[0] in gr1
  300. assert trp[4] in gr1
  301. def test_subtraction(self, trp, store):
  302. """
  303. Test graph addition.
  304. """
  305. with store.txn_ctx():
  306. gr1 = Graph(store, data={*trp[:4]})
  307. gr2 = Graph(store, data={*trp[2:6]})
  308. gr3 = gr1 - gr2
  309. assert len(gr3) == 1
  310. assert trp[0] in gr3
  311. assert trp[1] in gr3
  312. assert trp[2] not in gr3
  313. assert trp[3] not in gr3
  314. assert trp[4] not in gr3
  315. gr3 = gr2 - gr1
  316. assert len(gr3) == 2
  317. assert trp[0] not in gr3
  318. assert trp[1] not in gr3
  319. assert trp[2] not in gr3
  320. assert trp[3] not in gr3
  321. assert trp[4] in gr3
  322. assert trp[5] in gr3
  323. def test_ip_subtraction(self, trp, store):
  324. """
  325. Test graph in-place addition.
  326. """
  327. with store.txn_ctx():
  328. gr1 = Graph(store, data={*trp[:4]})
  329. gr2 = Graph(store, data={*trp[2:6]})
  330. gr1 -= gr2
  331. assert len(gr1) == 1
  332. assert trp[0] in gr1
  333. assert trp[1] in gr1
  334. assert trp[2] not in gr1
  335. assert trp[3] not in gr1
  336. assert trp[4] not in gr1
  337. def test_intersect(self, trp, store):
  338. """
  339. Test graph intersextion.
  340. """
  341. with store.txn_ctx():
  342. gr1 = Graph(store, data={*trp[:4]})
  343. gr2 = Graph(store, data={*trp[2:6]})
  344. gr3 = gr1 & gr2
  345. assert len(gr3) == 2
  346. assert trp[2] in gr3
  347. assert trp[3] in gr3
  348. assert trp[0] not in gr3
  349. assert trp[5] not in gr3
  350. def test_ip_intersect(self, trp, store):
  351. """
  352. Test graph intersextion.
  353. """
  354. with store.txn_ctx():
  355. gr1 = Graph(store, data={*trp[:4]})
  356. gr2 = Graph(store, data={*trp[2:6]})
  357. gr1 &= gr2
  358. assert len(gr1) == 2
  359. assert trp[2] in gr1
  360. assert trp[3] in gr1
  361. assert trp[0] not in gr1
  362. assert trp[5] not in gr1
  363. def test_xor(self, trp, store):
  364. """
  365. Test graph intersextion.
  366. """
  367. with store.txn_ctx():
  368. gr1 = Graph(store, data={*trp[:4]})
  369. gr2 = Graph(store, data={*trp[2:6]})
  370. gr3 = gr1 ^ gr2
  371. assert len(gr3) == 3
  372. assert trp[2] not in gr3
  373. assert trp[3] not in gr3
  374. assert trp[0] in gr3
  375. assert trp[5] in gr3
  376. def test_ip_xor(self, trp, store):
  377. """
  378. Test graph intersextion.
  379. """
  380. with store.txn_ctx():
  381. gr1 = Graph(store, data={*trp[:4]})
  382. gr2 = Graph(store, data={*trp[2:6]})
  383. gr1 ^= gr2
  384. assert len(gr1) == 3
  385. assert trp[2] not in gr1
  386. assert trp[3] not in gr1
  387. assert trp[0] in gr1
  388. assert trp[5] in gr1
  389. @pytest.mark.usefixtures('trp')
  390. @pytest.mark.usefixtures('store')
  391. class TestNamedGraphOps:
  392. """
  393. Test various operations on a named graph.
  394. """
  395. def test_len(self, trp, store):
  396. """
  397. Test the length of a graph with and without duplicates.
  398. """
  399. imr = Graph(store, uri='http://example.edu/imr01')
  400. assert len(imr) == 0
  401. with store.txn_ctx():
  402. imr.add((trp[0],))
  403. assert len(imr) == 1
  404. imr.add((trp[1],)) # Same values
  405. assert len(imr) == 1
  406. imr.add((trp[2],))
  407. assert len(imr) == 2
  408. imr.add(trp)
  409. assert len(imr) == 6
  410. def test_dup(self, trp, store):
  411. """
  412. Test operations with duplicate triples.
  413. """
  414. imr = Graph(store, uri='http://example.edu/imr01')
  415. with store.txn_ctx():
  416. imr.add((trp[0],))
  417. assert trp[1] in imr
  418. assert trp[2] not in imr
  419. def test_remove(self, trp, store):
  420. """
  421. Test adding and removing triples.
  422. """
  423. with store.txn_ctx():
  424. imr = Graph(store, uri='http://example.edu/imr01', data={*trp})
  425. imr.remove(trp[0])
  426. assert len(imr) == 5
  427. assert trp[0] not in imr
  428. assert trp[1] not in imr
  429. # This is the duplicate triple.
  430. imr.remove(trp[1])
  431. assert len(imr) == 5
  432. # This is the triple in reverse order.
  433. imr.remove(trp[2])
  434. assert len(imr) == 4
  435. imr.remove(trp[4])
  436. assert len(imr) == 3
  437. def test_union(self, trp, store):
  438. """
  439. Test graph union.
  440. """
  441. with store.txn_ctx():
  442. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
  443. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  444. gr3 = gr1 | gr2
  445. assert len(gr3) == 5
  446. assert trp[0] in gr3
  447. assert trp[4] in gr3
  448. assert gr3.uri == None
  449. def test_ip_union(self, trp, store):
  450. """
  451. Test graph in-place union.
  452. """
  453. with store.txn_ctx():
  454. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
  455. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  456. gr1 |= gr2
  457. assert len(gr1) == 5
  458. assert trp[0] in gr1
  459. assert trp[4] in gr1
  460. assert gr1.uri == URIRef('http://example.edu/imr01')
  461. def test_addition(self, trp, store):
  462. """
  463. Test graph addition.
  464. """
  465. with store.txn_ctx():
  466. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
  467. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  468. gr3 = gr1 + gr2
  469. assert len(gr3) == 5
  470. assert trp[0] in gr3
  471. assert trp[4] in gr3
  472. assert gr3.uri == None
  473. def test_ip_addition(self, trp, store):
  474. """
  475. Test graph in-place addition.
  476. """
  477. with store.txn_ctx():
  478. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
  479. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  480. gr1 += gr2
  481. assert len(gr1) == 5
  482. assert trp[0] in gr1
  483. assert trp[4] in gr1
  484. assert gr1.uri == URIRef('http://example.edu/imr01')
  485. def test_subtraction(self, trp, store):
  486. """
  487. Test graph addition.
  488. """
  489. with store.txn_ctx():
  490. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
  491. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  492. gr3 = gr1 - gr2
  493. assert len(gr3) == 1
  494. assert trp[0] in gr3
  495. assert trp[1] in gr3
  496. assert trp[2] not in gr3
  497. assert trp[3] not in gr3
  498. assert trp[4] not in gr3
  499. assert gr3.uri == None
  500. gr3 = gr2 - gr1
  501. assert len(gr3) == 2
  502. assert trp[0] not in gr3
  503. assert trp[1] not in gr3
  504. assert trp[2] not in gr3
  505. assert trp[3] not in gr3
  506. assert trp[4] in gr3
  507. assert trp[5] in gr3
  508. assert gr3.uri == None
  509. def test_ip_subtraction(self, trp, store):
  510. """
  511. Test graph in-place addition.
  512. """
  513. with store.txn_ctx():
  514. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
  515. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  516. gr1 -= gr2
  517. assert len(gr1) == 1
  518. assert trp[0] in gr1
  519. assert trp[1] in gr1
  520. assert trp[2] not in gr1
  521. assert trp[3] not in gr1
  522. assert trp[4] not in gr1
  523. assert gr1.uri == URIRef('http://example.edu/imr01')
  524. def test_intersect(self, trp, store):
  525. """
  526. Test graph intersextion.
  527. """
  528. with store.txn_ctx():
  529. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
  530. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  531. gr3 = gr1 & gr2
  532. assert len(gr3) == 2
  533. assert trp[2] in gr3
  534. assert trp[3] in gr3
  535. assert trp[0] not in gr3
  536. assert trp[5] not in gr3
  537. assert gr3.uri == None
  538. def test_ip_intersect(self, trp, store):
  539. """
  540. Test graph intersextion.
  541. """
  542. with store.txn_ctx():
  543. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
  544. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  545. gr1 &= gr2
  546. assert len(gr1) == 2
  547. assert trp[2] in gr1
  548. assert trp[3] in gr1
  549. assert trp[0] not in gr1
  550. assert trp[5] not in gr1
  551. assert gr1.uri == URIRef('http://example.edu/imr01')
  552. def test_xor(self, trp, store):
  553. """
  554. Test graph intersextion.
  555. """
  556. with store.txn_ctx():
  557. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
  558. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  559. gr3 = gr1 ^ gr2
  560. assert len(gr3) == 3
  561. assert trp[2] not in gr3
  562. assert trp[3] not in gr3
  563. assert trp[0] in gr3
  564. assert trp[5] in gr3
  565. assert gr3.uri == None
  566. def test_ip_xor(self, trp, store):
  567. """
  568. Test graph intersextion.
  569. """
  570. with store.txn_ctx():
  571. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
  572. gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
  573. gr1 ^= gr2
  574. assert len(gr1) == 3
  575. assert trp[2] not in gr1
  576. assert trp[3] not in gr1
  577. assert trp[0] in gr1
  578. assert trp[5] in gr1
  579. assert gr1.uri == URIRef('http://example.edu/imr01')
  580. @pytest.mark.usefixtures('trp')
  581. @pytest.mark.usefixtures('store')
  582. class TestHybridOps:
  583. """
  584. Test operations between IMR and graph.
  585. """
  586. def test_hybrid_union(self, trp, store):
  587. """
  588. Test hybrid IMR + graph union.
  589. """
  590. with store.txn_ctx():
  591. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
  592. gr2 = Graph(store, data={*trp[2:6]})
  593. gr3 = gr1 | gr2
  594. assert len(gr3) == 5
  595. assert trp[0] in gr3
  596. assert trp[4] in gr3
  597. assert isinstance(gr3, Graph)
  598. assert gr3.uri == None
  599. gr4 = gr2 | gr1
  600. assert isinstance(gr4, Graph)
  601. assert gr3 == gr4
  602. def test_ip_union_imr(self, trp, store):
  603. """
  604. Test IMR + graph in-place union.
  605. """
  606. with store.txn_ctx():
  607. gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
  608. gr2 = Graph(store, data={*trp[2:6]})
  609. gr1 |= gr2
  610. assert len(gr1) == 5
  611. assert trp[0] in gr1
  612. assert trp[4] in gr1
  613. assert gr1.uri == URIRef('http://example.edu/imr01')
  614. def test_ip_union_gr(self, trp, store):
  615. """
  616. Test graph + IMR in-place union.
  617. """
  618. with store.txn_ctx():
  619. gr1 = Graph(store, data={*trp[:3]})
  620. gr2 = Graph(store, uri='http://example.edu/imr01', data={*trp[2:6]})
  621. gr1 |= gr2
  622. assert len(gr1) == 5
  623. assert trp[0] in gr1
  624. assert trp[4] in gr1
  625. assert isinstance(gr1, Graph)