using System; using System.Linq; using System.Linq.Expressions; using Luticate2.Auth.Business.Fields; using Luticate2.Auth.Business.Fields.DMEC; using Luticate2.Auth.Interfaces; using Luticate2.Auth.Tests.Business.Fields; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Xunit; namespace Luticate2.Auth.Tests { public class RandomTests { // [Fact] // public void Test1() // { // var dbContext = new luticate2Context(); // var items = dbContext.LuGroups.Where(groups => groups.LuGroupsObjects.Any(x => x.Priority == 0)); // var list = items.ToList(); // Assert.NotNull(list); // Assert.Equal(1, list.Count); // } // // [Fact] // public void Test2() // { // var dbContext = new luticate2Context(); // var items = dbContext.LuGroups.OrderBy(x => x.Id).Where(groups => groups.LuGroupsObjects.Any(x => x.Priority == 0)); // var list = items.ToList(); // Assert.NotNull(list); // Assert.Equal(1, list.Count); // } // // [Fact] // public void Test3() // { // var dbContext = new luticate2Context(); // var items = dbContext.LuGroups.OrderBy(x => x.Id); // var list = items.ToList(); // Assert.NotNull(list); // Assert.Equal(2, list.Count); // } protected void AddILuExpressionConverter(IServiceCollection services) where TTypeImpl : class, ILuExpressionConverter { services.AddSingleton, TTypeImpl>(); services.TryAddSingleton, TTypeImpl>(); } protected IServiceProvider GetServiceProvider() { var services = new ServiceCollection(); services.AddSingleton(); AddILuExpressionConverter(services); AddILuExpressionConverter(services); AddILuExpressionConverter(services); AddILuExpressionConverter>(services); AddILuExpressionConverter>(services); AddILuExpressionConverter>(services); var serviceProvider = services.BuildServiceProvider(); return serviceProvider; } [Fact] public void Test4() { Expression> expDbo = (x => x.TestDbo1.Id); Expression> expModel = (x => x.TestModel1.Id); var options = new LuExpressionConverterOptions { Parameter = Expression.Parameter(typeof(TestModel2), "x") }; var converter = new LuExpressionConverterVisitor(options, GetServiceProvider()); var result = converter.Visit(expDbo.Body); var lamdba = result != null ? Expression.Lambda>(result, options.Parameter) : null; Assert.Equal(expModel.ToString(), lamdba?.ToString()); } [Fact] public void Test5() { Expression> expDbo = (x => 0); Expression> expModel = (x => 0); var options = new LuExpressionConverterOptions { Parameter = Expression.Parameter(typeof(TestModel2), "x") }; var converter = new LuExpressionConverterVisitor(options, GetServiceProvider()); var result = converter.Visit(expDbo.Body); var lamdba = result != null ? Expression.Lambda>(result, options.Parameter) : null; Assert.Equal(expModel.ToString(), lamdba?.ToString()); } [Fact] public void Test6() { Expression> expDbo = (x => x.Name.Contains("s")); Expression> expModel = (x => x.Name.Contains("s")); var options = new LuExpressionConverterOptions { Parameter = Expression.Parameter(typeof(TestModel2), "x") }; var converter = new LuExpressionConverterVisitor(options, GetServiceProvider()); var result = converter.Visit(expDbo.Body); var lamdba = result != null ? Expression.Lambda>(result, options.Parameter) : null; Assert.Equal(expModel.ToString(), lamdba?.ToString()); } [Fact] public void Test7() { Expression> expDbo = (x => x.TestDbo1.Name.Contains("s")); Expression> expModel = (x => x.TestModel1.Name.Contains("s")); var options = new LuExpressionConverterOptions { Parameter = Expression.Parameter(typeof(TestModel2), "x") }; var converter = new LuExpressionConverterVisitor(options, GetServiceProvider()); var result = converter.Visit(expDbo.Body); var lamdba = result != null ? Expression.Lambda>(result, options.Parameter) : null; Assert.Equal(expModel.ToString(), lamdba?.ToString()); } [Fact] public void Test8() { Expression> expDbo = (x => x.TestDbo1.Name.Contains(x.Name)); Expression> expModel = (x => x.TestModel1.Name.Contains(x.Name)); var options = new LuExpressionConverterOptions { Parameter = Expression.Parameter(typeof(TestModel2), "x") }; var converter = new LuExpressionConverterVisitor(options, GetServiceProvider()); var result = converter.Visit(expDbo.Body); var lamdba = result != null ? Expression.Lambda>(result, options.Parameter) : null; Assert.Equal(expModel.ToString(), lamdba?.ToString()); } [Fact] public void Test9() { Expression> expDbo = (x => x.TestDbo2s.Any()); Expression> expModel = (x => x.TestModel2.Any()); var options = new LuExpressionConverterOptions { Parameter = Expression.Parameter(typeof(TestModel1), "x") }; var converter = new LuExpressionConverterVisitor(options, GetServiceProvider()); var result = converter.Visit(expDbo.Body); var lamdba = result != null ? Expression.Lambda>(result, options.Parameter) : null; Assert.Equal(expModel.ToString(), lamdba?.ToString()); } [Fact] public void Test10() { Expression> expDbo = (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s"))); Expression> expModel = (x => x.TestModel2.Any(y => y.Name.Contains(x.Name + "s"))); var options = new LuExpressionConverterOptions { Parameter = Expression.Parameter(typeof(TestModel1), "x") }; var converter = new LuExpressionConverterVisitor(options, GetServiceProvider()); var result = converter.Visit(expDbo.Body); var lamdba = result != null ? Expression.Lambda>(result, options.Parameter) : null; Assert.Equal(expModel.ToString(), lamdba?.ToString()); } } }