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>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Tests.Common.Builders;
|
||||
using Umbraco.Cms.Tests.Common.Builders.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Models
|
||||
{
|
||||
[TestFixture]
|
||||
public class DataTypeTests
|
||||
{
|
||||
private DataTypeBuilder _builder;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp() => _builder = new DataTypeBuilder();
|
||||
|
||||
[Test]
|
||||
public void Can_Deep_Clone()
|
||||
{
|
||||
DataType dtd = _builder
|
||||
.WithId(3123)
|
||||
.Build();
|
||||
|
||||
var clone = (DataType)dtd.DeepClone();
|
||||
|
||||
Assert.AreNotSame(clone, dtd);
|
||||
Assert.AreEqual(clone, dtd);
|
||||
Assert.AreEqual(clone.CreateDate, dtd.CreateDate);
|
||||
Assert.AreEqual(clone.CreatorId, dtd.CreatorId);
|
||||
Assert.AreEqual(clone.DatabaseType, dtd.DatabaseType);
|
||||
Assert.AreEqual(clone.Id, dtd.Id);
|
||||
Assert.AreEqual(clone.Key, dtd.Key);
|
||||
Assert.AreEqual(clone.Level, dtd.Level);
|
||||
Assert.AreEqual(clone.Name, dtd.Name);
|
||||
Assert.AreEqual(clone.ParentId, dtd.ParentId);
|
||||
Assert.AreEqual(clone.Path, dtd.Path);
|
||||
Assert.AreEqual(clone.SortOrder, dtd.SortOrder);
|
||||
Assert.AreEqual(clone.Trashed, dtd.Trashed);
|
||||
Assert.AreEqual(clone.UpdateDate, dtd.UpdateDate);
|
||||
|
||||
// This double verifies by reflection
|
||||
PropertyInfo[] allProps = clone.GetType().GetProperties();
|
||||
foreach (PropertyInfo propertyInfo in allProps)
|
||||
{
|
||||
Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(dtd, null));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Serialize_Without_Error()
|
||||
{
|
||||
DataType item = _builder.Build();
|
||||
|
||||
Assert.DoesNotThrow(() => JsonConvert.SerializeObject(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Models.Entities;
|
||||
using Umbraco.Cms.Tests.Common.Builders;
|
||||
using Umbraco.Cms.Tests.Common.Builders.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Models
|
||||
{
|
||||
[TestFixture]
|
||||
public class PathValidationTests
|
||||
{
|
||||
private EntitySlimBuilder _builder;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp() => _builder = new EntitySlimBuilder();
|
||||
|
||||
[Test]
|
||||
public void Validate_Path()
|
||||
{
|
||||
EntitySlim entity = _builder
|
||||
.WithoutIdentity()
|
||||
.Build();
|
||||
|
||||
// it's empty with no id so we need to allow it
|
||||
Assert.IsTrue(entity.ValidatePath());
|
||||
|
||||
entity.Id = 1234;
|
||||
|
||||
// it has an id but no path, so we can't allow it
|
||||
Assert.IsFalse(entity.ValidatePath());
|
||||
|
||||
entity.Path = "-1";
|
||||
|
||||
// invalid path
|
||||
Assert.IsFalse(entity.ValidatePath());
|
||||
|
||||
entity.Path = string.Concat("-1,", entity.Id);
|
||||
|
||||
// valid path
|
||||
Assert.IsTrue(entity.ValidatePath());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Ensure_Path_Throws_Without_Id()
|
||||
{
|
||||
EntitySlim entity = _builder
|
||||
.WithoutIdentity()
|
||||
.Build();
|
||||
|
||||
// no id assigned
|
||||
Assert.Throws<InvalidOperationException>(() => entity.EnsureValidPath(Mock.Of<ILogger<EntitySlim>>(), umbracoEntity => new EntitySlim(), umbracoEntity => { }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Ensure_Path_Throws_Without_Parent()
|
||||
{
|
||||
EntitySlim entity = _builder
|
||||
.WithId(1234)
|
||||
.WithNoParentId()
|
||||
.Build();
|
||||
|
||||
// no parent found
|
||||
Assert.Throws<NullReferenceException>(() => entity.EnsureValidPath(Mock.Of<ILogger<EntitySlim>>(), umbracoEntity => null, umbracoEntity => { }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Ensure_Path_Entity_At_Root()
|
||||
{
|
||||
EntitySlim entity = _builder
|
||||
.WithId(1234)
|
||||
.Build();
|
||||
|
||||
entity.EnsureValidPath(Mock.Of<ILogger<EntitySlim>>(), umbracoEntity => null, umbracoEntity => { });
|
||||
|
||||
// works because it's under the root
|
||||
Assert.AreEqual("-1,1234", entity.Path);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Ensure_Path_Entity_Valid_Parent()
|
||||
{
|
||||
EntitySlim entity = _builder
|
||||
.WithId(1234)
|
||||
.WithParentId(888)
|
||||
.Build();
|
||||
|
||||
entity.EnsureValidPath(Mock.Of<ILogger<EntitySlim>>(), umbracoEntity => umbracoEntity.ParentId == 888 ? new EntitySlim { Id = 888, Path = "-1,888" } : null, umbracoEntity => { });
|
||||
|
||||
// works because the parent was found
|
||||
Assert.AreEqual("-1,888,1234", entity.Path);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Ensure_Path_Entity_Valid_Recursive_Parent()
|
||||
{
|
||||
EntitySlim parentA = _builder
|
||||
.WithId(999)
|
||||
.Build();
|
||||
|
||||
// Re-creating the class-level builder as we need to reset before usage when creating multiple entities.
|
||||
_builder = new EntitySlimBuilder();
|
||||
EntitySlim parentB = _builder
|
||||
.WithId(888)
|
||||
.WithParentId(999)
|
||||
.Build();
|
||||
|
||||
_builder = new EntitySlimBuilder();
|
||||
EntitySlim parentC = _builder
|
||||
.WithId(777)
|
||||
.WithParentId(888)
|
||||
.Build();
|
||||
|
||||
_builder = new EntitySlimBuilder();
|
||||
EntitySlim entity = _builder
|
||||
.WithId(1234)
|
||||
.WithParentId(777)
|
||||
.Build();
|
||||
|
||||
IUmbracoEntity GetParent(IUmbracoEntity umbracoEntity)
|
||||
{
|
||||
switch (umbracoEntity.ParentId)
|
||||
{
|
||||
case 999:
|
||||
return parentA;
|
||||
case 888:
|
||||
return parentB;
|
||||
case 777:
|
||||
return parentC;
|
||||
case 1234:
|
||||
return entity;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// this will recursively fix all paths
|
||||
entity.EnsureValidPath(Mock.Of<ILogger<IUmbracoEntity>>(), GetParent, umbracoEntity => { });
|
||||
|
||||
Assert.AreEqual("-1,999", parentA.Path);
|
||||
Assert.AreEqual("-1,999,888", parentB.Path);
|
||||
Assert.AreEqual("-1,999,888,777", parentC.Path);
|
||||
Assert.AreEqual("-1,999,888,777,1234", entity.Path);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user