Skip to content

Commit

Permalink
[test] update test_ocr.py for add test exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
GNEHUY committed Mar 7, 2024
1 parent e583fb9 commit a855f0e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/test_sif/test_ocr.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)

0 comments on commit a855f0e

Please sign in to comment.