test_nlp_server.py 3.29 KB
Newer Older
1
from unittest import TestCase
2

3
from fastapi.testclient import TestClient
4
5

import app.nlp_server
6
7
from app.summary.summary_strategy_interface import ISummaryStrategy

8
9
import json

10
11

class TestStrategy1(ISummaryStrategy):
12
13
14
15
16
    """
    This test implementation is used by test_strategies() & test_summarize().
    Its the first of two test implementations
    """

17
18
    id = "test1"

19
    def summarize(self, text: str, max_length: int) -> str:
20
21
22
23
        return text


class TestStrategy2(ISummaryStrategy):
24
25
26
27
28
    """
    This test implementation is used by test_strategies() & test_summarize().
    Its the second of two test implementations
    """

29
30
    id = "test2"

31
    def summarize(self, text: str, max_length: int) -> str:
32
        return "result text"
33
34
35
36


class TestNlpApi(TestCase):
    def test_root(self):
37
38
39
40
        """
        In this case the "/" path of the rest api is tested.
        """

41
        client = TestClient(app.nlp_server.app)
42
43
44
        response = client.get("/")
        assert response.status_code == 200
        assert response.json() == {"message": "nlp server is available"}
45
46

    def test_strategies(self):
47
48
49
50
        """
        In this case the "/strategies" path of the rest api is tested.
        """

51
52
53
54
55
56
57
        # init test client
        app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
        client = TestClient(app.nlp_server.app)

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

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

62
    def test_get_summarize(self):
63
        """
64
65
        In this case the get variant of the "/summarize/{strategy}"
        path of the rest api is tested
66
67
        """

68
69
70
71
72
        # init test client
        app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
        client = TestClient(app.nlp_server.app)

        # run test
73
74
75
76
        response1 = client.get(
            "/summarize/test1?max_length=130&text=test%20text")
        response2 = client.get(
            "/summarize/test2?max_length=130&text=test%20text")
77

78
        # assert result
79
80
81
82
83
84
        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"}
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

    def test_post_summarize(self):
        """
        In this case the post variant of the "/summarize/{strategy}"
        path of the rest api is tested
        """

        app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
        client = TestClient(app.nlp_server.app)
        test_data = json.dumps({"text": "test text", "maxLength": 130})

        # run test
        response1 = client.post(
            "/summarize/test1", test_data)
        response2 = client.post(
            "/summarize/test2", test_data)

        # 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"}