Skip to content
44 changes: 44 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: generate requirements
run: |
python -m pip install --upgrade pip
python -m pip install pipenv
pipenv lock && pipenv lock --dev
pipenv requirements > requirements.txt && pipenv requirements --dev > requirements-dev.txt
- name: install requirements
run: |
if [ -f requirements.txt ];then pip install -r requirements.txt; fi
if [ -f requirements-dev.txt ];then pip install -r requirements-dev.txt; fi
# - name: Lint 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: Test with pytest
run: |
pytest
52 changes: 24 additions & 28 deletions storeapi/test/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,30 @@ async def call_upload_endpoint(async_client: AsyncClient, token: str, file: Path
)


"""=====
TODO: aiofiles_mock_open has problem in unknown
that make logged_in_token error
"""
# @pytest.mark.anyio
# async def test_upload_image(
# async_client: AsyncClient, logged_in_token: str, sample_image: Path
# ):
# res = await call_upload_endpoint(async_client, logged_in_token, sample_image)
# assert res.status_code == 201
# assert res.json()["file_url"] == "https://fakefile.jpg"


# @pytest.mark.anyio
# async def test_temp_file_removed_after_upload(
# async_client: AsyncClient, logged_in_token: str, sample_image: Path, mocker
# ):
# # Spy on the NamedTemporaryFile function
# named_temp_file_spy = mocker.spy(tempfile, "NamedTemporaryFile")
#
# response = await call_upload_endpoint(async_client, logged_in_token, sample_image)
# assert response.status_code == 201
#
# # Get the filename of the temporary file created by the upload endpoint
# created_temp_file = named_temp_file_spy.spy_return
#
# # Check if the temp_file is removed after the file is uploaded
# assert not os.path.exists(created_temp_file.name)
@pytest.mark.anyio
async def test_upload_image(
async_client: AsyncClient, logged_in_token: str, sample_image: Path
):
res = await call_upload_endpoint(async_client, logged_in_token, sample_image)
assert res.status_code == 201
assert res.json()["file_url"] == "https://fakefile.jpg"


@pytest.mark.anyio
async def test_temp_file_removed_after_upload(
async_client: AsyncClient, logged_in_token: str, sample_image: Path, mocker
):
# Spy on the NamedTemporaryFile function
named_temp_file_spy = mocker.spy(tempfile, "NamedTemporaryFile")

response = await call_upload_endpoint(async_client, logged_in_token, sample_image)
assert response.status_code == 201

# Get the filename of the temporary file created by the upload endpoint
created_temp_file = named_temp_file_spy.spy_return

# Check if the temp_file is removed after the file is uploaded
assert not os.path.exists(created_temp_file.name)


@pytest.mark.anyio
Expand Down