2021-06-08 10:58:17 -06:00
|
|
|
// Copyright (c) Umbraco.
|
2020-12-23 11:35:49 +01:00
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-02-17 22:12:42 +01:00
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
2020-02-17 22:12:42 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tests covering methods in the KeyValueService class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
|
|
|
|
public class KeyValueServiceTests : UmbracoIntegrationTest
|
2020-02-17 22:12:42 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
private IKeyValueService KeyValueService => GetRequiredService<IKeyValueService>();
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Query_For_Key_Prefix()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
KeyValueService.SetValue("test1", "hello1");
|
|
|
|
|
KeyValueService.SetValue("test2", "hello2");
|
|
|
|
|
KeyValueService.SetValue("test3", "hello3");
|
|
|
|
|
KeyValueService.SetValue("test4", "hello4");
|
|
|
|
|
KeyValueService.SetValue("someotherprefix1", "helloagain1");
|
|
|
|
|
// Act
|
|
|
|
|
var value = KeyValueService.FindByKeyPrefix("test");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(4, value.Count);
|
|
|
|
|
Assert.AreEqual("hello1", value["test1"]);
|
|
|
|
|
Assert.AreEqual("hello2", value["test2"]);
|
|
|
|
|
Assert.AreEqual("hello3", value["test3"]);
|
|
|
|
|
Assert.AreEqual("hello4", value["test4"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetValue_ForMissingKey_ReturnsNull()
|
|
|
|
|
{
|
|
|
|
|
// Act
|
|
|
|
|
var value = KeyValueService.GetValue("foo");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsNull(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetValue_ForExistingKey_ReturnsValue()
|
|
|
|
|
{
|
|
|
|
|
KeyValueService.SetValue("foo", "bar");
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var value = KeyValueService.GetValue("foo");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.AreEqual("bar", value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void SetValue_ForExistingKey_SavesValue()
|
2020-02-17 22:12:42 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
KeyValueService.SetValue("foo", "bar");
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
KeyValueService.SetValue("foo", "buzz");
|
|
|
|
|
var value = KeyValueService.GetValue("foo");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.AreEqual("buzz", value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TrySetValue_ForExistingKeyWithProvidedValue_ReturnsTrueAndSetsValue()
|
|
|
|
|
{
|
|
|
|
|
KeyValueService.SetValue("foo", "bar");
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = KeyValueService.TrySetValue("foo", "bar", "buzz");
|
|
|
|
|
var value = KeyValueService.GetValue("foo");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsTrue(result);
|
|
|
|
|
Assert.AreEqual("buzz", value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TrySetValue_ForExistingKeyWithoutProvidedValue_ReturnsFalseAndDoesNotSetValue()
|
|
|
|
|
{
|
|
|
|
|
KeyValueService.SetValue("foo", "bar");
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = KeyValueService.TrySetValue("foo", "bang", "buzz");
|
|
|
|
|
var value = KeyValueService.GetValue("foo");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsFalse(result);
|
|
|
|
|
Assert.AreEqual("bar", value);
|
2020-02-17 22:12:42 +01:00
|
|
|
}
|
|
|
|
|
}
|