Skip to content

Commit a8b60d2

Browse files
authored
Merge pull request #11 from KSUSeniorProject-MathLearningMachine/add-basic-tests
Add basic tests
2 parents 5d4208f + 03bef71 commit a8b60d2

File tree

6 files changed

+132
-7
lines changed

6 files changed

+132
-7
lines changed

.github/workflows/docker-image.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Docker Image CI
2+
3+
on:
4+
push:
5+
branches: [ development ]
6+
pull_request:
7+
branches: [ development ]
8+
9+
jobs:
10+
11+
build:
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Update the environment files
18+
run: |
19+
echo "MATHPIX_APP_KEY=${{ secrets.MATHPIX_APP_KEY }}" >> mathpix-variables.env
20+
echo "MATHPIX_APP_ID=${{ secrets.MATHPIX_APP_ID }}" >> mathpix-variables.env
21+
- name: Build the Docker image
22+
run: |
23+
docker-compose build
24+
- name: Run the test suite
25+
run: |
26+
docker-compose run flask bash -c "cd /app/flask/flaskapp && python -m pytest"
27+

flask/flaskapp/tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
from flaskapp import app as flask_app
4+
5+
@pytest.fixture
6+
def app():
7+
yield flask_app
8+
9+
10+
@pytest.fixture
11+
def client(app):
12+
return app.test_client()

flask/flaskapp/tests/imagetest.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

readme.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ Open the integrated terminal to run commands in the container.
3737
3838
`$ flask run` will start the flask development server and send requests to the flask application.
3939

40-
## Running Unit Tests
41-
Run pytest in the top-level project directory to start unit tests.
40+
## Running Tests
41+
42+
To run the tests:
43+
44+
$ docker-compose build
45+
$ docker-compose run flask bash -c "cd /app/flask/flaskapp && python -m pytest"
46+
4247

4348
## Public API
4449

@@ -102,3 +107,5 @@ Run pytest in the top-level project directory to start unit tests.
102107
"text":(str) Doesnt mean much as we are using latex
103108
}
104109

110+
111+

requirements.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1+
antlr4-python3-runtime==4.8
12
attrs==20.2.0
3+
certifi==2020.6.20
4+
chardet==3.0.4
25
click==7.1.2
36
Flask==1.1.2
47
hypothesis==5.33.0
8+
idna==2.10
59
importlib-metadata==1.7.0
610
iniconfig==1.0.1
711
itsdangerous==1.1.0
812
Jinja2==2.11.2
13+
jsonschema==3.2.0
14+
latex2sympy==1.0.3
915
MarkupSafe==1.1.1
1016
more-itertools==8.5.0
17+
mpmath==1.1.0
1118
packaging==20.4
1219
pathlib2==2.3.5
13-
pkg-resources==0.0.0
1420
pluggy==0.13.1
1521
py==1.9.0
1622
pyparsing==2.4.7
23+
pyrsistent==0.17.3
1724
pytest==6.0.1
25+
requests==2.24.0
1826
six==1.15.0
1927
sortedcontainers==2.2.2
28+
sympy==1.6.2
2029
toml==0.10.1
30+
urllib3==1.25.10
2131
Werkzeug==1.0.1
2232
zipp==3.1.0
23-
requests==2.24.0
24-
sympy==1.6.2
25-
latex2sympy==1.0.3
26-
antlr4-python3-runtime==4.8

0 commit comments

Comments
 (0)