Fix tests

This commit is contained in:
Jeremiah Lowin 2024-12-03 13:58:29 -05:00
commit fae91fa2f6
2 changed files with 12 additions and 2 deletions

View file

@ -8,7 +8,7 @@ from typing import Any, Callable, Union
import httpx
import pydantic.json
import pydantic_core
from pydantic import Field
from pydantic import Field, ValidationInfo
from fastmcp.resources.base import Resource
@ -91,6 +91,15 @@ class FileResource(Resource):
raise ValueError("Path must be absolute")
return path
@pydantic.field_validator("is_binary")
@classmethod
def set_binary_from_mime_type(cls, is_binary: bool, info: ValidationInfo) -> bool:
"""Set is_binary based on mime_type if not explicitly set."""
if is_binary:
return True
mime_type = info.data.get("mime_type", "text/plain")
return not mime_type.startswith("text/")
async def read(self) -> Union[str, bytes]:
"""Read the file content."""
try:

View file

@ -22,7 +22,8 @@ class TestResourceTemplate:
assert template.uri_template == "test://{key}/{value}"
assert template.name == "test"
assert template.mime_type == "text/plain" # default
assert template.fn == my_func
test_input = {"key": "test", "value": 42}
assert template.fn(**test_input) == my_func(**test_input)
def test_template_matches(self):
"""Test matching URIs against a template."""