"""Tests for file_processor module.""" import os import pytest import tempfile import json from factsdb.file_processor import FileProcessor @pytest.fixture def processor(): return FileProcessor() class TestDetectFileType: def test_txt(self, processor): assert processor.detect_file_type("file.txt") == "txt" def test_html(self, processor): assert processor.detect_file_type("page.html") == "html" def test_htm(self, processor): assert processor.detect_file_type("page.htm") == "htm" def test_pdf(self, processor): assert processor.detect_file_type("doc.pdf") == "pdf" def test_json(self, processor): assert processor.detect_file_type("data.json") == "json" def test_xml(self, processor): assert processor.detect_file_type("data.xml") == "xml" def test_uppercase(self, processor): assert processor.detect_file_type("file.TXT") == "txt" def test_no_extension(self, processor): assert processor.detect_file_type("file") == "" class TestIsSupportedFileType: def test_supported(self, processor, tmp_path): for ext in ["txt", "md", "log", "html", "htm", "pdf", "xml", "json"]: f = tmp_path / f"file.{ext}" f.touch() assert processor.is_supported_file_type(str(f)) is True def test_unsupported(self, processor, tmp_path): f = tmp_path / "file.xyz" f.touch() assert processor.is_supported_file_type(str(f)) is False class TestExtractTextFromFile: def test_text_file(self, processor, tmp_path): f = tmp_path / "test.txt" f.write_text("Hello World") assert processor.extract_text_from_file(str(f)) == "Hello World" def test_html_file(self, processor, tmp_path): f = tmp_path / "test.html" f.write_text("
Hello
") text = processor.extract_text_from_file(str(f)) assert "Hello" in text def test_json_file(self, processor, tmp_path): f = tmp_path / "test.json" data = {"key": "value"} f.write_text(json.dumps(data)) text = processor.extract_text_from_file(str(f)) assert "key" in text def test_unsupported_type(self, processor, tmp_path): f = tmp_path / "test.xyz" f.write_text("x") with pytest.raises(Exception, match="Unsupported file type"): processor.extract_text_from_file(str(f)) def test_nonexistent_file(self, processor): with pytest.raises(Exception): processor.extract_text_from_file("/nonexistent/path/file.txt") class TestHtmlExtraction: def test_removes_scripts(self, processor, tmp_path): f = tmp_path / "test.html" f.write_text("Content
") text = processor.extract_text_from_file(str(f)) assert "alert" not in text assert "Content" in text def test_removes_styles(self, processor, tmp_path): f = tmp_path / "test.html" f.write_text("Content
") text = processor.extract_text_from_file(str(f)) assert "Content" in text class TestGetFileInfo: def test_basic_info(self, processor, tmp_path): f = tmp_path / "test.txt" f.write_text("hello") info = processor.get_file_info(str(f)) assert info["name"] == "test.txt" assert info["type"] == "txt" assert info["is_supported"] is True assert info["size"] == 5 def test_unsupported_info(self, processor, tmp_path): f = tmp_path / "test.xyz" f.write_text("hello") info = processor.get_file_info(str(f)) assert info["is_supported"] is False