# tests/test_data_engine.py import pytest import pandas as pd from pathlib import Path import tempfile import os from engines.data_engine import DataEngine, DataProfile class TestDataEngine: """Tests para el motor de datos""" def setup_method(self): self.engine = DataEngine() self.test_data = pd.DataFrame({ 'user_id': [1, 2, 3, 4, 5], 'user_name': ['Alice', 'Bob', 'Charlie', None, 'Eve'], 'age': [25, 30, 35, 28, 32], 'city': ['Madrid', 'Barcelona', 'Valencia', 'Sevilla', 'Bilbao'] }) def test_load_data_from_csv(self): """Test carga de datos desde CSV""" 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: # Cargar datos success = self.engine.load_data(tmp_csv_path, 'csv') assert success == True assert self.engine.current_data is not None assert len(self.engine.current_data) == 5 # Verificar que se generó el perfil assert self.engine.data_profile is not None assert self.engine.data_profile.total_rows == 5 assert self.engine.data_profile.total_columns == 4 finally: if Path(tmp_csv_path).exists(): os.unlink(tmp_csv_path) def test_data_profile_generation(self): """Test generación de perfil de datos""" 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: self.engine.load_data(tmp_csv_path, 'csv') profile = self.engine.data_profile # Verificar estadísticas básicas assert profile.total_rows == 5 assert profile.total_columns == 4 assert 'user_id' in profile.column_stats assert profile.missing_values['user_name'] == 1 # Un valor nulo # Verificar score de calidad assert 0 <= profile.quality_score <= 100 finally: if Path(tmp_csv_path).exists(): os.unlink(tmp_csv_path) def test_data_preview(self): """Test vista previa de datos""" 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: self.engine.load_data(tmp_csv_path, 'csv') preview = self.engine.get_preview(3) assert preview['total_rows'] == 5 assert preview['total_columns'] == 4 assert len(preview['data']) == 3 assert 'user_id' in preview['headers'] finally: if Path(tmp_csv_path).exists(): os.unlink(tmp_csv_path) def test_schema_detection(self): """Test detección de esquema""" 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: self.engine.load_data(tmp_csv_path, 'csv') schema = self.engine.detect_schema() # Debería detectar user_id como posible primary key assert 'user_id' in schema['potential_primary_keys'] assert len(schema['business_entities']) > 0 finally: if Path(tmp_csv_path).exists(): os.unlink(tmp_csv_path)