12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using Luticate2.Auth.Utils.Business.ExpressionConverter;
- 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.Fields;
- 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 services = new ServiceCollection();
- services.AddSingleton<ILuObjectConverterDescriptorIdentity, LuObjectConverterDescriptorIdentity>();
- services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
- services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
- services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
-
-
- services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
- services.AddSingleton<ILuObjectConverter<TestModel1, TestDbo1>, LuObjectConverterPoco<TestModel1, TestDbo1>>();
- services.AddSingleton<ILuObjectConverter<TestModel2, TestDbo2>, LuObjectConverterPoco<TestModel2, TestDbo2>>();
-
- var serviceProvider = services.BuildServiceProvider();
- return serviceProvider;
- }
-
- protected LuExpressionConverterOptions GetConverterOptions()
- {
- var options = new LuExpressionConverterOptions
- {
- Parameters = new Dictionary<ParameterExpression, Expression>(),
- Types = new Dictionary<Type, Type>
- {
- {typeof(TestDbo1), typeof(TestModel1)},
- {typeof(TestDbo2), typeof(TestModel2)}
- },
- Allocator = type =>
- {
- return Activator.CreateInstance(type);
- }
- };
- return options;
- }
-
- [Fact]
- void Test1()
- {
- var serviceProvider = GetServiceProvider();
- var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
- var model = new TestModel1
- {
- id = Guid.NewGuid(),
- name = "Test.",
- test_model2 = new List<TestModel2>
- {
- 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);
- }
- }
- }
|