# Path: chat/pubsub.py
# def _publish_record_event(user_ids: [],
#                           record_type: str,
#                           event: str,
#                           record: Record) -> None:
#     if len(user_ids) == 0:
#         return
#     _publish_event(user_ids, event, {
#         'event_type': event,
#         'type': 'record',
#         'record_type': record_type,
#         'record': serialize_record(record)
#     })


import unittest
from unittest.mock import Mock, patch
from skygear.encoding import deserialize_record
from ..pubsub import _publish_record_event




class TestPublishEvent(unittest.TestCase):

    def record(self):
        return deserialize_record({
            '_id': 'message/1',
            '_access': None,
            '_ownerID': 'user1',
            'conversation': 'conversation1',
            'body': 'hihi'
        })

    @patch('chat.pubsub.Hub', autospec=True)
    @patch('chat.pubsub._get_channels_by_user_ids',
           Mock(return_value=['channel1']))
    @patch('chat.pubsub.skyoptions',
           Mock(return_value={'apikey': 'changeme'}))
    def test_pubsub_publish_called(self, mock_hub):

# Path: chat/pubsub.py
# def _publish_record_event(user_ids: [],
#                           record_type: str,
#                           event: str,
#                           record: Record) -> None:
#     if len(user_ids) == 0:
#         return
#     _publish_event(user_ids, event, {
#         'event_type': event,
#         'type': 'record',
#         'record_type': record_type,
#         'record': serialize_record(record)
#     })


# Path: chat/test/test_publish_event.py
import unittest
from unittest.mock import Mock, patch
from skygear.encoding import deserialize_record
from ..pubsub import _publish_record_event





class TestPublishEvent(unittest.TestCase):

    def record(self):
        return deserialize_record({
            '_id': 'message/1',
            '_access': None,
            '_ownerID': 'user1',
            'conversation': 'conversation1',
            'body': 'hihi'
        })

    @patch('chat.pubsub.Hub', autospec=True)
    @patch('chat.pubsub._get_channels_by_user_ids',
           Mock(return_value=['channel1']))
    @patch('chat.pubsub.skyoptions',
           Mock(return_value={'apikey': 'changeme'}))
    def test_pubsub_publish_called(self, mock_hub):

        _publish_record_event('user1', 'message', 'create', self.record())