File size: 3,605 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
102
103
# tests/test_api_generator.py
import pytest
import pandas as pd
from pathlib import Path
import tempfile
import os
from backend.api_generator.api_factory import APIFactory, APIConfig
from engines.data_engine import DataEngine

class TestAPIFactory:
    """Tests para el generador de APIs"""
    
    def setup_method(self):
        self.factory = APIFactory()
        self.data_engine = DataEngine()
        
        # Datos de prueba
        self.test_data = pd.DataFrame({
            'id': [1, 2, 3],
            'name': ['Alice', 'Bob', 'Charlie'],
            'age': [25, 30, 35],
            'city': ['Madrid', 'Barcelona', 'Valencia']
        })
    
    def test_fastapi_generation(self):
        """Test generaci贸n de API FastAPI"""
        with tempfile.TemporaryDirectory() as temp_dir:
            # Crear archivo CSV de prueba
            csv_path = Path(temp_dir) / "test.csv"
            self.test_data.to_csv(csv_path, index=False)
            
            # Cargar datos en el motor
            self.data_engine.load_data(str(csv_path), 'csv')
            
            # Configuraci贸n de la API
            api_config = APIConfig(
                framework="fastapi",
                project_name="TestAPI",
                version="1.0.0",
                authentication=False,
                pagination=True,
                documentation=True,
                database="sqlite"
            )
            
            # Generar API
            output_dir = Path(temp_dir) / "generated_api"
            result = self.factory.generate_fastapi(
                self.data_engine.data_profile,
                api_config,
                output_dir
            )
            
            # Verificar resultados
            assert result["success"] == True
            assert output_dir.exists()
            
            # Verificar que se generaron archivos clave
            expected_files = [
                output_dir / "app" / "main.py",
                output_dir / "requirements.txt"
            ]
            
            for file_path in expected_files:
                assert file_path.exists(), f"Archivo no generado: {file_path}"
    
    def test_endpoint_detection(self):
        """Test detecci贸n autom谩tica de endpoints"""
        with tempfile.TemporaryDirectory() as temp_dir:
            csv_path = Path(temp_dir) / "test.csv"
            self.test_data.to_csv(csv_path, index=False)
            
            self.data_engine.load_data(str(csv_path), 'csv')
            
            # Analizar datos para endpoints
            endpoints = self.factory._analyze_data_for_endpoints(
                self.data_engine.data_profile
            )
            
            # Deber铆a detectar endpoints b谩sicos
            assert len(endpoints) > 0
            
            # Verificar tipos de operaciones
            operations = [ep.operation for ep in endpoints]
            assert "list" in operations
            assert "read" in operations
            assert "create" in operations
    
    def test_entity_detection(self):
        """Test detecci贸n de entidades"""
        with tempfile.TemporaryDirectory() as temp_dir:
            csv_path = Path(temp_dir) / "test.csv"
            self.test_data.to_csv(csv_path, index=False)
            
            self.data_engine.load_data(str(csv_path), 'csv')
            
            entities = self.factory._detect_entities(
                self.data_engine.data_profile
            )
            
            assert len(entities) > 0
            assert "columns" in entities[0]
            assert "name" in entities[0]