<commit_before>
from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks

import pymongo

from vumi.message import TransportUserMessage
from vumi.tests.utils import get_stubbed_worker, UTCNearNow

from tests.utils import MessageMaker
from tests.utils import ObjectMaker
from vusion import GarbageWorker


class GarabageWorkerTestCase(TestCase, MessageMaker, ObjectMaker):

    @inlineCallbacks
    def setUp(self):
        self.config = {
            'database_name': 'test',
            'application_name': 'garbage',
            'transport_name': 'garbage',
            'mongodb_host': 'localhost',
            'mongodb_port': 27017}

        connection = pymongo.Connection('localhost', 27017)
        db = connection[self.config['database_name']]
        self.unmatchable_replies_collection = db['unmatchable_reply']
        self.unmatchable_replies_collection.drop()
        self.templates_collection = db['templates']
        self.templates_collection.drop()
        self.shortcodes_collection = db['shortcodes']
        self.shortcodes_collection.drop()

        self.worker = get_stubbed_worker(GarbageWorker,
                                         config=self.config)
        self.broker = self.worker._amqp_client.broker
        self.worker.startService()
        yield self.worker.startWorker()

    def tearDown(self):
        self.broker.dispatched = {}
        self.unmatchable_replies_collection.drop()
        self.shortcodes_collection.drop()
        self.templates_collection.drop()

    @inlineCallbacks
    def send(self, msg):
        self.broker.publish_message(
            'vumi', '%s.inbound' % self.config['transport_name'], msg)
        yield self.broker.kick_delivery()

    @inlineCallbacks
    def test_receive_user_message_without_error_template(self):
        msg = self.mkmsg_in(to_addr='8282')
        self.shortcodes_collection.save(self.mkobj_shortcode(code='8282'))

        yield self.send(msg)

        self.assertEqual(1, self.unmatchable_replies_collection.count())
        messages = self.broker.get_messages('vumi', 'garbage.outbound')
        self.assertEqual(len(messages), 0)

    @inlineCallbacks
    def test_receive_user_message_with_one_error_template_matching_to_addr(self):
        participant_transport_metadata = {'some_key': 'some_value'}
        msg = self.mkmsg_in(content='Gen 2', to_addr='8181', transport_metadata=participant_transport_metadata)
        template_id = self.templates_collection.save(
            self.mkobj_template_unmatching_keyword()
        )
        self.shortcodes_collection.save(self.mkobj_shortcode(template_id))

        yield self.send(msg)

        self.assertTrue(2, self.unmatchable_replies_collection.count())
        messages = self.broker.get_messages('vumi', 'garbage.outbound')
        self.assertEqual(len(messages), 1)
        self.assertEqual(messages[0]['content'],
                         'Gen does not match any keyword.')
        self.assertEqual(messages[0]['to_addr'], msg['from_addr'])
        self.assertEqual(messages[0]['from_addr'], '256-8181')
        self.assertEqual(messages[0]['transport_metadata'], participant_transport_metadata)

    @inlineCallbacks
    def test_receive_user_message_with_two_error_template_matching_to_addr(self):
        msg = self.mkmsg_in(content='Gen 2', to_addr='8282')
        template_1_id = self.templates_collection.save(
            self.mkobj_template_unmatching_keyword())
        shortcode_1 = self.mkobj_shortcode(template_1_id, '8181')
        self.shortcodes_collection.save(shortcode_1)

        template_2_id = self.templates_collection.save(
            self.mkobj_template_unmatching_keyword(message="KEYWORD is not good."))
        shortcode_2 = self.mkobj_shortcode(template_2_id, '8282')
        self.shortcodes_collection.save(shortcode_2)

        yield self.send(msg)

        self.assertEqual(2, self.unmatchable_replies_collection.count())
        messages = self.broker.get_messages('vumi', 'garbage.outbound')
        self.assertEqual(len(messages), 1)
        self.assertEqual(messages[0]['content'],
                         'Gen is not good.')
        self.assertEqual(messages[0]['to_addr'], msg['from_addr'])<commit_msg>fix garbadge worker tests<commit_after>
from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks

import pymongo

