import unittest import tempfile from pathlib import Path from chatmastermind.glossary import Glossary, GlossaryError glossary_suffix: str = Glossary.file_suffix class TestGlossary(unittest.TestCase): def test_from_file_valid_yaml(self) -> None: # Prepare a temporary YAML file with valid content with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as yaml_file: yaml_file.write("Name: Sample\n" "Description: A brief description\n" "ID: '123'\n" "SourceLang: en\n" "TargetLang: es\n" "Entries:\n" " hello: hola\n" " goodbye: adiós\n" " 'yes': sí\n") # 'yes' is a YAML keyword and therefore quoted yaml_file_path = Path(yaml_file.name) glossary = Glossary.from_file(yaml_file_path) self.assertEqual(glossary.name, "Sample") self.assertEqual(glossary.desc, "A brief description") self.assertEqual(glossary.ID, "123") self.assertEqual(glossary.source_lang, "en") self.assertEqual(glossary.target_lang, "es") self.assertEqual(glossary.entries, {"hello": "hola", "goodbye": "adiós", "yes": "sí"}) yaml_file_path.unlink() # Remove the temporary file def test_to_file_writes_yaml(self) -> None: # Create glossary instance glossary = Glossary(name="Test", desc="Test description", ID="666", source_lang="en", target_lang="fr", entries={"yes": "oui"}) with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tmp_file: file_path = Path(tmp_file.name) glossary.to_file(file_path) with open(file_path, 'r') as file: content = file.read() self.assertIn("Name: Test", content) self.assertIn("Description: Test description", content) self.assertIn("ID: '666'", content) self.assertIn("SourceLang: en", content) self.assertIn("TargetLang: fr", content) self.assertIn("Entries", content) # 'yes' is a YAML keyword and therefore quoted self.assertIn("'yes': oui", content) file_path.unlink() # Remove the temporary file def test_write_read_glossary(self) -> None: # Create glossary instance # -> use 'yes' in order to test if the YAML quoting is correctly removed when reading the file glossary_write = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"}) with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tmp_file: file_path = Path(tmp_file.name) glossary_write.to_file(file_path) # create new instance from glossary file glossary_read = Glossary.from_file(file_path) self.assertEqual(glossary_write.name, glossary_read.name) self.assertEqual(glossary_write.source_lang, glossary_read.source_lang) self.assertEqual(glossary_write.target_lang, glossary_read.target_lang) self.assertDictEqual(glossary_write.entries, glossary_read.entries) file_path.unlink() # Remove the temporary file def test_import_export_csv(self) -> None: glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={}) # First export to CSV with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as csvfile: csv_file_path = Path(csvfile.name) glossary.entries = {"hello": "salut", "goodbye": "au revoir"} glossary.export_csv(glossary.entries, csv_file_path) # Now import CSV glossary.import_csv(csv_file_path) self.assertEqual(glossary.entries, {"hello": "salut", "goodbye": "au revoir"}) csv_file_path.unlink() # Remove the temporary file def test_import_export_tsv(self) -> None: glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={}) # First export to TSV with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tsvfile: tsv_file_path = Path(tsvfile.name) glossary.entries = {"hello": "salut", "goodbye": "au revoir"} glossary.export_tsv(glossary.entries, tsv_file_path) # Now import TSV glossary.import_tsv(tsv_file_path) self.assertEqual(glossary.entries, {"hello": "salut", "goodbye": "au revoir"}) tsv_file_path.unlink() # Remove the temporary file def test_to_file_wrong_suffix(self) -> None: """ Test for exception if suffix is wrong. """ glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"}) with tempfile.NamedTemporaryFile('w', delete=False, suffix='.wrong') as tmp_file: file_path = Path(tmp_file.name) with self.assertRaises(GlossaryError) as err: glossary.to_file(file_path) self.assertEqual(str(err.exception), "File suffix '.wrong' is not supported") def test_to_file_auto_suffix(self) -> None: """ Test if suffix is auto-generated if omitted. """ glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"}) with tempfile.NamedTemporaryFile('w', delete=False, suffix='') as tmp_file: file_path = Path(tmp_file.name) glossary.to_file(file_path) assert glossary.file_path is not None self.assertEqual(glossary.file_path.suffix, glossary_suffix) def test_to_str_with_id(self) -> None: # Create a Glossary instance with an ID glossary_with_id = Glossary(name="TestGlossary", source_lang="en", target_lang="fr", desc="A simple test glossary", ID="1001", entries={"one": "un"}) glossary_str = glossary_with_id.to_str() self.assertIn("TestGlossary (ID: 1001):", glossary_str) self.assertIn("- A simple test glossary", glossary_str) self.assertIn("- Languages: en -> fr", glossary_str) self.assertIn("- Entries: 1", glossary_str) def test_to_str_with_id_and_entries(self) -> None: # Create a Glossary instance with an ID and include entries glossary_with_entries = Glossary(name="TestGlossaryWithEntries", source_lang="en", target_lang="fr", desc="Another test glossary", ID="2002", entries={"hello": "salut", "goodbye": "au revoir"}) glossary_str_with_entries = glossary_with_entries.to_str(with_entries=True) self.assertIn("TestGlossaryWithEntries (ID: 2002):", glossary_str_with_entries) self.assertIn("- Entries:", glossary_str_with_entries) self.assertIn(" hello : salut", glossary_str_with_entries) self.assertIn(" goodbye : au revoir", glossary_str_with_entries) def test_to_str_without_id(self) -> None: # Create a Glossary instance without an ID glossary_without_id = Glossary(name="TestGlossaryNoID", source_lang="en", target_lang="fr", desc="A test glossary without an ID", ID=None, entries={"yes": "oui"}) glossary_str_no_id = glossary_without_id.to_str() self.assertIn("TestGlossaryNoID (ID: None):", glossary_str_no_id) self.assertIn("- A test glossary without an ID", glossary_str_no_id) self.assertIn("- Languages: en -> fr", glossary_str_no_id) self.assertIn("- Entries: 1", glossary_str_no_id) def test_to_str_without_id_and_no_entries(self) -> None: # Create a Glossary instance without an ID and no entries glossary_no_id_no_entries = Glossary(name="EmptyGlossary", source_lang="en", target_lang="fr", desc="An empty test glossary", ID=None, entries={}) glossary_str_no_id_no_entries = glossary_no_id_no_entries.to_str() self.assertIn("EmptyGlossary (ID: None):", glossary_str_no_id_no_entries) self.assertIn("- An empty test glossary", glossary_str_no_id_no_entries) self.assertIn("- Languages: en -> fr", glossary_str_no_id_no_entries) self.assertIn("- Entries: 0", glossary_str_no_id_no_entries)