1
+ from unittest .mock import MagicMock , patch
2
+
1
3
import pytest
2
4
from typer .testing import CliRunner
3
- from unittest . mock import patch , MagicMock
5
+
4
6
from datafog .client import app
5
7
6
8
runner = CliRunner ()
7
9
10
+
8
11
@pytest .fixture
9
12
def mock_datafog ():
10
- with patch (' datafog.client.DataFog' ) as mock :
13
+ with patch (" datafog.client.DataFog" ) as mock :
11
14
yield mock
12
15
16
+
13
17
def test_scan_image_no_urls ():
14
18
result = runner .invoke (app , ["scan-image" ])
15
19
assert result .exit_code == 1
16
20
assert "No image URLs or file paths provided" in result .stdout
17
21
22
+
18
23
@pytest .mark .asyncio
19
24
async def test_scan_image_success (mock_datafog ):
20
25
mock_instance = mock_datafog .return_value
21
26
mock_instance .run_ocr_pipeline .return_value = ["Mocked result" ]
22
-
23
- with patch (' datafog.client.asyncio.run' , new = lambda x : x ):
27
+
28
+ with patch (" datafog.client.asyncio.run" , new = lambda x : x ):
24
29
result = runner .invoke (app , ["scan-image" , "http://example.com/image.jpg" ])
25
-
30
+
26
31
assert result .exit_code == 0
27
32
assert "OCR Pipeline Results: ['Mocked result']" in result .stdout
28
- mock_instance .run_ocr_pipeline .assert_called_once_with (image_urls = ["http://example.com/image.jpg" ])
33
+ mock_instance .run_ocr_pipeline .assert_called_once_with (
34
+ image_urls = ["http://example.com/image.jpg" ]
35
+ )
36
+
29
37
30
38
def test_scan_text_no_texts ():
31
39
result = runner .invoke (app , ["scan-text" ])
32
40
assert result .exit_code == 1
33
41
assert "No texts provided" in result .stdout
34
42
43
+
35
44
@pytest .mark .asyncio
36
45
async def test_scan_text_success (mock_datafog ):
37
46
mock_instance = mock_datafog .return_value
38
47
mock_instance .run_text_pipeline .return_value = ["Mocked result" ]
39
-
40
- with patch (' datafog.client.asyncio.run' , new = lambda x : x ):
48
+
49
+ with patch (" datafog.client.asyncio.run" , new = lambda x : x ):
41
50
result = runner .invoke (app , ["scan-text" , "Sample text" ])
42
-
51
+
43
52
assert result .exit_code == 0
44
53
assert "Text Pipeline Results: ['Mocked result']" in result .stdout
45
54
mock_instance .run_text_pipeline .assert_called_once_with (str_list = ["Sample text" ])
46
55
56
+
47
57
def test_health ():
48
58
result = runner .invoke (app , ["health" ])
49
59
assert result .exit_code == 0
50
60
assert "DataFog is running" in result .stdout
51
61
52
- @patch ('datafog.client.get_config' )
62
+
63
+ @patch ("datafog.client.get_config" )
53
64
def test_show_config (mock_get_config ):
54
65
mock_get_config .return_value = {"key" : "value" }
55
66
result = runner .invoke (app , ["show-config" ])
56
67
assert result .exit_code == 0
57
68
assert "{'key': 'value'}" in result .stdout
58
69
59
- @patch ('datafog.client.SpacyAnnotator.download_model' )
70
+
71
+ @patch ("datafog.client.SpacyAnnotator.download_model" )
60
72
def test_download_model (mock_download_model ):
61
73
result = runner .invoke (app , ["download-model" , "en_core_web_sm" ])
62
74
assert result .exit_code == 0
63
75
assert "Model en_core_web_sm downloaded" in result .stdout
64
76
mock_download_model .assert_called_once_with ("en_core_web_sm" )
65
77
66
- @patch ('datafog.client.SpacyAnnotator' )
78
+
79
+ @patch ("datafog.client.SpacyAnnotator" )
67
80
def test_show_spacy_model_directory (mock_spacy_annotator ):
68
81
mock_instance = mock_spacy_annotator .return_value
69
82
mock_instance .show_model_path .return_value = "/path/to/model"
70
83
result = runner .invoke (app , ["show-spacy-model-directory" , "en_core_web_sm" ])
71
84
assert result .exit_code == 0
72
85
assert "/path/to/model" in result .stdout
73
86
74
- @patch ('datafog.client.SpacyAnnotator' )
87
+
88
+ @patch ("datafog.client.SpacyAnnotator" )
75
89
def test_list_spacy_models (mock_spacy_annotator ):
76
90
mock_instance = mock_spacy_annotator .return_value
77
91
mock_instance .list_models .return_value = ["model1" , "model2" ]
78
92
result = runner .invoke (app , ["list-spacy-models" ])
79
93
assert result .exit_code == 0
80
94
assert "['model1', 'model2']" in result .stdout
81
95
82
- @patch ('datafog.client.SpacyAnnotator' )
96
+
97
+ @patch ("datafog.client.SpacyAnnotator" )
83
98
def test_list_entities (mock_spacy_annotator ):
84
99
mock_instance = mock_spacy_annotator .return_value
85
100
mock_instance .list_entities .return_value = ["PERSON" , "ORG" ]
86
101
result = runner .invoke (app , ["list-entities" ])
87
102
assert result .exit_code == 0
88
- assert "['PERSON', 'ORG']" in result .stdout
103
+ assert "['PERSON', 'ORG']" in result .stdout
0 commit comments