JeanProjets's picture
Upload folder using huggingface_hub
6cde6d6 verified
Raw
History Blame Contribute Delete
1.61 kB
import pytest
from fastapi.testclient import TestClient
from main import app
from unittest.mock import patch
import numpy as np
# Create a TestClient instance to interact with the FastAPI app
client = TestClient(app)
def test_read_root():
"""Test the root endpoint (GET /)"""
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
@patch('main.model.predict')
def test_read_item_happy(mock_predict):
"""Test the prediction endpoint with a happy sentence by mocking the model prediction."""
# Configure the mock to return a value > 0.5 for a "happy" prediction
mock_predict.return_value = np.array([[0.8]])
text = "I just got a promotion at work and I am thrilled!"
response = client.get(f"/feeling_predictions/{text}")
assert response.status_code == 200
data = response.json()
assert data["text"] == text
assert data["feeling_result"] == "happy"
mock_predict.assert_called_once()
@patch('main.model.predict')
def test_read_item_sad(mock_predict):
"""Test the prediction endpoint with a sad sentence by mocking the model prediction."""
# Configure the mock to return a value <= 0.5 for a "sad" prediction
mock_predict.return_value = np.array([[0.2]])
text = "Everything is going wrong today and I just want to cry."
response = client.get(f"/feeling_predictions/{text}")
assert response.status_code == 200
data = response.json()
assert data["text"] == text
assert data["feeling_result"] == "sad"
mock_predict.assert_called_once()