NetCore: Further unit test migration (#9269)

* Migrated ContentExtensionsTests.

* Migrated PropertyCollectionTests.

* Migrated AbstractFileSystemTests and PhysicalFileSystem tests.

* Migrated SiteDomainHelper tests.

* Migrated DistributedCacheTests.

* Migrated AppCacheTests and derived test classes.
Amended HttpRequestApp underlying dictionary type to match that available in .NET Core HttpContext Items collection.

* Fixed namespace

Signed-off-by: Bjarke Berg <mail@bergmania.dk>

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
Andy Butland
2020-10-26 14:26:49 +01:00
committed by GitHub
parent 9cd752c2f8
commit 507facbdaa
21 changed files with 223 additions and 295 deletions

View File

@@ -1,261 +0,0 @@
using System;
using System.Linq;
using System.Web.UI;
using NUnit.Framework;
using Umbraco.Core.Cache;
namespace Umbraco.Tests.Cache
{
public abstract class AppCacheTests
{
internal abstract IAppCache AppCache { get; }
protected abstract int GetTotalItemCount { get; }
[SetUp]
public virtual void Setup()
{
}
[TearDown]
public virtual void TearDown()
{
AppCache.Clear();
}
[Test]
public void Throws_On_Reentry()
{
// don't run for DictionaryAppCache - not making sense
if (GetType() == typeof (DictionaryAppCacheTests))
Assert.Ignore("Do not run for DictionaryAppCache.");
Exception exception = null;
var result = AppCache.Get("blah", () =>
{
try
{
var result2 = AppCache.Get("blah");
}
catch (Exception e)
{
exception = e;
}
return "value";
});
Assert.IsNotNull(exception);
Assert.IsAssignableFrom<InvalidOperationException>(exception);
}
[Test]
public void Does_Not_Cache_Exceptions()
{
var counter = 0;
object result;
try
{
result = AppCache.Get("Blah", () =>
{
counter++;
throw new Exception("Do not cache this");
});
}
catch (Exception){}
try
{
result = AppCache.Get("Blah", () =>
{
counter++;
throw new Exception("Do not cache this");
});
}
catch (Exception){}
Assert.Greater(counter, 1);
}
[Test]
public void Ensures_Delegate_Result_Is_Cached_Once()
{
var counter = 0;
object result;
result = AppCache.Get("Blah", () =>
{
counter++;
return "";
});
result = AppCache.Get("Blah", () =>
{
counter++;
return "";
});
Assert.AreEqual(1, counter);
}
[Test]
public void Can_Get_By_Search()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
var cacheContent4 = new LiteralControl();
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Tester2", () => cacheContent2);
AppCache.Get("Tes3", () => cacheContent3);
AppCache.Get("different4", () => cacheContent4);
Assert.AreEqual(4, GetTotalItemCount);
var result = AppCache.SearchByKey("Tes");
Assert.AreEqual(3, result.Count());
}
[Test]
public void Can_Clear_By_Expression()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
var cacheContent4 = new LiteralControl();
AppCache.Get("TTes1t", () => cacheContent1);
AppCache.Get("Tester2", () => cacheContent2);
AppCache.Get("Tes3", () => cacheContent3);
AppCache.Get("different4", () => cacheContent4);
Assert.AreEqual(4, GetTotalItemCount);
AppCache.ClearByRegex("^\\w+es\\d.*");
Assert.AreEqual(2, GetTotalItemCount);
}
[Test]
public void Can_Clear_By_Search()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
var cacheContent4 = new LiteralControl();
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Tester2", () => cacheContent2);
AppCache.Get("Tes3", () => cacheContent3);
AppCache.Get("different4", () => cacheContent4);
Assert.AreEqual(4, GetTotalItemCount);
AppCache.ClearByKey("Test");
Assert.AreEqual(2, GetTotalItemCount);
}
[Test]
public void Can_Clear_By_Key()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
var cacheContent4 = new LiteralControl();
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Test2", () => cacheContent2);
AppCache.Get("Test3", () => cacheContent3);
AppCache.Get("Test4", () => cacheContent4);
Assert.AreEqual(4, GetTotalItemCount);
AppCache.Clear("Test1");
AppCache.Clear("Test2");
Assert.AreEqual(2, GetTotalItemCount);
}
[Test]
public void Can_Clear_All_Items()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
var cacheContent4 = new LiteralControl();
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Test2", () => cacheContent2);
AppCache.Get("Test3", () => cacheContent3);
AppCache.Get("Test4", () => cacheContent4);
Assert.AreEqual(4, GetTotalItemCount);
AppCache.Clear();
Assert.AreEqual(0, GetTotalItemCount);
}
[Test]
public void Can_Add_When_Not_Available()
{
var cacheContent1 = new MacroCacheContent();
AppCache.Get("Test1", () => cacheContent1);
Assert.AreEqual(1, GetTotalItemCount);
}
[Test]
public void Can_Get_When_Available()
{
var cacheContent1 = new MacroCacheContent();
var result = AppCache.Get("Test1", () => cacheContent1);
var result2 = AppCache.Get("Test1", () => cacheContent1);
Assert.AreEqual(1, GetTotalItemCount);
Assert.AreEqual(result, result2);
}
[Test]
public void Can_Remove_By_Type_Name()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
var cacheContent4 = new LiteralControl();
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Test2", () => cacheContent2);
AppCache.Get("Test3", () => cacheContent3);
AppCache.Get("Test4", () => cacheContent4);
Assert.AreEqual(4, GetTotalItemCount);
//Provider.ClearCacheObjectTypes("umbraco.MacroCacheContent");
AppCache.ClearOfType<MacroCacheContent>();
Assert.AreEqual(1, GetTotalItemCount);
}
[Test]
public void Can_Remove_By_Strong_Type()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
var cacheContent4 = new LiteralControl();
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Test2", () => cacheContent2);
AppCache.Get("Test3", () => cacheContent3);
AppCache.Get("Test4", () => cacheContent4);
Assert.AreEqual(4, GetTotalItemCount);
AppCache.ClearOfType<MacroCacheContent>();
Assert.AreEqual(1, GetTotalItemCount);
}
//just used for these tests
private class MacroCacheContent
{
}
}
}

