Browse Source

Add `DataSeederExtensions_Tests`.

pull/13940/head
maliming 4 years ago
parent
commit
fbc32f5f89
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 13
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeeder.cs
  2. 13
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeederExtensions.cs
  3. 43
      framework/test/Volo.Abp.Data.Tests/Volo/Abp/Data/DataSeederExtensions_Tests.cs

13
framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeeder.cs

@ -1,4 +1,6 @@
using System.Threading.Tasks; using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -25,12 +27,17 @@ public class DataSeeder : IDataSeeder, ITransientDependency
{ {
using (var scope = ServiceScopeFactory.CreateScope()) using (var scope = ServiceScopeFactory.CreateScope())
{ {
if (context.Properties.ContainsKey(nameof(DataSeederExtensions.SeedInSeparateUowAsync))) if (context.Properties.ContainsKey(DataSeederExtensions.SeedInSeparateUow))
{ {
var manager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>(); var manager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
foreach (var contributorType in Options.Contributors) foreach (var contributorType in Options.Contributors)
{ {
using (var uow = manager.Begin(requiresNew: true, isTransactional: true)) var options = context.Properties.TryGetValue(DataSeederExtensions.SeedInSeparateUowOptions, out var uowOptions)
? (AbpUnitOfWorkOptions) uowOptions
: new AbpUnitOfWorkOptions();
var requiresNew = context.Properties.TryGetValue(DataSeederExtensions.SeedInSeparateUowRequiresNew, out var obj) && (bool) obj;
using (var uow = manager.Begin(options, requiresNew))
{ {
var contributor = (IDataSeedContributor)scope.ServiceProvider.GetRequiredService(contributorType); var contributor = (IDataSeedContributor)scope.ServiceProvider.GetRequiredService(contributorType);
await contributor.SeedAsync(context); await contributor.SeedAsync(context);

13
framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeederExtensions.cs

@ -1,17 +1,26 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Uow;
namespace Volo.Abp.Data; namespace Volo.Abp.Data;
public static class DataSeederExtensions public static class DataSeederExtensions
{ {
public const string SeedInSeparateUow = "__SeedInSeparateUow";
public const string SeedInSeparateUowOptions = "__SeedInSeparateUowOptions";
public const string SeedInSeparateUowRequiresNew = "__SeedInSeparateUowRequiresNew";
public static Task SeedAsync(this IDataSeeder seeder, Guid? tenantId = null) public static Task SeedAsync(this IDataSeeder seeder, Guid? tenantId = null)
{ {
return seeder.SeedAsync(new DataSeedContext(tenantId)); return seeder.SeedAsync(new DataSeedContext(tenantId));
} }
public static Task SeedInSeparateUowAsync(this IDataSeeder seeder, Guid? tenantId = null) public static Task SeedInSeparateUowAsync(this IDataSeeder seeder, Guid? tenantId = null, AbpUnitOfWorkOptions options = null, bool requiresNew = false)
{ {
return seeder.SeedAsync(new DataSeedContext(tenantId).WithProperty(nameof(DataSeederExtensions.SeedInSeparateUowAsync), true)); var context = new DataSeedContext(tenantId);
context.WithProperty(SeedInSeparateUow, true);
context.WithProperty(SeedInSeparateUowOptions, options);
context.WithProperty(SeedInSeparateUowRequiresNew, requiresNew);
return seeder.SeedAsync(context);
} }
} }

43
framework/test/Volo.Abp.Data.Tests/Volo/Abp/Data/DataSeederExtensions_Tests.cs

@ -0,0 +1,43 @@
using System;
using System.Data;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NSubstitute;
using Volo.Abp.Modularity;
using Volo.Abp.Testing;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.Data;
public class DataSeederExtensions_Tests : AbpIntegratedTest<DataSeederExtensions_Tests.TestModule>
{
private IDataSeeder _dataSeeder;
protected override void AfterAddApplication(IServiceCollection services)
{
_dataSeeder = Substitute.For<IDataSeeder>();
services.Replace(ServiceDescriptor.Singleton(_dataSeeder));
base.AfterAddApplication(services);
}
[Fact]
public void SeedInSeparateUowAsync()
{
var tenantId = Guid.NewGuid();
_dataSeeder.SeedInSeparateUowAsync(tenantId, new AbpUnitOfWorkOptions(true, IsolationLevel.Serializable, 888), true);
_dataSeeder.Received().SeedAsync(Arg.Is<DataSeedContext>(x => x.TenantId == tenantId &&
x.Properties[DataSeederExtensions.SeedInSeparateUow].To<bool>() == true &&
x.Properties[DataSeederExtensions.SeedInSeparateUowOptions].As<AbpUnitOfWorkOptions>().IsTransactional == true &&
x.Properties[DataSeederExtensions.SeedInSeparateUowOptions].As<AbpUnitOfWorkOptions>().IsolationLevel == IsolationLevel.Serializable &&
x.Properties[DataSeederExtensions.SeedInSeparateUowOptions].As<AbpUnitOfWorkOptions>().Timeout == 888 &&
x.Properties[DataSeederExtensions.SeedInSeparateUowRequiresNew].To<bool>() == true));
}
[DependsOn(typeof(AbpDataModule))]
public class TestModule : AbpModule
{
}
}
Loading…
Cancel
Save