Files
2026-03-11 13:21:47 -05:00

71 lines
2.2 KiB
C#

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<LoggingMatrix> 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<List<LoggingMatrix>>();
return testSet;
}
public AppDbContext GetBlankContext()
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
return new AppDbContext(options);
}
public AppDbContext GetContextWithData(string jsonFileName)
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.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<T>(DbSet<T> collection, IEnumerable<T> range) where T : class
{
if (range != null) collection.AddRange(range);
}
}
}