From ab479e90ca260f1d4ef497ed7bc56b19e4d7a12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 08:56:41 +0300 Subject: [PATCH] Added tests for settings. --- .../Volo/Abp/Settings/SettingManager.cs | 7 +--- .../AbpIdentityApplicationTestBase.cs | 21 +++++++++- .../Abp/Settings/SettingManager_User_Tests.cs | 41 +++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index b90666bfe3..d426661417 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -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) diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs index 4f2f07fcfb..c8e3d391b9 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs @@ -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 action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } + + protected virtual T UsingDbContext(Func action) + { + using (var dbContext = GetRequiredService()) + { + return action.Invoke(dbContext); + } + } + } } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs index af6e7e195d..a570fbbab7 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs @@ -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"); + }); + } } } \ No newline at end of file