View File

@@ -1,122 +0,0 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Web;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Collections;
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Entities;
using Umbraco.Tests.Common;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web.Cache;
namespace Umbraco.Tests.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);
original.Add(new TestClone());
original.Add(new TestClone());
original.Add(new TestClone());
var val = _provider.GetCacheItem<DeepCloneableList<TestClone>>("test", () => original);
Assert.AreEqual(original.Count, val.Count);
foreach (var 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<TestClass>("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;
}
}
}
}

View File

@@ -1,209 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Umbraco.Core.Cache;
using Umbraco.Web.Composing;
using Umbraco.Core.Sync;
namespace Umbraco.Tests.Cache.DistributedCache
{
/// <summary>
/// Ensures that calls to DistributedCache methods carry through to the IServerMessenger correctly
/// </summary>
[TestFixture]
public class DistributedCacheTests
{
private Umbraco.Web.Cache.DistributedCache _distributedCache;
private IServerRegistrar ServerRegistrar { get; set; }
private TestServerMessenger ServerMessenger { get; set; }
[SetUp]
public void Setup()
{
ServerRegistrar = new TestServerRegistrar();
ServerMessenger = new TestServerMessenger();
var cacheRefresherCollection = new CacheRefresherCollection(new []
{
new TestCacheRefresher()
});
_distributedCache = new Umbraco.Web.Cache.DistributedCache(ServerMessenger, cacheRefresherCollection);
}
[TearDown]
public void Teardown()
{
Current.Reset();
}
[Test]
public void RefreshIntId()
{
for (var i = 1; i < 11; i++)
{
_distributedCache.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i);
}
Assert.AreEqual(10, ServerMessenger.IntIdsRefreshed.Count);
}
[Test]
public void RefreshIntIdFromObject()
{
for (var i = 0; i < 10; i++)
{
_distributedCache.Refresh(
Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"),
x => x.Id,
new TestObjectWithId{Id = i});
}
Assert.AreEqual(10, ServerMessenger.IntIdsRefreshed.Count);
}
[Test]
public void RefreshGuidId()
{
for (var i = 0; i < 11; i++)
{
_distributedCache.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), Guid.NewGuid());
}
Assert.AreEqual(11, ServerMessenger.GuidIdsRefreshed.Count);
}
[Test]
public void RemoveIds()
{
for (var i = 1; i < 13; i++)
{
_distributedCache.Remove(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i);
}
Assert.AreEqual(12, ServerMessenger.IntIdsRemoved.Count);
}
[Test]
public void FullRefreshes()
{
for (var i = 0; i < 13; i++)
{
_distributedCache.RefreshAll(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"));
}
Assert.AreEqual(13, ServerMessenger.CountOfFullRefreshes);
}
#region internal test classes
internal class TestObjectWithId
{
public int Id { get; set; }
}
internal class TestCacheRefresher : ICacheRefresher
{
public static readonly Guid UniqueId = Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73");
public Guid RefresherUniqueId => UniqueId;
public string Name => "Test Cache Refresher";
public void RefreshAll()
{ }
public void Refresh(int id)
{ }
public void Remove(int id)
{ }
public void Refresh(Guid id)
{ }
}
internal class TestServerMessenger : IServerMessenger
{
//used for tests
public List<int> IntIdsRefreshed = new List<int>();
public List<Guid> GuidIdsRefreshed = new List<Guid>();
public List<int> IntIdsRemoved = new List<int>();
public List<string> PayloadsRemoved = new List<string>();
public List<string> PayloadsRefreshed = new List<string>();
public int CountOfFullRefreshes = 0;
public void PerformRefresh<TPayload>(ICacheRefresher refresher, TPayload[] payload)
{
// doing nothing
}
public void PerformRefresh(ICacheRefresher refresher, string jsonPayload)
{
PayloadsRefreshed.Add(jsonPayload);
}
public void PerformRefresh<T>(ICacheRefresher refresher, Func<T, int> getNumericId, params T[] instances)
{
IntIdsRefreshed.AddRange(instances.Select(getNumericId));
}
public void PerformRefresh<T>(ICacheRefresher refresher, Func<T, Guid> getGuidId, params T[] instances)
{
GuidIdsRefreshed.AddRange(instances.Select(getGuidId));
}
public void PerformRemove(ICacheRefresher refresher, string jsonPayload)
{
PayloadsRemoved.Add(jsonPayload);
}
public void PerformRemove<T>(ICacheRefresher refresher, Func<T, int> getNumericId, params T[] instances)
{
IntIdsRemoved.AddRange(instances.Select(getNumericId));
}
public void PerformRemove(ICacheRefresher refresher, params int[] numericIds)
{
IntIdsRemoved.AddRange(numericIds);
}
public void PerformRefresh(ICacheRefresher refresher, params int[] numericIds)
{
IntIdsRefreshed.AddRange(numericIds);
}
public void PerformRefresh(ICacheRefresher refresher, params Guid[] guidIds)
{
GuidIdsRefreshed.AddRange(guidIds);
}
public void PerformRefreshAll(ICacheRefresher refresher)
{
CountOfFullRefreshes++;
}
}
internal class TestServerRegistrar : IServerRegistrar
{
public IEnumerable<IServerAddress> Registrations => new List<IServerAddress>
{
new TestServerAddress("localhost")
};
public ServerRole GetCurrentServerRole()
{
throw new NotImplementedException();
}
}
public class TestServerAddress : IServerAddress
{
public TestServerAddress(string address)
{
ServerAddress = address;
}
public string ServerAddress { get; private set; }
}
#endregion
}
}

