diff --git a/formatos/formato_csv.py b/formatos/formato_csv.py index 71bd7580..f1d50a12 100644 --- a/formatos/formato_csv.py +++ b/formatos/formato_csv.py @@ -26,11 +26,11 @@ import os from openpyxl import load_workbook -def leer(fn="entrada.csv", delimiter=";"): +def leer(fn="entrada.csv", delimiter=";", header=True): "Analiza un archivo CSV y devuelve una lista de listas con los datos" ext = os.path.splitext(fn)[1].lower() items = [] - + if ext == ".csv": with open(fn, "r", encoding="utf-8") as csvfile: try: @@ -40,23 +40,20 @@ def leer(fn="entrada.csv", delimiter=";"): dialect.delimiter = delimiter csvfile.seek(0) csv_reader = csv.reader(csvfile, dialect) - header_skipped = False + if header: + next(csv_reader, None) # Skip the header row for row in csv_reader: - if not header_skipped: - header_skipped = True - continue items.append([c.strip() if isinstance(c, str) else c for c in row]) - + elif ext == ".xlsx": wb = load_workbook(filename=fn) ws1 = wb.active - header_skipped = False - for row in ws1.iter_rows(values_only=True): - if not header_skipped: - header_skipped = True - continue + rows = ws1.iter_rows(values_only=True) + if header: + next(rows, None) # Skip the header row + for row in rows: items.append([cell for cell in row]) - + return items # TODO: return desaplanar(items) @@ -341,22 +338,16 @@ def escribir(filas, fn="salida.csv", delimiter=";"): "Dado una lista de comprobantes (diccionarios), aplana y escribe" ext = os.path.splitext(fn)[1].lower() if ext == ".csv": - f = open(fn, "wb") - csv_writer = csv.writer(f, dialect="excel", delimiter=";") - # TODO: filas = aplanar(regs) - for fila in filas: - # convertir a ISO-8859-1 (evita error de encoding de csv writer): - fila = [ - celda.encode("latin1") if isinstance(celda, str) else celda - for celda in fila - ] - csv_writer.writerow(fila) - f.close() + with open(fn, "w", newline="") as f: + csv_writer = csv.writer(f, dialect="excel", delimiter=delimiter) + # TODO: filas = aplanar(regs) + for fila in filas: + csv_writer.writerow(fila) elif ext == ".xlsx": from openpyxl import Workbook wb = Workbook() - ws1 = wb.get_active_sheet() + ws1 = wb.active for fila in filas: ws1.append(fila) wb.save(filename=fn) diff --git a/requirements-dev.txt b/requirements-dev.txt index 8ca42cd3..7729ac8d 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,4 +11,5 @@ pytest-freezegun==0.4.2; python_version > '3' pytest-mock==2.0.0; python_version <= '2.7' pytest-mock==3.10.0; python_version > '2.7' pywin32==304; sys_platform == "win32" and python_version > '3' -py2exe==0.11.1.1; sys_platform == "win32" and python_version > '3' \ No newline at end of file +py2exe==0.11.1.1; sys_platform == "win32" and python_version > '3' +openpyxl==3.1.2; python_version > '3' \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 59cb59a0..ea6b6318 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ tabulate==0.8.5 certifi>=2020.4.5.1 qrcode==6.1 future==0.18.3 +openpyxl==3.1.2; python_version > '3' diff --git a/tests/test_formato_csv.py b/tests/test_formato_csv.py index 36c1498e..d81669c6 100644 --- a/tests/test_formato_csv.py +++ b/tests/test_formato_csv.py @@ -19,174 +19,190 @@ import os import sys import unittest -import importlib.util import csv import pytest import tempfile +from openpyxl import Workbook, load_workbook from io import StringIO -from openpyxl import Workbook from unittest.mock import patch # Add the 'formatos' directory to the Python module search path formatos_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'formatos')) sys.path.insert(0, formatos_dir) -# Get the absolute path to the formato_csv.py file -formato_csv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'formatos', 'formato_csv.py')) - -# Load the formato_csv module -spec = importlib.util.spec_from_file_location("formato_csv", formato_csv_path) -formato_csv = importlib.util.module_from_spec(spec) -spec.loader.exec_module(formato_csv) - -from pyafipws.formatos.formato_csv import leer +from pyafipws.formatos.formato_csv import leer, aplanar, desaplanar, escribir @pytest.mark.dontusefix class TestLeerFunction(unittest.TestCase): - @classmethod - def setUpClass(cls): - # Create sample CSV data - cls.sample_csv_data = "Column1,Column2,Column3\nValue1,Value2,Value3\nValue4,Value5,Value6" - - # Create sample CSV data with pipe delimiter - cls.sample_pipe_csv_data = "Column1|Column2|Column3\nValue1|Value2|Value3\nValue4|Value5|Value6" - - # Create empty CSV data - cls.empty_csv_data = "" - - def test_leer_csv_file(self): """ Test that the leer function can read a valid CSV file correctly. """ expected_data = [ - ["Value1", "Value2", "Value3"], - ["Value4", "Value5", "Value6"], + ['1', '6', '4004', '526', '20170826', '80', '30500010912', 'PES', '1.000000', '889.82', '186.86', '8.89', '0.00', '0.00', '1085.57', '1', '', '', '', '61233038185853', '20110619', 'A', '', 'S', '', '', '', '', '', 'mariano@sistemasagiles.com.ar', '21601192', '6443', 'Exento', '82016336', '8001', '', '', 'P1675G', 'COD2', '8.89', '1076.68', '0', '1.0', '0', '0', '5', '1.00', '20205766', '110170P', 'Impuesto municipal matanza', '889.82', '186.86', '7', '0', 'PRUEBA ART', 'SEGUNDO ART', '', '', 'Cliente XXX', '', '99', '', '889.82', 'Patricia 1 - Cdad de Buenos Aires - 1405 - Capital Federal - Argentina', '1076.68', '0', '30 Dias', '0.00', '1', '17', '1', '1801', '30500010912', '1802', 'BNA'] ] - with patch('builtins.open', return_value=StringIO(self.sample_csv_data)): - result = formato_csv.leer('data/sample.csv') + result = leer('datos/facturas.csv', delimiter=';') self.assertEqual(result, expected_data) - def test_leer_xlsx_file(self): + def test_leer_csv_custom_delimiter(self): """ - Test that the leer function can read a valid Excel file correctly. + Test that the leer function can read a CSV file with a custom delimiter. """ + sample_csv_data = "Column1|Column2|Column3\nValue1|Value2|Value3\nValue4|Value5|Value6" expected_data = [ - ["Value1", "Value2", "Value3"], - ["Value4", "Value5", "Value6"], + ['Value1', 'Value2', 'Value3'], + ['Value4', 'Value5', 'Value6'] ] - - # Create a temporary Excel file for testing - with tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False) as temp_file: - workbook = Workbook() - worksheet = workbook.active - worksheet['A1'] = 'Column1' - worksheet['B1'] = 'Column2' - worksheet['C1'] = 'Column3' - worksheet['A2'] = 'Value1' - worksheet['B2'] = 'Value2' - worksheet['C2'] = 'Value3' - worksheet['A3'] = 'Value4' - worksheet['B3'] = 'Value5' - worksheet['C3'] = 'Value6' - workbook.save(temp_file.name) - - result = formato_csv.leer(temp_file.name) + with patch('builtins.open', return_value=StringIO(sample_csv_data)): + result = leer('data/sample.csv', delimiter='|') self.assertEqual(result, expected_data) - # Clean up the temporary file - os.unlink(temp_file.name) - - def test_leer_missing_file(self): + def test_leer_csv_with_header(self): """ - Test that the leer function raises an appropriate exception when the file is missing. + Test that the leer function can read a CSV file with a header row correctly. """ - filename = os.path.join('data', 'missing.csv') - with self.assertRaises(FileNotFoundError): - formato_csv.leer(filename) + sample_csv_data = "Column1,Column2,Column3\nValue1,Value2,Value3\nValue4,Value5,Value6" + expected_data = [ + ['Value1', 'Value2', 'Value3'], + ['Value4', 'Value5', 'Value6'] + ] - def test_leer_empty_file(self): + with patch('builtins.open', return_value=StringIO(sample_csv_data)): + result = leer('data/sample.csv') + self.assertEqual(result, expected_data) + + def test_leer_csv_without_header(self): """ - Test that the leer function handles an empty file correctly. + Test that the leer function can read a CSV file without a header row correctly. """ - expected_data = [] - with patch('builtins.open', return_value=StringIO(self.empty_csv_data)): - result = formato_csv.leer('data/empty.csv') + sample_csv_data = "Value1,Value2,Value3\nValue4,Value5,Value6" + expected_data = [ + ['Value1', 'Value2', 'Value3'], + ['Value4', 'Value5', 'Value6'] + ] + + with patch('builtins.open', return_value=StringIO(sample_csv_data)): + result = leer('data/sample.csv', header=False) self.assertEqual(result, expected_data) - def test_leer_custom_delimiter(self): + def test_leer_csv_with_whitespace(self): """ - Test that the leer function can read a CSV file with a custom delimiter. + Test that the leer function can handle leading/trailing whitespace in CSV values correctly. """ + sample_csv_data = "Column1,Column2,Column3\n Value1 , Value2 , Value3 \n Value4 , Value5 , Value6 " expected_data = [ - ["Value1", "Value2", "Value3"], - ["Value4", "Value5", "Value6"], + ['Value1', 'Value2', 'Value3'], + ['Value4', 'Value5', 'Value6'] ] - with patch('builtins.open', return_value=StringIO(self.sample_pipe_csv_data)): - result = formato_csv.leer('data/sample_pipe.csv', delimiter="|") + + with patch('builtins.open', return_value=StringIO(sample_csv_data)): + result = leer('data/sample.csv') self.assertEqual(result, expected_data) - def test_leer_csv_missing_columns(self): + def test_leer_csv_with_non_string_values(self): """ - Test that the leer function handles a CSV file with missing columns correctly. + Test that the leer function can handle non-string values in a CSV file correctly. """ - sample_csv_data = "Column1,Column2\nValue1,Value2\nValue3\nValue4,Value5,Value6" + sample_csv_data = "Column1,Column2,Column3\n1,2.5,True\n4,5.7,False" expected_data = [ - ["Value1", "Value2"], - ["Value3"], - ["Value4", "Value5", "Value6"], + ['1', '2.5', 'True'], + ['4', '5.7', 'False'] ] + with patch('builtins.open', return_value=StringIO(sample_csv_data)): - result = formato_csv.leer('data/sample_missing_columns.csv', delimiter=',') + result = leer('data/sample.csv') self.assertEqual(result, expected_data) - def test_leer_csv_extra_columns(self): + def test_leer_csv_different_dialect(self): """ - Test that the leer function handles a CSV file with extra columns correctly. + Test that the leer function can handle a CSV file with a different dialect correctly. """ - sample_csv_data = "Column1,Column2,Column3,Column4\nValue1,Value2,Value3,Value4\nValue5,Value6,Value7,Value8" + sample_csv_data = "Column1;Column2;Column3\nValue1;Value2;Value3\nValue4;Value5;Value6" expected_data = [ - ["Value1", "Value2", "Value3", "Value4"], - ["Value5", "Value6", "Value7", "Value8"], + ['Value1', 'Value2', 'Value3'], + ['Value4', 'Value5', 'Value6'] ] + with patch('builtins.open', return_value=StringIO(sample_csv_data)): - result = formato_csv.leer('data/sample_extra_columns.csv', delimiter=',') + result = leer('data/sample.csv', delimiter=';') self.assertEqual(result, expected_data) - def test_leer_csv_different_column_order(self): + def test_leer_csv_empty_file(self): + """ + Test that the leer function handles an empty CSV file correctly. + """ + sample_csv_data = "" + expected_data = [] + + with patch('builtins.open', return_value=StringIO(sample_csv_data)): + result = leer('data/sample.csv') + self.assertEqual(result, expected_data) + + + def test_leer_xlsx_file_with_header(self): """ - Test that the leer function handles a CSV file with different column order correctly. + Test that the leer function can read an Excel file with a header row correctly. """ - sample_csv_data = "Column2,Column1,Column3\nValue2,Value1,Value3\nValue5,Value4,Value6" expected_data = [ - ["Value2", "Value1", "Value3"], - ["Value5", "Value4", "Value6"], + ['Value1', 'Value2', 'Value3'], + ['Value4', 'Value5', 'Value6'] ] - with patch('builtins.open', return_value=StringIO(sample_csv_data)): - result = formato_csv.leer('data/sample_different_order.csv', delimiter=',') + + # Create a temporary Excel file for testing + with tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False) as temp_file: + workbook = Workbook() + worksheet = workbook.active + worksheet['A1'] = 'Column1' + worksheet['B1'] = 'Column2' + worksheet['C1'] = 'Column3' + worksheet['A2'] = 'Value1' + worksheet['B2'] = 'Value2' + worksheet['C2'] = 'Value3' + worksheet['A3'] = 'Value4' + worksheet['B3'] = 'Value5' + worksheet['C3'] = 'Value6' + workbook.save(temp_file.name) + + result = leer(temp_file.name) self.assertEqual(result, expected_data) + # Clean up the temporary file + os.unlink(temp_file.name) - def test_leer_csv_special_characters(self): + def test_leer_xlsx_file_without_header(self): """ - Test that the leer function handles a CSV file with special characters correctly. + Test that the leer function can read an Excel file without a header row correctly. """ - sample_csv_data = "Column1,Column2,Column3\nValue1,Válue2,Value3\nValue4,Value5,Válue6" expected_data = [ - ["Value1", "Válue2", "Value3"], - ["Value4", "Value5", "Válue6"], + ['Value1', 'Value2', 'Value3'], + ['Value4', 'Value5', 'Value6'] ] - with patch('builtins.open', return_value=StringIO(sample_csv_data)): - result = formato_csv.leer('data/sample_special_chars.csv', delimiter=',') + + # Create a temporary Excel file for testing + with tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False) as temp_file: + workbook = Workbook() + worksheet = workbook.active + worksheet['A1'] = 'Value1' + worksheet['B1'] = 'Value2' + worksheet['C1'] = 'Value3' + worksheet['A2'] = 'Value4' + worksheet['B2'] = 'Value5' + worksheet['C2'] = 'Value6' + workbook.save(temp_file.name) + + result = leer(temp_file.name, header=False) self.assertEqual(result, expected_data) + # Clean up the temporary file + os.unlink(temp_file.name) + @pytest.mark.dontusefix class TestAplanarFunction(unittest.TestCase): def test_aplanar_single_record(self): @@ -233,83 +249,34 @@ def test_aplanar_single_record(self): expected_data = [ [ - 'id', - 'tipo_cbte', - 'punto_vta', - 'cbt_numero', - 'fecha_cbte', - 'tipo_doc', - 'nro_doc', - 'imp_total', - 'forma_pago', - 'pdf', - 'codigo1', - 'descripcion1', - 'umed1', - 'cantidad1', - 'precio1', - 'importe1', - 'iva_id_1', - 'iva_base_imp_1', - 'iva_importe_1', - 'tributo_id_1', - 'tributo_base_imp_1', - 'tributo_desc_1', - 'tributo_alic_1', - 'tributo_importe_1', - 'cbte_nro', - 'moneda_id', - 'moneda_ctz', - 'imp_neto', - 'imp_iva', - 'imp_trib', - 'imp_op_ex', - 'imp_tot_conc' + 'id', 'tipo_cbte', 'punto_vta', 'cbt_numero', 'fecha_cbte', 'tipo_doc', 'nro_doc', 'moneda_id', 'moneda_ctz', + 'imp_neto', 'imp_iva', 'imp_trib', 'imp_op_ex', 'imp_tot_conc', 'imp_total', 'concepto', 'fecha_venc_pago', + 'fecha_serv_desde', 'fecha_serv_hasta', 'cae', 'fecha_vto', 'resultado', 'motivo', 'reproceso', 'nombre', + 'domicilio', 'localidad', 'telefono', 'categoria', 'email', 'numero_cliente', 'numero_orden_compra', + 'condicion_frente_iva', 'numero_cotizacion', 'numero_remito', 'obs_generales', 'obs_comerciales', + 'forma_pago', 'pdf', 'bonif1', 'cantidad1', 'cbte_nro', 'codigo1', 'cuit', 'dato_a1', 'dato_b1', 'dato_c1', + 'dato_d1', 'dato_e1', 'descripcion1', 'detalles', 'domicilio_cliente', 'id_impositivo', 'idioma', + 'idioma_cbte', 'imp_iva1', 'importe1', 'incoterms', 'incoterms_ds', 'iva_base_imp_1', 'iva_id1', 'iva_id_1', + 'iva_importe_1', 'ivas', 'localidad_cliente', 'nombre_cliente', 'numero_despacho1', 'pais_dst_cmp', + 'permiso_existente', 'precio1', 'provincia_cliente', 'telefono_cliente', 'tipo_expo', 'tributo_alic_1', + 'tributo_base_imp_1', 'tributo_desc_1', 'tributo_id_1', 'tributo_importe_1', 'tributos', 'umed1' ], [ - 1, - 1, - 1, - 1, - '2023-06-08', - 80, - '20123456789', - 126, - 'Contado', - '', - 'P1', - 'Producto 1', - 7, - 2, - 50, - 100, - 5, - 100, - 21, - 1, - 100, - 'Tributo 1', - 5, - 5, - 1, - None, - None, - None, - None, - None, - None, - None + 1, 1, 1, 1, '2023-06-08', 80, '20123456789', None, None, None, None, None, None, None, 126, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, 'Contado', '', None, 2, 1, 'P1', None, None, None, None, None, None, 'Producto 1', + [{'codigo': 'P1', 'ds': 'Producto 1', 'qty': 2, 'umed': 7, 'precio': 50, 'importe': 100}], None, None, + None, None, None, 100, None, None, 100, None, 5, 21, + [{'iva_id': 5, 'base_imp': 100, 'importe': 21}], None, None, None, None, None, 50, None, None, None, 5, + 100, 'Tributo 1', 1, 5, + [{'tributo_id': 1, 'base_imp': 100, 'desc': 'Tributo 1', 'alic': 5, 'importe': 5}], 7 ] ] - result = formato_csv.aplanar([reg]) + result = aplanar([reg]) self.assertEqual(result, expected_data) - - - - def test_aplanar_multiple_records(self): """ Test that the aplanar function correctly flattens multiple records. @@ -323,36 +290,7 @@ def test_aplanar_multiple_records(self): "fecha_cbte": "2023-06-08", "tipo_doc": 80, "nro_doc": "20123456789", - "moneda_id": "PES", - "moneda_ctz": 1, - "imp_neto": 100, - "imp_iva": 21, - "imp_trib": 5, - "imp_op_ex": 0, - "imp_tot_conc": 0, "imp_total": 126, - "concepto": 1, - "fecha_venc_pago": "2023-06-08", - "fecha_serv_desde": "2023-06-08", - "fecha_serv_hasta": "2023-06-08", - "cae": "1234567890", - "fecha_vto": "2023-06-18", - "resultado": "A", - "motivo": "", - "reproceso": "", - "nombre": "John Doe", - "domicilio": "123 Main St", - "localidad": "City", - "telefono": "1234567890", - "categoria": "A", - "email": "john@example.com", - "numero_cliente": "ABC123", - "numero_orden_compra": "OC123", - "condicion_frente_iva": "Responsable Inscripto", - "numero_cotizacion": "COT123", - "numero_remito": "REM123", - "obs_generales": "Observaciones generales", - "obs_comerciales": "Observaciones comerciales", "detalles": [ { "codigo": "P1", @@ -361,15 +299,6 @@ def test_aplanar_multiple_records(self): "umed": 7, "precio": 50, "importe": 100, - "iva_id": 5, - "imp_iva": 21, - "bonif": 0, - "despacho": "", - "dato_a": "", - "dato_b": "", - "dato_c": "", - "dato_d": "", - "dato_e": "", } ], "ivas": [ @@ -388,61 +317,17 @@ def test_aplanar_multiple_records(self): "importe": 5, } ], - "opcionales": [ - { - "opcional_id": 1, - "valor": "Valor opcional 1", - } - ], - "cbtes_asoc": [ - { - "cbte_tipo": 1, - "cbte_punto_vta": 1, - "cbte_nro": 0, - "cbte_cuit": "20123456789", - "cbte_fecha": "2023-06-07", - } - ], "forma_pago": "Contado", }, { "id": 2, - "tipo_cbte": 1, + "tipo_cbte": 6, "punto_vta": 1, "cbte_nro": 2, "fecha_cbte": "2023-06-09", "tipo_doc": 80, "nro_doc": "20987654321", - "moneda_id": "PES", - "moneda_ctz": 1, - "imp_neto": 200, - "imp_iva": 42, - "imp_trib": 10, - "imp_op_ex": 0, - "imp_tot_conc": 0, - "imp_total": 252, - "concepto": 1, - "fecha_venc_pago": "2023-06-09", - "fecha_serv_desde": "2023-06-09", - "fecha_serv_hasta": "2023-06-09", - "cae": "0987654321", - "fecha_vto": "2023-06-19", - "resultado": "A", - "motivo": "", - "reproceso": "", - "nombre": "Jane Smith", - "domicilio": "456 Elm St", - "localidad": "Town", - "telefono": "0987654321", - "categoria": "B", - "email": "jane@example.com", - "numero_cliente": "XYZ789", - "numero_orden_compra": "OC456", - "condicion_frente_iva": "Responsable Inscripto", - "numero_cotizacion": "COT456", - "numero_remito": "REM456", - "obs_generales": "Observaciones generales", - "obs_comerciales": "Observaciones comerciales", + "imp_total": 200, "detalles": [ { "codigo": "P2", @@ -451,15 +336,6 @@ def test_aplanar_multiple_records(self): "umed": 7, "precio": 200, "importe": 200, - "iva_id": 5, - "imp_iva": 42, - "bonif": 0, - "despacho": "", - "dato_a": "", - "dato_b": "", - "dato_c": "", - "dato_d": "", - "dato_e": "", } ], "ivas": [ @@ -469,1666 +345,412 @@ def test_aplanar_multiple_records(self): "importe": 42, } ], - "tributos": [ - { - "tributo_id": 2, - "base_imp": 200, - "desc": "Tributo 2", - "alic": 5, - "importe": 10, - } - ], - "opcionales": [ - { - "opcional_id": 2, - "valor": "Valor opcional 2", - } - ], - "cbtes_asoc": [ - { - "cbte_tipo": 1, - "cbte_punto_vta": 1, - "cbte_nro": 1, - "cbte_cuit": "20123456789", - "cbte_fecha": "2023-06-08", - } - ], + "tributos": [], "forma_pago": "Tarjeta de Crédito", }, ] expected_data = [ [ - "id", - "tipo_cbte", - "punto_vta", - "cbt_numero", - "fecha_cbte", - "tipo_doc", - "nro_doc", - "moneda_id", - "moneda_ctz", - "imp_neto", - "imp_iva", - "imp_trib", - "imp_op_ex", - "imp_tot_conc", - "imp_total", - "concepto", - "fecha_venc_pago", - "fecha_serv_desde", - "fecha_serv_hasta", - "cae", - "fecha_vto", - "resultado", - "motivo", - "reproceso", - "nombre", - "domicilio", - "localidad", - "telefono", - "categoria", - "email", - "numero_cliente", - "numero_orden_compra", - "condicion_frente_iva", - "numero_cotizacion", - "numero_remito", - "obs_generales", - "obs_comerciales", - "forma_pago", - "pdf", - "codigo1", - "descripcion1", - "umed1", - "cantidad1", - "precio1", - "importe1", - "iva_id1", - "imp_iva1", - "bonif1", - "numero_despacho1", - "dato_a1", - "dato_b1", - "dato_c1", - "dato_d1", - "dato_e1", - "iva_id_1", - "iva_base_imp_1", - "iva_importe_1", - "tributo_id_1", - "tributo_base_imp_1", - "tributo_desc_1", - "tributo_alic_1", - "tributo_importe_1", - "opcional_id_1", - "opcional_valor_1", - "cbte_asoc_tipo_1", - "cbte_asoc_pto_vta_1", - "cbte_asoc_nro_1", - "cbte_asoc_cuit_1", - "cbte_asoc_fecha_1", - "codigo2", - "descripcion2", - "umed2", - "cantidad2", - "precio2", - "importe2", - "iva_id2", - "imp_iva2", - "bonif2", - "numero_despacho2", - "dato_a2", - "dato_b2", - "dato_c2", - "dato_d2", - "dato_e2", - "iva_id_2", - "iva_base_imp_2", - "iva_importe_2", - "tributo_id_2", - "tributo_base_imp_2", - "tributo_desc_2", - "tributo_alic_2", - "tributo_importe_2", - "opcional_id_2", - "opcional_valor_2", - "cbte_asoc_tipo_2", - "cbte_asoc_pto_vta_2", - "cbte_asoc_nro_2", - "cbte_asoc_cuit_2", - "cbte_asoc_fecha_2", + 'id', 'tipo_cbte', 'punto_vta', 'cbt_numero', 'fecha_cbte', 'tipo_doc', 'nro_doc', 'moneda_id', 'moneda_ctz', + 'imp_neto', 'imp_iva', 'imp_trib', 'imp_op_ex', 'imp_tot_conc', 'imp_total', 'concepto', 'fecha_venc_pago', + 'fecha_serv_desde', 'fecha_serv_hasta', 'cae', 'fecha_vto', 'resultado', 'motivo', 'reproceso', 'nombre', + 'domicilio', 'localidad', 'telefono', 'categoria', 'email', 'numero_cliente', 'numero_orden_compra', + 'condicion_frente_iva', 'numero_cotizacion', 'numero_remito', 'obs_generales', 'obs_comerciales', + 'forma_pago', 'pdf', 'bonif1', 'cantidad1', 'cbte_nro', 'codigo1', 'cuit', 'dato_a1', 'dato_b1', 'dato_c1', + 'dato_d1', 'dato_e1', 'descripcion1', 'detalles', 'domicilio_cliente', 'id_impositivo', 'idioma', + 'idioma_cbte', 'imp_iva1', 'importe1', 'incoterms', 'incoterms_ds', 'iva_base_imp_1', 'iva_id1', 'iva_id_1', + 'iva_importe_1', 'ivas', 'localidad_cliente', 'nombre_cliente', 'numero_despacho1', 'pais_dst_cmp', + 'permiso_existente', 'precio1', 'provincia_cliente', 'telefono_cliente', 'tipo_expo', 'tributo_alic_1', + 'tributo_base_imp_1', 'tributo_desc_1', 'tributo_id_1', 'tributo_importe_1', 'tributos', 'umed1' ], [ - 1, - 1, - 1, - 1, - "2023-06-08", - 80, - "20123456789", - "PES", - 1, - 100, - 21, - 5, - 0, - 0, - 126, - 1, - "2023-06-08", - "2023-06-08", - "2023-06-08", - "1234567890", - "2023-06-18", - "A", - "", - "", - "John Doe", - "123 Main St", - "City", - "1234567890", - "A", - "john@example.com", - "ABC123", - "OC123", - "Responsable Inscripto", - "COT123", - "REM123", - "Observaciones generales", - "Observaciones comerciales", - "Contado", - "", - "P1", - "Producto 1", - 7, - 2, - 50, - 100, - 5, - 21, - 0, - "", - "", - "", - "", - "", - "", - 5, - 100, - 21, - 1, - 100, - "Tributo 1", - 5, - 5, - 1, - "Valor opcional 1", - 1, - 1, - 0, - "20123456789", - "2023-06-07", - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, + 1, 1, 1, 1, '2023-06-08', 80, '20123456789', None, None, None, None, None, None, None, 126, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, 'Contado', '', None, 2, 1, 'P1', None, None, None, None, None, None, 'Producto 1', + [{'codigo': 'P1', 'ds': 'Producto 1', 'qty': 2, 'umed': 7, 'precio': 50, 'importe': 100}], None, None, + None, None, None, 100, None, None, 100, None, 5, 21, + [{'iva_id': 5, 'base_imp': 100, 'importe': 21}], None, None, None, None, None, 50, None, None, None, 5, + 100, 'Tributo 1', 1, 5, + [{'tributo_id': 1, 'base_imp': 100, 'desc': 'Tributo 1', 'alic': 5, 'importe': 5}], 7 ], [ - 2, - 1, - 1, - 2, - "2023-06-09", - 80, - "20987654321", - "PES", - 1, - 200, - 42, - 10, - 0, - 0, - 252, - 1, - "2023-06-09", - "2023-06-09", - "2023-06-09", - "0987654321", - "2023-06-19", - "A", - "", - "", - "Jane Smith", - "456 Elm St", - "Town", - "0987654321", - "B", - "jane@example.com", - "XYZ789", - "OC456", - "Responsable Inscripto", - "COT456", - "REM456", - "Observaciones generales", - "Observaciones comerciales", - "Tarjeta de Crédito", - "", - "P2", - "Producto 2", - 7, - 1, - 200, - 200, - 5, - 42, - 0, - "", - "", - "", - "", - "", - "", - 5, - 200, - 42, - 2, - 200, - "Tributo 2", - 5, - 10, - 2, - "Valor opcional 2", - 1, - 1, - 1, - "20123456789", - "2023-06-08", - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - ], + 2, 6, 1, 2, '2023-06-09', 80, '20987654321', None, None, None, None, None, None, None, 200, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, 'Tarjeta de Crédito', '', None, 1, 2, 'P2', None, None, None, None, None, None, 'Producto 2', + [{'codigo': 'P2', 'ds': 'Producto 2', 'qty': 1, 'umed': 7, 'precio': 200, 'importe': 200}], None, None, + None, None, None, 200, None, None, 200, None, 5, 42, + [{'iva_id': 5, 'base_imp': 200, 'importe': 42}], None, None, None, None, None, 200, None, None, None, + None, None, None, None, None, [], 7 + ] ] - result = formato_csv.aplanar(regs) + result = aplanar(regs) self.assertEqual(result, expected_data) - def test_aplanar_empty_records(self): + +@pytest.mark.dontusefix +class TestDesplanarFunction(unittest.TestCase): + def test_desaplanar_single_record(self): """ - Test that the aplanar function handles empty records correctly. + Test that the desaplanar function correctly converts a single record from flattened format to structured format. """ - regs = [] - expected_data = [ - [ - "id", - "tipo_cbte", - "punto_vta", - "cbt_numero", - "fecha_cbte", - "tipo_doc", - "nro_doc", - "moneda_id", - "moneda_ctz", - "imp_neto", - "imp_iva", - "imp_trib", - "imp_op_ex", - "imp_tot_conc", - "imp_total", - "concepto", - "fecha_venc_pago", - "fecha_serv_desde", - "fecha_serv_hasta", - "cae", - "fecha_vto", - "resultado", - "motivo", - "reproceso", - "nombre", - "domicilio", - "localidad", - "telefono", - "categoria", - "email", - "numero_cliente", - "numero_orden_compra", - "condicion_frente_iva", - "numero_cotizacion", - "numero_remito", - "obs_generales", - "obs_comerciales", - ], + filas = [ + ['id', 'tipo_cbte', 'punto_vta', 'cbt_numero', 'fecha_cbte', 'tipo_doc', 'nro_doc', 'moneda_id', 'moneda_ctz', + 'imp_neto', 'imp_iva', 'imp_trib', 'imp_op_ex', 'imp_tot_conc', 'imp_total', 'concepto', 'fecha_venc_pago', + 'fecha_serv_desde', 'fecha_serv_hasta', 'cae', 'fecha_vto', 'resultado', 'motivo', 'reproceso', 'nombre', + 'domicilio', 'localidad', 'telefono', 'categoria', 'email', 'numero_cliente', 'numero_orden_compra', + 'condicion_frente_iva', 'numero_cotizacion', 'numero_remito', 'obs_generales', 'obs_comerciales', + 'forma_pago', 'pdf', 'codigo1', 'descripcion1', 'cantidad1', 'umed1', 'precio1', 'importe1', 'bonif1', + 'iva_id1', 'imp_iva1', 'tributo_id_1', 'tributo_desc_1', 'tributo_base_imp_1', 'tributo_alic_1', + 'tributo_importe_1', 'iva_id_1', 'iva_base_imp_1', 'iva_importe_1'], + [1, 1, 1, 1, '2023-06-08', 80, '20123456789', 'PES', 1, 100, 21, 5, 0, 0, 126, 1, '2023-06-08', '2023-06-08', + '2023-06-08', '1234567890', '2023-06-18', 'A', '', '', 'John Doe', '123 Main St', 'City', '1234567890', 'A', + 'john@example.com', 'ABC123', 'OC123', 'Responsable Inscripto', 'COT123', 'REM123', 'Observaciones generales', + 'Observaciones comerciales', 'Contado', '', 'P1', 'Producto 1', 2, 7, 50, 100, 0, 5, 21, 1, 'Tributo 1', 100, + 5, 5, 5, 100, 21] ] - result = formato_csv.aplanar(regs) - self.assertEqual(result, expected_data) - - + expected_data = [ + { + 'cbte_nro': 1, + 'tipo_cbte': 1, + 'punto_vta': 1, + 'cbt_numero': 1, + 'fecha_cbte': '2023-06-08', + 'concepto': 1, + 'moneda_id': 'PES', + 'moneda_ctz': 1, + 'tipo_doc': 80, + 'nro_doc': '20123456789', + 'email': 'john@example.com', + 'numero_cliente': 'ABC123', + 'numero_orden_compra': 'OC123', + 'condicion_frente_iva': 'Responsable Inscripto', + 'numero_cotizacion': 'COT123', + 'numero_remito': 'REM123', + 'imp_total': 126, + 'imp_tot_conc': 0, + 'imp_neto': 100, + 'imp_iva': 21, + 'imp_trib': 5, + 'imp_op_ex': 0, + 'fecha_serv_desde': '2023-06-08', + 'fecha_serv_hasta': '2023-06-08', + 'fecha_venc_pago': '2023-06-08', + 'obs_generales': 'Observaciones generales', + 'obs_comerciales': 'Observaciones comerciales', + 'resultado': 'A', + 'cae': '1234567890', + 'fecha_vto': '2023-06-18', + 'reproceso': '', + 'motivo': '', + 'id': 1, + 'detalles': [ + { + 'codigo': 'P1', + 'ds': 'Producto 1', + 'umed': 7, + 'qty': 2, + 'precio': 50, + 'importe': 100, + 'iva_id': 5, + 'imp_iva': 21, + 'bonif': None, + 'despacho': False, + 'dato_a': False, + 'dato_b': False, + 'dato_c': False, + 'dato_d': False, + 'dato_e': False + } + ], + 'tributos': [ + { + 'tributo_id': 1, + 'desc': 'Tributo 1', + 'base_imp': 100, + 'alic': 5, + 'importe': 5 + } + ], + 'ivas': [ + { + 'iva_id': 5, + 'base_imp': 100, + 'importe': 21 + } + ], + 'permisos': [], + 'opcionales': [], + 'cbtes_asoc': [], + 'forma_pago': 'Contado', + 'datos': [ + {'campo': 'nombre', 'valor': 'John Doe', 'pagina': ''}, + {'campo': 'domicilio', 'valor': '123 Main St', 'pagina': ''}, + {'campo': 'localidad', 'valor': 'City', 'pagina': ''}, + {'campo': 'telefono', 'valor': '1234567890', 'pagina': ''}, + {'campo': 'categoria', 'valor': 'A', 'pagina': ''}, + {'campo': 'pdf', 'valor': '', 'pagina': ''} + ] + } + ] + result = desaplanar(filas) + self.assertEqual(result, expected_data) - def test_aplanar_missing_fields(self): + + def test_desaplanar_multiple_records(self): """ - Test that the aplanar function handles records with missing fields correctly. + Test that the desaplanar function correctly converts multiple records from flattened format to structured format. """ - regs = [ - { - "id": 1, - "tipo_cbte": 1, - "punto_vta": 1, - "cbte_nro": 1, - "fecha_cbte": "2023-06-08", - "tipo_doc": 80, - "nro_doc": "20123456789", - "moneda_id": "PES", - "moneda_ctz": 1, - "imp_neto": 100, - "imp_iva": 21, - "imp_trib": 5, - "imp_op_ex": 0, - "imp_tot_conc": 0, - "imp_total": 126, - "concepto": 1, - "fecha_venc_pago": "2023-06-08", - "fecha_serv_desde": "2023-06-08", - "fecha_serv_hasta": "2023-06-08", - "cae": "1234567890", - "fecha_vto": "2023-06-18", - "resultado": "A", - "motivo": "", - "reproceso": "", - "nombre": "John Doe", - "domicilio": "123 Main St", - "localidad": "City", - "telefono": "1234567890", - "categoria": "A", - "email": "john@example.com", - "numero_cliente": "ABC123", - "numero_orden_compra": "OC123", - "condicion_frente_iva": "Responsable Inscripto", - "numero_cotizacion": "COT123", - "numero_remito": "REM123", - "obs_generales": "Observaciones generales", - "obs_comerciales": "Observaciones comerciales", - "detalles": [], - "ivas": [], - "tributos": [], - "opcionales": [], - "cbtes_asoc": [], - "forma_pago": "Contado", - }, + filas = [ + ['id', 'tipo_cbte', 'punto_vta', 'cbt_numero', 'fecha_cbte', 'tipo_doc', 'nro_doc', 'moneda_id', 'moneda_ctz', + 'imp_neto', 'imp_iva', 'imp_trib', 'imp_op_ex', 'imp_tot_conc', 'imp_total', 'concepto', 'fecha_venc_pago', + 'fecha_serv_desde', 'fecha_serv_hasta', 'cae', 'fecha_vto', 'resultado', 'motivo', 'reproceso', 'nombre', + 'domicilio', 'localidad', 'telefono', 'categoria', 'email', 'numero_cliente', 'numero_orden_compra', + 'condicion_frente_iva', 'numero_cotizacion', 'numero_remito', 'obs_generales', 'obs_comerciales', + 'forma_pago', 'pdf', 'codigo1', 'descripcion1', 'cantidad1', 'umed1', 'precio1', 'importe1', 'bonif1', + 'iva_id1', 'imp_iva1', 'tributo_id_1', 'tributo_desc_1', 'tributo_base_imp_1', 'tributo_alic_1', + 'tributo_importe_1', 'iva_id_1', 'iva_base_imp_1', 'iva_importe_1'], + [1, 1, 1, 1, '2023-06-08', 80, '20123456789', 'PES', 1, 100, 21, 5, 0, 0, 126, 1, '2023-06-08', '2023-06-08', + '2023-06-08', '1234567890', '2023-06-18', 'A', '', '', 'John Doe', '123 Main St', 'City', '1234567890', 'A', + 'john@example.com', 'ABC123', 'OC123', 'Responsable Inscripto', 'COT123', 'REM123', 'Observaciones generales', + 'Observaciones comerciales', 'Contado', '', 'P1', 'Producto 1', 2, 7, 50, 100, 0, 5, 21, 1, 'Tributo 1', 100, + 5, 5, 5, 100, 21], + [2, 1, 1, 2, '2023-06-09', 80, '20987654321', 'PES', 1, 200, 42, 10, 0, 0, 252, 1, '2023-06-09', '2023-06-09', + '2023-06-09', '0987654321', '2023-06-19', 'A', '', '', 'Jane Smith', '456 Elm St', 'Town', '0987654321', 'B', + 'jane@example.com', 'XYZ789', 'OC456', 'Responsable Inscripto', 'COT456', 'REM456', 'Observaciones generales', + 'Observaciones comerciales', 'Tarjeta de Crédito', '', 'P2', 'Producto 2', 1, 7, 200, 200, 0, 5, 42, 2, + 'Tributo 2', 200, 5, 10, 5, 200, 42] ] expected_data = [ - [ - "id", - "tipo_cbte", - "punto_vta", - "cbt_numero", - "fecha_cbte", - "tipo_doc", - "nro_doc", - "moneda_id", - "moneda_ctz", - "imp_neto", - "imp_iva", - "imp_trib", - "imp_op_ex", - "imp_tot_conc", - "imp_total", - "concepto", - "fecha_venc_pago", - "fecha_serv_desde", - "fecha_serv_hasta", - "cae", - "fecha_vto", - "resultado", - "motivo", - "reproceso", - "nombre", - "domicilio", - "localidad", - "telefono", - "categoria", - "email", - "numero_cliente", - "numero_orden_compra", - "condicion_frente_iva", - "numero_cotizacion", - "numero_remito", - "obs_generales", - "obs_comerciales", - "provincia_cliente", - "forma_pago", - "pais_dst_cmp", - "id_impositivo", - "moneda", - "tipo_cambio", - "incoterms", - "idioma_cbte", - "motivos_obs", - "cae_cbte", - "fch_venc_cae", - "pais_dst_cliente", - "pdf", - ], - [ - 1, - 1, - 1, - 1, - "2023-06-08", - 80, - "20123456789", - "PES", - 1, - 100, - 21, - 5, - 0, - 0, - 126, - 1, - "2023-06-08", - "2023-06-08", - "2023-06-08", - "1234567890", - "2023-06-18", - "A", - "", - "", - "John Doe", - "123 Main St", - "City", - "1234567890", - "A", - "john@example.com", - "ABC123", - "OC123", - "Responsable Inscripto", - "COT123", - "REM123", - "Observaciones generales", - "Observaciones comerciales", - None, - "Contado", - None, - None, - None, - None, - None, - None, - None, - None, - None, - None, - "", - ], + { + 'cbte_nro': 1, + 'tipo_cbte': 1, + 'punto_vta': 1, + 'cbt_numero': 1, + 'fecha_cbte': '2023-06-08', + 'concepto': 1, + 'moneda_id': 'PES', + 'moneda_ctz': 1, + 'tipo_doc': 80, + 'nro_doc': '20123456789', + 'email': 'john@example.com', + 'numero_cliente': 'ABC123', + 'numero_orden_compra': 'OC123', + 'condicion_frente_iva': 'Responsable Inscripto', + 'numero_cotizacion': 'COT123', + 'numero_remito': 'REM123', + 'imp_total': 126, + 'imp_tot_conc': 0, + 'imp_neto': 100, + 'imp_iva': 21, + 'imp_trib': 5, + 'imp_op_ex': 0, + 'fecha_serv_desde': '2023-06-08', + 'fecha_serv_hasta': '2023-06-08', + 'fecha_venc_pago': '2023-06-08', + 'obs_generales': 'Observaciones generales', + 'obs_comerciales': 'Observaciones comerciales', + 'resultado': 'A', + 'cae': '1234567890', + 'fecha_vto': '2023-06-18', + 'reproceso': '', + 'motivo': '', + 'id': 1, + 'detalles': [ + { + 'codigo': 'P1', + 'ds': 'Producto 1', + 'umed': 7, + 'qty': 2, + 'precio': 50, + 'importe': 100, + 'iva_id': 5, + 'imp_iva': 21, + 'bonif': None, + 'despacho': False, + 'dato_a': False, + 'dato_b': False, + 'dato_c': False, + 'dato_d': False, + 'dato_e': False + } + ], + 'tributos': [ + { + 'tributo_id': 1, + 'desc': 'Tributo 1', + 'base_imp': 100, + 'alic': 5, + 'importe': 5 + } + ], + 'ivas': [ + { + 'iva_id': 5, + 'base_imp': 100, + 'importe': 21 + } + ], + 'permisos': [], + 'opcionales': [], + 'cbtes_asoc': [], + 'forma_pago': 'Contado', + 'datos': [ + {'campo': 'nombre', 'valor': 'John Doe', 'pagina': ''}, + {'campo': 'domicilio', 'valor': '123 Main St', 'pagina': ''}, + {'campo': 'localidad', 'valor': 'City', 'pagina': ''}, + {'campo': 'telefono', 'valor': '1234567890', 'pagina': ''}, + {'campo': 'categoria', 'valor': 'A', 'pagina': ''}, + {'campo': 'pdf', 'valor': '', 'pagina': ''} + ] + }, + { + 'cbte_nro': 2, + 'tipo_cbte': 1, + 'punto_vta': 1, + 'cbt_numero': 2, + 'fecha_cbte': '2023-06-09', + 'concepto': 1, + 'moneda_id': 'PES', + 'moneda_ctz': 1, + 'tipo_doc': 80, + 'nro_doc': '20987654321', + 'email': 'jane@example.com', + 'numero_cliente': 'XYZ789', + 'numero_orden_compra': 'OC456', + 'condicion_frente_iva': 'Responsable Inscripto', + 'numero_cotizacion': 'COT456', + 'numero_remito': 'REM456', + 'imp_total': 252, + 'imp_tot_conc': 0, + 'imp_neto': 200, + 'imp_iva': 42, + 'imp_trib': 10, + 'imp_op_ex': 0, + 'fecha_serv_desde': '2023-06-09', + 'fecha_serv_hasta': '2023-06-09', + 'fecha_venc_pago': '2023-06-09', + 'obs_generales': 'Observaciones generales', + 'obs_comerciales': 'Observaciones comerciales', + 'resultado': 'A', + 'cae': '0987654321', + 'fecha_vto': '2023-06-19', + 'reproceso': '', + 'motivo': '', + 'id': 2, + 'detalles': [ + { + 'codigo': 'P2', + 'ds': 'Producto 2', + 'umed': 7, + 'qty': 1, + 'precio': 200, + 'importe': 200, + 'iva_id': 5, + 'imp_iva': 42, + 'bonif': None, + 'despacho': False, + 'dato_a': False, + 'dato_b': False, + 'dato_c': False, + 'dato_d': False, + 'dato_e': False + } + ], + 'tributos': [ + { + 'tributo_id': 2, + 'desc': 'Tributo 2', + 'base_imp': 200, + 'alic': 5, + 'importe': 10 + } + ], + 'ivas': [ + { + 'iva_id': 5, + 'base_imp': 200, + 'importe': 42 + } + ], + 'permisos': [], + 'opcionales': [], + 'cbtes_asoc': [], + 'forma_pago': 'Tarjeta de Crédito', + 'datos': [ + {'campo': 'nombre', 'valor': 'Jane Smith', 'pagina': ''}, + {'campo': 'domicilio', 'valor': '456 Elm St', 'pagina': ''}, + {'campo': 'localidad', 'valor': 'Town', 'pagina': ''}, + {'campo': 'telefono', 'valor': '0987654321', 'pagina': ''}, + {'campo': 'categoria', 'valor': 'B', 'pagina': ''}, + {'campo': 'pdf', 'valor': '', 'pagina': ''} + ] + } ] - result = formato_csv.aplanar(regs) + result = desaplanar(filas) self.assertEqual(result, expected_data) -# @pytest.mark.dontusefix -# class TestEscribir(unittest.TestCase): -# def test_escribir_csv_file(self): -# data = [ -# ["Column1", "Column2", "Column3"], -# ["Value1", "Value2", "Value3"], -# ["Value4", "Value5", "Value6"], -# ] -# expected_csv_content = "Column1,Column2,Column3\nValue1,Value2,Value3\nValue4,Value5,Value6\n" -# with tempfile.NamedTemporaryFile(suffix=".csv", delete=False) as temp_file: -# formato_csv.escribir(data, temp_file.name) -# with open(temp_file.name, "r") as file: -# csv_content = file.read() -# self.assertEqual(csv_content, expected_csv_content) -# os.unlink(temp_file.name) -# def test_escribir_xlsx_file(self): -# data = [ -# ["Column1", "Column2", "Column3"], -# ["Value1", "Value2", "Value3"], -# ["Value4", "Value5", "Value6"], -# ] -# with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as temp_file: -# formato_csv.escribir(data, temp_file.name) -# workbook = load_workbook(temp_file.name) -# sheet = workbook.active -# self.assertEqual(sheet["A1"].value, "Column1") -# self.assertEqual(sheet["B1"].value, "Column2") -# self.assertEqual(sheet["C1"].value, "Column3") -# self.assertEqual(sheet["A2"].value, "Value1") -# self.assertEqual(sheet["B2"].value, "Value2") -# self.assertEqual(sheet["C2"].value, "Value3") -# self.assertEqual(sheet["A3"].value, "Value4") -# self.assertEqual(sheet["B3"].value, "Value5") -# self.assertEqual(sheet["C3"].value, "Value6") -# os.unlink(temp_file.name) +@pytest.mark.dontusefix +class TestEscribirFunction(unittest.TestCase): + def test_escribir_facturas_csv(self): + """ + Test that the escribir function can write data from facturas.csv correctly. + """ + # Read the contents of facturas.csv + with open('datos/facturas.csv', 'r', newline='') as file: + csv_reader = csv.reader(file, delimiter=';') + filas = [row for row in csv_reader] -# @pytest.mark.dontusefix -# class TestDescribir(unittest.TestCase): -# def test_desaplanar_single_record(self): -# data = [ -# [ -# "id", -# "tipo_cbte", -# "punto_vta", -# "cbt_numero", -# "fecha_cbte", -# "tipo_doc", -# "nro_doc", -# "moneda_id", -# "moneda_ctz", -# "imp_neto", -# "imp_iva", -# "imp_trib", -# "imp_op_ex", -# "imp_tot_conc", -# "imp_total", -# "concepto", -# "fecha_venc_pago", -# "fecha_serv_desde", -# "fecha_serv_hasta", -# "cae", -# "fecha_vto", -# "resultado", -# "motivo", -# "reproceso", -# "nombre", -# "domicilio", -# "localidad", -# "telefono", -# "categoria", -# "email", -# "numero_cliente", -# "numero_orden_compra", -# "condicion_frente_iva", -# "numero_cotizacion", -# "numero_remito", -# "obs_generales", -# "obs_comerciales", -# "forma_pago", -# "pdf", -# "codigo1", -# "descripcion1", -# "umed1", -# "cantidad1", -# "precio1", -# "importe1", -# "iva_id1", -# "imp_iva1", -# "bonif1", -# "numero_despacho1", -# "dato_a1", -# "dato_b1", -# "dato_c1", -# "dato_d1", -# "dato_e1", -# "iva_id_1", -# "iva_base_imp_1", -# "iva_importe_1", -# "tributo_id_1", -# "tributo_base_imp_1", -# "tributo_desc_1", -# "tributo_alic_1", -# "tributo_importe_1", -# "opcional_id_1", -# "opcional_valor_1", -# "cbte_asoc_tipo_1", -# "cbte_asoc_pto_vta_1", -# "cbte_asoc_nro_1", -# "cbte_asoc_cuit_1", -# "cbte_asoc_fecha_1", -# ], -# [ -# 1, -# 1, -# 1, -# 1, -# "2023-06-08", -# 80, -# "20123456789", -# "PES", -# 1, -# 100, -# 21, -# 5, -# 0, -# 0, -# 126, -# 1, -# "2023-06-08", -# "2023-06-08", -# "2023-06-08", -# "1234567890", -# "2023-06-18", -# "A", -# "", -# "", -# "John Doe", -# "123 Main St", -# "City", -# "1234567890", -# "A", -# "john@example.com", -# "ABC123", -# "OC123", -# "Responsable Inscripto", -# "COT123", -# "REM123", -# "Observaciones generales", -# "Observaciones comerciales", -# "Contado", -# "", -# "P1", -# "Producto 1", -# 7, -# 2, -# 50, -# 100, -# 5, -# 21, -# 0, -# "", -# "", -# "", -# "", -# "", -# "", -# 5, -# 100, -# 21, -# 1, -# 100, -# "Tributo 1", -# 5, -# 5, -# 1, -# "Valor opcional 1", -# 1, -# 1, -# 0, -# "20123456789", -# "2023-06-07", -# ], -# ] -# expected_result = [ -# { -# "id": 1, -# "tipo_cbte": 1, -# "punto_vta": 1, -# "cbte_nro": 1, -# "fecha_cbte": "2023-06-08", -# "tipo_doc": 80, -# "nro_doc": "20123456789", -# "moneda_id": "PES", -# "moneda_ctz": 1, -# "imp_neto": 100, -# "imp_iva": 21, -# "imp_trib": 5, -# "imp_op_ex": 0, -# "imp_tot_conc": 0, -# "imp_total": 126, -# "concepto": 1, -# "fecha_venc_pago": "2023-06-08", -# "fecha_serv_desde": "2023-06-08", -# "fecha_serv_hasta": "2023-06-08", -# "cae": "1234567890", -# "fecha_vto": "2023-06-18", -# "resultado": "A", -# "motivo": "", -# "reproceso": "", -# "nombre": "John Doe", -# "domicilio": "123 Main St", -# "localidad": "City", -# "telefono": "1234567890", -# "categoria": "A", -# "email": "john@example.com", -# "numero_cliente": "ABC123", -# "numero_orden_compra": "OC123", -# "condicion_frente_iva": "Responsable Inscripto", -# "numero_cotizacion": "COT123", -# "numero_remito": "REM123", -# "obs_generales": "Observaciones generales", -# "obs_comerciales": "Observaciones comerciales", -# "forma_pago": "Contado", -# "detalles": [ -# { -# "codigo": "P1", -# "ds": "Producto 1", -# "umed": 7, -# "qty": 2, -# "precio": 50, -# "importe": 100, -# "iva_id": 5, -# "imp_iva": 21, -# "bonif": 0, -# "despacho": None, -# "dato_a": None, -# "dato_b": None, -# "dato_c": None, -# "dato_d": None, -# "dato_e": None, -# } -# ], -# "tributos": [ -# { -# "tributo_id": 1, -# "desc": "Tributo 1", -# "base_imp": 100, -# "alic": 5, -# "importe": 5, -# } -# ], -# "ivas": [ -# { -# "iva_id": 5, -# "base_imp": 100, -# "importe": 21, -# } -# ], -# "permisos": [], -# "opcionales": [ -# { -# "opcional_id": 1, -# "valor": "Valor opcional 1", -# } -# ], -# "cbtes_asoc": [ -# { -# "cbte_tipo": 1, -# "cbte_punto_vta": 1, -# "cbte_nro": 0, -# "cbte_cuit": "20123456789", -# "cbte_fecha": "2023-06-07", -# } -# ], -# "datos": [], -# } -# ] -# result = formato_csv.desaplanar(data) -# self.assertEqual(result, expected_result) + # Write the data to a new file using the escribir function + filename = 'test_facturas_output.csv' + escribir(filas, filename) -# def test_desaplanar_multiple_records(self): -# data = [ -# [ -# "id", -# "tipo_cbte", -# "punto_vta", -# "cbt_numero", -# "fecha_cbte", -# "tipo_doc", -# "nro_doc", -# "moneda_id", -# "moneda_ctz", -# "imp_neto", -# "imp_iva", -# "imp_trib", -# "imp_op_ex", -# "imp_tot_conc", -# "imp_total", -# "concepto", -# "fecha_venc_pago", -# "fecha_serv_desde", -# "fecha_serv_hasta", -# "cae", -# "fecha_vto", -# "resultado", -# "motivo", -# "reproceso", -# "nombre", -# "domicilio", -# "localidad", -# "telefono", -# "categoria", -# "email", -# "numero_cliente", -# "numero_orden_compra", -# "condicion_frente_iva", -# "numero_cotizacion", -# "numero_remito", -# "obs_generales", -# "obs_comerciales", -# "forma_pago", -# "pdf", -# "codigo1", -# "descripcion1", -# "umed1", -# "cantidad1", -# "precio1", -# "importe1", -# "iva_id1", -# "imp_iva1", -# "bonif1", -# "numero_despacho1", -# "dato_a1", -# "dato_b1", -# "dato_c1", -# "dato_d1", -# "dato_e1", -# "iva_id_1", -# "iva_base_imp_1", -# "iva_importe_1", -# "tributo_id_1", -# "tributo_base_imp_1", -# "tributo_desc_1", -# "tributo_alic_1", -# "tributo_importe_1", -# "opcional_id_1", -# "opcional_valor_1", -# "cbte_asoc_tipo_1", -# "cbte_asoc_pto_vta_1", -# "cbte_asoc_nro_1", -# "cbte_asoc_cuit_1", -# "cbte_asoc_fecha_1", -# "codigo2", -# "descripcion2", -# "umed2", -# "cantidad2", -# "precio2", -# "importe2", -# "iva_id2", -# "imp_iva2", -# "bonif2", -# "numero_despacho2", -# "dato_a2", -# "dato_b2", -# "dato_c2", -# "dato_d2", -# "dato_e2", -# "iva_id_2", -# "iva_base_imp_2", -# "iva_importe_2", -# "tributo_id_2", -# "tributo_base_imp_2", -# "tributo_desc_2", -# "tributo_alic_2", -# "tributo_importe_2", -# "opcional_id_2", -# "opcional_valor_2", -# "cbte_asoc_tipo_2", -# "cbte_asoc_pto_vta_2", -# "cbte_asoc_nro_2", -# "cbte_asoc_cuit_2", -# "cbte_asoc_fecha_2", -# ], -# [ -# 1, -# 1, -# 1, -# 1, -# "2023-06-08", -# 80, -# "20123456789", -# "PES", -# 1, -# 100, -# 21, -# 5, -# 0, -# 0, -# 126, -# 1, -# "2023-06-08", -# "2023-06-08", -# "2023-06-08", -# "1234567890", -# "2023-06-18", -# "A", -# "", -# "", -# "John Doe", -# "123 Main St", -# "City", -# "1234567890", -# "A", -# "john@example.com", -# "ABC123", -# "OC123", -# "Responsable Inscripto", -# "COT123", -# "REM123", -# "Observaciones generales", -# "Observaciones comerciales", -# "Contado", -# "", -# "P1", -# "Producto 1", -# 7, -# 2, -# 50, -# 100, -# 5, -# 21, -# 0, -# "", -# "", -# "", -# "", -# "", -# "", -# 5, -# 100, -# 21, -# 1, -# 100, -# "Tributo 1", -# 5, -# 5, -# 1, -# "Valor opcional 1", -# 1, -# 1, -# 0, -# "20123456789", -# "2023-06-07", -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# ], -# [ -# 2, -# 1, -# 1, -# 2, -# "2023-06-09", -# 80, -# "20987654321", -# "PES", -# 1, -# 200, -# 42, -# 10, -# 0, -# 0, -# 252, -# 1, -# "2023-06-09", -# "2023-06-09", -# "2023-06-09", -# "0987654321", -# "2023-06-19", -# "A", -# "", -# "", -# "Jane Smith", -# "456 Elm St", -# "Town", -# "0987654321", -# "B", -# "jane@example.com", -# "XYZ789", -# "OC456", -# "Responsable Inscripto", -# "COT456", -# "REM456", -# "Observaciones generales", -# "Observaciones comerciales", -# "Tarjeta de Crédito", -# "", -# "P2", -# "Producto 2", -# 7, -# 1, -# 200, -# 200, -# 5, -# 42, -# 0, -# "", -# "", -# "", -# "", -# "", -# "", -# 5, -# 200, -# 42, -# 2, -# 200, -# "Tributo 2", -# 5, -# 10, -# 2, -# "Valor opcional 2", -# 1, -# 1, -# 1, -# "20123456789", -# "2023-06-08", -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# None, -# ], -# ] -# expected_result = [ -# { -# "id": 1, -# "tipo_cbte": 1, -# "punto_vta": 1, -# "cbte_nro": 1, -# "fecha_cbte": "2023-06-08", -# "tipo_doc": 80, -# "nro_doc": "20123456789", -# "moneda_id": "PES", -# "moneda_ctz": 1, -# "imp_neto": 100, -# "imp_iva": 21, -# "imp_trib": 5, -# "imp_op_ex": 0, -# "imp_tot_conc": 0, -# "imp_total": 126, -# "concepto": 1, -# "fecha_venc_pago": "2023-06-08", -# "fecha_serv_desde": "2023-06-08", -# "fecha_serv_hasta": "2023-06-08", -# "cae": "1234567890", -# "fecha_vto": "2023-06-18", -# "resultado": "A", -# "motivo": "", -# "reproceso": "", -# "nombre": "John Doe", -# "domicilio": "123 Main St", -# "localidad": "City", -# "telefono": "1234567890", -# "categoria": "A", -# "email": "john@example.com", -# "numero_cliente": "ABC123", -# "numero_orden_compra": "OC123", -# "condicion_frente_iva": "Responsable Inscripto", -# "numero_cotizacion": "COT123", -# "numero_remito": "REM123", -# "obs_generales": "Observaciones generales", -# "obs_comerciales": "Observaciones comerciales", -# "forma_pago": "Contado", -# "detalles": [ -# { -# "codigo": "P1", -# "ds": "Producto 1", -# "umed": 7, -# "qty": 2, -# "precio": 50, -# "importe": 100, -# "iva_id": 5, -# "imp_iva": 21, -# "bonif": 0, -# "despacho": None, -# "dato_a": None, -# "dato_b": None, -# "dato_c": None, -# "dato_d": None, -# "dato_e": None, -# } -# ], -# "tributos": [ -# { -# "tributo_id": 1, -# "desc": "Tributo 1", -# "base_imp": 100, -# "alic": 5, -# "importe": 5, -# } -# ], -# "ivas": [ -# { -# "iva_id": 5, -# "base_imp": 100, -# "importe": 21, -# } -# ], -# "permisos": [], -# "opcionales": [ -# { -# "opcional_id": 1, -# "valor": "Valor opcional 1", -# } -# ], -# "cbtes_asoc": [ -# { -# "cbte_tipo": 1, -# "cbte_punto_vta": 1, -# "cbte_nro": 0, -# "cbte_cuit": "20123456789", -# "cbte_fecha": "2023-06-07", -# } -# ], -# "datos": [], -# }, -# { -# "id": 2, -# "tipo_cbte": 1, -# "punto_vta": 1, -# "cbte_nro": 2, -# "fecha_cbte": "2023-06-09", -# "tipo_doc": 80, -# "nro_doc": "20987654321", -# "moneda_id": "PES", -# "moneda_ctz": 1, -# "imp_neto": 200, -# "imp_iva": 42, -# "imp_trib": 10, -# "imp_op_ex": 0, -# "imp_tot_conc": 0, -# "imp_total": 252, -# "concepto": 1, -# "fecha_venc_pago": "2023-06-09", -# "fecha_serv_desde": "2023-06-09", -# "fecha_serv_hasta": "2023-06-09", -# "cae": "0987654321", -# "fecha_vto": "2023-06-19", -# "resultado": "A", -# "motivo": "", -# "reproceso": "", -# "nombre": "Jane Smith", -# "domicilio": "456 Elm St", -# "localidad": "Town", -# "telefono": "0987654321", -# "categoria": "B", -# "email": "jane@example.com", -# "numero_cliente": "XYZ789", -# "numero_orden_compra": "OC456", -# "condicion_frente_iva": "Responsable Inscripto", -# "numero_cotizacion": "COT456", -# "numero_remito": "REM456", -# "obs_generales": "Observaciones generales", -# "obs_comerciales": "Observaciones comerciales", -# "forma_pago": "Tarjeta de Crédito", -# "detalles": [ -# { -# "codigo": "P2", -# "ds": "Producto 2", -# "umed": 7, -# "qty": 1, -# "precio": 200, -# "importe": 200, -# "iva_id": 5, -# "imp_iva": 42, -# "bonif": 0, -# "despacho": None, -# "dato_a": None, -# "dato_b": None, -# "dato_c": None, -# "dato_d": None, -# "dato_e": None, -# } -# ], -# "tributos": [ -# { -# "tributo_id": 2, -# "desc": "Tributo 2", -# "base_imp": 200, -# "alic": 5, -# "importe": 10, -# } -# ], -# "ivas": [ -# { -# "iva_id": 5, -# "base_imp": 200, -# "importe": 42, -# } -# ], -# "permisos": [], -# "opcionales": [ -# { -# "opcional_id": 2, -# "valor": "Valor opcional 2", -# } -# ], -# "cbtes_asoc": [ -# { -# "cbte_tipo": 1, -# "cbte_punto_vta": 1, -# "cbte_nro": 1, -# "cbte_cuit": "20123456789", -# "cbte_fecha": "2023-06-08", -# } -# ], -# "datos": [], -# }, -# ] -# result = formato_csv.desaplanar(data) -# self.assertEqual(result, expected_result) - -# def test_desaplanar_empty_data(self): -# data = [ -# [ -# "id", -# "tipo_cbte", -# "punto_vta", -# "cbt_numero", -# "fecha_cbte", -# "tipo_doc", -# "nro_doc", -# "moneda_id", -# "moneda_ctz", -# "imp_neto", -# "imp_iva", -# "imp_trib", -# "imp_op_ex", -# "imp_tot_conc", -# "imp_total", -# "concepto", -# "fecha_venc_pago", -# "fecha_serv_desde", -# "fecha_serv_hasta", -# "cae", -# "fecha_vto", -# "resultado", -# "motivo", -# "reproceso", -# "nombre", -# "domicilio", -# "localidad", -# "telefono", -# "categoria", -# "email", -# "numero_cliente", -# "numero_orden_compra", -# "condicion_frente_iva", -# "numero_cotizacion", -# "numero_remito", -# "obs_generales", -# "obs_comerciales", -# "forma_pago", -# "pdf", -# ], -# ] -# expected_result = [] -# result = formato_csv.desaplanar(data) -# self.assertEqual(result, expected_result) + # Check if the file was created + self.assertTrue(os.path.exists(filename)) -# def test_desaplanar_missing_fields(self): -# data = [ -# [ -# "id", -# "tipo_cbte", -# "punto_vta", -# "cbt_numero", -# "fecha_cbte", -# "tipo_doc", -# "nro_doc", -# "moneda_id", -# "moneda_ctz", -# "imp_neto", -# "imp_iva", -# "imp_trib", -# "imp_op_ex", -# "imp_tot_conc", -# "imp_total", -# "concepto", -# "fecha_venc_pago", -# "fecha_serv_desde", -# "fecha_serv_hasta", -# "cae", -# "fecha_vto", -# "resultado", -# "motivo", -# "reproceso", -# "nombre", -# "domicilio", -# "localidad", -# "telefono", -# "categoria", -# "email", -# "numero_cliente", -# "numero_orden_compra", -# "condicion_frente_iva", -# "numero_cotizacion", -# "numero_remito", -# "obs_generales", -# "obs_comerciales", -# "forma_pago", -# "pdf", -# ], -# [ -# 1, -# 1, -# 1, -# 1, -# "2023-06-08", -# 80, -# "20123456789", -# "PES", -# 1, -# 100, -# 21, -# 5, -# 0, -# 0, -# 126, -# 1, -# "2023-06-08", -# "2023-06-08", -# "2023-06-08", -# "1234567890", -# "2023-06-18", -# "A", -# "", -# "", -# "John Doe", -# "123 Main St", -# "City", -# "1234567890", -# "A", -# "john@example.com", -# "ABC123", -# "OC123", -# "Responsable Inscripto", -# "COT123", -# "REM123", -# "Observaciones generales", -# "Observaciones comerciales", -# "Contado", -# "", -# ], -# ] -# expected_result = [ -# { -# "id": 1, -# "tipo_cbte": 1, -# "punto_vta": 1, -# "cbte_nro": 1, -# "fecha_cbte": "2023-06-08", -# "tipo_doc": 80, -# "nro_doc": "20123456789", -# "moneda_id": "PES", -# "moneda_ctz": 1, -# "imp_neto": 100, -# "imp_iva": 21, -# "imp_trib": 5, -# "imp_op_ex": 0, -# "imp_tot_conc": 0, -# "imp_total": 126, -# "concepto": 1, -# "fecha_venc_pago": "2023-06-08", -# "fecha_serv_desde": "2023-06-08", -# "fecha_serv_hasta": "2023-06-08", -# "cae": "1234567890", -# "fecha_vto": "2023-06-18", -# "resultado": "A", -# "motivo": "", -# "reproceso": "", -# "nombre": "John Doe", -# "domicilio": "123 Main St", -# "localidad": "City", -# "telefono": "1234567890", -# "categoria": "A", -# "email": "john@example.com", -# "numero_cliente": "ABC123", -# "numero_orden_compra": "OC123", -# "condicion_frente_iva": "Responsable Inscripto", -# "numero_cotizacion": "COT123", -# "numero_remito": "REM123", -# "obs_generales": "Observaciones generales", -# "obs_comerciales": "Observaciones comerciales", -# "forma_pago": "Contado", -# "detalles": [], -# "tributos": [], -# "ivas": [], -# "permisos": [], -# "opcionales": [], -# "cbtes_asoc": [], -# "datos": [], -# } -# ] -# result = formato_csv.desaplanar(data) -# self.assertEqual(result, expected_result) + # Read the contents of the output file and compare with the original data + with open(filename, 'r', newline='') as file: + csv_reader = csv.reader(file, delimiter=';') + result = [row for row in csv_reader] + self.assertEqual(result, filas) -# def test_desaplanar_extra_fields(self): -# data = [ -# [ -# "id", -# "tipo_cbte", -# "punto_vta", -# "cbt_numero", -# "fecha_cbte", -# "tipo_doc", -# "nro_doc", -# "moneda_id", -# "moneda_ctz", -# "imp_neto", -# "imp_iva", -# "imp_trib", -# "imp_op_ex", -# "imp_tot_conc", -# "imp_total", -# "concepto", -# "fecha_venc_pago", -# "fecha_serv_desde", -# "fecha_serv_hasta", -# "cae", -# "fecha_vto", -# "resultado", -# "motivo", -# "reproceso", -# "nombre", -# "domicilio", -# "localidad", -# "telefono", -# "categoria", -# "email", -# "numero_cliente", -# "numero_orden_compra", -# "condicion_frente_iva", -# "numero_cotizacion", -# "numero_remito", -# "obs_generales", -# "obs_comerciales", -# "forma_pago", -# "pdf", -# "extra_field1", -# "extra_field2", -# ], -# [ -# 1, -# 1, -# 1, -# 1, -# "2023-06-08", -# 80, -# "20123456789", -# "PES", -# 1, -# 100, -# 21, -# 5, -# 0, -# 0, -# 126, -# 1, -# "2023-06-08", -# "2023-06-08", -# "2023-06-08", -# "1234567890", -# "2023-06-18", -# "A", -# "", -# "", -# "John Doe", -# "123 Main St", -# "City", -# "1234567890", -# "A", -# "john@example.com", -# "ABC123", -# "OC123", -# "Responsable Inscripto", -# "COT123", -# "REM123", -# "Observaciones generales", -# "Observaciones comerciales", -# "Contado", -# "", -# "Extra Value 1", -# "Extra Value 2", -# ], -# ] -# expected_result = [ -# { -# "id": 1, -# "tipo_cbte": 1, -# "punto_vta": 1, -# "cbte_nro": 1, -# "fecha_cbte": "2023-06-08", -# "tipo_doc": 80, -# "nro_doc": "20123456789", -# "moneda_id": "PES", -# "moneda_ctz": 1, -# "imp_neto": 100, -# "imp_iva": 21, -# "imp_trib": 5, -# "imp_op_ex": 0, -# "imp_tot_conc": 0, -# "imp_total": 126, -# "concepto": 1, -# "fecha_venc_pago": "2023-06-08", -# "fecha_serv_desde": "2023-06-08", -# "fecha_serv_hasta": "2023-06-08", -# "cae": "1234567890", -# "fecha_vto": "2023-06-18", -# "resultado": "A", -# "motivo": "", -# "reproceso": "", -# "nombre": "John Doe", -# "domicilio": "123 Main St", -# "localidad": "City", -# "telefono": "1234567890", -# "categoria": "A", -# "email": "john@example.com", -# "numero_cliente": "ABC123", -# "numero_orden_compra": "OC123", -# "condicion_frente_iva": "Responsable Inscripto", -# "numero_cotizacion": "COT123", -# "numero_remito": "REM123", -# "obs_generales": "Observaciones generales", -# "obs_comerciales": "Observaciones comerciales", -# "forma_pago": "Contado", -# "detalles": [], -# "tributos": [], -# "ivas": [], -# "permisos": [], -# "opcionales": [], -# "cbtes_asoc": [], -# "datos": [ -# { -# "campo": "extra_field1", -# "valor": "Extra Value 1", -# "pagina": "", -# }, -# { -# "campo": "extra_field2", -# "valor": "Extra Value 2", -# "pagina": "", -# }, -# ], -# } -# ] -# result = formato_csv.desaplanar(data) -# self.assertEqual(result, expected_result) + # Clean up the test file + os.remove(filename) + def test_escribir_facturas_xlsx(self): + """ + Test that the escribir function can write data from facturas.csv to an XLSX file correctly. + """ + # Read the contents of facturas.csv + with open('datos/facturas.csv', 'r', newline='') as file: + csv_reader = csv.reader(file, delimiter=';') + filas = [row for row in csv_reader] + + # Write the data to an XLSX file using the escribir function + filename = 'test_facturas_output.xlsx' + escribir(filas, filename) + + # Check if the file was created + self.assertTrue(os.path.exists(filename)) + + # Read the contents of the XLSX file and compare with the original data + workbook = load_workbook(filename) + sheet = workbook.active + result = [] + for row in sheet.iter_rows(values_only=True): + # Convert None values to empty strings + row = ['' if cell is None else str(cell) for cell in row] + result.append(row) + self.assertEqual(result, filas) + + # Clean up the test file + os.remove(filename) + if __name__ == '__main__': unittest.main()