From a855f0e053b797bc729b117b5a77bcb48e72f0a2 Mon Sep 17 00:00:00 2001 From: DeHors Date: Thu, 7 Mar 2024 14:11:00 +0800 Subject: [PATCH] [test] update test_ocr.py for add test exceptions --- tests/test_sif/test_ocr.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_sif/test_ocr.py b/tests/test_sif/test_ocr.py index c010e75c..a94af0c1 100644 --- a/tests/test_sif/test_ocr.py +++ b/tests/test_sif/test_ocr.py @@ -1,8 +1,11 @@ # 2024/3/5 @ yuheng import pytest +import json from EduNLP.SIF.segment import seg +from EduNLP.SIF.parser.ocr import ocr_formula_figure, FormulaRecognitionError +from unittest.mock import patch def test_ocr(figure0, figure1, figure0_base64, figure1_base64): @@ -26,3 +29,34 @@ def test_ocr(figure0, figure1, figure0_base64, figure1_base64): figures=True ) assert seg_test_text.text_segments == ['如图所示,有三组机器人在踢足球'] + + +def test_ocr_formula_figure_exceptions(figure0_base64): + """Simulate a non-200 status code""" + with patch('EduNLP.SIF.parser.ocr.requests.post') as mock_post: + mock_post.return_value.status_code = 404 + with pytest.raises(FormulaRecognitionError) as exc_info: + ocr_formula_figure(figure0_base64, is_base64=True) + assert "HTTP error 404" in str(exc_info.value) + + """Simulate an invalid JSON response""" + with patch('EduNLP.SIF.parser.ocr.requests.post') as mock_post: + mock_post.return_value.status_code = 200 + mock_post.return_value.content = b"invalid_json_response" + with pytest.raises(FormulaRecognitionError) as exc_info: + ocr_formula_figure(figure0_base64, is_base64=True) + assert "Error processing response" in str(exc_info.value) + + """Simulate image not recognized as a formula""" + with patch('EduNLP.SIF.parser.ocr.requests.post') as mock_post: + mock_post.return_value.status_code = 200 + mock_post.return_value.content = json.dumps({ + "data": { + 'success': 1, + 'is_formula': 0, + 'detect_formula': 0 + } + }).encode('utf-8') + with pytest.raises(FormulaRecognitionError) as exc_info: + ocr_formula_figure(figure0_base64, is_base64=True) + assert "Image is not recognized as a formula" in str(exc_info.value)