From 3149b82954adcbdb5e7f0148689106d0e25f50cf Mon Sep 17 00:00:00 2001 From: juk0de Date: Fri, 29 Sep 2023 18:53:02 +0200 Subject: [PATCH] hist_cmd: added module 'test_hist_cmd.py' --- chatmastermind/commands/hist.py | 2 +- tests/test_hist_cmd.py | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/test_hist_cmd.py diff --git a/chatmastermind/commands/hist.py b/chatmastermind/commands/hist.py index 104ae2f..7d8a5a2 100644 --- a/chatmastermind/commands/hist.py +++ b/chatmastermind/commands/hist.py @@ -29,7 +29,7 @@ def convert_messages(args: argparse.Namespace, config: Config) -> None: m.file_path = m.file_path.with_suffix('') chat.msg_write(msgs) # read all messages with the current default suffix - msgs = chat.msg_gather(loc='disk', glob='*{msg_suffix}') + msgs = chat.msg_gather(loc='disk', glob=f'*{msg_suffix}') # make sure we converted all of the original messages for mid in msg_ids: if not any(mid == m.msg_id() for m in msgs): diff --git a/tests/test_hist_cmd.py b/tests/test_hist_cmd.py new file mode 100644 index 0000000..fb0a093 --- /dev/null +++ b/tests/test_hist_cmd.py @@ -0,0 +1,57 @@ +import unittest +import argparse +import tempfile +import yaml +from pathlib import Path +from chatmastermind.message import Message, Question +from chatmastermind.chat import ChatDB +from chatmastermind.configuration import Config +from chatmastermind.commands.hist import convert_messages + + +msg_suffix = Message.file_suffix_write + + +class TestConvertMessages(unittest.TestCase): + def setUp(self) -> None: + self.db_dir = tempfile.TemporaryDirectory() + self.cache_dir = tempfile.TemporaryDirectory() + self.db_path = Path(self.db_dir.name) + self.cache_path = Path(self.cache_dir.name) + self.args = argparse.Namespace() + self.config = Config() + self.config.cache = self.cache_dir.name + self.config.db = self.db_dir.name + self.args.convert = 'yaml' + # Prepare some messages + self.chat = ChatDB.from_dir(Path(self.cache_path), + Path(self.db_path)) + self.messages = [Message(Question(f'Question {i}')) for i in range(0, 6)] + self.chat.db_write(self.messages[0:2]) + self.chat.cache_write(self.messages[2:]) + # Change some of the suffixes + assert self.messages[0].file_path + assert self.messages[1].file_path + self.messages[0].file_path.rename(self.messages[0].file_path.with_suffix('.txt')) + self.messages[1].file_path.rename(self.messages[1].file_path.with_suffix('.yaml')) + + def tearDown(self) -> None: + self.db_dir.cleanup() + self.cache_dir.cleanup() + + def test_convert_messages(self) -> None: + convert_messages(self.args, self.config) + msgs = self.chat.msg_gather(loc='disk', glob='*.*') + # Check if the number of messages is the same as before + self.assertEqual(len(msgs), len(self.messages)) + # Check if all messages have the requested suffix + for msg in msgs: + assert msg.file_path + self.assertEqual(msg.file_path.suffix, msg_suffix) + # Check if the message IDs are correctly maintained + for m_new, m_old in zip(msgs, self.messages): + self.assertEqual(m_new.msg_id(), m_old.msg_id()) + # check if all messages have the new format + for m in msgs: + with open(str(m.file_path), "r") as fd: + yaml.load(fd, Loader=yaml.FullLoader)