Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LuDboModelExpressionConverterTests.cs 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using Luticate2.Auth.Business;
  6. using Luticate2.Auth.Business.Fields;
  7. using Luticate2.Auth.Business.ObjectConverter;
  8. using Luticate2.Auth.Dbo;
  9. using Luticate2.Auth.Dbo.Result;
  10. using Luticate2.Auth.Interfaces;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Xunit;
  13. namespace Luticate2.Auth.Tests.Business.Fields
  14. {
  15. public static class StaticDbo
  16. {
  17. public static bool StaticField;
  18. public static bool StaticProperty { get; set; }
  19. }
  20. public class TestDbo1
  21. {
  22. public Guid Id { get; set; }
  23. public string Name { get; set; }
  24. public IList<TestDbo2> TestDbo2s { get; set; }
  25. public string ToString()
  26. {
  27. throw new NotImplementedException();
  28. }
  29. }
  30. public class TestDbo2
  31. {
  32. public Guid Id { get; set; }
  33. public string Name { get; set; }
  34. public string NameVirtual { get; set; }
  35. public TestDbo1 TestDbo1 { get; set; }
  36. public TestDbo2 Parent { get; set; }
  37. public TestModel1 Unused { get; set; }
  38. }
  39. public class TestModel1
  40. {
  41. public Guid id { get; set; }
  42. public string name { get; set; }
  43. public ICollection<TestModel2> test_model2 { get; set; }
  44. }
  45. public class TestModel2
  46. {
  47. public Guid id { get; set; }
  48. public string name { get; set; }
  49. public Guid test_model1_id { get; set; }
  50. public TestModel1 test_model1 { get; set; }
  51. public TestModel2 parent { get; set; }
  52. }
  53. public class LuDMECTest1 : LuObjectConverterDescriptor<TestDbo1, TestModel1>
  54. {
  55. public LuDMECTest1()
  56. {
  57. AddMemberConverter(x => x.Id, y => y.id, PropertyType.Value);
  58. AddMemberConverter(x => x.Name, y => y.name, PropertyType.Value);
  59. AddMemberConverter(x => x.TestDbo2s, y => y.test_model2, PropertyType.Object);
  60. AddStaticMethodConverter(
  61. (Expression<Func<TestDbo1, string>>)(x => x.ToString()),
  62. (Expression<Func<TestModel1, string>>)(x => x.id + ": " + x.name)
  63. );
  64. }
  65. }
  66. public class LuDMECTest2 : LuObjectConverterDescriptor<TestDbo2, TestModel2>
  67. {
  68. public LuDMECTest2()
  69. {
  70. AddMemberConverter(x => x.Id, y => y.id, PropertyType.Value);
  71. AddMemberConverter(x => x.Name, y => y.name, PropertyType.Value);
  72. AddMemberConverter(x => x.NameVirtual, y => y.name + " " + y.name, PropertyType.Value);
  73. AddMemberConverter(x => x.TestDbo1, y => y.test_model1, PropertyType.Object);
  74. AddMemberConverter(x => x.Parent, y => y.parent, PropertyType.Object);
  75. }
  76. }
  77. public class LuDboModelExpressionConverterTests
  78. {
  79. public IServiceProvider GetServiceProvider()
  80. {
  81. var services = new ServiceCollection();
  82. // services.AddSingleton<ILuExpressionConverter<TestDbo1, TestModel1>, LuDMECTest1>();
  83. // services.AddSingleton<ILuExpressionConverter<TestDbo2, TestModel2>, LuDMECTest2>();
  84. // services.AddSingleton<ILuExpressionConverter<string, string>, LuExpressionConverterIdentity<string>>();
  85. // services.AddSingleton<ILuExpressionConverter<Guid, Guid>, LuExpressionConverterIdentity<Guid>>();
  86. var serviceProvider = services.BuildServiceProvider();
  87. return serviceProvider;
  88. }
  89. // [Fact]
  90. // public void Test0()
  91. // {
  92. // var serviceProvider = GetServiceProvider();
  93. // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo1, TestModel1>>();
  94. // Expression<Func<TestDbo1, TestDbo1>> expDbo = (x => x);
  95. // Expression<Func<TestModel1, object>> expModel = (x => (object) x);
  96. // var convertResult = converter.ConvertLamdba(expDbo);
  97. // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
  98. // Assert.NotNull(convertResult.Data);
  99. // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
  100. // }
  101. //
  102. // [Fact]
  103. // public void Test1()
  104. // {
  105. // var serviceProvider = GetServiceProvider();
  106. // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo1, TestModel1>>();
  107. // Expression<Func<TestDbo1, Guid>> expDbo = (x => x.Id);
  108. // Expression<Func<TestModel1, object>> expModel = (x => (object) x.Id);
  109. // var convertResult = converter.ConvertLamdba(expDbo);
  110. // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
  111. // Assert.NotNull(convertResult.Data);
  112. // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
  113. // }
  114. //
  115. // [Fact]
  116. // public void Test2()
  117. // {
  118. // var serviceProvider = GetServiceProvider();
  119. // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo2, TestModel2>>();
  120. // Expression<Func<TestDbo2, Guid>> expDbo = (x => x.TestDbo1.Id);
  121. // Expression<Func<TestModel2, object>> expModel = (x => (object) x.TestModel1.Id);
  122. // var convertResult = converter.ConvertLamdba(expDbo);
  123. // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
  124. // Assert.NotNull(convertResult.Data);
  125. // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
  126. // }
  127. //
  128. // [Fact]
  129. // public void Test3()
  130. // {
  131. // var serviceProvider = GetServiceProvider();
  132. // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo2, TestModel2>>();
  133. // Expression<Func<TestDbo2, int>> expDbo = (x => x.TestDbo1.Name.Length);
  134. // Expression<Func<TestModel2, object>> expModel = (x => (object) x.TestModel1.Name.Length);
  135. // var convertResult = converter.ConvertLamdba(expDbo);
  136. // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
  137. // Assert.NotNull(convertResult.Data);
  138. // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
  139. // }
  140. //
  141. // [Fact]
  142. // public void Test4()
  143. // {
  144. // var serviceProvider = GetServiceProvider();
  145. // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo2, TestModel2>>();
  146. // Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Length == 0 || x.Name.Length != 0);
  147. // Expression<Func<TestModel2, object>> expModel = (x => (object) (x.TestModel1.Name.Length == 0 || x.Name.Length != 0));
  148. // var convertResult = converter.ConvertLamdba(expDbo);
  149. // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
  150. // Assert.NotNull(convertResult.Data);
  151. // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
  152. // }
  153. }
  154. }