tests: splitted 'test_main.py' into 3 modules
This commit is contained in:
parent
85375346f4
commit
6a81b9b146
@ -7,8 +7,6 @@ from chatmastermind.main import create_parser, ask_cmd
|
||||
from chatmastermind.api_client import ai
|
||||
from chatmastermind.configuration import Config
|
||||
from chatmastermind.storage import create_chat_hist, save_answers, dump_data
|
||||
from chatmastermind.tags import Tag, TagLine, TagError
|
||||
from chatmastermind.message import source_code, MessageError, Question, Answer
|
||||
from unittest import mock
|
||||
from unittest.mock import patch, MagicMock, Mock, ANY
|
||||
|
||||
@ -233,201 +231,3 @@ class TestCreateParser(CmmTestCase):
|
||||
mock_cmdparser.add_parser.assert_any_call('config', help=ANY, aliases=ANY)
|
||||
mock_cmdparser.add_parser.assert_any_call('print', help=ANY, aliases=ANY)
|
||||
self.assertTrue('.config.yaml' in parser.get_default('config'))
|
||||
|
||||
|
||||
class TestTag(CmmTestCase):
|
||||
def test_valid_tag(self) -> None:
|
||||
tag = Tag('mytag')
|
||||
self.assertEqual(tag, 'mytag')
|
||||
|
||||
def test_invalid_tag(self) -> None:
|
||||
with self.assertRaises(TagError):
|
||||
Tag('tag with space')
|
||||
|
||||
def test_default_separator(self) -> None:
|
||||
self.assertEqual(Tag.default_separator, ' ')
|
||||
|
||||
def test_alternative_separators(self) -> None:
|
||||
self.assertEqual(Tag.alternative_separators, [','])
|
||||
|
||||
|
||||
class TestTagLine(CmmTestCase):
|
||||
def test_valid_tagline(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1 tag2')
|
||||
self.assertEqual(tagline, 'TAGS: tag1 tag2')
|
||||
|
||||
def test_valid_tagline_with_newline(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1\n tag2')
|
||||
self.assertEqual(tagline, 'TAGS: tag1 tag2')
|
||||
|
||||
def test_invalid_tagline(self) -> None:
|
||||
with self.assertRaises(TagError):
|
||||
TagLine('tag1 tag2')
|
||||
|
||||
def test_prefix(self) -> None:
|
||||
self.assertEqual(TagLine.prefix, 'TAGS:')
|
||||
|
||||
def test_from_set(self) -> None:
|
||||
tags = {Tag('tag1'), Tag('tag2')}
|
||||
tagline = TagLine.from_set(tags)
|
||||
self.assertEqual(tagline, 'TAGS: tag1 tag2')
|
||||
|
||||
def test_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1 tag2')
|
||||
tags = tagline.tags()
|
||||
self.assertEqual(tags, {Tag('tag1'), Tag('tag2')})
|
||||
|
||||
def test_tags_with_newline(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1\n tag2')
|
||||
tags = tagline.tags()
|
||||
self.assertEqual(tags, {Tag('tag1'), Tag('tag2')})
|
||||
|
||||
def test_merge(self) -> None:
|
||||
tagline1 = TagLine('TAGS: tag1 tag2')
|
||||
tagline2 = TagLine('TAGS: tag2 tag3')
|
||||
merged_tagline = tagline1.merge({tagline2})
|
||||
self.assertEqual(merged_tagline, 'TAGS: tag1 tag2 tag3')
|
||||
|
||||
def test_delete_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1 tag2 tag3')
|
||||
new_tagline = tagline.delete_tags({Tag('tag1'), Tag('tag3')})
|
||||
self.assertEqual(new_tagline, 'TAGS: tag2')
|
||||
|
||||
def test_add_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1')
|
||||
new_tagline = tagline.add_tags({Tag('tag2'), Tag('tag3')})
|
||||
self.assertEqual(new_tagline, 'TAGS: tag1 tag2 tag3')
|
||||
|
||||
def test_rename_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: old1 old2')
|
||||
new_tagline = tagline.rename_tags({(Tag('old1'), Tag('new1')), (Tag('old2'), Tag('new2'))})
|
||||
self.assertEqual(new_tagline, 'TAGS: new1 new2')
|
||||
|
||||
def test_match_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1 tag2 tag3')
|
||||
|
||||
# Test case 1: Match any tag in 'tags_or'
|
||||
tags_or = {Tag('tag1'), Tag('tag4')}
|
||||
tags_and: set[Tag] = set()
|
||||
tags_not: set[Tag] = set()
|
||||
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 2: Match all tags in 'tags_and'
|
||||
tags_or = set()
|
||||
tags_and = {Tag('tag1'), Tag('tag2'), Tag('tag3')}
|
||||
tags_not = set()
|
||||
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 3: Match any tag in 'tags_or' and match all tags in 'tags_and'
|
||||
tags_or = {Tag('tag1'), Tag('tag4')}
|
||||
tags_and = {Tag('tag1'), Tag('tag2')}
|
||||
tags_not = set()
|
||||
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 4: Match any tag in 'tags_or', match all tags in 'tags_and', and exclude tags in 'tags_not'
|
||||
tags_or = {Tag('tag1'), Tag('tag4')}
|
||||
tags_and = {Tag('tag1'), Tag('tag2')}
|
||||
tags_not = {Tag('tag5')}
|
||||
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 5: No matching tags in 'tags_or'
|
||||
tags_or = {Tag('tag4'), Tag('tag5')}
|
||||
tags_and = set()
|
||||
tags_not = set()
|
||||
self.assertFalse(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 6: Not all tags in 'tags_and' are present
|
||||
tags_or = set()
|
||||
tags_and = {Tag('tag1'), Tag('tag2'), Tag('tag3'), Tag('tag4')}
|
||||
tags_not = set()
|
||||
self.assertFalse(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 7: Some tags in 'tags_not' are present
|
||||
tags_or = {Tag('tag1')}
|
||||
tags_and = set()
|
||||
tags_not = {Tag('tag2')}
|
||||
self.assertFalse(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 8: 'tags_or' and 'tags_and' are None, match all tags
|
||||
tags_not = set()
|
||||
self.assertTrue(tagline.match_tags(None, None, tags_not))
|
||||
|
||||
# Test case 9: 'tags_or' and 'tags_and' are None, match all tags except excluded tags
|
||||
tags_not = {Tag('tag2')}
|
||||
self.assertFalse(tagline.match_tags(None, None, tags_not))
|
||||
|
||||
|
||||
class SourceCodeTestCase(CmmTestCase):
|
||||
def test_source_code_with_include_delims(self) -> None:
|
||||
text = """
|
||||
Some text before the code block
|
||||
```python
|
||||
print("Hello, World!")
|
||||
```
|
||||
Some text after the code block
|
||||
```python
|
||||
x = 10
|
||||
y = 20
|
||||
print(x + y)
|
||||
```
|
||||
"""
|
||||
expected_result = [
|
||||
" ```python\n print(\"Hello, World!\")\n ```\n",
|
||||
" ```python\n x = 10\n y = 20\n print(x + y)\n ```\n"
|
||||
]
|
||||
result = source_code(text, include_delims=True)
|
||||
self.assertEqual(result, expected_result)
|
||||
|
||||
def test_source_code_without_include_delims(self) -> None:
|
||||
text = """
|
||||
Some text before the code block
|
||||
```python
|
||||
print("Hello, World!")
|
||||
```
|
||||
Some text after the code block
|
||||
```python
|
||||
x = 10
|
||||
y = 20
|
||||
print(x + y)
|
||||
```
|
||||
"""
|
||||
expected_result = [
|
||||
" print(\"Hello, World!\")\n",
|
||||
" x = 10\n y = 20\n print(x + y)\n"
|
||||
]
|
||||
result = source_code(text, include_delims=False)
|
||||
self.assertEqual(result, expected_result)
|
||||
|
||||
def test_source_code_with_single_code_block(self) -> None:
|
||||
text = "```python\nprint(\"Hello, World!\")\n```"
|
||||
expected_result = ["```python\nprint(\"Hello, World!\")\n```\n"]
|
||||
result = source_code(text, include_delims=True)
|
||||
self.assertEqual(result, expected_result)
|
||||
|
||||
def test_source_code_with_no_code_blocks(self) -> None:
|
||||
text = "Some text without any code blocks"
|
||||
expected_result: list[str] = []
|
||||
result = source_code(text, include_delims=True)
|
||||
self.assertEqual(result, expected_result)
|
||||
|
||||
|
||||
class QuestionTestCase(CmmTestCase):
|
||||
def test_question_with_prefix(self) -> None:
|
||||
with self.assertRaises(MessageError):
|
||||
Question("=== QUESTION === What is your name?")
|
||||
|
||||
def test_question_without_prefix(self) -> None:
|
||||
question = Question("What is your favorite color?")
|
||||
self.assertIsInstance(question, Question)
|
||||
self.assertEqual(question, "What is your favorite color?")
|
||||
|
||||
|
||||
class AnswerTestCase(CmmTestCase):
|
||||
def test_answer_with_prefix(self) -> None:
|
||||
with self.assertRaises(MessageError):
|
||||
Answer("=== ANSWER === Yes")
|
||||
|
||||
def test_answer_without_prefix(self) -> None:
|
||||
answer = Answer("No")
|
||||
self.assertIsInstance(answer, Answer)
|
||||
self.assertEqual(answer, "No")
|
||||
|
||||
78
tests/test_message.py
Normal file
78
tests/test_message.py
Normal file
@ -0,0 +1,78 @@
|
||||
from .test_main import CmmTestCase
|
||||
from chatmastermind.message import source_code, MessageError, Question, Answer
|
||||
|
||||
|
||||
class SourceCodeTestCase(CmmTestCase):
|
||||
def test_source_code_with_include_delims(self) -> None:
|
||||
text = """
|
||||
Some text before the code block
|
||||
```python
|
||||
print("Hello, World!")
|
||||
```
|
||||
Some text after the code block
|
||||
```python
|
||||
x = 10
|
||||
y = 20
|
||||
print(x + y)
|
||||
```
|
||||
"""
|
||||
expected_result = [
|
||||
" ```python\n print(\"Hello, World!\")\n ```\n",
|
||||
" ```python\n x = 10\n y = 20\n print(x + y)\n ```\n"
|
||||
]
|
||||
result = source_code(text, include_delims=True)
|
||||
self.assertEqual(result, expected_result)
|
||||
|
||||
def test_source_code_without_include_delims(self) -> None:
|
||||
text = """
|
||||
Some text before the code block
|
||||
```python
|
||||
print("Hello, World!")
|
||||
```
|
||||
Some text after the code block
|
||||
```python
|
||||
x = 10
|
||||
y = 20
|
||||
print(x + y)
|
||||
```
|
||||
"""
|
||||
expected_result = [
|
||||
" print(\"Hello, World!\")\n",
|
||||
" x = 10\n y = 20\n print(x + y)\n"
|
||||
]
|
||||
result = source_code(text, include_delims=False)
|
||||
self.assertEqual(result, expected_result)
|
||||
|
||||
def test_source_code_with_single_code_block(self) -> None:
|
||||
text = "```python\nprint(\"Hello, World!\")\n```"
|
||||
expected_result = ["```python\nprint(\"Hello, World!\")\n```\n"]
|
||||
result = source_code(text, include_delims=True)
|
||||
self.assertEqual(result, expected_result)
|
||||
|
||||
def test_source_code_with_no_code_blocks(self) -> None:
|
||||
text = "Some text without any code blocks"
|
||||
expected_result: list[str] = []
|
||||
result = source_code(text, include_delims=True)
|
||||
self.assertEqual(result, expected_result)
|
||||
|
||||
|
||||
class QuestionTestCase(CmmTestCase):
|
||||
def test_question_with_prefix(self) -> None:
|
||||
with self.assertRaises(MessageError):
|
||||
Question("=== QUESTION === What is your name?")
|
||||
|
||||
def test_question_without_prefix(self) -> None:
|
||||
question = Question("What is your favorite color?")
|
||||
self.assertIsInstance(question, Question)
|
||||
self.assertEqual(question, "What is your favorite color?")
|
||||
|
||||
|
||||
class AnswerTestCase(CmmTestCase):
|
||||
def test_answer_with_prefix(self) -> None:
|
||||
with self.assertRaises(MessageError):
|
||||
Answer("=== ANSWER === Yes")
|
||||
|
||||
def test_answer_without_prefix(self) -> None:
|
||||
answer = Answer("No")
|
||||
self.assertIsInstance(answer, Answer)
|
||||
self.assertEqual(answer, "No")
|
||||
124
tests/test_tags.py
Normal file
124
tests/test_tags.py
Normal file
@ -0,0 +1,124 @@
|
||||
from .test_main import CmmTestCase
|
||||
from chatmastermind.tags import Tag, TagLine, TagError
|
||||
|
||||
|
||||
class TestTag(CmmTestCase):
|
||||
def test_valid_tag(self) -> None:
|
||||
tag = Tag('mytag')
|
||||
self.assertEqual(tag, 'mytag')
|
||||
|
||||
def test_invalid_tag(self) -> None:
|
||||
with self.assertRaises(TagError):
|
||||
Tag('tag with space')
|
||||
|
||||
def test_default_separator(self) -> None:
|
||||
self.assertEqual(Tag.default_separator, ' ')
|
||||
|
||||
def test_alternative_separators(self) -> None:
|
||||
self.assertEqual(Tag.alternative_separators, [','])
|
||||
|
||||
|
||||
class TestTagLine(CmmTestCase):
|
||||
def test_valid_tagline(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1 tag2')
|
||||
self.assertEqual(tagline, 'TAGS: tag1 tag2')
|
||||
|
||||
def test_valid_tagline_with_newline(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1\n tag2')
|
||||
self.assertEqual(tagline, 'TAGS: tag1 tag2')
|
||||
|
||||
def test_invalid_tagline(self) -> None:
|
||||
with self.assertRaises(TagError):
|
||||
TagLine('tag1 tag2')
|
||||
|
||||
def test_prefix(self) -> None:
|
||||
self.assertEqual(TagLine.prefix, 'TAGS:')
|
||||
|
||||
def test_from_set(self) -> None:
|
||||
tags = {Tag('tag1'), Tag('tag2')}
|
||||
tagline = TagLine.from_set(tags)
|
||||
self.assertEqual(tagline, 'TAGS: tag1 tag2')
|
||||
|
||||
def test_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1 tag2')
|
||||
tags = tagline.tags()
|
||||
self.assertEqual(tags, {Tag('tag1'), Tag('tag2')})
|
||||
|
||||
def test_tags_with_newline(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1\n tag2')
|
||||
tags = tagline.tags()
|
||||
self.assertEqual(tags, {Tag('tag1'), Tag('tag2')})
|
||||
|
||||
def test_merge(self) -> None:
|
||||
tagline1 = TagLine('TAGS: tag1 tag2')
|
||||
tagline2 = TagLine('TAGS: tag2 tag3')
|
||||
merged_tagline = tagline1.merge({tagline2})
|
||||
self.assertEqual(merged_tagline, 'TAGS: tag1 tag2 tag3')
|
||||
|
||||
def test_delete_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1 tag2 tag3')
|
||||
new_tagline = tagline.delete_tags({Tag('tag1'), Tag('tag3')})
|
||||
self.assertEqual(new_tagline, 'TAGS: tag2')
|
||||
|
||||
def test_add_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1')
|
||||
new_tagline = tagline.add_tags({Tag('tag2'), Tag('tag3')})
|
||||
self.assertEqual(new_tagline, 'TAGS: tag1 tag2 tag3')
|
||||
|
||||
def test_rename_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: old1 old2')
|
||||
new_tagline = tagline.rename_tags({(Tag('old1'), Tag('new1')), (Tag('old2'), Tag('new2'))})
|
||||
self.assertEqual(new_tagline, 'TAGS: new1 new2')
|
||||
|
||||
def test_match_tags(self) -> None:
|
||||
tagline = TagLine('TAGS: tag1 tag2 tag3')
|
||||
|
||||
# Test case 1: Match any tag in 'tags_or'
|
||||
tags_or = {Tag('tag1'), Tag('tag4')}
|
||||
tags_and: set[Tag] = set()
|
||||
tags_not: set[Tag] = set()
|
||||
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 2: Match all tags in 'tags_and'
|
||||
tags_or = set()
|
||||
tags_and = {Tag('tag1'), Tag('tag2'), Tag('tag3')}
|
||||
tags_not = set()
|
||||
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 3: Match any tag in 'tags_or' and match all tags in 'tags_and'
|
||||
tags_or = {Tag('tag1'), Tag('tag4')}
|
||||
tags_and = {Tag('tag1'), Tag('tag2')}
|
||||
tags_not = set()
|
||||
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 4: Match any tag in 'tags_or', match all tags in 'tags_and', and exclude tags in 'tags_not'
|
||||
tags_or = {Tag('tag1'), Tag('tag4')}
|
||||
tags_and = {Tag('tag1'), Tag('tag2')}
|
||||
tags_not = {Tag('tag5')}
|
||||
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 5: No matching tags in 'tags_or'
|
||||
tags_or = {Tag('tag4'), Tag('tag5')}
|
||||
tags_and = set()
|
||||
tags_not = set()
|
||||
self.assertFalse(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 6: Not all tags in 'tags_and' are present
|
||||
tags_or = set()
|
||||
tags_and = {Tag('tag1'), Tag('tag2'), Tag('tag3'), Tag('tag4')}
|
||||
tags_not = set()
|
||||
self.assertFalse(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 7: Some tags in 'tags_not' are present
|
||||
tags_or = {Tag('tag1')}
|
||||
tags_and = set()
|
||||
tags_not = {Tag('tag2')}
|
||||
self.assertFalse(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||
|
||||
# Test case 8: 'tags_or' and 'tags_and' are None, match all tags
|
||||
tags_not = set()
|
||||
self.assertTrue(tagline.match_tags(None, None, tags_not))
|
||||
|
||||
# Test case 9: 'tags_or' and 'tags_and' are None, match all tags except excluded tags
|
||||
tags_not = {Tag('tag2')}
|
||||
self.assertFalse(tagline.match_tags(None, None, tags_not))
|
||||
Loading…
x
Reference in New Issue
Block a user