import json
import os
import unittest

from bs4 import BeautifulSoup

from django.test import Client

from pdl import views
from pdl.models import Proyecto


class SimpleTest(unittest.TestCase):
    def setUp(self):
        this_folder = os.path.abspath(os.path.dirname(__file__))
        dummy_db_json = os.path.join(this_folder, 'dummy_db.json')
        self.dummy_items = json.loads(open(dummy_db_json, 'r').read())
        self.maxDiff = None

    def test_index(self):
        c = Client()
        response = c.get('/')
        soup = BeautifulSoup(response.content)
        result = soup.find_all('h1')[0].get_text()
        self.assertEqual('Proyectos de Ley', result)

    def test_get_last_items(self):
        for i in self.dummy_items:
            b = Proyecto(**i)
            b.save()

        expected = dict(codigo="03774")
        item = views.get_last_items()[0]
        result = dict(codigo=item.codigo)
        self.assertEqual(expected, result)

    def test_prettify_item(self):
        this_folder = os.path.abspath(os.path.dirname(__file__))
        prettified_file = os.path.join(this_folder, 'prettified_03774.txt')
        with open(prettified_file, "r") as f:
            prettified_item = f.read()
        item = self.dummy_items[0]
        result = views.prettify_item(item)
        self.assertEqual(prettified_item, result)

    def test_hiperlink_congre(self):
        expected = "<a href='/congresista/dammert_ego_aguirre/' title='ver " \
                   "todos sus proyectos'>Dammert Ego Aguirre, Manuel " \
                   "Enrique Ernesto</a>;\n" \
                   "<a href='/congresista/lescano_ancieta_yonhy/' " \
                   "title='ver todos sus proyectos'>Lescano Ancieta, " \
                   "Yonhy</a>"
        congresistas = "Dammert Ego Aguirre, Manuel Enrique Ernesto; " \
                       "Lescano Ancieta, Yonhy"
        result = views.hiperlink_congre(congresistas)
        self.assertEqual(expected, result)

    def test_convert_name_to_slug(self):
        name = 'Eguren Neuenschwander, Juan Carlos'
        expected = 'eguren_neuenschwander_juan/'
        result = views.convert_name_to_slug(name)
        self.assertEqual(expected, result)
@@ -15,16 +15,26 @@
 port os%0A
+import re%0A
 import u
@@ -41,16 +41,16 @@
 nittest%0A
-
 %0Afrom bs
@@ -103,16 +103,49 @@
 t Client
+%0Afrom django.test import TestCase
 %0A%0Afrom p
@@ -215,17 +215,8 @@
 est(
-unittest.
 Test
@@ -731,32 +731,98 @@
 st_items(self):%0A
+        # TODO make sure we are not using the production database%0A
         for i in
@@ -915,28 +915,15 @@
 d = 
-dict(codigo=%22
+'
 03774
-%22)
+'
 %0A   
@@ -981,32 +981,68 @@
 t = 
-dict(codigo=item.codigo)
+re.search(%22%3Cb%3E(%5B0-9%5D%7B5%7D)/%5B0-9%5D%7B4%7D-CR%3C/b%3E%22, item).groups()%5B0%5D
 %0A   
@@ -1342,16 +1342,16 @@
 .read()%0A
-
         
@@ -1376,16 +1376,190 @@
 items%5B0%5D
+%0A%0A        # save it to test database%0A        b = Proyecto(**item)%0A        b.save()%0A        # now get it as QuerySet object%0A        item = Proyecto.objects.get(codigo='03774')
 %0A       

fix tests when using a Queryset object