1
1
import pytest
2
2
from pathlib import Path
3
+ import os
4
+
3
5
from ..src .openize .markitdown .converters import WordConverter , PDFConverter , ExcelConverter , PowerPointConverter
4
6
from ..src .openize .markitdown .factory import ConverterFactory
5
- from ..src .openize .markitdown .llm_strategy import SaveLocally , InsertIntoLLM
7
+ from ..src .openize .markitdown .llm_strategy import SaveLocally , LLMFactory , OpenAIClient , ClaudeClient
6
8
from ..src .openize .markitdown .processor import DocumentProcessor
7
- import os
9
+
8
10
9
11
@pytest .fixture
10
12
def sample_output_dir ():
@@ -18,49 +20,83 @@ def sample_md_file(sample_output_dir):
18
20
md_file .write_text ("# Sample Markdown File\n \n This is a test." )
19
21
return md_file
20
22
21
- # Test Converters
22
- def test_word_converter (sample_output_dir ):
23
+
24
+ # --------- Converter Tests ---------
25
+
26
+ def test_word_converter ():
23
27
converter = WordConverter ()
24
28
assert converter is not None
25
29
26
- def test_pdf_converter (sample_output_dir ):
30
+ def test_pdf_converter ():
27
31
converter = PDFConverter ()
28
32
assert converter is not None
29
33
30
- def test_excel_converter (sample_output_dir ):
34
+ def test_excel_converter ():
31
35
converter = ExcelConverter ()
32
36
assert converter is not None
33
37
34
- def test_ppt_converter (sample_output_dir ):
38
+ def test_ppt_converter ():
35
39
converter = PowerPointConverter ()
36
40
assert converter is not None
37
41
38
- # Test ConverterFactory
42
+
43
+ # --------- Factory Tests ---------
44
+
39
45
def test_converter_factory ():
40
46
assert isinstance (ConverterFactory .get_converter (".docx" ), WordConverter )
41
47
assert isinstance (ConverterFactory .get_converter (".pdf" ), PDFConverter )
42
48
assert isinstance (ConverterFactory .get_converter (".xlsx" ), ExcelConverter )
43
49
assert isinstance (ConverterFactory .get_converter (".pptx" ), PowerPointConverter )
44
50
45
51
46
- # Test LLM Strategy
52
+ # --------- Strategy Pattern Tests ---------
53
+
47
54
def test_save_locally (sample_md_file ):
48
55
strategy = SaveLocally ()
49
56
strategy .process (sample_md_file )
50
57
assert sample_md_file .exists ()
51
58
52
- def test_insert_into_llm (mocker , sample_md_file ):
53
- mocker .patch ("openai.ChatCompletion.create" , return_value = {"choices" : [{"message" : {"content" : "LLM Response" }}]})
54
- strategy = InsertIntoLLM ()
59
+ def test_insert_into_llm_openai (mocker , sample_md_file ):
60
+ mocker .patch ("openai.ChatCompletion.create" , return_value = {
61
+ "choices" : [{"message" : {"content" : "Mocked OpenAI Response" }}]
62
+ })
63
+ strategy = OpenAIClient (provider = "openai" )
64
+ strategy .process (sample_md_file )
65
+
66
+ def test_insert_into_llm_claude (mocker , sample_md_file ):
67
+ mock_anthropic = mocker .patch ("openize.markitdown.llm_strategy.Anthropic" )
68
+ mock_client = mock_anthropic .return_value
69
+ mock_client .messages .create .return_value .content = "Mocked Claude Response"
70
+ strategy = ClaudeClient (provider = "claude" )
55
71
strategy .process (sample_md_file )
56
72
57
- # Test DocumentProcessor
58
- def test_document_processor (mocker , sample_output_dir ):
59
- mocker .patch ("packages.src.openize.markitdown.factory.ConverterFactory.get_converter" , return_value = WordConverter ())
73
+
74
+ # --------- Document Processor Tests ---------
75
+
76
+ def test_document_processor_local_conversion (mocker , sample_output_dir ):
77
+ mock_converter = mocker .patch ("openize.markitdown.factory.ConverterFactory.get_converter" , return_value = WordConverter ())
60
78
processor = DocumentProcessor (output_dir = sample_output_dir )
61
79
processor .process_document ("sample.docx" , insert_into_llm = False )
62
80
output_file = sample_output_dir / "sample.md"
63
81
assert output_file .exists ()
64
82
65
- if __name__ == "__main__" :
66
- pytest .main ()
83
+ def test_document_processor_with_llm_openai (mocker , sample_output_dir ):
84
+ mock_converter = mocker .patch ("openize.markitdown.factory.ConverterFactory.get_converter" , return_value = WordConverter ())
85
+ mocker .patch ("openai.ChatCompletion.create" , return_value = {
86
+ "choices" : [{"message" : {"content" : "LLM Output" }}]
87
+ })
88
+ processor = DocumentProcessor (output_dir = sample_output_dir )
89
+ processor .process_document ("sample.docx" , insert_into_llm = True , llm_provider = "openai" )
90
+ output_file = sample_output_dir / "sample.md"
91
+ assert output_file .exists ()
92
+
93
+ def test_document_processor_with_llm_claude (mocker , sample_output_dir ):
94
+ mock_converter = mocker .patch ("openize.markitdown.factory.ConverterFactory.get_converter" , return_value = WordConverter ())
95
+ mock_anthropic = mocker .patch ("openize.markitdown.llm_strategy.Anthropic" )
96
+ mock_client = mock_anthropic .return_value
97
+ mock_client .messages .create .return_value .content = "LLM Claude Output"
98
+ processor = DocumentProcessor (output_dir = sample_output_dir )
99
+ processor .process_document ("sample.docx" , insert_into_llm = True , llm_provider = "claude" )
100
+ output_file = sample_output_dir / "sample.md"
101
+ assert output_file .exists ()
102
+
0 commit comments