Skip to content

Commit

Permalink
Merge pull request #1 from Enterwell/feature/extension-tests
Browse files Browse the repository at this point in the history
Added tests
  • Loading branch information
AleksandarDev authored Oct 1, 2024
2 parents a4eb748 + a3259e9 commit 5abf504
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 71 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/BuildAndTest.yml
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 Enterwell.AutoMapper.Extensions.Tests/AutoMapperExtensionsTests.cs
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 Enterwell.AutoMapper.Extensions.Tests/AutoMapperTestProfile.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="11.0.0" />
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand All @@ -20,4 +20,8 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Enterwell.AutoMapper.Extensions\Enterwell.AutoMapper.Extensions.csproj" />
</ItemGroup>

</Project>
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 Enterwell.AutoMapper.Extensions.Tests/MockComposedEntitySource.cs
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 Enterwell.AutoMapper.Extensions.Tests/MockEntityDestination.cs

This file was deleted.

Loading

0 comments on commit 5abf504

Please sign in to comment.