alessandro trinca tornidor commited on
Commit
a9c8d84
·
1 Parent(s): a707261

test: update test cases, optimize some imports

Browse files
my_ghost_writer/jsonpath_comparator.py CHANGED
@@ -1,4 +1,3 @@
1
- from jsonpath_ng import parse
2
  from jsonpath_ng.ext import parse as parse_ext
3
  from typing import Dict, Set, Any, List
4
 
 
 
1
  from jsonpath_ng.ext import parse as parse_ext
2
  from typing import Dict, Set, Any, List
3
 
tests/my_ghost_writer/test_custom_synonym_handler.py CHANGED
@@ -5,75 +5,96 @@ from my_ghost_writer.type_hints import RelatedEntry, TermRelationships
5
 
6
 
7
  class TestCustomSynonymHandler(unittest.TestCase):
8
- def test_custom_synonym_handler_add_entry_ok1(self):
9
- word_input = "happy"
10
- related_input = []
11
- for rel in [
12
- {'definition': 'definition of happy', 'type': 'synonym', 'words': ['joy', 'cheer']},
13
- {'definition': 'definition of sad', 'type': 'antonym', 'words': ['sad', 'sadness']},
14
- {'definition': 'another definition of happy', 'type': 'synonym', 'words': ['content', 'cheerful', 'joyful']}
15
- ]:
16
- tmp = RelatedEntry(**rel)
17
- related_input.append(tmp)
18
- test_custom_synonym_handler = CustomSynonymHandler()
19
- self.assertEqual(test_custom_synonym_handler.inverted_index, {})
20
- self.assertEqual(test_custom_synonym_handler.lexicon, {})
21
-
22
- test_custom_synonym_handler.add_entry(word_input, related_input)
 
 
 
 
 
 
23
  expected_lexicon = {
24
  "happy": {
25
- "synonym": [
26
- {
27
- "words": ["joy", "cheer"],
28
- "definition": "definition of happy"
29
- },
30
- {
31
- "words": ["content", "cheerful", "joyful"],
32
- "definition": "another definition of happy"
33
- }
34
  ],
35
- "antonym": [
36
- {
37
- "words": ["sad", "sadness"],
38
- "definition": "definition of sad"
39
- }
40
  ]
41
  }
42
  }
43
  expected_inverted_index = {
44
- "joy": { "happy" },
45
- "cheer": { "happy" },
46
- "sad": { "happy" },
47
- "sadness": { "happy" },
48
- "content": { "happy" },
49
- "cheerful": { "happy" },
50
- "joyful": { "happy" }
51
  }
52
- self.assertEqual(test_custom_synonym_handler.lexicon, expected_lexicon)
53
- self.assertEqual(test_custom_synonym_handler.inverted_index, expected_inverted_index)
54
 
55
- synonyms_related = test_custom_synonym_handler.get_related("happy", TermRelationships.SYNONYM)
56
- self.assertListEqual(synonyms_related, [
57
- {'definition': 'definition of happy', 'words': ['joy', 'cheer']},
58
- {'definition': 'another definition of happy', 'words': ['content', 'cheerful', 'joyful']}
59
- ])
60
- antonyms_related = test_custom_synonym_handler.get_related("happy", TermRelationships.ANTONYM)
61
- self.assertListEqual(antonyms_related, [{'definition': 'definition of sad', 'words': ['sad', 'sadness']}])
 
62
 
63
- test_custom_synonym_handler.add_entry("text", [
64
- RelatedEntry(**{'definition': 'definition of text', 'type': 'synonym', 'words': ['word', 'sentence']})
 
 
65
  ])
66
- self.assertEqual(test_custom_synonym_handler.lexicon, {
67
- **{"text": {'synonym': [{'definition': 'definition of text', 'words': ['word', 'sentence']}]}},
68
- **expected_lexicon
69
- })
70
- self.assertEqual(test_custom_synonym_handler.inverted_index, {
71
- "word": {"text"}, "sentence": {"text"}, **expected_inverted_index
72
- })
73
-
74
- test_custom_synonym_handler.delete_entry("text")
75
- self.assertEqual(test_custom_synonym_handler.lexicon, expected_lexicon)
76
- self.assertEqual(test_custom_synonym_handler.inverted_index, expected_inverted_index)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
 
79
  if __name__ == '__main__':
 
5
 
6
 
7
  class TestCustomSynonymHandler(unittest.TestCase):
