2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-09-17 10:35:11 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-12-23 11:35:49 +01:00
|
|
|
using NUnit.Framework;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Persistence.Repositories;
|
2021-02-15 11:41:12 +01:00
|
|
|
using Umbraco.Cms.Core.Scoping;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
|
2022-01-13 17:44:11 +00:00
|
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
2021-02-11 08:30:27 +01:00
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
2020-02-15 12:17:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositories;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
2025-03-21 18:02:31 +01:00
|
|
|
internal sealed class KeyValueRepositoryTests : UmbracoIntegrationTest
|
2020-02-15 12:17:48 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void CanSetAndGet()
|
2020-02-15 12:17:48 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
ICoreScopeProvider provider = ScopeProvider;
|
2020-02-15 12:17:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Insert new key/value
|
|
|
|
|
using (var scope = provider.CreateCoreScope())
|
|
|
|
|
{
|
|
|
|
|
var keyValue = new KeyValue { Identifier = "foo", Value = "bar", UpdateDate = DateTime.Now };
|
|
|
|
|
var repo = CreateRepository(provider);
|
|
|
|
|
repo.Save(keyValue);
|
|
|
|
|
scope.Complete();
|
|
|
|
|
}
|
2020-02-15 12:17:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Retrieve key/value
|
|
|
|
|
using (var scope = provider.CreateCoreScope())
|
|
|
|
|
{
|
|
|
|
|
var repo = CreateRepository(provider);
|
|
|
|
|
var keyValue = repo.Get("foo");
|
|
|
|
|
scope.Complete();
|
2020-02-15 12:17:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("bar", keyValue.Value);
|
|
|
|
|
}
|
2020-02-15 12:17:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Update value
|
|
|
|
|
using (var scope = provider.CreateCoreScope())
|
|
|
|
|
{
|
|
|
|
|
var repo = CreateRepository(provider);
|
|
|
|
|
var keyValue = repo.Get("foo");
|
|
|
|
|
keyValue.Value = "buzz";
|
|
|
|
|
keyValue.UpdateDate = DateTime.Now;
|
|
|
|
|
repo.Save(keyValue);
|
|
|
|
|
scope.Complete();
|
|
|
|
|
}
|
2020-02-15 12:17:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Retrieve key/value again
|
|
|
|
|
using (var scope = provider.CreateCoreScope())
|
|
|
|
|
{
|
|
|
|
|
var repo = CreateRepository(provider);
|
|
|
|
|
var keyValue = repo.Get("foo");
|
|
|
|
|
scope.Complete();
|
2020-02-15 12:17:48 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("buzz", keyValue.Value);
|
2020-02-15 12:17:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
private IKeyValueRepository CreateRepository(ICoreScopeProvider provider) =>
|
|
|
|
|
new KeyValueRepository((IScopeAccessor)provider, LoggerFactory.CreateLogger<KeyValueRepository>());
|
2020-02-15 12:17:48 +01:00
|
|
|
}
|