Two xUnit projects, wired into Musify.slnx under a /Tests/ folder that
mirrors Core/ and Hosts/:
Musify.Domain.Tests Value-object logic (TrackAudio/TrackPictures'
IsProcessed/IsFailed/IsInProgress). Entities are
otherwise anemic EF models with no behavior to
test.
Musify.Application.Tests All ~45 Application handlers and services
(Albums, Tracks, PlayLists, Users, Mixes,
YouTube, plus TrackStreamIssuer,
UploadIntentValidator, YouTubeTrackProvisioner).
IDatabase exposes DbSet<T> directly, so handlers compose real LINQ queries
against it — that can't be expressed with a mock (no IQueryable provider
behind one). TestSupport/TestDatabase.cs is instead a real DbContext on a
private SQLite in-memory connection per test (fresh per test since xUnit
instantiates the test class per test method): fast, no external dependency,
and — unlike EF Core's InMemory provider — SQLite enforces real foreign keys
and supports transactions, which two handlers need. External dependencies
(IStorageService, IEventBus, IYouTubeMusicService, ...) are substituted with
NSubstitute.
TestSupport/TestEntities.cs and TestConfigurations.cs build fully-populated
domain entities and configuration POCOs with sensible defaults for every
required member, so no test method needs to fill in properties it doesn't
care about, and no builder ever hands back null.
Also, needed to make the Application layer itself testable without a real
Postgres or user-secrets:
- DatabaseDesignTimeFactory now falls back to the Database__ConnectionString
env var, and DesignSettings.json is optional — unblocks the `migrate`
one-shot container from deploy/compose.yml, which hit the same gap.
- Directory.Packages.props: xunit, xunit.runner.visualstudio, NSubstitute,
Microsoft.EntityFrameworkCore.Sqlite (pinned to the same 10.0.8 as the rest
of EF Core), Microsoft.NET.Test.Sdk, coverlet.collector. Also pins
SQLitePCLRaw.bundle_e_sqlite3 to 2.1.13 — EFCore.Sqlite 10.0.8 pulls 2.1.11
transitively, which has a known high-severity advisory (GHSA-2m69-gcr7-jv3q).
- backend/.dockerignore excludes Tests/: no application Dockerfile needs it
in its build context.
162 tests, all green: `dotnet test backend/Musify.slnx`.
62 lines
2.3 KiB
C#
62 lines
2.3 KiB
C#
using ErrorOr;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Musify.Application.Contracts;
|
|
using Musify.Application.Services;
|
|
using Musify.Application.Tests.TestSupport;
|
|
using Musify.Application.YouTube;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace Musify.Application.Tests.YouTube;
|
|
|
|
public sealed class GetYouTubeAlbumQueryHandlerTests : HandlerTestBase
|
|
{
|
|
private readonly IYouTubeMusicService youTubeMusicService = Substitute.For<IYouTubeMusicService>();
|
|
|
|
private GetYouTubeAlbumQueryHandler CreateHandler()
|
|
{
|
|
var provisioner = new YouTubeTrackProvisioner(
|
|
Database, youTubeMusicService, TestConfigurations.Track(), NoOpLogger<YouTubeTrackProvisioner>());
|
|
|
|
return new GetYouTubeAlbumQueryHandler(youTubeMusicService, provisioner, NoOpLogger<GetYouTubeAlbumQueryHandler>());
|
|
}
|
|
|
|
private static YouTubeAlbumDetail BuildDetail() => new(
|
|
new YouTubeAlbumResult("album-1", "Album Title", "Album Artist", "https://img/thumb.jpg", 2024, false, false, []),
|
|
Description: "An album",
|
|
TotalDurationSeconds: 600,
|
|
Tracks: [new YouTubeAlbumTrack("track-1", "Track One", 200, 1, false)]);
|
|
|
|
[Fact]
|
|
public async Task Handle_EmptyAlbumId_ReturnsValidationError()
|
|
{
|
|
var result = await CreateHandler().Handle(new GetYouTubeAlbumQuery(string.Empty), CancellationToken.None);
|
|
|
|
Assert.Equal(ErrorType.Validation, result.FirstError.Type);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_ServiceSucceeds_MaterializesAlbumAndReturnsDetail()
|
|
{
|
|
var detail = BuildDetail();
|
|
youTubeMusicService.GetAlbumAsync("album-1", Arg.Any<CancellationToken>()).Returns(detail);
|
|
|
|
var result = await CreateHandler().Handle(new GetYouTubeAlbumQuery("album-1"), CancellationToken.None);
|
|
|
|
Assert.False(result.IsError);
|
|
Assert.Same(detail, result.Value);
|
|
Assert.NotEmpty(await Database.ExternalAlbums.ToListAsync());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_ServiceFails_ReturnsError()
|
|
{
|
|
youTubeMusicService.GetAlbumAsync("missing", Arg.Any<CancellationToken>())
|
|
.Returns(Error.NotFound(description: "Album not found"));
|
|
|
|
var result = await CreateHandler().Handle(new GetYouTubeAlbumQuery("missing"), CancellationToken.None);
|
|
|
|
Assert.True(result.IsError);
|
|
}
|
|
}
|