Add FindByNameRequired throwing variant (yak: Add FindByName! throwing variant)

This commit is contained in:
2026-09-14 21:23:57 +01:00
parent 325925a592
commit 4b6e6f5a00
4 changed files with 203 additions and 1 deletions
+118
View File
@@ -339,4 +339,122 @@ public class BeforeTests
DbBase.Context = null; // cleanup
}
// ---------------------------------------------------------------------
// (17) DbBase.FindRequired<T> finds the matching entity — same result as Find<T>.
// ---------------------------------------------------------------------
[Fact]
public void DbBase_FindRequired_T_finds_matching_entity()
{
var db = new DbContext();
var acme = new Client { Name = "Acme" };
var globex = new Client { Name = "Globex" };
db.Attach(acme);
db.Attach(globex);
var result = DbBase.FindRequired<Client>(c => c.Name == "Globex", db, "name == Globex");
Assert.Same(globex, result);
}
// ---------------------------------------------------------------------
// (18) DbBase.FindRequired<T> throws when no match is found.
// ---------------------------------------------------------------------
[Fact]
public void DbBase_FindRequired_T_throws_when_no_match()
{
var db = new DbContext();
db.Attach(new Client { Name = "Acme" });
var ex = Assert.Throws<InvalidOperationException>(() =>
DbBase.FindRequired<Client>(c => c.Name == "Nobody", db, "name == Nobody"));
Assert.Contains("Nobody", ex.Message);
Assert.Contains("no matching Client found", ex.Message);
}
// ---------------------------------------------------------------------
// (19) DbBase.FindRequired<T> throws with a useful message when no context is
// configured (both explicit null and singleton null).
// ---------------------------------------------------------------------
[Fact]
public void DbBase_FindRequired_T_throws_when_no_context_configured()
{
DbBase.Context = null;
var ex = Assert.Throws<InvalidOperationException>(() =>
DbBase.FindRequired<Client>(_ => true, null, "true"));
Assert.Contains("no DbContext configured", ex.Message);
Assert.Contains("DbBase.Context", ex.Message);
}
// ---------------------------------------------------------------------
// (20) Client.FindByNameRequired finds the matching client by name.
// ---------------------------------------------------------------------
[Fact]
public void Client_FindByNameRequired_finds_by_name()
{
var db = new DbContext();
var acme = new Client { Name = "Acme Corp" };
db.Attach(acme);
var result = Client.FindByNameRequired("Acme Corp", db);
Assert.Same(acme, result);
}
// ---------------------------------------------------------------------
// (21) Client.FindByNameRequired throws when no match — message includes name.
// ---------------------------------------------------------------------
[Fact]
public void Client_FindByNameRequired_throws_with_name_when_not_found()
{
var db = new DbContext();
db.Attach(new Client { Name = "Acme Corp" });
var ex = Assert.Throws<InvalidOperationException>(() =>
Client.FindByNameRequired("Nobody", db));
Assert.Contains("Nobody", ex.Message);
Assert.Contains("no matching Client found", ex.Message);
}
// ---------------------------------------------------------------------
// (22) Client.FindByNameRequired uses Context singleton when no context arg.
// ---------------------------------------------------------------------
[Fact]
public void Client_FindByNameRequired_uses_Context_singleton()
{
var db = new DbContext();
var acme = new Client { Name = "SingletonCo" };
db.Attach(acme);
DbBase.Context = db;
try
{
var result = Client.FindByNameRequired("SingletonCo");
Assert.Same(acme, result);
}
finally
{
DbBase.Context = null;
}
}
// ---------------------------------------------------------------------
// (23) Client.FindByNameRequired throws with a useful message when the
// singleton is not configured (no arg, no singleton).
// ---------------------------------------------------------------------
[Fact]
public void Client_FindByNameRequired_throws_with_context_hint_when_singleton_null()
{
DbBase.Context = null;
var ex = Assert.Throws<InvalidOperationException>(() =>
Client.FindByNameRequired("SomeBody"));
Assert.Contains("no DbContext configured", ex.Message);
Assert.Contains("DbBase.Context", ex.Message);
}
}