using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using LCU.API.Collections.RepoApiTests.Repository; using LCU.Shared.Collections.RepoApiLib.Models; using LCU.Shared.Collections.RepoApiLib.Persistence; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json.Linq; namespace LCU.API.Collections.RepoApiTests { public class DbContextTestSet { public IReadOnlyCollection RepoAccounts { get; set; } public IReadOnlyCollection AuctionAssignments { get; set; } public IReadOnlyCollection EventAlerts { get; set; } public IReadOnlyCollection ManualStatuses { get; set; } } public abstract class DbContextFromJson : TestWithSqlite { 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} + :: current Dir:{Directory.GetCurrentDirectory()}"); } var fileData = File.ReadAllText(filePath); var allData = JObject.Parse(fileData); var data = allData["repoAccounts"]; testSet.RepoAccounts = data.ToObject>(); var assignments = allData["auctionAssignments"]; testSet.AuctionAssignments = assignments.ToObject>(); var eventAlerts = allData["eventAlerts"]; testSet.EventAlerts = eventAlerts.ToObject>(); var repoManualStatus = allData["repoManualStatus"]; testSet.ManualStatuses = repoManualStatus.ToObject>(); return testSet; } public static T GetTestObjectFromFile(string filePath) { // 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} + :: current Dir:{Directory.GetCurrentDirectory()}"); } var fileData = File.ReadAllText(filePath); var data = JObject.Parse(fileData); return data.ToObject(); } public AppDbContext GetContextWithData(string jsonFileName) { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .EnableSensitiveDataLogging(true) .Options; var context = new AppDbContext(options); var testData = GetTestAccounts(jsonFileName); context.RepoAccounts.AddRange(testData.RepoAccounts); context.AuctionAssignments.AddRange(testData.AuctionAssignments); context.EventAlerts.AddRange(testData.EventAlerts); context.RepoManualStatuses.AddRange(testData.ManualStatuses); context.SaveChanges(); return context; } public AppDbContext GetSqliteContextWithData(string jsonFileName) { var context = SqliteDbContext; var testData = GetTestAccounts(jsonFileName); context.RepoAccounts.AddRange(testData.RepoAccounts); context.AuctionAssignments.AddRange(testData.AuctionAssignments); context.EventAlerts.AddRange(testData.EventAlerts); context.RepoManualStatuses.AddRange(testData.ManualStatuses); context.SaveChanges(); return context; } } }