Browse Source

Add EntityCache_Tests

pull/14055/head
LiangShiWei 4 years ago
parent
commit
11f8c7476d
  1. 5
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs
  2. 7
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/EntityCache_Tests.cs
  3. 5
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs
  4. 5
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs
  5. 2
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/AbpMongoDbTestModule.cs
  6. 9
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Domain/EntityCache_Tests.cs
  7. 3
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/ITestAppMongoDbContext.cs
  8. 3
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs
  9. 3
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/TestAutoMapProfile.cs
  10. 11
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs
  11. 93
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestMemoryDistributedCache.cs
  12. 133
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs

5
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs

@ -14,6 +14,8 @@ public static class EntityCacheServiceCollectionExtensions
IEntityCache<TEntity, TKey>,
EntityCacheWithoutCacheItem<TEntity, TKey>
>();
services
.TryAddTransient<EntityCacheWithoutCacheItem<TEntity, TKey>>();
return services;
}
@ -27,6 +29,8 @@ public static class EntityCacheServiceCollectionExtensions
IEntityCache<TEntityCacheItem, TKey>,
EntityCacheWithObjectMapper<TEntity, TEntityCacheItem, TKey>
>();
services
.TryAddTransient<EntityCacheWithObjectMapper<TEntity, TEntityCacheItem, TKey>>();
return services;
}
@ -40,6 +44,7 @@ public static class EntityCacheServiceCollectionExtensions
IEntityCache<TEntityCacheItem, TKey>,
EntityCacheWithObjectMapperContext<TObjectMapperContext, TEntity, TEntityCacheItem, TKey>
>();
services.TryAddTransient<EntityCacheWithObjectMapperContext<TObjectMapperContext, TEntity, TEntityCacheItem, TKey>>();
return services;
}
}

7
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/EntityCache_Tests.cs

@ -0,0 +1,7 @@
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.EntityFrameworkCore.Domain;
public class EntityCache_Tests : EntityCache_Tests<AbpEntityFrameworkCoreTestModule>
{
}

5
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs

@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext;
using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.EntityFrameworkCore;
@ -19,6 +20,8 @@ public class TestMigrationsDbContext : AbpDbContext<TestMigrationsDbContext>
public DbSet<EntityWithIntPk> EntityWithIntPks { get; set; }
public DbSet<Author> Author { get; set; }
public DbSet<Product> Products { get; set; }
public TestMigrationsDbContext(DbContextOptions<TestMigrationsDbContext> options)
: base(options)
@ -51,5 +54,7 @@ public class TestMigrationsDbContext : AbpDbContext<TestMigrationsDbContext>
d.HasKey(x => new { x.CityId, x.Name });
});
});
modelBuilder.Entity<Product>();
}
}

5
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs

@ -7,6 +7,7 @@ using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.EntityFrameworkCore.TestApp.FourthContext;
using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.TestApp.EntityFrameworkCore;
@ -28,6 +29,8 @@ public class TestAppDbContext : AbpDbContext<TestAppDbContext>, IThirdDbContext,
public DbSet<Author> Author { get; set; }
public DbSet<FourthDbContextDummyEntity> FourthDummyEntities { get; set; }
public DbSet<Product> Products { get; set; }
public TestAppDbContext(DbContextOptions<TestAppDbContext> options)
: base(options)
@ -79,6 +82,8 @@ public class TestAppDbContext : AbpDbContext<TestAppDbContext>, IThirdDbContext,
b.ApplyObjectExtensionMappings();
});
modelBuilder.Entity<Product>();
modelBuilder.TryConfigureObjectExtensions<TestAppDbContext>();
}
}

2
framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/AbpMongoDbTestModule.cs

