<commit_before>from twisted.internet.defer import inlineCallbacks

from vumi.tests.helpers import VumiTestCase
from vumi.transports.base import Transport
from vumi.transports.tests.helpers import TransportHelper


class ToyTransport(Transport):
    def setup_transport(self):
        self.msgs = []

    def handle_outbound_message(self, msg):
        self.msgs.append(msg)


class TestBaseTransport(VumiTestCase):

    TEST_MIDDLEWARE_CONFIG = {
        "middleware": [
            {"mw1": "vumi.middleware.tests.utils.RecordingMiddleware"},
            {"mw2": "vumi.middleware.tests.utils.RecordingMiddleware"},
        ],
    }

    def setUp(self):
        self.tx_helper = self.add_helper(TransportHelper(ToyTransport))

    @inlineCallbacks
    def test_start_transport(self):
        tr = yield self.tx_helper.get_transport({})
        self.assertEqual(self.tx_helper.transport_name, tr.transport_name)
        self.assertTrue(len(tr.connectors) >= 1)
        connector = tr.connectors[tr.transport_name]
        self.assertTrue(connector._consumers.keys(), set(['outbound']))
        self.assertTrue(connector._publishers.keys(),
                        set(['inbound', 'event']))
        self.assertEqual(tr.failure_publisher.routing_key,
                         '%s.failures' % (tr.transport_name,))

    @inlineCallbacks
    def test_middleware_for_inbound_messages(self):
        transport = yield self.tx_helper.get_transport(
            self.TEST_MIDDLEWARE_CONFIG)
        orig_msg = self.tx_helper.make_inbound("inbound")
        yield transport.publish_message(**orig_msg.payload)
        [msg] = self.tx_helper.get_dispatched_inbound()
        self.assertEqual(msg['record'], [
            ['mw2', 'inbound', self.tx_helper.transport_name],
            ['mw1', 'inbound', self.tx_helper.transport_name],
            ])

    @inlineCallbacks
    def test_middleware_for_events(self):
        transport = yield self.tx_helper.get_transport(
            self.TEST_MIDDLEWARE_CONFIG)
        orig_msg = self.tx_helper.make_ack()
        yield transport.publish_event(**orig_msg.payload)
        [msg] = self.tx_helper.get_dispatched_events()
        self.assertEqual(msg['record'], [
            ['mw2', 'event', self.tx_helper.transport_name],
            ['mw1', 'event', self.tx_helper.transport_name],
            ])

    @inlineCallbacks
    def test_middleware_for_failures(self):
        transport = yield self.tx_helper.get_transport(
            self.TEST_MIDDLEWARE_CONFIG)
        orig_msg = self.tx_helper.make_outbound("outbound")
        yield transport.send_failure(orig_msg, ValueError(), "dummy_traceback")
        [msg] = self.tx_helper.get_dispatched_failures()
        self.assertEqual(msg['record'], [
            ['mw2', 'failure', self.tx_helper.transport_name],
            ['mw1', 'failure', self.tx_helper.transport_name],
            ])

    @inlineCallbacks
    def test_middleware_for_outbound_messages(self):
        transport = yield self.tx_helper.get_transport(
            self.TEST_MIDDLEWARE_CONFIG)
        yield self.tx_helper.make_dispatch_outbound("outbound")
        [msg] = transport.msgs
        self.assertEqual(msg['record'], [
            ('mw1', 'outbound', self.tx_helper.transport_name),<commit_msg>Make `msgs.append` an outbound handler in Transport tests instead of needing a toy subclass that overrides handle_outbound_message<commit_after>from twisted.internet.defer import inlineCallbacks

from vumi.tests.helpers import VumiTestCase
from vumi.transports.base import Transport
from vumi.transports.tests.helpers import TransportHelper


class TestBaseTransport(VumiTestCase):

    TEST_MIDDLEWARE_CONFIG = {
        "middleware": [
            {"mw1": "vumi.middleware.tests.utils.RecordingMiddleware"},
            {"mw2": "vumi.middleware.tests.utils.RecordingMiddleware"},
        ],
    }

    def setUp(self):
        self.tx_helper = self.add_helper(TransportHelper(Transport))

    @inlineCallbacks
    def test_start_transport(self):
        tr = yield self.tx_helper.get_transport({})
        self.assertEqual(self.tx_helper.transport_name, tr.transport_name)
        self.assertTrue(len(tr.connectors) >= 1)
        connector = tr.connectors[tr.transport_name]
        self.assertTrue(connector._consumers.keys(), set(['outbound']))
        self.assertTrue(connector._publishers.keys(),
                        set(['inbound', 'event']))
        self.assertEqual(tr.failure_publisher.routing_key,
                         '%s.failures' % (tr.transport_name,))

    @inlineCallbacks
    def test_middleware_for_inbound_messages(self):
        transport = yield self.tx_helper.get_transport(
            self.TEST_MIDDLEWARE_CONFIG)
        orig_msg = self.tx_helper.make_inbound("inbound")
        yield transport.publish_message(**orig_msg.payload)
        [msg] = self.tx_helper.get_dispatched_inbound()
        self.assertEqual(msg['record'], [
            ['mw2', 'inbound', self.tx_helper.transport_name],
            ['mw1', 'inbound', self.tx_helper.transport_name],
            ])

    @inlineCallbacks
    def test_middleware_for_events(self):
        transport = yield self.tx_helper.get_transport(
            self.TEST_MIDDLEWARE_CONFIG)
        orig_msg = self.tx_helper.make_ack()
        yield transport.publish_event(**orig_msg.payload)
        [msg] = self.tx_helper.get_dispatched_events()
        self.assertEqual(msg['record'], [
            ['mw2', 'event', self.tx_helper.transport_name],
            ['mw1', 'event', self.tx_helper.transport_name],
            ])

    @inlineCallbacks
    def test_middleware_for_failures(self):
        transport = yield self.tx_helper.get_transport(
            self.TEST_MIDDLEWARE_CONFIG)
        orig_msg = self.tx_helper.make_outbound("outbound")
        yield transport.send_failure(orig_msg, ValueError(), "dummy_traceback")
        [msg] = self.tx_helper.get_dispatched_failures()
        self.assertEqual(msg['record'], [
            ['mw2', 'failure', self.tx_helper.transport_name],
            ['mw1', 'failure', self.tx_helper.transport_name],
            ])

    @inlineCallbacks
    def test_middleware_for_outbound_messages(self):
        msgs = []
        transport = yield self.tx_helper.get_transport(
            self.TEST_MIDDLEWARE_CONFIG)
        transport.add_outbound_handler(msgs.append)

        yield self.tx_helper.make_dispatch_outbound("outbound")

        [msg] = msgs
        self.assertEqual(msg['record'], [
            ('mw1', 'outbound', self.tx_helper.transport_name),
{'fraction_non_alphanumeric': 0.1144559902200489, 'fraction_numerical': 0.003056234718826406, 'mean_word_length': 3.000611246943765, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6521217', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1946, 'n_words': 276}