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.

ExpressionConverterTests.cs 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.ExpressionConverter;
  7. using Luticate2.Auth.Utils.Interfaces;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Xunit;
  10. namespace Luticate2.Auth.Tests.Business.ExpressionConverter
  11. {
  12. public class ExpressionConverterTests
  13. {
  14. protected IServiceProvider GetServiceProvider()
  15. {
  16. var services = new ServiceCollection();
  17. services.AddLuObjectConverterDescriptors();
  18. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
  19. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
  20. var serviceProvider = services.BuildServiceProvider();
  21. return serviceProvider;
  22. }
  23. protected ILuExpressionConverterVisitorOptions GetConverterOptions()
  24. {
  25. var options = new LuConvertersOptions
  26. {
  27. Parameters = new Dictionary<ParameterExpression, Expression>(),
  28. TypeConverter = new LuConvertersTypeConverter(new Dictionary<Type, Type>
  29. {
  30. {typeof(TestDbo1), typeof(TestModel1)},
  31. {typeof(TestDbo2), typeof(TestModel2)}
  32. })
  33. };
  34. return options;
  35. }
  36. protected LuExpressionConverterVisitor GetConverter()
  37. {
  38. return new LuExpressionConverterVisitor(GetConverterOptions(), GetServiceProvider());
  39. }
  40. public static IEnumerable<object[]> GetTests()
  41. {
  42. return new List<object[]>
  43. {
  44. new object[]
  45. {
  46. "TestSimpleProperty1",
  47. (Expression<Func<TestDbo2, Guid>>) (x => x.TestDbo1.Id),
  48. (Expression<Func<TestModel2, Guid>>) (Param_0 => Param_0.test_model1.id)
  49. },
  50. new object[]
  51. {
  52. "TestSimpleProperty2",
  53. (Expression<Func<TestDbo2, bool>>) (x => x.TestDbo1.Id == Guid.Empty),
  54. (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.test_model1.id == Guid.Empty)
  55. },
  56. new object[]
  57. {
  58. "TestSimpleProperty3",
  59. (Expression<Func<TestDbo2, bool>>) (x => x.TestDbo1.Name.Length == 2),
  60. (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.test_model1.name.Length == 2)
  61. },
  62. new object[]
  63. {
  64. "TestStaticProperty1",
  65. (Expression<Func<TestDbo2, bool>>) (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticField),
  66. (Expression<Func<TestModel2, bool>>) (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticField)
  67. },
  68. new object[]
  69. {
  70. "TestStaticProperty2",
  71. (Expression<Func<TestDbo2, bool>>) (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticProperty),
  72. (Expression<Func<TestModel2, bool>>) (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticProperty)
  73. },
  74. new object[]
  75. {
  76. "TestVirtualProperty1",
  77. (Expression<Func<TestDbo2, string>>) (x => x.NameVirtual),
  78. (Expression<Func<TestModel2, string>>) (Param_0 => Param_0.name == null ? "[no data]" : Param_0.name + " " + Param_0.name)
  79. },
  80. new object[]
  81. {
  82. "TestVirtualProperty2",
  83. (Expression<Func<TestDbo2, string>>) (x => x.Parent.NameVirtual),
  84. (Expression<Func<TestModel2, string>>) (Param_0 => Param_0.parent.name == null ? "[no data]" : Param_0.parent.name + " " + Param_0.parent.name)
  85. },
  86. new object[]
  87. {
  88. "TestStaticValue1",
  89. (Expression<Func<TestDbo2, int>>) (x => 0),
  90. (Expression<Func<TestModel2, int>>) (Param_0 => 0)
  91. },
  92. new object[]
  93. {
  94. "TestStaticValue2",
  95. (Expression<Func<TestDbo2, TestDbo2>>) (x => x),
  96. (Expression<Func<TestModel2, TestModel2>>) (Param_0 => Param_0)
  97. },
  98. new object[]
  99. {
  100. "TestStaticValue3",
  101. (Expression<Func<TestDbo2, bool>>) (x => StaticDbo.StaticField),
  102. (Expression<Func<TestModel2, bool>>) (Param_0 => StaticDbo.StaticField)
  103. },
  104. new object[]
  105. {
  106. "TestComplexExpression1",
  107. (Expression<Func<TestDbo2, int>>) (x => (x.Name + " " + x.Name).Length),
  108. (Expression<Func<TestModel2, int>>) (Param_0 => (Param_0.name + " " + Param_0.name).Length)
  109. },
  110. new object[]
  111. {
  112. "TestComplexExpression2",
  113. (Expression<Func<TestDbo2, int>>) (x => (x.NameVirtual + " " + x.Name).Length),
  114. (Expression<Func<TestModel2, int>>) (Param_0 => ((Param_0.name == null ? "[no data]" : (Param_0.name + " " + Param_0.name)) + " " + Param_0.name).Length)
  115. },
  116. new object[]
  117. {
  118. "TestSimpleMethodSimpleArg1",
  119. (Expression<Func<TestDbo2, bool>>) (x => x.Name.Contains("s")),
  120. (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.name.Contains("s"))
  121. },
  122. new object[]
  123. {
  124. "TestSimpleMethodSimpleArg2",
  125. (Expression<Func<TestDbo2, bool>>) (x => x.TestDbo1.Name.Contains("s")),
  126. (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.test_model1.name.Contains("s"))
  127. },
  128. new object[]
  129. {
  130. "TestSimpleMethodSimpleArg3",
  131. (Expression<Func<TestDbo2, string>>) (x => x.TestDbo1.ToString()),
  132. (Expression<Func<TestModel2, string>>) (Param_0 => Param_0.test_model1.id + ": " + Param_0.test_model1.name)
  133. },
  134. new object[]
  135. {
  136. "TestSimpleMethodAdvArg1",
  137. (Expression<Func<TestDbo2, bool>>) (x => x.TestDbo1.Name.Contains(x.Name)),
  138. (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.test_model1.name.Contains(Param_0.name))
  139. },
  140. new object[]
  141. {
  142. "TestAdvMethodSimpleArg1",
  143. (Expression<Func<TestDbo1, bool>>) (x => x.TestDbo2s.Any()),
  144. (Expression<Func<TestModel1, bool>>) (Param_0 => Param_0.test_model2.Any())
  145. },
  146. new object[]
  147. {
  148. "TestAdvMethodAdvArg1",
  149. (Expression<Func<TestDbo1, bool>>) (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s"))),
  150. (Expression<Func<TestModel1, bool>>) (Param_0 => Param_0.test_model2.Any(Param_1 => Param_1.name.Contains(Param_0.name + "s")))
  151. }
  152. };
  153. }
  154. [Theory]
  155. [MemberData(nameof(GetTests))]
  156. public void Test(string testName, LambdaExpression expDbo, LambdaExpression expModel)
  157. {
  158. var converter = GetConverter();
  159. var result = converter.Visit(expDbo);
  160. Assert.Equal(expModel.ToString(), result?.ToString());
  161. }
  162. }
  163. }