Skip to content

Commit

Permalink
adding and debugging tests, adding test execution to actions
Browse files Browse the repository at this point in the history
  • Loading branch information
krachwal committed Nov 30, 2023
1 parent 5c2f53b commit 6f1bf12
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/run-backend-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Commit Workflow
on:
push
jobs:
run-python-tests:
runs-on: ubuntu-latest
name: Python Unit Tests
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Test with pytest
run: |
pip install pytest
pytest
1 change: 0 additions & 1 deletion .github/workflows/run-frontend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ jobs:
working-directory: ./react-frontend
build: npm run build
start: npm start

22 changes: 20 additions & 2 deletions python-backend/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import re
from sympy.abc import _clash1
import uuid

from pydantic import BaseModel
Expand All @@ -10,27 +12,43 @@

origins = ["http://localhost:5173", "localhost:5173"]

# Only allow traffic from localhost and restrict methods to those in use
app.add_middleware(CORSMiddleware,
allow_origins=origins,
allow_methods=["GET", "POST", "OPTIONS"],
allow_credentials=True,
allow_headers=["*"])


# The structure of the post body from the frontend
class Input(BaseModel):
p: str
q: str
i: int


def convert(polynomial: str):
"""
Take an acceptable math polynomial entered by a user and convert to one that Python can parse
:param polynomial: incoming polynomial entered by user in web frontend
:return: python parse-able polynomial
"""
return re.sub(r'([0-9.-])+([a-zA-Z])','\\1*\\2', polynomial.replace('^', '**'))


@app.post("/analyze")
async def analyze(request: Request):
# parse posted body
"""
:param request: HTTP request
:return: HTTP response indicating success of parsing inputs with a 200 or a 500 to indicate failure parsing inputs
"""
# parse posted body as Input
data = Input(**(await request.json()))

# convert to math expression
try:
expression = sympify(data.p) / sympify(data.q)
expression = sympify(convert(data.p), _clash1) / sympify(convert(data.q), _clash1)
# printing for the time being will process later on
print(simplify(expression))
except Exception as e:
Expand Down
6 changes: 6 additions & 0 deletions python-backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Automatically generated by https://github.com/damnever/pigar.

fastapi==0.104.0
pydantic==2.4.2
pytest==7.4.3
sympy==1.12
51 changes: 51 additions & 0 deletions python-backend/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
from sympy import sympify, simplify, SympifyError

from main import convert

TEST_INPUT_1 = "4^x"
CONVERSION_1 = "4**x"
TEST_INPUT_2 = "4x^2+3"
CONVERSION_2 = "4*x**2+3"
TEST_INPUT_3 = "4x^2+3x^5-1"
CONVERSION_3 = "4*x**2+3*x**5-1"
SYMPY_3 = "3*x**5+4*x**2-1"
TEST_INPUT_4 = "4x^2 + 3x^5 - 1"
CONVERSION_4 = "4*x**2 + 3*x**5 - 1"
SYMPY_4 = "3*x**5+4*x**2-1"
TEST_INPUT_5 = "2x^2"
TEST_INPUT_6 = "x"
SIMPLIFIED_5_6 = "2*x"
TEST_INPUT_7 = "6x^2 - 2 - 4x"
TEST_INPUT_8 = "x - 1"
SIMPLIFIED_7_8 = "6*x + 2"


def test_convert():
assert convert("") == ""
assert convert(TEST_INPUT_1) == CONVERSION_1
assert convert(TEST_INPUT_2) == CONVERSION_2
assert convert(TEST_INPUT_3) == CONVERSION_3
assert convert(TEST_INPUT_4) == CONVERSION_4


def test_sympify():
assert sympify(convert(TEST_INPUT_1))
with pytest.raises(SympifyError):
sympify(TEST_INPUT_2)
assert sympify(convert(TEST_INPUT_2)).__str__().replace(' ', '') == convert(TEST_INPUT_2)
with pytest.raises(SympifyError):
sympify(TEST_INPUT_3)
assert sympify(convert(TEST_INPUT_3)).__str__().replace(' ', '') == SYMPY_3
with pytest.raises(SympifyError):
sympify(TEST_INPUT_4)
assert sympify(convert(TEST_INPUT_4)).__str__().replace(' ', '') == SYMPY_4


def test_simplify():
assert simplify(sympify(convert(TEST_INPUT_1)))
assert simplify(sympify(convert(TEST_INPUT_2)))
assert simplify(sympify(convert(TEST_INPUT_3)))
assert simplify(sympify(convert(TEST_INPUT_4)))
assert simplify(sympify(convert(TEST_INPUT_5)) / sympify(convert(TEST_INPUT_6))).__str__() == SIMPLIFIED_5_6
assert simplify(sympify(convert(TEST_INPUT_7)) / sympify(convert(TEST_INPUT_8))).__str__() == SIMPLIFIED_7_8

0 comments on commit 6f1bf12

Please sign in to comment.