Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ExpressionConverterTests.cs 8.1KB

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