<commit_before>from django.test import TestCase
from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

from bellman.groups.commands import announce
from bellman.groups.models import Group, Person
from bellman.models import SlackAccount

class CommandTestCase(TestCase):

    default_user_name = 'username'
    default_team_id = 'team_id'
    default_user_id = 'user_id'

    def get_user_account(self, username=None):
        user, _ = User.objects.get_or_create(
            username=(username or self.default_user_name))
        user.is_active = True
        user.save()
        return user

    def get_slack_account(self, username=None, team_id=None):
        user = self.get_user_account(username)
        slackaccount, _ = SlackAccount.objects.get_or_create(
            user=user, team_id=(team_id or self.default_team_id))
        return slackaccount

    def assertCommandResponse(self, command_str, expected_response,
                              team_id=None, user_id=None, token=None):
        team_id = team_id or self.default_team_id
        user_id = user_id or self.default_user_id
        token = token or settings.SLACK_TOKEN
        command, _, text = command_str.partition(' ')
        parameters = {
            'token': token,
            'command': command,
            'text': text,
            'team_id': team_id,
            'user_id': user_id,
        }
        response = self.client.post(reverse('dispatcher'), parameters)
        self.assertEqual(response.json(), {
            'text': expected_response,
        })


class GroupsCommandTestCase(CommandTestCase):

    def setUp(self):
        self.slackaccount = self.get_slack_account()

    def test_list(self):
        Group.objects.create(
            group_name='Group Name', slackaccount=self.slackaccount)
        self.assertCommandResponse('/announce list', '\n'.join([
            'Groups:',
            '- Group Name',
        ]))

    def test_empty_list(self):
        self.assertCommandResponse('/announce list', 'No groups exist')

    def test_list_member(self):
        group = Group.objects.create(
            group_name='group-name', slackaccount=self.slackaccount)
        person = Person.objects.create(
            person_id=self.default_user_id, slackaccount=self.slackaccount)
        person.groups.add(group)
        self.assertCommandResponse('/announce list', '\n'.join([
            'Groups:',
            '- group-name (member)',
        ]))

    def test_members(self):
        group = Group.objects.create(
            group_name='my-group', slackaccount=self.slackaccount)
        person = Person.objects.create(
            person_name='<NAME>',
            person_id=self.default_user_id, slackaccount=self.slackaccount)
        person.groups.add(group)
        self.assertCommandResponse(
            '/announce members my-group',
            'People in my-group: Example Person')

    def test_create(self):
        self.assertEqual(
            Group.objects.filter(group_name='my-group',
                                 slackaccount=self.slackaccount).count(), 0)
        self.assertCommandResponse(
            '/announce create my-group',
            'The group my-group has been created.')
        self.assertEqual(
            Group.objects.filter(group_name='my-group',
                                 slackaccount=self.slackaccount).count(), 1)

    def test_join_nonexistent(self):
        self.assertCommandResponse(
            '/announce join my-group',
            'The group my-group does not exist.')

    def test_join(self):
        group = Group.objects.create(
            group_name='my-group', slackaccount=self.slackaccount)
        self.assertCommandResponse(
            '/announce join my-group',
            'You\'ve been added to my-group.')

    def test_leave_nonexistent(self):
        self.assertCommandResponse(
            '/announce leave my-group',
            'The group my-group does not exist.')

    def test_leave(self):
        group = Group.objects.create(
            group_name='my-group', slackaccount=self.slackaccount)
        person = Person.objects.create(
            person_id=self.default_user_id, slackaccount=self.slackaccount)
        person.groups.add(group)
        self.assertCommandResponse(
            '/announce leave my-group',
            'You\'ve been removed from my-group.')<commit_msg>flake8 fixes<commit_after>from django.test import TestCase
from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

from bellman.groups.models import Group, Person
from bellman.models import SlackAccount


class CommandTestCase(TestCase):

    default_user_name = 'username'
    default_team_id = 'team_id'
    default_user_id = 'user_id'

    def get_user_account(self, username=None):
        user, _ = User.objects.get_or_create(
            username=(username or self.default_user_name))
        user.is_active = True
        user.save()
        return user

    def get_slack_account(self, username=None, team_id=None):
        user = self.get_user_account(username)
        slackaccount, _ = SlackAccount.objects.get_or_create(
            user=user, team_id=(team_id or self.default_team_id))
        return slackaccount

    def assertCommandResponse(self, command_str, expected_response,
                              team_id=None, user_id=None, token=None):
        team_id = team_id or self.default_team_id
        user_id = user_id or self.default_user_id
        token = token or settings.SLACK_TOKEN
        command, _, text = command_str.partition(' ')
        parameters = {
            'token': token,
            'command': command,
            'text': text,
            'team_id': team_id,
            'user_id': user_id,
        }
        response = self.client.post(reverse('dispatcher'), parameters)
        self.assertEqual(response.json(), {
            'text': expected_response,
        })


class GroupsCommandTestCase(CommandTestCase):

    def setUp(self):
        self.slackaccount = self.get_slack_account()

    def test_list(self):
        Group.objects.create(
            group_name='Group Name', slackaccount=self.slackaccount)
        self.assertCommandResponse('/announce list', '\n'.join([
            'Groups:',
            '- Group Name',
        ]))

    def test_empty_list(self):
        self.assertCommandResponse('/announce list', 'No groups exist')

    def test_list_member(self):
        group = Group.objects.create(
            group_name='group-name', slackaccount=self.slackaccount)
        person = Person.objects.create(
            person_id=self.default_user_id, slackaccount=self.slackaccount)
        person.groups.add(group)
        self.assertCommandResponse('/announce list', '\n'.join([
            'Groups:',
            '- group-name (member)',
        ]))

    def test_members(self):
        group = Group.objects.create(
            group_name='my-group', slackaccount=self.slackaccount)
        person = Person.objects.create(
            person_name='Example Person',
            person_id=self.default_user_id, slackaccount=self.slackaccount)
        person.groups.add(group)
        self.assertCommandResponse(
            '/announce members my-group',
            'People in my-group: Example Person')

    def test_create(self):
        self.assertEqual(
            Group.objects.filter(group_name='my-group',
                                 slackaccount=self.slackaccount).count(), 0)
        self.assertCommandResponse(
            '/announce create my-group',
            'The group my-group has been created.')
        self.assertEqual(
            Group.objects.filter(group_name='my-group',
                                 slackaccount=self.slackaccount).count(), 1)

    def test_join_nonexistent(self):
        self.assertCommandResponse(
            '/announce join my-group',
            'The group my-group does not exist.')

    def test_join(self):
        Group.objects.create(
            group_name='my-group', slackaccount=self.slackaccount)
        self.assertCommandResponse(
            '/announce join my-group',
            'You\'ve been added to my-group.')

    def test_leave_nonexistent(self):
        self.assertCommandResponse(
            '/announce leave my-group',
            'The group my-group does not exist.')

    def test_leave(self):
        group = Group.objects.create(
            group_name='my-group', slackaccount=self.slackaccount)
        person = Person.objects.create(
            person_id=self.default_user_id, slackaccount=self.slackaccount)
        person.groups.add(group)
        self.assertCommandResponse(
            '/announce leave my-group',
            'You\'ve been removed from my-group.')
{'fraction_non_alphanumeric': 0.10263664139413828, 'fraction_numerical': 0.0005658028742786014, 'mean_word_length': 2.3350943396226413, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '6422574', 'n_tokens_mistral': 2480, 'n_tokens_neox': 2423, 'n_words': 486}