8
+ def setUp(self):
9
+ """Set up a fresh handler and test data for each test."""
10
+ self.handler = CustomSynonymHandler()
11
+ self.happy_related_data = [
12
+ {'definition': 'definition of happy', 'type': 'synonym', 'words': ['joy', 'cheer']},
13
+ {'definition': 'definition of sad', 'type': 'antonym', 'words': ['sad', 'sadness']},
14
+ {'definition': 'another definition of happy', 'type': 'synonym', 'words': ['content', 'cheerful', 'joyful']}
15
+ ]
16
+ self.happy_related_entries = [RelatedEntry(**rel) for rel in self.happy_related_data]
17
+
18
+ def test_initial_state(self):
19
+ """Tests that a new handler is empty."""
20
+ self.assertEqual(self.handler.lexicon, {})
21
+ self.assertEqual(self.handler.inverted_index, {})
22
+
23
+ def test_add_entry_populates_lexicon_and_index(self):
24
+ """Tests that add_entry correctly populates the lexicon and inverted index."""
25
+ # Act
26
+ self.handler.add_entry("happy", self.happy_related_entries)
27
+
28
+ # Assert
29
  expected_lexicon = {
30
  "happy": {
31
+ TermRelationships.SYNONYM: [
32
+ {"words": ["joy", "cheer"], "definition": "definition of happy"},
33
+ {"words": ["content", "cheerful", "joyful"], "definition": "another definition of happy"}
 
 
 
 
 
 
34
  ],
35
+ TermRelationships.ANTONYM: [
36
+ {"words": ["sad", "sadness"], "definition": "definition of sad"}
 
 
 
37
  ]
38
  }
39
  }
40
  expected_inverted_index = {
41
+ "joy": {"happy"}, "cheer": {"happy"}, "sad": {"happy"},
42
+ "sadness": {"happy"}, "content": {"happy"}, "cheerful": {"happy"},
43
+ "joyful": {"happy"}
 
 
 
 
44
  }
45
+ self.assertEqual(self.handler.lexicon, expected_lexicon)
46
+ self.assertEqual(self.handler.inverted_index, expected_inverted_index)
47
 
48
+ def test_get_related_retrieves_correct_data(self):
49
+ """Tests that get_related returns the correct entries for a given relationship type."""
50
+ # Add a new entry
51
+ self.handler.add_entry("happy", self.happy_related_entries)
52
+
53
+ # get synonyms and antonyms
54
+ synonyms = self.handler.get_related("happy", TermRelationships.SYNONYM)
55
+ antonyms = self.handler.get_related("happy", TermRelationships.ANTONYM)
56
 
57
+ # Assert
58
+ self.assertCountEqual(synonyms, [
59
+ {'words': ['joy', 'cheer'], 'definition': 'definition of happy'},
60
+ {'words': ['content', 'cheerful', 'joyful'], 'definition': 'another definition of happy'}
61
  ])
62
+ self.assertCountEqual(antonyms, [{'words': ['sad', 'sadness'], 'definition': 'definition of sad'}])
63
+
64
+ def test_get_related_returns_empty_for_no_match(self):
65
+ """Tests that get_related returns an empty list for non-existent words or types."""
66
+ # Add a new entry
67
+ self.handler.add_entry("happy", self.happy_related_entries)
68
+
69
+ # get hypernyms and synonyms
70
+ empty_result_for_type = self.handler.get_related("happy", TermRelationships.HYPERNYM)
71
+ empty_result_for_word = self.handler.get_related("sad", TermRelationships.SYNONYM)
72
+
73
+ # Assert
74
+ self.assertEqual(empty_result_for_type, [])
75
+ self.assertEqual(empty_result_for_word, [])
76
+
77
+ def test_delete_entry_removes_from_lexicon_and_index(self):
78
+ """Tests that delete_entry correctly removes a word and its associations."""
79
+ # Add a new entry
80
+ self.handler.add_entry("happy", self.happy_related_entries)
81
+ text_entry = RelatedEntry(**{'definition': 'text def', 'type': 'synonym', 'words': ['word']})
82
+ self.handler.add_entry("text", [text_entry])
83
+
84
+ # delete
85
+ self.handler.delete_entry("text")
86
+
87
+ # Assert
88
+ self.assertNotIn("text", self.handler.lexicon)
89
+ self.assertNotIn("word", self.handler.inverted_index)
90
+ # Ensure other entries are unaffected
91
+ self.assertIn("happy", self.handler.lexicon)
92
+ self.assertIn("joy", self.handler.inverted_index)
93
+
94
+ def test_delete_nonexistent_entry_raises_key_error(self):
95
+ """Tests that deleting a word not in the lexicon raises a KeyError."""
96
+ with self.assertRaises(KeyError):
97
+ self.handler.delete_entry("nonexistent")
98
 
99
 
100
  if __name__ == '__main__':
tests/my_ghost_writer/test_text_parsers2.py CHANGED
@@ -4,11 +4,11 @@ from unittest.mock import patch, MagicMock
4
 
5
  from fastapi import HTTPException
6
 
 
7
  from my_ghost_writer.text_parsers2 import (extract_contextual_info_by_indices, get_wordnet_synonyms, inflect_synonym,
8
  is_nlp_available, process_synonym_groups)
9
- from my_ghost_writer.jsonpath_comparator import JSONPathComparator
10
- from my_ghost_writer.jsonpath_extractor import JSONPathStructureAnalyzer, analyze_dict_list_simple
11
- from my_ghost_writer.type_hints import TermRelationships
12
  from tests import EVENTS_FOLDER
13
  from tests.my_ghost_writer.helpers_tests import analyze_detailed_report_lists
14
 
@@ -256,6 +256,41 @@ class TestTextParsers2(unittest.TestCase):
256
  # Test with a regular verb
