Browse Source

Revised MultiTenantManager_Tests.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
08dd58c92e
  1. 14
      src/Volo.Abp.TestBase/AbpIntegratedTest.cs
  2. 1
      test/AbpTestBase/project.json
  3. 38
      test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/AsyncLocalAmbientTenantScopeProvider_Tests.cs
  4. 9
      test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestBase.cs
  5. 10
      test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestModule.cs
  6. 133
      test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_TenantResolver_Tests.cs
  7. 99
      test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_Tests.cs

14
src/Volo.Abp.TestBase/AbpIntegratedTest.cs

@ -17,8 +17,12 @@ namespace Volo.Abp.TestBase
{
var services = CreateServiceCollection();
BeforeAppApplication(services);
Application = services.AddApplication<TStartupModule>();
AfterAddApplication(services);
var serviceProvider = CreateServiceProvider(services);
Application.Initialize(serviceProvider);
@ -29,6 +33,16 @@ namespace Volo.Abp.TestBase
return new ServiceCollection();
}
protected virtual void BeforeAppApplication(IServiceCollection services)
{
}
protected virtual void AfterAddApplication(IServiceCollection services)
{
}
protected virtual IServiceProvider CreateServiceProvider(IServiceCollection services)
{
MainServiceScope = services.BuildServiceProvider().CreateScope();

1
test/AbpTestBase/project.json

@ -5,6 +5,7 @@
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"NSubstitute": "2.0.0-rc",
"Shouldly": "2.8.2",
"Volo.Abp.TestBase": "1.0.0-*",
"xunit": "2.2.0-beta4-build3444",
"xunit.extensibility.execution": "2.2.0-beta4-build3444"
},

38
test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/AsyncLocalAmbientTenantScopeProvider_Tests.cs

@ -1,38 +0,0 @@
using Shouldly;
using Xunit;
namespace Volo.Abp.MultiTenancy
{
public class AsyncLocalAmbientTenantScopeProvider_Tests
{
[Fact]
public void Should_Support_Inner_Scopes()
{
var scopeProvider = new AsyncLocalTenantScopeProvider();
Assert.Null(scopeProvider.CurrentScope?.Tenant);
using (scopeProvider.EnterScope(new TenantInfo("A")))
{
Assert.Equal("A", scopeProvider.CurrentScope?.Tenant?.Id);
using (scopeProvider.EnterScope(new TenantInfo("B")))
{
Assert.Equal("B", scopeProvider.CurrentScope?.Tenant?.Id);
using (scopeProvider.EnterScope(null))
{
Assert.NotNull(scopeProvider.CurrentScope);
Assert.Null(scopeProvider.CurrentScope.Tenant);
}
Assert.Equal("B", scopeProvider.CurrentScope?.Tenant?.Id);
}
Assert.Equal("A", scopeProvider.CurrentScope?.Tenant?.Id);
}
scopeProvider.CurrentScope.ShouldBeNull();
}
}
}

9
test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestBase.cs

@ -0,0 +1,9 @@
using Volo.Abp.TestBase;
namespace Volo.Abp.MultiTenancy
{
public class MultiTenancyTestBase : AbpIntegratedTest<MultiTenancyTestModule>
{
}
}

10
test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestModule.cs

@ -0,0 +1,10 @@
using Volo.Abp.Modularity;
namespace Volo.Abp.MultiTenancy
{
[DependsOn(typeof(AbpMultiTenancyModule))]
public class MultiTenancyTestModule : AbpModule
{
}
}

133
test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_TenantResolver_Tests.cs