@ -30,7 +30,7 @@ public class AbpMongoDbTestModule : AbpModule
context.Services.AddMongoDbContext<TestAppMongoDbContext>(options =>
{
options.AddDefaultRepositories<ITestAppMongoDbContext>();
options.AddDefaultRepositories<ITestAppMongoDbContext>(true);
options.AddRepository<City, CityRepository>();
options.ReplaceDbContext<IThirdDbContext>();

9
framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Domain/EntityCache_Tests.cs

@ -0,0 +1,9 @@
using Volo.Abp.TestApp.Testing;
using Xunit;
namespace Volo.Abp.MongoDB.Domain;
[Collection(MongoTestCollection.Name)]
public class EntityCache_Tests : EntityCache_Tests<AbpMongoDbTestModule>
{
}

3
framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/ITestAppMongoDbContext.cs

@ -2,6 +2,7 @@
using Volo.Abp.Data;
using Volo.Abp.MongoDB;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.TestApp.MongoDB;
@ -11,4 +12,6 @@ public interface ITestAppMongoDbContext : IAbpMongoDbContext
IMongoCollection<Person> People { get; }
IMongoCollection<City> Cities { get; }
IMongoCollection<Product> Products { get; }
}

3
framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs

@ -6,6 +6,7 @@ using Volo.Abp.MongoDB;
using Volo.Abp.MongoDB.TestApp.FourthContext;
using Volo.Abp.MongoDB.TestApp.ThirdDbContext;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.TestApp.MongoDB;
@ -23,6 +24,8 @@ public class TestAppMongoDbContext : AbpMongoDbContext, ITestAppMongoDbContext,
public IMongoCollection<ThirdDbContextDummyEntity> DummyEntities => Collection<ThirdDbContextDummyEntity>();
public IMongoCollection<FourthDbContextDummyEntity> FourthDummyEntities => Collection<FourthDbContextDummyEntity>();
public IMongoCollection<Product> Products => Collection<Product>();
protected internal override void CreateModel(IMongoModelBuilder modelBuilder)
{

3
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/TestAutoMapProfile.cs

@ -1,4 +1,5 @@
using AutoMapper;
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.TestApp.Domain;
@ -7,5 +8,7 @@ public class TestAutoMapProfile : Profile
public TestAutoMapProfile()
{
CreateMap<PersonEto, Person>().ReverseMap();
CreateMap<Product, ProductCacheItem>();
}
}

11
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs

@ -1,11 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.Application;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.AutoMapper;
using Volo.Abp.Domain.Entities.Caching;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.TestApp.Application.Dto;
using Volo.Abp.TestApp.Testing;
using Volo.Abp.Threading;
namespace Volo.Abp.TestApp;
@ -22,6 +27,10 @@ public class TestAppModule : AbpModule
{
ConfigureAutoMapper();
ConfigureDistributedEventBus();
context.Services.Replace(ServiceDescriptor.Singleton<IDistributedCache, TestMemoryDistributedCache>());
context.Services.AddEntityCache<Product, Guid>();
context.Services.AddEntityCache<Product, ProductCacheItem, Guid>();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)

93
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestMemoryDistributedCache.cs

@ -0,0 +1,93 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.TestApp;
[DisableConventionalRegistration]
public class TestMemoryDistributedCache : MemoryDistributedCache, ICacheSupportsMultipleItems
{
public TestMemoryDistributedCache(IOptions<MemoryDistributedCacheOptions> optionsAccessor)
: base(optionsAccessor)
{
}
public TestMemoryDistributedCache(IOptions<MemoryDistributedCacheOptions> optionsAccessor, ILoggerFactory loggerFactory)
: base(optionsAccessor, loggerFactory)
{
}
public byte[][] GetMany(IEnumerable<string> keys)
{
var values = new List<byte[]>();
foreach (var key in keys)
{
values.Add(Get(key));
}
return values.ToArray();
}
public async Task<byte[][]> GetManyAsync(IEnumerable<string> keys, CancellationToken token = default)
{
var values = new List<byte[]>();
foreach (var key in keys)
{
values.Add(await GetAsync(key, token));
}
return values.ToArray();
}
public void SetMany(IEnumerable<KeyValuePair<string, byte[]>> items, DistributedCacheEntryOptions options)
{
foreach (var item in items)
{
Set(item.Key, item.Value, options);
}
}
public async Task SetManyAsync(IEnumerable<KeyValuePair<string, byte[]>> items, DistributedCacheEntryOptions options, CancellationToken token = default)
{
foreach (var item in items)
{
await SetAsync(item.Key, item.Value, options, token);
}
}
public void RefreshMany(IEnumerable<string> keys)
{
foreach (var key in keys)
{
Refresh(key);
}
}
public async Task RefreshManyAsync(IEnumerable<string> keys, CancellationToken token = default)
{
foreach (var key in keys)
{
await RefreshAsync(key, token);
}
}
public void RemoveMany(IEnumerable<string> keys)
{
foreach (var key in keys)
{
Remove(key);
}
}
public async Task RemoveManyAsync(IEnumerable<string> keys, CancellationToken token = default)
{
foreach (var key in keys)
{
await RemoveAsync(key, token);
}
}
}

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

@ -0,0 +1,133 @@
using System;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Entities.Caching;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;
using Xunit;
namespace Volo.Abp.TestApp.Testing;
public class EntityCache_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
protected readonly IRepository<Product, Guid> ProductRepository;
protected readonly IEntityCache<Product, Guid> ProductEntityCache;
protected readonly IEntityCache<ProductCacheItem, Guid> ProductCacheItem;
public EntityCache_Tests()
{
ProductRepository = GetRequiredService<IRepository<Product, Guid>>();
ProductEntityCache = GetRequiredService<IEntityCache<Product, Guid>>();
ProductCacheItem = GetRequiredService<IEntityCache<ProductCacheItem, Guid>>();
}
[Fact]
public async Task Should_Return_Null_IF_Entity_Not_Exist()
{
var notExistId = Guid.NewGuid();
(await ProductEntityCache.FindAsync(notExistId)).ShouldBeNull();
(await ProductCacheItem.FindAsync(notExistId)).ShouldBeNull();
}
[Fact]
public async Task Should_Throw_EntityNotFoundException_IF_Entity_Not_Exist()
{
var notExistId = Guid.NewGuid();
await Assert.ThrowsAsync<EntityNotFoundException>(() => ProductEntityCache.GetAsync(notExistId));
await Assert.ThrowsAsync<EntityNotFoundException>(() => ProductCacheItem.GetAsync(notExistId));
}
[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);
product.ShouldNotBeNull();
product.Id.ShouldBe(id);
product.Name.ShouldBe("Product1");
product.Price.ShouldBe(decimal.One);
var productCacheItem = await ProductCacheItem.FindAsync(product.Id);
productCacheItem.ShouldNotBeNull();
productCacheItem.Id.ShouldBe(id);
productCacheItem.Name.ShouldBe("Product1");
productCacheItem.Price.ShouldBe(decimal.One);
}
[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 ProductEntityCache.FindAsync(product.Id)).ShouldBeNull();
(await ProductCacheItem.FindAsync(product.Id)).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();
product.Name = "Product2";
product.Price = decimal.Zero;
await ProductRepository.UpdateAsync(product);
product = await ProductEntityCache.FindAsync(product.Id);
product.ShouldNotBeNull();
product.Id.ShouldBe(id);
product.Name.ShouldBe("Product2");
product.Price.ShouldBe(decimal.Zero);
var productCacheItem = await ProductCacheItem.FindAsync(product.Id);
productCacheItem.ShouldNotBeNull();
productCacheItem.Id.ShouldBe(id);
productCacheItem.Name.ShouldBe("Product2");
productCacheItem.Price.ShouldBe(decimal.Zero);
}
}
[Serializable]
public class Product : Entity<Guid>
{
public Product()
{
}
public Product(Guid id, string name, decimal price)
{
Id = id;
Name = name;
Price = price;
}
[JsonInclude]
public override Guid Id { get; protected set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
[Serializable]
public class ProductCacheItem
{
public Guid Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Loading…
Cancel
Save