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

from mock import Mock
from twisted.internet.defer import inlineCallbacks, returnValue

from vumi.application.tests.test_sandbox import (
    ResourceTestCaseBase, DummyAppWorker)

from go.apps.jsbox.contacts import ContactsResource
from go.vumitools.tests.utils import GoPersistenceMixin
from go.vumitools.account import AccountStore
from go.vumitools.contact import ContactStore


class StubbedAppWorker(DummyAppWorker):
    def __init__(self):
        super(StubbedAppWorker, self).__init__()
        self.user_api = Mock()

    def user_api_for_api(self, api):
        return self.user_api


class TestContactsResource(ResourceTestCaseBase, GoPersistenceMixin):
    use_riak = True
    app_worker_cls = StubbedAppWorker
    resource_cls = ContactsResource

    @inlineCallbacks
    def setUp(self):
        super(TestContactsResource, self).setUp()
        yield self._persist_setUp()

        # We pass `self` in as the VumiApi object here, because mk_user() just
        # grabs .account_store off it.
        self.manager = self.get_riak_manager()
        self.account_store = AccountStore(self.manager)
        self.account = yield self.mk_user(self, u'user')
        self.contact_store = ContactStore.from_user_account(self.account)
        yield self.contact_store.contacts.enable_search()

        self.app_worker.user_api.contact_store = self.contact_store
        yield self.create_resource({'delivery_class': u'sms'})

    def tearDown(self):
        super(TestContactsResource, self).tearDown()
        return self._persist_tearDown()

    def check_reply(self, reply, **kw):
        kw.setdefault('success', True)
        for key, expected_value in kw.iteritems():
            self.assertEqual(reply[key], expected_value)

    def check_contact_reply(self, reply, **expected_fields):
        for field_name, expected_value in expected_fields.iteritems():
            self.assertEqual(reply['contact'][field_name], expected_value)
        self.check_reply(reply)

    @inlineCallbacks
    def assert_bad_command(self, cmd, **kw):
        reply = yield self.dispatch_command(cmd, **kw)
        self.check_reply(reply, success=False)

    @inlineCallbacks
    def check_contact_fields(self, key, **expected_fields):
        contact = yield self.contact_store.get_contact_by_key(key)
        fields = yield contact.get_data()
        for field_name, expected_value in expected_fields.iteritems():
            self.assertEqual(fields[field_name], expected_value)

    @inlineCallbacks
    def new_contact(self, groups=[], **fields):
        contact = yield self.contact_store.new_contact(**fields)
        map(contact.add_to_group, groups)
        yield contact.save()
        returnValue(contact)

    @inlineCallbacks
    def test_handle_get(self):
        contact = yield self.new_contact(
            name=u'<NAME>',
            surname=u'Person',
            msisdn=u'+27831234567')
        reply = yield self.dispatch_command('get', addr=u'+27831234567')
        self.check_contact_reply(
            reply,
            key=contact.key,
            name=u'<NAME>',
            surname=u'Person',
            msisdn=u'+27831234567')

    @inlineCallbacks
    def test_handle_get_parsing(self):<commit_msg>Change 'map' to 'for c in coll: fn(x)' for new_contact() in ContactsResource tests<commit_after># -*- coding: utf-8 -*-

from mock import Mock
from twisted.internet.defer import inlineCallbacks, returnValue

from vumi.application.tests.test_sandbox import (
    ResourceTestCaseBase, DummyAppWorker)

from go.apps.jsbox.contacts import ContactsResource
from go.vumitools.tests.utils import GoPersistenceMixin
from go.vumitools.account import AccountStore
from go.vumitools.contact import ContactStore


class StubbedAppWorker(DummyAppWorker):
    def __init__(self):
        super(StubbedAppWorker, self).__init__()
        self.user_api = Mock()

    def user_api_for_api(self, api):
        return self.user_api


class TestContactsResource(ResourceTestCaseBase, GoPersistenceMixin):
    use_riak = True
    app_worker_cls = StubbedAppWorker
    resource_cls = ContactsResource

    @inlineCallbacks
    def setUp(self):
        super(TestContactsResource, self).setUp()
        yield self._persist_setUp()

        # We pass `self` in as the VumiApi object here, because mk_user() just
        # grabs .account_store off it.
        self.manager = self.get_riak_manager()
        self.account_store = AccountStore(self.manager)
        self.account = yield self.mk_user(self, u'user')
        self.contact_store = ContactStore.from_user_account(self.account)
        yield self.contact_store.contacts.enable_search()

        self.app_worker.user_api.contact_store = self.contact_store
        yield self.create_resource({'delivery_class': u'sms'})

    def tearDown(self):
        super(TestContactsResource, self).tearDown()
        return self._persist_tearDown()

    def check_reply(self, reply, **kw):
        kw.setdefault('success', True)
        for key, expected_value in kw.iteritems():
            self.assertEqual(reply[key], expected_value)

    def check_contact_reply(self, reply, **expected_fields):
        for field_name, expected_value in expected_fields.iteritems():
            self.assertEqual(reply['contact'][field_name], expected_value)
        self.check_reply(reply)

    @inlineCallbacks
    def assert_bad_command(self, cmd, **kw):
        reply = yield self.dispatch_command(cmd, **kw)
        self.check_reply(reply, success=False)

    @inlineCallbacks
    def check_contact_fields(self, key, **expected_fields):
        contact = yield self.contact_store.get_contact_by_key(key)
        fields = yield contact.get_data()
        for field_name, expected_value in expected_fields.iteritems():
            self.assertEqual(fields[field_name], expected_value)

    @inlineCallbacks
    def new_contact(self, groups=[], **fields):
        contact = yield self.contact_store.new_contact(**fields)
        for group in groups:
            contact.add_to_group(group)
        yield contact.save()
        returnValue(contact)

    @inlineCallbacks
    def test_handle_get(self):
        contact = yield self.new_contact(
            name=u'<NAME>',
            surname=u'Person',
            msisdn=u'+27831234567')
        reply = yield self.dispatch_command('get', addr=u'+27831234567')
        self.check_contact_reply(
            reply,
            key=contact.key,
            name=u'<NAME>',
            surname=u'Person',
            msisdn=u'+27831234567')

    @inlineCallbacks
    def test_handle_get_parsing(self):
{'fraction_non_alphanumeric': 0.09779322853688029, 'fraction_numerical': 0.010278113663845224, 'mean_word_length': 3.236235595390525, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2539202', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1949, 'n_words': 394}