Browse Source

Revert "Add support and tests for entity history with JSON properties"

This reverts commit 4d00ee9365.
pull/24519/head
maliming 5 months ago
parent
commit
96a7303fc6
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 10
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs
  2. 7
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs
  3. 130
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs
  4. 23
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs
  5. 25
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs
  6. 28
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs

10
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs

@ -202,6 +202,11 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency
}
}
if (AbpEfCoreNavigationHelper == null)
{
return propertyChanges;
}
foreach (var (navigationEntry, index) in entityEntry.Navigations.Select((value, i) => ( value, i )))
{
var propertyInfo = navigationEntry.Metadata.PropertyInfo;
@ -225,11 +230,6 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency
continue;
}
if (AbpEfCoreNavigationHelper == null)
{
return propertyChanges;
}
if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index))
{

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

@ -5,7 +5,6 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Auditing;
using Volo.Abp.Autofac;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EntityFrameworkCore.Domain;
@ -87,12 +86,6 @@ public class AbpEntityFrameworkCoreTestModule : AbpModule
abpDbContextConfigurationContext.DbContextOptions.UseSqlite(sqliteConnection).AddAbpDbContextOptionsExtension();
});
});
Configure<AbpAuditingOptions>(options =>
{
options.EntityHistorySelectors.Add(new NamedTypeSelector(nameof(AppEntityWithJsonProperty), type => type == typeof(AppEntityWithJsonProperty)));
options.EntityHistorySelectors.Add(new NamedTypeSelector(nameof(TestSharedEntity), type => type == typeof(TestSharedEntity)));
});
}
public override void OnPreApplicationInitialization(ApplicationInitializationContext context)

130
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs

@ -1,130 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Auditing;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EntityFrameworkCore.EntityHistory;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.EntityFrameworkCore;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.EntityFrameworkCore.Auditing;
public class EntityHistoryHelper_Tests : EntityFrameworkCoreTestBase
{
private readonly IEntityHistoryHelper _entityHistoryHelper;
private readonly IRepository<AppEntityWithJsonProperty, Guid> _appEntityWithJsonRepository;
private readonly IRepository<TestSharedEntity, Guid> _testSharedEntityRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public EntityHistoryHelper_Tests()
{
_entityHistoryHelper = GetRequiredService<IEntityHistoryHelper>();
_appEntityWithJsonRepository = GetRequiredService<IRepository<AppEntityWithJsonProperty, Guid>>();
_testSharedEntityRepository = GetRequiredService<IRepository<TestSharedEntity, Guid>>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
}
[Fact]
public async Task CreateChangeList_Should_Track_Nested_Json_Property_Changes_As_Separate_Property_Changes()
{
// Arrange & Act
EntityChangeInfo entityChange = null;
await WithUnitOfWorkAsync(async () =>
{
var entity = new AppEntityWithJsonProperty(Guid.NewGuid(), "Test Entity")
{
Data = new JsonPropertyObject()
{
{ "Name", "String Name" },
{ "Value", "String Value"}
},
Count = 10
};
await _appEntityWithJsonRepository.InsertAsync(entity);
var dbContext = await GetDbContextAsync();
var entries = dbContext.ChangeTracker.Entries().ToList();
var entityChanges = _entityHistoryHelper.CreateChangeList(entries);
entityChange = entityChanges.FirstOrDefault(x => x.EntityTypeFullName.Contains(nameof(AppEntityWithJsonProperty)));
});
// Assert
entityChange.ShouldNotBeNull();
var dataPropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == nameof(AppEntityWithJsonProperty.Data));
dataPropertyChange.ShouldBeNull();
var jsonNamePropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == nameof(AppEntityWithJsonProperty.Data) + "." + "Name");
jsonNamePropertyChange.ShouldNotBeNull();
jsonNamePropertyChange.PropertyTypeFullName.ShouldBe(typeof(string).FullName);
jsonNamePropertyChange.NewValue.ShouldBe("\"String Name\"");
var jsonValuePropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == "Value");
jsonValuePropertyChange.ShouldNotBeNull();
jsonValuePropertyChange.PropertyTypeFullName.ShouldBe(typeof(string).FullName);
jsonValuePropertyChange.NewValue.ShouldBe("\"String Value\"");
}
[Fact]
public async Task CreateChangeList_Should_Track_Shared_Entities_With_Their_Respective_Entity_Names()
{
// Arrange & Act
List<EntityChangeInfo> entityChanges = null;
await WithUnitOfWorkAsync(async () =>
{
var entity = new TestSharedEntity(Guid.NewGuid())
{
TenantId = null,
IsDeleted = false,
Name = "Test Person1",
Age = 10,
Birthday = DateTime.Now
}.SetProperty("testProperty", "Test Value1");
_testSharedEntityRepository.SetEntityName("TestSharedEntity1");
await _testSharedEntityRepository.InsertAsync(entity);
var entity2 = new TestSharedEntity(Guid.NewGuid())
{
TenantId = null,
IsDeleted = false,
Name = "Test Person2",
Age = 20,
Birthday = DateTime.Now
}.SetProperty("testProperty", "Test Value2");
_testSharedEntityRepository.SetEntityName("TestSharedEntity2");
await _testSharedEntityRepository.InsertAsync(entity2);
var dbContext = await GetDbContextAsync();
var entries = dbContext.ChangeTracker.Entries().ToList();
entityChanges = _entityHistoryHelper.CreateChangeList(entries);
});
entityChanges.ShouldContain(x => x.EntityTypeFullName == "TestSharedEntity1");
entityChanges.ShouldContain(x => x.EntityTypeFullName == "TestSharedEntity2");
}
private async Task<TestAppDbContext> GetDbContextAsync()
{
var uow = _unitOfWorkManager.Current;
if (uow == null)
{
throw new InvalidOperationException("No active unit of work found");
}
var dbContextProvider = uow.ServiceProvider.GetRequiredService<IDbContextProvider<TestAppDbContext>>();
return await dbContextProvider.GetDbContextAsync();
}
}

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