257
  self.assertEqual(inflect_synonym("look", original_token_info), "look")
258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  def test_process_synonym_groups(self):
260
  """Tests the full processing pipeline for a verb."""
261
  word = "look"
@@ -318,6 +353,41 @@ class TestTextParsers2(unittest.TestCase):
318
  result = process_synonym_groups("look", context_info)
319
  self.assertListEqual(result, [])
320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
 
322
  if __name__ == '__main__':
323
  unittest.main()
 
4
 
5
  from fastapi import HTTPException
6
 
7
+ from my_ghost_writer.custom_synonym_handler import CustomSynonymHandler
8
  from my_ghost_writer.text_parsers2 import (extract_contextual_info_by_indices, get_wordnet_synonyms, inflect_synonym,
9
  is_nlp_available, process_synonym_groups)
10
+ from my_ghost_writer.jsonpath_extractor import JSONPathStructureAnalyzer
11
+ from my_ghost_writer.type_hints import TermRelationships, RelatedEntry
 
12
  from tests import EVENTS_FOLDER
13
  from tests.my_ghost_writer.helpers_tests import analyze_detailed_report_lists
14
 
 
256
  # Test with a regular verb
257
  self.assertEqual(inflect_synonym("look", original_token_info), "look")
258
 
259
+ def test_inflect_synonym_verbs(self):
260
+ """Tests various verb inflections using subtests."""
261
+ test_cases = [
262
+ # (tag, synonym, expected_inflection)
263
+ ("VBD", "write", "wrote"), # Past tense
264
+ ("VBD", "look", "looked"),
265
+ ("VBG", "write", "writing"), # Present participle
266
+ ("VBG", "look", "looking"),
267
+ ("VBZ", "write", "writes"), # 3rd person singular
268
+ ("VBZ", "look", "looks"),
269
+ ]
270
+
271
+ for tag, synonym, expected in test_cases:
272
+ with self.subTest(tag=tag, synonym=synonym):
273
+ original_token_info = {
274
+ 'pos': 'VERB', 'tag': tag, 'is_lower': True, 'is_title': False, 'is_upper': False
275
+ }
276
+ self.assertEqual(inflect_synonym(synonym, original_token_info), expected)
277
+
278
+ def test_inflect_synonym_casing(self):
279
+ """Tests that casing is correctly applied during inflection."""
280
+ test_cases = [
281
+ # (is_title, is_upper, synonym, expected)
282
+ (True, False, "write", "Wrote"),
283
+ (False, True, "write", "WROTE"),
284
+ (False, False, "look", "looked"),
285
+ ]
286
+ for is_title, is_upper, synonym, expected in test_cases:
287
+ with self.subTest(is_title=is_title, is_upper=is_upper):
288
+ original_token_info = {
289
+ 'pos': 'VERB', 'tag': 'VBD', 'is_lower': not (is_title or is_upper),
290
+ 'is_title': is_title, 'is_upper': is_upper
291
+ }
292
+ self.assertEqual(inflect_synonym(synonym, original_token_info), expected)
293
+
294
  def test_process_synonym_groups(self):
295
  """Tests the full processing pipeline for a verb."""
296
  word = "look"
 
353
  result = process_synonym_groups("look", context_info)
354
  self.assertListEqual(result, [])
355
 
356
+ @patch("my_ghost_writer.text_parsers2.custom_synonym_handler", new_callable=CustomSynonymHandler)
357
+ def test_process_synonym_groups_includes_custom_entries(self, mock_handler):
358
+ """Tests that custom synonyms are correctly processed and included in the results."""
359
+ # Arrange
360
+ # 1. Add a custom synonym to our mocked handler
361
+ custom_entry = RelatedEntry(
362
+ type=TermRelationships.SYNONYM,
363
+ words=["gleeful", "elated"],
364
+ definition="A custom definition for happy"
365
+ )
366
+ mock_handler.add_entry("happy", [custom_entry])
367
+
368
+ # 2. Define the context for the word "happy"
369
+ word = "happy"
370
+ context_info = {
371
+ 'lemma': 'happy', 'pos': 'ADJ', 'tag': 'JJ', 'is_lower': True,
372
+ 'is_title': False, 'is_upper': False
373
+ }
374
+
375
+ # Act
376
+ result_groups = process_synonym_groups(word, context_info)
377
+
378
+ # Assert
379
+ # 1. Find the group that came from our custom source
380
+ custom_group = next((g for g in result_groups if g.related_words and g.related_words[0].is_custom), None)
381
+
382
+ # 2. Assert that the custom group was found and has the correct data
383
+ self.assertIsNotNone(custom_group, "A custom synonym group should have been found in the results.")
384
+ self.assertEqual(custom_group.definition, "A custom definition for happy")
385
+ self.assertEqual(custom_group.relation_type, TermRelationships.SYNONYM)
386
+
387
+ # 3. Check that the custom words are present
388
+ custom_base_forms = {word.base_form for word in custom_group.related_words}
389
+ self.assertEqual(custom_base_forms, {"gleeful", "elated"})
390
+
391
 
392
  if __name__ == '__main__':
393
  unittest.main()