using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Linq.Expressions; using Luticate2.Auth.Utils.Business.Converters; using Luticate2.Auth.Utils.Business.Fields; using Luticate2.Auth.Utils.Business.ObjectConverter; using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor; using Luticate2.Auth.Utils.Dbo; using Luticate2.Auth.Utils.Dbo.Result; using Luticate2.Auth.Utils.Interfaces; using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Luticate2.Auth.Tests.Business.ObjectConverter { public class ObjectConverterTests { protected IServiceProvider GetServiceProvider() { var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>); var typeILuObjectConverterDescriptorEnumerable = typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable)); var typeILuObjectConverter = typeof(ILuObjectConverter<,>); var listTypes = new [] { typeof(IEnumerable<>), typeof(ICollection<>), typeof(Collection<>), typeof(IList<>), typeof(List<>) }; var listImplemType = new Dictionary { {typeof(IEnumerable<>), typeof(LuObjectConverterList)}, {typeof(ICollection<>), typeof(LuObjectConverterCollection)}, {typeof(Collection<>), typeof(LuObjectConverterCollection)}, {typeof(IList<>), typeof(LuObjectConverterList)}, {typeof(List<>), typeof(LuObjectConverterList)} }; var services = new ServiceCollection(); services.AddSingleton(); services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable)); services.AddSingleton, LuOcdTest1>(); services.AddSingleton, LuOcdTest2>(); services.AddSingleton(); foreach (var typeFrom in listTypes) { foreach (var typeTo in listTypes) { services.AddSingleton(typeILuObjectConverter.MakeGenericType(typeFrom, typeTo), listImplemType[typeTo]); } } services.AddSingleton, LuObjectConverterPoco>(); services.AddSingleton, LuObjectConverterPoco>(); 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)}, {typeof(TestModel1), typeof(TestDbo1)}, {typeof(TestModel2), typeof(TestDbo2)} }), Allocator = new LuConvertersAllocator() }; return options; } [Fact] void Test1() { var serviceProvider = GetServiceProvider(); var converter = serviceProvider.GetService>(); var model = new TestModel1 { id = Guid.NewGuid(), name = "Test.", test_model2 = new List { new TestModel2 { id = Guid.NewGuid(), name = "bla", parent = null, test_model1 = null, test_model1_id = Guid.Empty } } }; var result = converter.Convert(model, LuPartialFieldsParser.Parse("*").Data, GetConverterOptions()); Assert.Equal(LuStatus.Success.ToInt(), result.Status); var dbo = result.Data as TestDbo1; Assert.NotNull(dbo); Assert.Equal(model.id, dbo.Id); Assert.Equal(model.name, dbo.Name); Assert.Equal(model.test_model2.Count, dbo.TestDbo2s.Count); foreach (var e in model.test_model2.Zip(dbo.TestDbo2s, (model2, dbo2) => new {Model = model2, Dbo = dbo2})) { Assert.Equal(e.Model.id, e.Dbo.Id); Assert.Equal(e.Model.name, e.Dbo.Name); Assert.Equal(e.Model.name + " " + e.Model.name, e.Dbo.NameVirtual); Assert.Null(e.Dbo.Parent); Assert.Null(e.Dbo.TestDbo1); } } } }