question cmd: added test module
This commit is contained in:
parent
76f2373397
commit
4c843d95d3
47
tests/test_question_cmd.py
Normal file
47
tests/test_question_cmd.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import unittest
|
||||||
|
import argparse
|
||||||
|
import tempfile
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
from chatmastermind.commands.question import create_message
|
||||||
|
from chatmastermind.message import Message, Question
|
||||||
|
from chatmastermind.chat import ChatDB
|
||||||
|
|
||||||
|
|
||||||
|
class TestMessageCreate(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Test if messages created by the 'question' command have
|
||||||
|
the correct format.
|
||||||
|
"""
|
||||||
|
def setUp(self) -> None:
|
||||||
|
self.db_path = tempfile.TemporaryDirectory()
|
||||||
|
self.cache_path = tempfile.TemporaryDirectory()
|
||||||
|
self.chat = ChatDB.from_dir(cache_path=Path(self.cache_path.name),
|
||||||
|
db_path=Path(self.db_path.name))
|
||||||
|
# create arguments mock
|
||||||
|
self.args = MagicMock(spec=argparse.Namespace)
|
||||||
|
self.args.ai = None
|
||||||
|
self.args.model = None
|
||||||
|
self.args.output_tags = None
|
||||||
|
|
||||||
|
def message_list(self, tmp_dir: tempfile.TemporaryDirectory) -> list[Path]:
|
||||||
|
# exclude '.next'
|
||||||
|
return list(Path(tmp_dir.name).glob('*.[ty]*'))
|
||||||
|
|
||||||
|
def test_message_file_created(self) -> None:
|
||||||
|
self.args.question = "What is this?"
|
||||||
|
cache_dir_files = self.message_list(self.cache_path)
|
||||||
|
self.assertEqual(len(cache_dir_files), 0)
|
||||||
|
create_message(self.chat, self.args)
|
||||||
|
cache_dir_files = self.message_list(self.cache_path)
|
||||||
|
self.assertEqual(len(cache_dir_files), 1)
|
||||||
|
message = Message.from_file(cache_dir_files[0])
|
||||||
|
self.assertIsInstance(message, Message)
|
||||||
|
self.assertEqual(message.question, Question("What is this?")) # type: ignore [union-attr]
|
||||||
|
|
||||||
|
def test_single_question(self) -> None:
|
||||||
|
self.args.question = "What is this?"
|
||||||
|
message = create_message(self.chat, self.args)
|
||||||
|
self.assertIsInstance(message, Message)
|
||||||
|
self.assertEqual(message.question, Question("What is this?"))
|
||||||
|
self.assertEqual(len(message.question.source_code()), 0)
|
||||||
Loading…
x
Reference in New Issue
Block a user