File size: 1,841 Bytes
5f5216b
25fca04
5f5216b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unittest
from post_process import process_response


class TestProcessResponse(unittest.TestCase):

    def test_valid_response(self):
        resp = {
            "english": "I scored a slam dunk yesterday at the West Park with my classmates, succeeding in a dunk.",
            "important_words": [
                {
                    "word_en": "dunk",
                    "meaning_ch": "扣篮",
                    "usage": "He made a spectacular dunk."
                },
                {
                    "word_en": "slam",
                    "meaning_ch": "猛烈撞击",
                    "usage": "The door slammed shut."
                },
                {
                    "word_en": "scored",
                    "meaning_ch": "得分",
                    "usage": "She scored the winning goal."
                }
            ]
        }
        english, words = process_response(resp)
        self.assertEqual(
            english, "I scored a slam dunk yesterday at the West Park with my classmates, succeeding in a dunk.")
        self.assertEqual(len(words), 3)
        self.assertEqual(words[0]["word_en"], "dunk")

    def test_invalid_response_missing_keys(self):
        resp = {
            "english": "I scored a slam dunk yesterday at the West Park with my classmates, succeeding in a dunk."
        }
        english, words = process_response(resp)
        self.assertIsNone(english)
        self.assertIsNone(words)

    def test_invalid_response_empty(self):
        resp = {}
        english, words = process_response(resp)
        self.assertIsNone(english)
        self.assertIsNone(words)

    def test_none_response(self):
        english, words = process_response(None)
        self.assertIsNone(english)
        self.assertIsNone(words)


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