Coverage for mindsdb / integrations / handlers / discord_handler / tests / test_discord.py: 0%

26 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1import unittest 

2from unittest.mock import patch 

3 

4from mindsdb_sql_parser import ast 

5from mindsdb_sql_parser.ast import BinaryOperation, Identifier, Constant 

6from mindsdb_sql_parser.ast.select.star import Star 

7from mindsdb.integrations.handlers.discord_handler.discord_handler import DiscordHandler 

8 

9 

10class DiscordHandlerTest(unittest.TestCase): 

11 @classmethod 

12 def setUpClass(cls): 

13 cls.handler = DiscordHandler( 

14 name='discord_datasource', connection_data={'token': 'test-discord-token'} 

15 ) 

16 

17 def test_0_check_connection(self): 

18 assert self.handler.check_connection() 

19 

20 @patch('mindsdb.integrations.handlers.discord_handler.discord_handler.requests.get') 

21 def test_1_read_messages(self, mock_get): 

22 mock_get.return_value.status_code = 200 

23 mock_get.return_value.json.return_value = [{'content': 'Test message'}] 

24 query = ast.Select( 

25 targets=[Star()], 

26 from_table="messages", 

27 where=BinaryOperation( 

28 op='=', args=[Identifier('channel_id'), Constant('1234567890')] 

29 ), 

30 ) 

31 

32 self.handler._tables['messages'].select(query) 

33 mock_get.assert_called_with( 

34 'https://discord.com/api/v10/channels/1234567890/messages', 

35 headers={ 

36 'Authorization': 'Bot test-discord-token', 

37 'Content-Type': 'application/json', 

38 }, 

39 params={'limit': 100}, 

40 ) 

41 

42 @patch( 

43 'mindsdb.integrations.handlers.discord_handler.discord_handler.requests.post' 

44 ) 

45 def test_2_send_message(self, mock_post): 

46 mock_post.return_value.status_code = 200 

47 self.handler._tables['messages'].send_message( 

48 [{'channel_id': '1234567890', 'text': 'Test message'}] 

49 ) 

50 mock_post.assert_called_with( 

51 'https://discord.com/api/v10/channels/1234567890/messages', 

52 headers={ 

53 'Authorization': 'Bot test-discord-token', 

54 'Content-Type': 'application/json', 

55 }, 

56 json={'content': 'Test message'}, 

57 ) 

58 

59 

60if __name__ == '__main__': 

61 unittest.main()