Skip to content

Commit

Permalink
Merge pull request #11 from KSUSeniorProject-MathLearningMachine/add-…
Browse files Browse the repository at this point in the history
…basic-tests

Add basic tests
  • Loading branch information
kemp authored Sep 18, 2020
2 parents 5d4208f + 03bef71 commit a8b60d2
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 7 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Docker Image CI

on:
push:
branches: [ development ]
pull_request:
branches: [ development ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Update the environment files
run: |
echo "MATHPIX_APP_KEY=${{ secrets.MATHPIX_APP_KEY }}" >> mathpix-variables.env
echo "MATHPIX_APP_ID=${{ secrets.MATHPIX_APP_ID }}" >> mathpix-variables.env
- name: Build the Docker image
run: |
docker-compose build
- name: Run the test suite
run: |
docker-compose run flask bash -c "cd /app/flask/flaskapp && python -m pytest"
12 changes: 12 additions & 0 deletions flask/flaskapp/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest

from flaskapp import app as flask_app

@pytest.fixture
def app():
yield flask_app


@pytest.fixture
def client(app):
return app.test_client()
1 change: 1 addition & 0 deletions flask/flaskapp/tests/imagetest.txt

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions flask/flaskapp/tests/test_flaskapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import jsonschema
from jsonschema import validate

def test_home_page(client, app):
"""A basic test to ensure the home page is functioning correctly"""
res = client.get('/')
assert b'Hello, World!' == res.data


def test_mathpix_ocr(client, app):
with open('tests/imagetest.txt', 'r') as file:
image = file.read().replace('\n', '')

response_schema = {
"type": "object",
"properties": {
"confidence": {"type": "number"},
"latex_styled": {"type": "string"},
"request_id": {"type": "string"},
},
}
res = client.post('/mathpix-ocr', json={
'b64_img': image
})

assert True == validate_json(instance=res.get_json(), schema=response_schema)


def test_solve_image(client, app):
with open('tests/imagetest.txt', 'r') as file:
image = file.read().replace('\n', '')

response_schema = {
"type": "object",
"properties": {
"confidence": {"type": "number"},
"input_detected": {"type": "string"},
"solved": {"type": "string"},
},
}

res = client.post('/solve-image', json={
'b64_img': image
})

assert True == validate_json(instance=res.get_json(), schema=response_schema)
assert r'F(0)=x' == res.get_json()['input_detected']
assert r'F{\left(0 \right)} = x' == res.get_json()['solved']


def test_solve_latex(client, app):
response_schema = {
"type": "object",
"properties": {
"solved": {"type": "string"},
},
}

res = client.post('/solve-latex', json={
'latex': r'F(x)=2+2'
})

assert True == validate_json(instance=res.get_json(), schema=response_schema)
assert r'F{\left(x \right)} = 4' == res.get_json()['solved']


def validate_json(instance, schema):
try:
validate(instance, schema)
except jsonschema.exceptions.ValidationError as err:
return False
return True
11 changes: 9 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ Open the integrated terminal to run commands in the container.
`$ flask run` will start the flask development server and send requests to the flask application.

## Running Unit Tests
Run pytest in the top-level project directory to start unit tests.
## Running Tests

To run the tests:

$ docker-compose build
$ docker-compose run flask bash -c "cd /app/flask/flaskapp && python -m pytest"


## Public API

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



16 changes: 11 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
antlr4-python3-runtime==4.8
attrs==20.2.0
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
Flask==1.1.2
hypothesis==5.33.0
idna==2.10
importlib-metadata==1.7.0
iniconfig==1.0.1
itsdangerous==1.1.0
Jinja2==2.11.2
jsonschema==3.2.0
latex2sympy==1.0.3
MarkupSafe==1.1.1
more-itertools==8.5.0
mpmath==1.1.0
packaging==20.4
pathlib2==2.3.5
pkg-resources==0.0.0
pluggy==0.13.1
py==1.9.0
pyparsing==2.4.7
pyrsistent==0.17.3
pytest==6.0.1
requests==2.24.0
six==1.15.0
sortedcontainers==2.2.2
sympy==1.6.2
toml==0.10.1
urllib3==1.25.10
Werkzeug==1.0.1
zipp==3.1.0
requests==2.24.0
sympy==1.6.2
latex2sympy==1.0.3
antlr4-python3-runtime==4.8

0 comments on commit a8b60d2

Please sign in to comment.