From a2ae52014b4cbec2d9de935b6db33ffb13884794 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 3 Feb 2024 21:10:21 +0100 Subject: [PATCH] glossary test: added suffix testcases --- tests/test_glossary.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/tests/test_glossary.py b/tests/test_glossary.py index 55c732d..74d60a9 100644 --- a/tests/test_glossary.py +++ b/tests/test_glossary.py @@ -1,14 +1,17 @@ import unittest import tempfile from pathlib import Path -from chatmastermind.glossary import Glossary +from chatmastermind.glossary import Glossary, GlossaryError + + +glossary_suffix: str = Glossary.file_suffix 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: + with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as yaml_file: yaml_file.write("Name: Sample\n" "ID: '123'\n" "SourceLang: en\n" @@ -30,7 +33,7 @@ class TestGlossary(unittest.TestCase): # Create glossary instance glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"}) - with tempfile.NamedTemporaryFile('w', delete=False) as tmp_file: + with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tmp_file: file_path = Path(tmp_file.name) glossary.to_file(file_path) @@ -50,7 +53,7 @@ class TestGlossary(unittest.TestCase): # -> 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: + with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tmp_file: file_path = Path(tmp_file.name) glossary_write.to_file(file_path) @@ -67,7 +70,7 @@ class TestGlossary(unittest.TestCase): glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={}) # First export to CSV - with tempfile.NamedTemporaryFile('w', delete=False) as csvfile: + with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as csvfile: csv_file_path = Path(csvfile.name) glossary.entries = {"hello": "salut", "goodbye": "au revoir"} glossary.export_csv(glossary.entries, csv_file_path) @@ -81,7 +84,7 @@ class TestGlossary(unittest.TestCase): glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={}) # First export to TSV - with tempfile.NamedTemporaryFile('w', delete=False) as tsvfile: + with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tsvfile: tsv_file_path = Path(tsvfile.name) glossary.entries = {"hello": "salut", "goodbye": "au revoir"} glossary.export_tsv(glossary.entries, tsv_file_path) @@ -90,3 +93,25 @@ class TestGlossary(unittest.TestCase): glossary.import_tsv(tsv_file_path) 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: + """ + Test for exception if suffix is wrong. + """ + glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"}) + with tempfile.NamedTemporaryFile('w', delete=False, suffix='.wrong') as tmp_file: + file_path = Path(tmp_file.name) + with self.assertRaises(GlossaryError) as err: + glossary.to_file(file_path) + self.assertEqual(str(err.exception), "File suffix '.wrong' is not supported") + + def test_to_file_auto_suffix(self) -> None: + """ + Test if suffix is auto-generated if omitted. + """ + glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"}) + with tempfile.NamedTemporaryFile('w', delete=False, suffix='') as tmp_file: + file_path = Path(tmp_file.name) + glossary.to_file(file_path) + assert glossary.file_path is not None + self.assertEqual(glossary.file_path.suffix, glossary_suffix)