File size: 2,419 Bytes
dd8100a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi.testclient import TestClient
from main import app
from main import TextInput
from fastapi.encoders import jsonable_encoder

client = TestClient(app)

# 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 eating and sleeping")

        # 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/", 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/", json=payload_dict)

        # Assert that the response status code is 200 OK
        assert response.status_code == 200
        
        # Assert that the sentiment returned is negative
        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/", json=payload_dict)

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