test_resource_api.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import pdb
  2. import pytest
  3. from io import BytesIO
  4. from uuid import uuid4
  5. from rdflib import Graph, Literal, URIRef
  6. from lakesuperior.api import resource as rsrc_api
  7. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  8. from lakesuperior.exceptions import (
  9. IncompatibleLdpTypeError, InvalidResourceError, ResourceNotExistsError,
  10. TombstoneError)
  11. from lakesuperior.globals import RES_CREATED, RES_UPDATED
  12. from lakesuperior.model.ldpr import Ldpr
  13. @pytest.fixture(scope='module')
  14. def random_uuid():
  15. return str(uuid.uuid4())
  16. @pytest.fixture
  17. def dc_rdf():
  18. return b'''
  19. PREFIX dcterms: <http://purl.org/dc/terms/>
  20. PREFIX ldp: <http://www.w3.org/ns/ldp#>
  21. <> dcterms:title "Direct Container" ;
  22. ldp:membershipResource <info:fcres/member> ;
  23. ldp:hasMemberRelation dcterms:relation .
  24. '''
  25. @pytest.fixture
  26. def ic_rdf():
  27. return b'''
  28. PREFIX dcterms: <http://purl.org/dc/terms/>
  29. PREFIX ldp: <http://www.w3.org/ns/ldp#>
  30. PREFIX ore: <http://www.openarchives.org/ore/terms/>
  31. <> dcterms:title "Indirect Container" ;
  32. ldp:membershipResource <info:fcres/top_container> ;
  33. ldp:hasMemberRelation dcterms:relation ;
  34. ldp:insertedContentRelation ore:proxyFor .
  35. '''
  36. @pytest.mark.usefixtures('db')
  37. class TestResourceApi:
  38. '''
  39. Test interaction with the Resource API.
  40. '''
  41. def test_nodes_exist(self):
  42. """
  43. Verify whether nodes exist or not.
  44. """
  45. assert rsrc_api.exists('/') is True
  46. assert rsrc_api.exists('/{}'.format(uuid4())) is False
  47. def test_get_root_node_metadata(self):
  48. """
  49. Get the root node metadata.
  50. The ``dcterms:title`` property should NOT be included.
  51. """
  52. gr = rsrc_api.get_metadata('/')
  53. assert isinstance(gr, Graph)
  54. assert len(gr) == 9
  55. assert gr[gr.identifier : nsc['rdf'].type : nsc['ldp'].Resource ]
  56. assert not gr[gr.identifier : nsc['dcterms'].title : "Repository Root"]
  57. def test_get_root_node(self):
  58. """
  59. Get the root node.
  60. The ``dcterms:title`` property should be included.
  61. """
  62. rsrc = rsrc_api.get('/')
  63. assert isinstance(rsrc, Ldpr)
  64. gr = rsrc.imr
  65. assert len(gr) == 10
  66. assert gr[gr.identifier : nsc['rdf'].type : nsc['ldp'].Resource ]
  67. assert gr[
  68. gr.identifier : nsc['dcterms'].title : Literal('Repository Root')]
  69. def test_get_nonexisting_node(self):
  70. """
  71. Get a non-existing node.
  72. """
  73. with pytest.raises(ResourceNotExistsError):
  74. gr = rsrc_api.get('/{}'.format(uuid4()))
  75. def test_create_ldp_rs(self):
  76. """
  77. Create an RDF resource (LDP-RS) from a provided graph.
  78. """
  79. uid = '/rsrc_from_graph'
  80. uri = nsc['fcres'][uid]
  81. gr = Graph().parse(
  82. data='<> a <http://ex.org/type#A> .', format='turtle',
  83. publicID=uri)
  84. #pdb.set_trace()
  85. evt = rsrc_api.create_or_replace(uid, graph=gr)
  86. rsrc = rsrc_api.get(uid)
  87. assert rsrc.imr[
  88. rsrc.uri : nsc['rdf'].type : URIRef('http://ex.org/type#A')]
  89. assert rsrc.imr[
  90. rsrc.uri : nsc['rdf'].type : nsc['ldp'].RDFSource]
  91. def test_create_ldp_nr(self):
  92. """
  93. Create a non-RDF resource (LDP-NR).
  94. """
  95. uid = '/{}'.format(uuid4())
  96. data = b'Hello. This is some dummy content.'
  97. rsrc_api.create_or_replace(
  98. uid, stream=BytesIO(data), mimetype='text/plain')
  99. rsrc = rsrc_api.get(uid)
  100. assert rsrc.content.read() == data
  101. def test_replace_rsrc(self):
  102. uid = '/test_replace'
  103. uri = nsc['fcres'][uid]
  104. gr1 = Graph().parse(
  105. data='<> a <http://ex.org/type#A> .', format='turtle',
  106. publicID=uri)
  107. evt = rsrc_api.create_or_replace(uid, graph=gr1)
  108. assert evt == RES_CREATED
  109. rsrc = rsrc_api.get(uid)
  110. assert rsrc.imr[
  111. rsrc.uri : nsc['rdf'].type : URIRef('http://ex.org/type#A')]
  112. assert rsrc.imr[
  113. rsrc.uri : nsc['rdf'].type : nsc['ldp'].RDFSource]
  114. gr2 = Graph().parse(
  115. data='<> a <http://ex.org/type#B> .', format='turtle',
  116. publicID=uri)
  117. #pdb.set_trace()
  118. evt = rsrc_api.create_or_replace(uid, graph=gr2)
  119. assert evt == RES_UPDATED
  120. rsrc = rsrc_api.get(uid)
  121. assert not rsrc.imr[
  122. rsrc.uri : nsc['rdf'].type : URIRef('http://ex.org/type#A')]
  123. assert rsrc.imr[
  124. rsrc.uri : nsc['rdf'].type : URIRef('http://ex.org/type#B')]
  125. assert rsrc.imr[
  126. rsrc.uri : nsc['rdf'].type : nsc['ldp'].RDFSource]
  127. def test_replace_incompatible_type(self):
  128. """
  129. Verify replacing resources with incompatible type.
  130. Replacing a LDP-NR with a LDP-RS, or vice versa, should fail.
  131. """
  132. uid_rs = '/test_incomp_rs'
  133. uid_nr = '/test_incomp_nr'
  134. data = b'mock binary content'
  135. gr = Graph().parse(
  136. data='<> a <http://ex.org/type#A> .', format='turtle',
  137. publicID=nsc['fcres'][uid_rs])
  138. rsrc_api.create_or_replace(uid_rs, graph=gr)
  139. rsrc_api.create_or_replace(
  140. uid_nr, stream=BytesIO(data), mimetype='text/plain')
  141. with pytest.raises(IncompatibleLdpTypeError):
  142. rsrc_api.create_or_replace(uid_nr, graph=gr)
  143. with pytest.raises(IncompatibleLdpTypeError):
  144. rsrc_api.create_or_replace(
  145. uid_rs, stream=BytesIO(data), mimetype='text/plain')
  146. with pytest.raises(IncompatibleLdpTypeError):
  147. rsrc_api.create_or_replace(uid_nr)
  148. def test_delta_update(self):
  149. """
  150. Update a resource with two sets of add and remove triples.
  151. """
  152. uid = '/test_delta_patch'
  153. uri = nsc['fcres'][uid]
  154. init_trp = {
  155. (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Person),
  156. (URIRef(uri), nsc['foaf'].name, Literal('Joe Bob')),
  157. }
  158. remove_trp = {
  159. (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Person),
  160. }
  161. add_trp = {
  162. (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Organization),
  163. }
  164. gr = Graph()
  165. gr += init_trp
  166. rsrc_api.create_or_replace(uid, graph=gr)
  167. rsrc_api.update_delta(uid, remove_trp, add_trp)
  168. rsrc = rsrc_api.get(uid)
  169. assert rsrc.imr[
  170. rsrc.uri : nsc['rdf'].type : nsc['foaf'].Organization]
  171. assert rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joe Bob')]
  172. assert not rsrc.imr[
  173. rsrc.uri : nsc['rdf'].type : nsc['foaf'].Person]
  174. def test_delta_update_wildcard(self):
  175. """
  176. Update a resource using wildcard modifiers.
  177. """
  178. uid = '/test_delta_patch_wc'
  179. uri = nsc['fcres'][uid]
  180. init_trp = {
  181. (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Person),
  182. (URIRef(uri), nsc['foaf'].name, Literal('Joe Bob')),
  183. (URIRef(uri), nsc['foaf'].name, Literal('Joe Average Bob')),
  184. (URIRef(uri), nsc['foaf'].name, Literal('Joe 12oz Bob')),
  185. }
  186. remove_trp = {
  187. (URIRef(uri), nsc['foaf'].name, None),
  188. }
  189. add_trp = {
  190. (URIRef(uri), nsc['foaf'].name, Literal('Joan Knob')),
  191. }
  192. gr = Graph()
  193. gr += init_trp
  194. rsrc_api.create_or_replace(uid, graph=gr)
  195. rsrc_api.update_delta(uid, remove_trp, add_trp)
  196. rsrc = rsrc_api.get(uid)
  197. assert rsrc.imr[
  198. rsrc.uri : nsc['rdf'].type : nsc['foaf'].Person]
  199. assert rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joan Knob')]
  200. assert not rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joe Bob')]
  201. assert not rsrc.imr[
  202. rsrc.uri : nsc['foaf'].name : Literal('Joe Average Bob')]
  203. assert not rsrc.imr[
  204. rsrc.uri : nsc['foaf'].name : Literal('Joe 12oz Bob')]
  205. def test_create_ldp_dc_post(self, dc_rdf):
  206. """
  207. Create an LDP Direct Container via POST.
  208. """
  209. rsrc_api.create_or_replace('/member')
  210. dc_uid = rsrc_api.create(
  211. '/', 'test_dc_post', rdf_data=dc_rdf, rdf_fmt='turtle')
  212. dc_rsrc = rsrc_api.get(dc_uid)
  213. member_rsrc = rsrc_api.get('/member')
  214. assert nsc['ldp'].Container in dc_rsrc.ldp_types
  215. assert nsc['ldp'].DirectContainer in dc_rsrc.ldp_types
  216. def test_create_ldp_dc_put(self, dc_rdf):
  217. """
  218. Create an LDP Direct Container via PUT.
  219. """
  220. dc_uid = '/test_dc_put01'
  221. rsrc_api.create_or_replace(
  222. dc_uid, rdf_data=dc_rdf, rdf_fmt='turtle')
  223. dc_rsrc = rsrc_api.get(dc_uid)
  224. member_rsrc = rsrc_api.get('/member')
  225. assert nsc['ldp'].Container in dc_rsrc.ldp_types
  226. assert nsc['ldp'].DirectContainer in dc_rsrc.ldp_types
  227. def test_add_dc_member(self, dc_rdf):
  228. """
  229. Add members to a direct container and verify special properties.
  230. """
  231. dc_uid = '/test_dc_put02'
  232. rsrc_api.create_or_replace(
  233. dc_uid, rdf_data=dc_rdf, rdf_fmt='turtle')
  234. dc_rsrc = rsrc_api.get(dc_uid)
  235. child_uid = rsrc_api.create(dc_uid, None)
  236. member_rsrc = rsrc_api.get('/member')
  237. assert member_rsrc.imr[
  238. member_rsrc.uri: nsc['dcterms'].relation: nsc['fcres'][child_uid]]
  239. def test_indirect_container(self, ic_rdf):
  240. """
  241. Create an indirect container verify special properties.
  242. """
  243. cont_uid = '/top_container'
  244. ic_uid = '{}/test_ic'.format(cont_uid)
  245. member_uid = '{}/ic_member'.format(ic_uid)
  246. target_uid = '/ic_target'
  247. ic_member_rdf = b'''
  248. PREFIX ore: <http://www.openarchives.org/ore/terms/>
  249. <> ore:proxyFor <info:fcres/ic_target> .'''
  250. rsrc_api.create_or_replace(cont_uid)
  251. rsrc_api.create_or_replace(target_uid)
  252. rsrc_api.create_or_replace(ic_uid, rdf_data=ic_rdf, rdf_fmt='turtle')
  253. rsrc_api.create_or_replace(
  254. member_uid, rdf_data=ic_member_rdf, rdf_fmt='turtle')
  255. ic_rsrc = rsrc_api.get(ic_uid)
  256. assert nsc['ldp'].Container in ic_rsrc.ldp_types
  257. assert nsc['ldp'].IndirectContainer in ic_rsrc.ldp_types
  258. assert nsc['ldp'].DirectContainer not in ic_rsrc.ldp_types
  259. member_rsrc = rsrc_api.get(member_uid)
  260. top_cont_rsrc = rsrc_api.get(cont_uid)
  261. assert top_cont_rsrc.imr[
  262. top_cont_rsrc.uri: nsc['dcterms'].relation:
  263. nsc['fcres'][target_uid]]