initial_tests.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python3
  2. ## Small set of tests to begin with.
  3. ## For testing, import this file:
  4. ##
  5. ## `from tests.initial_tests import *`
  6. ##
  7. ## Then clear the data store with clear() and run
  8. ## individual functions inspecting the dataset at each step.
  9. import pdb
  10. import rdflib
  11. from rdflib.graph import Dataset
  12. from rdflib.namespace import RDF
  13. from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore
  14. from rdflib.term import URIRef
  15. query_ep = 'http://localhost:3030/lakesuperior-dev/query'
  16. update_ep = 'http://localhost:3030/lakesuperior-dev/update'
  17. store = SPARQLUpdateStore(queryEndpoint=query_ep, update_endpoint=update_ep,
  18. autocommit=False)
  19. ds = Dataset(store, default_union=True)
  20. def query(q):
  21. res = ds.query(q)
  22. print(res.serialize().decode('utf-8'))
  23. def clear():
  24. '''Clear triplestore.'''
  25. for g in ds.graphs():
  26. ds.remove_graph(g)
  27. store.commit()
  28. print('All graphs removed from store.')
  29. def insert(report=False):
  30. '''Add a resource.'''
  31. res1 = ds.graph(URIRef('urn:res:12873624'))
  32. meta1 = ds.graph(URIRef('urn:meta:12873624'))
  33. res1.add((URIRef('urn:state:001'), RDF.type, URIRef('http://example.edu#Blah')))
  34. meta1.add((URIRef('urn:state:001'), RDF.type, URIRef('http://example.edu#ActiveState')))
  35. store.commit()
  36. if report:
  37. print('Inserted resource:')
  38. query('''
  39. SELECT ?s ?p ?o
  40. FROM <urn:res:12873624>
  41. FROM <urn:meta:12873624> {
  42. ?s a <http://example.edu#ActiveState> .
  43. ?s ?p ?o .
  44. }'''
  45. )
  46. def update(report=False):
  47. '''Update resource and create a historic snapshot.'''
  48. res1 = ds.graph(URIRef('urn:res:12873624'))
  49. meta1 = ds.graph(URIRef('urn:meta:12873624'))
  50. res1.add((URIRef('urn:state:002'), RDF.type, URIRef('http://example.edu#Boo')))
  51. meta1.remove((URIRef('urn:state:001'), RDF.type, URIRef('http://example.edu#ActiveState')))
  52. meta1.add((URIRef('urn:state:001'), RDF.type, URIRef('http://example.edu#Snapshot')))
  53. meta1.add((URIRef('urn:state:002'), RDF.type, URIRef('http://example.edu#ActiveState')))
  54. meta1.add((URIRef('urn:state:002'), URIRef('http://example.edu#prevState'), URIRef('urn:state:001')))
  55. store.commit()
  56. if report:
  57. print('Updated resource:')
  58. query('''
  59. SELECT ?s ?p ?o
  60. FROM <urn:res:12873624>
  61. FROM <urn:meta:12873624> {
  62. ?s a <http://example.edu#ActiveState> .
  63. ?s ?p ?o .
  64. }'''
  65. )
  66. print('Version snapshot:')
  67. query('''
  68. SELECT ?s ?p ?o
  69. FROM <urn:res:12873624>
  70. FROM <urn:meta:12873624> {
  71. ?s a <http://example.edu#Snapshot> .
  72. ?s ?p ?o .
  73. }'''
  74. )
  75. def delete(report=False):
  76. '''Delete resource and leave a tombstone.'''
  77. meta1 = ds.graph(URIRef('urn:meta:12873624'))
  78. meta1.remove((URIRef('urn:state:002'), RDF.type, URIRef('http://example.edu#ActiveState')))
  79. meta1.add((URIRef('urn:state:002'), RDF.type, URIRef('http://example.edu#Tombstone')))
  80. store.commit()
  81. if report:
  82. print('Deleted resource (tombstone):')
  83. query('''
  84. SELECT ?s ?p ?o
  85. FROM <urn:res:12873624>
  86. FROM <urn:meta:12873624> {
  87. ?s a <http://example.edu#Tombstone> .
  88. ?s ?p ?o .
  89. }'''
  90. )
  91. def undelete(report=False):
  92. '''Resurrect resource from a tombstone.'''
  93. meta1 = ds.graph(URIRef('urn:meta:12873624'))
  94. meta1.remove((URIRef('urn:state:002'), RDF.type, URIRef('http://example.edu#Tombstone')))
  95. meta1.add((URIRef('urn:state:002'), RDF.type, URIRef('http://example.edu#ActiveState')))
  96. store.commit()
  97. if report:
  98. print('Undeleted resource:')
  99. query('''
  100. SELECT ?s ?p ?o
  101. FROM <urn:res:12873624>
  102. FROM <urn:meta:12873624> {
  103. ?s a <http://example.edu#ActiveState> .
  104. ?s ?p ?o .
  105. }'''
  106. )
  107. def abort_tx(report=False):
  108. '''Abort an operation in the middle of a transaction and roll back.'''
  109. try:
  110. res2 = ds.graph(URIRef('urn:state:002'))
  111. res2.add((URIRef('urn:lake:12873624'), RDF.type, URIRef('http://example.edu#Werp')))
  112. raise RuntimeError('Something awful happened!')
  113. store.commit()
  114. except RuntimeError as e:
  115. print('Exception caught: {}'.format(e))
  116. store.rollback()
  117. if report:
  118. print('Failed operation (no updates):')
  119. query('''
  120. SELECT ?s ?p ?o
  121. FROM <urn:res:12873624>
  122. FROM <urn:meta:12873624> {
  123. ?s a <http://example.edu#ActiveState> .
  124. ?s ?p ?o .
  125. }'''
  126. )
  127. def partial_query(report=False):
  128. '''Execute a query containing a token that throws an error in the middle.
  129. The purpose of this is to verify whether the store is truly transactional,
  130. i.e. the whole operation in a transaction is rolled back even if some
  131. updates have already been processed.'''
  132. # @TODO
  133. pass