|
1 | 1 | #!/usr/bin/python
|
2 | 2 | # -*- coding: utf8 -*-
|
| 3 | + |
3 | 4 | # This program is free software; you can redistribute it and/or modify
|
4 | 5 | # it under the terms of the GNU General Public License as published by the
|
5 | 6 | # Free Software Foundation; either version 3, or (at your option) any later
|
|
16 | 17 | __copyright__ = "Copyright (C) 2010-2019 Mariano Reingart"
|
17 | 18 | __license__ = "GPL 3.0"
|
18 | 19 |
|
19 |
| - |
20 | 20 | import os
|
21 |
| -import unittest |
22 | 21 | import pytest
|
23 | 22 | from decimal import Decimal
|
24 | 23 | from pyafipws.formatos import formato_xml
|
25 | 24 |
|
| 25 | + |
26 | 26 | @pytest.mark.dontusefix
|
27 |
| -class TestFormatoXML(unittest.TestCase): |
28 |
| - def setUp(self): |
| 27 | +class TestFormatoXML: |
| 28 | + @pytest.fixture(autouse=True) |
| 29 | + def setup_teardown(self): |
29 | 30 | # Configuración inicial de los archivos de entrada y salida
|
30 | 31 | self.entrada_xml = "datos/facturas.xml"
|
31 | 32 | self.salida_xml = "tests/test_salida.xml"
|
32 |
| - |
33 |
| - def tearDown(self): |
| 33 | + yield |
34 | 34 | # Limpiar el archivo de salida después de cada prueba
|
35 | 35 | if os.path.exists(self.salida_xml):
|
36 | 36 | os.remove(self.salida_xml)
|
37 | 37 |
|
38 | 38 | def test_leer(self):
|
39 | 39 | # Prueba de la función leer
|
40 | 40 | regs = formato_xml.leer(self.entrada_xml)
|
41 |
| - self.assertEqual(len(regs), 1) |
| 41 | + assert len(regs) == 1 |
42 | 42 | reg = regs[0]
|
43 |
| - self.assertEqual(reg["concepto"], 1) |
44 |
| - self.assertEqual(reg["tipo_doc"], 80) |
45 |
| - self.assertEqual(reg["nro_doc"], 30500010912) |
46 |
| - self.assertEqual(reg["tipo_cbte"], 6) |
47 |
| - self.assertEqual(reg["punto_vta"], 5) |
48 |
| - self.assertEqual(reg["cbt_numero"], 7) |
49 |
| - self.assertEqual(reg["imp_total"], Decimal("1085.57")) |
50 |
| - self.assertEqual(reg["imp_neto"], Decimal("889.82")) |
51 |
| - self.assertEqual(reg["imp_iva"], Decimal("186.86")) |
52 |
| - self.assertEqual(reg["imp_trib"], Decimal("8.89")) |
53 |
| - self.assertEqual(reg["imp_op_ex"], Decimal("0.00")) |
54 |
| - self.assertEqual(reg["fecha_cbte"], "20110609") |
55 |
| - self.assertEqual(reg["fecha_venc_pago"], "") |
56 |
| - self.assertEqual(reg["fecha_serv_desde"], "") |
57 |
| - self.assertEqual(reg["fecha_serv_hasta"], "") |
58 |
| - self.assertEqual(reg["moneda_id"], "PES") |
59 |
| - self.assertEqual(reg["moneda_ctz"], Decimal("1.000000")) |
60 |
| - self.assertEqual(str(reg["cae"]), "61233038185853") |
61 |
| - self.assertEqual(reg["fecha_vto"], "20110619") |
62 |
| - self.assertEqual(len(reg["detalles"]), 1) |
63 |
| - self.assertEqual(len(reg["ivas"]), 1) |
64 |
| - self.assertEqual(len(reg["tributos"]), 1) |
65 |
| - self.assertEqual(len(reg["cbtes_asoc"]), 0) |
66 |
| - self.assertEqual(len(reg["opcionales"]), 0) |
| 43 | + assert reg["concepto"] == 1 |
| 44 | + assert reg["tipo_doc"] == 80 |
| 45 | + assert reg["nro_doc"] == 30500010912 |
| 46 | + assert reg["tipo_cbte"] == 6 |
| 47 | + assert reg["punto_vta"] == 5 |
| 48 | + assert reg["cbt_numero"] == 7 |
| 49 | + assert reg["imp_total"] == Decimal("1085.57") |
| 50 | + assert reg["imp_neto"] == Decimal("889.82") |
| 51 | + assert reg["imp_iva"] == Decimal("186.86") |
| 52 | + assert reg["imp_trib"] == Decimal("8.89") |
| 53 | + assert reg["imp_op_ex"] == Decimal("0.00") |
| 54 | + assert reg["fecha_cbte"] == "20110609" |
| 55 | + assert reg["fecha_venc_pago"] == "" |
| 56 | + assert reg["fecha_serv_desde"] == "" |
| 57 | + assert reg["fecha_serv_hasta"] == "" |
| 58 | + assert reg["moneda_id"] == "PES" |
| 59 | + assert reg["moneda_ctz"] == Decimal("1.000000") |
| 60 | + assert str(reg["cae"]) == "61233038185853" |
| 61 | + assert reg["fecha_vto"] == "20110619" |
| 62 | + assert len(reg["detalles"]) == 1 |
| 63 | + assert len(reg["ivas"]) == 1 |
| 64 | + assert len(reg["tributos"]) == 1 |
| 65 | + assert len(reg["cbtes_asoc"]) == 0 |
| 66 | + assert len(reg["opcionales"]) == 0 |
67 | 67 |
|
68 | 68 | def test_escribir(self):
|
69 | 69 | # Prueba de la función escribir
|
70 | 70 | regs = formato_xml.leer(self.entrada_xml)
|
71 | 71 | formato_xml.escribir(regs, self.salida_xml)
|
72 |
| - self.assertTrue(os.path.exists(self.salida_xml)) |
| 72 | + assert os.path.exists(self.salida_xml) |
73 | 73 |
|
74 | 74 | # Verificar si el archivo escrito contiene el contenido XML esperado
|
75 | 75 | with open(self.salida_xml, "r") as f:
|
76 | 76 | xml_content = f.read()
|
77 |
| - self.assertIn('<?xml version="1.0" encoding="UTF-8"?>', xml_content) |
78 |
| - self.assertIn("<comprobantes>", xml_content) |
79 |
| - self.assertIn("</comprobantes>", xml_content) |
| 77 | + assert '<?xml version="1.0" encoding="UTF-8"?>' in xml_content |
| 78 | + assert "<comprobantes>" in xml_content |
| 79 | + assert "</comprobantes>" in xml_content |
80 | 80 |
|
81 | 81 | def test_serializar(self):
|
82 | 82 | # Prueba de la función serializar
|
83 | 83 | regs = formato_xml.leer(self.entrada_xml)
|
84 | 84 | xml = formato_xml.serializar(regs)
|
85 |
| - self.assertIsInstance(xml, str) |
86 |
| - self.assertTrue(xml.startswith('<?xml version="1.0" encoding="UTF-8"?>')) |
87 |
| - self.assertIn("<comprobantes>", xml) |
88 |
| - self.assertIn("<comprobante>", xml) |
89 |
| - self.assertIn("<detalles>", xml) |
90 |
| - self.assertIn("<ivas>", xml) |
91 |
| - self.assertIn("<tributos>", xml) |
| 85 | + assert isinstance(xml, str) |
| 86 | + assert xml.startswith('<?xml version="1.0" encoding="UTF-8"?>') |
| 87 | + assert "<comprobantes>" in xml |
| 88 | + assert "<comprobante>" in xml |
| 89 | + assert "<detalles>" in xml |
| 90 | + assert "<ivas>" in xml |
| 91 | + assert "<tributos>" in xml |
92 | 92 |
|
93 | 93 | def test_mapear(self):
|
94 | 94 | # Prueba de la función mapear
|
95 | 95 | # Mapeo con comportamiento predeterminado
|
96 | 96 | old = {"tipo_cbte": 1, "punto_vta": 2, "cbt_numero": 3}
|
97 |
| - new = formato_xml.mapear({}, old, {"tipo": "tipo_cbte", "ptovta": "punto_vta", "numero": "cbt_numero"}) |
98 |
| - self.assertEqual(new, {"tipo": 1, "ptovta": 2, "numero": 3}) |
| 97 | + new = formato_xml.mapear( |
| 98 | + {}, |
| 99 | + old, |
| 100 | + { |
| 101 | + "tipo": "tipo_cbte", |
| 102 | + "ptovta": "punto_vta", |
| 103 | + "numero": "cbt_numero" |
| 104 | + }, |
| 105 | + ) |
| 106 | + assert new == {"tipo": 1, "ptovta": 2, "numero": 3} |
99 | 107 |
|
100 | 108 | # Mapeo con swap=True
|
101 | 109 | old = {"tipo_cbte": 1, "punto_vta": 2, "cbt_numero": 3}
|
102 |
| - new = formato_xml.mapear({}, old, {"tipo_cbte": "tipo", "punto_vta": "ptovta", "cbt_numero": "numero"}, swap=True) |
103 |
| - self.assertEqual(new, {"tipo": 1, "ptovta": 2, "numero": 3}) |
| 110 | + new = formato_xml.mapear( |
| 111 | + {}, |
| 112 | + old, |
| 113 | + { |
| 114 | + "tipo_cbte": "tipo", |
| 115 | + "punto_vta": "ptovta", |
| 116 | + "cbt_numero": "numero" |
| 117 | + }, |
| 118 | + swap=True, |
| 119 | + ) |
| 120 | + assert new == {"tipo": 1, "ptovta": 2, "numero": 3} |
104 | 121 |
|
105 | 122 | # Mapeo con valor faltante
|
106 | 123 | old = {"tipo_cbte": 1, "punto_vta": 2}
|
107 |
| - new = formato_xml.mapear({}, old, {"tipo": "tipo_cbte", "ptovta": "punto_vta", "numero": "cbt_numero"}) |
108 |
| - self.assertEqual(new, {"tipo": 1, "ptovta": 2}) |
| 124 | + new = formato_xml.mapear( |
| 125 | + {}, |
| 126 | + old, |
| 127 | + { |
| 128 | + "tipo": "tipo_cbte", |
| 129 | + "ptovta": "punto_vta", |
| 130 | + "numero": "cbt_numero" |
| 131 | + }, |
| 132 | + ) |
| 133 | + assert new == {"tipo": 1, "ptovta": 2} |
109 | 134 |
|
110 | 135 | # Mapeo con diccionario vacío
|
111 | 136 | old = {}
|
112 |
| - new = formato_xml.mapear({}, old, {"tipo": "tipo_cbte", "ptovta": "punto_vta", "numero": "cbt_numero"}) |
113 |
| - self.assertEqual(new, {}) |
| 137 | + new = formato_xml.mapear( |
| 138 | + {}, |
| 139 | + old, |
| 140 | + { |
| 141 | + "tipo": "tipo_cbte", |
| 142 | + "ptovta": "punto_vta", |
| 143 | + "numero": "cbt_numero" |
| 144 | + }, |
| 145 | + ) |
| 146 | + assert new == {} |
114 | 147 |
|
115 | 148 | def test_desserializar(self):
|
116 | 149 | # Prueba de la función desserializar
|
117 | 150 | xml_data = open(self.entrada_xml, "rb").read()
|
118 | 151 | regs = formato_xml.desserializar(xml_data)
|
119 |
| - self.assertEqual(len(regs), 1) |
| 152 | + assert len(regs) == 1 |
120 | 153 | # Agregar más aserciones para los datos deserializados
|
121 | 154 |
|
122 | 155 | def test_serializar_empty(self):
|
123 | 156 | # Prueba de la función serializar con una lista vacía
|
124 | 157 | regs = []
|
125 | 158 | xml = formato_xml.serializar(regs)
|
126 |
| - self.assertIsInstance(xml, str) |
127 |
| - self.assertTrue(xml.startswith('<?xml version="1.0" encoding="UTF-8"?>')) |
128 |
| - self.assertIn("<comprobantes/>", xml) |
| 159 | + assert isinstance(xml, str) |
| 160 | + assert xml.startswith('<?xml version="1.0" encoding="UTF-8"?>') |
| 161 | + assert "<comprobantes/>" in xml |
129 | 162 |
|
130 | 163 | def test_mapear_exception(self):
|
131 | 164 | # Prueba para cubrir el manejo de excepciones en la función mapear
|
132 | 165 | old = {"a": 1, "b": 2}
|
133 | 166 | mapping = None # Proporcionar un mapeo inválido para provocar una excepción
|
134 |
| - with self.assertRaises(Exception): |
| 167 | + with pytest.raises(Exception): |
135 | 168 | formato_xml.mapear({}, old, mapping)
|
136 | 169 |
|
| 170 | + |
137 | 171 | if __name__ == "__main__":
|
138 |
| - unittest.main() |
| 172 | + pytest.main() |
0 commit comments