Browse Source

Add unit tests

pull/5774/head
liangshiwei 6 years ago
parent
commit
6319b22753
  1. 112
      framework/test/Volo.Abp.AutoMapper.Tests/AutoMapper/AbpAutoMapperMultiLingualDtoExtensions_Tests.cs
  2. 15
      framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapperTestModule.cs

112
framework/test/Volo.Abp.AutoMapper.Tests/AutoMapper/AbpAutoMapperMultiLingualDtoExtensions_Tests.cs

@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.AutoMapper;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Settings;
using Volo.Abp.Testing;
using Xunit;
namespace AutoMapper
{
public class AbpAutoMapperMultiLingualDtoExtensions_Tests : AbpIntegratedTest<AutoMapperTestModule>
{
private readonly Volo.Abp.ObjectMapping.IObjectMapper _objectMapper;
private readonly Book _book;
public AbpAutoMapperMultiLingualDtoExtensions_Tests()
{
_objectMapper = ServiceProvider.GetRequiredService<Volo.Abp.ObjectMapping.IObjectMapper>();
var id = Guid.NewGuid();
_book = new Book(id, 100)
{
Translations = new List<BookTranslation>
{
new BookTranslation
{
CoreId = id,
Language = "en",
Name = "C# in Depth"
},
new BookTranslation
{
CoreId = id,
Language = "zh-Hans",
Name = "深入理解C#"
}
}
};
}
[Fact]
public void Should_Map_CurrentUICulture()
{
CultureInfo.CurrentCulture = new CultureInfo("en");
var bookDto = _objectMapper.Map<Book, BookDto>(_book);
bookDto.Name.ShouldBe("C# in Depth");
bookDto.Price.ShouldBe(_book.Price);
bookDto.Id.ShouldBe(_book.Id);
}
[Fact]
public void Should_Map_FeedbackUICulture()
{
CultureInfo.CurrentCulture = new CultureInfo("en-us");
var bookDto = _objectMapper.Map<Book, BookDto>(_book);
bookDto.Name.ShouldBe("C# in Depth");
bookDto.Price.ShouldBe(100);
bookDto.Id.ShouldBe(_book.Id);
}
}
public class Book : Entity<Guid>, IMultiLingualEntity<BookTranslation>
{
public Book(Guid id, decimal price)
{
Id = id;
Price = price;
}
public decimal Price { get; set; }
public ICollection<BookTranslation> Translations { get; set; }
}
public class BookTranslation : IEntityTranslation<Book, Guid>
{
public string Name { get; set; }
public string Language { get; set; }
public Book Core { get; set; }
public Guid CoreId { get; set; }
}
public class BookDto
{
public string Name { get; set; }
public Guid Id { get; set; }
public decimal Price { get; set; }
}
public class BookProfile : Profile
{
public BookProfile()
{
}
public BookProfile(ISettingProvider settingsProvider)
{
this.CreateMultLingualMap<Book, BookTranslation, BookDto>(settingsProvider, true).TranslationMap
.ForMember(d => d.Id, m => m.MapFrom(s => s.CoreId));
}
}
}

15
framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapperTestModule.cs

@ -1,12 +1,15 @@
using Volo.Abp.Modularity;
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Settings;
namespace Volo.Abp.AutoMapper
{
[DependsOn(
typeof(AbpAutoMapperModule),
typeof(AbpObjectExtendingTestModule)
)]
)]
public class AutoMapperTestModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
@ -14,7 +17,13 @@ namespace Volo.Abp.AutoMapper
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<AutoMapperTestModule>();
options.Configurators.Add(configurationContext =>
{
configurationContext.MapperConfiguration.AddProfile(
new BookProfile(configurationContext.ServiceProvider.GetService<ISettingProvider>()));
});
});
}
}
}
}

Loading…
Cancel
Save