glossary test: added suffix testcases
This commit is contained in:
parent
6e89e014bf
commit
a2ae52014b
@ -1,14 +1,17 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import tempfile
|
import tempfile
|
||||||
from pathlib import Path
|
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):
|
class TestGlossary(unittest.TestCase):
|
||||||
|
|
||||||
def test_from_file_valid_yaml(self) -> None:
|
def test_from_file_valid_yaml(self) -> None:
|
||||||
# Prepare a temporary YAML file with valid content
|
# 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"
|
yaml_file.write("Name: Sample\n"
|
||||||
"ID: '123'\n"
|
"ID: '123'\n"
|
||||||
"SourceLang: en\n"
|
"SourceLang: en\n"
|
||||||
@ -30,7 +33,7 @@ class TestGlossary(unittest.TestCase):
|
|||||||
# 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"})
|
||||||
|
|
||||||
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)
|
file_path = Path(tmp_file.name)
|
||||||
glossary.to_file(file_path)
|
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
|
# -> 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) as tmp_file:
|
with tempfile.NamedTemporaryFile('w', delete=False, 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)
|
||||||
|
|
||||||
@ -67,7 +70,7 @@ class TestGlossary(unittest.TestCase):
|
|||||||
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) as csvfile:
|
with tempfile.NamedTemporaryFile('w', delete=False, 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)
|
||||||
@ -81,7 +84,7 @@ class TestGlossary(unittest.TestCase):
|
|||||||
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) as tsvfile:
|
with tempfile.NamedTemporaryFile('w', delete=False, 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)
|
||||||
@ -90,3 +93,25 @@ class TestGlossary(unittest.TestCase):
|
|||||||
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
|
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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user