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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Luticate2.Auth.Business.ExpressionConverter;
  6. using Luticate2.Auth.Business.ObjectConverter;
  7. using Luticate2.Auth.Interfaces;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Xunit;
  10. namespace Luticate2.Auth.Tests.Business.ObjectConverter
  11. {
  12. public class ObjectConverterTests
  13. {
  14. protected IServiceProvider GetServiceProvider()
  15. {
  16. var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
  17. var typeILuObjectConverterDescriptorEnumerable =
  18. typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable));
  19. var services = new ServiceCollection();
  20. services.AddSingleton<ILuObjectConverterDescriptorIdentity, LuObjectConverterDescriptorIdentity>();
  21. services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
  22. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
  23. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
  24. var serviceProvider = services.BuildServiceProvider();
  25. return serviceProvider;
  26. }
  27. protected LuExpressionConverterOptions GetConverterOptions()
  28. {
  29. var options = new LuExpressionConverterOptions
  30. {
  31. Parameters = new Dictionary<ParameterExpression, Expression>(),
  32. Types = new Dictionary<Type, Type>
  33. {
  34. {typeof(TestDbo1), typeof(TestModel1)},
  35. {typeof(TestDbo2), typeof(TestModel2)}
  36. }
  37. };
  38. return options;
  39. }
  40. protected LuExpressionConverterVisitor GetConverter()
  41. {
  42. return new LuExpressionConverterVisitor(GetConverterOptions(), GetServiceProvider());
  43. }
  44. [Fact]
  45. public void TestSimpleProperty1()
  46. {
  47. Expression<Func<TestDbo2, Guid>> expDbo = (x => x.TestDbo1.Id);
  48. Expression<Func<TestModel2, Guid>> expModel = (Param_0 => Param_0.test_model1.id);
  49. var converter = GetConverter();
  50. var result = converter.Visit(expDbo);
  51. Assert.Equal(expModel.ToString(), result?.ToString());
  52. }
  53. [Fact]
  54. public void TestSimpleProperty2()
  55. {
  56. Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Id == Guid.Empty);
  57. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.id == Guid.Empty);
  58. var converter = GetConverter();
  59. var result = converter.Visit(expDbo);
  60. Assert.Equal(expModel.ToString(), result?.ToString());
  61. }
  62. [Fact]
  63. public void TestSimpleProperty3()
  64. {
  65. Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Length == 2);
  66. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Length == 2);
  67. var converter = GetConverter();
  68. var result = converter.Visit(expDbo);
  69. Assert.Equal(expModel.ToString(), result?.ToString());
  70. }
  71. [Fact]
  72. public void TestStaticProperty1()
  73. {
  74. Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticField);
  75. Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticField);
  76. var converter = GetConverter();
  77. var result = converter.Visit(expDbo);
  78. Assert.Equal(expModel.ToString(), result?.ToString());
  79. }
  80. [Fact]
  81. public void TestStaticProperty2()
  82. {
  83. Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticProperty);
  84. Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticProperty);
  85. var converter = GetConverter();
  86. var result = converter.Visit(expDbo);
  87. Assert.Equal(expModel.ToString(), result?.ToString());
  88. }
  89. [Fact]
  90. public void TestVirtualProperty1()
  91. {
  92. Expression<Func<TestDbo2, string>> expDbo = (x => x.NameVirtual);
  93. Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.name + " " + Param_0.name);
  94. var converter = GetConverter();
  95. var result = converter.Visit(expDbo);
  96. Assert.Equal(expModel.ToString(), result?.ToString());
  97. }
  98. [Fact]
  99. public void TestVirtualProperty2()
  100. {
  101. Expression<Func<TestDbo2, string>> expDbo = (x => x.Parent.NameVirtual);
  102. Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.parent.name + " " + Param_0.parent.name);
  103. var converter = GetConverter();
  104. var result = converter.Visit(expDbo);
  105. Assert.Equal(expModel.ToString(), result?.ToString());
  106. }
  107. [Fact]
  108. public void TestStaticValue1()
  109. {
  110. Expression<Func<TestDbo2, int>> expDbo = (x => 0);
  111. Expression<Func<TestModel2, int>> expModel = (Param_0 => 0);
  112. var converter = GetConverter();
  113. var result = converter.Visit(expDbo);
  114. Assert.Equal(expModel.ToString(), result?.ToString());
  115. }
  116. [Fact]
  117. public void TestStaticValue2()
  118. {
  119. Expression<Func<TestDbo2, TestDbo2>> expDbo = (x => x);
  120. Expression<Func<TestModel2, TestModel2>> expModel = (Param_0 => Param_0);
  121. var converter = GetConverter();
  122. var result = converter.Visit(expDbo);
  123. Assert.Equal(expModel.ToString(), result?.ToString());
  124. }
  125. [Fact]
  126. public void TestStaticValue3()
  127. {
  128. Expression<Func<TestDbo2, bool>> expDbo = (x => StaticDbo.StaticField);
  129. Expression<Func<TestModel2, bool>> expModel = (Param_0 => StaticDbo.StaticField);
  130. var converter = GetConverter();
  131. var result = converter.Visit(expDbo);
  132. Assert.Equal(expModel.ToString(), result?.ToString());
  133. }
  134. [Fact]
  135. public void TestComplexExpression1()
  136. {
  137. Expression<Func<TestDbo2, int>> expDbo = (x => (x.Name + " " + x.Name).Length);
  138. Expression<Func<TestModel2, int>> expModel = (Param_0 => (Param_0.name + " " + Param_0.name).Length);
  139. var converter = GetConverter();
  140. var result = converter.Visit(expDbo);
  141. Assert.Equal(expModel.ToString(), result?.ToString());
  142. }
  143. [Fact]
  144. public void TestComplexExpression2()
  145. {
  146. Expression<Func<TestDbo2, int>> expDbo = (x => (x.NameVirtual + " " + x.Name).Length);
  147. Expression<Func<TestModel2, int>> expModel = (Param_0 => (Param_0.name + " " + Param_0.name + " " + Param_0.name).Length);
  148. var converter = GetConverter();
  149. var result = converter.Visit(expDbo);
  150. Assert.Equal(expModel.ToString(), result?.ToString());
  151. }
  152. [Fact]
  153. public void TestSimpleMethodSimpleArg1()
  154. {
  155. Expression<Func<TestDbo2, bool>> expDbo = (x => x.Name.Contains("s"));
  156. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.name.Contains("s"));
  157. var converter = GetConverter();
  158. var result = converter.Visit(expDbo);
  159. Assert.Equal(expModel.ToString(), result?.ToString());
  160. }
  161. [Fact]
  162. public void TestSimpleMethodSimpleArg2()
  163. {
  164. Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains("s"));
  165. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains("s"));
  166. var converter = GetConverter();
  167. var result = converter.Visit(expDbo);
  168. Assert.Equal(expModel.ToString(), result?.ToString());
  169. }
  170. [Fact]
  171. public void TestSimpleMethodSimpleArg3()
  172. {
  173. Expression<Func<TestDbo2, string>> expDbo = (x => x.TestDbo1.ToString());
  174. Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.test_model1.id + ": " + Param_0.test_model1.name);
  175. var converter = GetConverter();
  176. var result = converter.Visit(expDbo);
  177. Assert.Equal(expModel.ToString(), result?.ToString());
  178. }
  179. [Fact]
  180. public void TestSimpleMethodAdvArg1()
  181. {
  182. Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains(x.Name));
  183. Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains(Param_0.name));
  184. var converter = GetConverter();
  185. var result = converter.Visit(expDbo);
  186. Assert.Equal(expModel.ToString(), result?.ToString());
  187. }
  188. [Fact]
  189. public void TestAdvMethodSimpleArg1()
  190. {
  191. Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any());
  192. Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any());
  193. var converter = GetConverter();
  194. var result = converter.Visit(expDbo);
  195. Assert.Equal(expModel.ToString(), result?.ToString());
  196. }
  197. [Fact]
  198. public void TestAdvMethodAdvArg1()
  199. {
  200. Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s")));
  201. Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any(Param_1 => Param_1.name.Contains(Param_0.name + "s")));
  202. var converter = GetConverter();
  203. var result = converter.Visit(expDbo);
  204. Assert.Equal(expModel.ToString(), result?.ToString());
  205. }
  206. }
  207. }