@ -37,8 +37,6 @@ public class TestMigrationsDbContext : AbpDbContext<TestMigrationsDbContext>
public DbSet<TestSharedEntity> TestSharedEntity => Set<TestSharedEntity>("TestSharedEntity1");
public DbSet<TestSharedEntity> TestSharedEntity2 => Set<TestSharedEntity>("TestSharedEntity2");
public DbSet<AppEntityWithJsonProperty> EntitiesWithObjectProperty { get; set; }
public TestMigrationsDbContext(DbContextOptions<TestMigrationsDbContext> options)
: base(options)
@ -142,26 +140,5 @@ public class TestMigrationsDbContext : AbpDbContext<TestMigrationsDbContext>
{
b.ConfigureByConvention();
});
modelBuilder.Entity<AppEntityWithJsonProperty>(b =>
{
b.ConfigureByConvention();
b.OwnsOne(x => x.Data, b2 =>
{
b2.ToJson();
b2.Property<object>("Name")
.HasConversion<string>(
v => v.ToString(),
v => v
);
b2.Property<object>("Value")
.HasConversion<string>(
v => v.ToString(),
v => v
);
});
});
}
}

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

@ -40,11 +40,9 @@ public class TestAppDbContext : AbpDbContext<TestAppDbContext>, IThirdDbContext,
public DbSet<Blog> Blogs { get; set; }
public DbSet<BlogPost> BlogPosts { get; set; }
public DbSet<TestSharedEntity> TestSharedEntity => Set<TestSharedEntity>("TestSharedEntity1");
public DbSet<TestSharedEntity> TestSharedEntity2 => Set<TestSharedEntity>("TestSharedEntity2");
public DbSet<AppEntityWithJsonProperty> EntitiesWithObjectProperty { get; set; }
public TestAppDbContext(DbContextOptions<TestAppDbContext> options)
: base(options)
@ -168,27 +166,6 @@ public class TestAppDbContext : AbpDbContext<TestAppDbContext>, IThirdDbContext,
b.ConfigureByConvention();
});
modelBuilder.Entity<AppEntityWithJsonProperty>(b =>
{
b.ConfigureByConvention();
b.OwnsOne(x => x.Data, b2 =>
{
b2.ToJson();
b2.Property<object>("Name")
.HasConversion<string>(
v => v.ToString(),
v => v
);
b2.Property<object>("Value")
.HasConversion<string>(
v => v.ToString(),
v => v
);
});
});
modelBuilder.TryConfigureObjectExtensions<TestAppDbContext>();
}
}

28
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs

@ -1,28 +0,0 @@
using System;
using System.Collections.Generic;
using Volo.Abp.Domain.Entities.Auditing;
namespace Volo.Abp.TestApp.Domain;
public class AppEntityWithJsonProperty : FullAuditedAggregateRoot<Guid>
{
public string Name { get; set; }
public JsonPropertyObject Data { get; set; }
public int Count { get; set; }
public AppEntityWithJsonProperty()
{
}
public AppEntityWithJsonProperty(Guid id, string name) : base(id)
{
Name = name;
}
}
public class JsonPropertyObject : Dictionary<string, object>
{
}
Loading…
Cancel
Save