emanism6/Text-Classification-Fastapi
0
1from fastapi.testclient import TestClient2from main import app3from main import TextInput4from fastapi.encoders import jsonable_encoder5 6client = TestClient(app)7 8# Test the sentiment analysis endpoint for positive sentiment9def test_positive_sentiment():10 with client:11 # Define the request payload 12 # Initialize payload as a TextInput object13 payload = TextInput(text="I love this eating and sleeping")14 15 # Convert TextInput object to JSON-serializable dictionary16 payload_dict = jsonable_encoder(payload)17 18 # Send a POST request to the sentiment analysis endpoint19 response = client.post("/analyze/", json=payload_dict)20 21 # Assert that the response status code is 200 OK22 assert response.status_code == 20023 24 # Assert that the sentiment returned is positive25 assert response.json()[0]['label'] == "positive"26 27# Test the sentiment analysis endpoint for negative sentiment28def test_negative_sentiment():29 with client:30 # Define the request payload 31 # Initialize payload as a TextInput object32 payload = TextInput(text="I'm really disappointed with this service. It's terrible.")33 34 # Convert TextInput object to JSON-serializable dictionary35 payload_dict = jsonable_encoder(payload)36 37 # Send a POST request to the sentiment analysis endpoint38 response = client.post("/analyze/", json=payload_dict)39 40 # Assert that the response status code is 200 OK41 assert response.status_code == 20042 43 # Assert that the sentiment returned is negative44 assert response.json()[0]['label'] == "negative"45 46# Test the sentiment analysis endpoint for neutral sentiment47def test_neutral_sentiment():48 with client:49 # Define the request payload 50 # Initialize payload as a TextInput object51 payload = TextInput(text="This is a neutral statement.")52 53 # Convert TextInput object to JSON-serializable dictionary54 payload_dict = jsonable_encoder(payload)55 56 # Send a POST request to the sentiment analysis endpoint57 response = client.post("/analyze/", json=payload_dict)58 59 # Assert that the response status code is 200 OK60 assert response.status_code == 20061 62 # Assert that the sentiment returned is neutral63 assert response.json()[0]['label'] == "neutral"64 