Example #1
0
class MessagePublisherTestCase(TestCase):
    def setUp(self):
        self._message_publisher = MessagePublisher()

    @mock.patch.object(MessagePublisher, '_producer')
    def test_send_entity_update_message(self, mock_producer):
        self._message_publisher.send_entity_update_message(entity)

        mock_producer.send.assert_called_once_with(topic,
                                                   key=key,
                                                   value=Matcher(
                                                       EntityUpdateMessage, {
                                                           'action': 'update',
                                                           'type': topic,
                                                           'key': key,
                                                           'version': version,
                                                           'payload': data,
                                                           'tags': []
                                                       }))

    @mock.patch.object(MessagePublisher, '_producer')
    def test_send_entity_delete_message(self, mock_producer):
        self._message_publisher.send_entity_delete_message(entity)

        mock_producer.send.assert_called_once_with(topic,
                                                   key=key,
                                                   value=Matcher(
                                                       EntityDeleteMessage, {
                                                           'action': 'delete',
                                                           'type': topic,
                                                           'key': key,
                                                           'version': version
                                                       }))
Example #2
0
class Command(BaseCommand):

    help = 'publish all data from a business entity to the business entity channel/topic'

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._entity_manager = BusinessEntityManager()
        self._logger = logging.getLogger(__name__)
        self._chunk_size = settings.DEFAULT_CHUNK_SIZE
        self._message_service = MessagePublisher()

    def add_arguments(self, parser):
        parser.add_argument(Constants.BUSINESS_ENTITY, type=str)
        parser.add_argument(Constants.CHUNK_SIZE,
                            type=int,
                            nargs='?',
                            default=settings.DEFAULT_CHUNK_SIZE)

    def handle(self, *args, **kwargs):
        self._chunk_size = kwargs[Constants.CHUNK_SIZE]
        business_entity = kwargs[Constants.BUSINESS_ENTITY]
        queryset = self._entity_manager.find_all(business_entity)
        tags = [Constants.FULL_EXPORT]

        for business_entity in queryset.iterator(chunk_size=self._chunk_size):
            self._message_service.send_entity_update_message(
                business_entity, tags)

        self._message_service.shutdown()
Example #3
0
class BusinessEntityManager:

    _message_publisher = MessagePublisher()

    def update_or_create(self, entity_name: str, key: str, version: str,
                         user: User,
                         data: str) -> (AbstractBusinessEntity, bool):
        business_entity_class = BusinessEntityUtils.get_entity_class(
            entity_name)
        business_entity, created = business_entity_class.objects.update_or_create(
            key=key,
            version=version,
            defaults={
                'key': key,
                'version': version,
                'publisher': user,
                'data': data
            })

        self._message_publisher.send_entity_update_message(business_entity)

        return created

    def find_all(self, entity_name: str) -> QuerySet:
        business_entity_class = BusinessEntityUtils.get_entity_class(
            entity_name)
        return business_entity_class.objects.all()

    def delete(self, entity_name: str, key: str, version: str) -> int:
        business_entity_class = BusinessEntityUtils.get_entity_class(
            entity_name)
        return self.delete_by_queryset(
            business_entity_class.objects.filter(key=key, version=version))

    def delete_by_key(self, entity_name: str, key: str) -> int:
        business_entity_class = BusinessEntityUtils.get_entity_class(
            entity_name)
        return self.delete_by_queryset(
            business_entity_class.objects.filter(key=key))

    def delete_by_queryset(self, queryset: QuerySet) -> int:
        for entity in queryset.all():
            self._message_publisher.send_entity_delete_message(entity)
        # return only the number of deleted objects
        return queryset.delete()[0]

    def delete_by_instance(self, entity: AbstractBusinessEntity) -> int:
        self._message_publisher.send_entity_delete_message(entity)
        return entity.delete()[0]
Example #4
0
 def setUp(self):
     self._message_publisher = MessagePublisher()
Example #5
0
 def setUp(self):
     settings.MESSAGING_PROVIDER = 'kafka'
     self._message_publisher = MessagePublisher()
Example #6
0
 def __init__(self, **kwargs):
     super().__init__(**kwargs)
     self._entity_manager = BusinessEntityManager()
     self._logger = logging.getLogger(__name__)
     self._chunk_size = settings.DEFAULT_CHUNK_SIZE
     self._message_service = MessagePublisher()