chore: initial git load of code space
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using Strata.SqlTools.SqlBreakdown.Expressions;
|
||||
using LinqExpression = System.Linq.Expressions.Expression;
|
||||
|
||||
namespace Strata.SqlTools.Rules.Tests;
|
||||
|
||||
public class Tests
|
||||
{
|
||||
private static readonly Guid _pesDataTableGUID = new Guid("41639c8f-fecf-4449-b6e6-53f796c0c3e4");
|
||||
private const string EncounterIDKey = "DimPatientEnEncounterID";
|
||||
|
||||
public Dictionary<long, IHierarchicalData>? _encounterLookup;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetup()
|
||||
{
|
||||
var allData = JsonSerializer.Deserialize<List<IHierarchicalData>>(File.ReadAllText(@"Data.json"), new JsonSerializerOptions
|
||||
{
|
||||
Converters =
|
||||
{
|
||||
new HierarchicalDataConverter(),
|
||||
new FlatDataConverter(),
|
||||
new ObjectToInferredTypesConverter()
|
||||
}
|
||||
});
|
||||
|
||||
if (allData == null || allData.Count == 0)
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
var rootDatas = allData.Where(x => x.DataSourceGuid == _pesDataTableGUID).ToList();
|
||||
|
||||
foreach (var rootData in rootDatas)
|
||||
{
|
||||
var encounterID = (long)rootData.Data[EncounterIDKey];
|
||||
var childData = allData.Where(x => (long)x.Data[EncounterIDKey] == encounterID && x.DataSourceGuid != rootData.DataSourceGuid);
|
||||
rootData.AllChildData = childData;
|
||||
}
|
||||
|
||||
_encounterLookup = rootDatas.ToDictionary(x => (long)x.Data[EncounterIDKey]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test3()
|
||||
{
|
||||
var dictionary = new Dictionary<string, object>();
|
||||
dictionary["key1"] = "value1";
|
||||
dictionary["RowID"] = 123;
|
||||
|
||||
var dictionaryParam = LinqExpression.Parameter(typeof(Dictionary<string, object>));
|
||||
|
||||
var indexProperty = GetIndexProperty(dictionary.GetType());
|
||||
|
||||
var indexerExpr = LinqExpression.MakeIndex(dictionaryParam, indexProperty, new[] { LinqExpression.Constant("key1") });
|
||||
|
||||
var lambda = LinqExpression.Lambda<Func<Dictionary<string, object>, object>>(indexerExpr, dictionaryParam).Compile();
|
||||
|
||||
var result = lambda(dictionary);
|
||||
|
||||
Assert.That(result, Is.EqualTo("value1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test4()
|
||||
{
|
||||
if (_encounterLookup == null || _encounterLookup.Count == 0)
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
IHierarchicalData encounter = _encounterLookup.First().Value;
|
||||
|
||||
var inputProperty = new InputPropertyExpression(Guid.Parse("41639c8f-fecf-4449-b6e6-53f796c0c3e4"), "DischargeDateTime");
|
||||
|
||||
var func = GetDataExpression(inputProperty);
|
||||
LambdaExpression lambda = func;
|
||||
var compiledLambda = lambda.Compile();
|
||||
compiledLambda.DynamicInvoke(encounter);
|
||||
}
|
||||
|
||||
private static Expression<Func<IHierarchicalData, object>> GetDataExpression(InputPropertyExpression Property)
|
||||
{
|
||||
if (Property.SecondaryDataSource == null)
|
||||
{
|
||||
Expression<Func<IHierarchicalData, object>> func = hd => hd.Data[Property.DataKeyLookup];
|
||||
return func;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("should not happen");
|
||||
}
|
||||
|
||||
private static PropertyInfo GetIndexProperty(Type type)
|
||||
{
|
||||
return type.GetProperties().First(x => x.GetIndexParameters().Length > 0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test1()
|
||||
{
|
||||
// Addition is an add expression for "1 + 2"
|
||||
var one = LinqExpression.Constant(1, typeof(int));
|
||||
var two = LinqExpression.Constant(2, typeof(int));
|
||||
var addition = LinqExpression.Add(one, two);
|
||||
|
||||
var fifty = LinqExpression.Constant(50, typeof(int));
|
||||
var lessThan = LinqExpression.LessThanOrEqual(addition, fifty);
|
||||
|
||||
var expr = lessThan;
|
||||
|
||||
// Print out the expression.
|
||||
var exprString = expr.ToString();
|
||||
TestContext.Out.WriteLine(exprString);
|
||||
|
||||
var lambda = LinqExpression.Lambda<Func<bool>>(expr).Compile();
|
||||
|
||||
var result = lambda();
|
||||
Assert.That(result, Is.True);
|
||||
}
|
||||
|
||||
[TestCase("01", ExpectedResult = true)]
|
||||
[TestCase("012C", ExpectedResult = true)]
|
||||
[TestCase("012c", ExpectedResult = true)]
|
||||
[TestCase("oasd", ExpectedResult = false)]
|
||||
public bool Test2(string value)
|
||||
{
|
||||
var foobaz = new Foobaz
|
||||
{
|
||||
Properties =
|
||||
{
|
||||
{ "FirstName", "Bob" }
|
||||
}
|
||||
};
|
||||
|
||||
var param = LinqExpression.Parameter(typeof(Foobaz), "input");
|
||||
var memberExpression = LinqExpression.Property(param, "DepartmentCode");
|
||||
|
||||
var startsWithCall = StartsWith(memberExpression, value);
|
||||
|
||||
Expression<Func<Foobaz, bool>> lambda = LinqExpression.Lambda<Func<Foobaz, bool>>(startsWithCall, param);
|
||||
|
||||
TestContext.WriteLine(lambda.ToString());
|
||||
|
||||
var result = lambda.Compile()(foobaz);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static System.Linq.Expressions.Expression StartsWith(System.Linq.Expressions.Expression expression, string value)
|
||||
{
|
||||
return StartsWith(expression, LinqExpression.Constant(value));
|
||||
}
|
||||
|
||||
static MethodCallExpression StartsWith(System.Linq.Expressions.Expression expression, System.Linq.Expressions.Expression valueExpression)
|
||||
{
|
||||
var startWithMethodInfo = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string), typeof(StringComparison) })!;
|
||||
//"".StartsWith("", StringComparison.OrdinalIgnoreCase)
|
||||
var call = LinqExpression.Call(expression, startWithMethodInfo, valueExpression, LinqExpression.Constant(StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
return call;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class FoobarBase
|
||||
{
|
||||
public abstract string DepartmentCode { get; }
|
||||
}
|
||||
|
||||
public class Foobaz : FoobarBase
|
||||
{
|
||||
public Foobaz()
|
||||
{
|
||||
DepartmentId = DateTime.Now.Ticks;
|
||||
}
|
||||
|
||||
public override string DepartmentCode => "012C3";
|
||||
|
||||
public long DepartmentId { get; }
|
||||
|
||||
public IDictionary<string, object> Properties { get; } = new Dictionary<string, object>();
|
||||
}
|
||||
Reference in New Issue
Block a user