Added tests for settings.

pull/204/head
Halil İbrahim Kalkan 8 years ago
parent f9618dcf14
commit ab479e90ca

@ -131,16 +131,11 @@ namespace Volo.Abp.Settings
return settingValues.Values.ToList();
}
public virtual Task SetAsync(string name, string value, string entityType, string entityId, bool forceToSet = false)
public virtual async Task SetAsync(string name, string value, string entityType, string entityId, bool forceToSet = false)
{
Check.NotNull(name, nameof(name));
Check.NotNull(entityType, nameof(entityType));
return SetInternalAsync(name, value, entityType, entityId, forceToSet);
}
protected virtual async Task SetInternalAsync(string name, string value, string entityType, string entityId, bool forceToSet = false)
{
var setting = SettingDefinitionManager.Get(name);
if (!forceToSet)

@ -1,4 +1,6 @@
using Volo.Abp.TestBase;
using System;
using Volo.Abp.Settings.EntityFrameworkCore;
using Volo.Abp.TestBase;
namespace Volo.Abp.Settings
{
@ -8,5 +10,22 @@ namespace Volo.Abp.Settings
{
options.UseAutofac();
}
protected virtual void UsingDbContext(Action<IAbpSettingsDbContext> action)
{
using (var dbContext = GetRequiredService<IAbpSettingsDbContext>())
{
action.Invoke(dbContext);
}
}
protected virtual T UsingDbContext<T>(Func<IAbpSettingsDbContext, T> action)
{
using (var dbContext = GetRequiredService<IAbpSettingsDbContext>())
{
return action.Invoke(dbContext);
}
}
}
}

@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
@ -102,5 +103,45 @@ namespace Volo.Abp.Settings
settingValues.Count.ShouldBe(1);
settingValues.ShouldContain(sv => sv.Name == "MySetting2" && sv.Value == "user1-store-value");
}
[Fact]
public async Task Should_Delete_Setting_Record_When_Set_To_Null()
{
await _settingManager.SetForUserAsync(AbpIdentityTestDataBuilder.User1Id, "MySetting2", null);
UsingDbContext(context =>
{
context.Settings.Count(
s =>
s.EntityType == UserSettingValueProvider.DefaultEntityType &&
s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() &&
s.Name == "MySetting2"
).ShouldBe(0);
});
}
[Fact]
public async Task Should_Change_User_Setting()
{
(await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id))
.ShouldBe("user1-store-value");
await _settingManager.SetForUserAsync(AbpIdentityTestDataBuilder.User1Id, "MySetting2", "user1-new-store-value");
(await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id))
.ShouldBe("user1-new-store-value");
UsingDbContext(context =>
{
var setting = context.Settings.Single(
s =>
s.EntityType == UserSettingValueProvider.DefaultEntityType &&
s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() &&
s.Name == "MySetting2"
);
setting.Value.ShouldBe("user1-new-store-value");
});
}
}
}
Loading…
Cancel
Save