Browse Source

Update EntityCache_Tests

pull/14055/head
LiangShiWei 4 years ago
parent
commit
04e86d0686
  1. 14
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs
  2. 44
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs

14
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.DependencyInjection;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.TestApp.Domain; using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.TestApp; 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 IstanbulCityId { get; } = new Guid("4d734a0e-3e6b-4bad-bb43-ef8cf1b09633");
public static Guid LondonCityId { get; } = new Guid("27237527-605e-4652-a2a5-68e0e512da36"); 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<Person, Guid> _personRepository; private readonly IBasicRepository<Person, Guid> _personRepository;
private readonly ICityRepository _cityRepository; private readonly ICityRepository _cityRepository;
private readonly IRepository<EntityWithIntPk, int> _entityWithIntPksRepository; private readonly IRepository<EntityWithIntPk, int> _entityWithIntPksRepository;
private readonly IRepository<Product, Guid> _productRepository;
public TestDataBuilder( public TestDataBuilder(
IBasicRepository<Person, Guid> personRepository, IBasicRepository<Person, Guid> personRepository,
ICityRepository cityRepository, ICityRepository cityRepository,
IRepository<EntityWithIntPk, int> entityWithIntPksRepository) IRepository<EntityWithIntPk, int> entityWithIntPksRepository,
IRepository<Product, Guid> productRepository)
{ {
_personRepository = personRepository; _personRepository = personRepository;
_cityRepository = cityRepository; _cityRepository = cityRepository;
_entityWithIntPksRepository = entityWithIntPksRepository; _entityWithIntPksRepository = entityWithIntPksRepository;
_productRepository = productRepository;
} }
public async Task BuildAsync() public async Task BuildAsync()
@ -35,6 +41,7 @@ public class TestDataBuilder : ITransientDependency
await AddCities(); await AddCities();
await AddPeople(); await AddPeople();
await AddEntitiesWithPks(); await AddEntitiesWithPks();
await AddProducts();
} }
private async Task AddCities() private async Task AddCities()
@ -87,4 +94,9 @@ public class TestDataBuilder : ITransientDependency
{ {
await _entityWithIntPksRepository.InsertAsync(new EntityWithIntPk("Entity1")); await _entityWithIntPksRepository.InsertAsync(new EntityWithIntPk("Entity1"));
} }
private async Task AddProducts()
{
await _productRepository.InsertAsync(new Product(ProductId, "Product1", decimal.One));
}
} }

44
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs

