"""Tests for Pydantic models."""
from datetime import datetime
import pytest
from pydantic import ValidationError
from sharepoint_mcp.models import (
FileContent,
FileListResult,
FileMetadata,
Library,
ListItem,
SearchResult,
SharingLink,
Site,
UploadResult,
)
def test_site_model():
"""Test Site model validation."""
site = Site(
id="site-1",
name="Test Site",
url="https://contoso.sharepoint.com/sites/test",
description="A test site",
)
assert site.id == "site-1"
assert site.name == "Test Site"
assert site.url == "https://contoso.sharepoint.com/sites/test"
assert site.description == "A test site"
def test_site_model_without_description():
"""Test Site model with optional description."""
site = Site(
id="site-1",
name="Test Site",
url="https://contoso.sharepoint.com/sites/test",
)
assert site.description is None
def test_library_model():
"""Test Library model validation."""
library = Library(
id="drive-1",
name="Documents",
web_url="https://contoso.sharepoint.com/sites/test/Documents",
item_count=42,
)
assert library.id == "drive-1"
assert library.name == "Documents"
assert library.item_count == 42
def test_file_metadata_model():
"""Test FileMetadata model validation."""
metadata = FileMetadata(
id="file-1",
name="report.pdf",
size=1024000,
mime_type="application/pdf",
web_url="https://contoso.sharepoint.com/report.pdf",
created_at=datetime(2025, 1, 1, 10, 0, 0),
modified_at=datetime(2025, 1, 2, 15, 30, 0),
created_by="John Doe",
modified_by="Jane Smith",
)
assert metadata.id == "file-1"
assert metadata.name == "report.pdf"
assert metadata.size == 1024000
assert metadata.mime_type == "application/pdf"
assert metadata.created_by == "John Doe"
assert metadata.modified_by == "Jane Smith"
def test_file_metadata_optional_fields():
"""Test FileMetadata with optional fields."""
metadata = FileMetadata(
id="file-1",
name="document.txt",
size=5000,
web_url="https://contoso.sharepoint.com/doc.txt",
created_at=datetime(2025, 1, 1),
modified_at=datetime(2025, 1, 1),
)
assert metadata.mime_type is None
assert metadata.created_by is None
assert metadata.modified_by is None
def test_file_content_text():
"""Test FileContent model for text files."""
content = FileContent(
name="readme.txt",
mime_type="text/plain",
content="Hello, World!",
is_text=True,
)
assert content.name == "readme.txt"
assert content.is_text is True
assert content.content == "Hello, World!"
def test_file_content_binary():
"""Test FileContent model for binary files."""
content = FileContent(
name="image.png",
mime_type="image/png",
content="iVBORw0KGgoAAAANSUhEUgA==", # Base64
is_text=False,
)
assert content.name == "image.png"
assert content.is_text is False
assert content.content.startswith("iVBOR")
def test_file_list_result():
"""Test FileListResult model."""
files = [
FileMetadata(
id="file-1",
name="doc1.pdf",
size=1000,
web_url="https://contoso.sharepoint.com/doc1.pdf",
created_at=datetime(2025, 1, 1),
modified_at=datetime(2025, 1, 1),
),
FileMetadata(
id="file-2",
name="doc2.pdf",
size=2000,
web_url="https://contoso.sharepoint.com/doc2.pdf",
created_at=datetime(2025, 1, 2),
modified_at=datetime(2025, 1, 2),
),
]
result = FileListResult(
files=files,
next_cursor="cursor-123",
total_count=50,
)
assert len(result.files) == 2
assert result.next_cursor == "cursor-123"
assert result.total_count == 50
def test_upload_result():
"""Test UploadResult model."""
result = UploadResult(
id="file-new",
name="uploaded.txt",
web_url="https://contoso.sharepoint.com/uploaded.txt",
size=5000,
)
assert result.id == "file-new"
assert result.name == "uploaded.txt"
assert result.size == 5000
def test_sharing_link():
"""Test SharingLink model."""
link = SharingLink(
link_url="https://contoso.sharepoint.com/share/abc123",
link_type="view",
expires_at=datetime(2025, 2, 1),
)
assert link.link_url == "https://contoso.sharepoint.com/share/abc123"
assert link.link_type == "view"
assert link.expires_at == datetime(2025, 2, 1)
def test_sharing_link_without_expiration():
"""Test SharingLink without expiration."""
link = SharingLink(
link_url="https://contoso.sharepoint.com/share/abc123",
link_type="edit",
)
assert link.expires_at is None
def test_search_result():
"""Test SearchResult model."""
result = SearchResult(
id="file-1",
name="quarterly-report.pdf",
site_name="Finance",
web_url="https://contoso.sharepoint.com/finance/report.pdf",
snippet="Q4 revenue increased by 15%...",
)
assert result.id == "file-1"
assert result.name == "quarterly-report.pdf"
assert result.site_name == "Finance"
assert result.snippet == "Q4 revenue increased by 15%..."
def test_list_item():
"""Test ListItem model."""
item = ListItem(
id="item-1",
fields={"Title": "Task 1", "Status": "In Progress", "Priority": "High"},
created_at=datetime(2025, 1, 1),
modified_at=datetime(2025, 1, 5),
)
assert item.id == "item-1"
assert item.fields["Title"] == "Task 1"
assert item.fields["Status"] == "In Progress"
assert len(item.fields) == 3
def test_model_validation_error():
"""Test that models raise validation errors for invalid data."""
with pytest.raises(ValidationError):
Site(
# Missing required fields
name="Test Site",
)
with pytest.raises(ValidationError):
FileMetadata(
# Invalid type for size
id="file-1",
name="doc.txt",
size="not-a-number",
web_url="https://example.com",
created_at=datetime(2025, 1, 1),
modified_at=datetime(2025, 1, 1),
)