Files
monorepo/LCU.Api.Internal.DataCleanUp/LCU.Api.Internal.DataCleanUpTest/Repositories/DbContextFromJson.cs
T
2026-03-13 09:28:55 -05:00

71 lines
2.0 KiB
C#

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