From 04e86d0686ede2ce7ba32c6bbc3dbea750e22b12 Mon Sep 17 00:00:00 2001 From: LiangShiWei Date: Wed, 28 Sep 2022 11:05:20 +0800 Subject: [PATCH] Update EntityCache_Tests --- .../Volo/Abp/TestApp/TestDataBuilder.cs | 14 +++++- .../Abp/TestApp/Testing/EntityCache_Tests.cs | 44 +++++++------------ 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs index c31363bc98..de2e95895c 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; using Volo.Abp.TestApp.Domain; +using Volo.Abp.TestApp.Testing; namespace Volo.Abp.TestApp; @@ -15,19 +16,24 @@ public class TestDataBuilder : ITransientDependency public static Guid IstanbulCityId { get; } = new Guid("4d734a0e-3e6b-4bad-bb43-ef8cf1b09633"); public static Guid LondonCityId { get; } = new Guid("27237527-605e-4652-a2a5-68e0e512da36"); + + public static Guid ProductId { get; } = new Guid("c5b3fbd4-7b0b-4a72-8d99-ccf6c8c98f61"); private readonly IBasicRepository _personRepository; private readonly ICityRepository _cityRepository; private readonly IRepository _entityWithIntPksRepository; + private readonly IRepository _productRepository; public TestDataBuilder( IBasicRepository personRepository, ICityRepository cityRepository, - IRepository entityWithIntPksRepository) + IRepository entityWithIntPksRepository, + IRepository productRepository) { _personRepository = personRepository; _cityRepository = cityRepository; _entityWithIntPksRepository = entityWithIntPksRepository; + _productRepository = productRepository; } public async Task BuildAsync() @@ -35,6 +41,7 @@ public class TestDataBuilder : ITransientDependency await AddCities(); await AddPeople(); await AddEntitiesWithPks(); + await AddProducts(); } private async Task AddCities() @@ -87,4 +94,9 @@ public class TestDataBuilder : ITransientDependency { await _entityWithIntPksRepository.InsertAsync(new EntityWithIntPk("Entity1")); } + + private async Task AddProducts() + { + await _productRepository.InsertAsync(new Product(ProductId, "Product1", decimal.One)); + } } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs index e244e6f79b..4d1f810705 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs @@ -10,14 +10,14 @@ using Xunit; namespace Volo.Abp.TestApp.Testing; -public class EntityCache_Tests : TestAppTestBase +public abstract class EntityCache_Tests : TestAppTestBase where TStartupModule : IAbpModule { protected readonly IRepository ProductRepository; protected readonly IEntityCache ProductEntityCache; protected readonly IEntityCache ProductCacheItem; - public EntityCache_Tests() + protected EntityCache_Tests() { ProductRepository = GetRequiredService>(); ProductEntityCache = GetRequiredService>(); @@ -43,18 +43,15 @@ public class EntityCache_Tests : TestAppTestBase [Fact] public async Task Should_Return_EntityCache() { - var id = Guid.NewGuid(); - var product = await ProductRepository.InsertAsync(new Product(id, "Product1", decimal.One)); - - product = await ProductEntityCache.FindAsync(product.Id); + var product = await ProductEntityCache.FindAsync(TestDataBuilder.ProductId); product.ShouldNotBeNull(); - product.Id.ShouldBe(id); + product.Id.ShouldBe(TestDataBuilder.ProductId); product.Name.ShouldBe("Product1"); product.Price.ShouldBe(decimal.One); var productCacheItem = await ProductCacheItem.FindAsync(product.Id); productCacheItem.ShouldNotBeNull(); - productCacheItem.Id.ShouldBe(id); + productCacheItem.Id.ShouldBe(TestDataBuilder.ProductId); productCacheItem.Name.ShouldBe("Product1"); productCacheItem.Price.ShouldBe(decimal.One); } @@ -62,38 +59,32 @@ public class EntityCache_Tests : TestAppTestBase [Fact] public async Task Should_Return_Null_IF_Deleted() { - var id = Guid.NewGuid(); - var product = await ProductRepository.InsertAsync(new Product(id, "Product1", decimal.One)); - (await ProductEntityCache.FindAsync(id)).ShouldNotBeNull(); - (await ProductCacheItem.FindAsync(id)).ShouldNotBeNull(); - - await ProductRepository.DeleteAsync(id); + await ProductRepository.DeleteAsync(TestDataBuilder.ProductId); - (await ProductEntityCache.FindAsync(product.Id)).ShouldBeNull(); - (await ProductCacheItem.FindAsync(product.Id)).ShouldBeNull(); + (await ProductEntityCache.FindAsync(TestDataBuilder.ProductId)).ShouldBeNull(); + (await ProductCacheItem.FindAsync(TestDataBuilder.ProductId)).ShouldBeNull(); } [Fact] public async Task Should_Return_New_EntityCache_IF_Updated() { - var id = Guid.NewGuid(); - var product = await ProductRepository.InsertAsync(new Product(id, "Product1", decimal.One)); - (await ProductEntityCache.FindAsync(id)).ShouldNotBeNull(); - (await ProductCacheItem.FindAsync(id)).ShouldNotBeNull(); - + (await ProductEntityCache.FindAsync(TestDataBuilder.ProductId)).ShouldNotBeNull(); + (await ProductCacheItem.FindAsync(TestDataBuilder.ProductId)).ShouldNotBeNull(); + + var product = await ProductRepository.FindAsync(TestDataBuilder.ProductId); product.Name = "Product2"; product.Price = decimal.Zero; await ProductRepository.UpdateAsync(product); product = await ProductEntityCache.FindAsync(product.Id); product.ShouldNotBeNull(); - product.Id.ShouldBe(id); + product.Id.ShouldBe(TestDataBuilder.ProductId); product.Name.ShouldBe("Product2"); product.Price.ShouldBe(decimal.Zero); var productCacheItem = await ProductCacheItem.FindAsync(product.Id); productCacheItem.ShouldNotBeNull(); - productCacheItem.Id.ShouldBe(id); + productCacheItem.Id.ShouldBe(TestDataBuilder.ProductId); productCacheItem.Name.ShouldBe("Product2"); productCacheItem.Price.ShouldBe(decimal.Zero); } @@ -102,14 +93,9 @@ public class EntityCache_Tests : TestAppTestBase [Serializable] public class Product : Entity { - public Product() - { - - } - public Product(Guid id, string name, decimal price) + : base(id) { - Id = id; Name = name; Price = price; }