-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor options validators and add tests
- Loading branch information
Showing
7 changed files
with
164 additions
and
26 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
DiscordTranslationBot.Tests/Configuration/DiscordOptionsValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using DiscordTranslationBot.Configuration; | ||
using FluentValidation.TestHelper; | ||
|
||
namespace DiscordTranslationBot.Tests.Configuration; | ||
|
||
public sealed class DiscordOptionsValidatorTests | ||
{ | ||
private readonly DiscordOptionsValidator _sut = new(); | ||
|
||
[Fact] | ||
public void Valid_Options_ValidatesWithoutErrors() | ||
{ | ||
// Arrange | ||
var options = new DiscordOptions { BotToken = "token" }; | ||
|
||
// Act | ||
var result = _sut.TestValidate(options); | ||
|
||
// Assert | ||
result.ShouldNotHaveAnyValidationErrors(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
public void Invalid_BotToken_HasValidationErrors(string? botToken) | ||
{ | ||
// Arrange | ||
var options = new DiscordOptions { BotToken = botToken! }; | ||
|
||
// Act | ||
var result = _sut.TestValidate(options); | ||
|
||
// Assert | ||
result.ShouldHaveValidationErrorFor(x => x.BotToken).Only(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...Bot.Tests/Configuration/TranslationProviders/TranslationProvidersOptionsValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using DiscordTranslationBot.Configuration.TranslationProviders; | ||
using FluentValidation.TestHelper; | ||
|
||
namespace DiscordTranslationBot.Tests.Configuration.TranslationProviders; | ||
|
||
public sealed class TranslationProvidersOptionsValidatorTests | ||
{ | ||
private readonly TranslationProvidersOptionsValidator _sut = new(); | ||
|
||
[Fact] | ||
public void Valid_Options_ValidatesWithoutErrors() | ||
{ | ||
// Arrange | ||
var options = new TranslationProvidersOptions | ||
{ | ||
AzureTranslator = new AzureTranslatorOptions | ||
{ | ||
Enabled = true, | ||
ApiUrl = new Uri("http://localhost", UriKind.Absolute), | ||
Region = "westus", | ||
SecretKey = "secret" | ||
}, | ||
LibreTranslate = new LibreTranslateOptions { Enabled = false } | ||
}; | ||
|
||
// Act | ||
var result = _sut.TestValidate(options); | ||
|
||
// Assert | ||
result.ShouldNotHaveAnyValidationErrors(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
public void Invalid_ProviderOptions_HasValidationErrors(string? stringValue) | ||
{ | ||
// Arrange | ||
var options = new TranslationProvidersOptions | ||
{ | ||
AzureTranslator = new AzureTranslatorOptions | ||
{ | ||
Enabled = true, | ||
ApiUrl = null, | ||
Region = stringValue, | ||
SecretKey = stringValue | ||
}, | ||
LibreTranslate = new LibreTranslateOptions { Enabled = true, ApiUrl = null } | ||
}; | ||
|
||
// Act | ||
var result = _sut.TestValidate(options); | ||
|
||
// Assert | ||
result.ShouldHaveValidationErrorFor(x => x.AzureTranslator.ApiUrl); | ||
result.ShouldHaveValidationErrorFor(x => x.AzureTranslator.Region); | ||
result.ShouldHaveValidationErrorFor(x => x.AzureTranslator.SecretKey); | ||
result.ShouldHaveValidationErrorFor(x => x.LibreTranslate.ApiUrl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 14 additions & 4 deletions
18
DiscordTranslationBot/Configuration/TranslationProviders/LibreTranslateOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
namespace DiscordTranslationBot.Configuration.TranslationProviders; | ||
using FluentValidation; | ||
|
||
namespace DiscordTranslationBot.Configuration.TranslationProviders; | ||
|
||
/// <summary> | ||
/// Options for the LibreTranslate provider. | ||
/// </summary> | ||
public sealed class LibreTranslateOptions : TranslationProviderOptionsBase | ||
public sealed class LibreTranslateOptions : TranslationProviderOptionsBase { } | ||
|
||
/// <summary> | ||
/// Validator for <see cref="LibreTranslateOptions" />. | ||
/// </summary> | ||
public sealed class LibreTranslateOptionsValidator : AbstractValidator<LibreTranslateOptions> | ||
{ | ||
/// <summary> | ||
/// The API URL for LibreTranslate. | ||
/// Initializes validation rules. | ||
/// </summary> | ||
public Uri? ApiUrl { get; init; } | ||
public LibreTranslateOptionsValidator() | ||
{ | ||
Include(new TranslationProviderOptionsBaseValidator()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters