Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

base.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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(length: int = 4) -> bytes:
  13. chars = "test"
  14. result = "".join(chars[i % len(chars)] for i in range(length)).encode()
  15. assert len(result) == length
  16. return result
  17. def get_example_filepath() -> str:
  18. return "~/test"
  19. class BaseStorageTest:
  20. @pytest.fixture(autouse=True)
  21. def setup_method(self, *args, **kwargs):
  22. """Should be implemented in child classes to setup specific storage."""
  23. self.storage: BaseStorage
  24. def test_save(self):
  25. """Test saving data."""
  26. self.storage.save(get_example_filename(), get_example_data())
  27. def test_load_once(self):
  28. """Test loading data once."""
  29. assert self.storage.load_once(get_example_filename()) == get_example_data()
  30. def test_load_stream(self):
  31. """Test loading data as a stream."""
  32. generator = self.storage.load_stream(get_example_filename())
  33. assert isinstance(generator, Generator)
  34. assert next(generator) == get_example_data()
  35. def test_download(self):
  36. """Test downloading data."""
  37. self.storage.download(get_example_filename(), get_example_filepath())
  38. def test_exists(self):
  39. """Test checking if a file exists."""
  40. assert self.storage.exists(get_example_filename())
  41. def test_delete(self):
  42. """Test deleting a file."""
  43. self.storage.delete(get_example_filename())