from django.test import TestCase, Client
from django.core.urlresolvers import reverse

class ViewsTest(TestCase):
    """
    Check basic context of indexes' views.
    """
    def setUp(self):
        self.client = Client()

    def test_index_names(self):
        """
        Tests index_names context.
        """
        response = self.client.get(reverse("meps:index_names"))
        self.failUnlessEqual(len(response.context['meps']), 1194)
        self.assertTrue(u"ECR" in [i.group for i in response.context['meps']])
        a = response.context['meps'].all()[:2]
        self.assertTrue(a[0].last[0] < a[1].last[1])

    def test_index_groups(self):
        """
        Tests index_groups context.
        """
        response = self.client.get(reverse("meps:index_groups"))
        self.failUnlessEqual(len(response.context['groups']), 12)
        self.failUnlessEqual(repr(response.context['groups'][0]), "{'count': 54, 'code': u'ECR', 'name': u'Conservateurs et R\\xe9formistes europ\\xe9ens'}")

    def test_index_countries(self):
        """
        Tests index_countries context.
        """
        response = self.client.get(reverse("meps:index_countries"))
        self.failUnlessEqual(len(response.context['countries']), 27)
        self.failUnlessEqual(repr(response.context['countries'][0]), "{'count': 141, 'code': u'DE', 'name': u'Allemagne'}")

    def test_index_by_country(self):
        """
        Tests index_by_country context.
        """
        response = self.client.get(reverse("meps:index_by_country", args=('DE',)))
        self.failUnlessEqual(len(response.context['meps']), 141)
        self.failUnlessEqual(response.context['meps'].first().group, u"PPE")
        self.failUnlessEqual(response.context['meps'].first().first, u"Albert")

    def test_index_by_group(self):
        """
        Tests index_by_group context.
        """
        response = self.client.get(reverse("meps:index_by_group", args=('ALDE',)))
        self.failUnlessEqual(len(response.context['meps']), 144)
        self.failUnlessEqual(response.context['meps'].first().group, u"ALDE")
        self.failUnlessEqual(response.context['meps'].first().first, u"Adina-Ioana")

    def test_mep(self):
        """
        Tests mep context.
        """
        response = self.client.get(reverse("meps:mep", args=('AlbertDess',)))
        self.failUnlessEqual(repr(response.context['mep']['cv']['position'][-1]), 'u"M\\xe9daill\\xe9 de l\'ordre bavarois du M\\xe9rite (2007)."')
        self.failUnlessEqual(str(response.context['mep']['contact']['address'][0]['street']), '60, rue Wiertz')
        self.failUnlessEqual(repr(response.context['positions']), "[]")
        self.failUnlessEqual(repr(response.context['visible_count']), "0")

    def test_js_functionnal_test(self):
        """
        Test the functionnal test that allow to make the js fail
        """
        from django.conf import settings
        settings.DEBUG = True
        response = self.client.get(reverse("meps:mep_addposition", args=('AlbertDess',)), HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.failUnlessEqual(response.content, '{"success": true}')

    def test_js_functionnal_test_make_fail(self):
        """
        Test the functionnal test that allow to make the js fail
        """
        from django.conf import settings
        settings.DEBUG = True
        response = self.client.get(reverse("meps:mep_addposition", args=('AlbertDess',)), {'text' : 'fail'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.failUnlessEqual(response.content, '{"success": false}')

@@ -916,37 +916,35 @@
 , %22%7B
-'count': 54, 'code': u'ECR'
+u'value': %7Bu'count': 268
 , 
+u
 'nam
@@ -953,59 +953,107 @@
 : u'
-Conservateurs et R%5C%5Cxe9formistes europ
+Groupe du Parti Populaire Europ%5C%5Cxe9en (D%5C%5Cxe9mocrates-Chr
 %5C%5Cxe9
+ti
 ens
-'%7D%22)%0A
+)'%7D, u'key': u'PPE'%7D%22)%0A        
 %0A   
@@ -1356,16 +1356,28 @@
 %5B0%5D), %22%7B
+u'value': %7Bu
 'count':
@@ -1386,23 +1386,9 @@
 41, 
-'code': u'DE', 
+u
 'nam
@@ -1404,16 +1404,32 @@
 emagne'%7D
+, u'key': u'DE'%7D
 %22)%0A%0A    

make two tests pass again after structure changes