Tests for RealmService.FindBySlug

This commit is contained in:
eelke 2026-02-15 07:15:11 +01:00
parent 7a5cb703ec
commit a80c133e2a
3 changed files with 49 additions and 16 deletions

View file

@ -19,8 +19,8 @@ public class DbFixture : IAsyncLifetime
private string Password => "password";
private string DbHostname => _postgresqlServer.Hostname;
private int DbPort => _postgresqlServer.GetMappedPublicPort(PostgreSqlBuilder.PostgreSqlPort);
public Db CreateDbContext(string dbName)
public Db CreateDbContext(string dbName = "testdb")
{
var db = new Db(Options.Create<DbConfiguration>(new()
{

View file

@ -1,4 +1,5 @@
using FluentResults;
using IdentityShroud.Core.Contracts;
using IdentityShroud.Core.Services;
using IdentityShroud.Core.Tests.Fixtures;
using IdentityShroud.Core.Tests.Substitutes;
@ -8,19 +9,20 @@ namespace IdentityShroud.Core.Tests.Services;
public class RealmServiceTests : IClassFixture<DbFixture>
{
private readonly Db _db;
private readonly DbFixture _dbFixture;
private readonly IEncryptionService _encryptionService = EncryptionServiceSubstitute.CreatePassthrough();
public RealmServiceTests(DbFixture dbFixture)
{
_db = dbFixture.CreateDbContext("realmservice");
if (!_db.Database.EnsureCreated())
TruncateTables();
_dbFixture = dbFixture;
using Db db = dbFixture.CreateDbContext();
if (!db.Database.EnsureCreated())
TruncateTables(db);
}
private void TruncateTables()
private void TruncateTables(Db db)
{
_db.Database.ExecuteSqlRaw("TRUNCATE realm CASCADE;");
db.Database.ExecuteSqlRaw("TRUNCATE realm CASCADE;");
}
[Theory]
@ -28,17 +30,20 @@ public class RealmServiceTests : IClassFixture<DbFixture>
[InlineData("a7c2a39c-3ed9-4790-826e-43bb2e5e480c")]
public async Task Create(string? idString)
{
// Setup
Guid? realmId = null;
if (idString is not null)
realmId = new(idString);
var encryptionService = EncryptionServiceSubstitute.CreatePassthrough();
RealmService sut = new(_db, encryptionService);
using Db db = _dbFixture.CreateDbContext();
RealmService sut = new(db, _encryptionService);
// Act
var response = await sut.Create(
new(realmId, "slug", "New realm"),
TestContext.Current.CancellationToken);
// Verify
RealmCreateResponse val = ResultAssert.Success(response);
if (realmId.HasValue)
Assert.Equal(realmId, val.Id);
@ -50,4 +55,33 @@ public class RealmServiceTests : IClassFixture<DbFixture>
// TODO verify data has been stored!
}
[Theory]
[InlineData("slug", null)]
[InlineData("foo", "Foo")]
public async Task FindBySlug(string slug, string? name)
{
using (var setupContext = _dbFixture.CreateDbContext())
{
setupContext.Realms.Add(new()
{
Slug = "foo",
Name = "Foo",
});
setupContext.Realms.Add(new()
{
Slug = "bar",
Name = "Bar",
});
await setupContext.SaveChangesAsync(TestContext.Current.CancellationToken);
}
using Db actContext = _dbFixture.CreateDbContext();
RealmService sut = new(actContext, _encryptionService);
// Act
var result = await sut.FindBySlug(slug, TestContext.Current.CancellationToken);
Assert.Equal(name, result?.Name);
}
}