Change type msg_location to an Enum instead of Literal to be able to get all values easy and improve type checks.
This commit is contained in:
+47
-47
@@ -7,7 +7,7 @@ from io import StringIO
|
||||
from unittest.mock import patch
|
||||
from chatmastermind.tags import TagLine
|
||||
from chatmastermind.message import Message, Question, Answer, Tag, MessageFilter
|
||||
from chatmastermind.chat import Chat, ChatDB, ChatError
|
||||
from chatmastermind.chat import Chat, ChatDB, ChatError, msg_location
|
||||
|
||||
|
||||
msg_suffix: str = Message.file_suffix_write
|
||||
@@ -595,92 +595,92 @@ class TestChatDB(TestChatBase):
|
||||
chat_db = ChatDB.from_dir(pathlib.Path(self.cache_path.name),
|
||||
pathlib.Path(self.db_path.name))
|
||||
# search for a DB file in memory
|
||||
self.assertEqual(chat_db.msg_find([str(self.message1.file_path)], loc='mem'), [self.message1])
|
||||
self.assertEqual(chat_db.msg_find([self.message1.msg_id()], loc='mem'), [self.message1])
|
||||
self.assertEqual(chat_db.msg_find(['0001.msg'], loc='mem'), [self.message1])
|
||||
self.assertEqual(chat_db.msg_find(['0001'], loc='mem'), [self.message1])
|
||||
self.assertEqual(chat_db.msg_find([str(self.message1.file_path)], loc=msg_location.MEM), [self.message1])
|
||||
self.assertEqual(chat_db.msg_find([self.message1.msg_id()], loc=msg_location.MEM), [self.message1])
|
||||
self.assertEqual(chat_db.msg_find(['0001.msg'], loc=msg_location.MEM), [self.message1])
|
||||
self.assertEqual(chat_db.msg_find(['0001'], loc=msg_location.MEM), [self.message1])
|
||||
# and on disk
|
||||
self.assertEqual(chat_db.msg_find([str(self.message2.file_path)], loc='db'), [self.message2])
|
||||
self.assertEqual(chat_db.msg_find([self.message2.msg_id()], loc='db'), [self.message2])
|
||||
self.assertEqual(chat_db.msg_find(['0002.msg'], loc='db'), [self.message2])
|
||||
self.assertEqual(chat_db.msg_find(['0002'], loc='db'), [self.message2])
|
||||
self.assertEqual(chat_db.msg_find([str(self.message2.file_path)], loc=msg_location.DB), [self.message2])
|
||||
self.assertEqual(chat_db.msg_find([self.message2.msg_id()], loc=msg_location.DB), [self.message2])
|
||||
self.assertEqual(chat_db.msg_find(['0002.msg'], loc=msg_location.DB), [self.message2])
|
||||
self.assertEqual(chat_db.msg_find(['0002'], loc=msg_location.DB), [self.message2])
|
||||
# now search the cache -> expect empty result
|
||||
self.assertEqual(chat_db.msg_find([str(self.message3.file_path)], loc='cache'), [])
|
||||
self.assertEqual(chat_db.msg_find([self.message3.msg_id()], loc='cache'), [])
|
||||
self.assertEqual(chat_db.msg_find(['0003.msg'], loc='cache'), [])
|
||||
self.assertEqual(chat_db.msg_find(['0003'], loc='cache'), [])
|
||||
self.assertEqual(chat_db.msg_find([str(self.message3.file_path)], loc=msg_location.CACHE), [])
|
||||
self.assertEqual(chat_db.msg_find([self.message3.msg_id()], loc=msg_location.CACHE), [])
|
||||
self.assertEqual(chat_db.msg_find(['0003.msg'], loc=msg_location.CACHE), [])
|
||||
self.assertEqual(chat_db.msg_find(['0003'], loc=msg_location.CACHE), [])
|
||||
# search for multiple messages
|
||||
# -> search one twice, expect result to be unique
|
||||
search_names = ['0001', '0002.msg', self.message3.msg_id(), str(self.message3.file_path)]
|
||||
expected_result = [self.message1, self.message2, self.message3]
|
||||
result = chat_db.msg_find(search_names, loc='all')
|
||||
result = chat_db.msg_find(search_names, loc=msg_location.ALL)
|
||||
self.assert_messages_equal(result, expected_result)
|
||||
|
||||
def test_msg_latest(self) -> None:
|
||||
chat_db = ChatDB.from_dir(pathlib.Path(self.cache_path.name),
|
||||
pathlib.Path(self.db_path.name))
|
||||
self.assertEqual(chat_db.msg_latest(loc='mem'), self.message4)
|
||||
self.assertEqual(chat_db.msg_latest(loc='db'), self.message4)
|
||||
self.assertEqual(chat_db.msg_latest(loc='disk'), self.message4)
|
||||
self.assertEqual(chat_db.msg_latest(loc='all'), self.message4)
|
||||
self.assertEqual(chat_db.msg_latest(loc=msg_location.MEM), self.message4)
|
||||
self.assertEqual(chat_db.msg_latest(loc=msg_location.DB), self.message4)
|
||||
self.assertEqual(chat_db.msg_latest(loc=msg_location.DISK), self.message4)
|
||||
self.assertEqual(chat_db.msg_latest(loc=msg_location.ALL), self.message4)
|
||||
# the cache is currently empty:
|
||||
self.assertIsNone(chat_db.msg_latest(loc='cache'))
|
||||
self.assertIsNone(chat_db.msg_latest(loc=msg_location.CACHE))
|
||||
# add new messages to the cache dir
|
||||
new_message = Message(question=Question("New Question"),
|
||||
answer=Answer("New Answer"))
|
||||
chat_db.cache_add([new_message])
|
||||
self.assertEqual(chat_db.msg_latest(loc='cache'), new_message)
|
||||
self.assertEqual(chat_db.msg_latest(loc='mem'), new_message)
|
||||
self.assertEqual(chat_db.msg_latest(loc='disk'), new_message)
|
||||
self.assertEqual(chat_db.msg_latest(loc='all'), new_message)
|
||||
self.assertEqual(chat_db.msg_latest(loc=msg_location.CACHE), new_message)
|
||||
self.assertEqual(chat_db.msg_latest(loc=msg_location.MEM), new_message)
|
||||
self.assertEqual(chat_db.msg_latest(loc=msg_location.DISK), new_message)
|
||||
self.assertEqual(chat_db.msg_latest(loc=msg_location.ALL), new_message)
|
||||
# the DB does not contain the new message
|
||||
self.assertEqual(chat_db.msg_latest(loc='db'), self.message4)
|
||||
self.assertEqual(chat_db.msg_latest(loc=msg_location.DB), self.message4)
|
||||
|
||||
def test_msg_gather(self) -> None:
|
||||
chat_db = ChatDB.from_dir(pathlib.Path(self.cache_path.name),
|
||||
pathlib.Path(self.db_path.name))
|
||||
all_messages = [self.message1, self.message2, self.message3, self.message4]
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='all'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='db'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='mem'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='disk'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='cache'), [])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.ALL), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.DB), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.MEM), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.DISK), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.CACHE), [])
|
||||
# add a new message, but only to the internal list
|
||||
new_message = Message(Question("What?"))
|
||||
all_messages_mem = all_messages + [new_message]
|
||||
chat_db.msg_add([new_message])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='mem'), all_messages_mem)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='all'), all_messages_mem)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.MEM), all_messages_mem)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.ALL), all_messages_mem)
|
||||
# the nr. of messages on disk did not change -> expect old result
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='db'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='disk'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='cache'), [])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.DB), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.DISK), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.CACHE), [])
|
||||
# test with MessageFilter
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='all', mfilter=MessageFilter(tags_or={Tag('tag1')})),
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.ALL, mfilter=MessageFilter(tags_or={Tag('tag1')})),
|
||||
[self.message1])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='disk', mfilter=MessageFilter(tags_or={Tag('tag2')})),
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.DISK, mfilter=MessageFilter(tags_or={Tag('tag2')})),
|
||||
[self.message2])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='cache', mfilter=MessageFilter(tags_or={Tag('tag3')})),
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.CACHE, mfilter=MessageFilter(tags_or={Tag('tag3')})),
|
||||
[])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='mem', mfilter=MessageFilter(question_contains="What")),
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.MEM, mfilter=MessageFilter(question_contains="What")),
|
||||
[new_message])
|
||||
|
||||
def test_msg_move_and_gather(self) -> None:
|
||||
chat_db = ChatDB.from_dir(pathlib.Path(self.cache_path.name),
|
||||
pathlib.Path(self.db_path.name))
|
||||
all_messages = [self.message1, self.message2, self.message3, self.message4]
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='db'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='cache'), [])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.DB), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.CACHE), [])
|
||||
# move first message to the cache
|
||||
chat_db.cache_move(self.message1)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='cache'), [self.message1])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.CACHE), [self.message1])
|
||||
self.assertEqual(self.message1.file_path.parent, pathlib.Path(self.cache_path.name)) # type: ignore [union-attr]
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='db'), [self.message2, self.message3, self.message4])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='all'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='disk'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='mem'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.DB), [self.message2, self.message3, self.message4])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.ALL), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.DISK), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.MEM), all_messages)
|
||||
# now move first message back to the DB
|
||||
chat_db.db_move(self.message1)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='cache'), [])
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.CACHE), [])
|
||||
self.assertEqual(self.message1.file_path.parent, pathlib.Path(self.db_path.name)) # type: ignore [union-attr]
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc='db'), all_messages)
|
||||
self.assert_messages_equal(chat_db.msg_gather(loc=msg_location.DB), all_messages)
|
||||
|
||||
@@ -4,7 +4,7 @@ import tempfile
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
from chatmastermind.message import Message, Question
|
||||
from chatmastermind.chat import ChatDB, ChatError
|
||||
from chatmastermind.chat import ChatDB, ChatError, msg_location
|
||||
from chatmastermind.configuration import Config
|
||||
from chatmastermind.commands.hist import convert_messages
|
||||
|
||||
@@ -41,7 +41,7 @@ class TestConvertMessages(unittest.TestCase):
|
||||
def test_convert_messages(self) -> None:
|
||||
self.args.convert = 'yaml'
|
||||
convert_messages(self.args, self.config)
|
||||
msgs = self.chat.msg_gather(loc='disk', glob='*.*')
|
||||
msgs = self.chat.msg_gather(loc=msg_location.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
|
||||
|
||||
+14
-14
@@ -9,7 +9,7 @@ from chatmastermind.configuration import Config
|
||||
from chatmastermind.commands.question import create_message, question_cmd
|
||||
from chatmastermind.tags import Tag
|
||||
from chatmastermind.message import Message, Question, Answer
|
||||
from chatmastermind.chat import Chat, ChatDB
|
||||
from chatmastermind.chat import Chat, ChatDB, msg_location
|
||||
from chatmastermind.ai import AIError
|
||||
from .test_common import TestWithFakeAI
|
||||
|
||||
@@ -279,7 +279,7 @@ class TestQuestionCmdAsk(TestQuestionCmd):
|
||||
# check for the expected message files
|
||||
chat = ChatDB.from_dir(Path(self.cache_dir.name),
|
||||
Path(self.db_dir.name))
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
self.assertEqual(len(self.message_list(self.cache_dir)), 1)
|
||||
self.assert_msgs_equal_except_file_path(cached_msg, expected_responses)
|
||||
|
||||
@@ -337,7 +337,7 @@ class TestQuestionCmdAsk(TestQuestionCmd):
|
||||
# check for the expected message files
|
||||
chat = ChatDB.from_dir(Path(self.cache_dir.name),
|
||||
Path(self.db_dir.name))
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
self.assertEqual(len(self.message_list(self.cache_dir)), 1)
|
||||
self.assert_msgs_equal_except_file_path(cached_msg, [expected_question])
|
||||
|
||||
@@ -375,7 +375,7 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
# we expect the original message + the one with the new response
|
||||
expected_responses = [message] + [expected_response]
|
||||
question_cmd(self.args, self.config)
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
print(self.message_list(self.cache_dir))
|
||||
self.assertEqual(len(self.message_list(self.cache_dir)), 2)
|
||||
self.assert_msgs_equal_except_file_path(cached_msg, expected_responses)
|
||||
@@ -396,7 +396,7 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
model=self.args.model,
|
||||
file_path=Path(self.cache_dir.name) / f'0001{msg_suffix}')
|
||||
chat.msg_write([message])
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
assert cached_msg[0].file_path
|
||||
cached_msg_file_id = cached_msg[0].file_path.stem
|
||||
|
||||
@@ -412,7 +412,7 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
tags=message.tags,
|
||||
file_path=Path('<NOT COMPARED>'))
|
||||
question_cmd(self.args, self.config)
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
self.assertEqual(len(self.message_list(self.cache_dir)), 1)
|
||||
self.assert_msgs_equal_except_file_path(cached_msg, [expected_response])
|
||||
# also check that the file ID has not been changed
|
||||
@@ -435,7 +435,7 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
model=self.args.model,
|
||||
file_path=Path(self.cache_dir.name) / f'0001{msg_suffix}')
|
||||
chat.msg_write([message])
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
assert cached_msg[0].file_path
|
||||
cached_msg_file_id = cached_msg[0].file_path.stem
|
||||
|
||||
@@ -452,7 +452,7 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
tags=message.tags,
|
||||
file_path=Path('<NOT COMPARED>'))
|
||||
question_cmd(self.args, self.config)
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
self.assertEqual(len(self.message_list(self.cache_dir)), 1)
|
||||
self.assert_msgs_equal_except_file_path(cached_msg, [expected_response])
|
||||
# also check that the file ID has not been changed
|
||||
@@ -475,7 +475,7 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
model=self.args.model,
|
||||
file_path=Path(self.cache_dir.name) / f'0001{msg_suffix}')
|
||||
chat.msg_write([message])
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
assert cached_msg[0].file_path
|
||||
|
||||
# repeat the last question with new arguments (without overwriting)
|
||||
@@ -493,7 +493,7 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
tags={Tag('newtag')},
|
||||
file_path=Path('<NOT COMPARED>'))
|
||||
question_cmd(self.args, self.config)
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
self.assertEqual(len(self.message_list(self.cache_dir)), 2)
|
||||
self.assert_msgs_equal_except_file_path(cached_msg, [message] + [new_expected_response])
|
||||
|
||||
@@ -513,7 +513,7 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
model=self.args.model,
|
||||
file_path=Path(self.cache_dir.name) / f'0001{msg_suffix}')
|
||||
chat.msg_write([message])
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
assert cached_msg[0].file_path
|
||||
|
||||
# repeat the last question with new arguments
|
||||
@@ -530,7 +530,7 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
tags={Tag('newtag')},
|
||||
file_path=Path('<NOT COMPARED>'))
|
||||
question_cmd(self.args, self.config)
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
self.assertEqual(len(self.message_list(self.cache_dir)), 1)
|
||||
self.assert_msgs_equal_except_file_path(cached_msg, [new_expected_response])
|
||||
|
||||
@@ -586,8 +586,8 @@ class TestQuestionCmdRepeat(TestQuestionCmd):
|
||||
self.assertEqual(len(self.message_list(self.cache_dir)), 4)
|
||||
self.assertEqual(len(self.message_list(self.db_dir)), 1)
|
||||
expected_cache_messages = [expected_responses[0], message2, expected_responses[1], expected_responses[2]]
|
||||
cached_msg = chat.msg_gather(loc='cache')
|
||||
cached_msg = chat.msg_gather(loc=msg_location.CACHE)
|
||||
self.assert_msgs_equal_except_file_path(cached_msg, expected_cache_messages)
|
||||
# check that the DB message has not been modified at all
|
||||
db_msg = chat.msg_gather(loc='db')
|
||||
db_msg = chat.msg_gather(loc=msg_location.DB)
|
||||
self.assert_msgs_all_equal(db_msg, [message3])
|
||||
|
||||
Reference in New Issue
Block a user