Skip to content

Commit

Permalink
YAGNI: simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
austins committed Feb 2, 2024
1 parent 62d1652 commit 7bf8a0b
Show file tree
Hide file tree
Showing 20 changed files with 54 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

namespace DiscordTranslationBot.Tests.Commands.TempReply;

public sealed class DeleteTempReplyValidatorTests : TestBase
public sealed class DeleteTempReplyValidatorTests
{
private readonly DeleteTempReplyValidator _sut;

public DeleteTempReplyValidatorTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public DeleteTempReplyValidatorTests()
{
_sut = new DeleteTempReplyValidator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

namespace DiscordTranslationBot.Tests.Commands.TempReply;

public sealed class SendTempReplyValidatorTests : TestBase
public sealed class SendTempReplyValidatorTests
{
private readonly SendTempReplyValidator _sut;

public SendTempReplyValidatorTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public SendTempReplyValidatorTests()
{
_sut = new SendTempReplyValidator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@

namespace DiscordTranslationBot.Tests.Configuration;

public sealed class DiscordOptionsValidatorTests : TestBase
public sealed class DiscordOptionsValidatorTests
{
private readonly DiscordOptionsValidator _sut = new();

public DiscordOptionsValidatorTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

[Fact]
public void Valid_Options_ValidatesWithoutErrors()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@

namespace DiscordTranslationBot.Tests.Configuration.TranslationProviders;

public sealed class TranslationProvidersOptionsValidatorTests : TestBase
public sealed class TranslationProvidersOptionsValidatorTests
{
private readonly TranslationProvidersOptionsValidator _sut = new();

public TranslationProvidersOptionsValidatorTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

[Fact]
public void Valid_Options_ValidatesWithoutErrors()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Divergic.Logging.Xunit" Version="4.3.0"/>
<PackageReference Include="FluentAssertions" Version="6.12.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace DiscordTranslationBot.Tests.Handlers;

public sealed class FlagEmojiReactionHandlerTests : TestBase
public sealed class FlagEmojiReactionHandlerTests
{
private const string Content = """
👍 test<:disdainsam:630009232128868353> _test_*test*
Expand All @@ -35,8 +35,7 @@ public sealed class FlagEmojiReactionHandlerTests : TestBase
private readonly FlagEmojiReactionHandler _sut;
private readonly TranslationProviderBase _translationProvider;

public FlagEmojiReactionHandlerTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public FlagEmojiReactionHandlerTests()
{
_translationProvider = Substitute.For<TranslationProviderBase>();
_translationProvider.ProviderName.Returns("Test Provider");
Expand Down Expand Up @@ -66,7 +65,7 @@ public FlagEmojiReactionHandlerTests(ITestOutputHelper testOutputHelper)
new[] { _translationProvider },
_countryService,
_mediator,
CreateLogger<FlagEmojiReactionHandler>());
Substitute.For<ILogger<FlagEmojiReactionHandler>>());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

namespace DiscordTranslationBot.Tests.Handlers;

public sealed class RedirectLogMessageToLoggerHandlerTests : TestBase
public sealed class RedirectLogMessageToLoggerHandlerTests
{
private readonly ICacheLogger<RedirectLogMessageToLoggerHandler> _logger;
private readonly ILogger<RedirectLogMessageToLoggerHandler> _logger;
private readonly RedirectLogMessageToLoggerHandler _sut;

public RedirectLogMessageToLoggerHandlerTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public RedirectLogMessageToLoggerHandlerTests()
{
_logger = CreateLogger<RedirectLogMessageToLoggerHandler>(LogLevel.Trace);
_logger = Substitute.For<ILogger<RedirectLogMessageToLoggerHandler>>();
_sut = new RedirectLogMessageToLoggerHandler(_logger);
}

Expand All @@ -26,6 +25,8 @@ public RedirectLogMessageToLoggerHandlerTests(ITestOutputHelper testOutputHelper
public async Task Handle_LogNotification_Success(LogSeverity severity, LogLevel expectedLevel)
{
// Arrange
_logger.IsEnabled(expectedLevel).Returns(true);

var request = new LogNotification
{
LogMessage = new LogMessage(severity, "source1", "message1", new InvalidOperationException("test"))
Expand All @@ -35,9 +36,16 @@ public async Task Handle_LogNotification_Success(LogSeverity severity, LogLevel
await _sut.Handle(request, CancellationToken.None);

// Assert
_logger.Count.Should().Be(1);
_logger.Last!.LogLevel.Should().Be(expectedLevel);
_logger.Last.Exception.Should().Be(request.LogMessage.Exception);
_logger.Last.Message.Should().Be($"Discord {request.LogMessage.Source}: {request.LogMessage.Message}");
var logArgs = _logger.ReceivedCalls()
.Single(call => call.GetMethodInfo().Name == nameof(ILogger.Log))
.GetArguments();

logArgs[0].Should().Be(expectedLevel);

var logMessages = (IReadOnlyList<KeyValuePair<string, object>>)logArgs[2]!;
logMessages[0].Value.Should().Be(request.LogMessage.Source);
logMessages[1].Value.Should().Be(request.LogMessage.Message);

logArgs[3].Should().Be(request.LogMessage.Exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

namespace DiscordTranslationBot.Tests.Handlers;

public sealed class RegisterCommandsHandlerTests : TestBase
public sealed class RegisterCommandsHandlerTests
{
private const string ProviderName = "Test Provider";
private readonly IDiscordClient _client;
private readonly RegisterCommandsHandler _sut;
private readonly TranslationProviderBase _translationProvider;

public RegisterCommandsHandlerTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public RegisterCommandsHandlerTests()
{
_client = Substitute.For<IDiscordClient>();

Expand All @@ -24,7 +23,7 @@ public RegisterCommandsHandlerTests(ITestOutputHelper testOutputHelper)
_sut = new RegisterCommandsHandler(
_client,
new[] { _translationProvider },
CreateLogger<RegisterCommandsHandler>());
Substitute.For<ILogger<RegisterCommandsHandler>>());
}

[Fact]
Expand Down
7 changes: 3 additions & 4 deletions DiscordTranslationBot.Tests/Handlers/TempReplyHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@

namespace DiscordTranslationBot.Tests.Handlers;

public sealed class TempReplyHandlerTests : TestBase
public sealed class TempReplyHandlerTests
{
private readonly IMediator _mediator;

private readonly TempReplyHandler _sut;

public TempReplyHandlerTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public TempReplyHandlerTests()
{
_mediator = Substitute.For<IMediator>();

_sut = new TempReplyHandler(_mediator, CreateLogger<TempReplyHandler>());
_sut = new TempReplyHandler(_mediator, Substitute.For<ILogger<TempReplyHandler>>());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@

namespace DiscordTranslationBot.Tests.Handlers;

public sealed class TranslateMessageCommandHandlerTests : TestBase
public sealed class TranslateMessageCommandHandlerTests
{
private const ulong BotUserId = 1UL;
private readonly IMessageCommandInteraction _command;
private readonly IMessage _message;
private readonly TranslateMessageCommandHandler _sut;
private readonly IReadOnlyList<TranslationProviderBase> _translationProviders;

public TranslateMessageCommandHandlerTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public TranslateMessageCommandHandlerTests()
{
var client = Substitute.For<IDiscordClient>();
client.CurrentUser.Id.Returns(BotUserId);
Expand All @@ -30,7 +29,7 @@ public TranslateMessageCommandHandlerTests(ITestOutputHelper testOutputHelper)
_sut = Substitute.ForPartsOf<TranslateMessageCommandHandler>(
client,
_translationProviders,
CreateLogger<TranslateMessageCommandHandler>());
Substitute.For<ILogger<TranslateMessageCommandHandler>>());

_message = Substitute.For<IMessage>();
_message.Author.Id.Returns(2UL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@

namespace DiscordTranslationBot.Tests.Handlers;

public sealed class TranslateSlashCommandHandlerTests : TestBase
public sealed class TranslateSlashCommandHandlerTests
{
private const string ProviderName = "Test Provider";
private readonly TranslateSlashCommandHandler _sut;
private readonly TranslationProviderBase _translationProvider;

public TranslateSlashCommandHandlerTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public TranslateSlashCommandHandlerTests()
{
_translationProvider = Substitute.For<TranslationProviderBase>();
_translationProvider.ProviderName.Returns(ProviderName);

_sut = new TranslateSlashCommandHandler(
new[] { _translationProvider },
CreateLogger<TranslateSlashCommandHandler>());
Substitute.For<ILogger<TranslateSlashCommandHandler>>());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@

namespace DiscordTranslationBot.Tests.Mediator;

public sealed class BackgroundCommandBehaviorTests : TestBase
public sealed class BackgroundCommandBehaviorTests
{
public BackgroundCommandBehaviorTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

[Fact]
public async Task Handle_NotABackgroundCommand_Success()
{
// Arrange
var sut = new BackgroundCommandBehavior<IRequest<int>, int>(
CreateLogger<BackgroundCommandBehavior<IRequest<int>, int>>());
Substitute.For<ILogger<BackgroundCommandBehavior<IRequest<int>, int>>>());

var request = Substitute.For<IRequest<int>>();

Expand All @@ -35,7 +30,7 @@ public async Task Handle_BackgroundCommand_InvalidDelay_Throws(int seconds)
{
// Arrange
var sut = new BackgroundCommandBehavior<IRequest, Unit>(
CreateLogger<BackgroundCommandBehavior<IRequest, Unit>>());
Substitute.For<ILogger<BackgroundCommandBehavior<IRequest, Unit>>>());

var request = Substitute.For<IBackgroundCommand>();
request.Delay.Returns(TimeSpan.FromSeconds(seconds));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

namespace DiscordTranslationBot.Tests.Mediator;

public sealed class ValidationBehaviorTests : TestBase
public sealed class ValidationBehaviorTests
{
private readonly IBaseRequest _request;

private readonly ValidationBehavior<IBaseRequest, Unit> _sut;
private readonly IValidator<IBaseRequest> _validator;

public ValidationBehaviorTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public ValidationBehaviorTests()
{
_request = Substitute.For<IBaseRequest>();
_validator = Substitute.For<IValidator<IBaseRequest>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ namespace DiscordTranslationBot.Tests.Providers.Translation.AzureTranslator;
public sealed class AzureTranslatorProviderTests : TranslationProviderBaseTests
{
private readonly IAzureTranslatorClient _client;
private readonly ICacheLogger<AzureTranslatorProvider> _logger;
private readonly ILogger<AzureTranslatorProvider> _logger;

public AzureTranslatorProviderTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public AzureTranslatorProviderTests()
{
_client = Substitute.For<IAzureTranslatorClient>();

Expand All @@ -33,7 +32,7 @@ public AzureTranslatorProviderTests(ITestOutputHelper testOutputHelper)

_client.GetLanguagesAsync(default).ReturnsForAnyArgs(languagesResponse);

_logger = CreateLogger<AzureTranslatorProvider>();
_logger = Substitute.For<ILogger<AzureTranslatorProvider>>();

Sut = new AzureTranslatorProvider(_client, _logger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ namespace DiscordTranslationBot.Tests.Providers.Translation.LibreTranslate;
public sealed class LibreTranslateProviderTests : TranslationProviderBaseTests
{
private readonly ILibreTranslateClient _client;
private readonly ICacheLogger<LibreTranslateProvider> _logger;
private readonly ILogger<LibreTranslateProvider> _logger;

public LibreTranslateProviderTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public LibreTranslateProviderTests()
{
_client = Substitute.For<ILibreTranslateClient>();

Expand All @@ -37,7 +36,7 @@ public LibreTranslateProviderTests(ITestOutputHelper testOutputHelper)

_client.GetLanguagesAsync(default).ReturnsForAnyArgs(languagesResponse);

_logger = CreateLogger<LibreTranslateProvider>();
_logger = Substitute.For<ILogger<LibreTranslateProvider>>();

Sut = new LibreTranslateProvider(_client, _logger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@

namespace DiscordTranslationBot.Tests.Providers.Translation;

public abstract class TranslationProviderBaseTests : TestBase, IAsyncLifetime
public abstract class TranslationProviderBaseTests : IAsyncLifetime
{
protected TranslationProviderBaseTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

protected TranslationProviderBase Sut { get; init; } = null!;

public async Task InitializeAsync()
Expand Down
7 changes: 3 additions & 4 deletions DiscordTranslationBot.Tests/Services/CountryServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

namespace DiscordTranslationBot.Tests.Services;

public sealed class CountryServiceTests : TestBase
public sealed class CountryServiceTests
{
private readonly CountryService _sut;

public CountryServiceTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
public CountryServiceTests()
{
_sut = new CountryService(CreateLogger<CountryService>());
_sut = new CountryService(Substitute.For<ILogger<CountryService>>());
}

public static TheoryData<string, string> TryGetCountryTestData =>
Expand Down
17 changes: 0 additions & 17 deletions DiscordTranslationBot.Tests/TestBase.cs

This file was deleted.

Loading

0 comments on commit 7bf8a0b

Please sign in to comment.