Compare commits

..

2 Commits

View File

@ -1,5 +1,5 @@
from unittest import mock
import pathlib
import tempfile
from typing import cast
from .test_main import CmmTestCase
from chatmastermind.message import source_code, Message, MessageError, Question, Answer
@ -82,39 +82,67 @@ class AnswerTestCase(CmmTestCase):
self.assertEqual(answer, "No")
class TestMessage(CmmTestCase):
class MessageToFileTxtTestCase(CmmTestCase):
def setUp(self) -> None:
self.file = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
self.file_path = pathlib.Path(self.file.name)
self.message = Message(Question('This is a question'),
Answer('This is an answer'),
{Tag('tag1'), Tag('tag2')},
self.file_path)
def tearDown(self) -> None:
self.file.close()
self.file_path.unlink()
def test_to_file_txt(self) -> None:
self.message.to_file(self.file_path)
with open(self.file_path, "r") as fd:
content = fd.read()
expected_content = "TAGS: tag1 tag2\n=== QUESTION ===\nThis is a question\n=== ANSWER ===\nThis is an answer\n"
self.assertEqual(content, expected_content)
def test_to_file_unsupported_file_type(self) -> None:
unsupported_file_path = pathlib.Path("example.doc")
with self.assertRaises(MessageError) as cm:
self.message.to_file(unsupported_file_path)
self.assertEqual(str(cm.exception), "File type '.doc' is not supported")
def test_to_file_no_file_path(self) -> None:
"""
Provoke an exception using an empty path.
"""
with self.assertRaises(MessageError) as cm:
# clear the internal file_path
self.message.file_path = None
self.message.to_file(None)
self.assertEqual(str(cm.exception), "Got no valid path to write message")
# reset the internal file_path
self.message.file_path = self.file_path
class MessageFromFileTxtTestCase(CmmTestCase):
def setUp(self) -> None:
self.file = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
self.file_path = pathlib.Path(self.file.name)
with open(self.file_path, "w") as fd:
fd.write("TAGS: tag1 tag2\n=== QUESTION ===\nThis is a question\n=== ANSWER ===\nThis is an answer\n")
def tearDown(self) -> None:
self.file.close()
self.file_path.unlink()
def test_from_file_txt(self) -> None:
file_path = pathlib.Path('message.txt')
with mock.patch('chatmastermind.message.pathlib.Path.exists', return_value=True), \
mock.patch('builtins.open',
mock.mock_open(
read_data="TAGS: tag1 tag2\n=== QUESTION ===\nThis is a question\n=== ANSWER ===\nThis is an answer")):
message = Message.from_file(file_path)
message = Message.from_file(self.file_path)
self.assertIsInstance(message, Message)
self.assertEqual(message.question, 'This is a question')
self.assertEqual(message.answer, 'This is an answer')
self.assertSetEqual(cast(set[Tag], message.tags), {Tag('tag1'), Tag('tag2')})
self.assertEqual(message.file_path, file_path)
self.assertEqual(message.file_path, self.file_path)
def test_from_file_yaml(self) -> None:
# TODO: Create a test yaml file
pass
def test_to_file_txt(self) -> None:
file_path = pathlib.Path('message.txt')
message = Message(Question('This is a question'),
Answer('This is an answer'),
{Tag('tag1'), Tag('tag2')},
file_path)
expected_content = "TAGS: tag1 tag2\n=== QUESTION ===\nThis is a question\n=== ANSWER ===\nThis is an answer\n"
# fake write
with mock.patch("builtins.open", mock.mock_open) as mock_fd:
message.to_file(None)
mock_fd().assert_called_once_with(file_path, 'w')
mock_fd().write.assert_called_once_with(expected_content)
def test_to_file_yaml(self) -> None:
# TODO: Create a test yaml file
pass
def test_from_file_not_exists(self) -> None:
file_not_exists = pathlib.Path("example.doc")
with self.assertRaises(MessageError) as cm:
Message.from_file(file_not_exists)
self.assertEqual(str(cm.exception), f"Message file '{file_not_exists}' does not exist")