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(); services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable)); services.AddSingleton, LuOcdTest1>(); services.AddSingleton, LuOcdTest2>(); var serviceProvider = services.BuildServiceProvider(); return serviceProvider; } protected LuConvertersOptions GetConverterOptions() { var options = new LuConvertersOptions { Parameters = new Dictionary(), TypeConverter = new LuConvertersTypeConverter(new Dictionary { {typeof(TestDbo1), typeof(TestModel1)}, {typeof(TestDbo2), typeof(TestModel2)} }) }; return options; } protected LuExpressionConverterVisitor GetConverter() { return new LuExpressionConverterVisitor(GetConverterOptions(), GetServiceProvider()); } [Fact] public void TestSimpleProperty1() { Expression> expDbo = (x => x.TestDbo1.Id); Expression> 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> expDbo = (x => x.TestDbo1.Id == Guid.Empty); Expression> 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> expDbo = (x => x.TestDbo1.Name.Length == 2); Expression> 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> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticField); Expression> 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> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticProperty); Expression> 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> expDbo = (x => x.NameVirtual); Expression> 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> expDbo = (x => x.Parent.NameVirtual); Expression> 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> expDbo = (x => 0); Expression> expModel = (Param_0 => 0); var converter = GetConverter(); var result = converter.Visit(expDbo); Assert.Equal(expModel.ToString(), result?.ToString()); } [Fact] public void TestStaticValue2() { Expression> expDbo = (x => x); Expression> expModel = (Param_0 => Param_0); var converter = GetConverter(); var result = converter.Visit(expDbo); Assert.Equal(expModel.ToString(), result?.ToString()); } [Fact] public void TestStaticValue3() { Expression> expDbo = (x => StaticDbo.StaticField); Expression> expModel = (Param_0 => StaticDbo.StaticField); var converter = GetConverter(); var result = converter.Visit(expDbo); Assert.Equal(expModel.ToString(), result?.ToString()); } [Fact] public void TestComplexExpression1() { Expression> expDbo = (x => (x.Name + " " + x.Name).Length); Expression> 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> expDbo = (x => (x.NameVirtual + " " + x.Name).Length); Expression> 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> expDbo = (x => x.Name.Contains("s")); Expression> 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> expDbo = (x => x.TestDbo1.Name.Contains("s")); Expression> 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> expDbo = (x => x.TestDbo1.ToString()); Expression> 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> expDbo = (x => x.TestDbo1.Name.Contains(x.Name)); Expression> 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> expDbo = (x => x.TestDbo2s.Any()); Expression> 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> expDbo = (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s"))); Expression> 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()); } } }