File size: 2,662 Bytes
33761d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from fastapi.testclient import TestClient
from main import app
from main import TextInput
from fastapi.encoders import jsonable_encoder

client = TestClient(app)

# Test the welcome endpoint
def test_welcome():
    # Test the welcome endpoint
    response = client.get("/")
    assert response.status_code == 200
    assert response.json() == "Welcome to our Text Classification API"

# Test the sentiment analysis endpoint for positive sentiment
def test_positive_sentiment():
    with client:
        # Define the request payload 
        # Initialize payload as a TextInput object
        payload = TextInput(text="I love this product! It's amazing!")

        # Convert TextInput object to JSON-serializable dictionary
        payload_dict = jsonable_encoder(payload)
        
        # Send a POST request to the sentiment analysis endpoint
        response = client.post("/analyze/{text}", json=payload_dict)

        # Assert that the response status code is 200 OK
        assert response.status_code == 200
        
        # Assert that the sentiment returned is positive
        assert response.json()[0]['label'] == "positive"

# Test the sentiment analysis endpoint for negative sentiment
def test_negative_sentiment():
    with client:
        # Define the request payload 
        # Initialize payload as a TextInput object
        payload = TextInput(text="I'm really disappointed with this service. It's terrible.")

        # Convert TextInput object to JSON-serializable dictionary
        payload_dict = jsonable_encoder(payload)
        
        # Send a POST request to the sentiment analysis endpoint
        response = client.post("/analyze/{text}", json=payload_dict)

        # Assert that the response status code is 200 OK
        assert response.status_code == 200
        
        # Assert that the sentiment returned is positive
        assert response.json()[0]['label'] == "negative"

# Test the sentiment analysis endpoint for neutral sentiment
def test_neutral_sentiment():
    with client:
        # Define the request payload 
        # Initialize payload as a TextInput object
        payload = TextInput(text="This is a neutral statement.")

        # Convert TextInput object to JSON-serializable dictionary
        payload_dict = jsonable_encoder(payload)
        
        # Send a POST request to the sentiment analysis endpoint
        response = client.post("/analyze/{text}", json=payload_dict)

        # Assert that the response status code is 200 OK
        assert response.status_code == 200
        
        # Assert that the sentiment returned is positive
        assert response.json()[0]['label'] == "neutral"