base.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from collections.abc import Generator
  2. import pytest
  3. from extensions.storage.base_storage import BaseStorage
  4. def get_example_folder() -> str:
  5. return "~/dify"
  6. def get_example_bucket() -> str:
  7. return "dify"
  8. def get_opendal_bucket() -> str:
  9. return "./dify"
  10. def get_example_filename() -> str:
  11. return "test.txt"
  12. def get_example_data() -> bytes:
  13. return b"test"
  14. def get_example_filepath() -> str:
  15. return "~/test"
  16. class BaseStorageTest:
  17. @pytest.fixture(autouse=True)
  18. def setup_method(self, *args, **kwargs):
  19. """Should be implemented in child classes to setup specific storage."""
  20. self.storage: BaseStorage
  21. def test_save(self):
  22. """Test saving data."""
  23. self.storage.save(get_example_filename(), get_example_data())
  24. def test_load_once(self):
  25. """Test loading data once."""
  26. assert self.storage.load_once(get_example_filename()) == get_example_data()
  27. def test_load_stream(self):
  28. """Test loading data as a stream."""
  29. generator = self.storage.load_stream(get_example_filename())
  30. assert isinstance(generator, Generator)
  31. assert next(generator) == get_example_data()
  32. def test_download(self):
  33. """Test downloading data."""
  34. self.storage.download(get_example_filename(), get_example_filepath())
  35. def test_exists(self):
  36. """Test checking if a file exists."""
  37. assert self.storage.exists(get_example_filename())
  38. def test_delete(self):
  39. """Test deleting a file."""
  40. self.storage.delete(get_example_filename())