262 lines
9.1 KiB
C#
262 lines
9.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using CtrEditor.HydraulicSimulator.Python;
|
|
using CtrEditor.HydraulicSimulator.TSNet;
|
|
using CtrEditor.ObjetosSim;
|
|
|
|
namespace CtrEditor.HydraulicSimulator.Tests
|
|
{
|
|
/// <summary>
|
|
/// Pruebas de integración para TSNet
|
|
/// </summary>
|
|
public static class TSNetIntegrationTest
|
|
{
|
|
/// <summary>
|
|
/// Ejecuta todas las pruebas de integración TSNet
|
|
/// </summary>
|
|
public static async Task<bool> RunAllTestsAsync()
|
|
{
|
|
Console.WriteLine("=== TSNet Integration Tests ===");
|
|
|
|
bool allPassed = true;
|
|
|
|
// Test 1: Inicialización de Python
|
|
allPassed &= await TestPythonInitialization();
|
|
|
|
// Test 2: Verificación de TSNet
|
|
allPassed &= await TestTSNetAvailability();
|
|
|
|
// Test 3: Ejecución de script básico
|
|
allPassed &= await TestBasicScriptExecution();
|
|
|
|
// Test 4: Creación de TSNetSimulationManager
|
|
allPassed &= await TestTSNetSimulationManager();
|
|
|
|
Console.WriteLine($"\n=== Tests Result: {(allPassed ? "PASSED" : "FAILED")} ===");
|
|
return allPassed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test 1: Inicialización de Python
|
|
/// </summary>
|
|
private static async Task<bool> TestPythonInitialization()
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine("\n1. Testing Python Initialization...");
|
|
|
|
// Verificar que los archivos existen
|
|
var pythonDll = PythonInterop.PythonDll;
|
|
Console.WriteLine($" Python DLL path: {pythonDll}");
|
|
|
|
if (!File.Exists(pythonDll))
|
|
{
|
|
Console.WriteLine($" ✗ Python DLL not found: {pythonDll}");
|
|
return false;
|
|
}
|
|
Console.WriteLine(" ✓ Python DLL found");
|
|
|
|
// Inicializar Python
|
|
var initialized = PythonInterop.Initialize();
|
|
Console.WriteLine($" Python initialized: {initialized}");
|
|
|
|
if (initialized)
|
|
{
|
|
Console.WriteLine(" ✓ Python initialization successful");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine(" ✗ Python initialization failed");
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($" ✗ Exception in Python initialization: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test 2: Verificación de TSNet
|
|
/// </summary>
|
|
private static async Task<bool> TestTSNetAvailability()
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine("\n2. Testing TSNet Availability...");
|
|
|
|
var available = PythonInterop.IsTSNetAvailable();
|
|
|
|
if (available)
|
|
{
|
|
Console.WriteLine(" ✓ TSNet is available");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine(" ✗ TSNet is not available");
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($" ✗ Exception checking TSNet: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test 3: Ejecución de script básico
|
|
/// </summary>
|
|
private static async Task<bool> TestBasicScriptExecution()
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine("\n3. Testing Basic Script Execution...");
|
|
|
|
var scriptPath = Path.Combine(
|
|
@"d:\Proyectos\VisualStudio\CtrEditor\HydraulicSimulator\TSNet",
|
|
"test_tsnet_integration.py"
|
|
);
|
|
|
|
Console.WriteLine($" Script path: {scriptPath}");
|
|
|
|
if (!File.Exists(scriptPath))
|
|
{
|
|
Console.WriteLine($" ✗ Test script not found: {scriptPath}");
|
|
return false;
|
|
}
|
|
|
|
var result = await PythonInterop.ExecuteScriptAsync(scriptPath);
|
|
|
|
Console.WriteLine($" Script execution result:");
|
|
Console.WriteLine($" Success: {result.Success}");
|
|
Console.WriteLine($" Exit code: {result.ExitCode}");
|
|
|
|
if (!string.IsNullOrEmpty(result.Output))
|
|
{
|
|
Console.WriteLine($" Output: {result.Output.Substring(0, Math.Min(200, result.Output.Length))}...");
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(result.Error))
|
|
{
|
|
Console.WriteLine($" Error: {result.Error}");
|
|
}
|
|
|
|
if (result.Success)
|
|
{
|
|
Console.WriteLine(" ✓ Script execution successful");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine(" ✗ Script execution failed");
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($" ✗ Exception in script execution: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test 4: Creación de TSNetSimulationManager
|
|
/// </summary>
|
|
private static async Task<bool> TestTSNetSimulationManager()
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine("\n4. Testing TSNetSimulationManager...");
|
|
|
|
using var manager = new TSNetSimulationManager();
|
|
|
|
Console.WriteLine($" Working directory: {manager.WorkingDirectory}");
|
|
Console.WriteLine($" Configuration duration: {manager.Configuration.Duration}s");
|
|
Console.WriteLine($" Configuration time step: {manager.Configuration.TimeStep}s");
|
|
|
|
// Verificar que el directorio de trabajo se crea
|
|
if (Directory.Exists(manager.WorkingDirectory))
|
|
{
|
|
Console.WriteLine(" ✓ Working directory created");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine(" ✗ Working directory not created");
|
|
return false;
|
|
}
|
|
|
|
Console.WriteLine(" ✓ TSNetSimulationManager created successfully");
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($" ✗ Exception creating TSNetSimulationManager: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ejecuta un test específico de generación INP
|
|
/// </summary>
|
|
public static async Task<bool> TestINPGeneration()
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine("\n=== Testing INP Generation ===");
|
|
|
|
using var manager = new TSNetSimulationManager();
|
|
|
|
// Simular un tanque simple
|
|
var mockTank = CreateMockTank();
|
|
manager.RegisterHydraulicObject(mockTank);
|
|
|
|
// Generar archivo INP
|
|
var inpFile = await manager.GenerateINPFileAsync();
|
|
|
|
if (File.Exists(inpFile))
|
|
{
|
|
Console.WriteLine($"✓ INP file generated: {inpFile}");
|
|
|
|
// Leer y mostrar parte del contenido
|
|
var content = await File.ReadAllTextAsync(inpFile);
|
|
var preview = content.Substring(0, Math.Min(500, content.Length));
|
|
Console.WriteLine($"INP Preview:\n{preview}...");
|
|
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("✗ INP file not generated");
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"✗ Exception in INP generation test: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Crea un tanque mock para pruebas
|
|
/// </summary>
|
|
private static osHydTank CreateMockTank()
|
|
{
|
|
return new osHydTank
|
|
{
|
|
Nombre = "TankTest",
|
|
Diameter = Math.Sqrt(1.0 / Math.PI) * 2, // Diámetro que da área = 1.0
|
|
MaxLevel = 2.0,
|
|
CurrentLevel = 1.0,
|
|
TankPressure = 1.013,
|
|
IsFixedPressure = false
|
|
};
|
|
}
|
|
}
|
|
}
|