def test__parse_response(self):

        xml = '''<S:Envelope
                xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">
                <S:Body>
                <ns2:consultaCEPResponse
                xmlns:ns2=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">
                    <return>
                        <bairro>Asa Norte</bairro>
                        <cep>70002900</cep>
                        <cidade>Brasília</cidade>
                        <complemento/>
                        <complemento2/>
                        <end>SBN Quadra 1 Bloco A</end>
                        <id>0</id>
                        <uf>DF</uf>
                    </return>
                </ns2:consultaCEPResponse>
                </S:Body>
                </S:Envelope>'''.replace('\n', '')

        response = Correios._parse_response(xml)

        self.assertEqual(response['rua'], 'SBN Quadra 1 Bloco A')
        self.assertEqual(response['bairro'], 'Asa Norte')
        self.assertEqual(response['cidade'], 'Brasília')
        self.assertEqual(response['uf'], 'DF')
        self.assertEqual(response['complemento'], '')
        self.assertEqual(response['outro'], '')
    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')

        self.assertRaises(CorreiosCEPInvalidCEPException,
                          Correios.get_cep, 37503003)

        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')
Exemple #3
0
    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')

        self.assertRaises(CorreiosCEPInvalidCEPException, Correios.get_cep,
                          37503003)

        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__mount_request(self):

        xml = Correios.HEADER
        xml += '<cli:consultaCEP>'
        xml += '<cep>%s</cep>' % '37503005'
        xml += '</cli:consultaCEP>'
        xml += Correios.FOOTER

        self.assertEqual(xml, Correios._mount_request('37503005'))
Exemple #5
0
    def test__mount_request(self):

        xml = Correios.HEADER
        xml += '<cli:consultaCEP>'
        xml += '<cep>%s</cep>' % '37503005'
        xml += '</cli:consultaCEP>'
        xml += Correios.FOOTER

        self.assertEqual(xml, Correios._mount_request('37503005'))
    def test__parse_error(self):

        xml = '''<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body><soap:Fault>
        <faultcode>soap:Server</faultcode>
        <faultstring>BUSCA DEFINIDA COMO EXATA, 0 CEP DEVE TER 8 DIGITOS</faultstring>
        <detail><ns2:SigepClienteException xmlns:ns2="http://cliente.bean.master.sigep.bsb.correios.com.br/">
        BUSCA DEFINIDA COMO EXATA, 0 CEP DEVE TER 8 DIGITOS
        </ns2:SigepClienteException></detail></soap:Fault>
        </soap:Body></soap:Envelope>'''.replace('\n', '')

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

        address = {
            'bairro': 'Santo Antônio',
            'cidade': 'Itajubá',
            'complemento': '',
            'outro': '- até 214/215',
            'rua': 'Rua Geraldino Campista',
            'uf': 'MG',
        }

        self.assertDictEqual(address, Correios.get_cep('37.503-130'))

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

        self.assertRaises(CorreiosCEPInvalidCEPException,
                          Correios.get_cep, '00000-000')

        self.assertRaises(CorreiosCEPInvalidCEPException,
                          Correios.get_cep, 37503003)
Exemple #8
0
    def test__parse_error(self):

        fault = Correios._parse_error(self.response_xml_error)
        self.assertEqual(
            fault, 'BUSCA DEFINIDA COMO EXATA, '
            '0 CEP DEVE TER 8 DIGITOS')
Exemple #9
0
    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, 'BUSCA DEFINIDA COMO EXATA, '
                                '0 CEP DEVE TER 8 DIGITOS')
    def test__parse_response(self):

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