Browse Source

Add test & refactor for EntityExtensionManager

pull/3360/head
Halil İbrahim Kalkan 6 years ago
parent
commit
2863b5cb5d
  1. 4
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 9
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Extensions/EntityExtensionManager.cs
  3. 2
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs
  4. 6
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs
  5. 25
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs
  6. 22
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/TestEntityExtensionConfigurator.cs
  7. 3
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs
  8. 3
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs
  9. 4
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs
  10. 1
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/ExtraProperties_Tests.cs

4
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

@ -183,7 +183,7 @@ namespace Volo.Abp.EntityFrameworkCore
return;
}
var propertyNames = EntityExtensions.GetPropertyNames(entityType);
var propertyNames = EntityExtensionManager.GetPropertyNames(entityType);
foreach (var propertyName in propertyNames)
{
@ -241,7 +241,7 @@ namespace Volo.Abp.EntityFrameworkCore
return;
}
var propertyNames = EntityExtensions.GetPropertyNames(entityType);
var propertyNames = EntityExtensionManager.GetPropertyNames(entityType);
foreach (var propertyName in propertyNames)
{

9
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Extensions/EntityExtensions.cs → framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Extensions/EntityExtensionManager.cs

@ -28,27 +28,26 @@ namespace Volo.Abp.EntityFrameworkCore.Extensions
}
}
public static class EntityExtensions
public static class EntityExtensionManager
{
private static readonly Dictionary<Type, EntityExtensionInfo> ExtensionInfos;
//TODO: Use PropertyBuilder<TProperty> instead
static EntityExtensions()
static EntityExtensionManager()
{
ExtensionInfos = new Dictionary<Type, EntityExtensionInfo>();
}
public static void AddProperty<TEntity>(
public static void AddProperty<TEntity, TProperty>(
string name,
Type propertyType,
Action<PropertyBuilder> propertyBuildAction)
{
var extensionInfo = ExtensionInfos
.GetOrAdd(typeof(TEntity), () => new EntityExtensionInfo());
var propertyExtensionInfo = extensionInfo.Properties
.GetOrAdd(name, () => new PropertyExtensionInfo(propertyType));
.GetOrAdd(name, () => new PropertyExtensionInfo(typeof(TProperty)));
propertyExtensionInfo.Actions.Add(propertyBuildAction);
}

2
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs

@ -22,7 +22,7 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters
if (entityType != null)
{
var propertyNames = EntityExtensions.GetPropertyNames(entityType);
var propertyNames = EntityExtensionManager.GetPropertyNames(entityType);
foreach (var propertyName in propertyNames)
{

6
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs

@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Autofac;
using Volo.Abp.EntityFrameworkCore.Domain;
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext;
using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext;
using Volo.Abp.Modularity;
@ -21,6 +22,11 @@ namespace Volo.Abp.EntityFrameworkCore
[DependsOn(typeof(AbpEfCoreTestSecondContextModule))]
public class AbpEntityFrameworkCoreTestModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
TestEntityExtensionConfigurator.Configure();
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpDbContext<TestAppDbContext>(options =>

25
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs

@ -1,9 +1,32 @@
using Volo.Abp.TestApp.Testing;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.TestApp.Testing;
using Xunit;
namespace Volo.Abp.EntityFrameworkCore.Domain
{
public class ExtraProperties_Tests : ExtraProperties_Tests<AbpEntityFrameworkCoreTestModule>
{
[Fact]
public async Task Should_Get_An_Extra_Property_Configured_As_Extension()
{
var london = await CityRepository.FindByNameAsync("London");
london.HasProperty("PhoneCode").ShouldBeTrue();
london.GetProperty<string>("PhoneCode").ShouldBe("42");
}
[Fact]
public async Task Should_Update_An_Existing_Extra_Property_Configured_As_Extension()
{
var london = await CityRepository.FindByNameAsync("London");
london.GetProperty<string>("PhoneCode").ShouldBe("42");
london.ExtraProperties["PhoneCode"] = "53";
await CityRepository.UpdateAsync(london);
var london2 = await CityRepository.FindByNameAsync("London");
london2.GetProperty<string>("PhoneCode").ShouldBe("53");
}
}
}

22
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/TestEntityExtensionConfigurator.cs

@ -0,0 +1,22 @@
using Volo.Abp.EntityFrameworkCore.Extensions;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.Threading;
namespace Volo.Abp.EntityFrameworkCore.Domain
{
public static class TestEntityExtensionConfigurator
{
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
public static void Configure()
{
OneTimeRunner.Run(() =>
{
EntityExtensionManager.AddProperty<City, string>(
"PhoneCode",
p => p.HasMaxLength(8)
);
});
}
}
}

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

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Extensions;
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext;
using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext;
using Volo.Abp.TestApp.Domain;
@ -36,6 +37,8 @@ namespace Volo.Abp.EntityFrameworkCore
modelBuilder.Entity<City>(b =>
{
EntityExtensionManager.ConfigureProperties<City>(b);
b.OwnsMany(c => c.Districts, d =>
{
d.WithOwner().HasForeignKey(x => x.CityId);

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

@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Extensions;
using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext;
using Volo.Abp.TestApp.Domain;
@ -43,6 +44,8 @@ namespace Volo.Abp.TestApp.EntityFrameworkCore
modelBuilder.Entity<City>(b =>
{
EntityExtensionManager.ConfigureProperties<City>(b);
b.OwnsMany(c => c.Districts, d =>
{
d.WithOwner().HasForeignKey(x => x.CityId);

4
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs

@ -21,7 +21,7 @@ namespace Volo.Abp.TestApp
private readonly IRepository<EntityWithIntPk, int> _entityWithIntPksRepository;
public TestDataBuilder(
IBasicRepository<Person, Guid> personRepository,
IBasicRepository<Person, Guid> personRepository,
ICityRepository cityRepository,
IRepository<EntityWithIntPk, int> entityWithIntPksRepository)
{
@ -46,7 +46,7 @@ namespace Volo.Abp.TestApp
await _cityRepository.InsertAsync(new City(Guid.NewGuid(), "Tokyo"));
await _cityRepository.InsertAsync(new City(Guid.NewGuid(), "Madrid"));
await _cityRepository.InsertAsync(new City(LondonCityId, "London") { ExtraProperties = { { "Population", 10_470_000 } } });
await _cityRepository.InsertAsync(new City(LondonCityId, "London") { ExtraProperties = { { "Population", 10_470_000 }, { "PhoneCode", "42" } } });
await _cityRepository.InsertAsync(istanbul);
await _cityRepository.InsertAsync(new City(Guid.NewGuid(), "Paris"));
await _cityRepository.InsertAsync(new City(Guid.NewGuid(), "Washington"));

1
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/ExtraProperties_Tests.cs

@ -41,6 +41,7 @@ namespace Volo.Abp.TestApp.Testing
public async Task Should_Update_An_Existing_Extra_Property()
{
var london = await CityRepository.FindByNameAsync("London");
london.GetProperty<int>("Population").ShouldBe(10_470_000);
london.ExtraProperties["Population"] = 11_000_042;
await CityRepository.UpdateAsync(london);

Loading…
Cancel
Save