tests: add tests for data client

This commit is contained in:
Stevan Freeborn
2025-05-31 21:58:18 -05:00
parent 7a12f2768d
commit 390b6f56c5
4 changed files with 126 additions and 3 deletions
@@ -27,7 +27,7 @@
<CoverletOutput>./TestResults/Coverage/</CoverletOutput>
<CoverletOutputFormat>cobertura</CoverletOutputFormat>
<Include>[StevesBot.Webhook]*</Include>
<ExcludeByFile>**/Program.cs,**/Worker.cs</ExcludeByFile>
<ExcludeByFile>**/Program.cs,**/SubscriptionWorker.cs</ExcludeByFile>
</PropertyGroup>
<Target Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResultAfterTest">
@@ -9,7 +9,7 @@ public class SubscribeTaskTests
task.CallbackUrl.Should().BeEmpty();
task.TopicUrl.Should().BeEmpty();
task.ExpiresAt.Should().Be(DateTime.MinValue);
task.ExpiresAt.Should().Be(DateTimeOffset.MinValue);
}
[Fact]
@@ -23,7 +23,7 @@ public class SubscribeTaskTests
{
CallbackUrl = callbackUrl,
TopicUrl = topicUrl,
ExpiresAt = expiresAt
ExpiresAt = expiresAt,
};
task.CallbackUrl.Should().Be(callbackUrl);
@@ -0,0 +1,81 @@
using System.Text.Json;
using Microsoft.Extensions.Options;
namespace StevesBot.Webhook.Tests.Unit;
public sealed class YouTubeDataApiClientTests : IDisposable
{
private readonly MockHttpMessageHandler _mockHttpMessageHandler = new();
private readonly Mock<ILogger<YouTubeDataApiClient>> _mockLogger = new();
private readonly Mock<IOptions<YouTubeClientOptions>> _mockOptions = new();
private readonly YouTubeDataApiClient _sut;
public YouTubeDataApiClientTests()
{
var httpClient = _mockHttpMessageHandler.ToHttpClient();
httpClient.BaseAddress = new Uri("https://test.com");
_mockOptions
.Setup(static x => x.Value)
.Returns(new YouTubeClientOptions());
_sut = new(httpClient, _mockLogger.Object, _mockOptions.Object);
}
[Fact]
public async Task GetVideoByIdAsync_WhenRequestFails_ItShouldReturnNull()
{
_mockHttpMessageHandler
.When("*/videos")
.Respond(HttpStatusCode.InternalServerError);
var result = await _sut.GetVideoByIdAsync("video_id");
result.Should().BeNull();
}
[Fact]
public async Task GetVideoByIdAsync_WhenRequestSucceedsButItemsEmpty_ItShouldReturnNull()
{
var videosResponse = new YouTubeVideoListResponse();
_mockHttpMessageHandler
.When("*/videos")
.Respond(
HttpStatusCode.OK,
"application/json",
JsonSerializer.Serialize(videosResponse)
);
var result = await _sut.GetVideoByIdAsync("video_id", ["snippet"]);
result.Should().BeNull();
}
[Fact]
public async Task GetVideoByIdAsync_WhenRequestSucceedsAndVideoFound_ItShouldReturnVideo()
{
var videoId = "video_id";
var video = new YouTubeVideo() { Id = videoId };
var videosResponse = new YouTubeVideoListResponse() { Items = [video] };
_mockHttpMessageHandler
.When("*/videos")
.Respond(
HttpStatusCode.OK,
"application/json",
JsonSerializer.Serialize(videosResponse)
);
var result = await _sut.GetVideoByIdAsync(videoId);
result.Should().BeEquivalentTo(video);
}
public void Dispose()
{
_mockHttpMessageHandler.Dispose();
}
}
@@ -0,0 +1,42 @@
namespace StevesBot.Webhook.Tests.Unit;
public class YouTubeLiveStreamingDetailsTests
{
[Fact]
public void Constructor_WhenCalled_ItShouldReturnAnInstance()
{
var result = new YouTubeLiveStreamingDetails();
result.ActualStartTime.Should().BeNull();
result.ActualEndTime.Should().BeNull();
result.ScheduledStartTime.Should().BeNull();
result.ScheduledEndTime.Should().BeNull();
result.ConcurrentViewers.Should().BeNull();
result.ActiveLiveChatId.Should().BeNull();
}
[Fact]
public void Constructor_WhenCalledWithValues_ItShouldReturnAnInstance()
{
var now = DateTimeOffset.UtcNow;
var viewers = 100UL;
var chatId = "chad_id";
var result = new YouTubeLiveStreamingDetails()
{
ActualStartTime = now,
ActualEndTime = now,
ScheduledStartTime = now,
ScheduledEndTime = now,
ConcurrentViewers = viewers,
ActiveLiveChatId = chatId,
};
result.ActualStartTime.Should().Be(now);
result.ActualEndTime.Should().Be(now);
result.ScheduledStartTime.Should().Be(now);
result.ScheduledEndTime.Should().Be(now);
result.ConcurrentViewers.Should().Be(viewers);
result.ActiveLiveChatId.Should().Be(chatId);
}
}