forked from PoweredSoft/DynamicQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeforeFilterTests.cs
88 lines (75 loc) · 3.32 KB
/
BeforeFilterTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using PoweredSoft.DynamicQuery.Core;
using PoweredSoft.DynamicQuery.Test.Mock;
using PoweredSoft.DynamicLinq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
namespace PoweredSoft.DynamicQuery.Test
{
public class BeforeFilterTests
{
private class MockBeforeQueryFilterInterceptor : IBeforeQueryFilterInterceptor
{
public IQueryable InterceptBeforeFiltering(IQueryCriteria criteria, IQueryable queryable)
{
return queryable.Where(t => t.Equal("Customer.FirstName", "David"));
}
}
private class MockBeforeQueryFilterGenericInterceptor :
IBeforeQueryFilterInterceptor<Order>,
IBeforeQueryFilterInterceptor<Customer>
{
public IQueryable<Order> InterceptBeforeFiltering(IQueryCriteria criteria, IQueryable<Order> queryable)
{
return queryable.Where(t => t.Customer.FirstName == "David");
}
public IQueryable<Customer> InterceptBeforeFiltering(IQueryCriteria criteria, IQueryable<Customer> queryable)
{
// leave throw it validates the test, if it gets in here it shoulnd't
throw new NotImplementedException();
}
}
[Fact]
public void NonGeneric()
{
MockContextFactory.SeedAndTestContextFor("BeforeFilterTests_NonGeneric", TestSeeders.SimpleSeedScenario, ctx =>
{
var criteria = new QueryCriteria();
var interceptor = new MockBeforeQueryFilterInterceptor();
// queryable of orders.
var queryable = ctx.Orders.AsQueryable();
// pass into the interceptor.
queryable = (IQueryable<Order>)interceptor.InterceptBeforeFiltering(criteria, queryable);
// query handler should pass by the same interceptor.
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
queryHandler.AddInterceptor(interceptor);
var result = queryHandler.Execute(ctx.Orders, criteria);
// compare results.
var expected = queryable.ToList();
Assert.Equal(expected, result.Data);
});
}
[Fact]
public void Generic()
{
MockContextFactory.SeedAndTestContextFor("BeforeFilterTests_Generic", TestSeeders.SimpleSeedScenario, ctx =>
{
var criteria = new QueryCriteria();
var interceptor = new MockBeforeQueryFilterGenericInterceptor();
// queryable of orders.
var queryable = ctx.Orders.AsQueryable();
// pass into the interceptor.
queryable = interceptor.InterceptBeforeFiltering(criteria, queryable);
// query handler should pass by the same interceptor.
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
queryHandler.AddInterceptor(interceptor);
var result = queryHandler.Execute(ctx.Orders, criteria);
// compare results.
var expected = queryable.ToList();
Assert.Equal(expected, result.Data);
});
}
}
}