using LCU.Api.Internal.MaidBotLib.Models; using LCU.Api.Internal.MaidBotLib.Persistence; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Text; namespace LCU.Api.Internal.MaidBotTests.RepositoryTests { public class DbContextTestSet { public IReadOnlyCollection LoggingMatrixSet { get; set; } } public abstract class DbContextFromJsonTest { public DbContextTestSet GetTestAccounts(string filePath) { var testSet = new DbContextTestSet(); // Get the absolute path to the JSON file var path = Path.IsPathRooted(filePath) ? filePath : Path.GetRelativePath(Directory.GetCurrentDirectory(), filePath); if (!File.Exists(path)) { throw new ArgumentException($"Could not find file at path: {path}"); } var fileData = File.ReadAllText(filePath); var allData = JObject.Parse(fileData); var runInfo = allData["LoggingMatrixs"]; testSet.LoggingMatrixSet = runInfo?.ToObject>(); return testSet; } public AppDbContext GetBlankContext() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; return new AppDbContext(options); } public AppDbContext GetContextWithData(string jsonFileName) { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var context = new AppDbContext(options); var testData = GetTestAccounts(jsonFileName); AddTo(context.LoggingMatrixs, testData.LoggingMatrixSet); context.SaveChanges(); return context; } private static void AddTo(DbSet collection, IEnumerable range) where T : class { if (range != null) collection.AddRange(range); } } }