test_2_0_resource_api.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. import pdb
  2. import pytest
  3. from io import BytesIO
  4. from uuid import uuid4
  5. from rdflib import Literal, URIRef
  6. from lakesuperior import env
  7. from lakesuperior.api import resource as rsrc_api
  8. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  9. from lakesuperior.exceptions import (
  10. IncompatibleLdpTypeError, InvalidResourceError, ResourceNotExistsError,
  11. TombstoneError)
  12. from lakesuperior.globals import RES_CREATED, RES_UPDATED
  13. from lakesuperior.model.ldp.ldpr import Ldpr
  14. from lakesuperior.model.rdf.graph import Graph, from_rdf
  15. @pytest.fixture(scope='module')
  16. def random_uuid():
  17. return str(uuid.uuid4())
  18. @pytest.fixture
  19. def dc_rdf():
  20. return b'''
  21. PREFIX dcterms: <http://purl.org/dc/terms/>
  22. PREFIX ldp: <http://www.w3.org/ns/ldp#>
  23. <> a ldp:DirectContainer ;
  24. dcterms:title "Direct Container" ;
  25. ldp:membershipResource <info:fcres/member> ;
  26. ldp:hasMemberRelation dcterms:relation .
  27. '''
  28. @pytest.fixture
  29. def ic_rdf():
  30. return b'''
  31. PREFIX dcterms: <http://purl.org/dc/terms/>
  32. PREFIX ldp: <http://www.w3.org/ns/ldp#>
  33. PREFIX ore: <http://www.openarchives.org/ore/terms/>
  34. <> a ldp:IndirectContainer ;
  35. dcterms:title "Indirect Container" ;
  36. ldp:membershipResource <info:fcres/top_container> ;
  37. ldp:hasMemberRelation dcterms:relation ;
  38. ldp:insertedContentRelation ore:proxyFor .
  39. '''
  40. @pytest.mark.usefixtures('db')
  41. class TestResourceCRUD:
  42. '''
  43. Test interaction with the Resource API.
  44. '''
  45. def test_nodes_exist(self):
  46. """
  47. Verify whether nodes exist or not.
  48. """
  49. assert rsrc_api.exists('/') is True
  50. assert rsrc_api.exists('/{}'.format(uuid4())) is False
  51. def test_get_root_node_metadata(self):
  52. """
  53. Get the root node metadata.
  54. The ``dcterms:title`` property should NOT be included.
  55. """
  56. gr = rsrc_api.get_metadata('/')
  57. assert isinstance(gr, Graph)
  58. assert len(gr) == 9
  59. with env.app_globals.rdf_store.txn_ctx():
  60. assert gr[gr.uri : nsc['rdf'].type : nsc['ldp'].Resource ]
  61. assert not gr[
  62. gr.uri : nsc['dcterms'].title : Literal("Repository Root")
  63. ]
  64. def test_get_root_node(self):
  65. """
  66. Get the root node.
  67. The ``dcterms:title`` property should be included.
  68. """
  69. rsrc = rsrc_api.get('/')
  70. assert isinstance(rsrc, Ldpr)
  71. gr = rsrc.imr
  72. assert len(gr) == 10
  73. with env.app_globals.rdf_store.txn_ctx():
  74. assert gr[gr.uri : nsc['rdf'].type : nsc['ldp'].Resource ]
  75. assert gr[
  76. gr.uri : nsc['dcterms'].title : Literal('Repository Root')]
  77. def test_get_nonexisting_node(self):
  78. """
  79. Get a non-existing node.
  80. """
  81. with pytest.raises(ResourceNotExistsError):
  82. gr = rsrc_api.get('/{}'.format(uuid4()))
  83. def test_create_ldp_rs(self):
  84. """
  85. Create an RDF resource (LDP-RS) from a provided graph.
  86. """
  87. uid = '/rsrc_from_graph'
  88. uri = nsc['fcres'][uid]
  89. with env.app_globals.rdf_store.txn_ctx():
  90. gr = from_rdf(
  91. data='<> a <http://ex.org/type#A> .', format='turtle',
  92. publicID=uri)
  93. evt, _ = rsrc_api.create_or_replace(uid, graph=gr)
  94. rsrc = rsrc_api.get(uid)
  95. with env.app_globals.rdf_store.txn_ctx():
  96. assert rsrc.imr[
  97. rsrc.uri : nsc['rdf'].type : URIRef('http://ex.org/type#A')]
  98. assert rsrc.imr[
  99. rsrc.uri : nsc['rdf'].type : nsc['ldp'].RDFSource]
  100. def test_create_ldp_nr(self):
  101. """
  102. Create a non-RDF resource (LDP-NR).
  103. """
  104. uid = '/{}'.format(uuid4())
  105. data = b'Hello. This is some dummy content.'
  106. rsrc_api.create_or_replace(
  107. uid, stream=BytesIO(data), mimetype='text/plain')
  108. rsrc = rsrc_api.get(uid)
  109. with rsrc.imr.store.txn_ctx():
  110. assert rsrc.content.read() == data
  111. def test_replace_rsrc(self):
  112. uid = '/test_replace'
  113. uri = nsc['fcres'][uid]
  114. with env.app_globals.rdf_store.txn_ctx():
  115. gr1 = from_rdf(
  116. data='<> a <http://ex.org/type#A> .', format='turtle',
  117. publicID=uri
  118. )
  119. evt, _ = rsrc_api.create_or_replace(uid, graph=gr1)
  120. assert evt == RES_CREATED
  121. rsrc = rsrc_api.get(uid)
  122. with env.app_globals.rdf_store.txn_ctx():
  123. assert rsrc.imr[
  124. rsrc.uri : nsc['rdf'].type : URIRef('http://ex.org/type#A')]
  125. assert rsrc.imr[
  126. rsrc.uri : nsc['rdf'].type : nsc['ldp'].RDFSource]
  127. with env.app_globals.rdf_store.txn_ctx():
  128. gr2 = from_rdf(
  129. data='<> a <http://ex.org/type#B> .', format='turtle',
  130. publicID=uri
  131. )
  132. #pdb.set_trace()
  133. evt, _ = rsrc_api.create_or_replace(uid, graph=gr2)
  134. assert evt == RES_UPDATED
  135. rsrc = rsrc_api.get(uid)
  136. with env.app_globals.rdf_store.txn_ctx():
  137. assert not rsrc.imr[
  138. rsrc.uri : nsc['rdf'].type : URIRef('http://ex.org/type#A')]
  139. assert rsrc.imr[
  140. rsrc.uri : nsc['rdf'].type : URIRef('http://ex.org/type#B')]
  141. assert rsrc.imr[
  142. rsrc.uri : nsc['rdf'].type : nsc['ldp'].RDFSource]
  143. def test_replace_incompatible_type(self):
  144. """
  145. Verify replacing resources with incompatible type.
  146. Replacing a LDP-NR with a LDP-RS, or vice versa, should fail.
  147. """
  148. uid_rs = '/test_incomp_rs'
  149. uid_nr = '/test_incomp_nr'
  150. data = b'mock binary content'
  151. with env.app_globals.rdf_store.txn_ctx():
  152. gr = from_rdf(
  153. data='<> a <http://ex.org/type#A> .', format='turtle',
  154. publicID=nsc['fcres'][uid_rs]
  155. )
  156. rsrc_api.create_or_replace(uid_rs, graph=gr)
  157. rsrc_api.create_or_replace(
  158. uid_nr, stream=BytesIO(data), mimetype='text/plain')
  159. with pytest.raises(IncompatibleLdpTypeError):
  160. rsrc_api.create_or_replace(uid_nr, graph=gr)
  161. with pytest.raises(IncompatibleLdpTypeError):
  162. rsrc_api.create_or_replace(
  163. uid_rs, stream=BytesIO(data), mimetype='text/plain')
  164. with pytest.raises(IncompatibleLdpTypeError):
  165. rsrc_api.create_or_replace(uid_nr)
  166. def test_delta_update(self):
  167. """
  168. Update a resource with two sets of add and remove triples.
  169. """
  170. uid = '/test_delta_patch'
  171. uri = nsc['fcres'][uid]
  172. init_trp = {
  173. (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Person),
  174. (URIRef(uri), nsc['foaf'].name, Literal('Joe Bob')),
  175. }
  176. remove_trp = {
  177. (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Person),
  178. }
  179. add_trp = {
  180. (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Organization),
  181. }
  182. with env.app_globals.rdf_store.txn_ctx():
  183. gr = Graph(data=init_trp)
  184. rsrc_api.create_or_replace(uid, graph=gr)
  185. rsrc_api.update_delta(uid, remove_trp, add_trp)
  186. rsrc = rsrc_api.get(uid)
  187. with env.app_globals.rdf_store.txn_ctx():
  188. assert rsrc.imr[
  189. rsrc.uri : nsc['rdf'].type : nsc['foaf'].Organization]
  190. assert rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joe Bob')]
  191. assert not rsrc.imr[
  192. rsrc.uri : nsc['rdf'].type : nsc['foaf'].Person]
  193. def test_delta_update_wildcard(self):
  194. """
  195. Update a resource using wildcard modifiers.
  196. """
  197. uid = '/test_delta_patch_wc'
  198. uri = nsc['fcres'][uid]
  199. init_trp = {
  200. (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Person),
  201. (URIRef(uri), nsc['foaf'].name, Literal('Joe Bob')),
  202. (URIRef(uri), nsc['foaf'].name, Literal('Joe Average Bob')),
  203. (URIRef(uri), nsc['foaf'].name, Literal('Joe 12oz Bob')),
  204. }
  205. remove_trp = {
  206. (URIRef(uri), nsc['foaf'].name, None),
  207. }
  208. add_trp = {
  209. (URIRef(uri), nsc['foaf'].name, Literal('Joan Knob')),
  210. }
  211. with env.app_globals.rdf_store.txn_ctx():
  212. gr = Graph(data=init_trp)
  213. rsrc_api.create_or_replace(uid, graph=gr)
  214. rsrc_api.update_delta(uid, remove_trp, add_trp)
  215. rsrc = rsrc_api.get(uid)
  216. with env.app_globals.rdf_store.txn_ctx():
  217. assert rsrc.imr[
  218. rsrc.uri : nsc['rdf'].type : nsc['foaf'].Person]
  219. assert rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joan Knob')]
  220. assert not rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joe Bob')]
  221. assert not rsrc.imr[
  222. rsrc.uri : nsc['foaf'].name : Literal('Joe Average Bob')]
  223. assert not rsrc.imr[
  224. rsrc.uri : nsc['foaf'].name : Literal('Joe 12oz Bob')]
  225. def test_sparql_update(self):
  226. """
  227. Update a resource using a SPARQL Update string.
  228. Use a mix of relative and absolute URIs.
  229. """
  230. uid = '/test_sparql'
  231. rdf_data = b'<> <http://purl.org/dc/terms/title> "Original title." .'
  232. update_str = '''DELETE {
  233. <> <http://purl.org/dc/terms/title> "Original title." .
  234. } INSERT {
  235. <> <http://purl.org/dc/terms/title> "Title #2." .
  236. <info:fcres/test_sparql>
  237. <http://purl.org/dc/terms/title> "Title #3." .
  238. <#h1> <http://purl.org/dc/terms/title> "This is a hash." .
  239. } WHERE {
  240. }'''
  241. rsrc_api.create_or_replace(uid, rdf_data=rdf_data, rdf_fmt='turtle')
  242. ver_uid = rsrc_api.create_version(uid, 'v1').split('fcr:versions/')[-1]
  243. rsrc = rsrc_api.update(uid, update_str)
  244. with env.app_globals.rdf_store.txn_ctx():
  245. assert (
  246. (rsrc.uri, nsc['dcterms'].title, Literal('Original title.'))
  247. not in set(rsrc.imr))
  248. assert (
  249. (rsrc.uri, nsc['dcterms'].title, Literal('Title #2.'))
  250. in set(rsrc.imr))
  251. assert (
  252. (rsrc.uri, nsc['dcterms'].title, Literal('Title #3.'))
  253. in set(rsrc.imr))
  254. assert ((
  255. URIRef(str(rsrc.uri) + '#h1'),
  256. nsc['dcterms'].title, Literal('This is a hash.'))
  257. in set(rsrc.imr))
  258. def test_create_ldp_dc_post(self, dc_rdf):
  259. """
  260. Create an LDP Direct Container via POST.
  261. """
  262. rsrc_api.create_or_replace('/member')
  263. dc_rsrc = rsrc_api.create(
  264. '/', 'test_dc_post', rdf_data=dc_rdf, rdf_fmt='turtle')
  265. member_rsrc = rsrc_api.get('/member')
  266. with env.app_globals.rdf_store.txn_ctx():
  267. assert nsc['ldp'].Container in dc_rsrc.ldp_types
  268. assert nsc['ldp'].DirectContainer in dc_rsrc.ldp_types
  269. def test_create_ldp_dc_put(self, dc_rdf):
  270. """
  271. Create an LDP Direct Container via PUT.
  272. """
  273. dc_uid = '/test_dc_put01'
  274. _, dc_rsrc = rsrc_api.create_or_replace(
  275. dc_uid, rdf_data=dc_rdf, rdf_fmt='turtle')
  276. member_rsrc = rsrc_api.get('/member')
  277. with env.app_globals.rdf_store.txn_ctx():
  278. assert nsc['ldp'].Container in dc_rsrc.ldp_types
  279. assert nsc['ldp'].DirectContainer in dc_rsrc.ldp_types
  280. def test_add_dc_member(self, dc_rdf):
  281. """
  282. Add members to a direct container and verify special properties.
  283. """
  284. dc_uid = '/test_dc_put02'
  285. _, dc_rsrc = rsrc_api.create_or_replace(
  286. dc_uid, rdf_data=dc_rdf, rdf_fmt='turtle')
  287. child_uid = rsrc_api.create(dc_uid).uid
  288. member_rsrc = rsrc_api.get('/member')
  289. with env.app_globals.rdf_store.txn_ctx():
  290. assert member_rsrc.imr[
  291. member_rsrc.uri: nsc['dcterms'].relation: nsc['fcres'][child_uid]]
  292. def test_create_ldp_dc_defaults1(self):
  293. """
  294. Create an LDP Direct Container with default values.
  295. """
  296. dc_rdf = b'''
  297. PREFIX dcterms: <http://purl.org/dc/terms/>
  298. PREFIX ldp: <http://www.w3.org/ns/ldp#>
  299. <> a ldp:DirectContainer ;
  300. ldp:membershipResource <info:fcres/member> .
  301. '''
  302. dc_uid = '/test_dc_defaults1'
  303. _, dc_rsrc = rsrc_api.create_or_replace(
  304. dc_uid, rdf_data=dc_rdf, rdf_fmt='turtle')
  305. child_uid = rsrc_api.create(dc_uid).uid
  306. member_rsrc = rsrc_api.get('/member')
  307. with env.app_globals.rdf_store.txn_ctx():
  308. assert member_rsrc.imr[
  309. member_rsrc.uri: nsc['ldp'].member: nsc['fcres'][child_uid]
  310. ]
  311. def test_create_ldp_dc_defaults2(self):
  312. """
  313. Create an LDP Direct Container with default values.
  314. """
  315. dc_rdf = b'''
  316. PREFIX dcterms: <http://purl.org/dc/terms/>
  317. PREFIX ldp: <http://www.w3.org/ns/ldp#>
  318. <> a ldp:DirectContainer ;
  319. ldp:hasMemberRelation dcterms:relation .
  320. '''
  321. dc_uid = '/test_dc_defaults2'
  322. _, dc_rsrc = rsrc_api.create_or_replace(
  323. dc_uid, rdf_data=dc_rdf, rdf_fmt='turtle')
  324. child_uid = rsrc_api.create(dc_uid).uid
  325. member_rsrc = rsrc_api.get(dc_uid)
  326. with env.app_globals.rdf_store.txn_ctx():
  327. #import pdb; pdb.set_trace()
  328. assert member_rsrc.imr[
  329. member_rsrc.uri: nsc['dcterms'].relation:
  330. nsc['fcres'][child_uid]]
  331. def test_create_ldp_dc_defaults3(self):
  332. """
  333. Create an LDP Direct Container with default values.
  334. """
  335. dc_rdf = b'''
  336. PREFIX dcterms: <http://purl.org/dc/terms/>
  337. PREFIX ldp: <http://www.w3.org/ns/ldp#>
  338. <> a ldp:DirectContainer .
  339. '''
  340. dc_uid = '/test_dc_defaults3'
  341. _, dc_rsrc = rsrc_api.create_or_replace(
  342. dc_uid, rdf_data=dc_rdf, rdf_fmt='turtle')
  343. child_uid = rsrc_api.create(dc_uid, None).uid
  344. member_rsrc = rsrc_api.get(dc_uid)
  345. with env.app_globals.rdf_store.txn_ctx():
  346. assert member_rsrc.imr[
  347. member_rsrc.uri: nsc['ldp'].member: nsc['fcres'][child_uid]]
  348. def test_indirect_container(self, ic_rdf):
  349. """
  350. Create an indirect container and verify special properties.
  351. """
  352. cont_uid = '/top_container'
  353. ic_uid = '{}/test_ic'.format(cont_uid)
  354. member_uid = '{}/ic_member'.format(ic_uid)
  355. target_uid = '/ic_target'
  356. ic_member_rdf = b'''
  357. PREFIX ore: <http://www.openarchives.org/ore/terms/>
  358. <> ore:proxyFor <info:fcres/ic_target> .'''
  359. rsrc_api.create_or_replace(cont_uid)
  360. rsrc_api.create_or_replace(target_uid)
  361. rsrc_api.create_or_replace(ic_uid, rdf_data=ic_rdf, rdf_fmt='turtle')
  362. rsrc_api.create_or_replace(
  363. member_uid, rdf_data=ic_member_rdf, rdf_fmt='turtle')
  364. ic_rsrc = rsrc_api.get(ic_uid)
  365. with env.app_globals.rdf_store.txn_ctx():
  366. assert nsc['ldp'].Container in ic_rsrc.ldp_types
  367. assert nsc['ldp'].IndirectContainer in ic_rsrc.ldp_types
  368. assert nsc['ldp'].DirectContainer not in ic_rsrc.ldp_types
  369. member_rsrc = rsrc_api.get(member_uid)
  370. top_cont_rsrc = rsrc_api.get(cont_uid)
  371. with env.app_globals.rdf_store.txn_ctx():
  372. assert top_cont_rsrc.imr[
  373. top_cont_rsrc.uri: nsc['dcterms'].relation:
  374. nsc['fcres'][target_uid]]
  375. # TODO WIP Complex test of all possible combinations of missing IC triples
  376. # falling back to default values.
  377. #def test_indirect_container_defaults(self):
  378. # """
  379. # Create an indirect container with various default values.
  380. # """
  381. # ic_rdf_base = b'''
  382. # PREFIX dcterms: <http://purl.org/dc/terms/>
  383. # PREFIX ldp: <http://www.w3.org/ns/ldp#>
  384. # PREFIX ore: <http://www.openarchives.org/ore/terms/>
  385. # <> a ldp:IndirectContainer ;
  386. # '''
  387. # ic_rdf_trp1 = '\nldp:membershipResource <info:fcres/top_container> ;'
  388. # ic_rdf_trp2 = '\nldp:hasMemberRelation dcterms:relation ;'
  389. # ic_rdf_trp3 = '\nldp:insertedContentRelation ore:proxyFor ;'
  390. # ic_def_rdf = [
  391. # ic_rdf_base + ic_rdf_trp1 + ic_trp2 + '\n.',
  392. # ic_rdf_base + ic_rdf_trp1 + ic_trp3 + '\n.',
  393. # ic_rdf_base + ic_rdf_trp2 + ic_trp3 + '\n.',
  394. # ic_rdf_base + ic_rdf_trp1 + '\n.',
  395. # ic_rdf_base + ic_rdf_trp2 + '\n.',
  396. # ic_rdf_base + ic_rdf_trp3 + '\n.',
  397. # ic_rdf_base + '\n.',
  398. # ]
  399. # target_uid = '/ic_target_def'
  400. # rsrc_api.create_or_replace(target_uid)
  401. # # Create several sets of indirect containers, each missing one or more
  402. # # triples from the original graph, which should be replaced by default
  403. # # values. All combinations are tried.
  404. # for i, ic_rdf in enumerate(ic_def_rdf):
  405. # cont_uid = f'/top_container_def{i}'
  406. # ic_uid = '{}/test_ic'.format(cont_uid)
  407. # member_uid = '{}/ic_member'.format(ic_uid)
  408. # rsrc_api.create_or_replace(cont_uid)
  409. # rsrc_api.create_or_replace(
  410. # ic_uid, rdf_data=ic_rdf, rdf_fmt='turtle'
  411. # )
  412. # ic_member_p = (
  413. # nsc['ore'].proxyFor if i in (1, 2, 5)
  414. # else nsc['ldp'].memberSubject
  415. # )
  416. # # WIP
  417. # #ic_member_o_uid = (
  418. # # 'ic_target_def' if i in (1, 2, 5)
  419. # # else nsc['ldp'].memberSubject
  420. # #)
  421. # ic_member_rdf = b'''
  422. # PREFIX ore: <http://www.openarchives.org/ore/terms/>
  423. # <> ore:proxyFor <info:fcres/ic_target_def> .'''
  424. # rsrc_api.create_or_replace(
  425. # member_uid, rdf_data=ic_member_rdf, rdf_fmt='turtle')
  426. # ic_rsrc = rsrc_api.get(ic_uid)
  427. # with env.app_globals.rdf_store.txn_ctx():
  428. # assert nsc['ldp'].Container in ic_rsrc.ldp_types
  429. # assert nsc['ldp'].IndirectContainer in ic_rsrc.ldp_types
  430. # top_cont_rsrc = rsrc_api.get(cont_uid)
  431. # for i, ic_rdf in enumerate(ic_def_rdf):
  432. # member_rsrc = rsrc_api.get(member_uid)
  433. # with env.app_globals.rdf_store.txn_ctx():
  434. # assert top_cont_rsrc.imr[
  435. # top_cont_rsrc.uri: nsc['dcterms'].relation:
  436. # nsc['fcres'][target_uid]]
  437. @pytest.mark.usefixtures('db')
  438. class TestRelativeUris:
  439. '''
  440. Test inserting and updating resources with relative URIs.
  441. '''
  442. def test_create_self_uri_rdf(self):
  443. """
  444. Create a resource with empty string ("self") URIs in the RDF body.
  445. """
  446. uid = '/reluri01'
  447. uri = nsc['fcres'][uid]
  448. data = '''
  449. <> a <urn:type:A> .
  450. <http://ex.org/external> <urn:pred:x> <> .
  451. '''
  452. rsrc_api.create_or_replace(uid, rdf_data=data, rdf_fmt='ttl')
  453. rsrc = rsrc_api.get(uid)
  454. with env.app_globals.rdf_store.txn_ctx():
  455. assert rsrc.imr[uri: nsc['rdf']['type']: URIRef('urn:type:A')]
  456. assert rsrc.imr[
  457. URIRef('http://ex.org/external'): URIRef('urn:pred:x'): uri]
  458. def test_create_self_uri_graph(self):
  459. """
  460. Create a resource with empty string ("self") URIs in a RDFlib graph.
  461. """
  462. uid = '/reluri02'
  463. uri = nsc['fcres'][uid]
  464. gr = Graph()
  465. with env.app_globals.rdf_store.txn_ctx():
  466. gr.add({
  467. (URIRef(''), nsc['rdf']['type'], URIRef('urn:type:A')),
  468. (
  469. URIRef('http://ex.org/external'),
  470. URIRef('urn:pred:x'), URIRef('')
  471. ),
  472. })
  473. rsrc_api.create_or_replace(uid, graph=gr)
  474. rsrc = rsrc_api.get(uid)
  475. with env.app_globals.rdf_store.txn_ctx():
  476. assert rsrc.imr[uri: nsc['rdf']['type']: URIRef('urn:type:A')]
  477. assert rsrc.imr[
  478. URIRef('http://ex.org/external'): URIRef('urn:pred:x'): uri]
  479. def test_create_hash_uri_rdf(self):
  480. """
  481. Create a resource with empty string ("self") URIs in the RDF body.
  482. """
  483. uid = '/reluri03'
  484. uri = nsc['fcres'][uid]
  485. data = '''
  486. <#hash1> a <urn:type:A> .
  487. <http://ex.org/external> <urn:pred:x> <#hash2> .
  488. '''
  489. rsrc_api.create_or_replace(uid, rdf_data=data, rdf_fmt='ttl')
  490. rsrc = rsrc_api.get(uid)
  491. with env.app_globals.rdf_store.txn_ctx():
  492. assert rsrc.imr[
  493. URIRef(str(uri) + '#hash1'): nsc['rdf'].type:
  494. URIRef('urn:type:A')]
  495. assert rsrc.imr[
  496. URIRef('http://ex.org/external'): URIRef('urn:pred:x'):
  497. URIRef(str(uri) + '#hash2')]
  498. @pytest.mark.skip
  499. def test_create_hash_uri_graph(self):
  500. """
  501. Create a resource with empty string ("self") URIs in a RDFlib graph.
  502. """
  503. uid = '/reluri04'
  504. uri = nsc['fcres'][uid]
  505. gr = Graph()
  506. with env.app_globals.rdf_store.txn_ctx():
  507. gr.add({
  508. (URIRef('#hash1'), nsc['rdf']['type'], URIRef('urn:type:A')),
  509. (
  510. URIRef('http://ex.org/external'),
  511. URIRef('urn:pred:x'), URIRef('#hash2')
  512. )
  513. })
  514. rsrc_api.create_or_replace(uid, graph=gr)
  515. rsrc = rsrc_api.get(uid)
  516. with env.app_globals.rdf_store.txn_ctx():
  517. assert rsrc.imr[
  518. URIRef(str(uri) + '#hash1'): nsc['rdf']['type']:
  519. URIRef('urn:type:A')]
  520. assert rsrc.imr[
  521. URIRef('http://ex.org/external'): URIRef('urn:pred:x'):
  522. URIRef(str(uri) + '#hash2')]
  523. @pytest.mark.skip(reason='RDFlib bug.')
  524. def test_create_child_uri_rdf(self):
  525. """
  526. Create a resource with empty string ("self") URIs in the RDF body.
  527. """
  528. uid = '/reluri05'
  529. uri = nsc['fcres'][uid]
  530. data = '''
  531. <child1> a <urn:type:A> .
  532. <http://ex.org/external> <urn:pred:x> <child2> .
  533. '''
  534. rsrc_api.create_or_replace(uid, rdf_data=data, rdf_fmt='ttl')
  535. rsrc = rsrc_api.get(uid)
  536. with env.app_globals.rdf_store.txn_ctx():
  537. assert rsrc.imr[
  538. URIRef(str(uri) + '/child1'): nsc['rdf'].type:
  539. URIRef('urn:type:A')]
  540. assert rsrc.imr[
  541. URIRef('http://ex.org/external'): URIRef('urn:pred:x'):
  542. URIRef(str(uri) + '/child2')]
  543. @pytest.mark.skip(reason='RDFlib bug.')
  544. def test_create_child_uri_graph(self):
  545. """
  546. Create a resource with empty string ("self") URIs in the RDF body.
  547. """
  548. uid = '/reluri06'
  549. uri = nsc['fcres'][uid]
  550. gr = Graph()
  551. with env.app_globals.rdf_store.txn_ctx():
  552. gr.add({
  553. (URIRef('child1'), nsc['rdf']['type'], URIRef('urn:type:A')),
  554. (
  555. URIRef('http://ex.org/external'),
  556. URIRef('urn:pred:x'), URIRef('child22')
  557. )
  558. })
  559. rsrc_api.create_or_replace(uid, graph=gr)
  560. rsrc = rsrc_api.get(uid)
  561. with env.app_globals.rdf_store.txn_ctx():
  562. assert rsrc.imr[
  563. URIRef(str(uri) + '/child1'): nsc['rdf'].type:
  564. URIRef('urn:type:A')]
  565. assert rsrc.imr[
  566. URIRef('http://ex.org/external'): URIRef('urn:pred:x'):
  567. URIRef(str(uri) + '/child2')]
  568. @pytest.mark.usefixtures('db')
  569. class TestAdvancedDelete:
  570. '''
  571. Test resource version lifecycle.
  572. '''
  573. def test_soft_delete(self):
  574. """
  575. Soft-delete (bury) a resource.
  576. """
  577. uid = '/test_soft_delete01'
  578. rsrc_api.create_or_replace(uid)
  579. rsrc_api.delete(uid)
  580. with pytest.raises(TombstoneError):
  581. rsrc_api.get(uid)
  582. def test_resurrect(self):
  583. """
  584. Restore (resurrect) a soft-deleted resource.
  585. """
  586. uid = '/test_soft_delete02'
  587. rsrc_api.create_or_replace(uid)
  588. rsrc_api.delete(uid)
  589. rsrc_api.resurrect(uid)
  590. rsrc = rsrc_api.get(uid)
  591. with env.app_globals.rdf_store.txn_ctx():
  592. assert nsc['ldp'].Resource in rsrc.ldp_types
  593. def test_hard_delete(self):
  594. """
  595. Hard-delete (forget) a resource.
  596. """
  597. uid = '/test_hard_delete01'
  598. rsrc_api.create_or_replace(uid)
  599. rsrc_api.delete(uid, False)
  600. with pytest.raises(ResourceNotExistsError):
  601. rsrc_api.get(uid)
  602. with pytest.raises(ResourceNotExistsError):
  603. rsrc_api.resurrect(uid)
  604. def test_delete_children(self):
  605. """
  606. Soft-delete a resource with children.
  607. """
  608. uid = '/test_soft_delete_children01'
  609. rsrc_api.create_or_replace(uid)
  610. for i in range(3):
  611. rsrc_api.create_or_replace('{}/child{}'.format(uid, i))
  612. rsrc_api.delete(uid)
  613. with pytest.raises(TombstoneError):
  614. rsrc_api.get(uid)
  615. for i in range(3):
  616. with pytest.raises(TombstoneError):
  617. rsrc_api.get('{}/child{}'.format(uid, i))
  618. # Cannot resurrect children of a tombstone.
  619. with pytest.raises(TombstoneError):
  620. rsrc_api.resurrect('{}/child{}'.format(uid, i))
  621. def test_resurrect_children(self):
  622. """
  623. Resurrect a resource with its children.
  624. This uses fixtures from the previous test.
  625. """
  626. uid = '/test_soft_delete_children01'
  627. rsrc_api.resurrect(uid)
  628. parent_rsrc = rsrc_api.get(uid)
  629. with env.app_globals.rdf_store.txn_ctx():
  630. assert nsc['ldp'].Resource in parent_rsrc.ldp_types
  631. for i in range(3):
  632. child_rsrc = rsrc_api.get('{}/child{}'.format(uid, i))
  633. with env.app_globals.rdf_store.txn_ctx():
  634. assert nsc['ldp'].Resource in child_rsrc.ldp_types
  635. def test_hard_delete_children(self):
  636. """
  637. Hard-delete (forget) a resource with its children.
  638. This uses fixtures from the previous test.
  639. """
  640. uid = '/test_hard_delete_children01'
  641. rsrc_api.create_or_replace(uid)
  642. for i in range(3):
  643. rsrc_api.create_or_replace('{}/child{}'.format(uid, i))
  644. rsrc_api.delete(uid, False)
  645. with pytest.raises(ResourceNotExistsError):
  646. rsrc_api.get(uid)
  647. with pytest.raises(ResourceNotExistsError):
  648. rsrc_api.resurrect(uid)
  649. for i in range(3):
  650. with pytest.raises(ResourceNotExistsError):
  651. rsrc_api.get('{}/child{}'.format(uid, i))
  652. with pytest.raises(ResourceNotExistsError):
  653. rsrc_api.resurrect('{}/child{}'.format(uid, i))
  654. def test_hard_delete_descendants(self):
  655. """
  656. Forget a resource with all its descendants.
  657. """
  658. uid = '/test_hard_delete_descendants01'
  659. rsrc_api.create_or_replace(uid)
  660. for i in range(1, 4):
  661. rsrc_api.create_or_replace('{}/child{}'.format(uid, i))
  662. for j in range(i):
  663. rsrc_api.create_or_replace('{}/child{}/grandchild{}'.format(
  664. uid, i, j))
  665. rsrc_api.delete(uid, False)
  666. with pytest.raises(ResourceNotExistsError):
  667. rsrc_api.get(uid)
  668. with pytest.raises(ResourceNotExistsError):
  669. rsrc_api.resurrect(uid)
  670. for i in range(1, 4):
  671. with pytest.raises(ResourceNotExistsError):
  672. rsrc_api.get('{}/child{}'.format(uid, i))
  673. with pytest.raises(ResourceNotExistsError):
  674. rsrc_api.resurrect('{}/child{}'.format(uid, i))
  675. for j in range(i):
  676. with pytest.raises(ResourceNotExistsError):
  677. rsrc_api.get('{}/child{}/grandchild{}'.format(
  678. uid, i, j))
  679. with pytest.raises(ResourceNotExistsError):
  680. rsrc_api.resurrect('{}/child{}/grandchild{}'.format(
  681. uid, i, j))
  682. @pytest.mark.usefixtures('db')
  683. class TestResourceVersioning:
  684. '''
  685. Test resource version lifecycle.
  686. '''
  687. def test_create_version(self):
  688. """
  689. Create a version snapshot.
  690. """
  691. uid = '/test_version1'
  692. rdf_data = b'<> <http://purl.org/dc/terms/title> "Original title." .'
  693. update_str = '''DELETE {
  694. <> <http://purl.org/dc/terms/title> "Original title." .
  695. } INSERT {
  696. <> <http://purl.org/dc/terms/title> "Title #2." .
  697. } WHERE {
  698. }'''
  699. rsrc_api.create_or_replace(uid, rdf_data=rdf_data, rdf_fmt='turtle')
  700. ver_uid = rsrc_api.create_version(uid, 'v1').split('fcr:versions/')[-1]
  701. #FIXME Without this, the test fails.
  702. #set(rsrc_api.get_version(uid, ver_uid))
  703. rsrc_api.update(uid, update_str)
  704. current = rsrc_api.get(uid)
  705. with env.app_globals.rdf_store.txn_ctx():
  706. assert (
  707. (current.uri, nsc['dcterms'].title, Literal('Title #2.'))
  708. in current.imr)
  709. assert (
  710. (current.uri, nsc['dcterms'].title, Literal('Original title.'))
  711. not in current.imr)
  712. v1 = rsrc_api.get_version(uid, ver_uid)
  713. with env.app_globals.rdf_store.txn_ctx():
  714. assert (
  715. (v1.uri, nsc['dcterms'].title, Literal('Original title.'))
  716. in set(v1))
  717. assert (
  718. (v1.uri, nsc['dcterms'].title, Literal('Title #2.'))
  719. not in set(v1))
  720. def test_revert_to_version(self):
  721. """
  722. Test reverting to a previous version.
  723. Uses assets from previous test.
  724. """
  725. uid = '/test_version1'
  726. ver_uid = 'v1'
  727. rsrc_api.revert_to_version(uid, ver_uid)
  728. rev = rsrc_api.get(uid)
  729. with env.app_globals.rdf_store.txn_ctx():
  730. assert (
  731. (rev.uri, nsc['dcterms'].title, Literal('Original title.'))
  732. in rev.imr)
  733. def test_versioning_children(self):
  734. """
  735. Test that children are not affected by version restoring.
  736. 1. create parent resource
  737. 2. Create child 1
  738. 3. Version parent
  739. 4. Create child 2
  740. 5. Restore parent to previous version
  741. 6. Verify that restored version still has 2 children
  742. """
  743. uid = '/test_version_children'
  744. ver_uid = 'v1'
  745. ch1_uid = '{}/kid_a'.format(uid)
  746. ch2_uid = '{}/kid_b'.format(uid)
  747. rsrc_api.create_or_replace(uid)
  748. rsrc_api.create_or_replace(ch1_uid)
  749. ver_uid = rsrc_api.create_version(uid, ver_uid).split('fcr:versions/')[-1]
  750. rsrc = rsrc_api.get(uid)
  751. with env.app_globals.rdf_store.txn_ctx():
  752. assert nsc['fcres'][ch1_uid] in rsrc.imr[
  753. rsrc.uri : nsc['ldp'].contains]
  754. rsrc_api.create_or_replace(ch2_uid)
  755. rsrc = rsrc_api.get(uid)
  756. with env.app_globals.rdf_store.txn_ctx():
  757. assert nsc['fcres'][ch2_uid] in rsrc.imr[
  758. rsrc.uri : nsc['ldp'].contains]
  759. rsrc_api.revert_to_version(uid, ver_uid)
  760. rsrc = rsrc_api.get(uid)
  761. with env.app_globals.rdf_store.txn_ctx():
  762. assert nsc['fcres'][ch1_uid] in rsrc.imr[
  763. rsrc.uri : nsc['ldp'].contains]
  764. assert nsc['fcres'][ch2_uid] in rsrc.imr[
  765. rsrc.uri : nsc['ldp'].contains]