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";
|
2019-10-13 15:06:47 -04:00
|
|
|
|
public bool? Not { get; set; }
|
2021-04-27 13:11:54 -04:00
|
|
|
|
public bool? CaseInsensitive { 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() }
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-12 18:37:32 -05:00
|
|
|
|
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
|
2018-10-22 22:10:41 -04:00
|
|
|
|
var result = queryHandler.Execute(ctx.Customers, criteria);
|
|
|
|
|
Assert.Equal(resultShouldMatch, result.Data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 13:11:54 -04:00
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-12 18:37:32 -05:00
|
|
|
|
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
|
2021-04-27 13:11:54 -04:00
|
|
|
|
var result = queryHandler.Execute(ctx.Items, criteria);
|
|
|
|
|
Assert.Equal(resultShouldMatch, result.Data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SimpleFilterCaseInsensitive()
|
|
|
|
|
{
|
|
|
|
|
MockContextFactory.SeedAndTestContextFor("FilterTests_SimpleFilterCaseInsensitive", TestSeeders.SimpleSeedScenario, ctx =>
|
|
|
|
|
{
|
|
|
|
|
var resultShouldMatch = ctx.Items.Where(t => t.Name.ToLower().EndsWith("Cables".ToLower())).ToList();
|
|
|
|
|
|
|
|
|
|
var criteria = new QueryCriteria()
|
|
|
|
|
{
|
|
|
|
|
Filters = new List<IFilter>
|
|
|
|
|
{
|
|
|
|
|
new SimpleFilter
|
|
|
|
|
{
|
|
|
|
|
Path = "Name",
|
|
|
|
|
Type = FilterType.EndsWith,
|
|
|
|
|
Value = "Cables",
|
|
|
|
|
CaseInsensitive = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
|
2018-10-22 22:05:23 -04:00
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
2019-12-12 18:37:32 -05:00
|
|
|
|
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
|
2018-10-22 22:05:23 -04:00
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-12 18:37:32 -05:00
|
|
|
|
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
|
2018-10-22 23:47:39 -04:00
|
|
|
|
var result = queryHandler.Execute(ctx.Customers, criteria);
|
|
|
|
|
Assert.Equal(resultShouldMatch, result.Data);
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-22 22:05:23 -04:00
|
|
|
|
}
|
|
|
|
|
}
|