123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using System;
- using System.Collections.Generic;
- using System.Linq.Expressions;
- using System.Reflection;
- using Luticate2.Auth.Business;
- using Luticate2.Auth.Business.Fields;
- using Luticate2.Auth.Business.ObjectConverter;
- using Luticate2.Auth.Dbo;
- using Luticate2.Auth.Dbo.Result;
- using Luticate2.Auth.Interfaces;
- using Microsoft.Extensions.DependencyInjection;
- using Xunit;
-
- namespace Luticate2.Auth.Tests.Business.Fields
- {
- public static class StaticDbo
- {
- public static bool StaticField;
-
- public static bool StaticProperty { get; set; }
- }
-
- public class TestDbo1
- {
- public Guid Id { get; set; }
-
- public string Name { get; set; }
-
- public IList<TestDbo2> TestDbo2s { get; set; }
-
- public string ToString()
- {
- throw new NotImplementedException();
- }
- }
-
- public class TestDbo2
- {
- public Guid Id { get; set; }
-
- public string Name { get; set; }
-
- public string NameVirtual { get; set; }
-
- public TestDbo1 TestDbo1 { get; set; }
-
- public TestDbo2 Parent { get; set; }
-
- public TestModel1 Unused { get; set; }
- }
-
- public class TestModel1
- {
- public Guid id { get; set; }
-
- public string name { get; set; }
-
- public ICollection<TestModel2> test_model2 { get; set; }
- }
-
- public class TestModel2
- {
- public Guid id { get; set; }
-
- public string name { get; set; }
-
- public Guid test_model1_id { get; set; }
-
- public TestModel1 test_model1 { get; set; }
-
- public TestModel2 parent { get; set; }
- }
-
- public class LuDMECTest1 : LuObjectConverterDescriptor<TestDbo1, TestModel1>
- {
- public LuDMECTest1()
- {
- AddMemberConverter(x => x.Id, y => y.id, PropertyType.Value);
- AddMemberConverter(x => x.Name, y => y.name, PropertyType.Value);
- AddMemberConverter(x => x.TestDbo2s, y => y.test_model2, PropertyType.Object);
- AddStaticMethodConverter(
- (Expression<Func<TestDbo1, string>>)(x => x.ToString()),
- (Expression<Func<TestModel1, string>>)(x => x.id + ": " + x.name)
- );
- }
- }
-
- public class LuDMECTest2 : LuObjectConverterDescriptor<TestDbo2, TestModel2>
- {
- public LuDMECTest2()
- {
- AddMemberConverter(x => x.Id, y => y.id, PropertyType.Value);
- AddMemberConverter(x => x.Name, y => y.name, PropertyType.Value);
- AddMemberConverter(x => x.NameVirtual, y => y.name + " " + y.name, PropertyType.Value);
- AddMemberConverter(x => x.TestDbo1, y => y.test_model1, PropertyType.Object);
- AddMemberConverter(x => x.Parent, y => y.parent, PropertyType.Object);
- }
- }
-
- public class LuDboModelExpressionConverterTests
- {
- public IServiceProvider GetServiceProvider()
- {
- var services = new ServiceCollection();
- // services.AddSingleton<ILuExpressionConverter<TestDbo1, TestModel1>, LuDMECTest1>();
- // services.AddSingleton<ILuExpressionConverter<TestDbo2, TestModel2>, LuDMECTest2>();
- // services.AddSingleton<ILuExpressionConverter<string, string>, LuExpressionConverterIdentity<string>>();
- // services.AddSingleton<ILuExpressionConverter<Guid, Guid>, LuExpressionConverterIdentity<Guid>>();
-
- var serviceProvider = services.BuildServiceProvider();
- return serviceProvider;
- }
-
- // [Fact]
- // public void Test0()
- // {
- // var serviceProvider = GetServiceProvider();
- // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo1, TestModel1>>();
- // Expression<Func<TestDbo1, TestDbo1>> expDbo = (x => x);
- // Expression<Func<TestModel1, object>> expModel = (x => (object) x);
- // var convertResult = converter.ConvertLamdba(expDbo);
- // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
- // Assert.NotNull(convertResult.Data);
- // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
- // }
- //
- // [Fact]
- // public void Test1()
- // {
- // var serviceProvider = GetServiceProvider();
- // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo1, TestModel1>>();
- // Expression<Func<TestDbo1, Guid>> expDbo = (x => x.Id);
- // Expression<Func<TestModel1, object>> expModel = (x => (object) x.Id);
- // var convertResult = converter.ConvertLamdba(expDbo);
- // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
- // Assert.NotNull(convertResult.Data);
- // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
- // }
- //
- // [Fact]
- // public void Test2()
- // {
- // var serviceProvider = GetServiceProvider();
- // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo2, TestModel2>>();
- // Expression<Func<TestDbo2, Guid>> expDbo = (x => x.TestDbo1.Id);
- // Expression<Func<TestModel2, object>> expModel = (x => (object) x.TestModel1.Id);
- // var convertResult = converter.ConvertLamdba(expDbo);
- // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
- // Assert.NotNull(convertResult.Data);
- // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
- // }
- //
- // [Fact]
- // public void Test3()
- // {
- // var serviceProvider = GetServiceProvider();
- // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo2, TestModel2>>();
- // Expression<Func<TestDbo2, int>> expDbo = (x => x.TestDbo1.Name.Length);
- // Expression<Func<TestModel2, object>> expModel = (x => (object) x.TestModel1.Name.Length);
- // var convertResult = converter.ConvertLamdba(expDbo);
- // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
- // Assert.NotNull(convertResult.Data);
- // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
- // }
- //
- // [Fact]
- // public void Test4()
- // {
- // var serviceProvider = GetServiceProvider();
- // var converter = serviceProvider.GetService<ILuDboModelExpressionConverter<TestDbo2, TestModel2>>();
- // Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Length == 0 || x.Name.Length != 0);
- // Expression<Func<TestModel2, object>> expModel = (x => (object) (x.TestModel1.Name.Length == 0 || x.Name.Length != 0));
- // var convertResult = converter.ConvertLamdba(expDbo);
- // Assert.Equal(LuStatus.Success.ToInt(), convertResult.Status);
- // Assert.NotNull(convertResult.Data);
- // Assert.Equal(expModel.ToString(), convertResult.Data.ToString());
- // }
- }
- }
|