Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #31 #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions MicroRuleEngine.Core.Tests/ExampleUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MicroRuleEngine.Core.Tests.Models;
using Newtonsoft.Json;

namespace MicroRuleEngine.Tests
{
Expand Down Expand Up @@ -245,6 +246,11 @@ public void RegexIsMatch()//Had to add a Regex evaluator to make it feel 'Comple
Assert.IsFalse(passes);
}

public class OrderParent
{
public Order PlacedOrder { get; set; }
}

public static Order GetOrder()
{
Order order = new Order()
Expand Down Expand Up @@ -278,5 +284,216 @@ public static Order GetOrder()
};
return order;
}

[TestMethod]
public void EnumerableFilterAndAggregation()
{



Order order = GetOrder();

if (order.Items.Where(x => x.ItemCode.StartsWith("M"))
.Sum(x => x.Cost) > 6)
{

}


Rule rule = new Rule
{
MemberName = "Items",
EnumerableFilter = new Rule
{
MemberName = "ItemCode",
Operator = "StartsWith",
Inputs = new []{"M"}
},
EnumerableValueExpression = new Selector
{
MemberName = "Cost",
Operator = "Sum"
},
Operator = "GreaterThan",
TargetValue = 5
};

MRE engine = new MRE();
var compiledRule = engine.CompileRule<Order>(rule);
bool passes = compiledRule(order);
Assert.IsTrue(passes);

order.Items[0].Cost = 4m;
passes = compiledRule(order);
Assert.IsFalse(passes);
}

[TestMethod]
public void CountAggregation()
{

Order order = GetOrder();

Rule rule = new Rule
{
MemberName = "Items",
EnumerableValueExpression = new Selector
{
Operator = "Count"
},
Operator = "GreaterThan",
TargetValue = 3
};

MRE engine = new MRE();
var compiledRule = engine.CompileRule<Order>(rule);
bool passes = compiledRule(order);
Assert.IsFalse(passes);

order.Items.Add(new Item());
order.Items.Add(new Item());

passes = compiledRule(order);

Assert.IsTrue(passes);

}

[TestMethod]
public void EnumerableAggregationOnChild()
{



Order order = GetOrder();

var orderParent = new OrderParent() {PlacedOrder = order};


Rule rule = new Rule
{
MemberName = "PlacedOrder.Items",
EnumerableValueExpression = new Selector
{
Operator = "Count"
},
Operator = "GreaterThan",
TargetValue = 3
};

MRE engine = new MRE();
var compiledRule = engine.CompileRule<OrderParent>(rule);
bool passes = compiledRule(orderParent);
Assert.IsFalse(passes);

order.Items.Add(new Item());
order.Items.Add(new Item());

passes = compiledRule(orderParent);

Assert.IsTrue(passes);
}

[TestMethod]
public void SerializeThenDeserialize()
{



Order order = GetOrder();

var orderParent = new OrderParent() {PlacedOrder = order};


Rule rule = new Rule
{
MemberName = "PlacedOrder.Items",
EnumerableValueExpression = new Selector
{
Operator = "Count"
},
Operator = "GreaterThan",
TargetValue = 3
};

var jsonString = JsonConvert.SerializeObject(rule);

var deserializedRule = JsonConvert.DeserializeObject<Rule>(jsonString);



MRE engine = new MRE();
var compiledRule = engine.CompileRule<OrderParent>(deserializedRule);
bool passes = compiledRule(orderParent);
Assert.IsFalse(passes);

order.Items.Add(new Item());
order.Items.Add(new Item());

passes = compiledRule(orderParent);

Assert.IsTrue(passes);
}

[TestMethod]
public void SerializeThenDeserializeComplexRules()
{



Order order = GetOrder();

var orderParent = new OrderParent() {PlacedOrder = order};


Rule rule = new Rule
{
Operator = "AndAlso",
Rules = new List<Rule>
{

new Rule
{
MemberName = "PlacedOrder.Items",
EnumerableValueExpression = new Selector
{
Operator = "Count"
},
Operator = "GreaterThan",
TargetValue = 3
},

new Rule
{
MemberName = "PlacedOrder.Items",
EnumerableValueExpression = new Selector
{
MemberName = "Cost",
Operator = "Sum"
},
Operator = "GreaterThan",
TargetValue = 5
}
}
};

var jsonString = JsonConvert.SerializeObject(rule);

var deserializedRule = JsonConvert.DeserializeObject<Rule>(jsonString);



MRE engine = new MRE();
var compiledRule = engine.CompileRule<OrderParent>(deserializedRule);
bool passes = compiledRule(orderParent);
Assert.IsFalse(passes);

order.Items.Add(new Item());
order.Items.Add(new Item());

passes = compiledRule(orderParent);

Assert.IsTrue(passes);
}
}
}
Loading