You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectConverterTests.cs 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Luticate2.Auth.Utils.Business.ExpressionConverter;
  6. using Luticate2.Auth.Utils.Business.Fields;
  7. using Luticate2.Auth.Utils.Business.ObjectConverter;
  8. using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
  9. using Luticate2.Auth.Utils.Dbo;
  10. using Luticate2.Auth.Utils.Dbo.Fields;
  11. using Luticate2.Auth.Utils.Dbo.Result;
  12. using Luticate2.Auth.Utils.Interfaces;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using Xunit;
  15. namespace Luticate2.Auth.Tests.Business.ObjectConverter
  16. {
  17. public class ObjectConverterTests
  18. {
  19. protected IServiceProvider GetServiceProvider()
  20. {
  21. var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
  22. var typeILuObjectConverterDescriptorEnumerable =
  23. typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable));
  24. var services = new ServiceCollection();
  25. services.AddSingleton<ILuObjectConverterDescriptorIdentity, LuObjectConverterDescriptorIdentity>();
  26. services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
  27. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
  28. services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
  29. services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
  30. services.AddSingleton<ILuObjectConverter<TestModel1, TestDbo1>, LuObjectConverterPoco<TestModel1, TestDbo1>>();
  31. services.AddSingleton<ILuObjectConverter<TestModel2, TestDbo2>, LuObjectConverterPoco<TestModel2, TestDbo2>>();
  32. var serviceProvider = services.BuildServiceProvider();
  33. return serviceProvider;
  34. }
  35. protected LuExpressionConverterOptions GetConverterOptions()
  36. {
  37. var options = new LuExpressionConverterOptions
  38. {
  39. Parameters = new Dictionary<ParameterExpression, Expression>(),
  40. Types = new Dictionary<Type, Type>
  41. {
  42. {typeof(TestDbo1), typeof(TestModel1)},
  43. {typeof(TestDbo2), typeof(TestModel2)}
  44. },
  45. Allocator = type =>
  46. {
  47. return Activator.CreateInstance(type);
  48. }
  49. };
  50. return options;
  51. }
  52. [Fact]
  53. void Test1()
  54. {
  55. var serviceProvider = GetServiceProvider();
  56. var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
  57. var model = new TestModel1
  58. {
  59. id = Guid.NewGuid(),
  60. name = "Test.",
  61. test_model2 = new List<TestModel2>
  62. {
  63. new TestModel2
  64. {
  65. id = Guid.NewGuid(),
  66. name = "bla",
  67. parent = null,
  68. test_model1 = null,
  69. test_model1_id = Guid.Empty
  70. }
  71. }
  72. };
  73. var result = converter.Convert(model, LuPartialFieldsParser.Parse("*").Data, GetConverterOptions());
  74. Assert.Equal(LuStatus.Success.ToInt(), result.Status);
  75. var dbo = result.Data as TestDbo1;
  76. Assert.NotNull(dbo);
  77. Assert.Equal(model.id, dbo.Id);
  78. Assert.Equal(model.name, dbo.Name);
  79. }
  80. }
  81. }