mirror of https://github.com/abpframework/abp.git
committed by
GitHub
10 changed files with 225 additions and 2 deletions
@ -0,0 +1,17 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.ValueConverters |
|||
{ |
|||
public class AbpDateTimeValueConverter : ValueConverter<DateTime?, DateTime?> |
|||
{ |
|||
public AbpDateTimeValueConverter(IClock clock, [CanBeNull] ConverterMappingHints mappingHints = null) |
|||
: base( |
|||
x => x.HasValue ? clock.Normalize(x.Value) : x, |
|||
x => x.HasValue ? clock.Normalize(x.Value) : x, mappingHints) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.TestApp.Testing; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.ValueConverters |
|||
{ |
|||
public class AbpDateTimeValueConverter_Tests : AbpDateTimeValueConverter_Tests<AbpEntityFrameworkCoreTestModule> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Volo.Abp.Domain.Repositories.EntityFrameworkCore; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
using Volo.Abp.TestApp.Domain; |
|||
|
|||
namespace Volo.Abp.TestApp.EntityFrameworkCore |
|||
{ |
|||
public class PersonRepository : EfCoreRepository<TestAppDbContext, Person, Guid>, IPersonRepository |
|||
{ |
|||
public PersonRepository(IDbContextProvider<TestAppDbContext> dbContextProvider) |
|||
: base(dbContextProvider) |
|||
{ |
|||
} |
|||
|
|||
public async Task<PersonView> GetViewAsync(string name) |
|||
{ |
|||
return await DbContext.PersonView.Where(x => x.Name == name).FirstOrDefaultAsync(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace Volo.Abp.TestApp.Domain |
|||
{ |
|||
public interface IPersonRepository : IBasicRepository<Person, Guid> |
|||
{ |
|||
Task<PersonView> GetViewAsync(string name); |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
using System; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace Volo.Abp.TestApp.Domain |
|||
{ |
|||
public class PersonView |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public DateTime CreationTime { get; set; } |
|||
|
|||
public DateTime? Birthday { get; set; } |
|||
|
|||
[DisableDateTimeNormalization] |
|||
public DateTime? LastActive { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Volo.Abp.Timing; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.TestApp.Testing |
|||
{ |
|||
public abstract class AbpDateTimeValueConverter_Tests<TStartupModule> : TestAppTestBase<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
private readonly IPersonRepository _personRepository; |
|||
|
|||
protected AbpDateTimeValueConverter_Tests() |
|||
{ |
|||
_personRepository = GetRequiredService<IPersonRepository>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task DateTime_Kind_Should_Be_Normalized_To_UTC_Test() |
|||
{ |
|||
var personId = Guid.Parse("4125582e-d100-4c27-aa84-e4de85830dca"); |
|||
await _personRepository.InsertAsync(new Person(personId, "bob lee", 18) |
|||
{ |
|||
Birthday = DateTime.Parse("2020-01-01 00:00:00"), |
|||
LastActive = DateTime.Parse("2020-01-01 00:00:00"), |
|||
}, true); |
|||
|
|||
var person = await _personRepository.GetAsync(personId); |
|||
|
|||
person.ShouldNotBeNull(); |
|||
person.CreationTime.Kind.ShouldBe(DateTimeKind.Utc); |
|||
|
|||
person.Birthday.ShouldNotBeNull(); |
|||
person.Birthday.Value.Kind.ShouldBe(DateTimeKind.Utc); |
|||
person.Birthday.Value.ToString("yyy-MM-dd HH:mm:ss").ShouldBe("2020-01-01 00:00:00"); |
|||
|
|||
//LastActive DisableDateTimeNormalization
|
|||
person.LastActive.ShouldNotBeNull(); |
|||
person.LastActive.Value.Kind.ShouldBe(DateTimeKind.Unspecified); |
|||
person.LastActive.Value.ToString("yyy-MM-dd HH:mm:ss").ShouldBe("2020-01-01 00:00:00"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task DateTime_Kind_Should_Be_Normalized_To_UTC_View_Test() |
|||
{ |
|||
var personName = "bob lee"; |
|||
await _personRepository.InsertAsync(new Person(Guid.NewGuid(), personName, 18) |
|||
{ |
|||
Birthday = DateTime.Parse("2020-01-01 00:00:00"), |
|||
LastActive = DateTime.Parse("2020-01-01 00:00:00"), |
|||
}, true); |
|||
|
|||
var person = await _personRepository.GetViewAsync(personName); |
|||
|
|||
person.ShouldNotBeNull(); |
|||
person.CreationTime.Kind.ShouldBe(DateTimeKind.Utc); |
|||
|
|||
person.Birthday.ShouldNotBeNull(); |
|||
person.Birthday.Value.Kind.ShouldBe(DateTimeKind.Utc); |
|||
person.Birthday.Value.ToString("yyy-MM-dd HH:mm:ss").ShouldBe("2020-01-01 00:00:00"); |
|||
|
|||
//LastActive DisableDateTimeNormalization
|
|||
person.LastActive.ShouldNotBeNull(); |
|||
person.LastActive.Value.Kind.ShouldBe(DateTimeKind.Unspecified); |
|||
person.LastActive.Value.ToString("yyy-MM-dd HH:mm:ss").ShouldBe("2020-01-01 00:00:00"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue