test_graph.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. pdb.set_trace()
  189. assert len(flt_gr) == 1
  190. assert trp[5] in flt_gr
  191. assert trp[0] not in flt_gr
  192. assert trp[2] not in flt_gr
  193. assert trp[3] not in flt_gr
  194. assert trp[4] not in flt_gr
  195. assert trp[6] not in flt_gr
  196. # Test for empty results.
  197. empty_flt_gr = gr.lookup(
  198. (URIRef('urn:s:1'), URIRef('urn:p:1'), URIRef('urn:o:2'))
  199. )
  200. assert len(empty_flt_gr) == 0
  201. @pytest.mark.usefixtures('trp')
  202. @pytest.mark.usefixtures('store')
  203. class TestGraphSlicing:
  204. """
  205. Test triple lookup.
  206. """
  207. # TODO
  208. pass
  209. @pytest.mark.usefixtures('trp')
  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)
  263. gr2 = Graph(store)
  264. gr1.add(trp[0:3])
  265. gr2.add(trp[2:6])
  266. gr3 = gr1 | gr2
  267. assert len(gr3) == 5
  268. assert trp[0] in gr3
  269. assert trp[4] in gr3
  270. def test_ip_union(self, trp, store):
  271. """
  272. Test graph in-place union.
  273. """
  274. with store.txn_ctx():
  275. gr1 = Graph(store)
  276. gr2 = Graph(store)
  277. gr1.add(trp[0:3])
  278. gr2.add(trp[2:6])
  279. gr1 |= gr2
  280. assert len(gr1) == 5
  281. assert trp[0] in gr1
  282. assert trp[4] in gr1
  283. def test_addition(self, trp):
  284. """
  285. Test graph addition.
  286. """
  287. gr1 = Graph()
  288. gr2 = Graph()
  289. gr1.add(trp[0:3])
  290. gr2.add(trp[2:6])
  291. gr3 = gr1 + gr2
  292. assert len(gr3) == 5
  293. assert trp[0] in gr3
  294. assert trp[4] in gr3
  295. def test_ip_addition(self, trp):
  296. """
  297. Test graph in-place addition.
  298. """
  299. gr1 = Graph()
  300. gr2 = Graph()
  301. gr1.add(trp[0:3])
  302. gr2.add(trp[2:6])
  303. gr1 += gr2
  304. assert len(gr1) == 5
  305. assert trp[0] in gr1
  306. assert trp[4] in gr1
  307. def test_subtraction(self, trp):
  308. """
  309. Test graph addition.
  310. """
  311. gr1 = Graph()
  312. gr2 = Graph()
  313. gr1.add(trp[0:4])
  314. gr2.add(trp[2:6])
  315. gr3 = gr1 - gr2
  316. assert len(gr3) == 1
  317. assert trp[0] in gr3
  318. assert trp[1] in gr3
  319. assert trp[2] not in gr3
  320. assert trp[3] not in gr3
  321. assert trp[4] not in gr3
  322. gr3 = gr2 - gr1
  323. assert len(gr3) == 2
  324. assert trp[0] not in gr3
  325. assert trp[1] not in gr3
  326. assert trp[2] not in gr3
  327. assert trp[3] not in gr3
  328. assert trp[4] in gr3
  329. assert trp[5] in gr3
  330. def test_ip_subtraction(self, trp):
  331. """
  332. Test graph in-place addition.
  333. """
  334. gr1 = Graph()
  335. gr2 = Graph()
  336. gr1.add(trp[0:4])
  337. gr2.add(trp[2:6])
  338. gr1 -= gr2
  339. assert len(gr1) == 1
  340. assert trp[0] in gr1
  341. assert trp[1] in gr1
  342. assert trp[2] not in gr1
  343. assert trp[3] not in gr1
  344. assert trp[4] not in gr1
  345. def test_intersect(self, trp):
  346. """
  347. Test graph intersextion.
  348. """
  349. gr1 = Graph()
  350. gr2 = Graph()
  351. gr1.add(trp[0:4])
  352. gr2.add(trp[2:6])
  353. gr3 = gr1 & gr2
  354. assert len(gr3) == 2
  355. assert trp[2] in gr3
  356. assert trp[3] in gr3
  357. assert trp[0] not in gr3
  358. assert trp[5] not in gr3
  359. def test_ip_intersect(self, trp):
  360. """
  361. Test graph intersextion.
  362. """
  363. gr1 = Graph()
  364. gr2 = Graph()
  365. gr1.add(trp[0:4])
  366. gr2.add(trp[2:6])
  367. gr1 &= gr2
  368. assert len(gr1) == 2
  369. assert trp[2] in gr1
  370. assert trp[3] in gr1
  371. assert trp[0] not in gr1
  372. assert trp[5] not in gr1
  373. def test_xor(self, trp):
  374. """
  375. Test graph intersextion.
  376. """
  377. gr1 = Graph()
  378. gr2 = Graph()
  379. gr1.add(trp[0:4])
  380. gr2.add(trp[2:6])
  381. gr3 = gr1 ^ gr2
  382. assert len(gr3) == 3
  383. assert trp[2] not in gr3
  384. assert trp[3] not in gr3
  385. assert trp[0] in gr3
  386. assert trp[5] in gr3
  387. def test_ip_xor(self, trp):
  388. """
  389. Test graph intersextion.
  390. """
  391. gr1 = Graph()
  392. gr2 = Graph()
  393. gr1.add(trp[0:4])
  394. gr2.add(trp[2:6])
  395. gr1 ^= gr2
  396. assert len(gr1) == 3
  397. assert trp[2] not in gr1
  398. assert trp[3] not in gr1
  399. assert trp[0] in gr1
  400. assert trp[5] in gr1
  401. @pytest.mark.usefixtures('trp')
  402. class TestNamedGraphOps:
  403. """
  404. Test various operations on a named graph.
  405. """
  406. def test_len(self, trp):
  407. """
  408. Test the length of a graph with and without duplicates.
  409. """
  410. imr = Graph(uri='http://example.edu/imr01')
  411. assert len(imr) == 0
  412. imr.add((trp[0],))
  413. assert len(imr) == 1
  414. imr.add((trp[1],)) # Same values
  415. assert len(imr) == 1
  416. imr.add((trp[2],))
  417. assert len(imr) == 2
  418. imr.add(trp)
  419. assert len(imr) == 6
  420. def test_dup(self, trp):
  421. """
  422. Test operations with duplicate triples.
  423. """
  424. imr = Graph(uri='http://example.edu/imr01')
  425. #import pdb; pdb.set_trace()
  426. imr.add((trp[0],))
  427. assert trp[1] in imr
  428. assert trp[2] not in imr
  429. def test_remove(self, trp):
  430. """
  431. Test adding and removing triples.
  432. """
  433. imr = Graph(uri='http://example.edu/imr01')
  434. imr.add(trp)
  435. imr.remove(trp[0])
  436. assert len(imr) == 5
  437. assert trp[0] not in imr
  438. assert trp[1] not in imr
  439. # This is the duplicate triple.
  440. imr.remove(trp[1])
  441. assert len(imr) == 5
  442. # This is the triple in reverse order.
  443. imr.remove(trp[2])
  444. assert len(imr) == 4
  445. imr.remove(trp[4])
  446. assert len(imr) == 3
  447. def test_union(self, trp):
  448. """
  449. Test graph union.
  450. """
  451. gr1 = Graph(uri='http://example.edu/imr01')
  452. gr2 = Graph(uri='http://example.edu/imr02')
  453. gr1.add(trp[0:3])
  454. gr2.add(trp[2:6])
  455. gr3 = gr1 | gr2
  456. assert len(gr3) == 5
  457. assert trp[0] in gr3
  458. assert trp[4] in gr3
  459. assert gr3.uri == URIRef('http://example.edu/imr01')
  460. def test_ip_union(self, trp):
  461. """
  462. Test graph in-place union.
  463. """
  464. gr1 = Graph(uri='http://example.edu/imr01')
  465. gr2 = Graph(uri='http://example.edu/imr02')
  466. gr1.add(trp[0:3])
  467. gr2.add(trp[2:6])
  468. gr1 |= gr2
  469. assert len(gr1) == 5
  470. assert trp[0] in gr1
  471. assert trp[4] in gr1
  472. assert gr1.uri == URIRef('http://example.edu/imr01')
  473. def test_addition(self, trp):
  474. """
  475. Test graph addition.
  476. """
  477. gr1 = Graph(uri='http://example.edu/imr01')
  478. gr2 = Graph(uri='http://example.edu/imr02')
  479. gr1.add(trp[0:3])
  480. gr2.add(trp[2:6])
  481. gr3 = gr1 + gr2
  482. assert len(gr3) == 5
  483. assert trp[0] in gr3
  484. assert trp[4] in gr3
  485. assert gr3.uri == URIRef('http://example.edu/imr01')
  486. def test_ip_addition(self, trp):
  487. """
  488. Test graph in-place addition.
  489. """
  490. gr1 = Graph(uri='http://example.edu/imr01')
  491. gr2 = Graph(uri='http://example.edu/imr02')
  492. gr1.add(trp[0:3])
  493. gr2.add(trp[2:6])
  494. gr1 += gr2
  495. assert len(gr1) == 5
  496. assert trp[0] in gr1
  497. assert trp[4] in gr1
  498. assert gr1.uri == URIRef('http://example.edu/imr01')
  499. def test_subtraction(self, trp):
  500. """
  501. Test graph addition.
  502. """
  503. gr1 = Graph(uri='http://example.edu/imr01')
  504. gr2 = Graph(uri='http://example.edu/imr02')
  505. gr1.add(trp[0:4])
  506. gr2.add(trp[2:6])
  507. gr3 = gr1 - gr2
  508. assert len(gr3) == 1
  509. assert trp[0] in gr3
  510. assert trp[1] in gr3
  511. assert trp[2] not in gr3
  512. assert trp[3] not in gr3
  513. assert trp[4] not in gr3
  514. assert gr3.uri == URIRef('http://example.edu/imr01')
  515. gr3 = gr2 - gr1
  516. assert len(gr3) == 2
  517. assert trp[0] not in gr3
  518. assert trp[1] not in gr3
  519. assert trp[2] not in gr3
  520. assert trp[3] not in gr3
  521. assert trp[4] in gr3
  522. assert trp[5] in gr3
  523. assert gr3.uri == URIRef('http://example.edu/imr02')
  524. def test_ip_subtraction(self, trp):
  525. """
  526. Test graph in-place addition.
  527. """
  528. gr1 = Graph(uri='http://example.edu/imr01')
  529. gr2 = Graph(uri='http://example.edu/imr02')
  530. gr1.add(trp[0:4])
  531. gr2.add(trp[2:6])
  532. gr1 -= gr2
  533. assert len(gr1) == 1
  534. assert trp[0] in gr1
  535. assert trp[1] in gr1
  536. assert trp[2] not in gr1
  537. assert trp[3] not in gr1
  538. assert trp[4] not in gr1
  539. assert gr1.uri == URIRef('http://example.edu/imr01')
  540. def test_intersect(self, trp):
  541. """
  542. Test graph intersextion.
  543. """
  544. gr1 = Graph(uri='http://example.edu/imr01')
  545. gr2 = Graph(uri='http://example.edu/imr02')
  546. gr1.add(trp[0:4])
  547. gr2.add(trp[2:6])
  548. gr3 = gr1 & gr2
  549. assert len(gr3) == 2
  550. assert trp[2] in gr3
  551. assert trp[3] in gr3
  552. assert trp[0] not in gr3
  553. assert trp[5] not in gr3
  554. assert gr3.uri == URIRef('http://example.edu/imr01')
  555. def test_ip_intersect(self, trp):
  556. """
  557. Test graph intersextion.
  558. """
  559. gr1 = Graph(uri='http://example.edu/imr01')
  560. gr2 = Graph(uri='http://example.edu/imr02')
  561. gr1.add(trp[0:4])
  562. gr2.add(trp[2:6])
  563. gr1 &= gr2
  564. assert len(gr1) == 2
  565. assert trp[2] in gr1
  566. assert trp[3] in gr1
  567. assert trp[0] not in gr1
  568. assert trp[5] not in gr1
  569. assert gr1.uri == URIRef('http://example.edu/imr01')
  570. def test_xor(self, trp):
  571. """
  572. Test graph intersextion.
  573. """
  574. gr1 = Graph(uri='http://example.edu/imr01')
  575. gr2 = Graph(uri='http://example.edu/imr02')
  576. gr1.add(trp[0:4])
  577. gr2.add(trp[2:6])
  578. gr3 = gr1 ^ gr2
  579. assert len(gr3) == 3
  580. assert trp[2] not in gr3
  581. assert trp[3] not in gr3
  582. assert trp[0] in gr3
  583. assert trp[5] in gr3
  584. assert gr3.uri == URIRef('http://example.edu/imr01')
  585. def test_ip_xor(self, trp):
  586. """
  587. Test graph intersextion.
  588. """
  589. gr1 = Graph(uri='http://example.edu/imr01')
  590. gr2 = Graph(uri='http://example.edu/imr02')
  591. gr1.add(trp[0:4])
  592. gr2.add(trp[2:6])
  593. gr1 ^= gr2
  594. assert len(gr1) == 3
  595. assert trp[2] not in gr1
  596. assert trp[3] not in gr1
  597. assert trp[0] in gr1
  598. assert trp[5] in gr1
  599. assert gr1.uri == URIRef('http://example.edu/imr01')
  600. @pytest.mark.usefixtures('trp')
  601. class TestHybridOps:
  602. """
  603. Test operations between IMR and graph.
  604. """
  605. def test_union(self, trp):
  606. """
  607. Test hybrid IMR + graph union.
  608. """
  609. gr1 = Graph(uri='http://example.edu/imr01')
  610. gr2 = Graph()
  611. gr1.add(trp[0:3])
  612. gr2.add(trp[2:6])
  613. gr3 = gr1 | gr2
  614. assert len(gr3) == 5
  615. assert trp[0] in gr3
  616. assert trp[4] in gr3
  617. assert isinstance(gr3, Graph)
  618. assert gr3.uri == URIRef('http://example.edu/imr01')
  619. gr4 = gr2 | gr1
  620. assert isinstance(gr4, Graph)
  621. assert gr3 == gr4
  622. def test_ip_union_imr(self, trp):
  623. """
  624. Test IMR + graph in-place union.
  625. """
  626. gr1 = Graph(uri='http://example.edu/imr01')
  627. gr2 = Graph()
  628. gr1.add(trp[0:3])
  629. gr2.add(trp[2:6])
  630. gr1 |= gr2
  631. assert len(gr1) == 5
  632. assert trp[0] in gr1
  633. assert trp[4] in gr1
  634. assert gr1.uri == URIRef('http://example.edu/imr01')
  635. def test_ip_union_gr(self, trp):
  636. """
  637. Test graph + IMR in-place union.
  638. """
  639. gr1 = Graph()
  640. gr2 = Graph(uri='http://example.edu/imr01')
  641. gr1.add(trp[0:3])
  642. gr2.add(trp[2:6])
  643. gr1 |= gr2
  644. assert len(gr1) == 5
  645. assert trp[0] in gr1
  646. assert trp[4] in gr1
  647. assert isinstance(gr1, Graph)