Migrated member group unit tests to new unit test project using builder pattern.

This commit is contained in:
Andy Butland
2020-03-28 20:54:10 +01:00
parent a72ae2278d
commit 50631c2b28
9 changed files with 162 additions and 29 deletions
@@ -9,7 +9,6 @@ namespace Umbraco.Tests.Common.Builders
_parentBuilder = parentBuilder;
}
public TParent Done()
{
return _parentBuilder;
@@ -8,6 +8,7 @@ namespace Umbraco.Tests.Common.Builders
: BuilderBase<DataType>,
IWithIdBuilder,
IWithKeyBuilder,
IWithCreatorIdBuilder,
IWithCreateDateBuilder,
IWithUpdateDateBuilder,
IWithDeleteDateBuilder,
@@ -133,6 +134,12 @@ namespace Umbraco.Tests.Common.Builders
set => _key = value;
}
int? IWithCreatorIdBuilder.CreatorId
{
get => _creatorId;
set => _creatorId = value;
}
DateTime? IWithCreateDateBuilder.CreateDate
{
get => _createDate;
@@ -12,6 +12,13 @@ namespace Umbraco.Tests.Common.Builders.Extensions
return builder;
}
public static T WithCreatorId<T>(this T builder, int creatorId)
where T : IWithCreatorIdBuilder
{
builder.CreatorId = creatorId;
return builder;
}
public static T WithCreateDate<T>(this T builder, DateTime createDate)
where T : IWithCreateDateBuilder
{
@@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace Umbraco.Tests.Common.Builders
{
public class GenericDictionaryBuilder<TBuilder, TKey, TValue>
: ChildBuilderBase<TBuilder, IDictionary<TKey, TValue>>
{
private readonly IDictionary<TKey, TValue> _dictionary;
public GenericDictionaryBuilder(TBuilder parentBuilder) : base(parentBuilder)
{
_dictionary = new Dictionary<TKey, TValue>();
}
public override IDictionary<TKey, TValue> Build()
{
return new Dictionary<TKey, TValue>();
}
public GenericDictionaryBuilder<TBuilder, TKey, TValue> AddKeyValue(TKey key, TValue value)
{
_dictionary.Add(key, value);
return this;
}
}
}
@@ -0,0 +1,9 @@
using System;
namespace Umbraco.Tests.Common.Builders.Interfaces
{
public interface IWithCreatorIdBuilder
{
int? CreatorId { get; set; }
}
}
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using Umbraco.Core.Models;
using Umbraco.Tests.Common.Builders.Interfaces;
namespace Umbraco.Tests.Common.Builders
{
public class MemberGroupBuilder
: BuilderBase<MemberGroup>,
IWithIdBuilder,
IWithKeyBuilder,
IWithCreatorIdBuilder,
IWithCreateDateBuilder,
IWithUpdateDateBuilder,
IWithNameBuilder
{
private int? _id;
private Guid? _key;
private DateTime? _createDate;
private DateTime? _updateDate;
private string _name;
private int? _creatorId;
private IDictionary<string, object> _additionalData = new Dictionary<string, object>();
private GenericDictionaryBuilder<MemberGroupBuilder, string, object> _additionalDataBuilder;
public GenericDictionaryBuilder<MemberGroupBuilder, string, object> AddAdditionalData()
{
var builder = new GenericDictionaryBuilder<MemberGroupBuilder, string, object>(this);
_additionalDataBuilder = builder;
return builder;
}
public override MemberGroup Build()
{
var id = _id ?? 1;
var key = _key ?? Guid.NewGuid();
var createDate = _createDate ?? DateTime.Now;
var updateDate = _updateDate ?? DateTime.Now;
var name = _name ?? Guid.NewGuid().ToString();
var creatorId = _creatorId ?? 1;
return new MemberGroup
{
Id = id,
Key = key,
CreateDate = createDate,
UpdateDate = updateDate,
Name = name,
CreatorId = creatorId,
};
}
int? IWithIdBuilder.Id
{
get => _id;
set => _id = value;
}
Guid? IWithKeyBuilder.Key
{
get => _key;
set => _key = value;
}
int? IWithCreatorIdBuilder.CreatorId
{
get => _creatorId;
set => _creatorId = value;
}
DateTime? IWithCreateDateBuilder.CreateDate
{
get => _createDate;
set => _createDate = value;
}
DateTime? IWithUpdateDateBuilder.UpdateDate
{
get => _updateDate;
set => _updateDate = value;
}
string IWithNameBuilder.Name
{
get => _name;
set => _name = value;
}
}
}
@@ -10,8 +10,8 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
[TestFixture]
public class DataTypeTests
{
private readonly DataTypeBuilder _builder = new DataTypeBuilder();
[Test]
public void Can_Deep_Clone()
{
@@ -3,28 +3,21 @@ using System.Diagnostics;
using Newtonsoft.Json;
using NUnit.Framework;
using Umbraco.Core.Models;
using Umbraco.Core.Serialization;
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 MemberGroupTests
{
private readonly MemberGroupBuilder _builder = new MemberGroupBuilder();
[Test]
public void Can_Deep_Clone()
{
// Arrange
var group = new MemberGroup()
{
CreateDate = DateTime.Now,
CreatorId = 4,
Id = 6,
Key = Guid.NewGuid(),
UpdateDate = DateTime.Now,
Name = "asdf"
};
group.AdditionalData.Add("test1", 123);
group.AdditionalData.Add("test2", "hello");
var group = BuildMemberGroup();
// Act
var clone = (MemberGroup)group.DeepClone();
@@ -44,29 +37,32 @@ 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(group, null));
}
}
[Test]
public void Can_Serialize_Without_Error()
{
var group = new MemberGroup()
{
CreateDate = DateTime.Now,
CreatorId = 4,
Id = 6,
Key = Guid.NewGuid(),
UpdateDate = DateTime.Now,
Name = "asdf"
};
group.AdditionalData.Add("test1", 123);
group.AdditionalData.Add("test2", "hello");
var group = BuildMemberGroup();
var json = JsonConvert.SerializeObject(group);
Debug.Print(json);
}
private MemberGroup BuildMemberGroup()
{
return _builder
.WithId(6)
.WithKey(Guid.NewGuid())
.WithName("asdf")
.WithCreatorId(4)
.WithCreateDate(DateTime.Now)
.WithUpdateDate(DateTime.Now)
.AddAdditionalData()
.AddKeyValue("test1", 123)
.AddKeyValue("test2", "hello")
.Done()
.Build();
}
}
}
-1
View File
@@ -298,7 +298,6 @@
<Compile Include="Models\ContentTypeTests.cs" />
<Compile Include="Models\DeepCloneHelperTests.cs" />
<Compile Include="Models\DictionaryTranslationTests.cs" />
<Compile Include="Models\MemberGroupTests.cs" />
<Compile Include="Models\MemberTests.cs" />
<Compile Include="Models\PropertyGroupTests.cs" />
<Compile Include="Models\PropertyTypeTests.cs" />