test_metadata_store.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import pytest
  2. from hashlib import sha256
  3. from lakesuperior.store.ldp_rs.metadata_store import MetadataStore
  4. class TestMetadataStore:
  5. """
  6. Tests for the LMDB Metadata store.
  7. """
  8. def test_put_checksum(self):
  9. """
  10. Put a new checksum.
  11. """
  12. uri = 'info:fcres/test_checksum'
  13. cksum = sha256(b'Bogus content')
  14. mds = MetadataStore()
  15. with mds.cur(index='checksums', write=True) as cur:
  16. cur.put(uri.encode('utf-8'), cksum.digest())
  17. with mds.cur(index='checksums') as cur:
  18. assert cur.get(uri.encode('utf-8')) == cksum.digest()
  19. def test_separate_txn(self):
  20. """
  21. Open a transaction and put a new checksum.
  22. Same as test_put_checksum but wrapping the cursor in a separate
  23. transaction. This is really to test the base store which is an abstract
  24. class.
  25. """
  26. uri = 'info:fcres/test_checksum_separate'
  27. cksum = sha256(b'More bogus content.')
  28. mds = MetadataStore()
  29. with mds.txn(True) as txn:
  30. with mds.cur(index='checksums', txn=txn) as cur:
  31. cur.put(uri.encode('utf-8'), cksum.digest())
  32. with mds.txn() as txn:
  33. with mds.cur(index='checksums', txn=txn) as cur:
  34. assert cur.get(uri.encode('utf-8')) == cksum.digest()
  35. def test_exception(self):
  36. """
  37. Test exceptions within cursor and transaction contexts.
  38. """
  39. uri = 'info:fcres/test_checksum_exception'
  40. cksum = sha256(b'More bogus content.')
  41. mds = MetadataStore()
  42. class CustomError(Exception):
  43. pass
  44. with pytest.raises(CustomError):
  45. with mds.txn() as txn:
  46. raise CustomError()
  47. with pytest.raises(CustomError):
  48. with mds.txn() as txn:
  49. with mds.cur(index='checksums', txn=txn) as cur:
  50. raise CustomError()
  51. with pytest.raises(CustomError):
  52. with mds.cur(index='checksums') as cur:
  53. raise CustomError()
  54. with pytest.raises(CustomError):
  55. with mds.txn(write=True) as txn:
  56. raise CustomError()
  57. with pytest.raises(CustomError):
  58. with mds.txn(write=True) as txn:
  59. with mds.cur(index='checksums', txn=txn) as cur:
  60. raise CustomError()
  61. with pytest.raises(CustomError):
  62. with mds.cur(index='checksums', write=True) as cur:
  63. raise CustomError()