Skip to content

Commit

Permalink
Merge pull request #33 from MLOps-essi-upc/api_test
Browse files Browse the repository at this point in the history
add API pytest
  • Loading branch information
mionaD-upc authored Dec 12, 2023
2 parents dcf3633 + dd3cdc1 commit d4282b0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
17 changes: 12 additions & 5 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
push:
branches:
- main
- github-actions
pull_request:
branches:
- main
Expand Down Expand Up @@ -38,13 +37,21 @@
echo "MLFLOW_TRACKING_USERNAME=${{ secrets.MLFLOW_TRACKING_USERNAME }}" >> .env
echo "MLFLOW_TRACKING_PASSWORD=${{ secrets.MLFLOW_TRACKING_PASSWORD }}" >> .env
- name: Install packages # Install dependencies
run: pip install pytest mlflow python-dotenv
- name: Install packages # Install dependencies needed for tests (not for whole project)
run: pip install pytest mlflow python-dotenv fastapi httpx torch torchvision pydantic python-multipart dvc

- name: Pull model
run: |
dvc remote modify origin --local auth basic
dvc remote modify origin --local user ${{ secrets.DAGSHUB_USERNAME }}
dvc remote modify origin --local password ${{ secrets.DAGSHUB_TOKEN }}
dvc pull -r origin models/trained_model.pt
- name: Run training tests
- name: Run tests
run: |
pytest tests/test_mlflow.py
pytest tests/test_train_model.py
pytest tests/test_api.py
continue-on-error: false


Expand Down Expand Up @@ -74,7 +81,7 @@
- name: Install packages # Install dependencies
run: pip install dvc docker

- name: Pull data
- name: Pull model
run: |
dvc remote modify origin --local auth basic
dvc remote modify origin --local user ${{ secrets.DAGSHUB_USERNAME }}
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
- 'src/web/**'
branches:
- main
- github-actions
pull_request:
paths:
- 'src/web/**'
Expand Down
1 change: 0 additions & 1 deletion src/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import List
from PIL import Image
from io import BytesIO
import uvicorn
from fastapi import FastAPI, Request, File, UploadFile, HTTPException
from torchvision import transforms
import torch
Expand Down
29 changes: 29 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from fastapi.testclient import TestClient
import io
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from src.app.api import app
from pathlib import Path

ROOT_DIR = Path(Path(__file__).resolve().parent)


def test_make_prediction_endpoint():
with TestClient(app) as client:
# Replace 'path/to/your/image.jpg' with the actual path to your test image
image_path = ROOT_DIR / 'test_image_1.jpg'

# Open the image file in binary mode
with open(image_path, 'rb') as img_file:
# Create a file-like object from the image file
img_file_object = io.BytesIO(img_file.read())

files = {'beans_img': ('test_image_1.jpg', img_file_object, 'image/jpeg')}
response = client.post("/make_prediction", files=files)

assert response.status_code == 200
assert set(response.json().keys()) == {'prediction', 'probs'}
Binary file added tests/test_image_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d4282b0

Please sign in to comment.