Migrated parth validation tests to new project and builder pattern.

This commit is contained in:
Andy Butland
2020-04-11 17:21:27 +02:00
parent 4456a784b0
commit 3085ce52f9
3 changed files with 88 additions and 39 deletions

View File

@@ -0,0 +1,50 @@
using Umbraco.Core.Models.Entities;
using Umbraco.Tests.Common.Builders.Interfaces;
namespace Umbraco.Tests.Common.Builders
{
public class EntitySlimBuilder
: BuilderBase<EntitySlim>,
IWithIdBuilder,
IWithParentIdBuilder
{
private int? _id;
private int? _parentId;
public override EntitySlim Build()
{
var id = _id ?? 1;
var parentId = _parentId ?? -1;
return new EntitySlim
{
Id = id,
ParentId = parentId,
};
}
public EntitySlimBuilder WithNoId()
{
_id = 0;
return this;
}
public EntitySlimBuilder WithNoParentId()
{
_parentId = 0;
return this;
}
int? IWithIdBuilder.Id
{
get => _id;
set => _id = value;
}
int? IWithParentIdBuilder.ParentId
{
get => _parentId;
set => _parentId = value;
}
}
}

View File

@@ -4,16 +4,22 @@ using NUnit.Framework;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Entities;
using Umbraco.Tests.Common.Builders;
using Umbraco.Tests.Common.Builders.Extensions;
namespace Umbraco.Tests.Models
namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
{
[TestFixture]
public class PathValidationTests
{
private readonly EntitySlimBuilder _builder = new EntitySlimBuilder();
[Test]
public void Validate_Path()
{
var entity = new EntitySlim();
var entity = _builder
.WithNoId()
.Build();
//it's empty with no id so we need to allow it
Assert.IsTrue(entity.ValidatePath());
@@ -37,7 +43,9 @@ namespace Umbraco.Tests.Models
[Test]
public void Ensure_Path_Throws_Without_Id()
{
var entity = new EntitySlim();
var entity = _builder
.WithNoId()
.Build();
//no id assigned
Assert.Throws<InvalidOperationException>(() => entity.EnsureValidPath(Mock.Of<ILogger>(), umbracoEntity => new EntitySlim(), umbracoEntity => { }));
@@ -46,7 +54,10 @@ namespace Umbraco.Tests.Models
[Test]
public void Ensure_Path_Throws_Without_Parent()
{
var entity = new EntitySlim { Id = 1234 };
var entity = _builder
.WithId(1234)
.WithNoParentId()
.Build();
//no parent found
Assert.Throws<NullReferenceException>(() => entity.EnsureValidPath(Mock.Of<ILogger>(), umbracoEntity => null, umbracoEntity => { }));
@@ -55,12 +66,9 @@ namespace Umbraco.Tests.Models
[Test]
public void Ensure_Path_Entity_At_Root()
{
var entity = new EntitySlim
{
Id = 1234,
ParentId = -1
};
var entity = _builder
.WithId(1234)
.Build();
entity.EnsureValidPath(Mock.Of<ILogger>(), umbracoEntity => null, umbracoEntity => { });
@@ -71,11 +79,10 @@ namespace Umbraco.Tests.Models
[Test]
public void Ensure_Path_Entity_Valid_Parent()
{
var entity = new EntitySlim
{
Id = 1234,
ParentId = 888
};
var entity = _builder
.WithId(1234)
.WithParentId(888)
.Build();
entity.EnsureValidPath(Mock.Of<ILogger>(), umbracoEntity => umbracoEntity.ParentId == 888 ? new EntitySlim { Id = 888, Path = "-1,888" } : null, umbracoEntity => { });
@@ -86,29 +93,22 @@ namespace Umbraco.Tests.Models
[Test]
public void Ensure_Path_Entity_Valid_Recursive_Parent()
{
var parentA = new EntitySlim
{
Id = 999,
ParentId = -1
};
var parentA = _builder
.WithId(999)
.Build();
var parentB = _builder
.WithId(888)
.WithParentId(999)
.Build();
var parentC = _builder
.WithId(777)
.WithParentId(888)
.Build();
var parentB = new EntitySlim
{
Id = 888,
ParentId = 999
};
var parentC = new EntitySlim
{
Id = 777,
ParentId = 888
};
var entity = new EntitySlim
{
Id = 1234,
ParentId = 777
};
var entity = _builder
.WithId(1234)
.WithParentId(777)
.Build();
Func<IUmbracoEntity, IUmbracoEntity> getParent = umbracoEntity =>
{
@@ -136,4 +136,4 @@ namespace Umbraco.Tests.Models
Assert.AreEqual("-1,999,888,777,1234", entity.Path);
}
}
}
}

View File

@@ -142,7 +142,6 @@
<Compile Include="Models\ContentScheduleTests.cs" />
<Compile Include="Models\CultureImpactTests.cs" />
<Compile Include="Models\ImageProcessorImageUrlGeneratorTest.cs" />
<Compile Include="Models\PathValidationTests.cs" />
<Compile Include="Models\VariationTests.cs" />
<Compile Include="Persistence\Mappers\MapperTestBase.cs" />
<Compile Include="Persistence\Repositories\DocumentRepositoryTest.cs" />