From d3e09545bc25aae9c2650b486b97a50c905610e5 Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 17:53:51 +0800 Subject: [PATCH 01/15] test workflow try to build in GitHub environment --- .github/workflows/python-app.yml | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 .github/workflows/python-app.yml diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml new file mode 100644 index 0000000..a7daf09 --- /dev/null +++ b/.github/workflows/python-app.yml @@ -0,0 +1,40 @@ +# 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: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pipenv + # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + if [ -f Pipfile ]; then pipenv install && pipenv install --dev; 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 From 39d3b2115e45d175450f324312199ef680dcf91a Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:01:38 +0800 Subject: [PATCH 02/15] Update python-app.yml modified workflow file --- .github/workflows/python-app.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index a7daf09..5c7426d 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -28,7 +28,8 @@ jobs: python -m pip install --upgrade pip pip install pipenv # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - if [ -f Pipfile ]; then pipenv install && pipenv install --dev; fi + if [ -f Pipfile ]; then pipenv install; fi + if [ -f Pipfile ]; then pipenv install --dev; fi # - name: Lint with flake8 # run: | # # stop the build if there are Python syntax errors or undefined names From 616214aea2e788009e3cb8f2ef6b60a6f4901ffb Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:09:14 +0800 Subject: [PATCH 03/15] Update python-app.yml --- .github/workflows/python-app.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 5c7426d..00bee4e 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -27,7 +27,6 @@ jobs: run: | python -m pip install --upgrade pip pip install pipenv - # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi if [ -f Pipfile ]; then pipenv install; fi if [ -f Pipfile ]; then pipenv install --dev; fi # - name: Lint with flake8 From 6cbd8e4bb39630642a7dea859941471c3399e945 Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:12:53 +0800 Subject: [PATCH 04/15] Update python-app.yml forgot activating venv shell --- .github/workflows/python-app.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 00bee4e..55089b3 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -27,8 +27,8 @@ jobs: run: | python -m pip install --upgrade pip pip install pipenv - if [ -f Pipfile ]; then pipenv install; fi - if [ -f Pipfile ]; then pipenv install --dev; fi + if [ -f Pipfile ]; then pipenv install && pipenv install --dev; fi + pipenv shell # - name: Lint with flake8 # run: | # # stop the build if there are Python syntax errors or undefined names From 608e4be31f397af3f46d2fdb9fc7e454b6b31fed Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:16:46 +0800 Subject: [PATCH 05/15] Update python-app.yml used my steps --- .github/workflows/python-app.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 55089b3..0d3bc86 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -26,9 +26,10 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip + python -m venv venv + source venv/bin/activate pip install pipenv if [ -f Pipfile ]; then pipenv install && pipenv install --dev; fi - pipenv shell # - name: Lint with flake8 # run: | # # stop the build if there are Python syntax errors or undefined names From 0e977d8f7850ad3247ad932ad70a29c165d30ff0 Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:19:36 +0800 Subject: [PATCH 06/15] Update python-app.yml run will make a new terminal --- .github/workflows/python-app.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 0d3bc86..1b3b0df 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -26,8 +26,6 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - python -m venv venv - source venv/bin/activate pip install pipenv if [ -f Pipfile ]; then pipenv install && pipenv install --dev; fi # - name: Lint with flake8 @@ -38,4 +36,5 @@ jobs: # flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | + pipenv shell pytest From ac55a0ee8a46c075b81e96d6ca4be69e13aca351 Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:22:59 +0800 Subject: [PATCH 07/15] Update python-app.yml used venv in all runs --- .github/workflows/python-app.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 1b3b0df..34bd380 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -25,6 +25,8 @@ jobs: python-version: "3.10" - name: Install dependencies run: | + python -m venv venv + source venv/bin/activate python -m pip install --upgrade pip pip install pipenv if [ -f Pipfile ]; then pipenv install && pipenv install --dev; fi @@ -36,5 +38,5 @@ jobs: # flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | - pipenv shell + source venv/bin/activate pytest From 8cd3d8d97eac84d6a05b1ed40a9c783f5607099f Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:29:08 +0800 Subject: [PATCH 08/15] Update python-app.yml used default pipenv environment --- .github/workflows/python-app.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 34bd380..132aafb 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -25,10 +25,8 @@ jobs: python-version: "3.10" - name: Install dependencies run: | - python -m venv venv - source venv/bin/activate python -m pip install --upgrade pip - pip install pipenv + python -m pip install pipenv if [ -f Pipfile ]; then pipenv install && pipenv install --dev; fi # - name: Lint with flake8 # run: | @@ -38,5 +36,5 @@ jobs: # flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | - source venv/bin/activate + python -m pipenv shell pytest From 9f6e542b78c9e6d0f0e2a08a333867e8ca06f351 Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:34:26 +0800 Subject: [PATCH 09/15] Update python-app.yml generated requirements.txt file by pipenv --- .github/workflows/python-app.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 132aafb..7b4065e 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -27,7 +27,9 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install pipenv - if [ -f Pipfile ]; then pipenv install && pipenv install --dev; fi + pipenv requirements && pipenv reqirements --dev + if [ -f Pipfile ]; pip install -r requirements.txt; fi + if [ -f Pipfile ]; pip install -r requirements-dev.txt; fi # - name: Lint with flake8 # run: | # # stop the build if there are Python syntax errors or undefined names @@ -36,5 +38,4 @@ jobs: # flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | - python -m pipenv shell pytest From 5f14992d8ba83a5fa1c2f66cba344ce75c9ad8b0 Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:36:20 +0800 Subject: [PATCH 10/15] Update python-app.yml fixed shell if condition --- .github/workflows/python-app.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 7b4065e..200ec42 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -28,8 +28,8 @@ jobs: python -m pip install --upgrade pip python -m pip install pipenv pipenv requirements && pipenv reqirements --dev - if [ -f Pipfile ]; pip install -r requirements.txt; fi - if [ -f Pipfile ]; pip install -r requirements-dev.txt; fi + if [ -f Pipfile ];then pip install -r requirements.txt; fi + if [ -f Pipfile ];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 From 6c0950326d4334f5f6e54863e0a130d9291434d1 Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:40:35 +0800 Subject: [PATCH 11/15] Update python-app.yml output requirement.txt from pipenv --- .github/workflows/python-app.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 200ec42..3ea67f4 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -27,7 +27,8 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install pipenv - pipenv requirements && pipenv reqirements --dev + pipenv install && pipenv install --dev + pipenv requirements > requirements.txt && pipenv reqirements --dev > requirements-dev.txt if [ -f Pipfile ];then pip install -r requirements.txt; fi if [ -f Pipfile ];then pip install -r requirements-dev.txt; fi # - name: Lint with flake8 From d48eec299d5290f65f0ea79d2509dfdcefa27c9d Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 18:54:29 +0800 Subject: [PATCH 12/15] Update python-app.yml fixed spell error --- .github/workflows/python-app.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 3ea67f4..d83476d 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -28,7 +28,7 @@ jobs: python -m pip install --upgrade pip python -m pip install pipenv pipenv install && pipenv install --dev - pipenv requirements > requirements.txt && pipenv reqirements --dev > requirements-dev.txt + pipenv requirements > requirements.txt && pipenv requirements --dev > requirements-dev.txt if [ -f Pipfile ];then pip install -r requirements.txt; fi if [ -f Pipfile ];then pip install -r requirements-dev.txt; fi # - name: Lint with flake8 From c54a53fb4abb3dc505d4aab1690343d3587d6d5a Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 19:48:35 +0800 Subject: [PATCH 13/15] Update python-app.yml skip pipenv install --- .github/workflows/python-app.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index d83476d..737fb46 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -27,7 +27,7 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install pipenv - pipenv install && pipenv install --dev + pipenv lock && pipenv lock --dev pipenv requirements > requirements.txt && pipenv requirements --dev > requirements-dev.txt if [ -f Pipfile ];then pip install -r requirements.txt; fi if [ -f Pipfile ];then pip install -r requirements-dev.txt; fi From f6e2fff126a54fac8825f6d38ed009133e8fc5f7 Mon Sep 17 00:00:00 2001 From: cia1099 <36960644+cia1099@users.noreply.github.com> Date: Wed, 15 May 2024 19:56:01 +0800 Subject: [PATCH 14/15] Update python-app.yml pipeline setup flow --- .github/workflows/python-app.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 737fb46..bf4cf6b 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -23,14 +23,16 @@ jobs: uses: actions/setup-python@v3 with: python-version: "3.10" - - name: Install dependencies + - 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 - if [ -f Pipfile ];then pip install -r requirements.txt; fi - if [ -f Pipfile ];then pip install -r requirements-dev.txt; fi + - 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 From b62ac7aac9bb0c2e7c9a1d7c30226552fcd39ea5 Mon Sep 17 00:00:00 2001 From: "Otto@AirM2" Date: Wed, 15 May 2024 20:09:20 +0800 Subject: [PATCH 15/15] checked aiofiles_mock_open can be used in other environment --- storeapi/test/test_upload.py | 52 +++++++++++++++++------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/storeapi/test/test_upload.py b/storeapi/test/test_upload.py index 0040805..85a6724 100644 --- a/storeapi/test/test_upload.py +++ b/storeapi/test/test_upload.py @@ -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