123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using Luticate2.Auth.Utils.Business.Converters;
- using Luticate2.Auth.Utils.Business.ExpressionConverter;
- using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
- using Luticate2.Auth.Utils.Interfaces;
- using Microsoft.Extensions.DependencyInjection;
- using Xunit;
-
- namespace Luticate2.Auth.Tests.Business.ExpressionConverter
- {
- public class ExpressionConverterTests
- {
-
- protected IServiceProvider GetServiceProvider()
- {
- var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
- var typeILuObjectConverterDescriptorEnumerable =
- typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable));
-
- var services = new ServiceCollection();
- services.AddSingleton<ILuObjectConverterDescriptorIdentity, LuObjectConverterDescriptorIdentity>();
- services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
- services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
- services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
-
- var serviceProvider = services.BuildServiceProvider();
- return serviceProvider;
- }
-
- protected LuConvertersOptions GetConverterOptions()
- {
- var options = new LuConvertersOptions
- {
- Parameters = new Dictionary<ParameterExpression, Expression>(),
- TypeConverter = new LuConvertersTypeConverter(new Dictionary<Type, Type>
- {
- {typeof(TestDbo1), typeof(TestModel1)},
- {typeof(TestDbo2), typeof(TestModel2)}
- })
- };
- return options;
- }
-
- protected LuExpressionConverterVisitor GetConverter()
- {
- return new LuExpressionConverterVisitor(GetConverterOptions(), GetServiceProvider());
- }
-
- [Fact]
- public void TestSimpleProperty1()
- {
- Expression<Func<TestDbo2, Guid>> expDbo = (x => x.TestDbo1.Id);
- Expression<Func<TestModel2, Guid>> expModel = (Param_0 => Param_0.test_model1.id);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestSimpleProperty2()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Id == Guid.Empty);
- Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.id == Guid.Empty);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestSimpleProperty3()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Length == 2);
- Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Length == 2);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestStaticProperty1()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticField);
- Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticField);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestStaticProperty2()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticProperty);
- Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticProperty);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestVirtualProperty1()
- {
- Expression<Func<TestDbo2, string>> expDbo = (x => x.NameVirtual);
- Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.name == null ? "[no data]" : Param_0.name + " " + Param_0.name);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestVirtualProperty2()
- {
- Expression<Func<TestDbo2, string>> expDbo = (x => x.Parent.NameVirtual);
- Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.parent.name == null ? "[no data]" : Param_0.parent.name + " " + Param_0.parent.name);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestStaticValue1()
- {
- Expression<Func<TestDbo2, int>> expDbo = (x => 0);
- Expression<Func<TestModel2, int>> expModel = (Param_0 => 0);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestStaticValue2()
- {
- Expression<Func<TestDbo2, TestDbo2>> expDbo = (x => x);
- Expression<Func<TestModel2, TestModel2>> expModel = (Param_0 => Param_0);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestStaticValue3()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => StaticDbo.StaticField);
- Expression<Func<TestModel2, bool>> expModel = (Param_0 => StaticDbo.StaticField);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestComplexExpression1()
- {
- Expression<Func<TestDbo2, int>> expDbo = (x => (x.Name + " " + x.Name).Length);
- Expression<Func<TestModel2, int>> expModel = (Param_0 => (Param_0.name + " " + Param_0.name).Length);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestComplexExpression2()
- {
- Expression<Func<TestDbo2, int>> expDbo = (x => (x.NameVirtual + " " + x.Name).Length);
- Expression<Func<TestModel2, int>> expModel = (Param_0 => ((Param_0.name == null ? "[no data]" : (Param_0.name + " " + Param_0.name)) + " " + Param_0.name).Length);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestSimpleMethodSimpleArg1()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => x.Name.Contains("s"));
- Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.name.Contains("s"));
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestSimpleMethodSimpleArg2()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains("s"));
- Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains("s"));
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestSimpleMethodSimpleArg3()
- {
- Expression<Func<TestDbo2, string>> expDbo = (x => x.TestDbo1.ToString());
- Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.test_model1.id + ": " + Param_0.test_model1.name);
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestSimpleMethodAdvArg1()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains(x.Name));
- Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains(Param_0.name));
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestAdvMethodSimpleArg1()
- {
- Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any());
- Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any());
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
-
- [Fact]
- public void TestAdvMethodAdvArg1()
- {
- Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s")));
- Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any(Param_1 => Param_1.name.Contains(Param_0.name + "s")));
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
- }
- }
|