Files
rapidocr/python/tests/test_cli.py
T
SWHL 017b8fd5c7 feat: add en/el/th ppocr_v5_rec_mobile paddle/onnx/openvino (#546)
* feat: add en/el/th ppocr_v5_rec_mobile paddle/onnx/openvino

* test: change to a more secure code
2025-09-06 17:50:05 +08:00

82 lines
1.9 KiB
Python

# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
import shlex
import sys
from pathlib import Path
import pytest
from pytest import mark
root_dir = Path(__file__).resolve().parent.parent
sys.path.append(str(root_dir))
from rapidocr import RapidOCR
from rapidocr.main import main
tests_dir = root_dir / "tests" / "test_files"
img_path = tests_dir / "ch_en_num.jpg"
@pytest.fixture()
def engine():
engine = RapidOCR()
return engine
@mark.parametrize("cmd,gt", [(f"--img_path {img_path}", "正品促销")])
def test_cli(capsys, cmd, gt):
main(shlex.split(cmd))
output = capsys.readouterr().out.strip()
assert gt in output
@mark.parametrize("cmd", [f"config --save_cfg_file {tests_dir}/config.yaml"])
def test_cli_config(capsys, cmd):
main(shlex.split(cmd))
output = capsys.readouterr().out.strip()
assert "The config file has saved in" in output
cfg_yaml_path = tests_dir / "config.yaml"
assert cfg_yaml_path.exists()
cfg_yaml_path.unlink()
@mark.parametrize("cmd", ["check"])
def test_cli_check(capsys, cmd):
main(shlex.split(cmd))
output = capsys.readouterr().out.strip()
assert "Success! rapidocr is installed correctly!" in output
@mark.parametrize(
"cmd,img_name",
[
(
f"--img_path {img_path} -vis --vis_save_dir {tests_dir}",
f"{img_path.stem}_vis.png",
),
(
f"--img_path {img_path} -vis --vis_save_dir {tests_dir} -word",
f"{img_path.stem}_vis_single.png",
),
],
)
def test_cli_vis(cmd, img_name):
main(shlex.split(cmd))
vis_path = tests_dir / img_name
assert vis_path.exists()
vis_path.unlink()
def test_cli_lang_type():
img_path = tests_dir / "japan.jpg"
cmd = f"--img_path {img_path} --lang_type japan -vis --vis_save_dir {tests_dir}"
vis_path = tests_dir / f"{img_path.stem}_vis.png"
main(shlex.split(cmd))
assert vis_path.exists()
vis_path.unlink()