diff --git a/tests/test_glossary_cmd.py b/tests/test_glossary_cmd.py index 86f7048..cc57b8f 100644 --- a/tests/test_glossary_cmd.py +++ b/tests/test_glossary_cmd.py @@ -1,6 +1,8 @@ import unittest import argparse import tempfile +import io +from contextlib import redirect_stdout from chatmastermind.configuration import Config from chatmastermind.commands.glossary import ( Glossary, @@ -13,7 +15,7 @@ from chatmastermind.commands.glossary import ( ) -class TestGlossaryCmd(unittest.TestCase): +class TestGlossaryCmdNoGlossaries(unittest.TestCase): def setUp(self) -> None: # create DB and cache @@ -96,3 +98,52 @@ class TestGlossaryCmd(unittest.TestCase): create_glossary(self.args, self.config) self.assertIn(str(err.exception).lower(), "already exists") expected_path.unlink() + + +class TestGlossaryCmdWithGlossaries(unittest.TestCase): + + def setUp(self) -> None: + # create DB and cache + self.db_dir = tempfile.TemporaryDirectory() + self.cache_dir = tempfile.TemporaryDirectory() + self.glossaries_dir = tempfile.TemporaryDirectory() + # create configuration + self.config = Config() + self.config.cache = self.cache_dir.name + self.config.db = self.db_dir.name + self.config.glossaries = self.glossaries_dir.name + # create a mock argparse.Namespace + self.args = argparse.Namespace( + create=True, + list=False, + print=False, + name='Glossary1', + file=None, + source_lang='en', + target_lang='de', + description=False, + ) + # create Glossary1 + glossary_cmd(self.args, self.config) + self.Glossary1_path = get_glossary_file_path('Glossary1', self.config) + # create Glossary2 + self.args.name = 'Glossary2' + glossary_cmd(self.args, self.config) + self.Glossary2_path = get_glossary_file_path('Glossary2', self.config) + + def test_glossaries_exist(self) -> None: + """ + Test if the default glossaries created in setUp exist. + """ + glo = Glossary.from_file(self.Glossary1_path) + self.assertEqual(glo.name, 'Glossary1') + glo = Glossary.from_file(self.Glossary2_path) + self.assertEqual(glo.name, 'Glossary2') + + def test_glossaries_list(self) -> None: + self.args.create = False + self.args.list = True + with redirect_stdout(io.StringIO()) as list_output: + glossary_cmd(self.args, self.config) + self.assertIn('Glossary1', list_output.getvalue()) + self.assertIn('Glossary2', list_output.getvalue())