using Microsoft.Extensions.Logging.Abstractions; using PlotLine.Services; using PlotLine.ViewModels; internal static class EntityMergeTests { public static void ConfirmationAndDistinctRecordsAreRequired() { // Null dependencies make any repository access before validation fail this test. var locations = new LocationService(null!, null!, null!, null!, NullLogger.Instance); var assets = new AssetService(null!, null!, null!, null!, null!, null!); foreach (var merge in new Func[] { async form => { await locations.MergeAsync(form); }, async form => { await assets.MergeAsync(form); } }) { Reject(merge, new EntityMergeForm { SourceID = 1, TargetID = 2 }, "Confirm"); Reject(merge, new EntityMergeForm { SourceID = 1, TargetID = 1, Confirm = true }, "different"); Reject(merge, new EntityMergeForm { SourceID = 1, TargetID = 0, Confirm = true }, "different"); Reject(merge, new EntityMergeForm { SourceID = 0, TargetID = 2, Confirm = true }, "different"); } } private static void Reject(Func merge, EntityMergeForm form, string message) { try { merge(form).GetAwaiter().GetResult(); } catch (InvalidOperationException ex) when (ex.Message.Contains(message, StringComparison.Ordinal)) { return; } throw new Exception("Invalid merge was not rejected before database access."); } }