Skip to content

Commit

Permalink
Merge pull request MapsterMapper#310 from cmann-andagon/nested-async-…
Browse files Browse the repository at this point in the history
…issue

Add test which reproduces an issue with nested async mappings
  • Loading branch information
chaowlert committed Feb 23, 2021
2 parents 553f080 + 7be4ce0 commit 354605d
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/Mapster.Async.Tests/AsyncTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
Expand Down Expand Up @@ -55,6 +56,37 @@ public void Sync()
dto.Name.ShouldBe("bar");
}

[TestMethod]
public async Task NestedAsync()
{
TypeAdapterConfig<DoCar, DtoCar>.NewConfig();
TypeAdapterConfig<DoOwner, DtoOwner>.NewConfig();
TypeAdapterConfig<DoCarOwnership, DtoCarOwnership>.NewConfig()
.Ignore(dest => dest.Car)
.Ignore(dest => dest.Owner)
.AfterMappingAsync(async (src, dest) =>
{
dest.Owner = await GetOwner(src.Owner);
})
.AfterMappingAsync(async (src, dest) =>
{
dest.Car = await GetCar(src.Car);
});

var dtoOwnership = await new DoCarOwnership()
{
Id = "1",
Car = "1",
Owner = "1"
}
.BuildAdapter()
.AdaptToTypeAsync<DtoCarOwnership>();

dtoOwnership.Car.ShouldNotBeNull();
dtoOwnership.Car.Make.ShouldBe("Car Maker Inc");
dtoOwnership.Owner.ShouldNotBeNull();
dtoOwnership.Owner.Name.ShouldBe("John Doe");
}

private static async Task<string> GetName()
{
Expand All @@ -67,6 +99,26 @@ private static async Task<string> GetNameError()
await Task.Delay(1);
throw new Exception("bar");
}

private static async Task<DtoCar> GetCar(string id)
{
await Task.Delay(1000);
return await new DoCar()
{
Id = id,
Make = "Car Maker Inc",
Model = "Generic",
}.BuildAdapter().AdaptToTypeAsync<DtoCar>();
}

private static async Task<DtoOwner> GetOwner(string id)
{
return await new DoOwner()
{
Id = id,
Name = "John Doe"
}.BuildAdapter().AdaptToTypeAsync<DtoOwner>();
}
}

public class Poco
Expand All @@ -78,4 +130,44 @@ public class Dto
public string Id { get; set; }
public string Name { get; set; }
}

public class DtoCarOwnership
{
public string Id { get; set; }
public DtoOwner Owner { get; set; }
public DtoCar Car { get; set; }
}

public class DoCarOwnership
{
public string Id { get; set; }
public string Owner { get; set; }
public string Car { get; set; }
}

public class DtoOwner
{
public string Id { get; set; }
public string Name { get; set; }
}

public class DoOwner
{
public string Id { get; set; }
public string Name { get; set; }
}

public class DtoCar
{
public string Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}

public class DoCar
{
public string Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
}

0 comments on commit 354605d

Please sign in to comment.