<commit_before># -*- coding: utf-8 -*-

"""Tests for vumi.persist.message_store."""

from twisted.internet.defer import inlineCallbacks, returnValue

from vumi.message import TransportEvent
from vumi.application.tests.test_base import ApplicationTestCase
from vumi.components import MessageStore


class TestMessageStore(ApplicationTestCase):
    # inherits from ApplicationTestCase for .mkmsg_in and .mkmsg_out

    @inlineCallbacks
    def setUp(self):
        yield super(TestMessageStore, self).setUp()
        self.redis = yield self.get_redis_manager()
        self.manager = self.get_riak_manager()
        self.store = MessageStore(self.manager, self.redis)

    @inlineCallbacks
    def _maybe_batch(self, tag, by_batch):
        add_kw, batch_id = {}, None
        if tag is not None:
            batch_id = yield self.store.batch_start([tag])
            if by_batch:
                add_kw['batch_id'] = batch_id
            else:
                add_kw['tag'] = tag
        returnValue((add_kw, batch_id))

    @inlineCallbacks
    def _create_outbound(self, tag=("pool", "tag"), by_batch=False):
        """Create and store an outbound message."""
        add_kw, batch_id = yield self._maybe_batch(tag, by_batch)
        msg = self.mkmsg_out(content="outfoo",
                             message_id=TransportEvent.generate_id())
        msg_id = msg['message_id']
        yield self.store.add_outbound_message(msg, **add_kw)
        returnValue((msg_id, msg, batch_id))

    @inlineCallbacks
    def _create_inbound(self, tag=("pool", "tag"), by_batch=False):
        """Create and store an inbound message."""
        add_kw, batch_id = yield self._maybe_batch(tag, by_batch)
        msg = self.mkmsg_in(content="infoo", to_addr="+1234567810001",
                            transport_type="sms",
                            message_id=TransportEvent.generate_id())
        msg_id = msg['message_id']
        yield self.store.add_inbound_message(msg, **add_kw)
        returnValue((msg_id, msg, batch_id))

    def _batch_status(self, ack=0, delivered=0, failed=0, pending=0, sent=0):
        return {
            'ack': ack, 'sent': sent,
            'delivery_report': sum([delivered, failed, pending]),
            'delivery_report.delivered': delivered,
            'delivery_report.failed': failed,
            'delivery_report.pending': pending,
            }

    @inlineCallbacks
    def test_batch_start(self):
        tag1 = ("poolA", "tag1")<commit_msg>fixing tests checking event status<commit_after># -*- coding: utf-8 -*-

"""Tests for vumi.persist.message_store."""

from twisted.internet.defer import inlineCallbacks, returnValue

from vumi.message import TransportEvent
from vumi.application.tests.test_base import ApplicationTestCase
from vumi.components import MessageStore


class TestMessageStore(ApplicationTestCase):
    # inherits from ApplicationTestCase for .mkmsg_in and .mkmsg_out

    @inlineCallbacks
    def setUp(self):
        yield super(TestMessageStore, self).setUp()
        self.redis = yield self.get_redis_manager()
        self.manager = self.get_riak_manager()
        self.store = MessageStore(self.manager, self.redis)

    @inlineCallbacks
    def _maybe_batch(self, tag, by_batch):
        add_kw, batch_id = {}, None
        if tag is not None:
            batch_id = yield self.store.batch_start([tag])
            if by_batch:
                add_kw['batch_id'] = batch_id
            else:
                add_kw['tag'] = tag
        returnValue((add_kw, batch_id))

    @inlineCallbacks
    def _create_outbound(self, tag=("pool", "tag"), by_batch=False):
        """Create and store an outbound message."""
        add_kw, batch_id = yield self._maybe_batch(tag, by_batch)
        msg = self.mkmsg_out(content="outfoo",
                             message_id=TransportEvent.generate_id())
        msg_id = msg['message_id']
        yield self.store.add_outbound_message(msg, **add_kw)
        returnValue((msg_id, msg, batch_id))

    @inlineCallbacks
    def _create_inbound(self, tag=("pool", "tag"), by_batch=False):
        """Create and store an inbound message."""
        add_kw, batch_id = yield self._maybe_batch(tag, by_batch)
        msg = self.mkmsg_in(content="infoo", to_addr="+1234567810001",
                            transport_type="sms",
                            message_id=TransportEvent.generate_id())
        msg_id = msg['message_id']
        yield self.store.add_inbound_message(msg, **add_kw)
        returnValue((msg_id, msg, batch_id))

    def _batch_status(self, ack=0, nack=0, delivered=0, failed=0, pending=0,
                        sent=0):
        return {
            'ack': ack, 'nack': nack, 'sent': sent,
            'delivery_report': sum([delivered, failed, pending]),
            'delivery_report.delivered': delivered,
            'delivery_report.failed': failed,
            'delivery_report.pending': pending,
            }

    @inlineCallbacks
    def test_batch_start(self):
        tag1 = ("poolA", "tag1")
{'fraction_non_alphanumeric': 0.11374030622390137, 'fraction_numerical': 0.008550407635712865, 'mean_word_length': 2.6661807580174925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3510243', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1493, 'n_words': 320}