Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Cache/DeepCloneAppCacheTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

123 lines
3.4 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Diagnostics;
using System.Linq;
using NUnit.Framework;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Collections;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Tests.Common;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Cache
{
[TestFixture]
public class DeepCloneAppCacheTests : RuntimeAppCacheTests
{
private DeepCloneAppCache _provider;
private ObjectCacheAppCache _memberCache;
protected override int GetTotalItemCount => _memberCache.MemoryCache.Count();
public override void Setup()
{
base.Setup();
_memberCache = new ObjectCacheAppCache();
_provider = new DeepCloneAppCache(_memberCache);
}
internal override IAppCache AppCache => _provider;
internal override IAppPolicyCache AppPolicyCache => _provider;
[Test]
public void Clones_List()
{
var original = new DeepCloneableList<TestClone>(ListCloneBehavior.Always)
{
new TestClone(),
new TestClone(),
new TestClone()
};
DeepCloneableList<TestClone> val = _provider.GetCacheItem("test", () => original);
Assert.AreEqual(original.Count, val.Count);
foreach (TestClone item in val)
{
Assert.IsTrue(item.IsClone);
}
}
[Test]
public void Ensures_Cloned_And_Reset()
{
var original = new TestClass()
{
Name = "hello"
};
Assert.IsTrue(original.IsDirty());
var val = _provider.GetCacheItem("test", () => original);
Assert.AreNotEqual(original.CloneId, val.CloneId);
Assert.IsFalse(val.IsDirty());
}
[Test]
public void DoesNotCacheExceptions()
{
string value;
Assert.Throws<Exception>(() => { value = (string)_provider.Get("key", () => GetValue(1)); });
Assert.Throws<Exception>(() => { value = (string)_provider.Get("key", () => GetValue(2)); });
// does not throw
value = (string)_provider.Get("key", () => GetValue(3));
Assert.AreEqual("succ3", value);
// cache
value = (string)_provider.Get("key", () => GetValue(4));
Assert.AreEqual("succ3", value);
}
private static string GetValue(int i)
{
Debug.Print("get" + i);
if (i < 3)
{
throw new Exception("fail");
}
return "succ" + i;
}
private class TestClass : BeingDirtyBase, IDeepCloneable
{
public TestClass() => CloneId = Guid.NewGuid();
private string _name;
public string Name
{
get => _name;
set => SetPropertyValueAndDetectChanges(value, ref _name, nameof(Name));
}
public Guid CloneId { get; set; }
public object DeepClone()
{
var shallowClone = (TestClass)MemberwiseClone();
DeepCloneHelper.DeepCloneRefProperties(this, shallowClone);
shallowClone.CloneId = Guid.NewGuid();
return shallowClone;
}
}
}
}