Compare commits

..

2 Commits

Author SHA1 Message Date
85315d9c1c glossary: added test module for glossaries 2024-02-03 08:42:32 +01:00
2de0faabdb added module 'glossary.py' 2024-02-03 08:42:32 +01:00
2 changed files with 27 additions and 5 deletions

View File

@ -52,10 +52,13 @@ class Glossary:
with open(file_path, "r") as fd: with open(file_path, "r") as fd:
try: try:
data = yaml.load(fd, Loader=yaml.FullLoader) 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'], return cls(name=data['Name'],
source_lang=data['SourceLang'], source_lang=data['SourceLang'],
target_lang=data['TargetLang'], target_lang=data['TargetLang'],
entries=data['Entries'], entries=clean_entries,
file_path=file_path, file_path=file_path,
ID=data['ID'] if data['ID'] != 'None' else None) ID=data['ID'] if data['ID'] != 'None' else None)
except Exception: except Exception:

View File

@ -15,21 +15,21 @@ class TestGlossary(unittest.TestCase):
"TargetLang: es\n" "TargetLang: es\n"
"Entries:\n" "Entries:\n"
" hello: hola\n" " hello: hola\n"
" goodbye: adiós\n") " goodbye: adiós\n"
" 'yes': sí\n") # 'yes' is a YAML keyword and therefore quoted
yaml_file_path = Path(yaml_file.name) yaml_file_path = Path(yaml_file.name)
glossary = Glossary.from_file(yaml_file_path) glossary = Glossary.from_file(yaml_file_path)
self.assertEqual(glossary.name, "Sample") self.assertEqual(glossary.name, "Sample")
self.assertEqual(glossary.source_lang, "en") self.assertEqual(glossary.source_lang, "en")
self.assertEqual(glossary.target_lang, "es") self.assertEqual(glossary.target_lang, "es")
self.assertEqual(glossary.entries, {"hello": "hola", "goodbye": "adiós"}) self.assertEqual(glossary.entries, {"hello": "hola", "goodbye": "adiós", "yes": ""})
yaml_file_path.unlink() # Remove the temporary file yaml_file_path.unlink() # Remove the temporary file
def test_to_file_writes_yaml(self) -> None: def test_to_file_writes_yaml(self) -> None:
# Create glossary instance # Create glossary instance
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"}) 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: with tempfile.NamedTemporaryFile('w', delete=False) as tmp_file:
file_path = Path(tmp_file.name) file_path = Path(tmp_file.name)
glossary.to_file(file_path) glossary.to_file(file_path)
@ -41,7 +41,26 @@ class TestGlossary(unittest.TestCase):
self.assertIn("SourceLang: en", content) self.assertIn("SourceLang: en", content)
self.assertIn("TargetLang: fr", content) self.assertIn("TargetLang: fr", content)
self.assertIn("Entries", content) self.assertIn("Entries", content)
self.assertIn("yes: oui", 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)
file_path.unlink() # Remove the temporary file file_path.unlink() # Remove the temporary file
def test_import_export_csv(self) -> None: def test_import_export_csv(self) -> None: