<commit_before>from django.test import TestCase
from django.test.client import Client
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User, AnonymousUser
from django.contrib.sites.models import Site
from actstream import follow,action
from actstream.models import Action
from knesset.mks.models import Member, Party, Membership
from knesset.mks.views import MemberListView
from knesset.laws.models import Law,Bill,PrivateProposal,Vote,VoteAction
from knesset.committees.models import CommitteeMeeting,Committee
from knesset.utils import RequestFactory
import datetime
import feedparser
just_id = lambda x: x.id

class MemberViewsTest(TestCase):

    def setUp(self):
        self.party_1 = Party.objects.create(name='party 1')
        self.party_2 = Party.objects.create(name='party 2')
        self.mk_1 = Member.objects.create(name='mk_1',
                                          start_date=datetime.date(2010,1,1),
                                          current_party=self.party_1)
        self.mk_2 = Member.objects.create(name='mk_2',
                                          start_date=datetime.date(2010,1,1),
                                           current_party=self.party_1)
        self.jacob = User.objects.create_user('jacob', '<EMAIL>',
                                              'JKM')

        self.committee_1 = Committee.objects.create(name='c1')
        self.meeting_1 = self.committee_1.meetings.create(date=datetime.date.today()-datetime.timedelta(1),
                                 protocol_text='jacob:\nI am a perfectionist\nadrian:\nI have a deadline')
        self.meeting_2 = self.committee_1.meetings.create(date=datetime.date.today()-datetime.timedelta(2),
                                 protocol_text='adrian:\nYou are a perfectionist\njacob:\nYou have a deadline')
        self.law = Law.objects.create(title='law 1')
        self.pp = PrivateProposal.objects.create(title='private proposal 1', date=datetime.date.today()-datetime.timedelta(3))
        self.pp.proposers.add(self.mk_1)
        self.bill_1 = Bill.objects.create(stage='1', title='bill 1', law=self.law)
        self.bill_1.proposals.add(self.pp)
        self.bill_1.proposers.add(self.mk_1)
        self.meeting_1.mks_attended.add(self.mk_1)
        self.meeting_1.save()
        self.meeting_2.mks_attended.add(self.mk_1)
        self.meeting_2.save()
        self.vote = Vote.objects.create(title='vote 1',time=datetime.datetime.now())
        self.vote_action = VoteAction.objects.create(member=self.mk_1, vote=self.vote, type='for')

        self.domain = 'http://' + Site.objects.get_current().domain

    def testMemberList(self):
        res = self.client.get(reverse('member-list'))
        self.assertEqual(res.status_code, 200)
        self.assertTemplateUsed(res, 'mks/member_list_with_bars.html')
        object_list = res.context['object_list']
        self.assertEqual(map(just_id, object_list), 
                         [ self.mk_1.id, self.mk_2.id, ])

    def testMemberDetail(self):
        res = self.client.get(reverse('member-detail', 
                                      kwargs={'object_id': self.mk_1.id}))
        self.assertTemplateUsed(res,
                                'mks/member_detail.html')
        self.assertEqual(res.context['object'].id, self.mk_1.id)

    def testPartyList(self):
        res = self.client.get(reverse('party-list'))
        self.assertEqual(res.status_code, 200)
        self.assertTemplateUsed(res, 'mks/party_list.html')
        object_list = res.context['object_list']
        self.assertEqual(map(just_id, object_list), 
                         [ self.party_1.id, self.party_2.id, ])

    def testPartyDetail(self):
        res = self.client.get(reverse('party-detail', 
                              kwargs={'object_id': self.party_1.id}))
        self.assertTemplateUsed(res, 'mks/party_detail.html')
        self.assertEqual(res.context['object'].id, self.party_1.id)
<commit_msg>adding some tests to member search. exact match test fails with two results.<commit_after>from django.test import TestCase
from django.test.client import Client
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User, AnonymousUser
from django.contrib.sites.models import Site
from actstream import follow,action
from actstream.models import Action
from knesset.mks.models import Member, Party, Membership
from knesset.mks.views import MemberListView
from knesset.laws.models import Law,Bill,PrivateProposal,Vote,VoteAction
from knesset.committees.models import CommitteeMeeting,Committee
from knesset.utils import RequestFactory
import datetime
import feedparser
just_id = lambda x: x.id

class MemberViewsTest(TestCase):

    def setUp(self):
        self.party_1 = Party.objects.create(name='party 1')
        self.party_2 = Party.objects.create(name='party 2')
        self.mk_1 = Member.objects.create(name='mk_1',
                                          start_date=datetime.date(2010,1,1),
                                          current_party=self.party_1)
        self.mk_2 = Member.objects.create(name='mk_2',
                                          start_date=datetime.date(2010,1,1),
                                           current_party=self.party_1)
        self.jacob = User.objects.create_user('jacob', '<EMAIL>',
                                              'JKM')

        self.committee_1 = Committee.objects.create(name='c1')
        self.meeting_1 = self.committee_1.meetings.create(date=datetime.date.today()-datetime.timedelta(1),
                                 protocol_text='jacob:\nI am a perfectionist\nadrian:\nI have a deadline')
        self.meeting_2 = self.committee_1.meetings.create(date=datetime.date.today()-datetime.timedelta(2),
                                 protocol_text='adrian:\nYou are a perfectionist\njacob:\nYou have a deadline')
        self.law = Law.objects.create(title='law 1')
        self.pp = PrivateProposal.objects.create(title='private proposal 1', date=datetime.date.today()-datetime.timedelta(3))
        self.pp.proposers.add(self.mk_1)
        self.bill_1 = Bill.objects.create(stage='1', title='bill 1', law=self.law)
        self.bill_1.proposals.add(self.pp)
        self.bill_1.proposers.add(self.mk_1)
        self.meeting_1.mks_attended.add(self.mk_1)
        self.meeting_1.save()
        self.meeting_2.mks_attended.add(self.mk_1)
        self.meeting_2.save()
        self.vote = Vote.objects.create(title='vote 1',time=datetime.datetime.now())
        self.vote_action = VoteAction.objects.create(member=self.mk_1, vote=self.vote, type='for')

        self.domain = 'http://' + Site.objects.get_current().domain

    def testMemberList(self):
        res = self.client.get(reverse('member-list'))
        self.assertEqual(res.status_code, 200)
        self.assertTemplateUsed(res, 'mks/member_list_with_bars.html')
        object_list = res.context['object_list']
        self.assertEqual(map(just_id, object_list), 
                         [ self.mk_1.id, self.mk_2.id, ])

    def testMemberDetail(self):
        res = self.client.get(reverse('member-detail', 
                                      kwargs={'object_id': self.mk_1.id}))
        self.assertTemplateUsed(res,
                                'mks/member_detail.html')
        self.assertEqual(res.context['object'].id, self.mk_1.id)

    def testMemberSearch(self):
        import json

        to_set = lambda x: set(map(lambda y: hash(tuple(sorted(y.items()))), x))

        res = self.client.get(reverse('member-by-name'),
                                      {'q': 'mk_'},
                                      HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        p = json.loads(res.content)['possible']
        self.assertEqual(to_set(p),
                         to_set(\
                            [{u'id': 1, u'name': u'mk_1', u'url': u'/member/1/'},
                             {u'id': 2, u'name': u'mk_2', u'url': u'/member/2/'},
                        ]))

        res = self.client.get(reverse('member-by-name'),
                                      {'q': 'mk_1'},
                                      HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        p = json.loads(res.content)['possible']
        self.assertEqual(p,
                         [{u'id': 1, u'name': u'mk_1', u'url': u'/member/1/'},])
    def testPartyList(self):
        res = self.client.get(reverse('party-list'))
        self.assertEqual(res.status_code, 200)
        self.assertTemplateUsed(res, 'mks/party_list.html')
        object_list = res.context['object_list']
        self.assertEqual(map(just_id, object_list), 
                         [ self.party_1.id, self.party_2.id, ])

    def testPartyDetail(self):
        res = self.client.get(reverse('party-detail', 
                              kwargs={'object_id': self.party_1.id}))
        self.assertTemplateUsed(res, 'mks/party_detail.html')
        self.assertEqual(res.context['object'].id, self.party_1.id)

{'fraction_non_alphanumeric': 0.1237410071942446, 'fraction_numerical': 0.014831211953514112, 'mean_word_length': 2.319617927994122, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3361836', 'n_tokens_mistral': 2789, 'n_tokens_neox': 2657, 'n_words': 416}