base.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_example_filename() -> str:
  9. return "test.txt"
  10. def get_example_data() -> bytes:
  11. return b"test"
  12. def get_example_filepath() -> str:
  13. return "/test"
  14. class BaseStorageTest:
  15. @pytest.fixture(autouse=True)
  16. def setup_method(self):
  17. """Should be implemented in child classes to setup specific storage."""
  18. self.storage = BaseStorage()
  19. def test_save(self):
  20. """Test saving data."""
  21. self.storage.save(get_example_filename(), get_example_data())
  22. def test_load_once(self):
  23. """Test loading data once."""
  24. assert self.storage.load_once(get_example_filename()) == get_example_data()
  25. def test_load_stream(self):
  26. """Test loading data as a stream."""
  27. generator = self.storage.load_stream(get_example_filename())
  28. assert isinstance(generator, Generator)
  29. assert next(generator) == get_example_data()
  30. def test_download(self):
  31. """Test downloading data."""
  32. self.storage.download(get_example_filename(), get_example_filepath())
  33. def test_exists(self):
  34. """Test checking if a file exists."""
  35. assert self.storage.exists(get_example_filename())
  36. def test_delete(self):
  37. """Test deleting a file."""
  38. self.storage.delete(get_example_filename())