Rating, notes, condition, region, purchase price and date, plus a market
value carrying the timestamp and source that make it interpretable.
Condition is load-bearing rather than cosmetic: price feeds quote per
condition, so it selects which quoted price applies to a copy. Market value
records when it was captured and where it came from — a collection total is
only as good as its staleness — and an edit to an unrelated field leaves
that timestamp alone, so a stale price cannot start looking freshly checked.
Two storage decisions worth naming:
* Money is stored as integer minor units. SQLite has no decimal type and
EF Core maps decimal to TEXT, which compares lexically: "9.00" sorts
above "10.00" and SUM is unavailable. A value converter keeps decimals
in C# while ordering and totalling work. A test pins the ordering.
* Enums serialise as names. The default is ordinals, which meant the API
rejected the browser's {"condition":"Cib"} with a 400 while the C# tests
passed, because they round-tripped ints and never spoke the client's
dialect. The tests now share the API's serializer options.
Also fixes a data-loss bug in the Python tools. Both built their PUT body
from a hardcoded list of field names, so any column added to the model was
omitted and therefore nulled. Adding collector fields meant the next art or
enrichment run would have erased every rating, note, condition, price and
valuation in the library. Payloads are now built by excluding the handful of
server-owned fields, so new columns carry through by default.
The migration was rehearsed against a copy of the live database before being
applied: 105 rows, descriptions and developers intact.
67 backend tests, 8 frontend.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
80 lines
3.0 KiB
C#
80 lines
3.0 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace LudosData.Api.Tests;
|
|
|
|
/// <summary>
|
|
/// Boots the real application against a throwaway SQLite file and upload folder.
|
|
///
|
|
/// Every collaborator the API uses in production is exercised here — the same
|
|
/// pipeline, the same Identity configuration, the same JWT validation. Only the
|
|
/// storage locations and the signing key are swapped, so a test that passes says
|
|
/// something about the shipped application rather than about a stand-in.
|
|
/// </summary>
|
|
public class LudosApiFactory : WebApplicationFactory<Program>, IAsyncLifetime
|
|
{
|
|
private readonly string _root = Path.Combine(
|
|
Path.GetTempPath(), "ludos-tests", Guid.NewGuid().ToString("N"));
|
|
|
|
public string SigningKey { get; } = "test-signing-key-of-at-least-32-characters-long";
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
Directory.CreateDirectory(_root);
|
|
|
|
builder.UseEnvironment(Environments.Development);
|
|
builder.UseSetting("ConnectionStrings:Default", $"Data Source={Path.Combine(_root, "test.db")}");
|
|
builder.UseSetting("Uploads:RootPath", Path.Combine(_root, "uploads"));
|
|
builder.UseSetting("DataProtection:KeysPath", Path.Combine(_root, "keys"));
|
|
builder.UseSetting("Jwt:Key", SigningKey);
|
|
builder.UseSetting("Jwt:Issuer", "LudosData");
|
|
builder.UseSetting("Jwt:Audience", "LudosData");
|
|
// The seeder would otherwise create a user and import 105 games, which
|
|
// would make "does this user see only their own rows" untestable.
|
|
builder.UseSetting("Seed:Enabled", "false");
|
|
}
|
|
|
|
/// <summary>Registers a fresh user and returns a client authenticated as them.</summary>
|
|
public async Task<HttpClient> CreateUserClientAsync(string userName)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostJsonAsync("/api/auth/register", new
|
|
{
|
|
userName,
|
|
email = $"{userName}@example.test",
|
|
password = "TestPassword123",
|
|
});
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
var auth = await response.Content.ReadJsonAsync<AuthPayload>();
|
|
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", auth!.Token);
|
|
return client;
|
|
}
|
|
|
|
public Task InitializeAsync() => Task.CompletedTask;
|
|
|
|
public new async Task DisposeAsync()
|
|
{
|
|
await base.DisposeAsync();
|
|
try
|
|
{
|
|
if (Directory.Exists(_root))
|
|
{
|
|
Directory.Delete(_root, recursive: true);
|
|
}
|
|
}
|
|
catch (IOException)
|
|
{
|
|
// A locked SQLite handle on a temp file is not worth failing a run over.
|
|
}
|
|
}
|
|
|
|
public record AuthPayload(string Token, DateTimeOffset ExpiresAt, UserPayload User);
|
|
public record UserPayload(string Id, string UserName, string? Email);
|
|
}
|