mgw / tests /my_ghost_writer /test_custom_synonym_handler.py
alessandro trinca tornidor
test: update test cases, optimize some imports
a9c8d84
import unittest
from my_ghost_writer.custom_synonym_handler import CustomSynonymHandler
from my_ghost_writer.type_hints import RelatedEntry, TermRelationships
class TestCustomSynonymHandler(unittest.TestCase):
def setUp(self):
"""Set up a fresh handler and test data for each test."""
self.handler = CustomSynonymHandler()
self.happy_related_data = [
{'definition': 'definition of happy', 'type': 'synonym', 'words': ['joy', 'cheer']},
{'definition': 'definition of sad', 'type': 'antonym', 'words': ['sad', 'sadness']},
{'definition': 'another definition of happy', 'type': 'synonym', 'words': ['content', 'cheerful', 'joyful']}
]
self.happy_related_entries = [RelatedEntry(**rel) for rel in self.happy_related_data]
def test_initial_state(self):
"""Tests that a new handler is empty."""
self.assertEqual(self.handler.lexicon, {})
self.assertEqual(self.handler.inverted_index, {})
def test_add_entry_populates_lexicon_and_index(self):
"""Tests that add_entry correctly populates the lexicon and inverted index."""
# Act
self.handler.add_entry("happy", self.happy_related_entries)
# Assert
expected_lexicon = {
"happy": {
TermRelationships.SYNONYM: [
{"words": ["joy", "cheer"], "definition": "definition of happy"},
{"words": ["content", "cheerful", "joyful"], "definition": "another definition of happy"}
],
TermRelationships.ANTONYM: [
{"words": ["sad", "sadness"], "definition": "definition of sad"}
]
}
}
expected_inverted_index = {
"joy": {"happy"}, "cheer": {"happy"}, "sad": {"happy"},
"sadness": {"happy"}, "content": {"happy"}, "cheerful": {"happy"},
"joyful": {"happy"}
}
self.assertEqual(self.handler.lexicon, expected_lexicon)
self.assertEqual(self.handler.inverted_index, expected_inverted_index)
def test_get_related_retrieves_correct_data(self):
"""Tests that get_related returns the correct entries for a given relationship type."""
# Add a new entry
self.handler.add_entry("happy", self.happy_related_entries)
# get synonyms and antonyms
synonyms = self.handler.get_related("happy", TermRelationships.SYNONYM)
antonyms = self.handler.get_related("happy", TermRelationships.ANTONYM)
# Assert
self.assertCountEqual(synonyms, [
{'words': ['joy', 'cheer'], 'definition': 'definition of happy'},
{'words': ['content', 'cheerful', 'joyful'], 'definition': 'another definition of happy'}
])
self.assertCountEqual(antonyms, [{'words': ['sad', 'sadness'], 'definition': 'definition of sad'}])
def test_get_related_returns_empty_for_no_match(self):
"""Tests that get_related returns an empty list for non-existent words or types."""
# Add a new entry
self.handler.add_entry("happy", self.happy_related_entries)
# get hypernyms and synonyms
empty_result_for_type = self.handler.get_related("happy", TermRelationships.HYPERNYM)
empty_result_for_word = self.handler.get_related("sad", TermRelationships.SYNONYM)
# Assert
self.assertEqual(empty_result_for_type, [])
self.assertEqual(empty_result_for_word, [])
def test_delete_entry_removes_from_lexicon_and_index(self):
"""Tests that delete_entry correctly removes a word and its associations."""
# Add a new entry
self.handler.add_entry("happy", self.happy_related_entries)
text_entry = RelatedEntry(**{'definition': 'text def', 'type': 'synonym', 'words': ['word']})
self.handler.add_entry("text", [text_entry])
# delete
self.handler.delete_entry("text")
# Assert
self.assertNotIn("text", self.handler.lexicon)
self.assertNotIn("word", self.handler.inverted_index)
# Ensure other entries are unaffected
self.assertIn("happy", self.handler.lexicon)
self.assertIn("joy", self.handler.inverted_index)
def test_delete_nonexistent_entry_raises_key_error(self):
"""Tests that deleting a word not in the lexicon raises a KeyError."""
with self.assertRaises(KeyError):
self.handler.delete_entry("nonexistent")
if __name__ == '__main__':
unittest.main()