Initial commit.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import unittest
|
||||
import json
|
||||
from unittest.mock import Mock, PropertyMock, MagicMock, AsyncMock, patch, mock_open
|
||||
from fjerkroa_bot import FjerkroaBot
|
||||
from discord import User, Message, TextChannel
|
||||
|
||||
|
||||
class TestBotBase(unittest.IsolatedAsyncioTestCase):
|
||||
|
||||
async def asyncSetUp(self):
|
||||
self.mock_response = Mock()
|
||||
self.mock_response.choices = [
|
||||
Mock(text="Nice day today!")
|
||||
]
|
||||
self.config_data = {
|
||||
"openai_key": "OPENAIKEY",
|
||||
"engine": "gpt-4",
|
||||
"max_tokens": 1024,
|
||||
"n": 1,
|
||||
"temperature": 0.9,
|
||||
}
|
||||
self.history_data = []
|
||||
|
||||
|
||||
class TestFunctionality(TestBotBase):
|
||||
|
||||
def test_load_config(self):
|
||||
with patch('builtins.open', mock_open(read_data=json.dumps(self.config_data))):
|
||||
result = FjerkroaBot.load_config('config.json')
|
||||
self.assertEqual(result, self.config_data)
|
||||
|
||||
async def test_on_message_event(self):
|
||||
with patch.object(FjerkroaBot, 'load_config', new=lambda s, c: self.config_data), \
|
||||
patch.object(FjerkroaBot, 'user', new_callable=PropertyMock) as mock_user:
|
||||
mock_user.return_value = MagicMock(spec=User)
|
||||
mock_user.return_value.id = 12
|
||||
bot = FjerkroaBot('config.json')
|
||||
message = MagicMock(spec=Message)
|
||||
message.content = "Hello, how are you?"
|
||||
message.author = AsyncMock(spec=User)
|
||||
message.author.id = 123
|
||||
message.author.bot = False
|
||||
message.channel = AsyncMock(spec=TextChannel)
|
||||
message.channel.send = AsyncMock()
|
||||
await bot.on_message(message)
|
||||
message.channel.send.assert_called_once_with("Hello!")
|
||||
|
||||
|
||||
if __name__ == "__mait__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user