From 637bfd5fe58ea604b5a5975103d7b5f98d268b77 Mon Sep 17 00:00:00 2001 From: James Napier Date: Mon, 11 Dec 2023 21:17:20 +1300 Subject: [PATCH] add tests for api --- .github/workflows/run-api-tests.yml | 39 ++++++++++++++++ api/requirements.txt | 4 ++ api/test_main.py | 70 +++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 .github/workflows/run-api-tests.yml create mode 100644 api/test_main.py diff --git a/.github/workflows/run-api-tests.yml b/.github/workflows/run-api-tests.yml new file mode 100644 index 0000000..353cfdf --- /dev/null +++ b/.github/workflows/run-api-tests.yml @@ -0,0 +1,39 @@ +name: Run API tests + +on: + pull_request: + branches: + - main + push: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.12] + + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Static Code Linting with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Unit Testing with pytest + env: + # add environment variables for tests + run: | + pytest diff --git a/api/requirements.txt b/api/requirements.txt index 2788630..4d54915 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -10,14 +10,18 @@ httpcore==1.0.2 httptools==0.6.1 httpx==0.25.2 idna==3.6 +iniconfig==2.0.0 itsdangerous==2.1.2 Jinja2==3.1.2 MarkupSafe==2.1.3 orjson==3.9.10 +packaging==23.2 +pluggy==1.3.0 pydantic==2.5.2 pydantic-extra-types==2.1.0 pydantic-settings==2.1.0 pydantic_core==2.14.5 +pytest==7.4.3 python-dotenv==1.0.0 python-minimizer==2.0.1 python-multipart==0.0.6 diff --git a/api/test_main.py b/api/test_main.py new file mode 100644 index 0000000..dbd8e2c --- /dev/null +++ b/api/test_main.py @@ -0,0 +1,70 @@ +from os import link +from fastapi.testclient import TestClient + +from .main import app + +client = TestClient(app) + + +SMALL_PROGRAM = '''"""Hello world""" + + +def main(): + """Hello world""" + print("hi" + "bye") + + +main() +''' + +SMALL_PROGRAM_MIN = """def main(): + print("hi"+"bye") +main()""" + +SMALL_PROGRAM_MIN_TINY = """def main(): + print("hi"+"bye") +main()""" + + +def test_minimise_code(): + response = client.post( + url="/minimise/", + json={"code": SMALL_PROGRAM, "lang": "python3", "indentation": " "}, + ) + assert response.status_code == 200 + assert response.json().code == {"lang": "python3", "code": SMALL_PROGRAM_MIN} + + response = client.post( + url="/minimise/", + json={"code": SMALL_PROGRAM, "lang": "python3", "indentation": " "}, + ) + assert response.status_code == 200 + assert response.json().code == {"lang": "python3", "code": SMALL_PROGRAM_MIN_TINY} + + +def test_minimise_code_and_get_link(): + response = client.post( + url="/minimise/", + json={"code": SMALL_PROGRAM, "lang": "python3", "indentation": " "}, + ) + assert response.status_code == 200 + assert response.json().code == { + "lang": "python3", + "code": SMALL_PROGRAM_MIN, + "link": "https://pythontutor.com/visualize.html#cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false&code=def+main%28%29%3A%0A++++print%28%22hi%22%2B%22bye%22%29%0Amain%28%29", + } + + response = client.post( + url="/minimise/", + json={ + "code": SMALL_PROGRAM, + "lang": "python3", + "indentation": " ", + }, + ) + assert response.status_code == 200 + assert response.json().code == { + "lang": "python3", + "code": SMALL_PROGRAM_MIN_TINY, + "link": "https://pythontutor.com/visualize.html#cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false&code=def+main%28%29%3A%0A+print%28%22hi%22%2B%22bye%22%29%0Amain%28%29", + }