@ -10,14 +10,14 @@ using Xunit;
namespace Volo.Abp.TestApp.Testing; namespace Volo.Abp.TestApp.Testing;
public class EntityCache_Tests<TStartupModule> : TestAppTestBase<TStartupModule> public abstract class EntityCache_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
where TStartupModule : IAbpModule where TStartupModule : IAbpModule
{ {
protected readonly IRepository<Product, Guid> ProductRepository; protected readonly IRepository<Product, Guid> ProductRepository;
protected readonly IEntityCache<Product, Guid> ProductEntityCache; protected readonly IEntityCache<Product, Guid> ProductEntityCache;
protected readonly IEntityCache<ProductCacheItem, Guid> ProductCacheItem; protected readonly IEntityCache<ProductCacheItem, Guid> ProductCacheItem;
public EntityCache_Tests() protected EntityCache_Tests()
{ {
ProductRepository = GetRequiredService<IRepository<Product, Guid>>(); ProductRepository = GetRequiredService<IRepository<Product, Guid>>();
ProductEntityCache = GetRequiredService<IEntityCache<Product, Guid>>(); ProductEntityCache = GetRequiredService<IEntityCache<Product, Guid>>();
@ -43,18 +43,15 @@ public class EntityCache_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
[Fact] [Fact]
public async Task Should_Return_EntityCache() public async Task Should_Return_EntityCache()
{ {
var id = Guid.NewGuid(); var product = await ProductEntityCache.FindAsync(TestDataBuilder.ProductId);
var product = await ProductRepository.InsertAsync(new Product(id, "Product1", decimal.One));
product = await ProductEntityCache.FindAsync(product.Id);
product.ShouldNotBeNull(); product.ShouldNotBeNull();
product.Id.ShouldBe(id); product.Id.ShouldBe(TestDataBuilder.ProductId);
product.Name.ShouldBe("Product1"); product.Name.ShouldBe("Product1");
product.Price.ShouldBe(decimal.One); product.Price.ShouldBe(decimal.One);
var productCacheItem = await ProductCacheItem.FindAsync(product.Id); var productCacheItem = await ProductCacheItem.FindAsync(product.Id);
productCacheItem.ShouldNotBeNull(); productCacheItem.ShouldNotBeNull();
productCacheItem.Id.ShouldBe(id); productCacheItem.Id.ShouldBe(TestDataBuilder.ProductId);
productCacheItem.Name.ShouldBe("Product1"); productCacheItem.Name.ShouldBe("Product1");
productCacheItem.Price.ShouldBe(decimal.One); productCacheItem.Price.ShouldBe(decimal.One);
} }
@ -62,38 +59,32 @@ public class EntityCache_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
[Fact] [Fact]
public async Task Should_Return_Null_IF_Deleted() public async Task Should_Return_Null_IF_Deleted()
{ {
var id = Guid.NewGuid(); await ProductRepository.DeleteAsync(TestDataBuilder.ProductId);
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 ProductEntityCache.FindAsync(product.Id)).ShouldBeNull(); (await ProductEntityCache.FindAsync(TestDataBuilder.ProductId)).ShouldBeNull();
(await ProductCacheItem.FindAsync(product.Id)).ShouldBeNull(); (await ProductCacheItem.FindAsync(TestDataBuilder.ProductId)).ShouldBeNull();
} }
[Fact] [Fact]
public async Task Should_Return_New_EntityCache_IF_Updated() public async Task Should_Return_New_EntityCache_IF_Updated()
{ {
var id = Guid.NewGuid(); (await ProductEntityCache.FindAsync(TestDataBuilder.ProductId)).ShouldNotBeNull();
var product = await ProductRepository.InsertAsync(new Product(id, "Product1", decimal.One)); (await ProductCacheItem.FindAsync(TestDataBuilder.ProductId)).ShouldNotBeNull();
(await ProductEntityCache.FindAsync(id)).ShouldNotBeNull();
(await ProductCacheItem.FindAsync(id)).ShouldNotBeNull(); var product = await ProductRepository.FindAsync(TestDataBuilder.ProductId);
product.Name = "Product2"; product.Name = "Product2";
product.Price = decimal.Zero; product.Price = decimal.Zero;
await ProductRepository.UpdateAsync(product); await ProductRepository.UpdateAsync(product);
product = await ProductEntityCache.FindAsync(product.Id); product = await ProductEntityCache.FindAsync(product.Id);
product.ShouldNotBeNull(); product.ShouldNotBeNull();
product.Id.ShouldBe(id); product.Id.ShouldBe(TestDataBuilder.ProductId);
product.Name.ShouldBe("Product2"); product.Name.ShouldBe("Product2");
product.Price.ShouldBe(decimal.Zero); product.Price.ShouldBe(decimal.Zero);
var productCacheItem = await ProductCacheItem.FindAsync(product.Id); var productCacheItem = await ProductCacheItem.FindAsync(product.Id);
productCacheItem.ShouldNotBeNull(); productCacheItem.ShouldNotBeNull();
productCacheItem.Id.ShouldBe(id); productCacheItem.Id.ShouldBe(TestDataBuilder.ProductId);
productCacheItem.Name.ShouldBe("Product2"); productCacheItem.Name.ShouldBe("Product2");
productCacheItem.Price.ShouldBe(decimal.Zero); productCacheItem.Price.ShouldBe(decimal.Zero);
} }
@ -102,14 +93,9 @@ public class EntityCache_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
[Serializable] [Serializable]
public class Product : Entity<Guid> public class Product : Entity<Guid>
{ {
public Product()
{
}
public Product(Guid id, string name, decimal price) public Product(Guid id, string name, decimal price)
: base(id)
{ {
Id = id;
Name = name; Name = name;
Price = price; Price = price;
} }

Loading…
Cancel
Save