|
| 1 | +namespace PanoramicData.Mapper.Test; |
| 2 | + |
| 3 | +public class MemberListTests |
| 4 | +{ |
| 5 | + private sealed class Source |
| 6 | + { |
| 7 | + public int Id { get; set; } |
| 8 | + public string Name { get; set; } = string.Empty; |
| 9 | + public string ApiKey { get; set; } = string.Empty; |
| 10 | + } |
| 11 | + |
| 12 | + private sealed class Destination |
| 13 | + { |
| 14 | + public int Id { get; set; } |
| 15 | + public string Name { get; set; } = string.Empty; |
| 16 | + } |
| 17 | + |
| 18 | + private sealed class SmallSource |
| 19 | + { |
| 20 | + public int Id { get; set; } |
| 21 | + public string Name { get; set; } = string.Empty; |
| 22 | + } |
| 23 | + |
| 24 | + private sealed class DestinationWithExtra |
| 25 | + { |
| 26 | + public int Id { get; set; } |
| 27 | + public string Name { get; set; } = string.Empty; |
| 28 | + public string Extra { get; set; } = string.Empty; |
| 29 | + } |
| 30 | + |
| 31 | + private sealed class MemberListSourceAllMappedProfile : Profile |
| 32 | + { |
| 33 | + public MemberListSourceAllMappedProfile() |
| 34 | + { |
| 35 | + // SmallSource has Id and Name, both exist on DestinationWithExtra |
| 36 | + // Extra on destination is unmatched but MemberList.Source only validates source members |
| 37 | + CreateMap<SmallSource, DestinationWithExtra>(MemberList.Source); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private sealed class MemberListSourceUnmappedProfile : Profile |
| 42 | + { |
| 43 | + public MemberListSourceUnmappedProfile() |
| 44 | + { |
| 45 | + CreateMap<Source, Destination>(MemberList.Source); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private sealed class MemberListSourceDoNotValidateProfile : Profile |
| 50 | + { |
| 51 | + public MemberListSourceDoNotValidateProfile() |
| 52 | + { |
| 53 | + CreateMap<Source, Destination>(MemberList.Source) |
| 54 | + .ForSourceMember(source => source.ApiKey, opt => opt.DoNotValidate()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private sealed class MemberListNoneProfile : Profile |
| 59 | + { |
| 60 | + public MemberListNoneProfile() |
| 61 | + { |
| 62 | + CreateMap<Source, Destination>(MemberList.None); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void MemberListSource_AllSourceMembersMapped_DoesNotThrow() |
| 68 | + { |
| 69 | + var config = new MapperConfiguration(cfg => |
| 70 | + cfg.AddProfile<MemberListSourceAllMappedProfile>()); |
| 71 | + |
| 72 | + // SmallSource.Id and SmallSource.Name both map to DestinationWithExtra |
| 73 | + // DestinationWithExtra.Extra is unmatched but irrelevant in MemberList.Source mode |
| 74 | + var act = () => config.AssertConfigurationIsValid(); |
| 75 | + |
| 76 | + act.Should().NotThrow(); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void MemberListSource_UnmappedSourceMember_Throws() |
| 81 | + { |
| 82 | + var config = new MapperConfiguration(cfg => |
| 83 | + cfg.AddProfile<MemberListSourceUnmappedProfile>()); |
| 84 | + |
| 85 | + // ApiKey exists on Source but not on Destination → unmapped source member |
| 86 | + var act = () => config.AssertConfigurationIsValid(); |
| 87 | + |
| 88 | + act.Should().Throw<AutoMapperConfigurationException>(); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void MemberListSource_ForSourceMemberDoNotValidate_ExcludesMember() |
| 93 | + { |
| 94 | + var config = new MapperConfiguration(cfg => |
| 95 | + cfg.AddProfile<MemberListSourceDoNotValidateProfile>()); |
| 96 | + |
| 97 | + var act = () => config.AssertConfigurationIsValid(); |
| 98 | + |
| 99 | + act.Should().NotThrow(); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void MemberListNone_SkipsValidationEntirely() |
| 104 | + { |
| 105 | + var config = new MapperConfiguration(cfg => |
| 106 | + cfg.AddProfile<MemberListNoneProfile>()); |
| 107 | + |
| 108 | + var act = () => config.AssertConfigurationIsValid(); |
| 109 | + |
| 110 | + act.Should().NotThrow(); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void MemberListSource_MappingStillWorks() |
| 115 | + { |
| 116 | + var config = new MapperConfiguration(cfg => |
| 117 | + cfg.AddProfile<MemberListSourceDoNotValidateProfile>()); |
| 118 | + |
| 119 | + var mapper = config.CreateMapper(); |
| 120 | + var source = new Source { Id = 42, Name = "Test", ApiKey = "secret" }; |
| 121 | + var result = mapper.Map<Destination>(source); |
| 122 | + |
| 123 | + result.Id.Should().Be(42); |
| 124 | + result.Name.Should().Be("Test"); |
| 125 | + } |
| 126 | +} |
0 commit comments