"""Tests for UPath/fsspec support for cloud storage compatibility.""" import uuid from pathlib import Path import pytest from PIL import Image as PILImage from pydantic import AnyUrl from upath import UPath from docling_core.types.doc import DoclingDocument, ImageRefMode from docling_core.types.doc.base import Size from docling_core.types.doc.document import ImageRef from docling_core.types.doc.utils import is_remote_path, relative_path @pytest.fixture def memory_base(): """Provide a unique memory:// base path for each test.""" return UPath(f"memory://test-{uuid.uuid4()}") # ============================================================================= # Tests for is_remote_path() # ============================================================================= def test_is_remote_path(): """Test is_remote_path() function with various path types.""" # Local paths should return False assert is_remote_path(Path("/local/path")) is False assert is_remote_path(Path(".")) is False assert is_remote_path(None) is False assert is_remote_path("/some/path") is False assert is_remote_path(object()) is False # File protocol should return False class MockFilePath: protocol = "file" class MockEmptyProtocol: protocol = "" assert is_remote_path(MockFilePath()) is False assert is_remote_path(MockEmptyProtocol()) is False # Cloud protocols should return True for protocol in ["s3", "gcs", "az", "http", "https"]: class MockCloudPath: pass MockCloudPath.protocol = protocol assert is_remote_path(MockCloudPath()) is True # Real UPath objects assert is_remote_path(UPath("/tmp/test")) is False assert is_remote_path(UPath("memory://test/path")) is True # ============================================================================= # Tests for relative_path() # ============================================================================= def test_relative_path(): """Test relative_path() function with various scenarios.""" # Basic relative path computation assert relative_path(Path("/a/b"), Path("/a/b/c/d.txt")) == Path("c/d.txt") assert relative_path("/a/b", "/a/b/c.txt") == Path("c.txt") # Navigation with parent directories # Sibling directory assert relative_path(Path("/a/b/c"), Path("/a/b/d/e.txt")) == Path("../d/e.txt") # Parent directory assert relative_path(Path("/a/b/c/d"), Path("/a/b/e.txt")) == Path("../../e.txt") # UPath with local file protocol src = UPath("/home/user/docs") target = UPath("/home/user/docs/images/img.png") assert relative_path(src, target) == Path("images/img.png") # ============================================================================= # UPath integration tests (memory filesystem) # ============================================================================= def test_upath_integration(sample_doc, memory_base): """Test UPath integration with various document operations.""" # JSON roundtrip json_path = memory_base / "doc.json" sample_doc.save_as_json(json_path) assert json_path.exists() loaded = DoclingDocument.load_from_json(json_path) assert sample_doc.export_to_dict() == loaded.export_to_dict() # YAML roundtrip yaml_path = memory_base / "doc.yaml" sample_doc.save_as_yaml(yaml_path) assert yaml_path.exists() loaded = DoclingDocument.load_from_yaml(yaml_path) assert sample_doc.export_to_dict() == loaded.export_to_dict() # Markdown export md_path = memory_base / "doc.md" sample_doc.save_as_markdown(md_path) assert md_path.exists() assert len(md_path.read_text()) > 0 # HTML export html_path = memory_base / "doc.html" sample_doc.save_as_html(html_path) assert html_path.exists() assert "