Spaces:
Runtime error
Runtime error
File size: 3,868 Bytes
68e9069 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | # 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) |