|
| 1 | +import jsonschema |
| 2 | +from jsonschema import validate |
| 3 | + |
| 4 | +def test_home_page(client, app): |
| 5 | + """A basic test to ensure the home page is functioning correctly""" |
| 6 | + res = client.get('/') |
| 7 | + assert b'Hello, World!' == res.data |
| 8 | + |
| 9 | + |
| 10 | +def test_mathpix_ocr(client, app): |
| 11 | + with open('tests/imagetest.txt', 'r') as file: |
| 12 | + image = file.read().replace('\n', '') |
| 13 | + |
| 14 | + response_schema = { |
| 15 | + "type": "object", |
| 16 | + "properties": { |
| 17 | + "confidence": {"type": "number"}, |
| 18 | + "latex_styled": {"type": "string"}, |
| 19 | + "request_id": {"type": "string"}, |
| 20 | + }, |
| 21 | + } |
| 22 | + res = client.post('/mathpix-ocr', json={ |
| 23 | + 'b64_img': image |
| 24 | + }) |
| 25 | + |
| 26 | + assert True == validate_json(instance=res.get_json(), schema=response_schema) |
| 27 | + |
| 28 | + |
| 29 | +def test_solve_image(client, app): |
| 30 | + with open('tests/imagetest.txt', 'r') as file: |
| 31 | + image = file.read().replace('\n', '') |
| 32 | + |
| 33 | + response_schema = { |
| 34 | + "type": "object", |
| 35 | + "properties": { |
| 36 | + "confidence": {"type": "number"}, |
| 37 | + "input_detected": {"type": "string"}, |
| 38 | + "solved": {"type": "string"}, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + res = client.post('/solve-image', json={ |
| 43 | + 'b64_img': image |
| 44 | + }) |
| 45 | + |
| 46 | + assert True == validate_json(instance=res.get_json(), schema=response_schema) |
| 47 | + assert r'F(0)=x' == res.get_json()['input_detected'] |
| 48 | + assert r'F{\left(0 \right)} = x' == res.get_json()['solved'] |
| 49 | + |
| 50 | + |
| 51 | +def test_solve_latex(client, app): |
| 52 | + response_schema = { |
| 53 | + "type": "object", |
| 54 | + "properties": { |
| 55 | + "solved": {"type": "string"}, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + res = client.post('/solve-latex', json={ |
| 60 | + 'latex': r'F(x)=2+2' |
| 61 | + }) |
| 62 | + |
| 63 | + assert True == validate_json(instance=res.get_json(), schema=response_schema) |
| 64 | + assert r'F{\left(x \right)} = 4' == res.get_json()['solved'] |
| 65 | + |
| 66 | + |
| 67 | +def validate_json(instance, schema): |
| 68 | + try: |
| 69 | + validate(instance, schema) |
| 70 | + except jsonschema.exceptions.ValidationError as err: |
| 71 | + return False |
| 72 | + return True |
0 commit comments