From bde2d02b8265215623a47728f3cacfbb88e28f7b Mon Sep 17 00:00:00 2001 From: SONIABHISHEK121 Date: Wed, 12 Jun 2024 16:15:05 +0530 Subject: [PATCH 1/2] feat : added tests for formato_xml.py module Signed-off-by: SONIABHISHEK121 --- formatos/formato_xml.py | 12 ++-- tests/test_formato_xml.py | 138 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 tests/test_formato_xml.py diff --git a/formatos/formato_xml.py b/formatos/formato_xml.py index 4b8b74882..9e944b6cf 100644 --- a/formatos/formato_xml.py +++ b/formatos/formato_xml.py @@ -266,8 +266,11 @@ def mapear(new, old, MAP, swap=False): try: for k, v in list(MAP.items()): if swap: - k, v = v, k - new[k] = old.get(v) + if k in old: + new[v] = old[k] + else: + if v in old: + new[k] = old[v] return new except: print(new, old, MAP) @@ -330,7 +333,8 @@ def desserializar(xml): def escribir(regs, fn="salida.xml"): "Dado una lista de comprobantes (diccionarios), convierte y escribe" xml = serializar(regs) - open(fn, "wb").write(xml) + with open(fn, "wb") as f: + f.write(xml.encode("utf-8")) def serializar(regs): @@ -341,7 +345,7 @@ def serializar(regs): for reg in regs: dic = {} for k, v in list(MAP_ENC.items()): - dic[v] = reg[k] + dic[v] = reg.get(k, "") dic.update( { diff --git a/tests/test_formato_xml.py b/tests/test_formato_xml.py new file mode 100644 index 000000000..7da560b5e --- /dev/null +++ b/tests/test_formato_xml.py @@ -0,0 +1,138 @@ +#!/usr/bin/python +# -*- coding: utf8 -*- +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 3, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. + +"""Test para formato_xml""" + +__author__ = "Mariano Reingart " +__copyright__ = "Copyright (C) 2010-2019 Mariano Reingart" +__license__ = "GPL 3.0" + + +import os +import unittest +import pytest +from decimal import Decimal +from pyafipws.formatos import formato_xml + +@pytest.mark.dontusefix +class TestFormatoXML(unittest.TestCase): + def setUp(self): + # Configuración inicial de los archivos de entrada y salida + self.entrada_xml = "datos/facturas.xml" + self.salida_xml = "tests/test_salida.xml" + + def tearDown(self): + # Limpiar el archivo de salida después de cada prueba + if os.path.exists(self.salida_xml): + os.remove(self.salida_xml) + + def test_leer(self): + # Prueba de la función leer + regs = formato_xml.leer(self.entrada_xml) + self.assertEqual(len(regs), 1) + reg = regs[0] + self.assertEqual(reg["concepto"], 1) + self.assertEqual(reg["tipo_doc"], 80) + self.assertEqual(reg["nro_doc"], 30500010912) + self.assertEqual(reg["tipo_cbte"], 6) + self.assertEqual(reg["punto_vta"], 5) + self.assertEqual(reg["cbt_numero"], 7) + self.assertEqual(reg["imp_total"], Decimal("1085.57")) + self.assertEqual(reg["imp_neto"], Decimal("889.82")) + self.assertEqual(reg["imp_iva"], Decimal("186.86")) + self.assertEqual(reg["imp_trib"], Decimal("8.89")) + self.assertEqual(reg["imp_op_ex"], Decimal("0.00")) + self.assertEqual(reg["fecha_cbte"], "20110609") + self.assertEqual(reg["fecha_venc_pago"], "") + self.assertEqual(reg["fecha_serv_desde"], "") + self.assertEqual(reg["fecha_serv_hasta"], "") + self.assertEqual(reg["moneda_id"], "PES") + self.assertEqual(reg["moneda_ctz"], Decimal("1.000000")) + self.assertEqual(str(reg["cae"]), "61233038185853") + self.assertEqual(reg["fecha_vto"], "20110619") + self.assertEqual(len(reg["detalles"]), 1) + self.assertEqual(len(reg["ivas"]), 1) + self.assertEqual(len(reg["tributos"]), 1) + self.assertEqual(len(reg["cbtes_asoc"]), 0) + self.assertEqual(len(reg["opcionales"]), 0) + + def test_escribir(self): + # Prueba de la función escribir + regs = formato_xml.leer(self.entrada_xml) + formato_xml.escribir(regs, self.salida_xml) + self.assertTrue(os.path.exists(self.salida_xml)) + + # Verificar si el archivo escrito contiene el contenido XML esperado + with open(self.salida_xml, "r") as f: + xml_content = f.read() + self.assertIn('', xml_content) + self.assertIn("", xml_content) + self.assertIn("", xml_content) + + def test_serializar(self): + # Prueba de la función serializar + regs = formato_xml.leer(self.entrada_xml) + xml = formato_xml.serializar(regs) + self.assertIsInstance(xml, str) + self.assertTrue(xml.startswith('')) + self.assertIn("", xml) + self.assertIn("", xml) + self.assertIn("", xml) + self.assertIn("", xml) + self.assertIn("", xml) + + def test_mapear(self): + # Prueba de la función mapear + # Mapeo con comportamiento predeterminado + old = {"tipo_cbte": 1, "punto_vta": 2, "cbt_numero": 3} + new = formato_xml.mapear({}, old, {"tipo": "tipo_cbte", "ptovta": "punto_vta", "numero": "cbt_numero"}) + self.assertEqual(new, {"tipo": 1, "ptovta": 2, "numero": 3}) + + # Mapeo con swap=True + old = {"tipo_cbte": 1, "punto_vta": 2, "cbt_numero": 3} + new = formato_xml.mapear({}, old, {"tipo_cbte": "tipo", "punto_vta": "ptovta", "cbt_numero": "numero"}, swap=True) + self.assertEqual(new, {"tipo": 1, "ptovta": 2, "numero": 3}) + + # Mapeo con valor faltante + old = {"tipo_cbte": 1, "punto_vta": 2} + new = formato_xml.mapear({}, old, {"tipo": "tipo_cbte", "ptovta": "punto_vta", "numero": "cbt_numero"}) + self.assertEqual(new, {"tipo": 1, "ptovta": 2}) + + # Mapeo con diccionario vacío + old = {} + new = formato_xml.mapear({}, old, {"tipo": "tipo_cbte", "ptovta": "punto_vta", "numero": "cbt_numero"}) + self.assertEqual(new, {}) + + def test_desserializar(self): + # Prueba de la función desserializar + xml_data = open(self.entrada_xml, "rb").read() + regs = formato_xml.desserializar(xml_data) + self.assertEqual(len(regs), 1) + # Agregar más aserciones para los datos deserializados + + def test_serializar_empty(self): + # Prueba de la función serializar con una lista vacía + regs = [] + xml = formato_xml.serializar(regs) + self.assertIsInstance(xml, str) + self.assertTrue(xml.startswith('')) + self.assertIn("", xml) + + def test_mapear_exception(self): + # Prueba para cubrir el manejo de excepciones en la función mapear + old = {"a": 1, "b": 2} + mapping = None # Proporcionar un mapeo inválido para provocar una excepción + with self.assertRaises(Exception): + formato_xml.mapear({}, old, mapping) + +if __name__ == "__main__": + unittest.main() From d42229e65d19f882c4794bd811c7ed16153c603c Mon Sep 17 00:00:00 2001 From: SONIABHISHEK121 Date: Thu, 15 Aug 2024 14:45:10 +0530 Subject: [PATCH 2/2] ran black and falke8 to follow PEP8 & removed unittest dependency Signed-off-by: SONIABHISHEK121 --- tests/test_formato_xml.py | 146 +++++++++++++++++++++++--------------- 1 file changed, 90 insertions(+), 56 deletions(-) diff --git a/tests/test_formato_xml.py b/tests/test_formato_xml.py index 7da560b5e..187f3ccdf 100644 --- a/tests/test_formato_xml.py +++ b/tests/test_formato_xml.py @@ -1,5 +1,6 @@ #!/usr/bin/python # -*- coding: utf8 -*- + # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 3, or (at your option) any later @@ -16,21 +17,20 @@ __copyright__ = "Copyright (C) 2010-2019 Mariano Reingart" __license__ = "GPL 3.0" - import os -import unittest import pytest from decimal import Decimal from pyafipws.formatos import formato_xml + @pytest.mark.dontusefix -class TestFormatoXML(unittest.TestCase): - def setUp(self): +class TestFormatoXML: + @pytest.fixture(autouse=True) + def setup_teardown(self): # Configuración inicial de los archivos de entrada y salida self.entrada_xml = "datos/facturas.xml" self.salida_xml = "tests/test_salida.xml" - - def tearDown(self): + yield # Limpiar el archivo de salida después de cada prueba if os.path.exists(self.salida_xml): os.remove(self.salida_xml) @@ -38,101 +38,135 @@ def tearDown(self): def test_leer(self): # Prueba de la función leer regs = formato_xml.leer(self.entrada_xml) - self.assertEqual(len(regs), 1) + assert len(regs) == 1 reg = regs[0] - self.assertEqual(reg["concepto"], 1) - self.assertEqual(reg["tipo_doc"], 80) - self.assertEqual(reg["nro_doc"], 30500010912) - self.assertEqual(reg["tipo_cbte"], 6) - self.assertEqual(reg["punto_vta"], 5) - self.assertEqual(reg["cbt_numero"], 7) - self.assertEqual(reg["imp_total"], Decimal("1085.57")) - self.assertEqual(reg["imp_neto"], Decimal("889.82")) - self.assertEqual(reg["imp_iva"], Decimal("186.86")) - self.assertEqual(reg["imp_trib"], Decimal("8.89")) - self.assertEqual(reg["imp_op_ex"], Decimal("0.00")) - self.assertEqual(reg["fecha_cbte"], "20110609") - self.assertEqual(reg["fecha_venc_pago"], "") - self.assertEqual(reg["fecha_serv_desde"], "") - self.assertEqual(reg["fecha_serv_hasta"], "") - self.assertEqual(reg["moneda_id"], "PES") - self.assertEqual(reg["moneda_ctz"], Decimal("1.000000")) - self.assertEqual(str(reg["cae"]), "61233038185853") - self.assertEqual(reg["fecha_vto"], "20110619") - self.assertEqual(len(reg["detalles"]), 1) - self.assertEqual(len(reg["ivas"]), 1) - self.assertEqual(len(reg["tributos"]), 1) - self.assertEqual(len(reg["cbtes_asoc"]), 0) - self.assertEqual(len(reg["opcionales"]), 0) + assert reg["concepto"] == 1 + assert reg["tipo_doc"] == 80 + assert reg["nro_doc"] == 30500010912 + assert reg["tipo_cbte"] == 6 + assert reg["punto_vta"] == 5 + assert reg["cbt_numero"] == 7 + assert reg["imp_total"] == Decimal("1085.57") + assert reg["imp_neto"] == Decimal("889.82") + assert reg["imp_iva"] == Decimal("186.86") + assert reg["imp_trib"] == Decimal("8.89") + assert reg["imp_op_ex"] == Decimal("0.00") + assert reg["fecha_cbte"] == "20110609" + assert reg["fecha_venc_pago"] == "" + assert reg["fecha_serv_desde"] == "" + assert reg["fecha_serv_hasta"] == "" + assert reg["moneda_id"] == "PES" + assert reg["moneda_ctz"] == Decimal("1.000000") + assert str(reg["cae"]) == "61233038185853" + assert reg["fecha_vto"] == "20110619" + assert len(reg["detalles"]) == 1 + assert len(reg["ivas"]) == 1 + assert len(reg["tributos"]) == 1 + assert len(reg["cbtes_asoc"]) == 0 + assert len(reg["opcionales"]) == 0 def test_escribir(self): # Prueba de la función escribir regs = formato_xml.leer(self.entrada_xml) formato_xml.escribir(regs, self.salida_xml) - self.assertTrue(os.path.exists(self.salida_xml)) + assert os.path.exists(self.salida_xml) # Verificar si el archivo escrito contiene el contenido XML esperado with open(self.salida_xml, "r") as f: xml_content = f.read() - self.assertIn('', xml_content) - self.assertIn("", xml_content) - self.assertIn("", xml_content) + assert '' in xml_content + assert "" in xml_content + assert "" in xml_content def test_serializar(self): # Prueba de la función serializar regs = formato_xml.leer(self.entrada_xml) xml = formato_xml.serializar(regs) - self.assertIsInstance(xml, str) - self.assertTrue(xml.startswith('')) - self.assertIn("", xml) - self.assertIn("", xml) - self.assertIn("", xml) - self.assertIn("", xml) - self.assertIn("", xml) + assert isinstance(xml, str) + assert xml.startswith('') + assert "" in xml + assert "" in xml + assert "" in xml + assert "" in xml + assert "" in xml def test_mapear(self): # Prueba de la función mapear # Mapeo con comportamiento predeterminado old = {"tipo_cbte": 1, "punto_vta": 2, "cbt_numero": 3} - new = formato_xml.mapear({}, old, {"tipo": "tipo_cbte", "ptovta": "punto_vta", "numero": "cbt_numero"}) - self.assertEqual(new, {"tipo": 1, "ptovta": 2, "numero": 3}) + new = formato_xml.mapear( + {}, + old, + { + "tipo": "tipo_cbte", + "ptovta": "punto_vta", + "numero": "cbt_numero" + }, + ) + assert new == {"tipo": 1, "ptovta": 2, "numero": 3} # Mapeo con swap=True old = {"tipo_cbte": 1, "punto_vta": 2, "cbt_numero": 3} - new = formato_xml.mapear({}, old, {"tipo_cbte": "tipo", "punto_vta": "ptovta", "cbt_numero": "numero"}, swap=True) - self.assertEqual(new, {"tipo": 1, "ptovta": 2, "numero": 3}) + new = formato_xml.mapear( + {}, + old, + { + "tipo_cbte": "tipo", + "punto_vta": "ptovta", + "cbt_numero": "numero" + }, + swap=True, + ) + assert new == {"tipo": 1, "ptovta": 2, "numero": 3} # Mapeo con valor faltante old = {"tipo_cbte": 1, "punto_vta": 2} - new = formato_xml.mapear({}, old, {"tipo": "tipo_cbte", "ptovta": "punto_vta", "numero": "cbt_numero"}) - self.assertEqual(new, {"tipo": 1, "ptovta": 2}) + new = formato_xml.mapear( + {}, + old, + { + "tipo": "tipo_cbte", + "ptovta": "punto_vta", + "numero": "cbt_numero" + }, + ) + assert new == {"tipo": 1, "ptovta": 2} # Mapeo con diccionario vacío old = {} - new = formato_xml.mapear({}, old, {"tipo": "tipo_cbte", "ptovta": "punto_vta", "numero": "cbt_numero"}) - self.assertEqual(new, {}) + new = formato_xml.mapear( + {}, + old, + { + "tipo": "tipo_cbte", + "ptovta": "punto_vta", + "numero": "cbt_numero" + }, + ) + assert new == {} def test_desserializar(self): # Prueba de la función desserializar xml_data = open(self.entrada_xml, "rb").read() regs = formato_xml.desserializar(xml_data) - self.assertEqual(len(regs), 1) + assert len(regs) == 1 # Agregar más aserciones para los datos deserializados def test_serializar_empty(self): # Prueba de la función serializar con una lista vacía regs = [] xml = formato_xml.serializar(regs) - self.assertIsInstance(xml, str) - self.assertTrue(xml.startswith('')) - self.assertIn("", xml) + assert isinstance(xml, str) + assert xml.startswith('') + assert "" in xml def test_mapear_exception(self): # Prueba para cubrir el manejo de excepciones en la función mapear old = {"a": 1, "b": 2} mapping = None # Proporcionar un mapeo inválido para provocar una excepción - with self.assertRaises(Exception): + with pytest.raises(Exception): formato_xml.mapear({}, old, mapping) + if __name__ == "__main__": - unittest.main() + pytest.main()