mirror of
https://github.com/langgenius/dify.git
synced 2026-04-30 07:28:05 +08:00
fix rerank mode is none
This commit is contained in:
63
api/tests/unit_tests/oss/local/test_local.py
Normal file
63
api/tests/unit_tests/oss/local/test_local.py
Normal file
@ -0,0 +1,63 @@
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import mock_open, patch
|
||||
|
||||
from extensions.storage.local_storage import LocalStorage
|
||||
|
||||
|
||||
class TestLocalStorage(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Configuration for each test
|
||||
self.app_config = {'root': '/test'}
|
||||
self.folder = 'test_folder/'
|
||||
self.storage = LocalStorage(self.app_config, self.folder)
|
||||
|
||||
@patch('os.makedirs')
|
||||
def test_save(self, mock_makedirs):
|
||||
# Test the save functionality
|
||||
test_data = b"test data"
|
||||
with patch('builtins.open', mock_open()) as mocked_file:
|
||||
self.storage.save('file.txt', test_data)
|
||||
mocked_file.assert_called_with(os.path.join(os.getcwd(), 'test_folder/file.txt'), "wb")
|
||||
handle = mocked_file()
|
||||
handle.write.assert_called_once_with(test_data)
|
||||
|
||||
@patch('os.path.exists', return_value=True)
|
||||
@patch('builtins.open', new_callable=mock_open, read_data=b"test data")
|
||||
def test_load_once(self, mock_open, mock_exists):
|
||||
# Test the load_once method
|
||||
data = self.storage.load_once('file.txt')
|
||||
self.assertEqual(data, b"test data")
|
||||
|
||||
@patch('os.path.exists', return_value=True)
|
||||
def test_load_stream(self, mock_exists):
|
||||
# Test the load_stream method
|
||||
with patch('builtins.open', mock_open(read_data=b"test data")) as mocked_file:
|
||||
generator = self.storage.load_stream('file.txt')
|
||||
output = list(generator)
|
||||
self.assertEqual(output, [b'test data'])
|
||||
|
||||
@patch('shutil.copyfile')
|
||||
@patch('os.path.exists', return_value=True)
|
||||
def test_download(self, mock_exists, mock_copyfile):
|
||||
# Test the download method
|
||||
self.storage.download('file.txt', 'target.txt')
|
||||
mock_copyfile.assert_called_once_with('test_folder/file.txt', 'target.txt')
|
||||
|
||||
@patch('os.path.exists', return_value=True)
|
||||
def test_exists(self, mock_exists):
|
||||
# Test the exists method
|
||||
self.assertTrue(self.storage.exists('file.txt'))
|
||||
|
||||
@patch('os.path.exists', return_value=True)
|
||||
@patch('os.remove')
|
||||
def test_delete(self, mock_remove, mock_exists):
|
||||
# Test the delete method
|
||||
self.storage.delete('file.txt')
|
||||
mock_remove.assert_called_once_with('test_folder/file.txt')
|
||||
|
||||
@patch('os.path.exists', return_value=False)
|
||||
def test_delete_file_not_found(self, mock_exists):
|
||||
# Test deleting a file that does not exist
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
self.storage.delete('file.txt')
|
||||
61
api/tests/unit_tests/oss/test_oss.py
Normal file
61
api/tests/unit_tests/oss/test_oss.py
Normal file
@ -0,0 +1,61 @@
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from extensions import ext_redis
|
||||
|
||||
|
||||
def get_example_filename() -> str:
|
||||
return 'test_text.txt'
|
||||
|
||||
|
||||
def get_example_file_data() -> bytes:
|
||||
return b'test_text'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup_mock_redis() -> None:
|
||||
# get
|
||||
ext_redis.redis_client.get = MagicMock(return_value=None)
|
||||
|
||||
# set
|
||||
ext_redis.redis_client.set = MagicMock(return_value=None)
|
||||
|
||||
# lock
|
||||
mock_redis_lock = MagicMock()
|
||||
mock_redis_lock.__enter__ = MagicMock()
|
||||
mock_redis_lock.__exit__ = MagicMock()
|
||||
ext_redis.redis_client.lock = mock_redis_lock
|
||||
|
||||
|
||||
class AbstractOssTest:
|
||||
def __init__(self):
|
||||
self.client = None
|
||||
self.filename = get_example_filename()
|
||||
self.data = get_example_file_data()
|
||||
|
||||
def save(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def load_once(self) -> bytes:
|
||||
raise NotImplementedError
|
||||
|
||||
def load_stream(self) -> Generator:
|
||||
raise NotImplementedError
|
||||
|
||||
def download(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def exists(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def delete(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def run_all_tests(self):
|
||||
self.save()
|
||||
self.load_once()
|
||||
self.load_stream()
|
||||
self.exists()
|
||||
self.delete()
|
||||
Reference in New Issue
Block a user