add tests
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
using ChatBot.Models.Configuration;
|
||||
using ChatBot.Models.Configuration.Validators;
|
||||
using FluentAssertions;
|
||||
|
||||
namespace ChatBot.Tests.Configuration.Validators;
|
||||
|
||||
public class DatabaseSettingsValidatorTests
|
||||
{
|
||||
private readonly DatabaseSettingsValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnSuccess_WhenSettingsAreValid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new DatabaseSettings
|
||||
{
|
||||
ConnectionString =
|
||||
"Host=localhost;Port=5432;Database=chatbot;Username=user;Password=pass",
|
||||
CommandTimeout = 30,
|
||||
EnableSensitiveDataLogging = false,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenConnectionStringIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new DatabaseSettings
|
||||
{
|
||||
ConnectionString = "",
|
||||
CommandTimeout = 30,
|
||||
EnableSensitiveDataLogging = false,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Database connection string is required"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenCommandTimeoutIsInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new DatabaseSettings
|
||||
{
|
||||
ConnectionString =
|
||||
"Host=localhost;Port=5432;Database=chatbot;Username=user;Password=pass",
|
||||
CommandTimeout = 0, // Invalid: <= 0
|
||||
EnableSensitiveDataLogging = false,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Command timeout must be greater than 0"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Validate_ShouldReturnFailure_WhenConnectionStringIsNullOrWhitespace(
|
||||
string? connectionString
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var settings = new DatabaseSettings
|
||||
{
|
||||
ConnectionString = connectionString!,
|
||||
CommandTimeout = 30,
|
||||
EnableSensitiveDataLogging = false,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Database connection string is required"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user