test_2_0_resource_api.py 34 KB

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