File size: 3,076 Bytes
7b0cda0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pytest
from unittest.mock import patch, MagicMock
from pathlib import Path
import pandas as pd
import numpy as np

from src.pipeline import process_all_images_to_excel   # заменить на реальный путь


@patch("src.pipeline.extract_text")
@patch("src.pipeline.iter_input_images")
@patch("src.pipeline.pd.DataFrame.to_excel")
def test_process_all_images_to_excel_ok(mock_to_excel, mock_iter, mock_extract, tmp_path):
    # Подготовим конфиг
    cfg = {
        "paths": {
            "input": str(tmp_path / "input"),
            "output": str(tmp_path / "output")
        },
        "export": {
            "excel_filename": "out.xlsx"
        },
        "colors": {"dummy_color": ([0,0,0],[255,255,255])}
    }

    # Создаём папки
    (tmp_path / "input").mkdir()
    (tmp_path / "output").mkdir()

    # --- Мокаем iter_input_images ---
    mock_iter.return_value = [
        ("img1", np.zeros((10,10,3))),
        ("img2", np.zeros((10,10,3))),
    ]

    # --- Мокаем extract_text ---
    mock_extract.side_effect = [
        ("TITLE1", [{"text": "S1", "score": 0.9}]),
        ("TITLE2", [{"text": "S2", "score": 0.95}]),
    ]

    # --- Выполняем функцию ---
    process_all_images_to_excel(cfg)

    # --- ПРОВЕРКИ ---

    # extract_text должен быть вызван 2 раза
    assert mock_extract.call_count == 2

    # Excel действительно формировался
    mock_to_excel.assert_called_once()

    # Проверяем, что путь правильный
    called_path, called_kwargs = mock_to_excel.call_args[0][0], mock_to_excel.call_args[1]
    assert called_kwargs["index"] is False
    assert "openpyxl" in called_kwargs["engine"]

    # Проверяем, что DataFrame содержит нужные строки
    df_created: pd.DataFrame = mock_to_excel.call_args[0][0]  # аргумент-DataFrame

    assert len(df_created) == 2
    assert set(df_created.columns) == {"filename", "title", "sensor_name", "score"}

    assert df_created.iloc[0]["filename"] == "img1"
    assert df_created.iloc[0]["title"] == "TITLE1"
    assert df_created.iloc[0]["sensor_name"] == "S1"
    assert df_created.iloc[0]["score"] == 0.9


@patch("src.pipeline.iter_input_images")
@patch("src.pipeline.pd.DataFrame.to_excel")
def test_process_all_images_no_data(mock_to_excel, mock_iter, tmp_path, capsys):
    cfg = {
        "paths": {
            "input": str(tmp_path / "input"),
            "output": str(tmp_path / "output")
        },
        "export": {"excel_filename": "out.xlsx"},
        "colors": {}
    }

    (tmp_path / "input").mkdir()
    (tmp_path / "output").mkdir()

    # iter_input_images вернёт пустой список
    mock_iter.return_value = []

    process_all_images_to_excel(cfg)

    # Excel не должен создаваться
    mock_to_excel.assert_not_called()

    # Проверяем вывод
    captured = capsys.readouterr()
    assert "нет данных" in captured.out.lower()