forked from MapsterMapper/Mapster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncTest.cs
More file actions
190 lines (163 loc) · 5.11 KB
/
AsyncTest.cs
File metadata and controls
190 lines (163 loc) · 5.11 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Threading.Tasks;
using MapsterMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
namespace Mapster.Async.Tests
{
[TestClass]
public class AsyncTest
{
[ClassInitialize]
public static void Setup(TestContext context)
{
TypeAdapterConfig<Poco, Dto>.Clear();
}
[TestMethod]
public async Task Async()
{
TypeAdapterConfig<Poco, Dto>.NewConfig()
.AfterMappingAsync(async dest => { dest.Name = await GetName(); });
var poco = new Poco {Id = "foo"};
var dto = await poco.BuildAdapter().AdaptToTypeAsync<Dto>();
dto.Name.ShouldBe("bar");
}
[TestMethod]
public async Task AsyncError()
{
TypeAdapterConfig<Poco, Dto>.NewConfig()
.AfterMappingAsync(async dest => { dest.Name = await GetNameError(); });
var poco = new Poco {Id = "foo"};
try
{
var dto = await poco.BuildAdapter().AdaptToTypeAsync<Dto>();
Assert.Fail("should error");
}
catch (Exception ex)
{
ex.Message.ShouldBe("bar");
}
}
[TestMethod]
public void Sync()
{
TypeAdapterConfig<Poco, Dto>.NewConfig()
.AfterMappingAsync(async dest => { dest.Name = await GetName(); });
var poco = new Poco {Id = "foo"};
Should.Throw<InvalidOperationException>(() => poco.Adapt<Dto>());
}
[TestMethod]
public async Task NestedAsync()
{
TypeAdapterConfig<DoCar, DtoCar>.NewConfig();
TypeAdapterConfig<DoOwner, DtoOwner>.NewConfig();
TypeAdapterConfig<DoCarOwnership, DtoCarOwnership>.NewConfig()
.Ignore(dest => dest.Car)
.Ignore(dest => dest.Owner)
.AfterMappingAsync(async (src, dest) =>
{
dest.Owner = await GetOwner(src.Owner);
})
.AfterMappingAsync(async (src, dest) =>
{
dest.Car = await GetCar(src.Car);
});
var dtoOwnership = await new DoCarOwnership()
{
Id = "1",
Car = "1",
Owner = "1"
}
.BuildAdapter()
.AdaptToTypeAsync<DtoCarOwnership>();
dtoOwnership.Car.ShouldNotBeNull();
dtoOwnership.Car.Make.ShouldBe("Car Maker Inc");
dtoOwnership.Owner.ShouldNotBeNull();
dtoOwnership.Owner.Name.ShouldBe("John Doe");
}
[TestMethod]
public async Task SimplyAsync()
{
TypeAdapterConfig<Poco, Dto>.NewConfig()
.AfterMappingAsync(async dest => { dest.Name = await GetName(); });
var poco = new Poco { Id = "foo" };
var dto = await poco.AdaptAsync<Dto>();
dto.Name.ShouldBe("bar");
IMapper instance = new Mapper();
var destination = await instance.MapAsync<Dto>(poco);
destination.Name.ShouldBe("bar");
}
private static async Task<string> GetName()
{
await Task.Delay(1);
return "bar";
}
private static async Task<string> GetNameError()
{
await Task.Delay(1);
throw new Exception("bar");
}
private static async Task<DtoCar> GetCar(string id)
{
await Task.Delay(1);
return await new DoCar()
{
Id = id,
Make = "Car Maker Inc",
Model = "Generic",
}.BuildAdapter().AdaptToTypeAsync<DtoCar>();
}
private static async Task<DtoOwner> GetOwner(string id)
{
await Task.Delay(1);
return await new DoOwner()
{
Id = id,
Name = "John Doe"
}.BuildAdapter().AdaptToTypeAsync<DtoOwner>();
}
}
public class Poco
{
public string Id { get; set; }
}
public class Dto
{
public string Id { get; set; }
public string Name { get; set; }
}
public class DtoCarOwnership
{
public string Id { get; set; }
public DtoOwner Owner { get; set; }
public DtoCar Car { get; set; }
}
public class DoCarOwnership
{
public string Id { get; set; }
public string Owner { get; set; }
public string Car { get; set; }
}
public class DtoOwner
{
public string Id { get; set; }
public string Name { get; set; }
}
public class DoOwner
{
public string Id { get; set; }
public string Name { get; set; }
}
public class DtoCar
{
public string Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
public class DoCar
{
public string Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
}