# -*- coding: utf-8 -*-

import requests
from unittest import mock
from unittest import TestCase
from jinja2 import Environment, FileSystemLoader

from pycep_correios.correios import Correios
from pycep_correios.correios_exceptions import \
    CorreiosCEPInvalidCEPException
from pycep_correios.correios_exceptions import \
    CorreiosTimeOutException
from pycep_correios.correios_exceptions import \
    CorreiosCEPTooManyRedirectsException
from pycep_correios.correios_exceptions import \
    CorreiosCEPConnectionErrorException


class TestCorreios(TestCase):

    def setUp(self):

        address = {
            'bairro': 'Asa Norte',
            'cep': '70002900',
            'cidade': 'Brasília',
            'end': 'SBN Quadra 1 Bloco A',
            'id': '0',
            'uf': 'DF',
        }

        self.expected_address = {
            'bairro': address['bairro'],
            'cidade': address['cidade'],
            'complemento': '',
            'outro': '',
            'rua': address['end'],
            'uf': address['uf'],
        }

        self.env = Environment(loader=FileSystemLoader('test/templates'))

        template = self.env.get_template('response.xml')
        xml = template.render(**address)
        self.response_xml = (xml.replace('\n', '')).replace('\t', '')

        template = self.env.get_template('response_error.xml')
        xml = template.render()
        self.response_xml_error = (xml.replace('\n', '')).replace('\t', '')

    @mock.patch('pycep_correios.correios.requests.post')
    def test_get_cep(self, mock_api_call):
        mock_api_call.return_value = mock.MagicMock(status_code=200,
                                                    ok=True,
                                                    text=self.response_xml)

        self.assertDictEqual(Correios.get_cep('70002900'),
                             self.expected_address)

        mock_api_call.return_value = mock.MagicMock(ok=False,
                                                    text=self.response_xml_error)

        self.assertRaises(CorreiosCEPInvalidCEPException,
                          Correios.get_cep, '1232710')

        mock_api_call.side_effect = requests.exceptions.Timeout()
        self.assertRaises(CorreiosTimeOutException,
                          Correios.get_cep, '12345-500')

        mock_api_call.side_effect = requests.exceptions.Timeout()
        self.assertRaises(CorreiosTimeOutException,
                          Correios.get_cep, '12345-500')

        mock_api_call.side_effect = requests.exceptions.TooManyRedirects()
        self.assertRaises(CorreiosCEPTooManyRedirectsException,
                          Correios.get_cep, '12345-500')

        mock_api_call.side_effect = requests.exceptions.ConnectionError()
        self.assertRaises(CorreiosCEPConnectionErrorException,
                          Correios.get_cep, '12345-500')

    def test__format_cep(self):
        self.assertRaises(CorreiosCEPInvalidCEPException,
                          Correios._format_cep, 37503003)

    def test__mount_request(self):
        cep = '37503005'
        template = self.env.get_template('consultacep.xml')
        xml = template.render(cep=cep)
        xml = (xml.replace('\n', '')).replace('\t', '')

        self.assertEqual(xml, Correios._mount_request(cep=cep))

    def test__parse_response(self):
        response = Correios._parse_response(self.response_xml)
        self.assertDictEqual(response, self.expected_address)

    def test__parse_error(self):
        fault = Correios._parse_error(self.response_xml_error)
        self.assertEqual(fault.strip(), 'BUSCA DEFINIDA COMO EXATA, '
                                        '0 CEP DEVE TER 8 DIGITOS')

@@ -1569,24 +1569,25 @@
 _call):%0A
+%0A
         
 mock_api
@@ -1582,109 +1582,110 @@
     
-mock_api_call.return_value = mock.MagicMock(status_code=200,%0A                                        
+# Aqui realizamos consulta com o CEP correto%0A        param = %7B%0A            'text': self.response_xml,%0A
     
@@ -1692,19 +1692,22 @@
         
+'
 ok
-=
+': 
 True,%0A  
@@ -1720,40 +1720,36 @@
     
-                        
+'status_code': 200,%0A
         
     
@@ -1740,32 +1740,35 @@
 00,%0A        
+%7D%0A%0A
         
 text=self.re
@@ -1755,38 +1755,67 @@
         
-text=self.response_xml
+mock_api_call.return_value = mock.MagicMock(**param
 )%0A%0A     
@@ -1933,61 +1933,77 @@
     
-mock_api_call.return_value = mock.MagicMock(ok=False,
+# Aqui realizamos consultas que de alguma forma retornam mensagens de
 %0A   
@@ -2003,33 +2003,39 @@
  de%0A        
- 
+# erro%0A
         
             
@@ -2014,32 +2014,42 @@
  # erro%0A        
+param = %7B%0A
                 
@@ -2048,59 +2048,136 @@
     
-                       text=self.response_xml_error
+'text': self.response_xml_error,%0A            'ok': False,%0A        %7D%0A%0A        mock_api_call.return_value = mock.MagicMock(**param
 )%0A%0A 

Ajuste no codigo de requisição