-
Notifications
You must be signed in to change notification settings - Fork 7
/
ConvertibleInterceptorTests.cs
114 lines (106 loc) · 4.21 KB
/
ConvertibleInterceptorTests.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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 QueryConvertInterceptorTests
{
private class CustomerModel
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
}
private class MockQueryConvertInterceptor : IQueryConvertInterceptor
{
public object InterceptResultTo(object entity)
{
var customer = entity as Customer;
var personModel = new CustomerModel
{
Id = customer.Id,
FirstName = customer.FirstName,
LastName = customer.LastName
};
return personModel;
}
}
private class MockQueryConvertGenericInterceptor :
IQueryConvertInterceptor<Customer>,
IQueryConvertInterceptor<Order>
{
public object InterceptResultTo(Customer entity)
{
var customer = entity;
var personModel = new CustomerModel
{
Id = customer.Id,
FirstName = customer.FirstName,
LastName = customer.LastName
};
return personModel;
}
public object InterceptResultTo(Order entity)
{
// leave the throw, its on purpose to match the type testing.
throw new NotImplementedException();
}
}
private class MockQueryConvertGenericInterceptor2 :
IQueryConvertInterceptor<Customer, CustomerModel>
{
public CustomerModel InterceptResultTo(Customer entity)
{
var customer = entity;
var personModel = new CustomerModel
{
Id = customer.Id,
FirstName = customer.FirstName,
LastName = customer.LastName
};
return personModel;
}
}
[Fact]
public void NonGeneric()
{
MockContextFactory.SeedAndTestContextFor("QueryConvertInterceptorTests_NonGeneric", TestSeeders.SimpleSeedScenario, ctx =>
{
var criteria = new QueryCriteria();
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
queryHandler.AddInterceptor(new MockQueryConvertInterceptor());
var result = queryHandler.Execute<Customer, CustomerModel>(ctx.Customers, criteria);
Assert.All(result.Data, t => Assert.IsType<CustomerModel>(t));
});
}
[Fact]
public void Generic()
{
MockContextFactory.SeedAndTestContextFor("ConvertibleIntereceptorTests_Generic", TestSeeders.SimpleSeedScenario, ctx =>
{
var criteria = new QueryCriteria();
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
queryHandler.AddInterceptor(new MockQueryConvertGenericInterceptor());
var result = queryHandler.Execute<Customer, CustomerModel>(ctx.Customers, criteria);
Assert.All(result.Data, t => Assert.IsType<CustomerModel>(t));
});
}
[Fact]
public void Generic2()
{
MockContextFactory.SeedAndTestContextFor("ConvertibleIntereceptorTests_Generic2", TestSeeders.SimpleSeedScenario, ctx =>
{
var criteria = new QueryCriteria();
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
queryHandler.AddInterceptor(new MockQueryConvertGenericInterceptor2());
var result = queryHandler.Execute<Customer, CustomerModel>(ctx.Customers, criteria);
Assert.All(result.Data, t => Assert.IsType<CustomerModel>(t));
});
}
}
}