110 lines
3.6 KiB
C#
110 lines
3.6 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Omika.Data.Configuration;
|
|
|
|
namespace Omika.Tests.Connection;
|
|
|
|
public class HfSqlTestConfigurationLoaderTests
|
|
{
|
|
[Theory]
|
|
[InlineData("Development")]
|
|
[InlineData("development")]
|
|
[InlineData("Test")]
|
|
[InlineData("test")]
|
|
public void IsTestEnvironment_TestEnvironments_ReturnsTrue(string environment)
|
|
{
|
|
Assert.True(HfSqlTestConfigurationLoader.IsTestEnvironment(environment));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Production")]
|
|
[InlineData("Staging")]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
public void IsTestEnvironment_NonTestEnvironments_ReturnsFalse(string? environment)
|
|
{
|
|
Assert.False(HfSqlTestConfigurationLoader.IsTestEnvironment(environment));
|
|
}
|
|
|
|
[Fact]
|
|
public void ResolveConfigPath_ExplicitEnvironmentVariable_ReturnsFullPath()
|
|
{
|
|
var tempFile = Path.Combine(Path.GetTempPath(), $"hfsql-test-{Guid.NewGuid():N}.json");
|
|
try
|
|
{
|
|
File.WriteAllText(tempFile, "{}");
|
|
Environment.SetEnvironmentVariable(
|
|
HfSqlTestConfigurationLoader.ConfigPathEnvironmentVariable, tempFile);
|
|
|
|
var path = HfSqlTestConfigurationLoader.ResolveConfigPath();
|
|
|
|
Assert.NotNull(path);
|
|
Assert.Equal(Path.GetFullPath(tempFile), path);
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable(
|
|
HfSqlTestConfigurationLoader.ConfigPathEnvironmentVariable, null);
|
|
File.Delete(tempFile);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void AddLocalTestConfiguration_ProductionEnvironment_DoesNotLoad()
|
|
{
|
|
var configuration = new ConfigurationManager();
|
|
|
|
var loaded = HfSqlTestConfigurationLoader.AddLocalTestConfiguration(
|
|
configuration, "Production");
|
|
|
|
Assert.False(loaded);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddLocalTestConfiguration_DevelopmentWithExplicitPath_LoadsFile()
|
|
{
|
|
var tempFile = Path.Combine(Path.GetTempPath(), $"hfsql-test-{Guid.NewGuid():N}.json");
|
|
try
|
|
{
|
|
File.WriteAllText(tempFile, """{ "E2E": { "AllowHfSqlWrites": false } }""");
|
|
Environment.SetEnvironmentVariable(
|
|
HfSqlTestConfigurationLoader.ConfigPathEnvironmentVariable, tempFile);
|
|
|
|
var configuration = new ConfigurationManager();
|
|
var loaded = HfSqlTestConfigurationLoader.AddLocalTestConfiguration(
|
|
configuration, "Development");
|
|
|
|
Assert.True(loaded);
|
|
Assert.Equal("False", configuration["E2E:AllowHfSqlWrites"]);
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable(
|
|
HfSqlTestConfigurationLoader.ConfigPathEnvironmentVariable, null);
|
|
File.Delete(tempFile);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void AddLocalTestConfiguration_DevelopmentWithoutFile_DoesNotLoad()
|
|
{
|
|
// Pointe vers un fichier inexistant pour garantir l'absence de chargement.
|
|
var missingPath = Path.Combine(Path.GetTempPath(), $"missing-{Guid.NewGuid():N}.json");
|
|
Environment.SetEnvironmentVariable(
|
|
HfSqlTestConfigurationLoader.ConfigPathEnvironmentVariable, missingPath);
|
|
|
|
try
|
|
{
|
|
var configuration = new ConfigurationManager();
|
|
var loaded = HfSqlTestConfigurationLoader.AddLocalTestConfiguration(
|
|
configuration, "Development");
|
|
|
|
Assert.False(loaded);
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable(
|
|
HfSqlTestConfigurationLoader.ConfigPathEnvironmentVariable, null);
|
|
}
|
|
}
|
|
}
|