Spaces:
Runtime error
Runtime error
Download tests/test_db_generator.py from DesarrolloTT/DataSuitePro: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/spaces/DesarrolloTT/DataSuitePro/resolve/main/tests/test_db_generator.py
- Command line
-
hf download hf://spaces/DesarrolloTT/DataSuitePro/tests/test_db_generator.py
-
curl -L -o test_db_generator.py https://huggingface.co/spaces/DesarrolloTT/DataSuitePro/resolve/main/tests/test_db_generator.py
3.72 kB
| # tests/test_db_generator.py | |
| import pytest | |
| import pandas as pd | |
| from pathlib import Path | |
| import tempfile | |
| import os | |
| import sqlite3 | |
| from backend.db_generator.database_factory import DatabaseFactory, DatabaseConfig | |
| class TestDatabaseFactory: | |
| """Tests para el generador de bases de datos""" | |
| def setup_method(self): | |
| self.factory = DatabaseFactory() | |
| self.test_data = pd.DataFrame({ | |
| 'user_id': [1, 2, 3], | |
| 'user_name': ['Alice', 'Bob', 'Charlie'], | |
| 'age': [25, 30, 35], | |
| 'city': ['Madrid', 'Barcelona', 'Valencia'] | |
| }) | |
| def test_sqlite_generation(self): | |
| """Test generaci贸n de base de datos SQLite""" | |
| with tempfile.TemporaryDirectory() as temp_dir: | |
| db_config = DatabaseConfig( | |
| db_type="sqlite", | |
| db_name="test_db", | |
| normalize=True, | |
| create_indexes=True | |
| ) | |
| output_dir = Path(temp_dir) | |
| result = self.factory.generate_database( | |
| self.test_data, db_config, output_dir | |
| ) | |
| assert result["success"] == True | |
| assert result["db_type"] == "sqlite" | |
| # Verificar que se cre贸 el archivo de BD | |
| db_path = Path(result["db_path"]) | |
| assert db_path.exists() | |
| assert db_path.stat().st_size > 0 | |
| # Verificar que la BD es v谩lida | |
| conn = sqlite3.connect(db_path) | |
| cursor = conn.cursor() | |
| # Verificar tabla | |
| cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") | |
| tables = cursor.fetchall() | |
| table_names = [table[0] for table in tables] | |
| assert any("test_db" in table_name for table_name in table_names) | |
| # Verificar datos | |
| cursor.execute("SELECT COUNT(*) FROM test_db;") | |
| count = cursor.fetchone()[0] | |
| assert count == 3 | |
| conn.close() | |
| def test_sql_script_generation(self): | |
| """Test generaci贸n de scripts SQL""" | |
| with tempfile.TemporaryDirectory() as temp_dir: | |
| for db_type in ['mysql', 'postgresql']: | |
| db_config = DatabaseConfig( | |
| db_type=db_type, | |
| db_name="test_db", | |
| normalize=True, | |
| create_indexes=True | |
| ) | |
| output_dir = Path(temp_dir) | |
| result = self.factory.generate_database( | |
| self.test_data, db_config, output_dir | |
| ) | |
| assert result["success"] == True | |
| assert result["db_type"] == db_type | |
| # Verificar que se cre贸 el script SQL | |
| sql_script_path = Path(result["sql_script"]) | |
| assert sql_script_path.exists() | |
| # Verificar contenido del script | |
| content = sql_script_path.read_text() | |
| assert "CREATE TABLE" in content | |
| assert "INSERT INTO" in content | |
| def test_invalid_db_type(self): | |
| """Test con tipo de BD inv谩lido""" | |
| with tempfile.TemporaryDirectory() as temp_dir: | |
| db_config = DatabaseConfig( | |
| db_type="invalid_db", | |
| db_name="test_db" | |
| ) | |
| output_dir = Path(temp_dir) | |
| result = self.factory.generate_database( | |
| self.test_data, db_config, output_dir | |
| ) | |
| assert result["success"] == False | |
| assert "error" in result |