//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.Persistence.Initialization.Tests;
using Microsoft.Extensions.Logging.Abstractions;
using MUnique.OpenMU.Persistence.EntityFramework;
using MUnique.OpenMU.PlugIns;
///
/// 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.
///
[TestFixture]
internal class TypedContextModelTests
{
///
/// 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.
///
[Test]
public void PlugInConfigurationContextBuildsModel()
{
var provider = new PersistenceContextProvider(new NullLoggerFactory(), null);
using var context = provider.CreateNewTypedContext(typeof(PlugInConfiguration), false);
try
{
_ = context.GetAsync().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;
}
}
}
}