| import pytest |
| from fastapi.testclient import TestClient |
| from main import app |
|
|
| from unittest.mock import patch |
| import numpy as np |
|
|
| |
| 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.""" |
| |
| 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.""" |
| |
| 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() |
|
|