mirror of https://github.com/abpframework/abp.git
7 changed files with 157 additions and 0 deletions
@ -0,0 +1,21 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp2.1</TargetFramework> |
|||
<LangVersion>latest</LangVersion> |
|||
<AssemblyName>Volo.Abp.Settings.Tests</AssemblyName> |
|||
<PackageId>Volo.Abp.Settings.Tests</PackageId> |
|||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.Settings\Volo.Abp.Settings.csproj" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,20 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.Settings |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpSettingsModule), |
|||
typeof(AbpTestBaseModule) |
|||
)] |
|||
public class AbpSettingsTestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
Configure<SettingOptions>(options => |
|||
{ |
|||
options.ValueProviders.Add<TestSettingValueProvider>(); |
|||
options.DefinitionProviders.Add<TestSettingDefinitionProvider>(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Settings |
|||
{ |
|||
public class SettingManager_Tests : AbpIntegratedTest<AbpSettingsTestModule> |
|||
{ |
|||
private readonly ISettingManager _settingManager; |
|||
|
|||
public SettingManager_Tests() |
|||
{ |
|||
_settingManager = GetRequiredService<ISettingManager>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Get_Null_If_No_Value_Provided_And_No_Default_Value() |
|||
{ |
|||
(await _settingManager.GetOrNullAsync(TestSettingNames.TestSettingWithoutDefaultValue)) |
|||
.ShouldBeNull(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Get_Default_Value_If_No_Value_Provided_And_There_Is_A_Default_Value() |
|||
{ |
|||
(await _settingManager.GetOrNullAsync(TestSettingNames.TestSettingWithDefaultValue)) |
|||
.ShouldBe("default-value"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Set_And_Get_Encrypted_Values() |
|||
{ |
|||
(await _settingManager.GetOrNullAsync(TestSettingNames.TestSettingEncrypted)) |
|||
.ShouldBeNull(); |
|||
|
|||
await _settingManager.SetAsync( |
|||
TestSettingNames.TestSettingEncrypted, |
|||
"abc", |
|||
TestSettingValueProvider.ProviderName, |
|||
null |
|||
); |
|||
|
|||
(await _settingManager.GetOrNullAsync(TestSettingNames.TestSettingEncrypted)) |
|||
.ShouldBe("abc"); |
|||
} |
|||
|
|||
//TODO: Needs more tests with more advanced scenarios.
|
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
namespace Volo.Abp.Settings |
|||
{ |
|||
public class TestSettingDefinitionProvider : SettingDefinitionProvider |
|||
{ |
|||
public override void Define(ISettingDefinitionContext context) |
|||
{ |
|||
context.Add( |
|||
new SettingDefinition(TestSettingNames.TestSettingWithoutDefaultValue), |
|||
new SettingDefinition(TestSettingNames.TestSettingWithDefaultValue, "default-value"), |
|||
new SettingDefinition(TestSettingNames.TestSettingEncrypted, isEncrypted: true) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
namespace Volo.Abp.Settings |
|||
{ |
|||
public static class TestSettingNames |
|||
{ |
|||
public const string TestSettingWithoutDefaultValue = "TestSettingWithoutDefaultValue"; |
|||
public const string TestSettingWithDefaultValue = "TestSettingWithDefaultValue"; |
|||
public const string TestSettingEncrypted = "TestSettingEncrypted"; |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Settings |
|||
{ |
|||
public class TestSettingValueProvider : ISettingValueProvider, ITransientDependency |
|||
{ |
|||
public const string ProviderName = "Test"; |
|||
|
|||
private readonly Dictionary<string, string> _values; |
|||
|
|||
public string Name => ProviderName; |
|||
|
|||
public TestSettingValueProvider() |
|||
{ |
|||
_values = new Dictionary<string, string>(); |
|||
} |
|||
|
|||
public Task<string> GetOrNullAsync(SettingDefinition setting, string providerKey) |
|||
{ |
|||
return Task.FromResult(_values.GetOrDefault(setting.Name)); |
|||
} |
|||
|
|||
public Task SetAsync(SettingDefinition setting, string value, string providerKey) |
|||
{ |
|||
_values[setting.Name] = value; |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task ClearAsync(SettingDefinition setting, string providerKey) |
|||
{ |
|||
_values.Remove(setting.Name); |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue