Strip comments and dedupe normalized literals in backend tests
- Remove all // and /// comments from the backend test suite. - Replace hardcoded normalized-form string literals (e.g. NormalizedTitle/ NormalizedName duplicating a title/name in upper case) with a variable plus .ToUpperInvariant(), so the two values can't drift apart.
This commit is contained in:
+4
-2
@@ -13,7 +13,8 @@ namespace Musify.Infrastructure.Tests.Persistence
|
||||
await using var database = fixture.CreateDatabase();
|
||||
var before = DateTime.UtcNow;
|
||||
|
||||
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = "audit-user", NormalizedName = "AUDIT-USER" };
|
||||
var name = "audit-user";
|
||||
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = name, NormalizedName = name.ToUpperInvariant() };
|
||||
await database.Users.AddAsync(user, TestContext.Current.CancellationToken);
|
||||
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
|
||||
|
||||
@@ -28,7 +29,8 @@ namespace Musify.Infrastructure.Tests.Persistence
|
||||
{
|
||||
await using var database = fixture.CreateDatabase();
|
||||
|
||||
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = "audit-user-2", NormalizedName = "AUDIT-USER-2" };
|
||||
var name = "audit-user-2";
|
||||
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = name, NormalizedName = name.ToUpperInvariant() };
|
||||
await database.Users.AddAsync(user, TestContext.Current.CancellationToken);
|
||||
await database.SaveChangesAsync(TestContext.Current.CancellationToken);
|
||||
var originalCreatedAt = user.CreatedAt;
|
||||
|
||||
@@ -14,13 +14,15 @@ namespace Musify.Infrastructure.Tests.Persistence
|
||||
{
|
||||
await using var database = fixture.CreateDatabase();
|
||||
|
||||
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = "roundtrip-user", NormalizedName = "ROUNDTRIP-USER" };
|
||||
var userName = "roundtrip-user";
|
||||
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = userName, NormalizedName = userName.ToUpperInvariant() };
|
||||
await database.Users.AddAsync(user, TestContext.Current.CancellationToken);
|
||||
|
||||
var trackTitle = "Round Trip Track";
|
||||
var track = new LocalTrack
|
||||
{
|
||||
Title = "Round Trip Track",
|
||||
NormalizedTitle = "ROUND TRIP TRACK",
|
||||
Title = trackTitle,
|
||||
NormalizedTitle = trackTitle.ToUpperInvariant(),
|
||||
DurationSeconds = 123.4,
|
||||
OwnerUserId = user.Id,
|
||||
Owner = user,
|
||||
@@ -34,7 +36,7 @@ namespace Musify.Infrastructure.Tests.Persistence
|
||||
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(trackTitle, stored.Title);
|
||||
Assert.Equal("cover.webp", stored.Pictures.OriginalName);
|
||||
Assert.Equal("song.mp3", stored.Audio.OriginalName);
|
||||
Assert.Equal(user.Id, ((LocalTrack)stored).OwnerUserId);
|
||||
@@ -60,8 +62,10 @@ namespace Musify.Infrastructure.Tests.Persistence
|
||||
{
|
||||
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 userName = "cascade-user";
|
||||
var user = new User { Id = Random.Shared.NextInt64(1, long.MaxValue), Name = userName, NormalizedName = userName.ToUpperInvariant() };
|
||||
var albumTitle = "Cascade Album";
|
||||
var album = new UserAlbum { Title = albumTitle, NormalizedTitle = albumTitle.ToUpperInvariant(), OwnerUserId = user.Id };
|
||||
var track = BuildExternalTrack($"cascade-{Guid.NewGuid():N}");
|
||||
var link = new AlbumHasTrack { AlbumId = album.Id, TrackId = track.Id, TrackNumber = 1 };
|
||||
|
||||
@@ -78,15 +82,19 @@ namespace Musify.Infrastructure.Tests.Persistence
|
||||
Assert.True(await verifier.ExternalTracks.AnyAsync(t => t.Id == track.Id, TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
private static ExternalTrack BuildExternalTrack(string externalId) => new()
|
||||
private static ExternalTrack BuildExternalTrack(string externalId)
|
||||
{
|
||||
Title = "External",
|
||||
NormalizedTitle = "EXTERNAL",
|
||||
DurationSeconds = 100,
|
||||
Source = TrackSource.YouTube,
|
||||
ExternalId = externalId,
|
||||
Pictures = new TrackPictures(),
|
||||
Audio = new TrackAudio()
|
||||
};
|
||||
var title = "External";
|
||||
return new ExternalTrack
|
||||
{
|
||||
Title = title,
|
||||
NormalizedTitle = title.ToUpperInvariant(),
|
||||
DurationSeconds = 100,
|
||||
Source = TrackSource.YouTube,
|
||||
ExternalId = externalId,
|
||||
Pictures = new TrackPictures(),
|
||||
Audio = new TrackAudio()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,15 +131,9 @@ namespace Musify.Infrastructure.Tests.Services
|
||||
using var httpClient = new HttpClient();
|
||||
using var request = new HttpRequestMessage(HttpMethod.Put, uploadUrl)
|
||||
{
|
||||
// Not StringContent: it appends "; charset=utf-8" to Content-Type, which no longer
|
||||
// matches the "text/plain" the presigned URL's signature was computed over (SigV4
|
||||
// signs Content-Type byte-for-byte) and the server answers 403 SignatureDoesNotMatch.
|
||||
Content = new ByteArrayContent(Encoding.UTF8.GetBytes("presigned content"))
|
||||
};
|
||||
request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
|
||||
// GetUploadUrlAsync defaults preventOverwrite to true, which signs the URL together with
|
||||
// this header (see StorageService.GetUploadUrlAsync) — the actual PUT has to send it too,
|
||||
// or the signature no longer matches and the server answers 403.
|
||||
request.Headers.TryAddWithoutValidation("If-None-Match", "*");
|
||||
var response = await httpClient.SendAsync(request, TestContext.Current.CancellationToken);
|
||||
|
||||
|
||||
@@ -12,13 +12,6 @@ using Xunit;
|
||||
|
||||
namespace Musify.Infrastructure.Tests.TestSupport
|
||||
{
|
||||
/// <summary>
|
||||
/// One Postgres container and one SeaweedFS container (same image and startup
|
||||
/// command as <c>deploy/compose.yml</c>), started once for every test in the
|
||||
/// <see cref="InfrastructureCollection"/> and torn down when the run finishes.
|
||||
/// EF Core migrations run once here too, against the real <c>Database</c>
|
||||
/// DbContext, so a broken migration fails the whole run immediately.
|
||||
/// </summary>
|
||||
public sealed class InfrastructureTestFixture : IAsyncLifetime
|
||||
{
|
||||
public const string S3AccessKey = "musify-test";
|
||||
@@ -51,7 +44,6 @@ namespace Musify.Infrastructure.Tests.TestSupport
|
||||
await using var database = new Database(new DatabaseConfiguration { ConnectionString = ConnectionString });
|
||||
await database.Database.MigrateAsync();
|
||||
|
||||
// SeaweedFS does not auto-create buckets (see deploy/compose.yml's seaweedfs-init).
|
||||
using var s3 = CreateS3Client();
|
||||
await s3.PutBucketAsync(S3Bucket);
|
||||
}
|
||||
@@ -62,9 +54,6 @@ namespace Musify.Infrastructure.Tests.TestSupport
|
||||
await seaweedFs.DisposeAsync();
|
||||
}
|
||||
|
||||
/// <summary>A fresh, DI-wired <see cref="Database"/>, so <c>AuditableEntityInterceptor</c>
|
||||
/// (attached in <c>AddDatabase</c>'s DI registration, not in the class itself) is active —
|
||||
/// same wiring the real app uses, not a bare <c>new Database(...)</c>.</summary>
|
||||
public Database CreateDatabase()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
@@ -76,8 +65,6 @@ namespace Musify.Infrastructure.Tests.TestSupport
|
||||
return services.BuildServiceProvider().GetRequiredService<Database>();
|
||||
}
|
||||
|
||||
/// <summary>An <see cref="IAmazonS3"/> client configured exactly like
|
||||
/// <c>ServicesDependencyInjection.AddStorageService</c>, pointed at the SeaweedFS container.</summary>
|
||||
public IAmazonS3 CreateS3Client() =>
|
||||
new AmazonS3Client(S3AccessKey, S3SecretKey, new AmazonS3Config
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user