|
from fastapi.testclient import TestClient |
|
from main import app |
|
from main import TextInput |
|
from fastapi.encoders import jsonable_encoder |
|
|
|
client = TestClient(app) |
|
|
|
|
|
def test_welcome(): |
|
|
|
response = client.get("/") |
|
assert response.status_code == 200 |
|
assert response.json() == "Welcome to our Text Classification API" |
|
|
|
|
|
def test_positive_sentiment(): |
|
with client: |
|
|
|
|
|
payload = TextInput(text="I love this product! It's amazing!") |
|
|
|
|
|
payload_dict = jsonable_encoder(payload) |
|
|
|
|
|
response = client.post("/analyze/{text}", json=payload_dict) |
|
|
|
|
|
assert response.status_code == 200 |
|
|
|
|
|
assert response.json()[0]['label'] == "positive" |
|
|
|
|
|
def test_negative_sentiment(): |
|
with client: |
|
|
|
|
|
payload = TextInput(text="I'm really disappointed with this service. It's terrible.") |
|
|
|
|
|
payload_dict = jsonable_encoder(payload) |
|
|
|
|
|
response = client.post("/analyze/{text}", json=payload_dict) |
|
|
|
|
|
assert response.status_code == 200 |
|
|
|
|
|
assert response.json()[0]['label'] == "negative" |
|
|
|
|
|
def test_neutral_sentiment(): |
|
with client: |
|
|
|
|
|
payload = TextInput(text="This is a neutral statement.") |
|
|
|
|
|
payload_dict = jsonable_encoder(payload) |
|
|
|
|
|
response = client.post("/analyze/{text}", json=payload_dict) |
|
|
|
|
|
assert response.status_code == 200 |
|
|
|
|
|
assert response.json()[0]['label'] == "neutral" |