2024-01-18 10:35:44 +00:00
|
|
|
import sys
|
|
|
|
sys.path.append('modem')
|
2024-01-20 13:41:51 +00:00
|
|
|
import numpy as np
|
2024-01-18 10:35:44 +00:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
from config import CONFIG
|
|
|
|
from message_p2p import MessageP2P
|
2024-02-06 08:22:55 +00:00
|
|
|
from message_system_db_messages import DatabaseManagerMessages
|
2024-01-27 11:07:07 +00:00
|
|
|
from event_manager import EventManager
|
|
|
|
import queue
|
2024-01-29 16:50:28 +00:00
|
|
|
import base64
|
2024-01-18 10:35:44 +00:00
|
|
|
|
|
|
|
class TestDataFrameFactory(unittest.TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
config_manager = CONFIG('modem/config.ini.example')
|
|
|
|
cls.config = config_manager.read()
|
2024-01-27 11:07:07 +00:00
|
|
|
|
|
|
|
cls.event_queue = queue.Queue()
|
|
|
|
cls.event_manager = EventManager([cls.event_queue])
|
2024-01-18 10:35:44 +00:00
|
|
|
cls.mycall = f"{cls.config['STATION']['mycall']}-{cls.config['STATION']['myssid']}"
|
2024-02-06 08:22:55 +00:00
|
|
|
cls.database_manager = DatabaseManagerMessages(cls.event_manager)
|
2024-01-18 10:35:44 +00:00
|
|
|
|
|
|
|
def testFromApiParams(self):
|
|
|
|
api_params = {
|
2024-02-02 18:50:16 +00:00
|
|
|
'destination': 'DJ2LS-3',
|
2024-01-18 10:35:44 +00:00
|
|
|
'body': 'Hello World!',
|
|
|
|
}
|
|
|
|
message = MessageP2P.from_api_params(self.mycall, api_params)
|
2024-02-02 18:50:16 +00:00
|
|
|
self.assertEqual(message.destination, api_params['destination'])
|
2024-01-18 10:35:44 +00:00
|
|
|
self.assertEqual(message.body, api_params['body'])
|
|
|
|
|
2024-01-20 13:41:51 +00:00
|
|
|
def testToPayloadWithAttachment(self):
|
|
|
|
attachment = {
|
|
|
|
'name': 'test.gif',
|
|
|
|
'type': 'image/gif',
|
2024-01-29 16:50:28 +00:00
|
|
|
'data': str(base64.b64encode(np.random.bytes(1024)), 'utf-8')
|
2024-01-18 10:35:44 +00:00
|
|
|
}
|
2024-02-02 18:50:16 +00:00
|
|
|
apiParams = {'destination': 'DJ2LS-3', 'body': 'Hello World!', 'attachments': [attachment]}
|
2024-01-29 16:50:28 +00:00
|
|
|
message = MessageP2P.from_api_params(self.mycall, apiParams)
|
|
|
|
|
2024-01-18 10:35:44 +00:00
|
|
|
payload = message.to_payload()
|
2024-01-27 11:07:07 +00:00
|
|
|
received_message = MessageP2P.from_payload(payload)
|
|
|
|
self.assertEqual(message.origin, received_message.origin)
|
|
|
|
self.assertEqual(message.destination, received_message.destination)
|
|
|
|
self.assertCountEqual(message.attachments, received_message.attachments)
|
2024-01-29 16:50:28 +00:00
|
|
|
# FIXME...
|
|
|
|
#self.assertEqual(attachment['data'], received_message.attachments[0]['data'])
|
2024-01-27 11:07:07 +00:00
|
|
|
|
|
|
|
def testToPayloadWithAttachmentAndDatabase(self):
|
|
|
|
attachment = {
|
|
|
|
'name': 'test.gif',
|
|
|
|
'type': 'image/gif',
|
2024-01-29 16:50:28 +00:00
|
|
|
'data': str(base64.b64encode(np.random.bytes(1024)), 'utf-8')
|
2024-01-27 11:07:07 +00:00
|
|
|
}
|
2024-02-02 18:50:16 +00:00
|
|
|
apiParams = {'destination': 'DJ2LS-3', 'body': 'Hello World!', 'attachments': [attachment]}
|
2024-01-29 16:50:28 +00:00
|
|
|
message = MessageP2P.from_api_params(self.mycall, apiParams)
|
|
|
|
|
2024-01-27 11:07:07 +00:00
|
|
|
payload = message.to_payload()
|
2024-01-20 13:41:51 +00:00
|
|
|
received_message = MessageP2P.from_payload(payload)
|
2024-01-29 16:50:28 +00:00
|
|
|
received_message_dict = MessageP2P.to_dict(received_message)
|
2024-01-25 14:17:38 +00:00
|
|
|
self.database_manager.add_message(received_message_dict)
|
|
|
|
|
2024-01-20 13:41:51 +00:00
|
|
|
self.assertEqual(message.origin, received_message.origin)
|
|
|
|
self.assertEqual(message.destination, received_message.destination)
|
|
|
|
self.assertCountEqual(message.attachments, received_message.attachments)
|
2024-02-07 18:54:54 +00:00
|
|
|
result = self.database_manager.get_message_by_id(message.id)
|
|
|
|
self.assertEqual(result["is_read"], True)
|
|
|
|
self.assertEqual(result["destination"], message.destination)
|
|
|
|
|
|
|
|
|
2024-01-18 10:35:44 +00:00
|
|
|
|
2024-01-25 14:17:38 +00:00
|
|
|
|
2024-01-27 11:07:07 +00:00
|
|
|
|
2024-01-18 10:35:44 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|