From b4940f135cf6cb122b12267f0a693b11807865d0 Mon Sep 17 00:00:00 2001 From: maliming Date: Sun, 10 Nov 2019 14:28:06 +0800 Subject: [PATCH] Added Keyless Entity Types unit test. https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types --- .../AbpEntityFrameworkCoreTestModule.cs | 3 ++ .../EntityFrameworkCore/PersonRepository.cs | 24 +++++++++++++++ .../EntityFrameworkCore/TestAppDbContext.cs | 9 ++++++ .../Abp/TestApp/Domain/IPersonRepository.cs | 13 +++++++++ .../Volo/Abp/TestApp/Domain/PersonView.cs | 19 ++++++++++++ .../AbpDateTimeValueConverter_Tests.cs | 29 +++++++++++++++++-- 6 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/PersonRepository.cs create mode 100644 framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/IPersonRepository.cs create mode 100644 framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/PersonView.cs diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs index 85eb1119d5..8925c2bff3 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs @@ -60,6 +60,9 @@ namespace Volo.Abp.EntityFrameworkCore using (var context = new TestMigrationsDbContext(new DbContextOptionsBuilder().UseSqlite(connection).Options)) { context.GetService().CreateTables(); + context.Database.ExecuteSqlRaw( + @"CREATE VIEW View_PersonView AS + SELECT Id, Name, CreationTime, Birthday, LastActive FROM People"); } return connection; diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/PersonRepository.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/PersonRepository.cs new file mode 100644 index 0000000000..e42d88c2b8 --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/PersonRepository.cs @@ -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, IPersonRepository + { + public PersonRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + } + + public async Task GetViewAsync(Guid id) + { + return await DbContext.PersonView.Where(x => x.Id == id).FirstOrDefaultAsync(); + } + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs index bfcecdeb65..5e243b9a03 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs @@ -11,6 +11,8 @@ namespace Volo.Abp.TestApp.EntityFrameworkCore public DbSet Cities { get; set; } + public DbSet PersonView { get; set; } + public DbSet DummyEntities { get; set; } public DbSet EntityWithIntPks { get; set; } @@ -29,6 +31,13 @@ namespace Volo.Abp.TestApp.EntityFrameworkCore { b.HasKey(p => new {p.PersonId, p.Number}); }); + + modelBuilder + .Entity(p => + { + p.HasNoKey(); + p.ToView("View_PersonView"); + }); } } } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/IPersonRepository.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/IPersonRepository.cs new file mode 100644 index 0000000000..ac094de826 --- /dev/null +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/IPersonRepository.cs @@ -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 + { + Task GetViewAsync(Guid id); + } +} diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/PersonView.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/PersonView.cs new file mode 100644 index 0000000000..72249fa541 --- /dev/null +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/PersonView.cs @@ -0,0 +1,19 @@ +using System; +using Volo.Abp.Timing; + +namespace Volo.Abp.TestApp.Domain +{ + public class PersonView + { + public Guid Id { get; set; } + + public string Name { get; set; } + + public DateTime CreationTime { get; set; } + + public DateTime? Birthday { get; set; } + + [DisableDateTimeNormalization] + public DateTime? LastActive { get; set; } + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/AbpDateTimeValueConverter_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/AbpDateTimeValueConverter_Tests.cs index 6b0caaa7b6..bf95982daa 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/AbpDateTimeValueConverter_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/AbpDateTimeValueConverter_Tests.cs @@ -13,11 +13,11 @@ namespace Volo.Abp.TestApp.Testing public abstract class AbpDateTimeValueConverter_Tests : TestAppTestBase where TStartupModule : IAbpModule { - private readonly IBasicRepository _personRepository; + private readonly IPersonRepository _personRepository; protected AbpDateTimeValueConverter_Tests() { - _personRepository = GetRequiredService>(); + _personRepository = GetRequiredService(); } [Fact] @@ -44,5 +44,30 @@ namespace Volo.Abp.TestApp.Testing 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 personId = Guid.Parse("7a942bca-c911-4473-93aa-2daf88e18fb9"); + 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.GetViewAsync(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"); + } } } \ No newline at end of file