translation: added test module for glossary
This commit is contained in:
parent
ab5d8c251a
commit
d72abdc4c0
73
tests/test_glossary.py
Normal file
73
tests/test_glossary.py
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
import unittest
|
||||||
|
import tempfile
|
||||||
|
from pathlib import Path
|
||||||
|
from chatmastermind.glossary import Glossary
|
||||||
|
|
||||||
|
|
||||||
|
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) as yaml_file:
|
||||||
|
yaml_file.write("Name: Sample\n"
|
||||||
|
"ID: '123'\n"
|
||||||
|
"SourceLang: en\n"
|
||||||
|
"TargetLang: es\n"
|
||||||
|
"Entries:\n"
|
||||||
|
" hello: hola\n"
|
||||||
|
" goodbye: adiós\n")
|
||||||
|
yaml_file_path = Path(yaml_file.name)
|
||||||
|
|
||||||
|
glossary = Glossary.from_file(yaml_file_path)
|
||||||
|
self.assertEqual(glossary.name, "Sample")
|
||||||
|
self.assertEqual(glossary.source_lang, "en")
|
||||||
|
self.assertEqual(glossary.target_lang, "es")
|
||||||
|
self.assertEqual(glossary.entries, {"hello": "hola", "goodbye": "adiós"})
|
||||||
|
yaml_file_path.unlink() # Remove the temporary file
|
||||||
|
|
||||||
|
def test_to_file_writes_yaml(self) -> None:
|
||||||
|
# Create glossary instance
|
||||||
|
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"})
|
||||||
|
|
||||||
|
# Use a temporary file
|
||||||
|
with tempfile.NamedTemporaryFile('w', delete=False) 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("SourceLang: en", content)
|
||||||
|
self.assertIn("TargetLang: fr", content)
|
||||||
|
self.assertIn("Entries", content)
|
||||||
|
self.assertIn("yes: oui", content)
|
||||||
|
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) 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) 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
|
||||||
Loading…
x
Reference in New Issue
Block a user