JsonMappingTest.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using Xunit;
  4. namespace VCommon.VAutoMapper.Tests
  5. {
  6. public class JsonMappingTest
  7. {
  8. public class JsonObj
  9. {
  10. public string Name { get; set; }
  11. public string XXXX { get; set; }
  12. }
  13. //TODO JsonMappingTest
  14. [AutoMapFrom(typeof(Entity))]
  15. [AutoMapTo(typeof(Entity))]
  16. public class Dto
  17. {
  18. public Guid Id { get; set; }
  19. public Guid? NullableValue { get; set; }
  20. public string Name { get; set; }
  21. public bool IsEnable { get; set; }
  22. [AutoMapJsonConvert]
  23. public IReadOnlyDictionary<int, string> Tags { get; set; }
  24. //好耶! AutoMapper 更新到 10.x 之后支持 IReadOnlyDictionary 了!
  25. }
  26. public class Entity
  27. {
  28. public Guid Id { get; set; }
  29. public Guid? NullableValue { get; set; }
  30. public string Name { get; set; }
  31. public bool IsEnable { get; set; }
  32. public string Tags { get; set; }
  33. }
  34. [Fact]
  35. public void DtoToEntity()
  36. {
  37. var dto = new Dto
  38. {
  39. Id = new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1),
  40. Name = "DtoName",
  41. IsEnable = true,
  42. Tags = new Dictionary<int, string> { { 123, "abc" } }
  43. };
  44. var entity = dto.MapTo<Entity>();
  45. var dto2 = entity.MapTo<Dto>();
  46. Assert.Equal(dto.Id, entity.Id);
  47. Assert.Equal(dto.Name, entity.Name);
  48. Assert.Equal(dto.IsEnable, entity.IsEnable);
  49. }
  50. }
  51. }