Ejemplo n.º 1
0
class MessageProcessor:
    def __init__(self):
        self.logger = get_logger()
        self.ftp = SDXFTP(settings.FTP_HOST, settings.FTP_USER,
                          settings.FTP_PASS)

    def process(self, msg, tx_id):
        store_response_json = json.loads(msg)
        if tx_id is None:
            tx_id = store_response_json.get('tx_id')

        self.logger = self.logger.bind(tx_id=tx_id)
        self.logger.info('Received message')

        try:

            if store_response_json.get('feedback'):
                feedback_id = store_response_json.get('feedback_id')
                document = get_feedback_from_store(feedback_id)
                data = json.dumps(document).encode('utf-8')
                filename = f'feedback_{feedback_id}'
                self.ftp.deliver_binary(settings.FTP_FEEDBACK_FOLDER, filename,
                                        data)
            else:
                document = get_doc_from_store(tx_id)
                transform_processor = TransformProcessor(document, self.ftp)
                transform_processor.process()

            self.logger.info("Processed successfully")

            # If we don't unbind these fields, their current value will be retained for the next
            # submission.  This leads to incorrect values being logged out in the bound fields.
            self.logger = self.logger.unbind("tx_id", "ru_ref", "user_id")
        except KeyError:
            self.logger.error("No survey ID in document")
Ejemplo n.º 2
0
import mock

from requests import Response
from sdc.rabbit.exceptions import QuarantinableError, RetryableError
from unittest.mock import MagicMock

from app.helpers.sdxftp import SDXFTP


ftpconn = SDXFTP("", "", "")


class ProcessorTestBase:  # pylint: disable=no-member

    @staticmethod
    def _get_response():
        response = Response()
        response.status_code = 200
        response._content = b'Some content'
        return response

    def test_transform(self):
        with mock.patch('app.processors.transform_processor.get_sequence_no') as seq_mock:
            seq_mock.return_value = '1001'
            response = self._get_response()

            with mock.patch('app.processors.transform_processor.remote_call') as call_mock:

                # 500 something ary
                response.status_code = 500
                call_mock.return_value = response
import unittest
import mock
from unittest.mock import MagicMock
import json
import logging
from structlog import wrap_logger
from app.processors.cora_processor import CoraProcessor
from tests.test_data import cora_survey
from requests import Response
from app.helpers.sdxftp import SDXFTP
from sdc.rabbit.exceptions import QuarantinableError, RetryableError

logger = wrap_logger(logging.getLogger(__name__))
ftpconn = SDXFTP(logger, "", "", "")


class TestCoraProcessor(unittest.TestCase):
    def setUp(self):
        survey = json.loads(cora_survey)
        self.processor = CoraProcessor(logger, survey, ftpconn)
        self.processor.ftp.unzip_and_deliver = MagicMock(return_value=True)

    def _get_response(self):
        response = Response()
        response.status_code = 200
        response._content = b'Some content'
        return response

    def test_transform(self):
        with mock.patch(
                'app.processors.cora_processor.get_sequence_no') as seq_mock:
Ejemplo n.º 4
0
 def __init__(self):
     self.logger = get_logger()
     self.ftp = SDXFTP(settings.FTP_HOST, settings.FTP_USER,
                       settings.FTP_PASS)
 def __init__(self, logger=None):
     self.logger = logger or wrap_logger(logging.getLogger(__name__))
     self._ftp = SDXFTP(self.logger, settings.FTP_HOST, settings.FTP_USER,
                        settings.FTP_PASS)