- Directory.Packages.props: xunit 2.9.3 -> xunit.v3 4.0.0, bump xunit.runner.visualstudio to the matching 4.0.0 - All 5 test csproj: xunit -> xunit.v3 package reference, add <OutputType>Exe</OutputType> (v3 test projects are standalone executables) - backend/global.json: opt dotnet test into the new MTP-native mode (.NET 10 SDK removed the old VSTest bridge for MTP-based test projects like xunit v3) - IAsyncLifetime implementations (HandlerTestBase, ApiTestFixture, InfrastructureTestFixture, StorageServiceTests): v3's IAsyncLifetime now IS IAsyncDisposable, so InitializeAsync/DisposeAsync return ValueTask instead of Task. ApiTestFixture specifically now overrides WebApplicationFactory.DisposeAsync instead of implementing IAsyncLifetime.DisposeAsync explicitly, since v3 collapses both to the same single DisposeAsync slot - Replaced CancellationToken.None with TestContext.Current.CancellationToken across the suite (a v3-only API) and added it to every call that was silently omitting a CancellationToken argument, per the new xUnit1051 analyzer warnings that came with the v3 package Verified: dotnet build (0 warnings, 0 errors) and dotnet test, 250/250 passing under the new MTP runner.
92 lines
4.2 KiB
C#
92 lines
4.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Musify.Domain.Entities;
|
|
using Musify.Domain.ValueObjects;
|
|
using Musify.Infrastructure.Tests.TestSupport;
|
|
using Xunit;
|
|
|
|
namespace Musify.Infrastructure.Tests.Persistence;
|
|
|
|
[Collection(InfrastructureCollection.Name)]
|
|
public sealed class DatabaseTests(InfrastructureTestFixture fixture)
|
|
{
|
|
[Fact]
|
|
public async Task SaveChangesAsync_LocalTrackWithOwnedTypes_RoundTripsThroughPostgres()
|
|
{
|
|
await using var database = fixture.CreateDatabase();
|
|
|
|
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = "roundtrip-user", NormalizedName = "ROUNDTRIP-USER" };
|
|
await database.Users.AddAsync(user, TestContext.Current.CancellationToken);
|
|
|
|
var track = new LocalTrack
|
|
{
|
|
Title = "Round Trip Track",
|
|
NormalizedTitle = "ROUND TRIP TRACK",
|
|
DurationSeconds = 123.4,
|
|
OwnerUserId = user.Id,
|
|
Owner = user,
|
|
Pictures = new TrackPictures { OriginalName = "cover.webp", ProcessingStatus = ProcessingStatus.Pending },
|
|
Audio = new TrackAudio { OriginalName = "song.mp3", TranscodeStatus = ProcessingStatus.Pending }
|
|
};
|
|
await database.LocalTracks.AddAsync(track, TestContext.Current.CancellationToken);
|
|
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
|
|
|
|
await using var reloaded = fixture.CreateDatabase();
|
|
var stored = await reloaded.Tracks.AsNoTracking().SingleAsync(t => t.Id == track.Id, TestContext.Current.CancellationToken);
|
|
|
|
Assert.IsType<LocalTrack>(stored);
|
|
Assert.Equal("Round Trip Track", stored.Title);
|
|
Assert.Equal("cover.webp", stored.Pictures.OriginalName);
|
|
Assert.Equal("song.mp3", stored.Audio.OriginalName);
|
|
Assert.Equal(user.Id, ((LocalTrack)stored).OwnerUserId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExternalTracks_SourceAndExternalIdUniqueIndex_RejectsDuplicates()
|
|
{
|
|
await using var database = fixture.CreateDatabase();
|
|
|
|
var videoId = $"dup-{Guid.NewGuid():N}";
|
|
await database.ExternalTracks.AddAsync(BuildExternalTrack(videoId), TestContext.Current.CancellationToken);
|
|
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
|
|
|
|
await using var second = fixture.CreateDatabase();
|
|
await second.ExternalTracks.AddAsync(BuildExternalTrack(videoId), TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<DbUpdateException>(() => second.SaveChangesAsync(TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AlbumHasTracks_DeletingAlbum_CascadesToTheLinkRows()
|
|
{
|
|
await using var database = fixture.CreateDatabase();
|
|
|
|
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = "cascade-user", NormalizedName = "CASCADE-USER" };
|
|
var album = new UserAlbum { Title = "Cascade Album", NormalizedTitle = "CASCADE ALBUM", OwnerUserId = user.Id };
|
|
var track = BuildExternalTrack($"cascade-{Guid.NewGuid():N}");
|
|
var link = new AlbumHasTrack { AlbumId = album.Id, TrackId = track.Id, TrackNumber = 1 };
|
|
|
|
await database.AddRangeAsync(user, album, track, link);
|
|
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
|
|
|
|
await using var deleter = fixture.CreateDatabase();
|
|
var albumToDelete = await deleter.UserAlbums.SingleAsync(a => a.Id == album.Id, TestContext.Current.CancellationToken);
|
|
deleter.UserAlbums.Remove(albumToDelete);
|
|
await deleter.SaveChangesAsync(TestContext.Current.CancellationToken);
|
|
|
|
await using var verifier = fixture.CreateDatabase();
|
|
Assert.False(await verifier.AlbumHasTracks.AnyAsync(l => l.AlbumId == album.Id, TestContext.Current.CancellationToken));
|
|
Assert.True(await verifier.ExternalTracks.AnyAsync(t => t.Id == track.Id, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
private static ExternalTrack BuildExternalTrack(string externalId) => new()
|
|
{
|
|
Title = "External",
|
|
NormalizedTitle = "EXTERNAL",
|
|
DurationSeconds = 100,
|
|
Source = TrackSource.YouTube,
|
|
ExternalId = externalId,
|
|
Pictures = new TrackPictures(),
|
|
Audio = new TrackAudio()
|
|
};
|
|
}
|