test_nlp_server.py 1.78 KB
Newer Older
1
from unittest import TestCase
2
import app.nlp_server
3
from fastapi.testclient import TestClient
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from app.summary.summary_strategy_interface import ISummaryStrategy
import json


class TestStrategy1(ISummaryStrategy):
    id = "test1"

    def summarize(self, text: str) -> str:
        return text


class TestStrategy2(ISummaryStrategy):
    id = "test2"

    def summarize(self, text: str) -> str:
        return "result text"
20
21
22
23


class TestNlpApi(TestCase):
    def test_root(self):
24
        client = TestClient(app.nlp_server.app)
25
26
27
        response = client.get("/")
        assert response.status_code == 200
        assert response.json() == {"message": "nlp server is available"}
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

    def test_strategies(self):
        # init test client
        app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
        client = TestClient(app.nlp_server.app)

        # run test
        response = client.get("/strategies")

        #assert results
        assert response.status_code == 200
        assert response.json() == ["test1", "test2"]

    def test_summarize(self):
        # init test client
        app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
        client = TestClient(app.nlp_server.app)

        # run test
        response1 = client.post(
            "/summarize/test1", json.dumps({"text": "test text"}))
        response2 = client.post(
            "/summarize/test2", json.dumps({"text": "test text"}))

        #assert result
        assert response1.status_code == 200
        assert response1.json() == {"strategy": "test1",
                                    "quality": 0.5, "summary": "test text"}
        assert response2.status_code == 200
        assert response2.json() == {"strategy": "test2",
                                    "quality": 0.5, "summary": "result text"}