dotnet-dynamic-query/PoweredSoft.DynamicQuery.Test/FilterTests.cs

135 lines
4.9 KiB
C#
Raw Normal View History

2018-10-22 22:05:23 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PoweredSoft.DynamicQuery.Core;
using PoweredSoft.DynamicQuery.Test.Mock;
using Xunit;
2018-10-22 22:43:35 -04:00
using Xunit.Sdk;
2018-10-22 22:05:23 -04:00
namespace PoweredSoft.DynamicQuery.Test
{
public class FilterTests
{
2018-10-22 22:10:41 -04:00
private class MockIsChuckFilter : ISimpleFilter
{
public bool? And { get; set; } = false;
public FilterType Type { get; set; } = FilterType.Equal;
public string Path { get; set; } = "FirstName";
public object Value { get; set; } = "Chuck";
public bool? Not { get; set; }
2018-10-22 22:10:41 -04:00
}
[Fact]
public void TestInversionOfControl()
{
MockContextFactory.SeedAndTestContextFor("FilterTests_TestInversionOfControl", TestSeeders.SimpleSeedScenario, ctx =>
{
var resultShouldMatch = ctx.Customers.Where(t => t.FirstName == "Chuck").ToList();
var criteria = new QueryCriteria()
{
Filters = new List<IFilter> { new MockIsChuckFilter() }
};
var queryHandler = new QueryHandler();
var result = queryHandler.Execute(ctx.Customers, criteria);
Assert.Equal(resultShouldMatch, result.Data);
});
}
2018-10-22 22:05:23 -04:00
[Fact]
public void SimpleFilter()
{
MockContextFactory.SeedAndTestContextFor("FilterTests_SimpleFilter", TestSeeders.SimpleSeedScenario, ctx =>
{
var resultShouldMatch = ctx.Items.Where(t => t.Name.EndsWith("Cables")).ToList();
var criteria = new QueryCriteria()
{
Filters = new List<IFilter>
{
new SimpleFilter
{
Path = "Name",
Type = FilterType.EndsWith,
Value = "Cables"
}
}
};
var queryHandler = new QueryHandler();
var result = queryHandler.Execute(ctx.Items, criteria);
Assert.Equal(resultShouldMatch, result.Data);
});
}
2018-10-22 22:10:41 -04:00
2018-10-22 22:05:23 -04:00
[Fact]
2018-10-22 22:10:41 -04:00
public void CompositeFilter()
2018-10-22 22:05:23 -04:00
{
2018-10-22 22:10:41 -04:00
MockContextFactory.SeedAndTestContextFor("FilterTests_CompositeFilter", TestSeeders.SimpleSeedScenario, ctx =>
2018-10-22 22:05:23 -04:00
{
2018-10-22 22:10:41 -04:00
var resultShouldMatch = ctx.Customers.Where(t => t.FirstName == "John" || t.LastName == "Norris").ToList();
2018-10-22 22:05:23 -04:00
var criteria = new QueryCriteria()
{
2018-10-22 22:10:41 -04:00
Filters = new List<IFilter>
{
new CompositeFilter()
{
Type = FilterType.Composite,
Filters = new List<IFilter>
{
new SimpleFilter() { Path = "FirstName", Type = FilterType.Equal, Value = "John" },
new SimpleFilter() { Path = "LastName", Type = FilterType.Equal, Value = "Norris"}
}
}
}
2018-10-22 22:05:23 -04:00
};
var queryHandler = new QueryHandler();
var result = queryHandler.Execute(ctx.Customers, criteria);
Assert.Equal(resultShouldMatch, result.Data);
});
}
2018-10-22 23:47:39 -04:00
[Fact]
public void MoreComplexCompositeFilter()
{
MockContextFactory.SeedAndTestContextFor("FilterTests_MoreComplexCompositeFilter", TestSeeders.SimpleSeedScenario, ctx =>
{
var resultShouldMatch = ctx.Customers.Where(t => (t.FirstName == "John" || t.LastName == "Norris") && t.FirstName.Contains("o")).ToList();
var criteria = new QueryCriteria()
{
Filters = new List<IFilter>
{
new CompositeFilter()
{
Type = FilterType.Composite,
Filters = new List<IFilter>
{
new SimpleFilter() { Path = "FirstName", Type = FilterType.Equal, Value = "John" },
new SimpleFilter() { Path = "LastName", Type = FilterType.Equal, Value = "Norris"}
}
},
new SimpleFilter()
{
And = true,
Path = "FirstName",
Type = FilterType.Contains,
Value = "o"
}
}
};
var queryHandler = new QueryHandler();
var result = queryHandler.Execute(ctx.Customers, criteria);
Assert.Equal(resultShouldMatch, result.Data);
});
}
2018-10-22 22:05:23 -04:00
}
}