test_message: changed all tests to use the new '.msg' suffix
This commit is contained in:
parent
1743802262
commit
11d50ae551
@ -1,11 +1,16 @@
|
||||
import unittest
|
||||
import pathlib
|
||||
import tempfile
|
||||
import itertools
|
||||
from typing import cast
|
||||
from chatmastermind.message import source_code, Message, MessageError, Question, Answer, AILine, ModelLine, MessageFilter, message_in
|
||||
from chatmastermind.message import source_code, Message, MessageError, Question, Answer, AILine, ModelLine,\
|
||||
MessageFilter, message_in, message_valid_formats
|
||||
from chatmastermind.tags import Tag, TagLine
|
||||
|
||||
|
||||
msg_suffix: str = Message.file_suffix_write
|
||||
|
||||
|
||||
class SourceCodeTestCase(unittest.TestCase):
|
||||
def test_source_code_with_include_delims(self) -> None:
|
||||
text = """
|
||||
@ -101,7 +106,7 @@ class AnswerTestCase(unittest.TestCase):
|
||||
|
||||
class MessageToFileTxtTestCase(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path = pathlib.Path(self.file.name)
|
||||
self.message_complete = Message(Question('This is a question.'),
|
||||
Answer('This is an answer.'),
|
||||
@ -117,7 +122,7 @@ class MessageToFileTxtTestCase(unittest.TestCase):
|
||||
self.file_path.unlink()
|
||||
|
||||
def test_to_file_txt_complete(self) -> None:
|
||||
self.message_complete.to_file(self.file_path)
|
||||
self.message_complete.to_file(self.file_path, mformat='txt')
|
||||
|
||||
with open(self.file_path, "r") as fd:
|
||||
content = fd.read()
|
||||
@ -132,7 +137,7 @@ This is an answer.
|
||||
self.assertEqual(content, expected_content)
|
||||
|
||||
def test_to_file_txt_min(self) -> None:
|
||||
self.message_min.to_file(self.file_path)
|
||||
self.message_min.to_file(self.file_path, mformat='txt')
|
||||
|
||||
with open(self.file_path, "r") as fd:
|
||||
content = fd.read()
|
||||
@ -141,11 +146,17 @@ This is a question.
|
||||
"""
|
||||
self.assertEqual(content, expected_content)
|
||||
|
||||
def test_to_file_unsupported_file_type(self) -> None:
|
||||
def test_to_file_unsupported_file_suffix(self) -> None:
|
||||
unsupported_file_path = pathlib.Path("example.doc")
|
||||
with self.assertRaises(MessageError) as cm:
|
||||
self.message_complete.to_file(unsupported_file_path)
|
||||
self.assertEqual(str(cm.exception), "File type '.doc' is not supported")
|
||||
self.assertEqual(str(cm.exception), "File suffix '.doc' is not supported")
|
||||
|
||||
def test_to_file_unsupported_file_format(self) -> None:
|
||||
unsupported_file_format = pathlib.Path(f"example{msg_suffix}")
|
||||
with self.assertRaises(MessageError) as cm:
|
||||
self.message_complete.to_file(unsupported_file_format, mformat='doc') # type: ignore [arg-type]
|
||||
self.assertEqual(str(cm.exception), "File format 'doc' is not supported")
|
||||
|
||||
def test_to_file_no_file_path(self) -> None:
|
||||
"""
|
||||
@ -159,10 +170,24 @@ This is a question.
|
||||
# reset the internal file_path
|
||||
self.message_complete.file_path = self.file_path
|
||||
|
||||
def test_to_file_txt_auto_suffix(self) -> None:
|
||||
"""
|
||||
Test if suffix is auto-generated if omitted.
|
||||
"""
|
||||
file_path_no_suffix = self.file_path.with_suffix('')
|
||||
# test with file_path member
|
||||
self.message_min.file_path = file_path_no_suffix
|
||||
self.message_min.to_file(mformat='txt')
|
||||
self.assertEqual(self.message_min.file_path.suffix, msg_suffix)
|
||||
# test with explicit file_path
|
||||
self.message_min.file_path = file_path_no_suffix
|
||||
self.message_min.to_file(file_path=file_path_no_suffix, mformat='txt')
|
||||
self.assertEqual(self.message_min.file_path.suffix, msg_suffix)
|
||||
|
||||
|
||||
class MessageToFileYamlTestCase(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix='.yaml')
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path = pathlib.Path(self.file.name)
|
||||
self.message_complete = Message(Question('This is a question.'),
|
||||
Answer('This is an answer.'),
|
||||
@ -184,7 +209,7 @@ class MessageToFileYamlTestCase(unittest.TestCase):
|
||||
self.file_path.unlink()
|
||||
|
||||
def test_to_file_yaml_complete(self) -> None:
|
||||
self.message_complete.to_file(self.file_path)
|
||||
self.message_complete.to_file(self.file_path, mformat='yaml')
|
||||
|
||||
with open(self.file_path, "r") as fd:
|
||||
content = fd.read()
|
||||
@ -199,7 +224,7 @@ class MessageToFileYamlTestCase(unittest.TestCase):
|
||||
self.assertEqual(content, expected_content)
|
||||
|
||||
def test_to_file_yaml_multiline(self) -> None:
|
||||
self.message_multiline.to_file(self.file_path)
|
||||
self.message_multiline.to_file(self.file_path, mformat='yaml')
|
||||
|
||||
with open(self.file_path, "r") as fd:
|
||||
content = fd.read()
|
||||
@ -218,17 +243,31 @@ class MessageToFileYamlTestCase(unittest.TestCase):
|
||||
self.assertEqual(content, expected_content)
|
||||
|
||||
def test_to_file_yaml_min(self) -> None:
|
||||
self.message_min.to_file(self.file_path)
|
||||
self.message_min.to_file(self.file_path, mformat='yaml')
|
||||
|
||||
with open(self.file_path, "r") as fd:
|
||||
content = fd.read()
|
||||
expected_content = f"{Question.yaml_key}: This is a question.\n"
|
||||
self.assertEqual(content, expected_content)
|
||||
|
||||
def test_to_file_yaml_auto_suffix(self) -> None:
|
||||
"""
|
||||
Test if suffix is auto-generated if omitted.
|
||||
"""
|
||||
file_path_no_suffix = self.file_path.with_suffix('')
|
||||
# test with file_path member
|
||||
self.message_min.file_path = file_path_no_suffix
|
||||
self.message_min.to_file(mformat='yaml')
|
||||
self.assertEqual(self.message_min.file_path.suffix, msg_suffix)
|
||||
# test with explicit file_path
|
||||
self.message_min.file_path = file_path_no_suffix
|
||||
self.message_min.to_file(file_path=file_path_no_suffix, mformat='yaml')
|
||||
self.assertEqual(self.message_min.file_path.suffix, msg_suffix)
|
||||
|
||||
|
||||
class MessageFromFileTxtTestCase(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path = pathlib.Path(self.file.name)
|
||||
with open(self.file_path, "w") as fd:
|
||||
fd.write(f"""{TagLine.prefix} tag1 tag2
|
||||
@ -239,7 +278,7 @@ This is a question.
|
||||
{Answer.txt_header}
|
||||
This is an answer.
|
||||
""")
|
||||
self.file_min = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
||||
self.file_min = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path_min = pathlib.Path(self.file_min.name)
|
||||
with open(self.file_path_min, "w") as fd:
|
||||
fd.write(f"""{Question.txt_header}
|
||||
@ -259,7 +298,7 @@ This is a question.
|
||||
message = Message.from_file(self.file_path)
|
||||
self.assertIsNotNone(message)
|
||||
self.assertIsInstance(message, Message)
|
||||
if message: # mypy bug
|
||||
assert 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')})
|
||||
@ -274,7 +313,7 @@ This is a question.
|
||||
message = Message.from_file(self.file_path_min)
|
||||
self.assertIsNotNone(message)
|
||||
self.assertIsInstance(message, Message)
|
||||
if message: # mypy bug
|
||||
assert message
|
||||
self.assertEqual(message.question, 'This is a question.')
|
||||
self.assertEqual(message.file_path, self.file_path_min)
|
||||
self.assertIsNone(message.answer)
|
||||
@ -284,7 +323,7 @@ This is a question.
|
||||
MessageFilter(tags_or={Tag('tag1')}))
|
||||
self.assertIsNotNone(message)
|
||||
self.assertIsInstance(message, Message)
|
||||
if message: # mypy bug
|
||||
assert 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')})
|
||||
@ -311,13 +350,13 @@ This is a question.
|
||||
MessageFilter(tags_not={Tag('tag1')}))
|
||||
self.assertIsNotNone(message)
|
||||
self.assertIsInstance(message, Message)
|
||||
if message: # mypy bug
|
||||
assert message
|
||||
self.assertEqual(message.question, 'This is a question.')
|
||||
self.assertSetEqual(cast(set[Tag], message.tags), set())
|
||||
self.assertEqual(message.file_path, self.file_path_min)
|
||||
|
||||
def test_from_file_not_exists(self) -> None:
|
||||
file_not_exists = pathlib.Path("example.txt")
|
||||
file_not_exists = pathlib.Path(f"example{msg_suffix}")
|
||||
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")
|
||||
@ -396,7 +435,7 @@ This is a question.
|
||||
|
||||
class MessageFromFileYamlTestCase(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix='.yaml')
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path = pathlib.Path(self.file.name)
|
||||
with open(self.file_path, "w") as fd:
|
||||
fd.write(f"""
|
||||
@ -410,7 +449,7 @@ class MessageFromFileYamlTestCase(unittest.TestCase):
|
||||
- tag1
|
||||
- tag2
|
||||
""")
|
||||
self.file_min = tempfile.NamedTemporaryFile(delete=False, suffix='.yaml')
|
||||
self.file_min = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path_min = pathlib.Path(self.file_min.name)
|
||||
with open(self.file_path_min, "w") as fd:
|
||||
fd.write(f"""
|
||||
@ -431,7 +470,7 @@ class MessageFromFileYamlTestCase(unittest.TestCase):
|
||||
message = Message.from_file(self.file_path)
|
||||
self.assertIsInstance(message, Message)
|
||||
self.assertIsNotNone(message)
|
||||
if message: # mypy bug
|
||||
assert 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')})
|
||||
@ -446,14 +485,14 @@ class MessageFromFileYamlTestCase(unittest.TestCase):
|
||||
message = Message.from_file(self.file_path_min)
|
||||
self.assertIsInstance(message, Message)
|
||||
self.assertIsNotNone(message)
|
||||
if message: # mypy bug
|
||||
assert message
|
||||
self.assertEqual(message.question, 'This is a question.')
|
||||
self.assertSetEqual(cast(set[Tag], message.tags), set())
|
||||
self.assertEqual(message.file_path, self.file_path_min)
|
||||
self.assertIsNone(message.answer)
|
||||
|
||||
def test_from_file_not_exists(self) -> None:
|
||||
file_not_exists = pathlib.Path("example.yaml")
|
||||
file_not_exists = pathlib.Path(f"example{msg_suffix}")
|
||||
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")
|
||||
@ -463,7 +502,7 @@ class MessageFromFileYamlTestCase(unittest.TestCase):
|
||||
MessageFilter(tags_or={Tag('tag1')}))
|
||||
self.assertIsNotNone(message)
|
||||
self.assertIsInstance(message, Message)
|
||||
if message: # mypy bug
|
||||
assert 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')})
|
||||
@ -484,7 +523,7 @@ class MessageFromFileYamlTestCase(unittest.TestCase):
|
||||
MessageFilter(tags_not={Tag('tag1')}))
|
||||
self.assertIsNotNone(message)
|
||||
self.assertIsInstance(message, Message)
|
||||
if message: # mypy bug
|
||||
assert message
|
||||
self.assertEqual(message.question, 'This is a question.')
|
||||
self.assertSetEqual(cast(set[Tag], message.tags), set())
|
||||
self.assertEqual(message.file_path, self.file_path_min)
|
||||
@ -563,7 +602,7 @@ class MessageFromFileYamlTestCase(unittest.TestCase):
|
||||
|
||||
class TagsFromFileTestCase(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.file_txt = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
||||
self.file_txt = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path_txt = pathlib.Path(self.file_txt.name)
|
||||
with open(self.file_path_txt, "w") as fd:
|
||||
fd.write(f"""{TagLine.prefix} tag1 tag2 ptag3
|
||||
@ -572,7 +611,7 @@ This is a question.
|
||||
{Answer.txt_header}
|
||||
This is an answer.
|
||||
""")
|
||||
self.file_txt_no_tags = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
||||
self.file_txt_no_tags = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path_txt_no_tags = pathlib.Path(self.file_txt_no_tags.name)
|
||||
with open(self.file_path_txt_no_tags, "w") as fd:
|
||||
fd.write(f"""{Question.txt_header}
|
||||
@ -580,7 +619,7 @@ This is a question.
|
||||
{Answer.txt_header}
|
||||
This is an answer.
|
||||
""")
|
||||
self.file_txt_tags_empty = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
||||
self.file_txt_tags_empty = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path_txt_tags_empty = pathlib.Path(self.file_txt_tags_empty.name)
|
||||
with open(self.file_path_txt_tags_empty, "w") as fd:
|
||||
fd.write(f"""TAGS:
|
||||
@ -589,7 +628,7 @@ This is a question.
|
||||
{Answer.txt_header}
|
||||
This is an answer.
|
||||
""")
|
||||
self.file_yaml = tempfile.NamedTemporaryFile(delete=False, suffix='.yaml')
|
||||
self.file_yaml = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path_yaml = pathlib.Path(self.file_yaml.name)
|
||||
with open(self.file_path_yaml, "w") as fd:
|
||||
fd.write(f"""
|
||||
@ -602,7 +641,7 @@ This is an answer.
|
||||
- tag2
|
||||
- ptag3
|
||||
""")
|
||||
self.file_yaml_no_tags = tempfile.NamedTemporaryFile(delete=False, suffix='.yaml')
|
||||
self.file_yaml_no_tags = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path_yaml_no_tags = pathlib.Path(self.file_yaml_no_tags.name)
|
||||
with open(self.file_path_yaml_no_tags, "w") as fd:
|
||||
fd.write(f"""
|
||||
@ -679,24 +718,25 @@ class TagsFromDirTestCase(unittest.TestCase):
|
||||
{Tag('ctag5'), Tag('ctag6')}
|
||||
]
|
||||
self.files = [
|
||||
pathlib.Path(self.temp_dir.name, 'file1.txt'),
|
||||
pathlib.Path(self.temp_dir.name, 'file2.yaml'),
|
||||
pathlib.Path(self.temp_dir.name, 'file3.txt')
|
||||
pathlib.Path(self.temp_dir.name, f'file1{msg_suffix}'),
|
||||
pathlib.Path(self.temp_dir.name, f'file2{msg_suffix}'),
|
||||
pathlib.Path(self.temp_dir.name, f'file3{msg_suffix}')
|
||||
]
|
||||
self.files_no_tags = [
|
||||
pathlib.Path(self.temp_dir_no_tags.name, 'file4.txt'),
|
||||
pathlib.Path(self.temp_dir_no_tags.name, 'file5.yaml'),
|
||||
pathlib.Path(self.temp_dir_no_tags.name, 'file6.txt')
|
||||
pathlib.Path(self.temp_dir_no_tags.name, f'file4{msg_suffix}'),
|
||||
pathlib.Path(self.temp_dir_no_tags.name, f'file5{msg_suffix}'),
|
||||
pathlib.Path(self.temp_dir_no_tags.name, f'file6{msg_suffix}')
|
||||
]
|
||||
mformats = itertools.cycle(message_valid_formats)
|
||||
for file, tags in zip(self.files, self.tag_sets):
|
||||
message = Message(Question('This is a question.'),
|
||||
Answer('This is an answer.'),
|
||||
tags)
|
||||
message.to_file(file)
|
||||
message.to_file(file, next(mformats))
|
||||
for file in self.files_no_tags:
|
||||
message = Message(Question('This is a question.'),
|
||||
Answer('This is an answer.'))
|
||||
message.to_file(file)
|
||||
message.to_file(file, next(mformats))
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.temp_dir.cleanup()
|
||||
@ -719,7 +759,7 @@ class TagsFromDirTestCase(unittest.TestCase):
|
||||
|
||||
class MessageIDTestCase(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
||||
self.file = tempfile.NamedTemporaryFile(delete=False, suffix=msg_suffix)
|
||||
self.file_path = pathlib.Path(self.file.name)
|
||||
self.message = Message(Question('This is a question.'),
|
||||
file_path=self.file_path)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user