您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ObjectConverterTests.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using Luticate2.Auth.Utils.Business.Converters;
  8. using Luticate2.Auth.Utils.Business.ExpressionConverter;
  9. using Luticate2.Auth.Utils.Business.Fields;
  10. using Luticate2.Auth.Utils.Business.ObjectConverter;
  11. using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
  12. using Luticate2.Auth.Utils.Dbo;
  13. using Luticate2.Auth.Utils.Dbo.Fields;
  14. using Luticate2.Auth.Utils.Dbo.Result;
  15. using Luticate2.Auth.Utils.Interfaces;
  16. using Microsoft.Extensions.DependencyInjection;
  17. using Xunit;
  18. namespace Luticate2.Auth.Tests.Business.ObjectConverter
  19. {
  20. public class ObjectConverterTests
  21. {
  22. protected IServiceProvider GetServiceProvider()
  23. {
  24. var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
  25. var typeILuObjectConverterDescriptorEnumerable =
  26. typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable));
  27. var typeILuObjectConverter = typeof(ILuObjectConverter<,>);
  28. var listTypes = new []
  29. {
  30. typeof(IEnumerable<>),
  31. typeof(ICollection<>),
  32. typeof(Collection<>),
  33. typeof(IList<>),
  34. typeof(List<>)
  35. };
  36. var listImplemType = new Dictionary<Type, Type>
  37. {
  38. {typeof(IEnumerable<>), typeof(LuObjectConverterList)},
  39. {typeof(ICollection<>), typeof(LuObjectConverterCollection)},
  40. {typeof(Collection<>), typeof(LuObjectConverterCollection)},
  41. {typeof(IList<>), typeof(LuObjectConverterList)},
  42. {typeof(List<>), typeof(LuObjectConverterList)}
  43. };
  44. var services = new ServiceCollection();
  45. services.AddSingleton<ILuObjectConverterDescriptorIdentity, LuObjectConverterDescriptorIdentity>();
  46. services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
  47. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
  48. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
  49. services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
  50. foreach (var typeFrom in listTypes)
  51. {
  52. foreach (var typeTo in listTypes)
  53. {
  54. services.AddSingleton(typeILuObjectConverter.MakeGenericType(typeFrom, typeTo), listImplemType[typeTo]);
  55. }
  56. }
  57. services.AddSingleton<ILuObjectConverter<TestModel1, TestDbo1>, LuObjectConverterPoco<TestModel1, TestDbo1>>();
  58. services.AddSingleton<ILuObjectConverter<TestModel2, TestDbo2>, LuObjectConverterPoco<TestModel2, TestDbo2>>();
  59. var serviceProvider = services.BuildServiceProvider();
  60. return serviceProvider;
  61. }
  62. protected LuConvertersOptions GetConverterOptions()
  63. {
  64. var options = new LuConvertersOptions
  65. {
  66. Parameters = new Dictionary<ParameterExpression, Expression>(),
  67. TypeConverter = new LuConvertersTypeConverter(new Dictionary<Type, Type>
  68. {
  69. {typeof(TestDbo1), typeof(TestModel1)},
  70. {typeof(TestDbo2), typeof(TestModel2)},
  71. {typeof(TestModel1), typeof(TestDbo1)},
  72. {typeof(TestModel2), typeof(TestDbo2)}
  73. }),
  74. Allocator = new LuConvertersAllocator()
  75. };
  76. return options;
  77. }
  78. [Fact]
  79. void Test1()
  80. {
  81. var serviceProvider = GetServiceProvider();
  82. var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
  83. var model = new TestModel1
  84. {
  85. id = Guid.NewGuid(),
  86. name = "Test.",
  87. test_model2 = new List<TestModel2>
  88. {
  89. new TestModel2
  90. {
  91. id = Guid.NewGuid(),
  92. name = "bla",
  93. parent = null,
  94. test_model1 = null,
  95. test_model1_id = Guid.Empty
  96. }
  97. }
  98. };
  99. var result = converter.Convert(model, LuPartialFieldsParser.Parse("*").Data, GetConverterOptions());
  100. Assert.Equal(LuStatus.Success.ToInt(), result.Status);
  101. var dbo = result.Data as TestDbo1;
  102. Assert.NotNull(dbo);
  103. Assert.Equal(model.id, dbo.Id);
  104. Assert.Equal(model.name, dbo.Name);
  105. Assert.Equal(model.test_model2.Count, dbo.TestDbo2s.Count);
  106. foreach (var e in model.test_model2.Zip(dbo.TestDbo2s, (model2, dbo2) => new {Model = model2, Dbo = dbo2}))
  107. {
  108. Assert.Equal(e.Model.id, e.Dbo.Id);
  109. Assert.Equal(e.Model.name, e.Dbo.Name);
  110. Assert.Equal(e.Model.name + " " + e.Model.name, e.Dbo.NameVirtual);
  111. Assert.Equal(null, e.Dbo.Parent);
  112. Assert.Equal(null, e.Dbo.TestDbo1);
  113. }
  114. }
  115. }
  116. }