@ -1,133 +0,0 @@
using System;
using Microsoft.Extensions.Options;
using NSubstitute;
using Shouldly;
using Xunit;
namespace Volo.Abp.MultiTenancy
{
public class MultiTenantManager_TenantResolver_Tests
{
[Fact]
public void Should_Get_Current_Tenant_As_Null_If_No_Resolver()
{
//Arrange
var manager = new MultiTenancyManager(
Substitute.For<IServiceProvider>(),
Substitute.For<ITenantScopeProvider>(),
new OptionsWrapper<MultiTenancyOptions>(new MultiTenancyOptions())
);
//Assert
manager.CurrentTenant.ShouldBeNull();
}
[Fact]
public void Should_Get_Current_Tenant_From_Single_Resolver()
{
//Arrange
var fakeTenant = new TenantInfo("A");
var manager = new MultiTenancyManager(
Substitute.For<IServiceProvider>(),
Substitute.For<ITenantScopeProvider>(),
new OptionsWrapper<MultiTenancyOptions>(new MultiTenancyOptions
{
TenantResolvers =
{
new SimpleTenantResolver(context =>
{
context.Tenant = fakeTenant;
context.Handled = true;
})
}
}
)
);
//Assert
manager.CurrentTenant.ShouldBe(fakeTenant);
}
[Fact]
public void Should_Get_Current_Tenant_From_Multiple_Resolvers()
{
//Arrange
var selectedTenant = new TenantInfo("A");
var manager = new MultiTenancyManager(
Substitute.For<IServiceProvider>(),
Substitute.For<ITenantScopeProvider>(),
new OptionsWrapper<MultiTenancyOptions>(new MultiTenancyOptions
{
TenantResolvers =
{
new SimpleTenantResolver(context =>
{
context.Tenant = new TenantInfo("B");
context.Handled = false; //Causes go to the next resolver
}),
new SimpleTenantResolver(context =>
{
context.Tenant = selectedTenant;
}),
new SimpleTenantResolver(context =>
{
context.Tenant = new TenantInfo("C");
})
}
}
)
);
//Assert
manager.CurrentTenant.ShouldBe(selectedTenant);
}
[Fact]
public void Should_Get_Ambient_Tenant_If_Changed()
{
//Arrange
var oldTenant = new TenantInfo("A");
var manager = new MultiTenancyManager(
Substitute.For<IServiceProvider>(),
new AsyncLocalTenantScopeProvider(),
new OptionsWrapper<MultiTenancyOptions>(
new MultiTenancyOptions
{
TenantResolvers =
{
new SimpleTenantResolver(context =>
{
context.Tenant = oldTenant;
})
}
}
)
);
manager.CurrentTenant.ShouldBe(oldTenant);
//Act
var overridedTenant = new TenantInfo("B");
using (manager.ChangeTenant(overridedTenant))
{
//Assert
manager.CurrentTenant.ShouldBe(overridedTenant);
}
//Assert
manager.CurrentTenant.ShouldBe(oldTenant);
}
}
}

99
test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_Tests.cs

@ -0,0 +1,99 @@
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
namespace Volo.Abp.MultiTenancy
{
public class MultiTenantManager_Tests : MultiTenancyTestBase
{
private readonly IMultiTenancyManager _multiTenancyManager;
private readonly TenantInfo _tenantA = new TenantInfo("A");
private readonly TenantInfo _tenantB = new TenantInfo("B");
private TenantInfo _tenantToBeResolved;
public MultiTenantManager_Tests()
{
_multiTenancyManager = ServiceProvider.GetRequiredService<IMultiTenancyManager>();
}
[Fact]
public void CurrentTenant_Should_Be_Null_As_Default()
{
//Assert
_multiTenancyManager.CurrentTenant.ShouldBeNull();
}
protected override void AfterAddApplication(IServiceCollection services)
{
services.Configure<MultiTenancyOptions>(options =>
{
options.TenantResolvers.Add(new SimpleTenantResolver(context =>
{
if (_tenantToBeResolved == _tenantA)
{
context.Tenant = _tenantA;
}
}));
options.TenantResolvers.Add(new SimpleTenantResolver(context =>
{
if (_tenantToBeResolved == _tenantB)
{
context.Tenant = _tenantB;
}
}));
});
}
[Fact]
public void Should_Get_Current_Tenant_From_Single_Resolver()
{
//Arrange
_tenantToBeResolved = _tenantA;
//Assert
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantA);
}
[Fact]
public void Should_Get_Current_Tenant_From_Multiple_Resolvers()
{
//Arrange
_tenantToBeResolved = _tenantB;
//Assert
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantB);
}
[Fact]
public void Should_Get_Changed_Tenant_If_Wanted()
{
_multiTenancyManager.CurrentTenant.ShouldBe(null);
_tenantToBeResolved = _tenantB;
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantB);
using (_multiTenancyManager.ChangeTenant(_tenantA))
{
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantA);
using (_multiTenancyManager.ChangeTenant(_tenantB))
{
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantB);
}
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantA);
}
_multiTenancyManager.CurrentTenant.ShouldBe(_tenantB);
}
}
}
Loading…
Cancel
Save