Files
AdamuSw/tests/MUnique.OpenMU.Persistence.Initialization.Tests/TypedContextModelTests.cs
Acentech Dev af46499279 fix(build): run the persistence generator against the current data model
The PreBuild targets ran the generator with "--no-build", so it used whatever
assemblies happened to sit in its output folder. When that copy of the data model
was older than a newly added type, the generator regenerated the checked-in
*.Generated.cs files WITHOUT that type and overwrote them in the source tree.

Nothing failed at build time: the C# compile stayed green and docker builds
(-p:ci=true) skip the generator entirely, so they compiled whatever was in the
tree. The damage only surfaced at runtime, when EF validated the model and found
the inherited GameConfiguration.CastleSiegeConfiguration navigation pointing at a
keyless type - the server died on startup with "The entity type
'CastleSiegeConfiguration' requires a primary key to be defined".

Dropping the switch makes the generator build first, so its output always matches
the data model. The regenerated files here are that missing output: the Castle
Siege mappings, and the packet tests for packets whose XML was already committed.

TypedContextModelTests builds the typed context the startup reads its plugin
configurations through - the first one to touch the model - so this class of
breakage fails in seconds without a database.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-04 21:43:39 +03:00

57 lines
2.1 KiB
C#

// <copyright file="TypedContextModelTests.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>
namespace MUnique.OpenMU.Persistence.Initialization.Tests;
using Microsoft.Extensions.Logging.Abstractions;
using MUnique.OpenMU.Persistence.EntityFramework;
using MUnique.OpenMU.PlugIns;
/// <summary>
/// Tests that the typed contexts can build their entity model. A typed context keeps only the edited type
/// (plus its aggregate) and ignores every other type, so a type which is only mapped in the full context
/// slips through the build and blows up at runtime instead. The startup reads the plugin configurations
/// through such a context before anything else, so a broken model there means the server doesn't start.
/// No database is needed: building the model already runs the EF model validation.
/// </summary>
[TestFixture]
internal class TypedContextModelTests
{
/// <summary>
/// Builds the model of the typed context which the startup uses to read the plugin configurations.
/// A failing connection is fine here (there may be no database); a failing model is not.
/// </summary>
[Test]
public void PlugInConfigurationContextBuildsModel()
{
var provider = new PersistenceContextProvider(new NullLoggerFactory(), null);
using var context = provider.CreateNewTypedContext(typeof(PlugInConfiguration), false);
try
{
_ = context.GetAsync<PlugInConfiguration>().AsTask().GetAwaiter().GetResult();
}
catch (Exception ex)
{
AssertNoModelError(ex);
}
}
private static void AssertNoModelError(Exception exception)
{
for (var ex = exception; ex is not null; ex = ex.InnerException!)
{
if (ex is InvalidOperationException && ex.Message.Contains("requires a primary key"))
{
Assert.Fail($"The entity model of the typed context is broken: {ex.Message}");
}
if (ex.InnerException is null)
{
break;
}
}
}
}