using System; using System.Collections.Generic; using System.IO; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json.Linq; using LCU.Library.Internal.DataCleanUpModel.Entities; using LCU.Api.Internal.DataCleanUpLib.Persistence; namespace LCU.Api.Internal.DataCleanUpTest.RepositoryTests; public class DbContextTestSet { public IReadOnlyCollection? Ltv_DataCleanUps { get; set; } } public abstract class DbContextFromJsonTest { private const string RepositoryFolder = "Repositories"; private const string LtvFileName = "loanToValue_repo"; public DbContextTestSet GetTestRecords() { var testSet = new DbContextTestSet(); // Get the absolute path to the JSON file var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory); var path = $"{ basePath}//{RepositoryFolder}//{LtvFileName}.json"; if (!File.Exists(path)) { throw new ArgumentException($"Could not find file at path: {path}"); } var fileData = File.ReadAllText(path); var allData = JObject.Parse(fileData); var data = allData["Ltv_DataCleanUps"]; testSet.Ltv_DataCleanUps = data.ToObject>(); return testSet; } public AppDbContext GetBlankContext() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; return new AppDbContext(options); } public AppDbContext GetContextWithData() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var context = new AppDbContext(options); var testData = GetTestRecords(); AddTo(context.LtvDataCleanUp, testData.Ltv_DataCleanUps); context.SaveChanges(); return context; } private static void AddTo(DbSet collection, IEnumerable range) where T : class { if (range != null) collection.AddRange(range); } }