View File

@@ -1,55 +0,0 @@
using Moq;
using NUnit.Framework;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Cache
{
[TestFixture]
public class HttpRequestAppCacheTests : AppCacheTests
{
private HttpRequestAppCache _appCache;
private FakeHttpContextFactory _ctx;
public override void Setup()
{
base.Setup();
_ctx = new FakeHttpContextFactory("http://localhost/test");
_appCache = new HttpRequestAppCache(() => _ctx.HttpContext.Items);
}
internal override IAppCache AppCache
{
get { return _appCache; }
}
protected override int GetTotalItemCount
{
get { return _ctx.HttpContext.Items.Count; }
}
}
[TestFixture]
public class DictionaryAppCacheTests : AppCacheTests
{
private DictionaryAppCache _appCache;
public override void Setup()
{
base.Setup();
_appCache = new DictionaryAppCache();
}
internal override IAppCache AppCache
{
get { return _appCache; }
}
protected override int GetTotalItemCount
{
get { return _appCache.Count; }
}
}
}

View File

@@ -1,34 +0,0 @@
using System.Linq;
using NUnit.Framework;
using Umbraco.Core.Cache;
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Cache
{
[TestFixture]
public class ObjectAppCacheTests : RuntimeAppCacheTests
{
private ObjectCacheAppCache _provider;
protected override int GetTotalItemCount
{
get { return _provider.MemoryCache.Count(); }
}
public override void Setup()
{
base.Setup();
_provider = new ObjectCacheAppCache();
}
internal override IAppCache AppCache
{
get { return _provider; }
}
internal override IAppPolicyCache AppPolicyCache
{
get { return _provider; }
}
}
}

View File

@@ -1,27 +0,0 @@
using System;
using System.Threading;
using NUnit.Framework;
using Umbraco.Core.Cache;
namespace Umbraco.Tests.Cache
{
public abstract class RuntimeAppCacheTests : AppCacheTests
{
internal abstract IAppPolicyCache AppPolicyCache { get; }
[Test]
[Explicit("Testing for timeouts cannot work on VSTS.")]
public void Can_Add_And_Expire_Struct_Strongly_Typed_With_Null()
{
var now = DateTime.Now;
AppPolicyCache.Insert("DateTimeTest", () => now, new TimeSpan(0, 0, 0, 0, 200));
Assert.AreEqual(now, AppCache.GetCacheItem<DateTime>("DateTimeTest"));
Assert.AreEqual(now, AppCache.GetCacheItem<DateTime?>("DateTimeTest"));
Thread.Sleep(300); //sleep longer than the cache expiration
Assert.AreEqual(default(DateTime), AppCache.GetCacheItem<DateTime>("DateTimeTest"));
Assert.AreEqual(null, AppCache.GetCacheItem<DateTime?>("DateTimeTest"));
}
}
}