145 lines
4.2 KiB
C#
145 lines
4.2 KiB
C#
using Strata.SqlTools.Rules.Rule;
|
|
using Strata.SqlTools.Rules.Rule.Expression;
|
|
using ExpressionAnd = Strata.SqlTools.Rules.Rule.Expression.And;
|
|
using ExpressionOr = Strata.SqlTools.Rules.Rule.Expression.Or;
|
|
using GroupAnd = Strata.SqlTools.Rules.Rule.Groups.And;
|
|
|
|
namespace Strata.SqlTools.Rules.Tests;
|
|
|
|
[TestFixture]
|
|
public class VisitorTests
|
|
{
|
|
[Test]
|
|
public void Test()
|
|
{
|
|
|
|
var eqExpr = new Equal(1, 3);
|
|
var grExpr = new GreaterThan(20, 19);
|
|
|
|
var orExpr = new ExpressionOr(eqExpr, grExpr);
|
|
|
|
var propCharges = new Property("input", "TotalCharges");
|
|
var grChargesExpr = new GreaterThan(propCharges, 100.0m);
|
|
|
|
var propName = new Property("input", "DepartmentName");
|
|
var eqNameExpr = new Equal(propName, new StringLiteral("foobar"));
|
|
|
|
orExpr = new ExpressionOr(orExpr, grChargesExpr);
|
|
var andExpr = new ExpressionAnd(orExpr, eqNameExpr);
|
|
|
|
var visitor = new TestModifierVisitor();
|
|
var modified = visitor.Visit(andExpr);
|
|
var writer = new LocalVisitor();
|
|
|
|
var output = writer.Visit(modified);
|
|
|
|
TestContext.WriteLine(output);
|
|
Assert.That(modified, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Test2()
|
|
{
|
|
System.Linq.Expressions.Expression<Func<string[], bool>> any = items => items.Any(s => s.Length >= 4);
|
|
|
|
TestContext.WriteLine(any);
|
|
|
|
var ruleSet = CreateRuleSet();
|
|
|
|
var visitor = new LocalVisitor();
|
|
var condition = visitor.Visit(ruleSet.Expression);
|
|
TestContext.WriteLine(condition);
|
|
Assert.That(condition, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Test3()
|
|
{
|
|
var input = new TestRuleInput()
|
|
{
|
|
DepartmentId = 3,
|
|
PatientId = 5,
|
|
Details = new TestRuleDetail[]
|
|
{
|
|
new() { CPTCode = "123", UBRevCode = "0999" },
|
|
new() { CPTCode = "456", UBRevCode = "0250" }
|
|
}
|
|
};
|
|
|
|
var ruleSet = CreateWithRuleSet();
|
|
|
|
var rulesEngine = new RuleSetEngine();
|
|
var success = await rulesEngine.RunRules(ruleSet, input);
|
|
|
|
var visitor = new LocalVisitor();
|
|
var condition = visitor.Visit(ruleSet.Expression);
|
|
TestContext.WriteLine(condition);
|
|
|
|
Assert.That(success, Is.True);
|
|
}
|
|
|
|
private static RuleSet CreateRuleSet()
|
|
{
|
|
var parameter = new Parameter("input");
|
|
var collectionProperty = parameter.CollectionProperty("Details");
|
|
|
|
var any = collectionProperty.Any(p => p.Property("CPTCode") == "123");
|
|
|
|
var rootGroup = new GroupAnd(new IRule[]
|
|
{
|
|
new SingleRule("R1", new Equal(parameter.Property("DepartmentId"), 3)),
|
|
new SingleRule("R2", new Equal(parameter.Property("PatientId"), 5)),
|
|
new SingleRule("R3", any)
|
|
});
|
|
|
|
return new RuleSet(rootGroup, Guid.NewGuid());
|
|
}
|
|
|
|
private static RuleSet CreateWithRuleSet()
|
|
{
|
|
var parameter = new Parameter("input");
|
|
var details = parameter.CollectionProperty("Details");
|
|
var any = details.Any(p => p.Property("CPTCode") == "123" & p.Property("UBRevCode") == "0999");
|
|
|
|
var rootGroup = new GroupAnd(new IRule[]
|
|
{
|
|
//new SingleRule("R1", new Equal(new Property(details, "UBRevCode"), "0999")),
|
|
//new SingleRule("R2", new Equal(new Property(details, "CPTCode"), "123"))
|
|
new SingleRule("R1", any)
|
|
});
|
|
|
|
return new RuleSet(rootGroup, Guid.NewGuid());
|
|
}
|
|
}
|
|
|
|
public class TestModifierVisitor : Visitor
|
|
{
|
|
public override Strata.SqlTools.Rules.Rule.Expression.Expression Visit(Strata.SqlTools.Rules.Rule.Expression.IVisitable expression)
|
|
{
|
|
if (expression is NumberLiteral num)
|
|
{
|
|
var modified = new NumberLiteral(num + 100);
|
|
return base.VisitLiteral(modified);
|
|
}
|
|
|
|
return base.Visit(expression);
|
|
}
|
|
}
|
|
|
|
public class TestRuleInput
|
|
{
|
|
public int DepartmentId { get; set; }
|
|
public int PatientId { get; set; }
|
|
|
|
public string? ImGoingToMakeThisNull { get; set; } = null;
|
|
|
|
public TestRuleDetail[] Details { get; set; } = { };
|
|
}
|
|
|
|
public class TestRuleDetail
|
|
{
|
|
public string? CPTCode { get; set; }
|
|
public string? UBRevCode { get; set; }
|
|
}
|
|
|