<commit_before># -*- coding: utf-8 -*-
from os import path
from StringIO import StringIO
from zipfile import ZipFile

from django.conf import settings
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.core import mail

from django.core.files.storage import default_storage
from go.contacts.parsers.base import FieldNormalizer
from go.base.tests.utils import VumiGoDjangoTestCase


TEST_GROUP_NAME = u"Test Group"
TEST_CONTACT_NAME = u"Name"
TEST_CONTACT_SURNAME = u"Surname"
TEST_CONTACT_MSISDN = u"+27761234567"


def newest(models):
    return max(models, key=lambda m: m.created_at)


def person_url(person_key):
    return reverse('contacts:person', kwargs={'person_key': person_key})


def group_url(group_key):
    return reverse('contacts:group', kwargs={'group_key': group_key})


class BaseContactsTestCase(VumiGoDjangoTestCase):
    use_riak = True

    def setUp(self):
        super(BaseContactsTestCase, self).setUp()
        self.setup_api()
        self.setup_user_api()
        self.setup_client()

    def mkcontact(self, name=None, surname=None, msisdn=u'+1234567890',
                  **kwargs):
        return self.contact_store.new_contact(
            name=unicode(name or TEST_CONTACT_NAME),
            surname=unicode(surname or TEST_CONTACT_SURNAME),
            msisdn=unicode(msisdn), **kwargs)


class ContactsTestCase(BaseContactsTestCase):
    def test_redirect_index(self):
        response = self.client.get(reverse('contacts:index'))
        self.assertRedirects(response, reverse('contacts:groups'))

    def get_all_contacts(self, keys=None):
        if keys is None:
            keys = self.contact_store.list_contacts()
        contacts = []
        for batch in self.contact_store.contacts.load_all_bunches(keys):
            contacts.extend(batch)
        return contacts

    def get_latest_contact(self):
        return max(self.get_all_contacts(), key=lambda c: c.created_at)

    def test_contact_creation(self):
        group = self.contact_store.new_group(TEST_GROUP_NAME)
        response = self.client.post(reverse('contacts:new_person'), {
            'name': 'New',
            'surname': 'Person',
            'msisdn': '27761234567',
            'groups': [group.key],
        })
        contact = self.get_latest_contact()
        self.assertRedirects(response, person_url(contact.key))

    def test_contact_deleting(self):
        contact = self.mkcontact()
        person_url = reverse('contacts:person', kwargs={
            'person_key': contact.key,<commit_msg>Add test for contacts details view<commit_after># -*- coding: utf-8 -*-
from os import path
from StringIO import StringIO
from zipfile import ZipFile

from django.conf import settings
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.core import mail

from django.core.files.storage import default_storage
from go.contacts.parsers.base import FieldNormalizer
from go.base.tests.utils import VumiGoDjangoTestCase


TEST_GROUP_NAME = u"Test Group"
TEST_CONTACT_NAME = u"Name"
TEST_CONTACT_SURNAME = u"Surname"
TEST_CONTACT_MSISDN = u"+27761234567"


def newest(models):
    return max(models, key=lambda m: m.created_at)


def person_url(person_key):
    return reverse('contacts:person', kwargs={'person_key': person_key})


def group_url(group_key):
    return reverse('contacts:group', kwargs={'group_key': group_key})


class BaseContactsTestCase(VumiGoDjangoTestCase):
    use_riak = True

    def setUp(self):
        super(BaseContactsTestCase, self).setUp()
        self.setup_api()
        self.setup_user_api()
        self.setup_client()

    def mkcontact(self, name=None, surname=None, msisdn=u'+1234567890',
                  **kwargs):
        return self.contact_store.new_contact(
            name=unicode(name or TEST_CONTACT_NAME),
            surname=unicode(surname or TEST_CONTACT_SURNAME),
            msisdn=unicode(msisdn), **kwargs)


class ContactsTestCase(BaseContactsTestCase):
    def test_redirect_index(self):
        response = self.client.get(reverse('contacts:index'))
        self.assertRedirects(response, reverse('contacts:groups'))

    def get_all_contacts(self, keys=None):
        if keys is None:
            keys = self.contact_store.list_contacts()
        contacts = []
        for batch in self.contact_store.contacts.load_all_bunches(keys):
            contacts.extend(batch)
        return contacts

    def get_latest_contact(self):
        return max(self.get_all_contacts(), key=lambda c: c.created_at)

    def test_contat_details(self):
        group_1 = self.contact_store.new_group(u'lerp')
        group_2 = self.contact_store.new_group(u'larp')

        contact = self.mkcontact(
            name='Foo',
            surname='Bar',
            groups=[group_1, group_2],
            extra={'black': 'moth', 'super': 'rainbow'})

        response = self.client.get(person_url(contact.key))

        self.assertContains(response, 'Foo')
        self.assertContains(response, 'Bar')

        self.assertContains(response, 'lerp')
        self.assertContains(response, 'larp')

        self.assertContains(response, 'black')
        self.assertContains(response, 'moth')
        self.assertContains(response, 'super')
        self.assertContains(response, 'rainbow')

    def test_contact_creation(self):
        group = self.contact_store.new_group(TEST_GROUP_NAME)
        response = self.client.post(reverse('contacts:new_person'), {
            'name': 'New',
            'surname': 'Person',
            'msisdn': '27761234567',
            'groups': [group.key],
        })
        contact = self.get_latest_contact()
        self.assertRedirects(response, person_url(contact.key))

    def test_contact_deleting(self):
        contact = self.mkcontact()
        person_url = reverse('contacts:person', kwargs={
            'person_key': contact.key,
{'fraction_non_alphanumeric': 0.10900473933649289, 'fraction_numerical': 0.011848341232227487, 'mean_word_length': 3.3321114369501466, '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': '5132609', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1784, 'n_words': 352}