Noveramaaz/Text_classification_API_Docker
0
1from fastapi.testclient import TestClient2from main import app3from main import TextInput4from fastapi.encoders import jsonable_encoder5 6client = TestClient(app)7 8# Test the welcome endpoint9def test_welcome():10 # Test the welcome endpoint11 response = client.get("/")12 assert response.status_code == 20013 assert response.json() == "Welcome to our Text Classification API"14 15# Test the sentiment analysis endpoint for positive sentiment16def test_positive_sentiment():17 with client:18 # Define the request payload 19 # Initialize payload as a TextInput object20 payload = TextInput(text="I love this product! It's amazing!")21 22 # Convert TextInput object to JSON-serializable dictionary23 payload_dict = jsonable_encoder(payload)24 25 # Send a POST request to the sentiment analysis endpoint26 response = client.post("/analyze/{text}", json=payload_dict)27 28 # Assert that the response status code is 200 OK29 assert response.status_code == 20030 31 # Assert that the sentiment returned is positive32 assert response.json()[0]['label'] == "positive"33 34# Test the sentiment analysis endpoint for negative sentiment35def test_negative_sentiment():36 with client:37 # Define the request payload 38 # Initialize payload as a TextInput object39 payload = TextInput(text="I'm really disappointed with this service. It's terrible.")40 41 # Convert TextInput object to JSON-serializable dictionary42 payload_dict = jsonable_encoder(payload)43 44 # Send a POST request to the sentiment analysis endpoint45 response = client.post("/analyze/{text}", json=payload_dict)46 47 # Assert that the response status code is 200 OK48 assert response.status_code == 20049 50 # Assert that the sentiment returned is positive51 assert response.json()[0]['label'] == "negative"52 53# Test the sentiment analysis endpoint for neutral sentiment54def test_neutral_sentiment():55 with client:56 # Define the request payload 57 # Initialize payload as a TextInput object58 payload = TextInput(text="This is a neutral statement.")59 60 # Convert TextInput object to JSON-serializable dictionary61 payload_dict = jsonable_encoder(payload)62 63 # Send a POST request to the sentiment analysis endpoint64 response = client.post("/analyze/{text}", json=payload_dict)65 66 # Assert that the response status code is 200 OK67 assert response.status_code == 20068 69 # Assert that the sentiment returned is positive70 assert response.json()[0]['label'] == "neutral"