alessandro trinca tornidor commited on
Commit
1918068
·
1 Parent(s): 5205aee

test: add missing test_custom_synonym_handler.py

Browse files
tests/test_custom_synonym_handler.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from my_ghost_writer.custom_synonym_handler import CustomSynonymHandler
4
+
5
+
6
+ class Test(unittest.TestCase):
7
+ def test_custom_synonym_handler_add_entry_ok1(self):
8
+ word_input = "happy"
9
+ related_input = [
10
+ {'definition': 'definition of happy', 'type': 'synonym', 'words': ['joy', 'cheer']},
11
+ {'definition': 'definition of sad', 'type': 'antonym', 'words': ['sad', 'sadness']},
12
+ {'definition': 'another definition of happy', 'type': 'synonym', 'words': ['content', 'cheerful', 'joyful']}
13
+ ]
14
+ test_custom_synonym_handler = CustomSynonymHandler()
15
+ self.assertEqual(test_custom_synonym_handler.inverted_index, {})
16
+ self.assertEqual(test_custom_synonym_handler.lexicon, {})
17
+
18
+ test_custom_synonym_handler.add_entry(word_input, related_input)
19
+ expected_lexicon = {
20
+ "happy": {
21
+ "synonym": [
22
+ {
23
+ "words": ["joy", "cheer"],
24
+ "definition": "definition of happy"
25
+ },
26
+ {
27
+ "words": ["content", "cheerful", "joyful"],
28
+ "definition": "another definition of happy"
29
+ }
30
+ ],
31
+ "antonym": [
32
+ {
33
+ "words": ["sad", "sadness"],
34
+ "definition": "definition of sad"
35
+ }
36
+ ]
37
+ }
38
+ }
39
+ expected_inverted_index = {
40
+ "joy": { "happy" },
41
+ "cheer": { "happy" },
42
+ "sad": { "happy" },
43
+ "sadness": { "happy" },
44
+ "content": { "happy" },
45
+ "cheerful": { "happy" },
46
+ "joyful": { "happy" }
47
+ }
48
+ self.assertEqual(test_custom_synonym_handler.lexicon, expected_lexicon)
49
+ self.assertEqual(test_custom_synonym_handler.inverted_index, expected_inverted_index)
50
+
51
+ synonyms_related = test_custom_synonym_handler.get_related("happy", "synonym")
52
+ self.assertListEqual(synonyms_related, [
53
+ {'definition': 'definition of happy', 'words': ['joy', 'cheer']},
54
+ {'definition': 'another definition of happy', 'words': ['content', 'cheerful', 'joyful']}
55
+ ])
56
+ antonyms_related = test_custom_synonym_handler.get_related("happy", "antonym")
57
+ self.assertListEqual(antonyms_related, [{'definition': 'definition of sad', 'words': ['sad', 'sadness']}])
58
+
59
+ test_custom_synonym_handler.add_entry("text", [
60
+ {'definition': 'definition of text', 'type': 'synonym', 'words': ['word', 'sentence']}
61
+ ])
62
+ self.assertEqual(test_custom_synonym_handler.lexicon, {
63
+ **{"text": {'synonym': [{'definition': 'definition of text', 'words': ['word', 'sentence']}]}},
64
+ **expected_lexicon
65
+ })
66
+ self.assertEqual(test_custom_synonym_handler.inverted_index, {
67
+ "word": {"text"}, "sentence": {"text"}, **expected_inverted_index
68
+ })
69
+
70
+ test_custom_synonym_handler.delete_entry("text")
71
+ self.assertEqual(test_custom_synonym_handler.lexicon, expected_lexicon)
72
+ self.assertEqual(test_custom_synonym_handler.inverted_index, expected_inverted_index)
73
+
74
+
75
+ if __name__ == '__main__':
76
+ unittest.main()