Migrated macro unit tests to new project and builder pattern.

This commit is contained in:
Andy Butland
2020-04-13 19:22:31 +02:00
parent f961916245
commit a9a8cb7f6f
5 changed files with 325 additions and 10 deletions

View File

@@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Strings;
using Umbraco.Tests.Common.Builders.Extensions;
using Umbraco.Tests.Common.Builders.Interfaces;
namespace Umbraco.Tests.Common.Builders
{
public class MacroBuilder
: BuilderBase<Macro>,
IWithIdBuilder,
IWithKeyBuilder,
IWithAliasBuilder,
IWithNameBuilder
{
private List<MacroPropertyBuilder> _propertyBuilders = new List<MacroPropertyBuilder>();
private int? _id;
private Guid? _key;
private string _alias;
private string _name;
private bool? _useInEditor;
private int? _cacheDuration;
private bool? _cacheByPage;
private bool? _cacheByMember;
private bool? _dontRender;
private string _macroSource;
public MacroBuilder WithUseInEditor(bool useInEditor)
{
_useInEditor = useInEditor;
return this;
}
public MacroBuilder WithCacheDuration(int cacheDuration)
{
_cacheDuration = cacheDuration;
return this;
}
public MacroBuilder WithCacheByPage(bool cacheByPage)
{
_cacheByPage = cacheByPage;
return this;
}
public MacroBuilder WithCacheByMember(bool cacheByMember)
{
_cacheByMember = cacheByMember;
return this;
}
public MacroBuilder WithDontRender(bool dontRender)
{
_dontRender = dontRender;
return this;
}
public MacroBuilder WithSource(string macroSource)
{
_macroSource = macroSource;
return this;
}
public MacroPropertyBuilder AddProperty()
{
var builder = new MacroPropertyBuilder(this);
_propertyBuilders.Add(builder);
return builder;
}
public override Macro Build()
{
var id = _id ?? 1;
var name = _name ?? Guid.NewGuid().ToString();
var alias = _alias ?? name.ToCamelCase();
var key = _key ?? Guid.NewGuid();
var useInEditor = _useInEditor ?? false;
var cacheDuration = _cacheDuration ?? 0;
var cacheByPage = _cacheByPage ?? false;
var cacheByMember = _cacheByMember ?? false;
var dontRender = _dontRender ?? false;
var macroSource = _macroSource ?? string.Empty;
var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig());
var macro = new Macro(shortStringHelper, id, key, useInEditor, cacheDuration, alias, name, cacheByPage, cacheByMember, dontRender, macroSource);
foreach (var property in _propertyBuilders.Select(x => x.Build()))
{
macro.Properties.Add(property);
}
Reset();
return macro;
}
protected override void Reset()
{
_id = null;
_key = null;
_alias = null;
_name = null;
_useInEditor = null;
_cacheDuration = null;
_cacheByPage = null;
_cacheByMember = null;
_dontRender = null;
_macroSource = null;
_propertyBuilders = new List<MacroPropertyBuilder>();
}
int? IWithIdBuilder.Id
{
get => _id;
set => _id = value;
}
Guid? IWithKeyBuilder.Key
{
get => _key;
set => _key = value;
}
string IWithAliasBuilder.Alias
{
get => _alias;
set => _alias = value;
}
string IWithNameBuilder.Name
{
get => _name;
set => _name = value;
}
}
}

View File

@@ -0,0 +1,88 @@
using System;
using Umbraco.Core.Models;
using Umbraco.Tests.Common.Builders.Extensions;
using Umbraco.Tests.Common.Builders.Interfaces;
namespace Umbraco.Tests.Common.Builders
{
public class MacroPropertyBuilder
: ChildBuilderBase<MacroBuilder, IMacroProperty>,
IWithIdBuilder,
IWithKeyBuilder,
IWithAliasBuilder,
IWithNameBuilder,
IWithSortOrderBuilder
{
private int? _id;
private Guid? _key;
private string _alias;
private string _name;
private int? _sortOrder;
private string _editorAlias;
public MacroPropertyBuilder(MacroBuilder parentBuilder) : base(parentBuilder)
{
}
public MacroPropertyBuilder WithEditorAlias(string editorAlias)
{
_editorAlias = editorAlias;
return this;
}
public override IMacroProperty Build()
{
var id = _id ?? 1;
var name = _name ?? Guid.NewGuid().ToString();
var alias = _alias ?? name.ToCamelCase();
var key = _key ?? Guid.NewGuid();
var sortOrder = _sortOrder ?? 0;
var editorAlias = _editorAlias ?? string.Empty;
var macroProperty = new MacroProperty(6, Guid.NewGuid(), alias, name, sortOrder, editorAlias);
Reset();
return macroProperty;
}
protected override void Reset()
{
_id = null;
_key = null;
_alias = null;
_name = null;
_sortOrder = null;
_editorAlias = null;
}
int? IWithIdBuilder.Id
{
get => _id;
set => _id = value;
}
Guid? IWithKeyBuilder.Key
{
get => _key;
set => _key = value;
}
string IWithAliasBuilder.Alias
{
get => _alias;
set => _alias = value;
}
string IWithNameBuilder.Name
{
get => _name;
set => _name = value;
}
int? IWithSortOrderBuilder.SortOrder
{
get => _sortOrder;
set => _sortOrder = value;
}
}
}

