Compare commits

..

2 Commits

Author SHA1 Message Date
7591a71471 glossary: added test module for glossaries 2024-02-03 07:59:11 +01:00
e02074d96b added module 'glossary.py' 2024-02-03 07:59:11 +01:00
2 changed files with 5 additions and 27 deletions

View File

@ -52,13 +52,10 @@ class Glossary:
with open(file_path, "r") as fd:
try:
data = yaml.load(fd, Loader=yaml.FullLoader)
# remove any quotes from the entries that YAML may have added while dumping
# (e. g. for special keywords like 'yes')
clean_entries = {key.strip('\"\' '): value for key, value in data['Entries'].items()}
return cls(name=data['Name'],
source_lang=data['SourceLang'],
target_lang=data['TargetLang'],
entries=clean_entries,
entries=data['Entries'],
file_path=file_path,
ID=data['ID'] if data['ID'] != 'None' else None)
except Exception:

View File

@ -15,21 +15,21 @@ class TestGlossary(unittest.TestCase):
"TargetLang: es\n"
"Entries:\n"
" hello: hola\n"
" goodbye: adiós\n"
" 'yes': sí\n") # 'yes' is a YAML keyword and therefore quoted
" 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", "yes": ""})
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)
@ -41,26 +41,7 @@ class TestGlossary(unittest.TestCase):
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) 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)
self.assertIn("yes: oui", content)
file_path.unlink() # Remove the temporary file
def test_import_export_csv(self) -> None: