Files
NTBug/backend/tests/Helpdesk.IntegrationTests/DatabaseTests.cs
T

60 lines
3.1 KiB
C#

using Helpdesk.Api.Controllers;
using Helpdesk.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MySqlConnector;
using static Helpdesk.IntegrationTests.BackendFixture;
namespace Helpdesk.IntegrationTests;
public sealed class DatabaseTests(BackendFixture fixture) : IClassFixture<BackendFixture>
{
[Fact]
public async Task MigratorAppliesEveryScriptAndEfCanReadTheSchema()
{
using var client = fixture.CreateClient();
await using var scope = fixture.Services.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
var expected = Directory.EnumerateFiles(MigrationsPath, "V*__*.sql")
.Select(Path.GetFileName).Order(StringComparer.Ordinal).ToArray();
var applied = await db.Database
.SqlQueryRaw<string>("SELECT scriptname AS Value FROM schema_version ORDER BY schemaversionid")
.ToListAsync(Cancellation);
Assert.Equal(expected, applied);
var health = await ReadAsync<HealthResponse>(client.GetAsync("/health", Cancellation));
Assert.Equal(new HealthResponse("Healthy", expected[^1]), health);
await db.Users.AsNoTracking().ToListAsync(Cancellation);
await db.AuthTokens.AsNoTracking().ToListAsync(Cancellation);
await db.Buildings.AsNoTracking().ToListAsync(Cancellation);
await db.IssueCategories.AsNoTracking().ToListAsync(Cancellation);
await db.ProposalCategories.AsNoTracking().ToListAsync(Cancellation);
await db.Issues.AsNoTracking().ToListAsync(Cancellation);
await db.IssueEvents.AsNoTracking().ToListAsync(Cancellation);
await db.IssueTimings.ToListAsync(Cancellation);
await db.Attachments.AsNoTracking().ToListAsync(Cancellation);
await db.Proposals.AsNoTracking().ToListAsync(Cancellation);
await db.ProposalEvents.AsNoTracking().ToListAsync(Cancellation);
await db.ProposalVotes.AsNoTracking().ToListAsync(Cancellation);
await db.ProposalScores.ToListAsync(Cancellation);
await db.Notifications.AsNoTracking().ToListAsync(Cancellation);
await db.NotificationDeliveries.AsNoTracking().ToListAsync(Cancellation);
await db.PushDevices.AsNoTracking().ToListAsync(Cancellation);
}
[Fact]
public async Task DatabaseRejectsAnAssignedIssueWithoutAnAssignee()
{
await using var scope = fixture.Services.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
var reporterId = await db.Users.Select(user => user.Id).SingleAsync(Cancellation);
var categoryId = await db.IssueCategories.MinAsync(category => category.Id, Cancellation);
var error = await Assert.ThrowsAsync<MySqlException>(() => db.Database.ExecuteSqlInterpolatedAsync($"""
INSERT INTO issue (title, description, category_id, reporter_id, status)
VALUES ('Test', 'Test', {categoryId}, {reporterId}, 'ASSIGNED')
""", Cancellation));
Assert.Contains("ck_issue_assignee", error.Message);
}
}