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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 LuConvertersOptions 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. [Fact]
  46. public void TestSimpleProperty1()
  47. {
  48. Expression<Func<TestDbo2, Guid>> expDbo = (x => x.TestDbo1.Id);
  49. Expression<Func<TestModel2, Guid>> expModel = (Param_0 => Param_0.test_model1.id);
  50. var converter = GetConverter();
  51. var result = converter.Visit(expDbo);
  52. Assert.Equal(expModel.ToString(), result?.ToString());
  53. }
  54. [Fact]
  55. public void TestSimpleProperty2()
  56. {
  57. Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Id == Guid.Empty);
  58. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.id == Guid.Empty);
  59. var converter = GetConverter();
  60. var result = converter.Visit(expDbo);
  61. Assert.Equal(expModel.ToString(), result?.ToString());
  62. }
  63. [Fact]
  64. public void TestSimpleProperty3()
  65. {
  66. Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Length == 2);
  67. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Length == 2);
  68. var converter = GetConverter();
  69. var result = converter.Visit(expDbo);
  70. Assert.Equal(expModel.ToString(), result?.ToString());
  71. }
  72. [Fact]
  73. public void TestStaticProperty1()
  74. {
  75. Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticField);
  76. Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticField);
  77. var converter = GetConverter();
  78. var result = converter.Visit(expDbo);
  79. Assert.Equal(expModel.ToString(), result?.ToString());
  80. }
  81. [Fact]
  82. public void TestStaticProperty2()
  83. {
  84. Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticProperty);
  85. Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticProperty);
  86. var converter = GetConverter();
  87. var result = converter.Visit(expDbo);
  88. Assert.Equal(expModel.ToString(), result?.ToString());
  89. }
  90. [Fact]
  91. public void TestVirtualProperty1()
  92. {
  93. Expression<Func<TestDbo2, string>> expDbo = (x => x.NameVirtual);
  94. Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.name == null ? "[no data]" : Param_0.name + " " + Param_0.name);
  95. var converter = GetConverter();
  96. var result = converter.Visit(expDbo);
  97. Assert.Equal(expModel.ToString(), result?.ToString());
  98. }
  99. [Fact]
  100. public void TestVirtualProperty2()
  101. {
  102. Expression<Func<TestDbo2, string>> expDbo = (x => x.Parent.NameVirtual);
  103. Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.parent.name == null ? "[no data]" : Param_0.parent.name + " " + Param_0.parent.name);
  104. var converter = GetConverter();
  105. var result = converter.Visit(expDbo);
  106. Assert.Equal(expModel.ToString(), result?.ToString());
  107. }
  108. [Fact]
  109. public void TestStaticValue1()
  110. {
  111. Expression<Func<TestDbo2, int>> expDbo = (x => 0);
  112. Expression<Func<TestModel2, int>> expModel = (Param_0 => 0);
  113. var converter = GetConverter();
  114. var result = converter.Visit(expDbo);
  115. Assert.Equal(expModel.ToString(), result?.ToString());
  116. }
  117. [Fact]
  118. public void TestStaticValue2()
  119. {
  120. Expression<Func<TestDbo2, TestDbo2>> expDbo = (x => x);
  121. Expression<Func<TestModel2, TestModel2>> expModel = (Param_0 => Param_0);
  122. var converter = GetConverter();
  123. var result = converter.Visit(expDbo);
  124. Assert.Equal(expModel.ToString(), result?.ToString());
  125. }
  126. [Fact]
  127. public void TestStaticValue3()
  128. {
  129. Expression<Func<TestDbo2, bool>> expDbo = (x => StaticDbo.StaticField);
  130. Expression<Func<TestModel2, bool>> expModel = (Param_0 => StaticDbo.StaticField);
  131. var converter = GetConverter();
  132. var result = converter.Visit(expDbo);
  133. Assert.Equal(expModel.ToString(), result?.ToString());
  134. }
  135. [Fact]
  136. public void TestComplexExpression1()
  137. {
  138. Expression<Func<TestDbo2, int>> expDbo = (x => (x.Name + " " + x.Name).Length);
  139. Expression<Func<TestModel2, int>> expModel = (Param_0 => (Param_0.name + " " + Param_0.name).Length);
  140. var converter = GetConverter();
  141. var result = converter.Visit(expDbo);
  142. Assert.Equal(expModel.ToString(), result?.ToString());
  143. }
  144. [Fact]
  145. public void TestComplexExpression2()
  146. {
  147. Expression<Func<TestDbo2, int>> expDbo = (x => (x.NameVirtual + " " + x.Name).Length);
  148. Expression<Func<TestModel2, int>> expModel = (Param_0 => ((Param_0.name == null ? "[no data]" : (Param_0.name + " " + Param_0.name)) + " " + Param_0.name).Length);
  149. var converter = GetConverter();
  150. var result = converter.Visit(expDbo);
  151. Assert.Equal(expModel.ToString(), result?.ToString());
  152. }
  153. [Fact]
  154. public void TestSimpleMethodSimpleArg1()
  155. {
  156. Expression<Func<TestDbo2, bool>> expDbo = (x => x.Name.Contains("s"));
  157. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.name.Contains("s"));
  158. var converter = GetConverter();
  159. var result = converter.Visit(expDbo);
  160. Assert.Equal(expModel.ToString(), result?.ToString());
  161. }
  162. [Fact]
  163. public void TestSimpleMethodSimpleArg2()
  164. {
  165. Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains("s"));
  166. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains("s"));
  167. var converter = GetConverter();
  168. var result = converter.Visit(expDbo);
  169. Assert.Equal(expModel.ToString(), result?.ToString());
  170. }
  171. [Fact]
  172. public void TestSimpleMethodSimpleArg3()
  173. {
  174. Expression<Func<TestDbo2, string>> expDbo = (x => x.TestDbo1.ToString());
  175. Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.test_model1.id + ": " + Param_0.test_model1.name);
  176. var converter = GetConverter();
  177. var result = converter.Visit(expDbo);
  178. Assert.Equal(expModel.ToString(), result?.ToString());
  179. }
  180. [Fact]
  181. public void TestSimpleMethodAdvArg1()
  182. {
  183. Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains(x.Name));
  184. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains(Param_0.name));
  185. var converter = GetConverter();
  186. var result = converter.Visit(expDbo);
  187. Assert.Equal(expModel.ToString(), result?.ToString());
  188. }
  189. [Fact]
  190. public void TestAdvMethodSimpleArg1()
  191. {
  192. Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any());
  193. Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any());
  194. var converter = GetConverter();
  195. var result = converter.Visit(expDbo);
  196. Assert.Equal(expModel.ToString(), result?.ToString());
  197. }
  198. [Fact]
  199. public void TestAdvMethodAdvArg1()
  200. {
  201. Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s")));
  202. Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any(Param_1 => Param_1.name.Contains(Param_0.name + "s")));
  203. var converter = GetConverter();
  204. var result = converter.Visit(expDbo);
  205. Assert.Equal(expModel.ToString(), result?.ToString());
  206. }
  207. }
  208. }