test(tenpayv3): 适配新的模型测试

This commit is contained in:
Fu Diwei
2021-05-28 19:08:30 +08:00
parent 782d46cad9
commit e931d90365
19 changed files with 20 additions and 15 deletions

View File

@@ -14,7 +14,7 @@
<Content Include="ModelSamples/**/*.json"> <Content Include="ModelSamples/**/*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="ResourceSamples/**/*.json"> <Content Include="EventSamples/**/*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>

View File

@@ -39,21 +39,21 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
class JsonStringTypedStringListOrArrayTestEntity class JsonStringTypedStringListOrArrayTestEntity
{ {
[Newtonsoft.Json.JsonProperty("string_ilist")] [Newtonsoft.Json.JsonProperty("string_ilist")]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringTypedStringIListConverter))] [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.JsonTypedStringIListConverter))]
[System.Text.Json.Serialization.JsonPropertyName("string_ilist")] [System.Text.Json.Serialization.JsonPropertyName("string_ilist")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.StringTypedStringIListConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.JsonTypedStringIListConverter))]
public IList<string> StringTypedIList { get; set; } public IList<string> StringTypedIList { get; set; }
[Newtonsoft.Json.JsonProperty("string_list")] [Newtonsoft.Json.JsonProperty("string_list")]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringTypedStringListConverter))] [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.JsonTypedStringListConverter))]
[System.Text.Json.Serialization.JsonPropertyName("string_list")] [System.Text.Json.Serialization.JsonPropertyName("string_list")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.StringTypedStringListConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.JsonTypedStringListConverter))]
public List<string> StringTypedList { get; set; } public List<string> StringTypedList { get; set; }
[Newtonsoft.Json.JsonProperty("string_array")] [Newtonsoft.Json.JsonProperty("string_array")]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringTypedStringArrayConverter))] [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.JsonTypedStringArrayConverter))]
[System.Text.Json.Serialization.JsonPropertyName("string_array")] [System.Text.Json.Serialization.JsonPropertyName("string_array")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.StringTypedStringArrayConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.JsonTypedStringArrayConverter))]
public string[] StringTypedArray { get; set; } public string[] StringTypedArray { get; set; }
} }

View File

@@ -16,7 +16,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
[Fact(DisplayName = "验证模型定义")] [Fact(DisplayName = "验证模型定义")]
public void ModelDefinitionsTest() public void ModelDefinitionsTest()
{ {
static void SetPropertiesValueRecursively(object obj) static void TrySetPropertiesValueRecursively(object obj)
{ {
var lstProperty = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var lstProperty = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var tProperty in lstProperty) foreach (var tProperty in lstProperty)
@@ -24,6 +24,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
if (tProperty.SetMethod == null || !tProperty.SetMethod.IsPublic) if (tProperty.SetMethod == null || !tProperty.SetMethod.IsPublic)
continue; continue;
var newtonsoftJsonAttribute = tProperty.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>();
var systemTextJsonAttribute = tProperty.GetCustomAttribute<System.Text.Json.Serialization.JsonPropertyNameAttribute>();
if (newtonsoftJsonAttribute?.PropertyName != systemTextJsonAttribute?.Name)
throw new Exception($"`{obj.GetType().Name}` fields mismatching: `{newtonsoftJsonAttribute.PropertyName}` & `{systemTextJsonAttribute.Name}`");
if (tProperty.PropertyType.IsPrimitive) if (tProperty.PropertyType.IsPrimitive)
{ {
// noop // noop
@@ -33,7 +38,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
Type tEl = tProperty.PropertyType.Assembly.GetType(tProperty.PropertyType.FullName.Replace("[]", string.Empty)); Type tEl = tProperty.PropertyType.Assembly.GetType(tProperty.PropertyType.FullName.Replace("[]", string.Empty));
object propEl = (tEl == typeof(string)) ? string.Empty : Activator.CreateInstance(tEl); object propEl = (tEl == typeof(string)) ? string.Empty : Activator.CreateInstance(tEl);
propEl = Convert.ChangeType(propEl, tEl); propEl = Convert.ChangeType(propEl, tEl);
SetPropertiesValueRecursively(propEl); TrySetPropertiesValueRecursively(propEl);
Array prop = Array.CreateInstance(tEl, 1); Array prop = Array.CreateInstance(tEl, 1);
prop.SetValue(propEl, 0); prop.SetValue(propEl, 0);
@@ -60,7 +65,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
Type tGeneric = tProperty.PropertyType.GetGenericArguments().Single(); Type tGeneric = tProperty.PropertyType.GetGenericArguments().Single();
object propEl = (tGeneric == typeof(string)) ? string.Empty : Activator.CreateInstance(tGeneric); object propEl = (tGeneric == typeof(string)) ? string.Empty : Activator.CreateInstance(tGeneric);
propEl = Convert.ChangeType(propEl, tGeneric); propEl = Convert.ChangeType(propEl, tGeneric);
SetPropertiesValueRecursively(propEl); TrySetPropertiesValueRecursively(propEl);
Type tList = typeof(List<>).MakeGenericType(new Type[] { tGeneric }); Type tList = typeof(List<>).MakeGenericType(new Type[] { tGeneric });
object prop = Activator.CreateInstance(tList); object prop = Activator.CreateInstance(tList);
@@ -72,7 +77,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
else else
{ {
object prop = Activator.CreateInstance(tProperty.PropertyType); object prop = Activator.CreateInstance(tProperty.PropertyType);
SetPropertiesValueRecursively(prop); TrySetPropertiesValueRecursively(prop);
tProperty.SetValue(obj, prop); tProperty.SetValue(obj, prop);
} }
@@ -82,7 +87,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
var lstModel = _assembly.GetTypes() var lstModel = _assembly.GetTypes()
.Where(e => .Where(e =>
e.Namespace != null && e.Namespace != null &&
e.Namespace.Equals(_assembly.GetName().Name + "Models") && e.Namespace.Equals(_assembly.GetName().Name + ".Models") &&
e.IsClass && e.IsClass &&
!e.IsAbstract && !e.IsAbstract &&
!e.IsInterface && !e.IsInterface &&
@@ -137,7 +142,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
try try
{ {
object instance = _assembly.CreateInstance(tModel.Namespace + "." + tModel.Name); object instance = _assembly.CreateInstance(tModel.Namespace + "." + tModel.Name);
SetPropertiesValueRecursively(instance); TrySetPropertiesValueRecursively(instance);
new FlurlNewtonsoftJsonSerializer().Serialize(instance); new FlurlNewtonsoftJsonSerializer().Serialize(instance);
new FlurlSystemTextJsonSerializer().Serialize(instance); new FlurlSystemTextJsonSerializer().Serialize(instance);
@@ -290,7 +295,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
} }
VerifyJsonSamples("ModelSamples", "Models"); VerifyJsonSamples("ModelSamples", "Models");
VerifyJsonSamples("ResourceSamples", "Resources"); VerifyJsonSamples("EventSamples", "Events");
Assert.Empty(exceptions); Assert.Empty(exceptions);
} }

View File

@@ -22,7 +22,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
{ {
Assert.NotNull(response.ActivityList.First().ActivityId); Assert.NotNull(response.ActivityList.First().ActivityId);
Assert.NotNull(response.ActivityList.First().ActivityStatus); Assert.NotNull(response.ActivityList.First().ActivityStatus);
Assert.NotNull(response.ActivityList.First().ActivityBaseInformation); Assert.NotNull(response.ActivityList.First().ActivityBase);
Assert.NotNull(response.ActivityList.First().AwardSendRule); Assert.NotNull(response.ActivityList.First().AwardSendRule);
} }
} }