DataSuitePro / tests /test_converter.py
DePol1's picture
subida inicial
68e9069 verified
Raw History Blame Contribute Delete
3.27 kB
# tests/test_converter.py
import pytest
import pandas as pd
from pathlib import Path
import tempfile
import os
import json
from backend.converters.converter_engine import UniversalConverter, ConversionError
class TestUniversalConverter:
"""Tests para el motor de conversión universal"""
def setup_method(self):
self.converter = UniversalConverter()
self.test_data = pd.DataFrame({
'id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35]
})
def test_csv_to_json_conversion(self):
"""Test conversión CSV → JSON"""
# CORRECCIÓN: Crear archivo CSV temporal correctamente
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False, encoding='utf-8') as tmp_csv:
self.test_data.to_csv(tmp_csv, index=False)
tmp_csv_path = tmp_csv.name
try:
# CORRECCIÓN: Pasar options como diccionario vacío
json_path = self.converter.convert(tmp_csv_path, '.json', options={})
# Verificar que el archivo existe
assert Path(json_path).exists()
# Verificar que contiene datos JSON válidos
with open(json_path, 'r', encoding='utf-8') as f:
json_data = json.load(f)
assert len(json_data) == 3
assert json_data[0]['name'] == 'Alice'
finally:
# Limpiar archivos temporales
if Path(tmp_csv_path).exists():
os.unlink(tmp_csv_path)
if 'json_path' in locals() and Path(json_path).exists():
os.unlink(json_path)
def test_invalid_input_file(self):
"""Test con archivo de entrada inválido"""
with pytest.raises(ConversionError):
self.converter.convert("archivo_inexistente.csv", ".json", options={})
def test_unsupported_format(self):
"""Test con formato no soportado"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.pdf', delete=False) as tmp:
tmp_path = tmp.name
try:
with pytest.raises(ConversionError):
self.converter.convert(tmp_path, ".json", options={})
finally:
if Path(tmp_path).exists():
os.unlink(tmp_path)
def test_with_custom_options(self):
"""Test con opciones personalizadas"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False, encoding='utf-8') as tmp_csv:
self.test_data.to_csv(tmp_csv, index=False)
tmp_csv_path = tmp_csv.name
try:
# Probar con opciones personalizadas
options = {
'csv': {'sep': ','},
'json': {'orient': 'records'},
'export': {'index': False}
}
json_path = self.converter.convert(tmp_csv_path, '.json', options=options)
assert Path(json_path).exists()
finally:
if Path(tmp_csv_path).exists():
os.unlink(tmp_csv_path)
if 'json_path' in locals() and Path(json_path).exists():
os.unlink(json_path)