diff --git a/lib/crewai-tools/src/crewai_tools/rag/data_types.py b/lib/crewai-tools/src/crewai_tools/rag/data_types.py index 27ee48abb0..94d1dd58ba 100644 --- a/lib/crewai-tools/src/crewai_tools/rag/data_types.py +++ b/lib/crewai-tools/src/crewai_tools/rag/data_types.py @@ -123,8 +123,9 @@ def get_file_type(path: str) -> DataType | None: ".xml": DataType.XML, ".txt": DataType.TEXT_FILE, } + path_lower = path.lower() for ext, dtype in mapping.items(): - if path.endswith(ext): + if path_lower.endswith(ext): return dtype return None diff --git a/lib/crewai-tools/tests/rag/test_data_types.py b/lib/crewai-tools/tests/rag/test_data_types.py new file mode 100644 index 0000000000..41e8a35fc2 --- /dev/null +++ b/lib/crewai-tools/tests/rag/test_data_types.py @@ -0,0 +1,60 @@ +import os +import tempfile +from collections.abc import Callable, Iterator + +from crewai_tools.rag.data_types import DataType, DataTypes +import pytest + + +@pytest.fixture +def temp_file() -> Iterator[Callable[[str], str]]: + created_files: list[str] = [] + + def _create(suffix: str) -> str: + f = tempfile.NamedTemporaryFile(mode="w", suffix=suffix, delete=False) + f.write("content") + f.close() + created_files.append(f.name) + return f.name + + yield _create + + for path in created_files: + os.unlink(path) + + +class TestDataTypesFromContent: + @pytest.mark.parametrize( + "suffix,expected", + [ + (".pdf", DataType.PDF_FILE), + (".PDF", DataType.PDF_FILE), + (".csv", DataType.CSV), + (".CSV", DataType.CSV), + (".docx", DataType.DOCX), + (".DOCX", DataType.DOCX), + (".json", DataType.JSON), + (".Json", DataType.JSON), + (".xml", DataType.XML), + (".XML", DataType.XML), + (".md", DataType.MDX), + (".MD", DataType.MDX), + ], + ) + def test_file_extension_detection_is_case_insensitive( + self, temp_file: Callable[[str], str], suffix: str, expected: DataType + ) -> None: + path = temp_file(suffix) + assert DataTypes.from_content(path) == expected + + def test_uppercase_pdf_url_is_detected_as_pdf(self) -> None: + assert ( + DataTypes.from_content("https://example.com/file.PDF") + == DataType.PDF_FILE + ) + + def test_lowercase_pdf_url_is_detected_as_pdf(self) -> None: + assert ( + DataTypes.from_content("https://example.com/file.pdf") + == DataType.PDF_FILE + )