Files
2026-04-20 14:47:24 -05:00

105 lines
3.9 KiB
C#

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<RepoAccount> RepoAccounts { get; set; }
public IReadOnlyCollection<AuctionAssignment> AuctionAssignments { get; set; }
public IReadOnlyCollection<EventAlert> EventAlerts { get; set; }
public IReadOnlyCollection<RepoManualStatus> 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<List<RepoAccount>>();
var assignments = allData["auctionAssignments"];
testSet.AuctionAssignments = assignments.ToObject<List<AuctionAssignment>>();
var eventAlerts = allData["eventAlerts"];
testSet.EventAlerts = eventAlerts.ToObject<List<EventAlert>>();
var repoManualStatus = allData["repoManualStatus"];
testSet.ManualStatuses = repoManualStatus.ToObject<List<RepoManualStatus>>();
return testSet;
}
public static T GetTestObjectFromFile<T>(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<T>();
}
public AppDbContext GetContextWithData(string jsonFileName)
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.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;
}
}
}