Skip to content

Commit

Permalink
fix(file_upload): correct validation method and add unit tests (langg…
Browse files Browse the repository at this point in the history
  • Loading branch information
laipz8200 authored and JunXu01 committed Nov 9, 2024
1 parent 60c45d0 commit 850880f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/core/app/app_config/features/file_upload/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def validate_and_set_defaults(cls, config: dict) -> tuple[dict, list[str]]:
if not config.get("file_upload"):
config["file_upload"] = {}
else:
FileExtraConfig.model_validate(config["file_upload"])
FileUploadConfig.model_validate(config["file_upload"])

return config, ["file_upload"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from core.app.app_config.features.file_upload.manager import FileUploadConfigManager
from core.file.models import FileTransferMethod, FileUploadConfig, ImageConfig
from core.model_runtime.entities.message_entities import ImagePromptMessageContent


def test_convert_with_vision():
config = {
"file_upload": {
"enabled": True,
"number_limits": 5,
"allowed_file_upload_methods": [FileTransferMethod.REMOTE_URL],
"image": {"detail": "high"},
}
}
result = FileUploadConfigManager.convert(config, is_vision=True)
expected = FileUploadConfig(
image_config=ImageConfig(
number_limits=5,
transfer_methods=[FileTransferMethod.REMOTE_URL],
detail=ImagePromptMessageContent.DETAIL.HIGH,
)
)
assert result == expected


def test_convert_without_vision():
config = {
"file_upload": {
"enabled": True,
"number_limits": 5,
"allowed_file_upload_methods": [FileTransferMethod.REMOTE_URL],
}
}
result = FileUploadConfigManager.convert(config, is_vision=False)
expected = FileUploadConfig(
image_config=ImageConfig(number_limits=5, transfer_methods=[FileTransferMethod.REMOTE_URL])
)
assert result == expected


def test_validate_and_set_defaults():
config = {}
result, keys = FileUploadConfigManager.validate_and_set_defaults(config)
assert "file_upload" in result
assert keys == ["file_upload"]


def test_validate_and_set_defaults_with_existing_config():
config = {
"file_upload": {
"enabled": True,
"number_limits": 5,
"allowed_file_upload_methods": [FileTransferMethod.REMOTE_URL],
}
}
result, keys = FileUploadConfigManager.validate_and_set_defaults(config)
assert "file_upload" in result
assert keys == ["file_upload"]
assert result["file_upload"]["enabled"] is True
assert result["file_upload"]["number_limits"] == 5
assert result["file_upload"]["allowed_file_upload_methods"] == [FileTransferMethod.REMOTE_URL]

0 comments on commit 850880f

Please sign in to comment.