Skip to content

Commit

Permalink
add tests for api
Browse files Browse the repository at this point in the history
  • Loading branch information
jimbonothing64 committed Dec 11, 2023
1 parent b6a95ba commit 637bfd5
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/run-api-tests.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions api/test_main.py
Original file line number Diff line number Diff line change
@@ -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",
}

1 comment on commit 637bfd5

@vercel
Copy link

@vercel vercel bot commented on 637bfd5 Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

Resource is limited - try again in 5 minutes (more than 100, code: "api-deployments-free-per-day").

Please sign in to comment.