File size: 1,499 Bytes
46f5320
 
0128fae
46f5320
 
 
 
0128fae
46f5320
 
 
 
 
 
 
0128fae
46f5320
 
0128fae
 
 
 
 
 
 
 
 
 
 
 
46f5320
 
 
 
 
0128fae
 
 
46f5320
0128fae
 
 
 
 
 
 
46f5320
0128fae
46f5320
 
 
 
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
import unittest

import pandas as pd
from fastapi.testclient import TestClient

from app import app

TEST_DATA_FILE = "data/master_test_text2int.csv"

client = TestClient(app)


class TestStringMethods(unittest.TestCase):

    def setUp(self):
        """Creates a fastapi test client"""
        self.client = TestClient(app)

    def get_response_text2int(self, text):
        """Makes a post request to the endpoint"""
        r = None
        try:
            r = self.client.post("/text2int", json={"content": text}) \
                .json().get("message")
        except:
            pass
        return r

    def test_endpoint_text2int(self):
        """Tests if endpoint is working"""
        response = self.client.post("/text2int",
                                    json={"content": "fourteen"}
                                    )
        self.assertEqual(response.status_code, 200)

    def test_acc_score_text2int(self):
        """Calculates accuracy score for endpoint"""
        df = pd.read_csv(TEST_DATA_FILE)

        df["text2int"] = df["input"].apply(func=self.get_response_text2int)
        df["score"] = df[["output", "text2int"]].apply(
            lambda row: row[0] == row[1],
            axis=1
        )
        df.to_csv("data/text2int_results.csv", index=False)
        acc_score = df["score"].mean().__round__(2)

        self.assertGreaterEqual(acc_score, 0.8, f"Accuracy score: '{acc_score}'. Value is too low!")


if __name__ == '__main__':
    unittest.main()