from vumi.message import TransportUserMessage
from vumi.tests.utils import get_stubbed_worker, UTCNearNow

from tests.utils import MessageMaker
from tests.utils import ObjectMaker
from vusion import GarbageWorker


class GarabageWorkerTestCase(TestCase, MessageMaker, ObjectMaker):

    @inlineCallbacks
    def setUp(self):
        self.config = {
            'database_name': 'test',
            'application_name': 'garbage',
            'transport_name': 'garbage',
            'mongodb_host': 'localhost',
            'mongodb_port': 27017}

        connection = pymongo.Connection('localhost', 27017)
        db = connection[self.config['database_name']]
        self.unmatchable_replies_collection = db['unmatchable_reply']
        self.unmatchable_replies_collection.drop()
        self.templates_collection = db['templates']
        self.templates_collection.drop()
        self.shortcodes_collection = db['shortcodes']
        self.shortcodes_collection.drop()

        self.worker = get_stubbed_worker(GarbageWorker,
                                         config=self.config)
        self.broker = self.worker._amqp_client.broker
        self.worker.startService()
        yield self.worker.startWorker()

    def tearDown(self):
        self.broker.dispatched = {}
        self.unmatchable_replies_collection.drop()
        self.shortcodes_collection.drop()
        self.templates_collection.drop()

    @inlineCallbacks
    def send(self, msg):
        self.broker.publish_message(
            'vumi', '%s.inbound' % self.config['transport_name'], msg)
        yield self.broker.kick_delivery()

    @inlineCallbacks
    def test_receive_user_message_without_error_template(self):
        msg = self.mkmsg_in(to_addr='8282')
        self.shortcodes_collection.save(self.mkobj_shortcode(code='8282'))

        yield self.send(msg)

        self.assertEqual(1, self.unmatchable_replies_collection.count())
        messages = self.broker.get_messages('vumi', 'garbage.outbound')
        self.assertEqual(len(messages), 0)

    @inlineCallbacks
    def test_receive_user_message_with_one_error_template_matching_to_addr(self):
        participant_transport_metadata = {'some_key': 'some_value'}
        msg = self.mkmsg_in(content='Gen 2', to_addr='8181', transport_metadata=participant_transport_metadata)
        template_id = self.templates_collection.save(
            self.mkobj_template_unmatching_keyword()
        )
        self.shortcodes_collection.save(self.mkobj_shortcode(error_template=template_id))

        yield self.send(msg)

        self.assertTrue(2, self.unmatchable_replies_collection.count())
        messages = self.broker.get_messages('vumi', 'garbage.outbound')
        self.assertEqual(len(messages), 1)
        self.assertEqual(messages[0]['content'],
                         'Gen does not match any keyword.')
        self.assertEqual(messages[0]['to_addr'], msg['from_addr'])
        self.assertEqual(messages[0]['from_addr'], '256-8181')
        self.assertEqual(messages[0]['transport_metadata'], participant_transport_metadata)

    @inlineCallbacks
    def test_receive_user_message_with_two_error_template_matching_to_addr(self):
        msg = self.mkmsg_in(content='Gen 2', to_addr='8282')
        template_1_id = self.templates_collection.save(
            self.mkobj_template_unmatching_keyword())
        shortcode_1 = self.mkobj_shortcode(error_template=template_1_id, code='8181')
        self.shortcodes_collection.save(shortcode_1)

        template_2_id = self.templates_collection.save(
            self.mkobj_template_unmatching_keyword(message="KEYWORD is not good."))
        shortcode_2 = self.mkobj_shortcode(error_template=template_2_id, code='8282')
        self.shortcodes_collection.save(shortcode_2)

        yield self.send(msg)

        self.assertEqual(2, self.unmatchable_replies_collection.count())
        messages = self.broker.get_messages('vumi', 'garbage.outbound')
        self.assertEqual(len(messages), 1)
        self.assertEqual(messages[0]['content'],
                         'Gen is not good.')
        self.assertEqual(messages[0]['to_addr'], msg['from_addr'])
{'fraction_non_alphanumeric': 0.10467706013363029, 'fraction_numerical': 0.014769663579885125, 'mean_word_length': 3.3486238532110093, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5432403', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2467, 'n_words': 364}