-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathWhenMappingPrimitiveCustomMappingRegression.cs
More file actions
147 lines (117 loc) · 5.21 KB
/
WhenMappingPrimitiveCustomMappingRegression.cs
File metadata and controls
147 lines (117 loc) · 5.21 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Mapster.Tests
{
[TestClass]
public class WhenMappingPrimitiveCustomMappingRegression
{
[TestMethod]
public void CustomMappingDateTimeToPrimitive()
{
TypeAdapterConfig<DateTime, long>
.NewConfig()
.MapWith(src => new DateTimeOffset(src).ToUnixTimeSeconds());
TypeAdapterConfig<DateTime, string>
.NewConfig()
.MapWith(src => src.ToShortDateString());
var _source = new DateTime(2023, 10, 27, 0, 0, 0, DateTimeKind.Utc);
var _resultToLong = _source.Adapt<long>();
var _resultToString = _source.Adapt<string>();
_resultToLong.ShouldBe(new DateTimeOffset(new DateTime(2023, 10, 27, 0, 0, 0, DateTimeKind.Utc)).ToUnixTimeSeconds());
_resultToString.ShouldNotBe(_source.ToString());
_resultToString.ShouldBe(_source.ToShortDateString());
}
/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/561
/// </summary>
[TestMethod]
public void MappingToPrimitiveInsiderWithCustomMapping()
{
TypeAdapterConfig<Optional561<string?>, string?>
.NewConfig()
.MapToTargetWith((source, target) => source.HasValue ? source.Value : target);
var sourceNull = new Source561 { Name = new Optional561<string?>(null) };
var target = new Source561 { Name = new Optional561<string>("John") }.Adapt<Target561>();
var TargetDestinationFromNull = new Target561() { Name = "Me" };
var NullToupdateoptional = sourceNull.AdaptToTarget(TargetDestinationFromNull);
var _result = sourceNull.AdaptToTarget(target);
target.Name.ShouldBe("John");
NullToupdateoptional.Name.ShouldBe("Me");
}
/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/407
/// </summary>
[TestMethod]
public void MappingDatetimeToLongWithCustomMapping()
{
TypeAdapterConfig<DateTime, long>
.NewConfig()
.MapWith(src => new DateTimeOffset(src).ToUnixTimeSeconds());
TypeAdapterConfig<long, DateTime>
.NewConfig()
.MapWith(src => new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(src).Date);
var emptySource = new Source407() { Time = DateTime.UtcNow.Date };
var fromC1 = new DateTime(2023, 10, 27,0,0,0,DateTimeKind.Utc);
var fromC2 = new DateTimeOffset(new DateTime(2025, 11, 23, 0, 0, 0, DateTimeKind.Utc)).ToUnixTimeSeconds();
var c1 = new Source407 { Time = fromC1 };
var c2 = new Destination407 { Time = fromC2 };
var _result = c1.Adapt<Destination407>();
var _resultLongtoDateTime = c2.Adapt<Source407>();
_result.Time.ShouldBe(new DateTimeOffset(new DateTime(2023, 10, 27, 0, 0, 0, DateTimeKind.Utc)).ToUnixTimeSeconds());
_resultLongtoDateTime.Time.ShouldBe(new DateTime(2025, 11, 23).Date);
}
/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/407
/// </summary>
[TestMethod]
public void CustomMappingPrimitiveToProjection()
{
TypeAdapterConfig<DateTime, long>
.NewConfig()
.MapWith(src => new DateTimeOffset(src).ToUnixTimeSeconds());
TypeAdapterConfig<long, DateTime>
.NewConfig()
.MapWith(src => new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(src).Date);
var _sourceList = new List<Source407>();
_sourceList.Add(new Source407 { Time = new DateTime(2023, 10, 27, 0, 0, 0, DateTimeKind.Utc) });
var _fromC2List = new List<Destination407>();
_fromC2List.Add(new Destination407 { Time = new DateTimeOffset(new DateTime(2025, 11, 23, 0, 0, 0, DateTimeKind.Utc)).ToUnixTimeSeconds() });
var _resultProjectionDateTimeTolong = _sourceList.AsQueryable().ProjectToType<Destination407>().ToList();
var _resultProjectionLongToDateTime = _fromC2List.AsQueryable().ProjectToType<Source407>().ToList();
_resultProjectionDateTimeTolong[0].Time.ShouldBe(new DateTimeOffset(new DateTime(2023, 10, 27, 0, 0, 0, DateTimeKind.Utc)).ToUnixTimeSeconds());
_resultProjectionLongToDateTime[0].Time.ShouldBe(new DateTime(2025, 11, 23).Date);
}
}
#region TestClasses
public class Source407
{
public DateTime Time { get; set; }
}
public class Destination407
{
public long Time { get; set; }
}
class Optional561<T>
{
public Optional561(T? value)
{
if (value != null)
HasValue = true;
Value = value;
}
public bool HasValue { get; }
public T? Value { get; }
}
class Source561
{
public Optional561<string?> Name { get; set; }
}
class Target561
{
public string? Name { get; set; }
}
#endregion TestClasses
}