tests: add tests for data client
This commit is contained in:
@@ -27,7 +27,7 @@
|
|||||||
<CoverletOutput>./TestResults/Coverage/</CoverletOutput>
|
<CoverletOutput>./TestResults/Coverage/</CoverletOutput>
|
||||||
<CoverletOutputFormat>cobertura</CoverletOutputFormat>
|
<CoverletOutputFormat>cobertura</CoverletOutputFormat>
|
||||||
<Include>[StevesBot.Webhook]*</Include>
|
<Include>[StevesBot.Webhook]*</Include>
|
||||||
<ExcludeByFile>**/Program.cs,**/Worker.cs</ExcludeByFile>
|
<ExcludeByFile>**/Program.cs,**/SubscriptionWorker.cs</ExcludeByFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<Target Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResultAfterTest">
|
<Target Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResultAfterTest">
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ public class SubscribeTaskTests
|
|||||||
|
|
||||||
task.CallbackUrl.Should().BeEmpty();
|
task.CallbackUrl.Should().BeEmpty();
|
||||||
task.TopicUrl.Should().BeEmpty();
|
task.TopicUrl.Should().BeEmpty();
|
||||||
task.ExpiresAt.Should().Be(DateTime.MinValue);
|
task.ExpiresAt.Should().Be(DateTimeOffset.MinValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -23,7 +23,7 @@ public class SubscribeTaskTests
|
|||||||
{
|
{
|
||||||
CallbackUrl = callbackUrl,
|
CallbackUrl = callbackUrl,
|
||||||
TopicUrl = topicUrl,
|
TopicUrl = topicUrl,
|
||||||
ExpiresAt = expiresAt
|
ExpiresAt = expiresAt,
|
||||||
};
|
};
|
||||||
|
|
||||||
task.CallbackUrl.Should().Be(callbackUrl);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user