<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

import unittest

import jinja2

from pycep_correios import consultar_cep, formatar_cep, parser, validar_cep
from pycep_correios.excecoes import CEPInvalido

try:
    from unittest import mock
except ImportError:
    import mock


class TestCorreios(unittest.TestCase):

    def setUp(self):

        self.expected_address = {
            'bairro': 'Santo Antônio',
            'cep': '37503130',
            'cidade': 'Itajubá',
            'end': 'Rua Geraldino Campista',
            'id': '0',
            'uf': 'MG',
            'complemento': '',
            'complemento2': '- até 214/215',
        }

        loader = jinja2.PackageLoader('tests', 'templates')
        self.env = jinja2.Environment(loader=loader)

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

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

    @mock.patch('requests.post')
    def test_consultar_cep(self, mock_api_call):

        # Criamos uma requisicao que sera bem sucedida
        param = {
            'text': self.response_xml,
            'ok': True,
            'status_code': 200,
        }

        mock_api_call.return_value = mock.MagicMock(**param)

        self.assertDictEqual(consultar_cep('70002900'), self.expected_address)

        # Aqui, construímos uma requisição que retornará erro
        param = {
            'text': self.response_xml_error,
            'ok': False,
        }

        mock_api_call.return_value = mock.MagicMock(**param)
        self.assertRaises(CEPInvalido, consultar_cep, '1232710')

        mock_api_call.return_value = mock.MagicMock(**param)
        self.assertRaises(CEPInvalido, consultar_cep, '00000000')

    def test_formatar_cep(self):
        self.assertRaises(ValueError, formatar_cep, 37503003)
        self.assertRaises(ValueError, formatar_cep, '')
        self.assertRaises(ValueError, formatar_cep, None)
        self.assertRaises(ValueError, formatar_cep, False)
        self.assertRaises(ValueError, formatar_cep, True)
        self.assertEqual(formatar_cep('37.503-003'), '37503003')
        self.assertEqual(formatar_cep('   37.503-003'), '37503003')
        self.assertEqual(formatar_cep('37 503-003'), '37503003')
        self.assertEqual(formatar_cep('37.503&003saasd'), '37503003')
        self.assertEqual(formatar_cep('\n \r 37.503-003'), '37503003')
        self.assertEqual(formatar_cep('\n \r 37.503-003'), '37503003')
        # ponto e virgula
        self.assertEqual(formatar_cep('37.503-003;'), '37503003')
        # Unicode Greek Question Mark
        self.assertEqual(formatar_cep(u'37.503-003;'), '37503003')

    def test_validar_cep(self):
        self.assertRaises(ValueError, validar_cep, 37503003)
        self.assertRaises(ValueError, validar_cep, '')
        self.assertIs(validar_cep('37.503-003'), True)
        self.assertIs(validar_cep('37.503-00'), False)
        self.assertIs(validar_cep('   37.503-003'), True)
        self.assertIs(validar_cep('37.503&003saasd'), True)

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

        self.assertEqual(xml, parser.monta_requisicao(cep='37503005'))

    def test_parse_resposta(self):
        response = parser.parse_resposta(self.response_xml)
        self.assertDictEqual(response, self.expected_address)

    def test_parse_resposta_com_erro(self):
        fault = parser.parse_resposta_com_erro(self.response_xml_error)
        self.assertEqual(fault.strip(), 'BUSCA DEFINIDA COMO EXATA, '
                                        '0 CEP DEVE TER 8 DIGITOS')
<commit_msg>[REF] Adicionado #noqa no texto da mensagem de erro<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

import unittest

import jinja2

from pycep_correios import consultar_cep, formatar_cep, parser, validar_cep
from pycep_correios.excecoes import CEPInvalido

try:
    from unittest import mock
except ImportError:
    import mock


class TestCorreios(unittest.TestCase):

    def setUp(self):

        self.expected_address = {
            'bairro': 'Santo Antônio',
            'cep': '37503130',
            'cidade': 'Itajubá',
            'end': 'Rua Geraldino Campista',
            'id': '0',
            'uf': 'MG',
            'complemento': '',
            'complemento2': '- até 214/215',
        }

        loader = jinja2.PackageLoader('tests', 'templates')
        self.env = jinja2.Environment(loader=loader)

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

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

    @mock.patch('requests.post')
    def test_consultar_cep(self, mock_api_call):

        # Criamos uma requisicao que sera bem sucedida
        param = {
            'text': self.response_xml,
            'ok': True,
            'status_code': 200,
        }

        mock_api_call.return_value = mock.MagicMock(**param)

        self.assertDictEqual(consultar_cep('70002900'), self.expected_address)

        # Aqui, construímos uma requisição que retornará erro
        param = {
            'text': self.response_xml_error,
            'ok': False,
        }

        mock_api_call.return_value = mock.MagicMock(**param)
        self.assertRaises(CEPInvalido, consultar_cep, '1232710')

        mock_api_call.return_value = mock.MagicMock(**param)
        self.assertRaises(CEPInvalido, consultar_cep, '00000000')

    def test_formatar_cep(self):
        self.assertRaises(ValueError, formatar_cep, 37503003)
        self.assertRaises(ValueError, formatar_cep, '')
        self.assertRaises(ValueError, formatar_cep, None)
        self.assertRaises(ValueError, formatar_cep, False)
        self.assertRaises(ValueError, formatar_cep, True)
        self.assertEqual(formatar_cep('37.503-003'), '37503003')
        self.assertEqual(formatar_cep('   37.503-003'), '37503003')
        self.assertEqual(formatar_cep('37 503-003'), '37503003')
        self.assertEqual(formatar_cep('37.503&003saasd'), '37503003')
        self.assertEqual(formatar_cep('\n \r 37.503-003'), '37503003')
        self.assertEqual(formatar_cep('\n \r 37.503-003'), '37503003')
        # ponto e virgula
        self.assertEqual(formatar_cep('37.503-003;'), '37503003')
        # Unicode Greek Question Mark
        self.assertEqual(formatar_cep(u'37.503-003;'), '37503003')

    def test_validar_cep(self):
        self.assertRaises(ValueError, validar_cep, 37503003)
        self.assertRaises(ValueError, validar_cep, '')
        self.assertIs(validar_cep('37.503-003'), True)
        self.assertIs(validar_cep('37.503-00'), False)
        self.assertIs(validar_cep('   37.503-003'), True)
        self.assertIs(validar_cep('37.503&003saasd'), True)

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

        self.assertEqual(xml, parser.monta_requisicao(cep='37503005'))

    def test_parse_resposta(self):
        response = parser.parse_resposta(self.response_xml)
        self.assertDictEqual(response, self.expected_address)

    def test_parse_resposta_com_erro(self):
        fault = parser.parse_resposta_com_erro(self.response_xml_error)
        msg = 'BUSCA DEFINIDA COMO EXATA, 0 CEP DEVE TER 8 DIGITOS'
        self.assertEqual(fault.strip(), msg)

{'fraction_non_alphanumeric': 0.13050530255770432, 'fraction_numerical': 0.05963817841547099, 'mean_word_length': 2.927486526212641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 34}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4401888', 'n_tokens_mistral': 2956, 'n_tokens_neox': 2585, 'n_words': 434}