-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Enterwell/feature/extension-tests
Added tests
- Loading branch information
Showing
13 changed files
with
420 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Build and test | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build-and-test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Use dotnet 5.0.x | ||
uses: actions/setup-dotnet@v1 | ||
with: | ||
dotnet-version: '5.0.x' | ||
|
||
- name: Restore | ||
run: dotnet restore | ||
- name: Build | ||
run: dotnet build | ||
- name: Test | ||
run: dotnet test |
218 changes: 215 additions & 3 deletions
218
Enterwell.AutoMapper.Extensions.Tests/AutoMapperExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,227 @@ | ||
using System; | ||
using AutoMapper; | ||
using Xunit; | ||
|
||
namespace Enterwell.AutoMapper.Extensions.Tests | ||
{ | ||
/// <summary> | ||
/// Tests for AutoMapper extensions. | ||
/// </summary> | ||
public class AutoMapperExtensionsTests | ||
{ | ||
|
||
/// <summary> | ||
/// Tests MapTo extension. | ||
/// </summary> | ||
[Fact] | ||
public void Test1() | ||
public void AutoMapperExtensionsTests_MapTo() | ||
{ | ||
Assert.Equal(1,1); | ||
var mapper = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<MockSimpleEntitySource, MockSimpleEntityDestination>() | ||
.ForMember(dst => dst.DestinationPropOne, | ||
opt => opt.MapFrom(src => src.SourcePropOne)); | ||
}).CreateMapper(); | ||
|
||
var source = new MockSimpleEntitySource | ||
{ | ||
SourcePropOne = 5, | ||
CommonPropOne = Guid.NewGuid().ToString() | ||
}; | ||
|
||
var destination = source.MapTo<MockSimpleEntityDestination>(mapper); | ||
|
||
Assert.Equal(source.SourcePropOne, destination.DestinationPropOne); | ||
Assert.Equal(source.CommonPropOne, destination.CommonPropOne); | ||
} | ||
|
||
/// <summary> | ||
/// Tests MapProperty extension. | ||
/// </summary> | ||
[Fact] | ||
public void AutoMapperExtensionsTests_MapProperty() | ||
{ | ||
var mapper = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<MockSimpleEntitySource, MockSimpleEntityDestination>() | ||
.MapProperty(dst => dst.DestinationPropOne, src => src.SourcePropOne); | ||
}).CreateMapper(); | ||
|
||
var commonProp = Guid.NewGuid().ToString(); | ||
var srcProp = 5; | ||
var source = new MockSimpleEntitySource { CommonPropOne = commonProp, SourcePropOne = srcProp }; | ||
var dst = mapper.Map<MockSimpleEntityDestination>(source); | ||
|
||
Assert.Equal(source.CommonPropOne, dst.CommonPropOne); | ||
Assert.Equal(source.SourcePropOne, dst.DestinationPropOne); | ||
} | ||
|
||
/// <summary> | ||
/// Tests MapPropertyFunc extension. | ||
/// </summary> | ||
[Fact] | ||
public void AutoMapperExtensionsTests_MapPropertyFunc() | ||
{ | ||
const int expectedDestProp = 3; | ||
var mapper = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<MockSimpleEntitySource, MockSimpleEntityDestination>() | ||
.MapPropertyFunc(dst => dst.DestinationPropOne, src => Math.Min(src.SourcePropOne, expectedDestProp)); | ||
}).CreateMapper(); | ||
|
||
var commonProp = Guid.NewGuid().ToString(); | ||
const int srcProp = 5; | ||
var source = new MockSimpleEntitySource { CommonPropOne = commonProp, SourcePropOne = srcProp }; | ||
var dst = mapper.Map<MockSimpleEntityDestination>(source); | ||
|
||
Assert.Equal(source.CommonPropOne, dst.CommonPropOne); | ||
Assert.Equal(dst.DestinationPropOne, expectedDestProp); | ||
} | ||
|
||
/// <summary> | ||
/// Tests map with additional required property that is valid. | ||
/// </summary> | ||
[Fact] | ||
public void AutoMapperExtensionsTests_MapComposeTo_RequiredCompositePropertyValid() | ||
{ | ||
var mapper = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<MockSimpleEntitySource, MockComposedEntityDestination>() | ||
.MapProperty(dst => dst.DestinationPropOne, src => src.SourcePropOne) | ||
.MapCompositePropertyRequired(dst => dst.AdditionalRequiredProperty); | ||
}).CreateMapper(); | ||
|
||
var commonProp = Guid.NewGuid().ToString(); | ||
int srcProp = 5; | ||
var additionalProperty = DateTime.Now; | ||
|
||
var source = new MockSimpleEntitySource | ||
{ | ||
CommonPropOne = commonProp, | ||
SourcePropOne = srcProp | ||
}; | ||
|
||
var destination = source.MapComposeTo<MockComposedEntityDestination>(mapper, additionalProperty); | ||
|
||
Assert.Equal(source.CommonPropOne, destination.CommonPropOne); | ||
Assert.Equal(source.SourcePropOne, destination.DestinationPropOne); | ||
Assert.Equal(destination.AdditionalRequiredProperty, additionalProperty); | ||
} | ||
|
||
/// <summary> | ||
/// Tests MapCompose with additional required property that is missing. | ||
/// </summary> | ||
[Fact] | ||
public void AutoMapperExtensionsTests_MapComposeTo_RequiredCompositePropertyMissing() | ||
{ | ||
var mapper = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<MockSimpleEntitySource, MockComposedEntityDestination>() | ||
.MapProperty(dst => dst.DestinationPropOne, src => src.SourcePropOne) | ||
.MapCompositePropertyRequired(dst => dst.AdditionalRequiredProperty); | ||
}).CreateMapper(); | ||
|
||
var source = new MockSimpleEntitySource(); | ||
|
||
var exception = Record.Exception(() => source.MapComposeTo<MockComposedEntityDestination>(mapper)); | ||
|
||
Assert.NotNull(exception); | ||
} | ||
|
||
/// <summary> | ||
/// Tests MapCompose with additional required property provided wrong type. | ||
/// </summary> | ||
[Fact] | ||
public void AutoMapperExtensionsTests_MapComposeTo_CompositePropertyWrongType() | ||
{ | ||
var mapper = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<MockSimpleEntitySource, MockComposedEntityDestination>() | ||
.MapProperty(dst => dst.DestinationPropOne, src => src.SourcePropOne) | ||
.MapCompositePropertyRequired(dst => dst.AdditionalRequiredProperty); | ||
}).CreateMapper(); | ||
|
||
const string additionalPropertyWrongType = "String instead of DateTime"; | ||
|
||
var source = new MockSimpleEntitySource(); | ||
|
||
var exception = Record.Exception(() => source.MapComposeTo<MockComposedEntityDestination>(mapper, additionalPropertyWrongType)); | ||
|
||
Assert.NotNull(exception); | ||
} | ||
|
||
/// <summary> | ||
/// Tests MapCompose second additional property optional. | ||
/// </summary> | ||
[Fact] | ||
public void AutoMapperExtensionsTests_MapComposeTo_SecondPropertyOptional() | ||
{ | ||
var mapper = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<MockSimpleEntitySource, MockComposedEntityDestination>() | ||
.MapProperty(dst => dst.DestinationPropOne, src => src.SourcePropOne) | ||
.MapCompositePropertyRequired(dst => dst.AdditionalRequiredProperty) | ||
.MapCompositeProperty(dst => dst.SecondAdditionalOptionalProperty, 1); | ||
}).CreateMapper(); | ||
|
||
var source = new MockSimpleEntitySource(); | ||
var firstAdditionalProperty = DateTime.Now; | ||
var destination = source.MapComposeTo<MockComposedEntityDestination>(mapper, firstAdditionalProperty); | ||
Assert.Equal(destination.AdditionalRequiredProperty, firstAdditionalProperty); | ||
} | ||
|
||
/// <summary> | ||
/// Tests MapCompose two additional properties required. | ||
/// </summary> | ||
[Fact] | ||
public void AutoMapperExtensionsTests_MapComposeTo_TwoAdditionalPropertiesRequired() | ||
{ | ||
var mapper = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<MockSimpleEntitySource, MockComposedEntityDestination>() | ||
.MapProperty(dst => dst.DestinationPropOne, src => src.SourcePropOne) | ||
.MapCompositePropertyRequired(dst => dst.AdditionalRequiredProperty) | ||
.MapCompositePropertyRequired(dst => dst.SecondAdditionalOptionalProperty, 1); | ||
}).CreateMapper(); | ||
|
||
var source = new MockSimpleEntitySource(); | ||
var firstAdditionalProperty = DateTime.Now; | ||
var secondAdditionalProperty = Guid.NewGuid().ToString(); | ||
var destination = source.MapComposeTo<MockComposedEntityDestination>(mapper, firstAdditionalProperty, secondAdditionalProperty); | ||
Assert.Equal(destination.AdditionalRequiredProperty, firstAdditionalProperty); | ||
Assert.Equal(destination.SecondAdditionalOptionalProperty, secondAdditionalProperty); | ||
} | ||
|
||
/// <summary> | ||
/// Tests map with MapCompositeAll. | ||
/// </summary> | ||
[Fact] | ||
public void AutoMapperExtensionsTests_MapCompositeAll() | ||
{ | ||
var mapper = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<MockSimpleEntitySource, MockComposedEntityDestination>() | ||
.MapProperty(dst => dst.DestinationPropOne, src => src.SourcePropOne) | ||
.MapCompositeAll(typeof(MockComposedEntitySource)); | ||
}).CreateMapper(); | ||
|
||
var source = new MockSimpleEntitySource | ||
{ | ||
SourcePropOne = 1, | ||
CommonPropOne = Guid.NewGuid().ToString() | ||
}; | ||
|
||
var composedSource = new MockComposedEntitySource | ||
{ | ||
AdditionalRequiredProperty = DateTime.Now, | ||
SecondAdditionalOptionalProperty = Guid.NewGuid().ToString() | ||
}; | ||
|
||
var destination = source.MapComposeTo<MockComposedEntityDestination>(mapper, composedSource); | ||
|
||
Assert.Equal(destination.CommonPropOne, source.CommonPropOne); | ||
Assert.Equal(destination.DestinationPropOne, source.SourcePropOne); | ||
Assert.Equal(destination.AdditionalRequiredProperty, composedSource.AdditionalRequiredProperty); | ||
Assert.Equal(destination.SecondAdditionalOptionalProperty, composedSource.SecondAdditionalOptionalProperty); | ||
} | ||
} | ||
} |
35 changes: 0 additions & 35 deletions
35
Enterwell.AutoMapper.Extensions.Tests/AutoMapperTestProfile.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
Enterwell.AutoMapper.Extensions.Tests/MockComposedEntityDestination.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
|
||
namespace Enterwell.AutoMapper.Extensions.Tests | ||
{ | ||
/// <summary> | ||
/// Mock composed entity destination. | ||
/// </summary> | ||
/// <seealso cref="MockSimpleEntityDestination" /> | ||
public class MockComposedEntityDestination : MockSimpleEntityDestination | ||
{ | ||
/// <summary> | ||
/// Gets or sets the additional property. | ||
/// </summary> | ||
/// <value> | ||
/// The additional property. | ||
/// </value> | ||
public DateTime AdditionalRequiredProperty { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the second additional property. | ||
/// </summary> | ||
/// <value> | ||
/// The second additional property. | ||
/// </value> | ||
public string SecondAdditionalOptionalProperty { get; set; } | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Enterwell.AutoMapper.Extensions.Tests/MockComposedEntitySource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
|
||
namespace Enterwell.AutoMapper.Extensions.Tests | ||
{ | ||
/// <summary> | ||
/// Mock source composed entity. | ||
/// </summary> | ||
public class MockComposedEntitySource | ||
{ | ||
/// <summary> | ||
/// Gets or sets the additional property. | ||
/// </summary> | ||
/// <value> | ||
/// The additional property. | ||
/// </value> | ||
public DateTime AdditionalRequiredProperty { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the second additional property. | ||
/// </summary> | ||
/// <value> | ||
/// The second additional property. | ||
/// </value> | ||
public string SecondAdditionalOptionalProperty { get; set; } | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
Enterwell.AutoMapper.Extensions.Tests/MockEntityDestination.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.