diff --git a/src/Umbraco.Tests.Common/Builders/EntitySlimBuilder.cs b/src/Umbraco.Tests.Common/Builders/EntitySlimBuilder.cs new file mode 100644 index 0000000000..eedef3482c --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/EntitySlimBuilder.cs @@ -0,0 +1,50 @@ +using Umbraco.Core.Models.Entities; +using Umbraco.Tests.Common.Builders.Interfaces; + +namespace Umbraco.Tests.Common.Builders +{ + public class EntitySlimBuilder + : BuilderBase, + 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; + } + } +} diff --git a/src/Umbraco.Tests/Models/PathValidationTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/PathValidationTests.cs similarity index 71% rename from src/Umbraco.Tests/Models/PathValidationTests.cs rename to src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/PathValidationTests.cs index dab362cc35..36a282aaf4 100644 --- a/src/Umbraco.Tests/Models/PathValidationTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/PathValidationTests.cs @@ -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(() => entity.EnsureValidPath(Mock.Of(), 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(() => entity.EnsureValidPath(Mock.Of(), 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(), 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(), 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 getParent = umbracoEntity => { @@ -136,4 +136,4 @@ namespace Umbraco.Tests.Models Assert.AreEqual("-1,999,888,777,1234", entity.Path); } } -} \ No newline at end of file +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 36550066db..9c2e598875 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -142,7 +142,6 @@ -