File size: 1,709 Bytes
be754f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import importlib
import sys
import types
from pathlib import Path

from fastapi.testclient import TestClient


SPACE_DIR = Path(__file__).resolve().parents[1]


class FakePaddleOCR:
    def __init__(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs

    def ocr(self, *args, **kwargs):
        return [[
            [None, ("Cost 4", 0.99)],
            [None, ("Crit Rate", 0.98)],
            [None, ("10.5%", 0.97)],
        ]]


def load_app(monkeypatch):
    if str(SPACE_DIR) not in sys.path:
        sys.path.insert(0, str(SPACE_DIR))

    fake_paddleocr = types.ModuleType("paddleocr")
    fake_paddleocr.PaddleOCR = FakePaddleOCR
    monkeypatch.setitem(sys.modules, "paddleocr", fake_paddleocr)

    for module_name in (
        "main",
        "ocr_config",
        "ocr_engine",
        "ocr_service",
        "image_processing",
    ):
        sys.modules.pop(module_name, None)

    return importlib.import_module("main")


def test_health_returns_supported_languages(monkeypatch):
    main = load_app(monkeypatch)
    client = TestClient(main.app)

    response = client.get("/health?lang=jp")

    assert response.status_code == 200
    assert response.json()["ok"] is True
    assert response.json()["lang"] == "jp"
    assert set(response.json()["supported_langs"]) == {"kr", "en", "jp", "zh"}


def test_wake_loads_requested_language(monkeypatch):
    main = load_app(monkeypatch)
    client = TestClient(main.app)

    response = client.get("/wake?lang=en")

    assert response.status_code == 200
    body = response.json()
    assert body["ok"] is True
    assert body["lang"] == "en"
    assert body["loaded"] is True
    assert "en" in body["loaded_langs"]