Use block-scoped namespaces in the test suite
Converts all 73 test source files from file-scoped (namespace X;) to
block-scoped (namespace X { ... }) declarations, re-indenting their
contents accordingly. Matches the existing convention already used in
several production files (e.g. Musify.Infrastructure.Configuration,
Musify.Application.Contracts).
Mechanical change only, done with a script, verified with a full
rebuild (0 warnings, 0 errors) and test run (257/257 passing).
This commit is contained in:
@@ -2,32 +2,33 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Xunit;
|
||||
|
||||
namespace Musify.Application.Tests.TestSupport;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for handler tests: gives every test method its own empty, isolated
|
||||
/// <see cref="TestDatabase"/> (xUnit creates a new instance of the test class per test method,
|
||||
/// so there is no state to reset between tests) and a no-op logger factory for handlers that
|
||||
/// take an <see cref="ILogger{TCategoryName}"/>.
|
||||
/// </summary>
|
||||
public abstract class HandlerTestBase : IAsyncLifetime
|
||||
namespace Musify.Application.Tests.TestSupport
|
||||
{
|
||||
private TestDatabase database = null!;
|
||||
|
||||
protected TestDatabase Database => database;
|
||||
|
||||
public async ValueTask InitializeAsync() => database = await TestDatabase.CreateAsync();
|
||||
|
||||
public ValueTask DisposeAsync() => database.DisposeAsync();
|
||||
|
||||
protected static ILogger<T> NoOpLogger<T>() => NullLogger<T>.Instance;
|
||||
|
||||
/// <summary>Adds the entities and persists them, so handlers under test see committed data.</summary>
|
||||
protected async Task SeedAsync(params object[] entities)
|
||||
/// <summary>
|
||||
/// Base class for handler tests: gives every test method its own empty, isolated
|
||||
/// <see cref="TestDatabase"/> (xUnit creates a new instance of the test class per test method,
|
||||
/// so there is no state to reset between tests) and a no-op logger factory for handlers that
|
||||
/// take an <see cref="ILogger{TCategoryName}"/>.
|
||||
/// </summary>
|
||||
public abstract class HandlerTestBase : IAsyncLifetime
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
Database.Add(entity);
|
||||
private TestDatabase database = null!;
|
||||
|
||||
await Database.SaveChangesAsync(CancellationToken.None);
|
||||
protected TestDatabase Database => database;
|
||||
|
||||
public async ValueTask InitializeAsync() => database = await TestDatabase.CreateAsync();
|
||||
|
||||
public ValueTask DisposeAsync() => database.DisposeAsync();
|
||||
|
||||
protected static ILogger<T> NoOpLogger<T>() => NullLogger<T>.Instance;
|
||||
|
||||
/// <summary>Adds the entities and persists them, so handlers under test see committed data.</summary>
|
||||
protected async Task SeedAsync(params object[] entities)
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
Database.Add(entity);
|
||||
|
||||
await Database.SaveChangesAsync(CancellationToken.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,25 @@
|
||||
using Musify.Application.Configuration;
|
||||
|
||||
namespace Musify.Application.Tests.TestSupport;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration POCOs with every <c>required</c> member filled and the rest left at their
|
||||
/// production defaults, so handler tests don't have to know which fields matter for them.
|
||||
/// </summary>
|
||||
public static class TestConfigurations
|
||||
namespace Musify.Application.Tests.TestSupport
|
||||
{
|
||||
public static ApplicationStorageConfiguration Storage(string bucket = "test-bucket") =>
|
||||
new() { Bucket = bucket };
|
||||
/// <summary>
|
||||
/// Configuration POCOs with every <c>required</c> member filled and the rest left at their
|
||||
/// production defaults, so handler tests don't have to know which fields matter for them.
|
||||
/// </summary>
|
||||
public static class TestConfigurations
|
||||
{
|
||||
public static ApplicationStorageConfiguration Storage(string bucket = "test-bucket") =>
|
||||
new() { Bucket = bucket };
|
||||
|
||||
public static TrackConfiguration Track() => new();
|
||||
public static TrackConfiguration Track() => new();
|
||||
|
||||
public static PlayListConfiguration PlayList() => new();
|
||||
public static PlayListConfiguration PlayList() => new();
|
||||
|
||||
public static MixConfiguration Mix() => new();
|
||||
public static MixConfiguration Mix() => new();
|
||||
|
||||
public static UploadIntentConfiguration UploadIntent() => new();
|
||||
public static UploadIntentConfiguration UploadIntent() => new();
|
||||
|
||||
public static StreamGatewayConfiguration StreamGateway(string publicBaseUrl = "https://stream.musify.test") =>
|
||||
new() { PublicBaseUrl = publicBaseUrl };
|
||||
public static StreamGatewayConfiguration StreamGateway(string publicBaseUrl = "https://stream.musify.test") =>
|
||||
new() { PublicBaseUrl = publicBaseUrl };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,209 +3,210 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Domain.Entities;
|
||||
|
||||
namespace Musify.Application.Tests.TestSupport;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IDatabase"/> backed by a private, in-memory SQLite database.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <see cref="IDatabase"/> exposes raw <see cref="DbSet{TEntity}"/> properties, so handlers
|
||||
/// compose real LINQ queries (<c>Where</c>, <c>Select</c>, <c>Include</c>, ...) against it.
|
||||
/// Substituting <see cref="IDatabase"/> with a mock cannot express that — there is no
|
||||
/// <see cref="IQueryable{T}"/> provider behind a mock. SQLite's in-memory mode gives us a real
|
||||
/// relational engine instead: fast, no external dependency, and — unlike the EF Core InMemory
|
||||
/// provider — it actually supports transactions, which two handlers rely on.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The model mirrors the parts of <c>Musify.Infrastructure.Persistence.Database</c> that affect
|
||||
/// query shape (owned types, table-per-type inheritance, unique indexes). MassTransit outbox/saga
|
||||
/// state and Postgres-specific configuration are left out — the Application layer never touches
|
||||
/// them.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class TestDatabase : DbContext, IDatabase
|
||||
namespace Musify.Application.Tests.TestSupport
|
||||
{
|
||||
private readonly SqliteConnection connection;
|
||||
|
||||
private TestDatabase(SqliteConnection connection, DbContextOptions<TestDatabase> options)
|
||||
: base(options)
|
||||
{
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a fresh, empty database with its own private connection. Call <see cref="Seed"/>
|
||||
/// (via <see cref="Seeding.EntitySeedExtensions"/>) to populate it, then <see cref="DisposeAsync"/>
|
||||
/// when the test is done.
|
||||
/// An <see cref="IDatabase"/> backed by a private, in-memory SQLite database.
|
||||
/// </summary>
|
||||
public static async Task<TestDatabase> CreateAsync()
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <see cref="IDatabase"/> exposes raw <see cref="DbSet{TEntity}"/> properties, so handlers
|
||||
/// compose real LINQ queries (<c>Where</c>, <c>Select</c>, <c>Include</c>, ...) against it.
|
||||
/// Substituting <see cref="IDatabase"/> with a mock cannot express that — there is no
|
||||
/// <see cref="IQueryable{T}"/> provider behind a mock. SQLite's in-memory mode gives us a real
|
||||
/// relational engine instead: fast, no external dependency, and — unlike the EF Core InMemory
|
||||
/// provider — it actually supports transactions, which two handlers rely on.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The model mirrors the parts of <c>Musify.Infrastructure.Persistence.Database</c> that affect
|
||||
/// query shape (owned types, table-per-type inheritance, unique indexes). MassTransit outbox/saga
|
||||
/// state and Postgres-specific configuration are left out — the Application layer never touches
|
||||
/// them.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class TestDatabase : DbContext, IDatabase
|
||||
{
|
||||
var connection = new SqliteConnection("Filename=:memory:");
|
||||
await connection.OpenAsync();
|
||||
private readonly SqliteConnection connection;
|
||||
|
||||
var options = new DbContextOptionsBuilder<TestDatabase>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
private TestDatabase(SqliteConnection connection, DbContextOptions<TestDatabase> options)
|
||||
: base(options)
|
||||
{
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
var database = new TestDatabase(connection, options);
|
||||
await database.Database.EnsureCreatedAsync();
|
||||
return database;
|
||||
}
|
||||
/// <summary>
|
||||
/// Creates a fresh, empty database with its own private connection. Call <see cref="Seed"/>
|
||||
/// (via <see cref="Seeding.EntitySeedExtensions"/>) to populate it, then <see cref="DisposeAsync"/>
|
||||
/// when the test is done.
|
||||
/// </summary>
|
||||
public static async Task<TestDatabase> CreateAsync()
|
||||
{
|
||||
var connection = new SqliteConnection("Filename=:memory:");
|
||||
await connection.OpenAsync();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
var options = new DbContextOptionsBuilder<TestDatabase>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
modelBuilder.Entity<Track>().OwnsOne(track => track.Pictures);
|
||||
modelBuilder.Entity<Track>().Navigation(track => track.Pictures).IsRequired();
|
||||
var database = new TestDatabase(connection, options);
|
||||
await database.Database.EnsureCreatedAsync();
|
||||
return database;
|
||||
}
|
||||
|
||||
modelBuilder.Entity<Track>().OwnsOne(track => track.Audio);
|
||||
modelBuilder.Entity<Track>().Navigation(track => track.Audio).IsRequired();
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<LocalTrack>().ToTable("LocalTracks");
|
||||
modelBuilder.Entity<Track>().OwnsOne(track => track.Pictures);
|
||||
modelBuilder.Entity<Track>().Navigation(track => track.Pictures).IsRequired();
|
||||
|
||||
modelBuilder.Entity<LocalTrack>()
|
||||
.HasOne(track => track.Owner)
|
||||
.WithMany()
|
||||
.HasForeignKey(track => track.OwnerUserId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
modelBuilder.Entity<Track>().OwnsOne(track => track.Audio);
|
||||
modelBuilder.Entity<Track>().Navigation(track => track.Audio).IsRequired();
|
||||
|
||||
modelBuilder.Entity<ExternalTrack>().ToTable("ExternalTracks");
|
||||
modelBuilder.Entity<LocalTrack>().ToTable("LocalTracks");
|
||||
|
||||
modelBuilder.Entity<ExternalTrack>()
|
||||
.HasIndex(track => new { track.Source, track.ExternalId })
|
||||
.IsUnique();
|
||||
modelBuilder.Entity<LocalTrack>()
|
||||
.HasOne(track => track.Owner)
|
||||
.WithMany()
|
||||
.HasForeignKey(track => track.OwnerUserId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<TrackArtist>()
|
||||
.HasKey(trackArtist => new { trackArtist.TrackId, trackArtist.ArtistId });
|
||||
modelBuilder.Entity<ExternalTrack>().ToTable("ExternalTracks");
|
||||
|
||||
modelBuilder.Entity<TrackArtist>()
|
||||
.HasOne(trackArtist => trackArtist.Track)
|
||||
.WithMany(track => track.TrackArtists)
|
||||
.HasForeignKey(trackArtist => trackArtist.TrackId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
modelBuilder.Entity<ExternalTrack>()
|
||||
.HasIndex(track => new { track.Source, track.ExternalId })
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<TrackArtist>()
|
||||
.HasOne(trackArtist => trackArtist.Artist)
|
||||
.WithMany(artist => artist.TrackArtists)
|
||||
.HasForeignKey(trackArtist => trackArtist.ArtistId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
modelBuilder.Entity<TrackArtist>()
|
||||
.HasKey(trackArtist => new { trackArtist.TrackId, trackArtist.ArtistId });
|
||||
|
||||
modelBuilder.Entity<Artist>()
|
||||
.HasIndex(artist => artist.ExternalId)
|
||||
.IsUnique();
|
||||
modelBuilder.Entity<TrackArtist>()
|
||||
.HasOne(trackArtist => trackArtist.Track)
|
||||
.WithMany(track => track.TrackArtists)
|
||||
.HasForeignKey(trackArtist => trackArtist.TrackId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<PlayList>().OwnsOne(playList => playList.Pictures);
|
||||
modelBuilder.Entity<PlayList>().Navigation(playList => playList.Pictures).IsRequired();
|
||||
modelBuilder.Entity<TrackArtist>()
|
||||
.HasOne(trackArtist => trackArtist.Artist)
|
||||
.WithMany(artist => artist.TrackArtists)
|
||||
.HasForeignKey(trackArtist => trackArtist.ArtistId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<Album>().OwnsOne(album => album.Pictures);
|
||||
modelBuilder.Entity<Album>().Navigation(album => album.Pictures).IsRequired(false);
|
||||
modelBuilder.Entity<Artist>()
|
||||
.HasIndex(artist => artist.ExternalId)
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<UserAlbum>().ToTable("UserAlbums");
|
||||
modelBuilder.Entity<PlayList>().OwnsOne(playList => playList.Pictures);
|
||||
modelBuilder.Entity<PlayList>().Navigation(playList => playList.Pictures).IsRequired();
|
||||
|
||||
modelBuilder.Entity<UserAlbum>()
|
||||
.HasOne(album => album.Owner)
|
||||
.WithMany()
|
||||
.HasForeignKey(album => album.OwnerUserId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
modelBuilder.Entity<Album>().OwnsOne(album => album.Pictures);
|
||||
modelBuilder.Entity<Album>().Navigation(album => album.Pictures).IsRequired(false);
|
||||
|
||||
modelBuilder.Entity<ExternalAlbum>().ToTable("ExternalAlbums");
|
||||
modelBuilder.Entity<UserAlbum>().ToTable("UserAlbums");
|
||||
|
||||
modelBuilder.Entity<ExternalAlbum>()
|
||||
.HasIndex(album => new { album.Source, album.ExternalId })
|
||||
.IsUnique();
|
||||
modelBuilder.Entity<UserAlbum>()
|
||||
.HasOne(album => album.Owner)
|
||||
.WithMany()
|
||||
.HasForeignKey(album => album.OwnerUserId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<AlbumHasTrack>()
|
||||
.HasOne(albumTrack => albumTrack.Album)
|
||||
.WithMany(album => album.AlbumTracks)
|
||||
.HasForeignKey(albumTrack => albumTrack.AlbumId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
modelBuilder.Entity<ExternalAlbum>().ToTable("ExternalAlbums");
|
||||
|
||||
modelBuilder.Entity<AlbumHasTrack>()
|
||||
.HasOne(albumTrack => albumTrack.Track)
|
||||
.WithMany(track => track.AlbumTracks)
|
||||
.HasForeignKey(albumTrack => albumTrack.TrackId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
modelBuilder.Entity<ExternalAlbum>()
|
||||
.HasIndex(album => new { album.Source, album.ExternalId })
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<AlbumHasTrack>()
|
||||
.HasIndex(albumTrack => new { albumTrack.AlbumId, albumTrack.TrackId })
|
||||
.IsUnique();
|
||||
modelBuilder.Entity<AlbumHasTrack>()
|
||||
.HasOne(albumTrack => albumTrack.Album)
|
||||
.WithMany(album => album.AlbumTracks)
|
||||
.HasForeignKey(albumTrack => albumTrack.AlbumId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<Mix>()
|
||||
.HasIndex(mix => new { mix.UserId, mix.Position });
|
||||
modelBuilder.Entity<AlbumHasTrack>()
|
||||
.HasOne(albumTrack => albumTrack.Track)
|
||||
.WithMany(track => track.AlbumTracks)
|
||||
.HasForeignKey(albumTrack => albumTrack.TrackId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<MixItem>()
|
||||
.HasOne(item => item.Mix)
|
||||
.WithMany(mix => mix.Items)
|
||||
.HasForeignKey(item => item.MixId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
modelBuilder.Entity<AlbumHasTrack>()
|
||||
.HasIndex(albumTrack => new { albumTrack.AlbumId, albumTrack.TrackId })
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<MixItem>()
|
||||
.HasOne(item => item.Track)
|
||||
.WithMany()
|
||||
.HasForeignKey(item => item.TrackId)
|
||||
.IsRequired(false)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
modelBuilder.Entity<Mix>()
|
||||
.HasIndex(mix => new { mix.UserId, mix.Position });
|
||||
|
||||
modelBuilder.Entity<MixItem>()
|
||||
.HasIndex(item => new { item.MixId, item.Position });
|
||||
}
|
||||
modelBuilder.Entity<MixItem>()
|
||||
.HasOne(item => item.Mix)
|
||||
.WithMany(mix => mix.Items)
|
||||
.HasForeignKey(item => item.MixId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
public DbSet<User> Users => Set<User>();
|
||||
modelBuilder.Entity<MixItem>()
|
||||
.HasOne(item => item.Track)
|
||||
.WithMany()
|
||||
.HasForeignKey(item => item.TrackId)
|
||||
.IsRequired(false)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
public DbSet<PlayList> PlayLists => Set<PlayList>();
|
||||
modelBuilder.Entity<MixItem>()
|
||||
.HasIndex(item => new { item.MixId, item.Position });
|
||||
}
|
||||
|
||||
public DbSet<Album> Albums => Set<Album>();
|
||||
public DbSet<User> Users => Set<User>();
|
||||
|
||||
public DbSet<UserAlbum> UserAlbums => Set<UserAlbum>();
|
||||
public DbSet<PlayList> PlayLists => Set<PlayList>();
|
||||
|
||||
public DbSet<ExternalAlbum> ExternalAlbums => Set<ExternalAlbum>();
|
||||
public DbSet<Album> Albums => Set<Album>();
|
||||
|
||||
public DbSet<AlbumHasTrack> AlbumHasTracks => Set<AlbumHasTrack>();
|
||||
public DbSet<UserAlbum> UserAlbums => Set<UserAlbum>();
|
||||
|
||||
public DbSet<Track> Tracks => Set<Track>();
|
||||
public DbSet<ExternalAlbum> ExternalAlbums => Set<ExternalAlbum>();
|
||||
|
||||
public DbSet<LocalTrack> LocalTracks => Set<LocalTrack>();
|
||||
public DbSet<AlbumHasTrack> AlbumHasTracks => Set<AlbumHasTrack>();
|
||||
|
||||
public DbSet<ExternalTrack> ExternalTracks => Set<ExternalTrack>();
|
||||
public DbSet<Track> Tracks => Set<Track>();
|
||||
|
||||
public DbSet<Artist> Artists => Set<Artist>();
|
||||
public DbSet<LocalTrack> LocalTracks => Set<LocalTrack>();
|
||||
|
||||
public DbSet<TrackArtist> TrackArtists => Set<TrackArtist>();
|
||||
public DbSet<ExternalTrack> ExternalTracks => Set<ExternalTrack>();
|
||||
|
||||
public DbSet<UserHasTrack> UserHasTracks => Set<UserHasTrack>();
|
||||
public DbSet<Artist> Artists => Set<Artist>();
|
||||
|
||||
public DbSet<PlayListHasTrack> PlayListHasTracks => Set<PlayListHasTrack>();
|
||||
public DbSet<TrackArtist> TrackArtists => Set<TrackArtist>();
|
||||
|
||||
public DbSet<Mix> Mixes => Set<Mix>();
|
||||
public DbSet<UserHasTrack> UserHasTracks => Set<UserHasTrack>();
|
||||
|
||||
public DbSet<MixItem> MixItems => Set<MixItem>();
|
||||
public DbSet<PlayListHasTrack> PlayListHasTracks => Set<PlayListHasTrack>();
|
||||
|
||||
public DbSet<Upload> Uploads => Set<Upload>();
|
||||
public DbSet<Mix> Mixes => Set<Mix>();
|
||||
|
||||
public DbSet<UploadIntent> UploadIntents => Set<UploadIntent>();
|
||||
public DbSet<MixItem> MixItems => Set<MixItem>();
|
||||
|
||||
public DbSet<ListeningHistory> ListeningHistories => Set<ListeningHistory>();
|
||||
public DbSet<Upload> Uploads => Set<Upload>();
|
||||
|
||||
public async Task<IDatabaseTransaction> BeginTransactionAsync(System.Data.IsolationLevel isolationLevel, CancellationToken cancellationToken)
|
||||
{
|
||||
var transaction = await Database.BeginTransactionAsync(isolationLevel, cancellationToken);
|
||||
return new SqliteDatabaseTransaction(transaction);
|
||||
}
|
||||
public DbSet<UploadIntent> UploadIntents => Set<UploadIntent>();
|
||||
|
||||
public override async ValueTask DisposeAsync()
|
||||
{
|
||||
await base.DisposeAsync();
|
||||
await connection.DisposeAsync();
|
||||
}
|
||||
public DbSet<ListeningHistory> ListeningHistories => Set<ListeningHistory>();
|
||||
|
||||
private sealed class SqliteDatabaseTransaction(Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction inner)
|
||||
: IDatabaseTransaction
|
||||
{
|
||||
public Task CommitAsync(CancellationToken cancellationToken = default) =>
|
||||
inner.CommitAsync(cancellationToken);
|
||||
public async Task<IDatabaseTransaction> BeginTransactionAsync(System.Data.IsolationLevel isolationLevel, CancellationToken cancellationToken)
|
||||
{
|
||||
var transaction = await Database.BeginTransactionAsync(isolationLevel, cancellationToken);
|
||||
return new SqliteDatabaseTransaction(transaction);
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync() => inner.DisposeAsync();
|
||||
public override async ValueTask DisposeAsync()
|
||||
{
|
||||
await base.DisposeAsync();
|
||||
await connection.DisposeAsync();
|
||||
}
|
||||
|
||||
private sealed class SqliteDatabaseTransaction(Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction inner)
|
||||
: IDatabaseTransaction
|
||||
{
|
||||
public Task CommitAsync(CancellationToken cancellationToken = default) =>
|
||||
inner.CommitAsync(cancellationToken);
|
||||
|
||||
public ValueTask DisposeAsync() => inner.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,237 +1,238 @@
|
||||
using Musify.Domain.Entities;
|
||||
using Musify.Domain.ValueObjects;
|
||||
|
||||
namespace Musify.Application.Tests.TestSupport;
|
||||
|
||||
/// <summary>
|
||||
/// Builds fully-populated domain entities for tests, with sensible defaults for every
|
||||
/// <c>required</c> member. Every factory always returns a valid instance — never null — so
|
||||
/// callers never need a null check on what this class hands back.
|
||||
/// </summary>
|
||||
public static class TestEntities
|
||||
namespace Musify.Application.Tests.TestSupport
|
||||
{
|
||||
public static User User(
|
||||
long id = 1,
|
||||
string name = "test-user",
|
||||
string? firstName = "Test",
|
||||
string? secondName = "User",
|
||||
string? profilePictureUrl = null) =>
|
||||
new()
|
||||
{
|
||||
Id = id,
|
||||
Name = name,
|
||||
NormalizedName = name.ToUpperInvariant(),
|
||||
FirstName = firstName,
|
||||
SecondName = secondName,
|
||||
ProfilePictureUrl = profilePictureUrl
|
||||
};
|
||||
|
||||
public static TrackPictures ProcessedPictures() => new()
|
||||
/// <summary>
|
||||
/// Builds fully-populated domain entities for tests, with sensible defaults for every
|
||||
/// <c>required</c> member. Every factory always returns a valid instance — never null — so
|
||||
/// callers never need a null check on what this class hands back.
|
||||
/// </summary>
|
||||
public static class TestEntities
|
||||
{
|
||||
OriginalName = "original.webp",
|
||||
SmallName = "small.webp",
|
||||
MediumName = "medium.webp",
|
||||
LargeName = "large.webp",
|
||||
ProcessingStatus = ProcessingStatus.Completed
|
||||
};
|
||||
public static User User(
|
||||
long id = 1,
|
||||
string name = "test-user",
|
||||
string? firstName = "Test",
|
||||
string? secondName = "User",
|
||||
string? profilePictureUrl = null) =>
|
||||
new()
|
||||
{
|
||||
Id = id,
|
||||
Name = name,
|
||||
NormalizedName = name.ToUpperInvariant(),
|
||||
FirstName = firstName,
|
||||
SecondName = secondName,
|
||||
ProfilePictureUrl = profilePictureUrl
|
||||
};
|
||||
|
||||
public static TrackPictures PendingPictures() => new()
|
||||
{
|
||||
OriginalName = "original.webp",
|
||||
ProcessingStatus = ProcessingStatus.Pending
|
||||
};
|
||||
|
||||
public static TrackAudio ProcessedAudio(string folderName = "audio-folder") => new()
|
||||
{
|
||||
OriginalName = "original.mp3",
|
||||
FolderName = folderName,
|
||||
TranscodeStatus = ProcessingStatus.Completed
|
||||
};
|
||||
|
||||
public static TrackAudio PendingAudio() => new()
|
||||
{
|
||||
OriginalName = "original.mp3",
|
||||
TranscodeStatus = ProcessingStatus.Pending
|
||||
};
|
||||
|
||||
public static LocalTrack LocalTrack(
|
||||
User owner,
|
||||
string title = "Test Track",
|
||||
double durationSeconds = 180,
|
||||
TrackPictures? pictures = null,
|
||||
TrackAudio? audio = null,
|
||||
LifeCycleStatus lifeCycleStatus = LifeCycleStatus.Active) =>
|
||||
new()
|
||||
public static TrackPictures ProcessedPictures() => new()
|
||||
{
|
||||
Title = title,
|
||||
NormalizedTitle = title.ToUpperInvariant(),
|
||||
DurationSeconds = durationSeconds,
|
||||
LifeCycleStatus = lifeCycleStatus,
|
||||
OwnerUserId = owner.Id,
|
||||
Owner = owner,
|
||||
Pictures = pictures ?? ProcessedPictures(),
|
||||
Audio = audio ?? ProcessedAudio()
|
||||
OriginalName = "original.webp",
|
||||
SmallName = "small.webp",
|
||||
MediumName = "medium.webp",
|
||||
LargeName = "large.webp",
|
||||
ProcessingStatus = ProcessingStatus.Completed
|
||||
};
|
||||
|
||||
public static ExternalTrack ExternalTrack(
|
||||
string externalId = "video-id",
|
||||
string title = "Test External Track",
|
||||
double durationSeconds = 180,
|
||||
TrackSource source = TrackSource.YouTube,
|
||||
TrackPictures? pictures = null,
|
||||
TrackAudio? audio = null,
|
||||
LifeCycleStatus lifeCycleStatus = LifeCycleStatus.Active) =>
|
||||
new()
|
||||
public static TrackPictures PendingPictures() => new()
|
||||
{
|
||||
Title = title,
|
||||
NormalizedTitle = title.ToUpperInvariant(),
|
||||
DurationSeconds = durationSeconds,
|
||||
LifeCycleStatus = lifeCycleStatus,
|
||||
Source = source,
|
||||
ExternalId = externalId,
|
||||
Pictures = pictures ?? ProcessedPictures(),
|
||||
Audio = audio ?? ProcessedAudio()
|
||||
OriginalName = "original.webp",
|
||||
ProcessingStatus = ProcessingStatus.Pending
|
||||
};
|
||||
|
||||
public static Artist Artist(
|
||||
string name = "Test Artist",
|
||||
string externalId = "artist-id") =>
|
||||
new()
|
||||
public static TrackAudio ProcessedAudio(string folderName = "audio-folder") => new()
|
||||
{
|
||||
Name = name,
|
||||
NormalizedName = name.ToUpperInvariant(),
|
||||
ExternalId = externalId
|
||||
OriginalName = "original.mp3",
|
||||
FolderName = folderName,
|
||||
TranscodeStatus = ProcessingStatus.Completed
|
||||
};
|
||||
|
||||
public static PlayListPictures PlayListPictures() => new()
|
||||
{
|
||||
OriginalName = "original.webp",
|
||||
SmallName = "small.webp",
|
||||
MediumName = "medium.webp",
|
||||
LargeName = "large.webp"
|
||||
};
|
||||
|
||||
public static PlayList PlayList(
|
||||
long userId,
|
||||
string name = "Test Playlist",
|
||||
string? description = null,
|
||||
PlayListPictures? pictures = null,
|
||||
LifeCycleStatus lifeCycleStatus = LifeCycleStatus.Active) =>
|
||||
new()
|
||||
public static TrackAudio PendingAudio() => new()
|
||||
{
|
||||
UserId = userId,
|
||||
Name = name,
|
||||
NormalizedName = name.ToUpperInvariant(),
|
||||
Description = description,
|
||||
Pictures = pictures ?? PlayListPictures(),
|
||||
LifeCycleStatus = lifeCycleStatus
|
||||
OriginalName = "original.mp3",
|
||||
TranscodeStatus = ProcessingStatus.Pending
|
||||
};
|
||||
|
||||
public static AlbumPictures AlbumPictures() => new()
|
||||
{
|
||||
OriginalName = "original.webp",
|
||||
SmallName = "small.webp",
|
||||
MediumName = "medium.webp",
|
||||
LargeName = "large.webp"
|
||||
};
|
||||
public static LocalTrack LocalTrack(
|
||||
User owner,
|
||||
string title = "Test Track",
|
||||
double durationSeconds = 180,
|
||||
TrackPictures? pictures = null,
|
||||
TrackAudio? audio = null,
|
||||
LifeCycleStatus lifeCycleStatus = LifeCycleStatus.Active) =>
|
||||
new()
|
||||
{
|
||||
Title = title,
|
||||
NormalizedTitle = title.ToUpperInvariant(),
|
||||
DurationSeconds = durationSeconds,
|
||||
LifeCycleStatus = lifeCycleStatus,
|
||||
OwnerUserId = owner.Id,
|
||||
Owner = owner,
|
||||
Pictures = pictures ?? ProcessedPictures(),
|
||||
Audio = audio ?? ProcessedAudio()
|
||||
};
|
||||
|
||||
public static UserAlbum UserAlbum(
|
||||
long ownerUserId,
|
||||
string title = "Test Album",
|
||||
string? description = null,
|
||||
int? releaseYear = 2024,
|
||||
AlbumPictures? pictures = null) =>
|
||||
new()
|
||||
public static ExternalTrack ExternalTrack(
|
||||
string externalId = "video-id",
|
||||
string title = "Test External Track",
|
||||
double durationSeconds = 180,
|
||||
TrackSource source = TrackSource.YouTube,
|
||||
TrackPictures? pictures = null,
|
||||
TrackAudio? audio = null,
|
||||
LifeCycleStatus lifeCycleStatus = LifeCycleStatus.Active) =>
|
||||
new()
|
||||
{
|
||||
Title = title,
|
||||
NormalizedTitle = title.ToUpperInvariant(),
|
||||
DurationSeconds = durationSeconds,
|
||||
LifeCycleStatus = lifeCycleStatus,
|
||||
Source = source,
|
||||
ExternalId = externalId,
|
||||
Pictures = pictures ?? ProcessedPictures(),
|
||||
Audio = audio ?? ProcessedAudio()
|
||||
};
|
||||
|
||||
public static Artist Artist(
|
||||
string name = "Test Artist",
|
||||
string externalId = "artist-id") =>
|
||||
new()
|
||||
{
|
||||
Name = name,
|
||||
NormalizedName = name.ToUpperInvariant(),
|
||||
ExternalId = externalId
|
||||
};
|
||||
|
||||
public static PlayListPictures PlayListPictures() => new()
|
||||
{
|
||||
Title = title,
|
||||
NormalizedTitle = title.ToUpperInvariant(),
|
||||
Description = description,
|
||||
ReleaseYear = releaseYear,
|
||||
OwnerUserId = ownerUserId,
|
||||
Pictures = pictures ?? AlbumPictures()
|
||||
OriginalName = "original.webp",
|
||||
SmallName = "small.webp",
|
||||
MediumName = "medium.webp",
|
||||
LargeName = "large.webp"
|
||||
};
|
||||
|
||||
public static ExternalAlbum ExternalAlbum(
|
||||
string externalId = "album-id",
|
||||
string title = "Test External Album",
|
||||
TrackSource source = TrackSource.YouTube,
|
||||
string? thumbnailUrl = null) =>
|
||||
new()
|
||||
public static PlayList PlayList(
|
||||
long userId,
|
||||
string name = "Test Playlist",
|
||||
string? description = null,
|
||||
PlayListPictures? pictures = null,
|
||||
LifeCycleStatus lifeCycleStatus = LifeCycleStatus.Active) =>
|
||||
new()
|
||||
{
|
||||
UserId = userId,
|
||||
Name = name,
|
||||
NormalizedName = name.ToUpperInvariant(),
|
||||
Description = description,
|
||||
Pictures = pictures ?? PlayListPictures(),
|
||||
LifeCycleStatus = lifeCycleStatus
|
||||
};
|
||||
|
||||
public static AlbumPictures AlbumPictures() => new()
|
||||
{
|
||||
Title = title,
|
||||
NormalizedTitle = title.ToUpperInvariant(),
|
||||
Source = source,
|
||||
ExternalId = externalId,
|
||||
ThumbnailUrl = thumbnailUrl
|
||||
OriginalName = "original.webp",
|
||||
SmallName = "small.webp",
|
||||
MediumName = "medium.webp",
|
||||
LargeName = "large.webp"
|
||||
};
|
||||
|
||||
public static UploadIntent UploadIntent(
|
||||
long userId,
|
||||
UploadIntentPurpose purpose = UploadIntentPurpose.TrackPicture,
|
||||
string bucket = "test-bucket",
|
||||
string key = "temp/object.webp",
|
||||
string objectName = "object.webp",
|
||||
string contentType = "image/webp",
|
||||
long? expectedSizeBytes = 1024,
|
||||
UploadIntentStatus status = UploadIntentStatus.Issued,
|
||||
DateTime? expiresAt = null) =>
|
||||
new()
|
||||
{
|
||||
UserId = userId,
|
||||
Bucket = bucket,
|
||||
Key = key,
|
||||
ObjectName = objectName,
|
||||
ContentType = contentType,
|
||||
ExpectedSizeBytes = expectedSizeBytes,
|
||||
Purpose = purpose,
|
||||
Status = status,
|
||||
ExpiresAt = expiresAt ?? DateTime.UtcNow.AddMinutes(5)
|
||||
};
|
||||
public static UserAlbum UserAlbum(
|
||||
long ownerUserId,
|
||||
string title = "Test Album",
|
||||
string? description = null,
|
||||
int? releaseYear = 2024,
|
||||
AlbumPictures? pictures = null) =>
|
||||
new()
|
||||
{
|
||||
Title = title,
|
||||
NormalizedTitle = title.ToUpperInvariant(),
|
||||
Description = description,
|
||||
ReleaseYear = releaseYear,
|
||||
OwnerUserId = ownerUserId,
|
||||
Pictures = pictures ?? AlbumPictures()
|
||||
};
|
||||
|
||||
public static Mix Mix(
|
||||
long userId,
|
||||
string title = "Test Mix",
|
||||
string? subtitle = null,
|
||||
int position = 0) =>
|
||||
new()
|
||||
{
|
||||
UserId = userId,
|
||||
Title = title,
|
||||
Subtitle = subtitle,
|
||||
Position = position
|
||||
};
|
||||
public static ExternalAlbum ExternalAlbum(
|
||||
string externalId = "album-id",
|
||||
string title = "Test External Album",
|
||||
TrackSource source = TrackSource.YouTube,
|
||||
string? thumbnailUrl = null) =>
|
||||
new()
|
||||
{
|
||||
Title = title,
|
||||
NormalizedTitle = title.ToUpperInvariant(),
|
||||
Source = source,
|
||||
ExternalId = externalId,
|
||||
ThumbnailUrl = thumbnailUrl
|
||||
};
|
||||
|
||||
public static MixItem MixItem(
|
||||
Guid mixId,
|
||||
int position = 0,
|
||||
MixItemSource source = MixItemSource.Musify,
|
||||
Guid? trackId = null,
|
||||
string? videoId = null,
|
||||
string title = "Test Mix Item",
|
||||
string? artist = "Test Artist",
|
||||
double durationSeconds = 180,
|
||||
bool isExplicit = false) =>
|
||||
new()
|
||||
{
|
||||
MixId = mixId,
|
||||
Position = position,
|
||||
Source = source,
|
||||
TrackId = trackId,
|
||||
VideoId = videoId,
|
||||
Title = title,
|
||||
Artist = artist,
|
||||
DurationSeconds = durationSeconds,
|
||||
IsExplicit = isExplicit
|
||||
};
|
||||
public static UploadIntent UploadIntent(
|
||||
long userId,
|
||||
UploadIntentPurpose purpose = UploadIntentPurpose.TrackPicture,
|
||||
string bucket = "test-bucket",
|
||||
string key = "temp/object.webp",
|
||||
string objectName = "object.webp",
|
||||
string contentType = "image/webp",
|
||||
long? expectedSizeBytes = 1024,
|
||||
UploadIntentStatus status = UploadIntentStatus.Issued,
|
||||
DateTime? expiresAt = null) =>
|
||||
new()
|
||||
{
|
||||
UserId = userId,
|
||||
Bucket = bucket,
|
||||
Key = key,
|
||||
ObjectName = objectName,
|
||||
ContentType = contentType,
|
||||
ExpectedSizeBytes = expectedSizeBytes,
|
||||
Purpose = purpose,
|
||||
Status = status,
|
||||
ExpiresAt = expiresAt ?? DateTime.UtcNow.AddMinutes(5)
|
||||
};
|
||||
|
||||
public static ListeningHistory ListeningHistory(
|
||||
long userId,
|
||||
Guid trackId,
|
||||
DateTime? listenedAt = null) =>
|
||||
new()
|
||||
{
|
||||
UserId = userId,
|
||||
TrackId = trackId,
|
||||
ListenedAt = listenedAt ?? DateTime.UtcNow
|
||||
};
|
||||
public static Mix Mix(
|
||||
long userId,
|
||||
string title = "Test Mix",
|
||||
string? subtitle = null,
|
||||
int position = 0) =>
|
||||
new()
|
||||
{
|
||||
UserId = userId,
|
||||
Title = title,
|
||||
Subtitle = subtitle,
|
||||
Position = position
|
||||
};
|
||||
|
||||
public static MixItem MixItem(
|
||||
Guid mixId,
|
||||
int position = 0,
|
||||
MixItemSource source = MixItemSource.Musify,
|
||||
Guid? trackId = null,
|
||||
string? videoId = null,
|
||||
string title = "Test Mix Item",
|
||||
string? artist = "Test Artist",
|
||||
double durationSeconds = 180,
|
||||
bool isExplicit = false) =>
|
||||
new()
|
||||
{
|
||||
MixId = mixId,
|
||||
Position = position,
|
||||
Source = source,
|
||||
TrackId = trackId,
|
||||
VideoId = videoId,
|
||||
Title = title,
|
||||
Artist = artist,
|
||||
DurationSeconds = durationSeconds,
|
||||
IsExplicit = isExplicit
|
||||
};
|
||||
|
||||
public static ListeningHistory ListeningHistory(
|
||||
long userId,
|
||||
Guid trackId,
|
||||
DateTime? listenedAt = null) =>
|
||||
new()
|
||||
{
|
||||
UserId = userId,
|
||||
TrackId = trackId,
|
||||
ListenedAt = listenedAt ?? DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user