|
|
|
@ -1,107 +0,0 @@
|
|
|
|
|
using Xunit;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using Docker.DotNet;
|
|
|
|
|
using Docker.DotNet.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using API.Service;
|
|
|
|
|
|
|
|
|
|
public class QueryDataServiceApiTests : IAsyncLifetime
|
|
|
|
|
{
|
|
|
|
|
private string _connectionString;
|
|
|
|
|
private string _containerId;
|
|
|
|
|
private const string DatabaseName = "SQLuedo";
|
|
|
|
|
|
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
|
|
|
|
// Start a PostgreSQL container
|
|
|
|
|
var dockerClient = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine")).CreateClient();
|
|
|
|
|
var response = await dockerClient.Containers.CreateContainerAsync(new CreateContainerParameters
|
|
|
|
|
{
|
|
|
|
|
Image = "postgres:alpine3.19",
|
|
|
|
|
HostConfig = new HostConfig
|
|
|
|
|
{
|
|
|
|
|
PortBindings = new Dictionary<string, IList<PortBinding>>
|
|
|
|
|
{
|
|
|
|
|
{ "5432", new List<PortBinding> { new PortBinding { HostPort = "5432" } } }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Env = new List<string> { "POSTGRES_PASSWORD=root" }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_containerId = response.ID;
|
|
|
|
|
|
|
|
|
|
await dockerClient.Containers.StartContainerAsync(_containerId, new ContainerStartParameters());
|
|
|
|
|
await Task.Delay(5000);
|
|
|
|
|
|
|
|
|
|
_connectionString = $"Host=localhost;Port=5432;Username=postgres;Password=root;Database={DatabaseName}";
|
|
|
|
|
using (var connection = new NpgsqlConnection(_connectionString))
|
|
|
|
|
{
|
|
|
|
|
await connection.OpenAsync();
|
|
|
|
|
using (var command = new NpgsqlCommand($"SELECT 1 FROM pg_database WHERE datname = '{DatabaseName}'", connection))
|
|
|
|
|
{
|
|
|
|
|
var result = await command.ExecuteScalarAsync();
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
// Create the database only if it does not exist
|
|
|
|
|
using (var createCommand = new NpgsqlCommand($"CREATE DATABASE {DatabaseName}", connection))
|
|
|
|
|
{
|
|
|
|
|
await createCommand.ExecuteNonQueryAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
var dockerClient = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine")).CreateClient();
|
|
|
|
|
await dockerClient.Containers.StopContainerAsync(_containerId, new ContainerStopParameters());
|
|
|
|
|
await dockerClient.Containers.RemoveContainerAsync(_containerId, new ContainerRemoveParameters());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ExecuteQuery_ReturnsErrorMessage_WhenDatabaseNameIsEmpty()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var service = new QueryDataServiceApi();
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = service.ExecuteQuery("SELECT * FROM table", "");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Equal("Le nom de la base de données est requis.", result.Result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add more tests as needed
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void GetTables_ReturnsTables_WhenDatabaseNameIsProvided()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var service = new QueryDataServiceApi();
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = service.GetTables(DatabaseName);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.NotNull(result.Result);
|
|
|
|
|
// Add more assertions as needed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void GetColumns_ReturnsColumns_WhenDatabaseNameAndTableNameAreProvided()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var service = new QueryDataServiceApi();
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = service.GetColumns(DatabaseName, "table_name");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.NotNull(result.Result);
|
|
|
|
|
// Add more assertions as needed
|
|
|
|
|
}
|
|
|
|
|
}
|