You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectConverterTests.cs 5.1KB

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