Compare commits
2 Commits
7591a71471
...
85315d9c1c
| Author | SHA1 | Date | |
|---|---|---|---|
| 85315d9c1c | |||
| 2de0faabdb |
@ -52,10 +52,13 @@ 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=data['Entries'],
|
||||
entries=clean_entries,
|
||||
file_path=file_path,
|
||||
ID=data['ID'] if data['ID'] != 'None' else None)
|
||||
except Exception:
|
||||
|
||||
@ -15,21 +15,21 @@ class TestGlossary(unittest.TestCase):
|
||||
"TargetLang: es\n"
|
||||
"Entries:\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)
|
||||
|
||||
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"})
|
||||
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", 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,7 +41,26 @@ class TestGlossary(unittest.TestCase):
|
||||
self.assertIn("SourceLang: en", content)
|
||||
self.assertIn("TargetLang: fr", 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
|
||||
|
||||
def test_import_export_csv(self) -> None:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user