glossary test: fixed cleanup of temporary files

This commit is contained in:
2024-02-07 08:38:46 +01:00
parent 92fb2bbe15
commit 9c683be994
+11 -16
View File
@@ -26,7 +26,7 @@ class TestGlossary(unittest.TestCase):
" yes: sí\n" " yes: sí\n"
" I'm going home: me voy a casa\n") " I'm going home: me voy a casa\n")
yaml_file_path = Path(yaml_file.name) yaml_file_path = Path(yaml_file.name)
# create and check valid glossary
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.desc, "A brief description") self.assertEqual(glossary.desc, "A brief description")
@@ -55,7 +55,7 @@ class TestGlossary(unittest.TestCase):
" 'yes': ''\n" " 'yes': ''\n"
" \"I'm going home\": 'me voy a casa'\n") " \"I'm going home\": 'me voy a casa'\n")
yaml_file_path = Path(yaml_file.name) yaml_file_path = Path(yaml_file.name)
# create and check valid glossary
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.desc, "A brief description") self.assertEqual(glossary.desc, "A brief description")
@@ -77,13 +77,12 @@ class TestGlossary(unittest.TestCase):
target_lang="fr", target_lang="fr",
entries={"yes": "oui"}) entries={"yes": "oui"})
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tmp_file: with tempfile.NamedTemporaryFile('w', suffix=glossary_suffix) 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)
# read and check valid YAML
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
content = file.read() content = file.read()
self.assertIn("Name: Test", content) self.assertIn("Name: Test", content)
self.assertIn("Description: Test description", content) self.assertIn("Description: Test description", content)
self.assertIn("ID: '666'", content) self.assertIn("ID: '666'", content)
@@ -92,17 +91,15 @@ class TestGlossary(unittest.TestCase):
self.assertIn("Entries", content) self.assertIn("Entries", content)
# 'yes' is a YAML keyword and therefore quoted # 'yes' is a YAML keyword and therefore quoted
self.assertIn("'yes': oui", content) self.assertIn("'yes': oui", content)
file_path.unlink() # Remove the temporary file
def test_write_read_glossary(self) -> None: def test_write_read_glossary(self) -> None:
# Create glossary instance # Create glossary instance
# -> use 'yes' in order to test if the YAML quoting is correctly removed when reading the file # -> 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"}) 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: with tempfile.NamedTemporaryFile('w', suffix=glossary_suffix) as tmp_file:
file_path = Path(tmp_file.name) file_path = Path(tmp_file.name)
glossary_write.to_file(file_path) glossary_write.to_file(file_path)
# create new instance from glossary file # create new instance from glossary file
glossary_read = Glossary.from_file(file_path) glossary_read = Glossary.from_file(file_path)
self.assertEqual(glossary_write.name, glossary_read.name) self.assertEqual(glossary_write.name, glossary_read.name)
@@ -110,13 +107,11 @@ class TestGlossary(unittest.TestCase):
self.assertEqual(glossary_write.target_lang, glossary_read.target_lang) self.assertEqual(glossary_write.target_lang, glossary_read.target_lang)
self.assertDictEqual(glossary_write.entries, glossary_read.entries) self.assertDictEqual(glossary_write.entries, glossary_read.entries)
file_path.unlink() # Remove the temporary file
def test_import_export_csv(self) -> None: def test_import_export_csv(self) -> None:
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={}) glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={})
# First export to CSV # First export to CSV
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as csvfile: with tempfile.NamedTemporaryFile('w', suffix=glossary_suffix) as csvfile:
csv_file_path = Path(csvfile.name) csv_file_path = Path(csvfile.name)
glossary.entries = {"hello": "salut", "goodbye": "au revoir"} glossary.entries = {"hello": "salut", "goodbye": "au revoir"}
glossary.export_csv(glossary.entries, csv_file_path) glossary.export_csv(glossary.entries, csv_file_path)
@@ -124,13 +119,12 @@ class TestGlossary(unittest.TestCase):
# Now import CSV # Now import CSV
glossary.import_csv(csv_file_path) glossary.import_csv(csv_file_path)
self.assertEqual(glossary.entries, {"hello": "salut", "goodbye": "au revoir"}) self.assertEqual(glossary.entries, {"hello": "salut", "goodbye": "au revoir"})
csv_file_path.unlink() # Remove the temporary file
def test_import_export_tsv(self) -> None: def test_import_export_tsv(self) -> None:
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={}) glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={})
# First export to TSV # First export to TSV
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tsvfile: with tempfile.NamedTemporaryFile('w', suffix=glossary_suffix) as tsvfile:
tsv_file_path = Path(tsvfile.name) tsv_file_path = Path(tsvfile.name)
glossary.entries = {"hello": "salut", "goodbye": "au revoir"} glossary.entries = {"hello": "salut", "goodbye": "au revoir"}
glossary.export_tsv(glossary.entries, tsv_file_path) glossary.export_tsv(glossary.entries, tsv_file_path)
@@ -138,14 +132,13 @@ class TestGlossary(unittest.TestCase):
# Now import TSV # Now import TSV
glossary.import_tsv(tsv_file_path) glossary.import_tsv(tsv_file_path)
self.assertEqual(glossary.entries, {"hello": "salut", "goodbye": "au revoir"}) 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: def test_to_file_wrong_suffix(self) -> None:
""" """
Test for exception if suffix is wrong. Test for exception if suffix is wrong.
""" """
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"})
with tempfile.NamedTemporaryFile('w', delete=False, suffix='.wrong') as tmp_file: with tempfile.NamedTemporaryFile('w', suffix='.wrong') as tmp_file:
file_path = Path(tmp_file.name) file_path = Path(tmp_file.name)
with self.assertRaises(GlossaryError) as err: with self.assertRaises(GlossaryError) as err:
glossary.to_file(file_path) glossary.to_file(file_path)
@@ -156,11 +149,13 @@ class TestGlossary(unittest.TestCase):
Test if suffix is auto-generated if omitted. Test if suffix is auto-generated if omitted.
""" """
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"})
with tempfile.NamedTemporaryFile('w', delete=False, suffix='') as tmp_file: with tempfile.NamedTemporaryFile('w', suffix='') 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)
assert glossary.file_path is not None assert glossary.file_path is not None
self.assertEqual(glossary.file_path.suffix, glossary_suffix) self.assertEqual(glossary.file_path.suffix, glossary_suffix)
# remove glossary file (differs from 'tmp_file' because of the added suffix
glossary.file_path.unlink()
def test_to_str_with_id(self) -> None: def test_to_str_with_id(self) -> None:
# Create a Glossary instance with an ID # Create a Glossary instance with an ID