V10: fix build warnings in test projects (#12509)

* Run code cleanup

* Dotnet format benchmarks project

* Fix up Test.Common

* Run dotnet format + manual cleanup

* Run code cleanup for unit tests

* Run dotnet format

* Fix up errors

* Manual cleanup of Unit test project

* Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Fix according to review

* Fix after merge

* Fix errors

Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
Co-authored-by: Zeegaan <nge@umbraco.dk>
This commit is contained in:
Nikolaj Geisle
2022-06-21 08:09:38 +02:00
committed by GitHub
parent 29961d40a3
commit 7aeb400fce
599 changed files with 87303 additions and 86123 deletions

View File

@@ -10,206 +10,214 @@ using NUnit.Framework;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Collections;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Infrastructure.Scoping;
using IScope = Umbraco.Cms.Infrastructure.Scoping.IScope;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Cache
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Cache;
[TestFixture]
public class FullDataSetCachePolicyTests
{
[TestFixture]
public class FullDataSetCachePolicyTests
private IScopeAccessor DefaultAccessor
{
private IScopeAccessor DefaultAccessor
get
{
get
{
var accessor = new Mock<IScopeAccessor>();
var scope = new Mock<IScope>();
scope.Setup(x => x.RepositoryCacheMode).Returns(Cms.Core.Scoping.RepositoryCacheMode.Default);
accessor.Setup(x => x.AmbientScope).Returns(scope.Object);
return accessor.Object;
}
var accessor = new Mock<IScopeAccessor>();
var scope = new Mock<IScope>();
scope.Setup(x => x.RepositoryCacheMode).Returns(RepositoryCacheMode.Default);
accessor.Setup(x => x.AmbientScope).Returns(scope.Object);
return accessor.Object;
}
}
[Test]
public void Caches_Single()
[Test]
public void Caches_Single()
{
AuditItem[] getAll =
{
AuditItem[] getAll = new[]
new(1, AuditType.Copy, 123, "test", "blah"),
new(2, AuditType.Copy, 123, "test", "blah2"),
};
var isCached = false;
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Insert(It.IsAny<string>(), It.IsAny<Func<object>>(), It.IsAny<TimeSpan?>(), It.IsAny<bool>(), It.IsAny<string[]>()))
.Callback(() => isCached = true);
var policy =
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
var unused = policy.Get(1, id => new AuditItem(1, AuditType.Copy, 123, "test", "blah"), ids => getAll);
Assert.IsTrue(isCached);
}
[Test]
public void Get_Single_From_Cache()
{
AuditItem[] getAll =
{
new(1, AuditType.Copy, 123, "test", "blah"), new(2, AuditType.Copy, 123, "test", "blah2"),
};
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Get(It.IsAny<string>())).Returns(new AuditItem(1, AuditType.Copy, 123, "test", "blah"));
var defaultPolicy =
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
var found = defaultPolicy.Get(1, id => null, ids => getAll);
Assert.IsNotNull(found);
}
[Test]
public void Get_All_Caches_Empty_List()
{
var getAll = new AuditItem[] { };
var cached = new List<string>();
IList list = null;
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Insert(It.IsAny<string>(), It.IsAny<Func<object>>(), It.IsAny<TimeSpan?>(), It.IsAny<bool>(), It.IsAny<string[]>()))
.Callback((string cacheKey, Func<object> o, TimeSpan? t, bool b, string[] s) =>
{
new AuditItem(1, AuditType.Copy, 123, "test", "blah"),
new AuditItem(2, AuditType.Copy, 123, "test", "blah2")
};
cached.Add(cacheKey);
var isCached = false;
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Insert(It.IsAny<string>(), It.IsAny<Func<object>>(), It.IsAny<TimeSpan?>(), It.IsAny<bool>(), It.IsAny<string[]>()))
.Callback(() => isCached = true);
var policy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
AuditItem unused = policy.Get(1, id => new AuditItem(1, AuditType.Copy, 123, "test", "blah"), ids => getAll);
Assert.IsTrue(isCached);
}
[Test]
public void Get_Single_From_Cache()
{
AuditItem[] getAll = new[]
{
new AuditItem(1, AuditType.Copy, 123, "test", "blah"),
new AuditItem(2, AuditType.Copy, 123, "test", "blah2")
};
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Get(It.IsAny<string>())).Returns(new AuditItem(1, AuditType.Copy, 123, "test", "blah"));
var defaultPolicy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
AuditItem found = defaultPolicy.Get(1, id => null, ids => getAll);
Assert.IsNotNull(found);
}
[Test]
public void Get_All_Caches_Empty_List()
{
var getAll = new AuditItem[] { };
var cached = new List<string>();
IList list = null;
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Insert(It.IsAny<string>(), It.IsAny<Func<object>>(), It.IsAny<TimeSpan?>(), It.IsAny<bool>(), It.IsAny<string[]>()))
.Callback((string cacheKey, Func<object> o, TimeSpan? t, bool b, string[] s) =>
{
cached.Add(cacheKey);
list = o() as IList;
});
// Return null if this is the first pass.
cache.Setup(x => x.Get(It.IsAny<string>()))
.Returns(() => cached.Any() ? new DeepCloneableList<AuditItem>(ListCloneBehavior.CloneOnce) : null);
var policy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
AuditItem[] found = policy.GetAll(new object[] { }, ids => getAll);
Assert.AreEqual(1, cached.Count);
Assert.IsNotNull(list);
// Do it again, ensure that its coming from the cache!
policy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
found = policy.GetAll(new object[] { }, ids => getAll);
Assert.AreEqual(1, cached.Count);
Assert.IsNotNull(list);
}
[Test]
public void Get_All_Caches_As_Single_List()
{
AuditItem[] getAll = new[]
{
new AuditItem(1, AuditType.Copy, 123, "test", "blah"),
new AuditItem(2, AuditType.Copy, 123, "test", "blah2")
};
var cached = new List<string>();
IList list = null;
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Insert(It.IsAny<string>(), It.IsAny<Func<object>>(), It.IsAny<TimeSpan?>(), It.IsAny<bool>(), It.IsAny<string[]>()))
.Callback((string cacheKey, Func<object> o, TimeSpan? t, bool b, string[] s) =>
{
cached.Add(cacheKey);
list = o() as IList;
});
cache.Setup(x => x.Get(It.IsAny<string>())).Returns(new AuditItem[] { });
var defaultPolicy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
AuditItem[] found = defaultPolicy.GetAll(new object[] { }, ids => getAll);
Assert.AreEqual(1, cached.Count);
Assert.IsNotNull(list);
}
[Test]
public void Get_All_Without_Ids_From_Cache()
{
AuditItem[] getAll = new[] { (AuditItem)null };
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Get(It.IsAny<string>())).Returns(() => new DeepCloneableList<AuditItem>(ListCloneBehavior.CloneOnce)
{
new AuditItem(1, AuditType.Copy, 123, "test", "blah"),
new AuditItem(2, AuditType.Copy, 123, "test", "blah2")
list = o() as IList;
});
var defaultPolicy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
// Return null if this is the first pass.
cache.Setup(x => x.Get(It.IsAny<string>()))
.Returns(() => cached.Any() ? new DeepCloneableList<AuditItem>(ListCloneBehavior.CloneOnce) : null);
AuditItem[] found = defaultPolicy.GetAll(new object[] { }, ids => getAll);
Assert.AreEqual(2, found.Length);
}
var policy =
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
[Test]
public void If_CreateOrUpdate_Throws_Cache_Is_Removed()
var found = policy.GetAll(new object[] { }, ids => getAll);
Assert.AreEqual(1, cached.Count);
Assert.IsNotNull(list);
// Do it again, ensure that its coming from the cache!
policy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
found = policy.GetAll(new object[] { }, ids => getAll);
Assert.AreEqual(1, cached.Count);
Assert.IsNotNull(list);
}
[Test]
public void Get_All_Caches_As_Single_List()
{
AuditItem[] getAll =
{
AuditItem[] getAll = new[]
{
new AuditItem(1, AuditType.Copy, 123, "test", "blah"),
new AuditItem(2, AuditType.Copy, 123, "test", "blah2")
};
new(1, AuditType.Copy, 123, "test", "blah"),
new(2, AuditType.Copy, 123, "test", "blah2"),
};
var cacheCleared = false;
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Clear(It.IsAny<string>()))
.Callback(() => cacheCleared = true);
var cached = new List<string>();
IList list = null;
var defaultPolicy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
try
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Insert(It.IsAny<string>(), It.IsAny<Func<object>>(), It.IsAny<TimeSpan?>(), It.IsAny<bool>(), It.IsAny<string[]>()))
.Callback((string cacheKey, Func<object> o, TimeSpan? t, bool b, string[] s) =>
{
defaultPolicy.Update(new AuditItem(1, AuditType.Copy, 123, "test", "blah"), item => throw new Exception("blah!"));
}
catch
{
// We need this catch or nunit throws up.
}
finally
{
Assert.IsTrue(cacheCleared);
}
}
cached.Add(cacheKey);
[Test]
public void If_Removes_Throws_Cache_Is_Removed()
list = o() as IList;
});
cache.Setup(x => x.Get(It.IsAny<string>())).Returns(new AuditItem[] { });
var defaultPolicy =
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
var found = defaultPolicy.GetAll(new object[] { }, ids => getAll);
Assert.AreEqual(1, cached.Count);
Assert.IsNotNull(list);
}
[Test]
public void Get_All_Without_Ids_From_Cache()
{
AuditItem[] getAll = { null };
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Get(It.IsAny<string>())).Returns(() =>
new DeepCloneableList<AuditItem>(ListCloneBehavior.CloneOnce)
{
new(1, AuditType.Copy, 123, "test", "blah"),
new(2, AuditType.Copy, 123, "test", "blah2"),
});
var defaultPolicy =
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
var found = defaultPolicy.GetAll(new object[] { }, ids => getAll);
Assert.AreEqual(2, found.Length);
}
[Test]
public void If_CreateOrUpdate_Throws_Cache_Is_Removed()
{
AuditItem[] getAll =
{
AuditItem[] getAll = new[]
{
new AuditItem(1, AuditType.Copy, 123, "test", "blah"),
new AuditItem(2, AuditType.Copy, 123, "test", "blah2")
};
new(1, AuditType.Copy, 123, "test", "blah"),
new(2, AuditType.Copy, 123, "test", "blah2"),
};
var cacheCleared = false;
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Clear(It.IsAny<string>()))
.Callback(() => cacheCleared = true);
var cacheCleared = false;
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Clear(It.IsAny<string>()))
.Callback(() => cacheCleared = true);
var defaultPolicy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
try
{
defaultPolicy.Delete(new AuditItem(1, AuditType.Copy, 123, "test", "blah"), item => throw new Exception("blah!"));
}
catch
{
// We need this catch or nunit throws up.
}
finally
{
Assert.IsTrue(cacheCleared);
}
var defaultPolicy =
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
try
{
defaultPolicy.Update(new AuditItem(1, AuditType.Copy, 123, "test", "blah"), item => throw new Exception("blah!"));
}
catch
{
// We need this catch or nunit throws up.
}
finally
{
Assert.IsTrue(cacheCleared);
}
}
[Test]
public void If_Removes_Throws_Cache_Is_Removed()
{
AuditItem[] getAll =
{
new(1, AuditType.Copy, 123, "test", "blah"),
new(2, AuditType.Copy, 123, "test", "blah2"),
};
var cacheCleared = false;
var cache = new Mock<IAppPolicyCache>();
cache.Setup(x => x.Clear(It.IsAny<string>()))
.Callback(() => cacheCleared = true);
var defaultPolicy =
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, item => item.Id, false);
try
{
defaultPolicy.Delete(new AuditItem(1, AuditType.Copy, 123, "test", "blah"), item => throw new Exception("blah!"));
}
catch
{
// We need this catch or nunit throws up.
}
finally
{
Assert.IsTrue(cacheCleared);
}
}
}