test_lmdb_store.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import pytest
  2. from shutil import rmtree
  3. from rdflib import URIRef
  4. from lakesuperior.store_layouts.ldp_rs.lmdb_store import LmdbStore
  5. @pytest.fixture(scope='class')
  6. def store():
  7. store = LmdbStore('/tmp/test_lmdbstore')
  8. yield store
  9. store.close()
  10. rmtree('/tmp/test_lmdbstore')
  11. @pytest.mark.usefixtures('store')
  12. class TestLmdbStore:
  13. '''
  14. Unit tests for LMDB store.
  15. '''
  16. def test_create_triple(self, store):
  17. '''
  18. Test creation of a single triple.
  19. '''
  20. store.begin()
  21. store.add((
  22. URIRef('urn:test:s'), URIRef('urn:test:p'), URIRef('urn:test:o')))
  23. store.commit()
  24. res1 = set(store.triples((None, None, None)))
  25. res2 = set(store.triples((
  26. URIRef('urn:test:s'), URIRef('urn:test:p'), URIRef('urn:test:o'))))
  27. assert len(res1) == 1
  28. assert len(res2) == 1
  29. assert (
  30. URIRef('urn:test:s'), URIRef('urn:test:p'),
  31. URIRef('urn:test:o')) in res1 & res2
  32. def test_triple_match_1bound(self, store):
  33. '''
  34. Test triple patterns matching one bound term.
  35. '''
  36. res1 = set(store.triples((URIRef('urn:test:s'), None, None)))
  37. res2 = set(store.triples((None, URIRef('urn:test:p'), None)))
  38. res3 = set(store.triples((None, None, URIRef('urn:test:o'))))
  39. assert res1 == {(
  40. URIRef('urn:test:s'), URIRef('urn:test:p'), URIRef('urn:test:o'))}
  41. assert res2 == res1
  42. assert res3 == res2
  43. def test_triple_match_2bound(self, store):
  44. '''
  45. Test triple patterns matching two bound terms.
  46. '''
  47. res1 = set(store.triples(
  48. (URIRef('urn:test:s'), URIRef('urn:test:p'), None)))
  49. res2 = set(store.triples(
  50. (URIRef('urn:test:s'), None, URIRef('urn:test:o'))))
  51. res3 = set(store.triples(
  52. (None, URIRef('urn:test:p'), URIRef('urn:test:o'))))
  53. assert res1 == {(
  54. URIRef('urn:test:s'), URIRef('urn:test:p'), URIRef('urn:test:o'))}
  55. assert res2 == res1
  56. assert res3 == res2
  57. def test_triple_no_match(self, store):
  58. '''
  59. Test various mismatches.
  60. '''
  61. store.begin()
  62. store.add((
  63. URIRef('urn:test:s'),
  64. URIRef('urn:test:p2'), URIRef('urn:test:o2')))
  65. store.add((
  66. URIRef('urn:test:s3'),
  67. URIRef('urn:test:p3'), URIRef('urn:test:o3')))
  68. store.commit()
  69. res1 = set(store.triples((None, None, None)))
  70. assert len(res1) == 3
  71. res1 = set(store.triples(
  72. (URIRef('urn:test:s2'), URIRef('urn:test:p'), None)))
  73. res2 = set(store.triples(
  74. (URIRef('urn:test:s3'), None, URIRef('urn:test:o'))))
  75. res3 = set(store.triples(
  76. (None, URIRef('urn:test:p3'), URIRef('urn:test:o2'))))
  77. assert len(res1) == len(res2) == len(res3) == 0
  78. def test_remove(self, store):
  79. '''
  80. Test removing one or more triples.
  81. '''
  82. store.begin()
  83. store.remove((URIRef('urn:test:s3'),
  84. URIRef('urn:test:p3'), URIRef('urn:test:o3')))
  85. store.commit()
  86. res1 = set(store.triples((None, None, None)))
  87. assert len(res1) == 2
  88. store.begin()
  89. store.remove((URIRef('urn:test:s'), None, None))
  90. store.commit()
  91. res2 = set(store.triples((None, None, None)))
  92. assert len(res2) == 0