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

76 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using LCU.API.Collections.CollectionsApiLib.Models;
using LCU.Shared.Collections.RepoApiLib.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit.Sdk;
namespace LCU.API.Collections.RepoApiTests
{
public class RepoUpdateTestRow
{
public TemenosAccount CollectionAccount { get; set; }
public TemenosCase CollectionCase { get; set; }
public RepoAccount ExistingRepoAccount { get; set; }
public RepoAccount NewRepoAccount { get; set; }
}
public class CollectionsJsonFileDataAttribute : DataAttribute
{
private readonly string _filePath;
private readonly string _propertyName;
/// <summary>
/// Load data from a JSON file as the data source for a theory
/// </summary>
/// <param name="filePath">The absolute or relative path to the JSON file to load</param>
public CollectionsJsonFileDataAttribute(string filePath)
: this(filePath, null) { }
/// <summary>
/// Load data from a JSON file as the data source for a theory
/// </summary>
/// <param name="filePath">The absolute or relative path to the JSON file to load</param>
/// <param name="propertyName">The name of the property on the JSON file that contains the data for the test</param>
public CollectionsJsonFileDataAttribute(string filePath, string propertyName)
{
_filePath = filePath;
_propertyName = propertyName;
}
/// <inheritDoc />
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
if (testMethod == null) { throw new ArgumentNullException(nameof(testMethod)); }
// 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}");
}
// Load the file
var fileData = File.ReadAllText(_filePath);
if (string.IsNullOrEmpty(_propertyName))
{
//whole file is the data
return JsonConvert.DeserializeObject<List<RepoUpdateTestRow[]>>(fileData);
}
// Only use the specified property as the data
var allData = JObject.Parse(fileData);
var data = allData[_propertyName];
return data.ToObject<List<RepoUpdateTestRow[]>>();
}
}
}