Hobson/mathtext-fastapi
1
1import unittest2from pathlib import Path3 4import pandas as pd5from fastapi.testclient import TestClient6 7from app import app8 9# The raw file URL has to be used for GitLab.10URL = "https://gitlab.com/tangibleai/community/mathtext/-/raw/main/mathtext/data/master_test_text2int.csv"11 12DATA_DIR = Path(__file__).parent.parent / "mathtext_fastapi" / "data"13print(DATA_DIR)14 15client = TestClient(app)16 17 18class TestStringMethods(unittest.TestCase):19 20 def setUp(self):21 """Creates a fastapi test client"""22 self.client = TestClient(app)23 self.df = pd.read_csv(URL)24 25 def get_response_text2int(self, text):26 """Makes a post request to the endpoint"""27 r = None28 try:29 r = self.client.post("/text2int", json={"content": text}) \30 .json().get("message")31 except:32 pass33 return r34 35 def test_endpoint_text2int(self):36 """Tests if endpoint is working"""37 response = self.client.post("/text2int",38 json={"content": "fourteen"}39 )40 self.assertEqual(response.status_code, 200)41 42 def test_acc_score_text2int(self):43 """Calculates accuracy score for endpoint"""44 45 self.df["text2int"] = self.df["input"].apply(func=self.get_response_text2int)46 self.df["score"] = self.df[["output", "text2int"]].apply(47 lambda row: row[0] == row[1],48 axis=149 )50 self.df.to_csv(f"{DATA_DIR}/text2int_results.csv", index=False)51 acc_score = self.df["score"].mean().__round__(2)52 53 self.assertGreaterEqual(acc_score, 0.5, f"Accuracy score: '{acc_score}'. Value is too low!")54 55 56if __name__ == '__main__':57 unittest.main()58 