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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Luticate2.Auth.Utils.Business.Converters;
  6. using Luticate2.Auth.Utils.Business.Fields;
  7. using Luticate2.Auth.Utils.Dbo;
  8. using Luticate2.Auth.Utils.Dbo.Fields;
  9. using Luticate2.Auth.Utils.Dbo.Result;
  10. using Luticate2.Auth.Utils.Interfaces;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Xunit;
  13. namespace Luticate2.Auth.Tests.Business.ObjectConverter
  14. {
  15. public class ObjectConverterTests
  16. {
  17. protected IServiceProvider GetServiceProvider()
  18. {
  19. var services = new ServiceCollection();
  20. services.AddLuObjectConverterDescriptors();
  21. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
  22. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
  23. services.AddLuObjectConverters();
  24. services.AddLuObjectConverterPoco<TestModel1, TestDbo1>();
  25. services.AddLuObjectConverterPoco<TestModel2, TestDbo2>();
  26. var serviceProvider = services.BuildServiceProvider();
  27. return serviceProvider;
  28. }
  29. protected ILuObjectConverterOptions GetConverterOptions(IServiceProvider serviceProvider)
  30. {
  31. var options = new LuConvertersOptions
  32. {
  33. Parameters = new Dictionary<ParameterExpression, Expression>(),
  34. TypeConverter = new LuConvertersTypeConverter(new Dictionary<Type, Type>
  35. {
  36. {typeof(TestDbo1), typeof(TestModel1)},
  37. {typeof(TestDbo2), typeof(TestModel2)},
  38. {typeof(TestModel1), typeof(TestDbo1)},
  39. {typeof(TestModel2), typeof(TestDbo2)}
  40. }),
  41. Allocator = serviceProvider.GetService<ILuConvertersAllocator>()
  42. };
  43. return options;
  44. }
  45. protected TestModel1 GetBasicModel()
  46. {
  47. return new TestModel1
  48. {
  49. id = Guid.NewGuid(),
  50. name = "Test.",
  51. test_model2 = new List<TestModel2>
  52. {
  53. new TestModel2
  54. {
  55. id = Guid.NewGuid(),
  56. name = "bla",
  57. parent = null,
  58. test_model1 = null,
  59. test_model1_id = Guid.Empty,
  60. optional_int = 42
  61. }
  62. }
  63. };
  64. }
  65. [Fact]
  66. public void TestBasic1()
  67. {
  68. var serviceProvider = GetServiceProvider();
  69. var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
  70. var model = GetBasicModel();
  71. var result = converter.Convert(model, new TestDbo1(), new LuFieldDbo(), LuPartialFieldsParser.Parse("*").Data, GetConverterOptions(serviceProvider));
  72. Assert.Equal(LuStatus.Success.ToInt(), result.Status);
  73. var dbo = result.Data as TestDbo1;
  74. Assert.NotNull(dbo);
  75. Assert.Equal(model.id, dbo.Id);
  76. Assert.Equal(model.name, dbo.Name);
  77. Assert.NotNull(dbo.TestDbo2s);
  78. Assert.Equal(model.test_model2.Count, dbo.TestDbo2s.Count);
  79. foreach (var e in model.test_model2.Zip(dbo.TestDbo2s, (model2, dbo2) => new {Model = model2, Dbo = dbo2}))
  80. {
  81. Assert.Equal(e.Model.id, e.Dbo.Id);
  82. Assert.Equal(e.Model.name, e.Dbo.Name);
  83. Assert.Equal(e.Model.name + " " + e.Model.name, e.Dbo.NameVirtual);
  84. Assert.Equal(e.Model.optional_int, e.Dbo.NotOptionalInt);
  85. Assert.Null(e.Dbo.Parent);
  86. Assert.Null(e.Dbo.TestDbo1);
  87. }
  88. }
  89. [Fact]
  90. void TestPath1()
  91. {
  92. var serviceProvider = GetServiceProvider();
  93. var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
  94. var model = GetBasicModel();
  95. var result = converter.Convert(model, new TestDbo1(), new LuFieldDbo(), LuPartialFieldsParser.Parse("Id").Data, GetConverterOptions(serviceProvider));
  96. Assert.Equal(LuStatus.Success.ToInt(), result.Status);
  97. var dbo = result.Data as TestDbo1;
  98. Assert.NotNull(dbo);
  99. Assert.Equal(model.id, dbo.Id);
  100. Assert.Null(dbo.Name);
  101. Assert.Null(dbo.TestDbo2s);
  102. }
  103. [Fact]
  104. void TestPath2()
  105. {
  106. var serviceProvider = GetServiceProvider();
  107. var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
  108. var model = GetBasicModel();
  109. var result = converter.Convert(model, new TestDbo1(), new LuFieldDbo(), LuPartialFieldsParser.Parse("id").Data, GetConverterOptions(serviceProvider));
  110. Assert.Equal(LuStatus.Success.ToInt(), result.Status);
  111. var dbo = result.Data as TestDbo1;
  112. Assert.NotNull(dbo);
  113. Assert.Equal(model.id, dbo.Id);
  114. Assert.Null(dbo.Name);
  115. Assert.Null(dbo.TestDbo2s);
  116. }
  117. [Fact]
  118. public void TestPath3()
  119. {
  120. var serviceProvider = GetServiceProvider();
  121. var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
  122. var model = GetBasicModel();
  123. var result = converter.Convert(model, new TestDbo1(), new LuFieldDbo(), LuPartialFieldsParser.Parse("Id, TestDbo2s(Id, Name)").Data, GetConverterOptions(serviceProvider));
  124. Assert.Equal(LuStatus.Success.ToInt(), result.Status);
  125. var dbo = result.Data as TestDbo1;
  126. Assert.NotNull(dbo);
  127. Assert.Equal(model.id, dbo.Id);
  128. Assert.Null(dbo.Name);
  129. Assert.NotNull(dbo.TestDbo2s);
  130. Assert.Equal(model.test_model2.Count, dbo.TestDbo2s.Count);
  131. foreach (var e in model.test_model2.Zip(dbo.TestDbo2s, (model2, dbo2) => new {Model = model2, Dbo = dbo2}))
  132. {
  133. Assert.Equal(e.Model.id, e.Dbo.Id);
  134. Assert.Equal(e.Model.name, e.Dbo.Name);
  135. Assert.Null(e.Dbo.NameVirtual);
  136. Assert.Null(e.Dbo.Parent);
  137. Assert.Null(e.Dbo.TestDbo1);
  138. }
  139. }
  140. }
  141. }