123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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<TTypeFrom, TTypeTo, TTypeImpl>(IServiceCollection services)
- where TTypeImpl : class, ILuExpressionConverter<TTypeFrom, TTypeTo>
- {
- services.AddSingleton<ILuExpressionConverter<TTypeFrom, TTypeTo>, TTypeImpl>();
- services.TryAddSingleton<ILuExpressionConverter<TTypeFrom>, TTypeImpl>();
- }
-
- protected IServiceProvider GetServiceProvider()
- {
- var services = new ServiceCollection();
- services.AddSingleton<LuExpressionConverterEnumerable>();
- AddILuExpressionConverter<TestDbo1, TestModel1, LuDMECTest1>(services);
- AddILuExpressionConverter<TestDbo2, TestModel2, LuDMECTest2>(services);
- AddILuExpressionConverter<LuExpressionConverterStaticMethods, LuExpressionConverterStaticMethods, LuExpressionConverterStaticMethods>(services);
- AddILuExpressionConverter<string, string, LuExpressionConverterIdentity<string>>(services);
- AddILuExpressionConverter<Guid, Guid, LuExpressionConverterIdentity<Guid>>(services);
- AddILuExpressionConverter<bool, bool, LuExpressionConverterIdentity<bool>>(services);
-
- var serviceProvider = services.BuildServiceProvider();
- return serviceProvider;
- }
-
- [Fact]
- public void Test4()
- {
- Expression<Func<TestDbo2, Guid>> expDbo = (x => x.TestDbo1.Id);
- Expression<Func<TestModel2, Guid>> 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<Func<TestModel2, Guid>>(result, options.Parameter) : null;
- Assert.Equal(expModel.ToString(), lamdba?.ToString());
- }
-
- [Fact]
- public void Test5()
- {
- Expression<Func<TestDbo2, int>> expDbo = (x => 0);
- Expression<Func<TestModel2, int>> 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<Func<TestModel2, int>>(result, options.Parameter) : null;
- Assert.Equal(expModel.ToString(), lamdba?.ToString());
- }
-
- [Fact]
- public void Test6()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => x.Name.Contains("s"));
- Expression<Func<TestModel2, bool>> 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<Func<TestModel2, bool>>(result, options.Parameter) : null;
- Assert.Equal(expModel.ToString(), lamdba?.ToString());
- }
-
- [Fact]
- public void Test7()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains("s"));
- Expression<Func<TestModel2, bool>> 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<Func<TestModel2, bool>>(result, options.Parameter) : null;
- Assert.Equal(expModel.ToString(), lamdba?.ToString());
- }
-
- [Fact]
- public void Test8()
- {
- Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains(x.Name));
- Expression<Func<TestModel2, bool>> 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<Func<TestModel2, bool>>(result, options.Parameter) : null;
- Assert.Equal(expModel.ToString(), lamdba?.ToString());
- }
-
- [Fact]
- public void Test9()
- {
- Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any());
- Expression<Func<TestModel1, bool>> 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<Func<TestModel2, bool>>(result, options.Parameter) : null;
- Assert.Equal(expModel.ToString(), lamdba?.ToString());
- }
-
- [Fact]
- public void Test10()
- {
- Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s")));
- Expression<Func<TestModel1, bool>> 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<Func<TestModel2, bool>>(result, options.Parameter) : null;
- Assert.Equal(expModel.ToString(), lamdba?.ToString());
- }
- }
- }
|