123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using Luticate2.Auth.Utils.Business.Converters;
- using Luticate2.Auth.Utils.Business.Converters.ExpressionConverter;
- using Luticate2.Auth.Utils.Interfaces;
- using Microsoft.Extensions.DependencyInjection;
- using Xunit;
-
- namespace Luticate2.Auth.Tests.Business.ExpressionConverter
- {
- public class ExpressionConverterTests
- {
-
- protected IServiceProvider GetServiceProvider()
- {
- var services = new ServiceCollection();
- services.AddLuObjectConverterDescriptors();
- services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
- services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
-
- var serviceProvider = services.BuildServiceProvider();
- return serviceProvider;
- }
-
- protected ILuExpressionConverterVisitorOptions 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());
- }
-
- public static IEnumerable<object[]> GetTests()
- {
- return new List<object[]>
- {
- new object[]
- {
- "TestSimpleProperty1",
- (Expression<Func<TestDbo2, Guid>>) (x => x.TestDbo1.Id),
- (Expression<Func<TestModel2, Guid>>) (Param_0 => Param_0.test_model1.id)
- },
- new object[]
- {
- "TestSimpleProperty2",
- (Expression<Func<TestDbo2, bool>>) (x => x.TestDbo1.Id == Guid.Empty),
- (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.test_model1.id == Guid.Empty)
- },
- new object[]
- {
- "TestSimpleProperty3",
- (Expression<Func<TestDbo2, bool>>) (x => x.TestDbo1.Name.Length == 2),
- (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.test_model1.name.Length == 2)
- },
- new object[]
- {
- "TestStaticProperty1",
- (Expression<Func<TestDbo2, bool>>) (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticField),
- (Expression<Func<TestModel2, bool>>) (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticField)
- },
- new object[]
- {
- "TestStaticProperty2",
- (Expression<Func<TestDbo2, bool>>) (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticProperty),
- (Expression<Func<TestModel2, bool>>) (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticProperty)
- },
- new object[]
- {
- "TestVirtualProperty1",
- (Expression<Func<TestDbo2, string>>) (x => x.NameVirtual),
- (Expression<Func<TestModel2, string>>) (Param_0 => Param_0.name == null ? "[no data]" : Param_0.name + " " + Param_0.name)
- },
- new object[]
- {
- "TestVirtualProperty2",
- (Expression<Func<TestDbo2, string>>) (x => x.Parent.NameVirtual),
- (Expression<Func<TestModel2, string>>) (Param_0 => Param_0.parent.name == null ? "[no data]" : Param_0.parent.name + " " + Param_0.parent.name)
- },
- new object[]
- {
- "TestStaticValue1",
- (Expression<Func<TestDbo2, int>>) (x => 0),
- (Expression<Func<TestModel2, int>>) (Param_0 => 0)
- },
- new object[]
- {
- "TestStaticValue2",
- (Expression<Func<TestDbo2, TestDbo2>>) (x => x),
- (Expression<Func<TestModel2, TestModel2>>) (Param_0 => Param_0)
- },
- new object[]
- {
- "TestStaticValue3",
- (Expression<Func<TestDbo2, bool>>) (x => StaticDbo.StaticField),
- (Expression<Func<TestModel2, bool>>) (Param_0 => StaticDbo.StaticField)
- },
- new object[]
- {
- "TestComplexExpression1",
- (Expression<Func<TestDbo2, int>>) (x => (x.Name + " " + x.Name).Length),
- (Expression<Func<TestModel2, int>>) (Param_0 => (Param_0.name + " " + Param_0.name).Length)
- },
- new object[]
- {
- "TestComplexExpression2",
- (Expression<Func<TestDbo2, int>>) (x => (x.NameVirtual + " " + x.Name).Length),
- (Expression<Func<TestModel2, int>>) (Param_0 => ((Param_0.name == null ? "[no data]" : (Param_0.name + " " + Param_0.name)) + " " + Param_0.name).Length)
- },
- new object[]
- {
- "TestSimpleMethodSimpleArg1",
- (Expression<Func<TestDbo2, bool>>) (x => x.Name.Contains("s")),
- (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.name.Contains("s"))
- },
- new object[]
- {
- "TestSimpleMethodSimpleArg2",
- (Expression<Func<TestDbo2, bool>>) (x => x.TestDbo1.Name.Contains("s")),
- (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.test_model1.name.Contains("s"))
- },
- new object[]
- {
- "TestSimpleMethodSimpleArg3",
- (Expression<Func<TestDbo2, string>>) (x => x.TestDbo1.ToString()),
- (Expression<Func<TestModel2, string>>) (Param_0 => Param_0.test_model1.id + ": " + Param_0.test_model1.name)
- },
- new object[]
- {
- "TestSimpleMethodAdvArg1",
- (Expression<Func<TestDbo2, bool>>) (x => x.TestDbo1.Name.Contains(x.Name)),
- (Expression<Func<TestModel2, bool>>) (Param_0 => Param_0.test_model1.name.Contains(Param_0.name))
- },
- new object[]
- {
- "TestAdvMethodSimpleArg1",
- (Expression<Func<TestDbo1, bool>>) (x => x.TestDbo2s.Any()),
- (Expression<Func<TestModel1, bool>>) (Param_0 => Param_0.test_model2.Any())
- },
- new object[]
- {
- "TestAdvMethodAdvArg1",
- (Expression<Func<TestDbo1, bool>>) (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s"))),
- (Expression<Func<TestModel1, bool>>) (Param_0 => Param_0.test_model2.Any(Param_1 => Param_1.name.Contains(Param_0.name + "s")))
- }
- };
- }
-
- [Theory]
- [MemberData(nameof(GetTests))]
- public void Test(string testName, LambdaExpression expDbo, LambdaExpression expModel)
- {
- var converter = GetConverter();
- var result = converter.Visit(expDbo);
- Assert.Equal(expModel.ToString(), result?.ToString());
- }
- }
- }
|