forked from PoweredSoft/DynamicQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIncludeStrategyTests.cs
89 lines (76 loc) · 3.3 KB
/
IncludeStrategyTests.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
89
using Microsoft.EntityFrameworkCore;
using PoweredSoft.DynamicQuery.Core;
using PoweredSoft.DynamicQuery.Test.Mock;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
namespace PoweredSoft.DynamicQuery.Test
{
public class IncludeStrategyTests
{
private class MockIncludeStrategyInterceptor : IIncludeStrategyInterceptor
{
public IQueryable InterceptIncludeStrategy(IQueryCriteria criteria, IQueryable queryable)
{
queryable = ((IQueryable<Order>)queryable).Include(t => t.Customer);
return queryable;
}
}
private class MockIncludeStrategyGenericInterceptor :
IIncludeStrategyInterceptor<Order>,
IIncludeStrategyInterceptor<Customer>
{
public IQueryable<Order> InterceptIncludeStrategy(IQueryCriteria criteria, IQueryable<Order> queryable)
{
return queryable.Include(t => t.Customer);
}
public IQueryable<Customer> InterceptIncludeStrategy(IQueryCriteria criteria, IQueryable<Customer> queryable)
{
// should not go in here.
throw new NotImplementedException();
}
}
[Fact]
public void NonGeneric()
{
MockContextFactory.SeedAndTestContextFor("IncludeStrategyTests_NonGeneric", TestSeeders.SimpleSeedScenario, ctx =>
{
var criteria = new QueryCriteria();
var interceptor = new MockIncludeStrategyInterceptor();
// queryable of orders.
var queryable = ctx.Orders.AsQueryable();
// pass into the interceptor.
queryable = (IQueryable<Order>)interceptor.InterceptIncludeStrategy(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("IncludeStrategyTests_Generic", TestSeeders.SimpleSeedScenario, ctx =>
{
var criteria = new QueryCriteria();
var interceptor = new MockIncludeStrategyGenericInterceptor();
// queryable of orders.
var queryable = ctx.Orders.AsQueryable();
// pass into the interceptor.
queryable = interceptor.InterceptIncludeStrategy(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);
});
}
}
}