View File

@@ -3,18 +3,36 @@ using System.Linq;
using NUnit.Framework;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Entities;
using Umbraco.Tests.TestHelpers;
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 MacroTests
{
private readonly MacroBuilder _builder = new MacroBuilder();
[Test]
public void Can_Deep_Clone()
{
var macro = new Macro(TestHelper.ShortStringHelper, 1, Guid.NewGuid(), true, 3, "test", "Test", false, true, true, "~/script.cshtml");
macro.Properties.Add(new MacroProperty(6, Guid.NewGuid(), "rewq", "REWQ", 1, "asdfasdf"));
var macro = _builder
.WithId(1)
.WithUseInEditor(true)
.WithCacheDuration(3)
.WithAlias("test")
.WithName("Test")
.WithSource("~/script.cshtml")
.WithCacheByMember(true)
.WithDontRender(true)
.AddProperty()
.WithId(6)
.WithAlias("rewq")
.WithName("REWQ")
.WithSortOrder(1)
.WithEditorAlias("asdfasdf")
.Done()
.Build();
var clone = (Macro)macro.DeepClone();
@@ -24,7 +42,7 @@ namespace Umbraco.Tests.Models
Assert.AreEqual(clone.Properties.Count, macro.Properties.Count);
for (int i = 0; i < clone.Properties.Count; i++)
for (var i = 0; i < clone.Properties.Count; i++)
{
Assert.AreEqual(clone.Properties[i], macro.Properties[i]);
Assert.AreNotSame(clone.Properties[i], macro.Properties[i]);
@@ -37,9 +55,7 @@ namespace Umbraco.Tests.Models
//This double verifies by reflection
var allProps = clone.GetType().GetProperties();
foreach (var propertyInfo in allProps)
{
Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(macro, null));
}
//need to ensure the event handlers are wired
@@ -51,8 +67,6 @@ namespace Umbraco.Tests.Models
Assert.AreEqual(1, clone.AddedProperties.Count());
clone.Properties.Remove("rewq");
Assert.AreEqual(1, clone.RemovedProperties.Count());
}
}
}

View File

@@ -0,0 +1,73 @@
using System;
using NUnit.Framework;
using Umbraco.Tests.Common.Builders;
using Umbraco.Tests.Common.Builders.Extensions;
namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders
{
[TestFixture]
public class MacroBuilderTests
{
[Test]
public void Is_Built_Correctly()
{
// Arrange
const int id = 1;
var key = Guid.NewGuid();
const bool useInEditor = true;
const int cacheDuration = 3;
const string alias = "test";
const string name = "Test";
const string source = "~/script.cshtml";
const bool cacheByPage = false;
const bool cacheByMember = true;
const bool dontRender = true;
const int propertyId = 6;
const string propertyAlias = "rewq";
const string propertyName = "REWQ";
const int propertySortOrder = 1;
const string propertyEditorAlias = "asdfasdf";
var builder = new MacroBuilder();
// Act
var macro = builder
.WithId(id)
.WithKey(key)
.WithUseInEditor(useInEditor)
.WithCacheDuration(cacheDuration)
.WithAlias(alias)
.WithName(name)
.WithSource(source)
.WithCacheByPage(cacheByPage)
.WithCacheByMember(cacheByMember)
.WithDontRender(dontRender)
.AddProperty()
.WithId(propertyId)
.WithAlias(propertyAlias)
.WithName(propertyName)
.WithSortOrder(propertySortOrder)
.WithEditorAlias(propertyEditorAlias)
.Done()
.Build();
// Assert
Assert.AreEqual(id, macro.Id);
Assert.AreEqual(key, macro.Key);
Assert.AreEqual(useInEditor, macro.UseInEditor);
Assert.AreEqual(cacheDuration, macro.CacheDuration);
Assert.AreEqual(alias, macro.Alias);
Assert.AreEqual(name, macro.Name);
Assert.AreEqual(source, macro.MacroSource);
Assert.AreEqual(cacheByPage, macro.CacheByPage);
Assert.AreEqual(cacheByMember, macro.CacheByMember);
Assert.AreEqual(dontRender, macro.DontRender);
Assert.AreEqual(1, macro.Properties.Count);
Assert.AreEqual(propertyId, macro.Properties[0].Id);
Assert.AreEqual(propertyAlias, macro.Properties[0].Alias);
Assert.AreEqual(propertyName, macro.Properties[0].Name);
Assert.AreEqual(propertySortOrder, macro.Properties[0].SortOrder);
Assert.AreEqual(propertyEditorAlias, macro.Properties[0].EditorAlias);
}
}
}

View File

@@ -288,7 +288,6 @@
<Compile Include="Migrations\Stubs\FourElevenMigration.cs" />
<Compile Include="Migrations\Stubs\SixZeroMigration2.cs" />
<Compile Include="Testing\TestingTests\MockTests.cs" />
<Compile Include="Models\MacroTests.cs" />
<Compile Include="Models\Collections\Item.cs" />
<Compile Include="Models\Collections\OrderItem.cs" />
<Compile Include="Models\Collections\SimpleOrder.cs" />