|
| 1 | +"""Test the COT (Comprobante de Operaciones en Tránsito) module. |
| 2 | +
|
| 3 | +This module contains tests for the COT class, which is used to interact with the |
| 4 | +AFIP COT web service. The tests cover various scenarios, including connecting |
| 5 | +to the service, presenting remitos, reading validation responses, and handling |
| 6 | +errors. |
| 7 | +""" |
| 8 | +#!/usr/bin/python |
| 9 | +# -*- coding: utf8 -*- |
| 10 | +# This program is free software; you can redistribute it and/or modify |
| 11 | +# it under the terms of the GNU General Public License as published by the |
| 12 | +# Free Software Foundation; either version 3, or (at your option) any later |
| 13 | +# version. |
| 14 | +# |
| 15 | +# This program is distributed in the hope that it will be useful, but |
| 16 | +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY |
| 17 | +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 18 | +# for more details. |
| 19 | + |
| 20 | +"""Test para cot""" |
| 21 | + |
| 22 | +__author__ = "Mariano Reingart <[email protected]>" |
| 23 | +__copyright__ = "Copyright (C) 2010-2019 Mariano Reingart" |
| 24 | +__license__ = "GPL 3.0" |
| 25 | + |
| 26 | +"""Test the COT (Comprobante de Operaciones en Tránsito) module. |
| 27 | +
|
| 28 | +This module contains tests for the COT class, which is used to interact with the |
| 29 | +AFIP COT web service. The tests cover various scenarios, including connecting |
| 30 | +to the service, presenting remitos, reading validation responses, and handling |
| 31 | +errors. |
| 32 | +""" |
| 33 | + |
| 34 | +import pytest |
| 35 | +from pyafipws.cot import COT, INSTALL_DIR, __version__, HOMO |
| 36 | +from pysimplesoap.simplexml import SimpleXMLElement |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def cot_instance(): |
| 41 | + """Fixture to create a COT instance for testing.""" |
| 42 | + return COT() |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.dontusefix |
| 46 | +def test_conectar_with_error(cot_instance): |
| 47 | + """ |
| 48 | + Test the Conectar method with invalid parameters. |
| 49 | + Expects an exception to be raised. |
| 50 | + """ |
| 51 | + with pytest.raises(Exception): |
| 52 | + cot_instance.Conectar(url="invalid_url", proxy="invalid_proxy") |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.dontusefix |
| 56 | +def test_presentar_remito_with_file_not_found(cot_instance, monkeypatch): |
| 57 | + """ |
| 58 | + Test PresentarRemito method when the file is not found. |
| 59 | + Expects the method to return False and set an appropriate error message. |
| 60 | + """ |
| 61 | + monkeypatch.setattr("os.path.exists", lambda x: False) |
| 62 | + result = cot_instance.PresentarRemito("non_existent_file.txt") |
| 63 | + assert result is False |
| 64 | + assert "Archivo no encontrado" in cot_instance.Excepcion |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.dontusefix |
| 68 | +def test_leer_validacion_remito(cot_instance): |
| 69 | + """ |
| 70 | + Test LeerValidacionRemito method. |
| 71 | + Checks if the method correctly reads and sets remito validation data. |
| 72 | + """ |
| 73 | + cot_instance.remitos = [ |
| 74 | + { |
| 75 | + "NumeroUnico": "123", |
| 76 | + "Procesado": "SI", |
| 77 | + "COT": "COT123", |
| 78 | + "Errores": [("E001", "Error 1")], |
| 79 | + } |
| 80 | + ] |
| 81 | + assert cot_instance.LeerValidacionRemito() is True |
| 82 | + assert cot_instance.NumeroUnico == "123" |
| 83 | + assert cot_instance.Procesado == "SI" |
| 84 | + assert cot_instance.COT == "COT123" |
| 85 | + assert cot_instance.LeerValidacionRemito() is False |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.dontusefix |
| 89 | +def test_leer_error_validacion(cot_instance): |
| 90 | + """ |
| 91 | + Test LeerErrorValidacion method. |
| 92 | + Verifies if the method correctly reads and sets error validation data. |
| 93 | + """ |
| 94 | + cot_instance.errores = [("E001", "Error 1"), ("E002", "Error 2")] |
| 95 | + assert cot_instance.LeerErrorValidacion() is True |
| 96 | + assert cot_instance.CodigoError == "E002" |
| 97 | + assert cot_instance.MensajeError == "Error 2" |
| 98 | + assert cot_instance.LeerErrorValidacion() is True |
| 99 | + assert cot_instance.CodigoError == "E001" |
| 100 | + assert cot_instance.MensajeError == "Error 1" |
| 101 | + assert cot_instance.LeerErrorValidacion() is False |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.dontusefix |
| 105 | +def test_analizar_xml_with_invalid_xml(cot_instance): |
| 106 | + """ |
| 107 | + Test AnalizarXml method with invalid XML. |
| 108 | + Expects the method to return False and set an appropriate error message. |
| 109 | + """ |
| 110 | + assert cot_instance.AnalizarXml("<invalid>") is False |
| 111 | + assert "no element found" in cot_instance.Excepcion |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.dontusefix |
| 115 | +def test_main_function(monkeypatch): |
| 116 | + """ |
| 117 | + Test the main function of the COT module. |
| 118 | + Mocks necessary dependencies and checks if |
| 119 | + the function runs without errors. |
| 120 | + """ |
| 121 | + monkeypatch.setattr( |
| 122 | + "sys.argv", ["cot.py", "test_file.txt", "test_user", "test_password"] |
| 123 | + ) |
| 124 | + monkeypatch.setattr( |
| 125 | + "pyafipws.cot.COT.PresentarRemito", |
| 126 | + lambda self, filename, testing="": True |
| 127 | + ) |
| 128 | + from pyafipws.cot import main |
| 129 | + |
| 130 | + main() |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.dontusefix |
| 134 | +def test_cot_initialization(monkeypatch): |
| 135 | + """ |
| 136 | + Test the initialization and basic functionality of the COT class. |
| 137 | + Mocks WebClient and file operations, |
| 138 | + then checks various attributes and methods. |
| 139 | + """ |
| 140 | + |
| 141 | + def mock_webclient(*args, **kwargs): |
| 142 | + return lambda *a, **k: "<dummy_response></dummy_response>" |
| 143 | + |
| 144 | + monkeypatch.setattr("pyafipws.cot.WebClient", mock_webclient) |
| 145 | + monkeypatch.setattr("builtins.open", lambda *args: None) |
| 146 | + monkeypatch.setattr("os.path.exists", lambda x: True) |
| 147 | + |
| 148 | + cot = COT() |
| 149 | + cot.Usuario = "test_user" |
| 150 | + cot.Password = "test_password" |
| 151 | + |
| 152 | + cot.Conectar() |
| 153 | + assert cot.client is not None |
| 154 | + |
| 155 | + result = cot.PresentarRemito("test_file.txt") |
| 156 | + assert result is True |
| 157 | + assert cot.XmlResponse == "<dummy_response></dummy_response>" |
| 158 | + |
| 159 | + assert cot.LeerErrorValidacion() is False |
| 160 | + assert cot.LeerValidacionRemito() is False |
| 161 | + assert cot.AnalizarXml("<test></test>") is True |
| 162 | + assert cot.ObtenerTagXml("test") is None |
| 163 | + |
| 164 | + assert cot.InstallDir == INSTALL_DIR |
| 165 | + expected_version = ( |
| 166 | + f"{__version__} {'Homologación' if HOMO else ''}".strip() |
| 167 | + ) |
| 168 | + assert cot.Version.strip() == expected_version |
| 169 | + assert set(cot._public_methods_) == { |
| 170 | + "Conectar", |
| 171 | + "PresentarRemito", |
| 172 | + "LeerErrorValidacion", |
| 173 | + "LeerValidacionRemito", |
| 174 | + "AnalizarXml", |
| 175 | + "ObtenerTagXml", |
| 176 | + } |
| 177 | + assert cot._reg_progid_ == "COT" |
| 178 | + assert cot._reg_clsid_ == "{7518B2CF-23E9-4821-BC55-D15966E15620}" |
| 179 | + |
| 180 | + |
| 181 | +@pytest.mark.dontusefix |
| 182 | +def test_presentar_remito_with_different_responses(cot_instance, monkeypatch): |
| 183 | + """ |
| 184 | + Test PresentarRemito method with various XML responses. |
| 185 | + Checks if the method correctly handles different response structures. |
| 186 | + """ |
| 187 | + responses = [ |
| 188 | + "<cot><tipoError>0</tipoError></cot>", |
| 189 | + ( |
| 190 | + "<cot><tipoError>1</tipoError><codigoError>E001</codigoError>" |
| 191 | + "<mensajeError>Test Error</mensajeError></cot>" |
| 192 | + ), |
| 193 | + ( |
| 194 | + "<cot><cuitEmpresa>123456789</cuitEmpresa>" |
| 195 | + "<numeroComprobante>12345</numeroComprobante></cot>" |
| 196 | + ), |
| 197 | + ( |
| 198 | + "<cot><validacionesRemitos><remito><numeroUnico>123</numeroUnico>" |
| 199 | + "<procesado>SI</procesado><cot>COT123</cot></remito>" |
| 200 | + "</validacionesRemitos></cot>" |
| 201 | + ), |
| 202 | + ] |
| 203 | + |
| 204 | + for response in responses: |
| 205 | + monkeypatch.setattr( |
| 206 | + "pyafipws.cot.WebClient", |
| 207 | + lambda *args, **kwargs: lambda *a, **k: response |
| 208 | + ) |
| 209 | + monkeypatch.setattr("builtins.open", lambda *args: None) |
| 210 | + monkeypatch.setattr("os.path.exists", lambda x: True) |
| 211 | + |
| 212 | + result = cot_instance.PresentarRemito("test.txt") |
| 213 | + assert result is False |
| 214 | + cot_instance.AnalizarXml(response) |
| 215 | + |
| 216 | + |
| 217 | +@pytest.mark.dontusefix |
| 218 | +def test_presentar_remito_error_handling(cot_instance, monkeypatch): |
| 219 | + """ |
| 220 | + Test error handling in PresentarRemito method. |
| 221 | + Simulates an exception and checks if it's properly handled. |
| 222 | + """ |
| 223 | + |
| 224 | + def raise_exception(*args, **kwargs): |
| 225 | + raise Exception("Test exception") |
| 226 | + |
| 227 | + monkeypatch.setattr( |
| 228 | + "pyafipws.cot.WebClient", lambda *args, **kwargs: raise_exception |
| 229 | + ) |
| 230 | + monkeypatch.setattr("builtins.open", lambda *args: None) |
| 231 | + monkeypatch.setattr("os.path.exists", lambda x: True) |
| 232 | + |
| 233 | + result = cot_instance.PresentarRemito("test.txt") |
| 234 | + assert result is False |
| 235 | + assert cot_instance.Excepcion != "" |
| 236 | + |
| 237 | + |
| 238 | +@pytest.mark.dontusefix |
| 239 | +def test_obtener_tag_xml(cot_instance): |
| 240 | + """ |
| 241 | + Test ObtenerTagXml method. |
| 242 | + Checks if the method correctly retrieves values from XML tags. |
| 243 | + """ |
| 244 | + xml = ( |
| 245 | + "<root><tag1>value1</tag1><tag2><subtag>value2</subtag></tag2></root>" |
| 246 | + ) |
| 247 | + cot_instance.xml = SimpleXMLElement(xml) |
| 248 | + assert cot_instance.ObtenerTagXml("tag1") == "value1" |
| 249 | + assert cot_instance.ObtenerTagXml("tag2", "subtag") == "value2" |
| 250 | + assert cot_instance.ObtenerTagXml("nonexistent") is None |
| 251 | + |
| 252 | + |
| 253 | +@pytest.mark.dontusefix |
| 254 | +def test_analizar_xml_error_handling(cot_instance): |
| 255 | + """ |
| 256 | + Test error handling in AnalizarXml method. |
| 257 | + Checks if the method properly handles invalid XML input. |
| 258 | + """ |
| 259 | + assert cot_instance.AnalizarXml("") is False |
| 260 | + assert "no element found" in cot_instance.Excepcion |
| 261 | + |
| 262 | + |
| 263 | +@pytest.mark.dontusefix |
| 264 | +def test_limpiar(cot_instance): |
| 265 | + """ |
| 266 | + Test limpiar method. |
| 267 | + Verifies if the method correctly resets instance attributes. |
| 268 | + """ |
| 269 | + cot_instance.XmlResponse = "test" |
| 270 | + cot_instance.Excepcion = "test" |
| 271 | + cot_instance.TipoError = "test" |
| 272 | + cot_instance.limpiar() |
| 273 | + assert cot_instance.XmlResponse == "" |
| 274 | + assert cot_instance.Excepcion == "" |
| 275 | + assert cot_instance.TipoError == "" |
0 commit comments