FreeDATA/tests/test_message_p2p.py

77 lines
3 KiB
Python
Raw Normal View History

2024-01-18 10:35:44 +00:00
import sys
sys.path.append('modem')
import numpy as np
2024-01-18 10:35:44 +00:00
import unittest
from config import CONFIG
from message_p2p import MessageP2P
from message_system_db_manager import DatabaseManager
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-01-27 11:07:07 +00:00
cls.database_manager = DatabaseManager(cls.event_manager, uri='sqlite:///:memory:')
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'])
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()
received_message = MessageP2P.from_payload(payload)
2024-01-29 16:50:28 +00:00
received_message_dict = MessageP2P.to_dict(received_message)
self.database_manager.add_message(received_message_dict)
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-18 10:35:44 +00:00
result = self.database_manager.get_all_messages()
self.assertEqual(result[0]["destination"], message.destination)
2024-01-27 11:07:07 +00:00
2024-01-18 10:35:44 +00:00
if __name__ == '__main__':
unittest.main()