From 50631c2b2894168915c7851ce2c5cc4d8ae59360 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Sat, 28 Mar 2020 20:54:10 +0100 Subject: [PATCH 1/4] Migrated member group unit tests to new unit test project using builder pattern. --- .../Builders/ChildBuilderBase.cs | 1 - .../Builders/DataTypeBuilder.cs | 7 ++ .../Builders/Extensions/BuilderExtensions.cs | 7 ++ .../Builders/GenericDictionaryBuilder.cs | 26 ++++++ .../Interfaces/IWithCreatorIdBuilder.cs | 9 ++ .../Builders/MemberGroupBuilder.cs | 90 +++++++++++++++++++ .../Models/DataTypeTests.cs | 2 +- .../Models/MemberGroupTests.cs | 48 +++++----- src/Umbraco.Tests/Umbraco.Tests.csproj | 1 - 9 files changed, 162 insertions(+), 29 deletions(-) create mode 100644 src/Umbraco.Tests.Common/Builders/GenericDictionaryBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/Interfaces/IWithCreatorIdBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/MemberGroupBuilder.cs rename src/{Umbraco.Tests => Umbraco.Tests.UnitTests/Umbraco.Infrastructure}/Models/MemberGroupTests.cs (61%) diff --git a/src/Umbraco.Tests.Common/Builders/ChildBuilderBase.cs b/src/Umbraco.Tests.Common/Builders/ChildBuilderBase.cs index e1436ac1fe..fcd9691e1f 100644 --- a/src/Umbraco.Tests.Common/Builders/ChildBuilderBase.cs +++ b/src/Umbraco.Tests.Common/Builders/ChildBuilderBase.cs @@ -9,7 +9,6 @@ namespace Umbraco.Tests.Common.Builders _parentBuilder = parentBuilder; } - public TParent Done() { return _parentBuilder; diff --git a/src/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs b/src/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs index 20dc1bab81..04a464e5bd 100644 --- a/src/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs +++ b/src/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs @@ -8,6 +8,7 @@ namespace Umbraco.Tests.Common.Builders : BuilderBase, 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; diff --git a/src/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs b/src/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs index c8f3f80bf1..6a2407ae53 100644 --- a/src/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs +++ b/src/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs @@ -12,6 +12,13 @@ namespace Umbraco.Tests.Common.Builders.Extensions return builder; } + public static T WithCreatorId(this T builder, int creatorId) + where T : IWithCreatorIdBuilder + { + builder.CreatorId = creatorId; + return builder; + } + public static T WithCreateDate(this T builder, DateTime createDate) where T : IWithCreateDateBuilder { diff --git a/src/Umbraco.Tests.Common/Builders/GenericDictionaryBuilder.cs b/src/Umbraco.Tests.Common/Builders/GenericDictionaryBuilder.cs new file mode 100644 index 0000000000..0cf2284a49 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/GenericDictionaryBuilder.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; + +namespace Umbraco.Tests.Common.Builders +{ + public class GenericDictionaryBuilder + : ChildBuilderBase> + { + private readonly IDictionary _dictionary; + + public GenericDictionaryBuilder(TBuilder parentBuilder) : base(parentBuilder) + { + _dictionary = new Dictionary(); + } + + public override IDictionary Build() + { + return new Dictionary(); + } + + public GenericDictionaryBuilder AddKeyValue(TKey key, TValue value) + { + _dictionary.Add(key, value); + return this; + } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/Interfaces/IWithCreatorIdBuilder.cs b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithCreatorIdBuilder.cs new file mode 100644 index 0000000000..ae7712cf9e --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithCreatorIdBuilder.cs @@ -0,0 +1,9 @@ +using System; + +namespace Umbraco.Tests.Common.Builders.Interfaces +{ + public interface IWithCreatorIdBuilder + { + int? CreatorId { get; set; } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/MemberGroupBuilder.cs b/src/Umbraco.Tests.Common/Builders/MemberGroupBuilder.cs new file mode 100644 index 0000000000..6092c79abb --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/MemberGroupBuilder.cs @@ -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, + 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 _additionalData = new Dictionary(); + + private GenericDictionaryBuilder _additionalDataBuilder; + + public GenericDictionaryBuilder AddAdditionalData() + { + var builder = new GenericDictionaryBuilder(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; + } + } +} diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs index a097661a93..4be32a9b4f 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs @@ -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() { diff --git a/src/Umbraco.Tests/Models/MemberGroupTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs similarity index 61% rename from src/Umbraco.Tests/Models/MemberGroupTests.cs rename to src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs index c3cf1c8c76..158a70598c 100644 --- a/src/Umbraco.Tests/Models/MemberGroupTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs @@ -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(); + } } } diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 06f6a98573..eb500bf7e3 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -298,7 +298,6 @@ - From dface90de7f9a56a95a80f29d9eabd08571dbfd6 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Sun, 29 Mar 2020 16:17:06 +0200 Subject: [PATCH 2/4] Migrated member unit tests to new unit test project using builder pattern. --- .../Builders/ConfigurationEditorBuilder.cs | 2 +- .../Builders/DataTypeBuilder.cs | 73 +++-- .../Builders/DictionaryTranslationBuilder.cs | 1 - .../Builders/Extensions/BuilderExtensions.cs | 56 ++++ .../Builders/Extensions/StringExtensions.cs | 20 ++ .../Builders/GenericCollectionBuilder.cs | 26 ++ .../Builders/GenericDictionaryBuilder.cs | 4 +- .../Interfaces/IWithDescriptionBuilder.cs | 7 + .../Builders/Interfaces/IWithIconBuilder.cs | 7 + .../Builders/Interfaces/IWithLevelBuilder.cs | 7 + .../Interfaces/IWithParentIdBuilder.cs | 7 + .../Builders/Interfaces/IWithPathBuilder.cs | 7 + .../Interfaces/IWithSortOrderBuilder.cs | 7 + .../Interfaces/IWithThumbnailBuilder.cs | 7 + .../Interfaces/IWithTrashedBuilder.cs | 7 + .../Builders/MemberBuilder.cs | 280 ++++++++++++++++++ .../Builders/MemberGroupBuilder.cs | 19 +- .../Builders/MemberTypeBuilder.cs | 198 +++++++++++++ .../Builders/PropertyGroupBuilder.cs | 60 ++++ .../Builders/PropertyTypeBuilder.cs | 93 ++++++ .../Models/MemberGroupTests.cs | 4 +- .../Models/MemberTests.cs | 154 ++++++++++ src/Umbraco.Tests/Models/MemberTests.cs | 134 --------- src/Umbraco.Tests/Umbraco.Tests.csproj | 1 - 24 files changed, 998 insertions(+), 183 deletions(-) create mode 100644 src/Umbraco.Tests.Common/Builders/Extensions/StringExtensions.cs create mode 100644 src/Umbraco.Tests.Common/Builders/GenericCollectionBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/Interfaces/IWithDescriptionBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/Interfaces/IWithIconBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/Interfaces/IWithLevelBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/Interfaces/IWithParentIdBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/Interfaces/IWithPathBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/Interfaces/IWithSortOrderBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/Interfaces/IWithThumbnailBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/Interfaces/IWithTrashedBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/MemberBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/MemberTypeBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/PropertyGroupBuilder.cs create mode 100644 src/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs create mode 100644 src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs delete mode 100644 src/Umbraco.Tests/Models/MemberTests.cs diff --git a/src/Umbraco.Tests.Common/Builders/ConfigurationEditorBuilder.cs b/src/Umbraco.Tests.Common/Builders/ConfigurationEditorBuilder.cs index 7789090d16..3e0b9b96a9 100644 --- a/src/Umbraco.Tests.Common/Builders/ConfigurationEditorBuilder.cs +++ b/src/Umbraco.Tests.Common/Builders/ConfigurationEditorBuilder.cs @@ -21,7 +21,7 @@ namespace Umbraco.Tests.Common.Builders public override IConfigurationEditor Build() { - var defaultConfiguration = _defaultConfiguration ?? new Dictionary(); + var defaultConfiguration = _defaultConfiguration ?? new Dictionary(); return new ConfigurationEditor() { diff --git a/src/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs b/src/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs index 04a464e5bd..de7b8d9a97 100644 --- a/src/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs +++ b/src/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs @@ -12,7 +12,12 @@ namespace Umbraco.Tests.Common.Builders IWithCreateDateBuilder, IWithUpdateDateBuilder, IWithDeleteDateBuilder, - IWithNameBuilder + IWithNameBuilder, + IWithParentIdBuilder, + IWithTrashedBuilder, + IWithLevelBuilder, + IWithPathBuilder, + IWithSortOrderBuilder { private readonly DataEditorBuilder _dataEditorBuilder; private int? _id; @@ -35,54 +40,18 @@ namespace Umbraco.Tests.Common.Builders _dataEditorBuilder = new DataEditorBuilder(this); } - public DataTypeBuilder WithParentId(int parentId) - { - _parentId = parentId; - return this; - } - - public DataTypeBuilder WithTrashed(bool trashed) - { - _trashed = trashed; - return this; - } - // public DataTypeBuilder WithConfiguration(object configuration) // { // _configuration = configuration; // return this; // } - public DataTypeBuilder WithLevel(int level) - { - _level = level; - return this; - } - - public DataTypeBuilder WithPath(string path) - { - _path = path; - return this; - } - - public DataTypeBuilder WithCreatorId(int creatorId) - { - _creatorId = creatorId; - return this; - } - public DataTypeBuilder WithDatabaseType(ValueStorageType databaseType) { _databaseType = databaseType; return this; } - public DataTypeBuilder WithSortOrder(int sortOrder) - { - _sortOrder = sortOrder; - return this; - } - public DataEditorBuilder AddEditor() { return _dataEditorBuilder; @@ -163,5 +132,35 @@ namespace Umbraco.Tests.Common.Builders get => _name; set => _name = value; } + + int? IWithParentIdBuilder.ParentId + { + get => _parentId; + set => _parentId = value; + } + + bool? IWithTrashedBuilder.Trashed + { + get => _trashed; + set => _trashed = value; + } + + int? IWithLevelBuilder.Level + { + get => _level; + set => _level = value; + } + + string IWithPathBuilder.Path + { + get => _path; + set => _path = value; + } + + int? IWithSortOrderBuilder.SortOrder + { + get => _sortOrder; + set => _sortOrder = value; + } } } diff --git a/src/Umbraco.Tests.Common/Builders/DictionaryTranslationBuilder.cs b/src/Umbraco.Tests.Common/Builders/DictionaryTranslationBuilder.cs index 37fb7c5b07..d2f9d4bf02 100644 --- a/src/Umbraco.Tests.Common/Builders/DictionaryTranslationBuilder.cs +++ b/src/Umbraco.Tests.Common/Builders/DictionaryTranslationBuilder.cs @@ -21,7 +21,6 @@ namespace Umbraco.Tests.Common.Builders private DateTime? _updateDate; private string _value; - public DictionaryTranslationBuilder(DictionaryItemBuilder parentBuilder) : base(parentBuilder) { _languageBuilder = new LanguageBuilder(this); diff --git a/src/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs b/src/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs index 6a2407ae53..cce3b88470 100644 --- a/src/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs +++ b/src/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs @@ -53,5 +53,61 @@ namespace Umbraco.Tests.Common.Builders.Extensions builder.Key = key; return builder; } + + public static T WithParentId(this T builder, int parentId) + where T : IWithParentIdBuilder + { + builder.ParentId = parentId; + return builder; + } + + public static T WithTrashed(this T builder, bool trashed) + where T : IWithTrashedBuilder + { + builder.Trashed = trashed; + return builder; + } + + public static T WithLevel(this T builder, int level) + where T : IWithLevelBuilder + { + builder.Level = level; + return builder; + } + + public static T WithPath(this T builder, string path) + where T : IWithPathBuilder + { + builder.Path = path; + return builder; + } + + public static T WithSortOrder(this T builder, int sortOrder) + where T : IWithSortOrderBuilder + { + builder.SortOrder = sortOrder; + return builder; + } + + public static T WithDescription(this T builder, string description) + where T : IWithDescriptionBuilder + { + builder.Description = description; + return builder; + } + + public static T WithIcon(this T builder, string icon) + where T : IWithIconBuilder + { + builder.Icon = icon; + return builder; + } + + public static T WithThumbnail(this T builder, string thumbnail) + where T : IWithThumbnailBuilder + { + builder.Thumbnail = thumbnail; + return builder; + } } } diff --git a/src/Umbraco.Tests.Common/Builders/Extensions/StringExtensions.cs b/src/Umbraco.Tests.Common/Builders/Extensions/StringExtensions.cs new file mode 100644 index 0000000000..b426cabaa6 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Extensions/StringExtensions.cs @@ -0,0 +1,20 @@ +namespace Umbraco.Tests.Common.Builders.Extensions +{ + public static class StringExtensions + { + public static string ToCamelCase(this string s) + { + if (string.IsNullOrWhiteSpace(s)) + { + return string.Empty; + } + + if (s.Length == 1) + { + return s.ToLowerInvariant(); + } + + return char.ToLowerInvariant(s[0]) + s.Substring(1); + } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/GenericCollectionBuilder.cs b/src/Umbraco.Tests.Common/Builders/GenericCollectionBuilder.cs new file mode 100644 index 0000000000..c7e176e9b0 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/GenericCollectionBuilder.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; + +namespace Umbraco.Tests.Common.Builders +{ + public class GenericCollectionBuilder + : ChildBuilderBase> + { + private readonly IList _collection; + + public GenericCollectionBuilder(TBuilder parentBuilder) : base(parentBuilder) + { + _collection = new List(); + } + + public override IEnumerable Build() + { + return _collection; + } + + public GenericCollectionBuilder WithValue(T value) + { + _collection.Add(value); + return this; + } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/GenericDictionaryBuilder.cs b/src/Umbraco.Tests.Common/Builders/GenericDictionaryBuilder.cs index 0cf2284a49..8f6aedcf43 100644 --- a/src/Umbraco.Tests.Common/Builders/GenericDictionaryBuilder.cs +++ b/src/Umbraco.Tests.Common/Builders/GenericDictionaryBuilder.cs @@ -14,10 +14,10 @@ namespace Umbraco.Tests.Common.Builders public override IDictionary Build() { - return new Dictionary(); + return _dictionary; } - public GenericDictionaryBuilder AddKeyValue(TKey key, TValue value) + public GenericDictionaryBuilder WithKeyValue(TKey key, TValue value) { _dictionary.Add(key, value); return this; diff --git a/src/Umbraco.Tests.Common/Builders/Interfaces/IWithDescriptionBuilder.cs b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithDescriptionBuilder.cs new file mode 100644 index 0000000000..1a155073b3 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithDescriptionBuilder.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Tests.Common.Builders.Interfaces +{ + public interface IWithDescriptionBuilder + { + string Description { get; set; } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/Interfaces/IWithIconBuilder.cs b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithIconBuilder.cs new file mode 100644 index 0000000000..5de5224e18 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithIconBuilder.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Tests.Common.Builders.Interfaces +{ + public interface IWithIconBuilder + { + string Icon { get; set; } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/Interfaces/IWithLevelBuilder.cs b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithLevelBuilder.cs new file mode 100644 index 0000000000..dc6ee239ab --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithLevelBuilder.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Tests.Common.Builders.Interfaces +{ + public interface IWithLevelBuilder + { + int? Level { get; set; } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/Interfaces/IWithParentIdBuilder.cs b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithParentIdBuilder.cs new file mode 100644 index 0000000000..33d13b7ef1 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithParentIdBuilder.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Tests.Common.Builders.Interfaces +{ + public interface IWithParentIdBuilder + { + int? ParentId { get; set; } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/Interfaces/IWithPathBuilder.cs b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithPathBuilder.cs new file mode 100644 index 0000000000..ed632c4e7d --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithPathBuilder.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Tests.Common.Builders.Interfaces +{ + public interface IWithPathBuilder + { + string Path { get; set; } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/Interfaces/IWithSortOrderBuilder.cs b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithSortOrderBuilder.cs new file mode 100644 index 0000000000..3202c243fb --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithSortOrderBuilder.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Tests.Common.Builders.Interfaces +{ + public interface IWithSortOrderBuilder + { + int? SortOrder { get; set; } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/Interfaces/IWithThumbnailBuilder.cs b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithThumbnailBuilder.cs new file mode 100644 index 0000000000..ce5b10e274 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithThumbnailBuilder.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Tests.Common.Builders.Interfaces +{ + public interface IWithThumbnailBuilder + { + string Thumbnail { get; set; } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/Interfaces/IWithTrashedBuilder.cs b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithTrashedBuilder.cs new file mode 100644 index 0000000000..119e6a6e52 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/Interfaces/IWithTrashedBuilder.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Tests.Common.Builders.Interfaces +{ + public interface IWithTrashedBuilder + { + bool? Trashed { get; set; } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/MemberBuilder.cs b/src/Umbraco.Tests.Common/Builders/MemberBuilder.cs new file mode 100644 index 0000000000..cef4a35524 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/MemberBuilder.cs @@ -0,0 +1,280 @@ +using System; +using Umbraco.Core.Models; +using Umbraco.Tests.Common.Builders.Interfaces; + +namespace Umbraco.Tests.Common.Builders +{ + public class MemberBuilder + : BuilderBase, + IWithIdBuilder, + IWithKeyBuilder, + IWithCreatorIdBuilder, + IWithCreateDateBuilder, + IWithUpdateDateBuilder, + IWithNameBuilder, + IWithTrashedBuilder, + IWithLevelBuilder, + IWithPathBuilder, + IWithSortOrderBuilder + { + private MemberTypeBuilder _memberTypeBuilder; + private GenericCollectionBuilder _memberGroupsBuilder; + private GenericDictionaryBuilder _additionalDataBuilder; + private GenericDictionaryBuilder _propertyDataBuilder; + + private int? _id; + private Guid? _key; + private DateTime? _createDate; + private DateTime? _updateDate; + private string _name; + private int? _creatorId; + private string _username; + private string _rawPasswordValue; + private string _email; + private int? _failedPasswordAttempts; + private int? _level; + private string _path; + private bool? _isApproved; + private bool? _isLockedOut; + private DateTime? _lastLockoutDate; + private DateTime? _lastLoginDate; + private DateTime? _lastPasswordChangeDate; + private int? _sortOrder; + private bool? _trashed; + private int? _propertyIdsIncrementingFrom; + + public MemberBuilder WithUserName(string username) + { + _username = username; + return this; + } + + public MemberBuilder WithEmail(string email) + { + _email = email; + return this; + } + + public MemberBuilder WithRawPasswordValue(string rawPasswordValue) + { + _rawPasswordValue = rawPasswordValue; + return this; + } + + public MemberBuilder WithFailedPasswordAttempts(int failedPasswordAttempts) + { + _failedPasswordAttempts = failedPasswordAttempts; + return this; + } + + public MemberBuilder WithIsApproved(bool isApproved) + { + _isApproved = isApproved; + return this; + } + + public MemberBuilder WithIsLockedOut(bool isLockedOut) + { + _isLockedOut = isLockedOut; + return this; + } + + public MemberBuilder WithLastLockoutDate(DateTime lastLockoutDate) + { + _lastLockoutDate = lastLockoutDate; + return this; + } + + public MemberBuilder WithLastLoginDate(DateTime lastLoginDate) + { + _lastLoginDate = lastLoginDate; + return this; + } + + public MemberBuilder WithLastPasswordChangeDate(DateTime lastPasswordChangeDate) + { + _lastPasswordChangeDate = lastPasswordChangeDate; + return this; + } + + public MemberBuilder WithPropertyIdsIncrementingFrom(int propertyIdsIncrementingFrom) + { + _propertyIdsIncrementingFrom = propertyIdsIncrementingFrom; + return this; + } + + public MemberTypeBuilder AddMemberType() + { + var builder = new MemberTypeBuilder(this); + _memberTypeBuilder = builder; + return builder; + } + + public GenericCollectionBuilder AddMemberGroups() + { + var builder = new GenericCollectionBuilder(this); + _memberGroupsBuilder = builder; + return builder; + } + + public GenericDictionaryBuilder AddAdditionalData() + { + var builder = new GenericDictionaryBuilder(this); + _additionalDataBuilder = builder; + return builder; + } + + public GenericDictionaryBuilder AddPropertyData() + { + var builder = new GenericDictionaryBuilder(this); + _propertyDataBuilder = builder; + return builder; + } + + public override Member 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; + var username = _username ?? string.Empty; + var email = _email ?? string.Empty; + var rawPasswordValue = _rawPasswordValue ?? string.Empty; + var failedPasswordAttempts = _failedPasswordAttempts ?? 0; + var level = _level ?? 1; + var path = _path ?? "-1"; + var isApproved = _isApproved ?? false; + var isLockedOut = _isLockedOut ?? false; + var lastLockoutDate = _lastLockoutDate ?? DateTime.Now; + var lastLoginDate = _lastLoginDate ?? DateTime.Now; + var lastPasswordChangeDate = _lastPasswordChangeDate ?? DateTime.Now; + var sortOrder = _sortOrder ?? 0; + var trashed = _trashed ?? false; + + if (_memberTypeBuilder == null) + { + throw new InvalidOperationException("A member cannot be constructed without providing a member type (use AddMemberType)."); + } + + var memberType = _memberTypeBuilder.Build(); + + var member = new Member(name, email, username, rawPasswordValue, memberType) + { + Id = id, + Key = key, + CreateDate = createDate, + UpdateDate = updateDate, + CreatorId = creatorId, + Level = level, + Path = path, + SortOrder = sortOrder, + Trashed = trashed, + }; + + if (_propertyIdsIncrementingFrom.HasValue) + { + var i = _propertyIdsIncrementingFrom.Value; + foreach (var property in member.Properties) + { + property.Id = ++i; + } + } + + member.FailedPasswordAttempts = failedPasswordAttempts; + member.IsApproved = isApproved; + member.IsLockedOut = isLockedOut; + member.LastLockoutDate = lastLockoutDate; + member.LastLoginDate = lastLoginDate; + member.LastPasswordChangeDate = lastPasswordChangeDate; + + if (_memberGroupsBuilder != null) + { + member.Groups = _memberGroupsBuilder.Build(); + } + + if (_additionalDataBuilder != null) + { + var additionalData = _additionalDataBuilder.Build(); + foreach (var kvp in additionalData) + { + member.AdditionalData.Add(kvp.Key, kvp.Value); + } + } + + if (_propertyDataBuilder != null) + { + var propertyData = _propertyDataBuilder.Build(); + foreach (var kvp in propertyData) + { + member.SetValue(kvp.Key, kvp.Value); + } + + member.ResetDirtyProperties(false); + } + + return member; + } + + 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; + } + + bool? IWithTrashedBuilder.Trashed + { + get => _trashed; + set => _trashed = value; + } + + int? IWithLevelBuilder.Level + { + get => _level; + set => _level = value; + } + + string IWithPathBuilder.Path + { + get => _path; + set => _path = value; + } + + int? IWithSortOrderBuilder.SortOrder + { + get => _sortOrder; + set => _sortOrder = value; + } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/MemberGroupBuilder.cs b/src/Umbraco.Tests.Common/Builders/MemberGroupBuilder.cs index 6092c79abb..bfd7f30a14 100644 --- a/src/Umbraco.Tests.Common/Builders/MemberGroupBuilder.cs +++ b/src/Umbraco.Tests.Common/Builders/MemberGroupBuilder.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using Umbraco.Core.Models; using Umbraco.Tests.Common.Builders.Interfaces; @@ -14,15 +13,14 @@ namespace Umbraco.Tests.Common.Builders IWithUpdateDateBuilder, IWithNameBuilder { + private GenericDictionaryBuilder _additionalDataBuilder; + private int? _id; private Guid? _key; private DateTime? _createDate; private DateTime? _updateDate; private string _name; private int? _creatorId; - private IDictionary _additionalData = new Dictionary(); - - private GenericDictionaryBuilder _additionalDataBuilder; public GenericDictionaryBuilder AddAdditionalData() { @@ -40,7 +38,7 @@ namespace Umbraco.Tests.Common.Builders var name = _name ?? Guid.NewGuid().ToString(); var creatorId = _creatorId ?? 1; - return new MemberGroup + var memberGroup = new MemberGroup { Id = id, Key = key, @@ -49,6 +47,17 @@ namespace Umbraco.Tests.Common.Builders Name = name, CreatorId = creatorId, }; + + if (_additionalDataBuilder != null) + { + var additionalData = _additionalDataBuilder.Build(); + foreach (var kvp in additionalData) + { + memberGroup.AdditionalData.Add(kvp.Key, kvp.Value); + } + } + + return memberGroup; } int? IWithIdBuilder.Id diff --git a/src/Umbraco.Tests.Common/Builders/MemberTypeBuilder.cs b/src/Umbraco.Tests.Common/Builders/MemberTypeBuilder.cs new file mode 100644 index 0000000000..b01b8a1680 --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/MemberTypeBuilder.cs @@ -0,0 +1,198 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Umbraco.Core; +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 MemberTypeBuilder + : ChildBuilderBase, + IWithIdBuilder, + IWithAliasBuilder, + IWithNameBuilder, + IWithParentIdBuilder, + IWithSortOrderBuilder, + IWithCreatorIdBuilder, + IWithDescriptionBuilder, + IWithIconBuilder, + IWithThumbnailBuilder, + IWithTrashedBuilder + { + private readonly List _propertyGroupBuilders = new List(); + + private int? _id; + private string _alias; + private string _name; + private int? _parentId; + private int? _sortOrder; + private int? _creatorId; + private string _description; + private string _icon; + private string _thumbnail; + private bool? _trashed; + + public MemberTypeBuilder(MemberBuilder parentBuilder) : base(parentBuilder) + { + } + + public MemberTypeBuilder WithMembershipPropertyGroup() + { + var builder = new PropertyGroupBuilder(this) + .WithName(Constants.Conventions.Member.StandardPropertiesGroupName) + .WithSortOrder(1) + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextArea) + .WithValueStorageType(ValueStorageType.Ntext) + .WithAlias(Constants.Conventions.Member.Comments) + .WithName(Constants.Conventions.Member.CommentsLabel) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.Boolean) + .WithValueStorageType(ValueStorageType.Integer) + .WithAlias(Constants.Conventions.Member.IsApproved) + .WithName(Constants.Conventions.Member.IsApprovedLabel) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.Boolean) + .WithValueStorageType(ValueStorageType.Integer) + .WithAlias(Constants.Conventions.Member.IsLockedOut) + .WithName(Constants.Conventions.Member.IsLockedOutLabel) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.Label) + .WithValueStorageType(ValueStorageType.Date) + .WithAlias(Constants.Conventions.Member.LastLoginDate) + .WithName(Constants.Conventions.Member.LastLoginDateLabel) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.Label) + .WithValueStorageType(ValueStorageType.Date) + .WithAlias(Constants.Conventions.Member.LastPasswordChangeDate) + .WithName(Constants.Conventions.Member.LastPasswordChangeDateLabel) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.Label) + .WithValueStorageType(ValueStorageType.Date) + .WithAlias(Constants.Conventions.Member.LastLockoutDate) + .WithName(Constants.Conventions.Member.LastLockoutDateLabel) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.Label) + .WithValueStorageType(ValueStorageType.Integer) + .WithAlias(Constants.Conventions.Member.FailedPasswordAttempts) + .WithName(Constants.Conventions.Member.FailedPasswordAttemptsLabel) + .Done(); + _propertyGroupBuilders.Add(builder); + return this; + } + + public PropertyGroupBuilder AddPropertyGroup() + { + var builder = new PropertyGroupBuilder(this); + _propertyGroupBuilders.Add(builder); + return builder; + } + + public override MemberType Build() + { + var id = _id ?? 1; + var name = _name ?? Guid.NewGuid().ToString(); + var alias = _alias ?? name.ToCamelCase(); + var parentId = _parentId ?? -1; + var sortOrder = _sortOrder ?? 0; + var description = _description ?? string.Empty; + var icon = _icon ?? string.Empty; + var thumbnail = _thumbnail ?? string.Empty; + var creatorId = _creatorId ?? 0; + var trashed = _trashed ?? false; + + var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()); + + var memberType = new MemberType(shortStringHelper, parentId) + { + Id = id, + Alias = alias, + Name = name, + SortOrder = sortOrder, + Description = description, + Icon = icon, + Thumbnail = thumbnail, + CreatorId = creatorId, + Trashed = trashed, + }; + + foreach (var propertyGroup in _propertyGroupBuilders.Select(x => x.Build())) + { + memberType.PropertyGroups.Add(propertyGroup); + } + + memberType.ResetDirtyProperties(false); + + return memberType; + } + + int? IWithIdBuilder.Id + { + get => _id; + set => _id = value; + } + + string IWithAliasBuilder.Alias + { + get => _alias; + set => _alias = value; + } + + string IWithNameBuilder.Name + { + get => _name; + set => _name = value; + } + + int? IWithParentIdBuilder.ParentId + { + get => _parentId; + set => _parentId = value; + } + + int? IWithSortOrderBuilder.SortOrder + { + get => _sortOrder; + set => _sortOrder = value; + } + + int? IWithCreatorIdBuilder.CreatorId + { + get => _creatorId; + set => _creatorId = value; + } + + string IWithDescriptionBuilder.Description + { + get => _description; + set => _description = value; + } + + string IWithIconBuilder.Icon + { + get => _icon; + set => _icon = value; + } + + string IWithThumbnailBuilder.Thumbnail + { + get => _thumbnail; + set => _thumbnail = value; + } + + bool? IWithTrashedBuilder.Trashed + { + get => _trashed; + set => _trashed = value; + } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/PropertyGroupBuilder.cs b/src/Umbraco.Tests.Common/Builders/PropertyGroupBuilder.cs new file mode 100644 index 0000000000..4b349a7e8a --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/PropertyGroupBuilder.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Umbraco.Core.Models; +using Umbraco.Tests.Common.Builders.Interfaces; + +namespace Umbraco.Tests.Common.Builders +{ + public class PropertyGroupBuilder + : ChildBuilderBase, // TODO: likely want to genericise this, so can use for document and media types too. + IWithNameBuilder, + IWithSortOrderBuilder + { + private readonly List _propertyTypeBuilders = new List(); + + private string _name; + private int? _sortOrder; + + public PropertyGroupBuilder(MemberTypeBuilder parentBuilder) : base(parentBuilder) + { + } + + public PropertyTypeBuilder AddPropertyType() + { + var builder = new PropertyTypeBuilder(this); + _propertyTypeBuilders.Add(builder); + return builder; + } + + public override PropertyGroup Build() + { + var name = _name ?? Guid.NewGuid().ToString(); + var sortOrder = _sortOrder ?? 0; + + var properties = new PropertyTypeCollection(false); + foreach (var propertyType in _propertyTypeBuilders.Select(x => x.Build())) + { + properties.Add(propertyType); + } + + return new PropertyGroup(properties) + { + Name = name, + SortOrder = sortOrder, + }; + } + + string IWithNameBuilder.Name + { + get => _name; + set => _name = value; + } + + int? IWithSortOrderBuilder.SortOrder + { + get => _sortOrder; + set => _sortOrder = value; + } + } +} diff --git a/src/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs b/src/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs new file mode 100644 index 0000000000..955e2fca4c --- /dev/null +++ b/src/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs @@ -0,0 +1,93 @@ +using System; +using Moq; +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 PropertyTypeBuilder + : ChildBuilderBase, + IWithAliasBuilder, + IWithNameBuilder, + IWithSortOrderBuilder, + IWithDescriptionBuilder + { + private string _propertyEditorAlias; + private ValueStorageType? _valueStorageType; + private string _alias; + private string _name; + private int? _sortOrder; + private string _description; + private int? _dataTypeId; + + public PropertyTypeBuilder(PropertyGroupBuilder parentBuilder) : base(parentBuilder) + { + } + + public PropertyTypeBuilder WithPropertyEditorAlias(string propertyEditorAlias) + { + _propertyEditorAlias = propertyEditorAlias; + return this; + } + + public PropertyTypeBuilder WithValueStorageType(ValueStorageType valueStorageType) + { + _valueStorageType = valueStorageType; + return this; + } + + public PropertyTypeBuilder WithDataTypeId(int dataTypeId) + { + _dataTypeId = dataTypeId; + return this; + } + + public override PropertyType Build() + { + var propertyEditorAlias = _propertyEditorAlias ?? Guid.NewGuid().ToString().ToCamelCase(); + var valueStorageType = _valueStorageType ?? ValueStorageType.Ntext; + var name = _name ?? Guid.NewGuid().ToString(); + var alias = _alias ?? name.ToCamelCase(); + var sortOrder = _sortOrder ?? 0; + var dataTypeId = _dataTypeId ?? 0; + var description = _description ?? string.Empty; + + var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()); + + return new PropertyType(shortStringHelper, propertyEditorAlias, valueStorageType) + { + Alias = alias, + Name = name, + SortOrder = sortOrder, + DataTypeId = dataTypeId, + Description = description, + }; + } + + string IWithAliasBuilder.Alias + { + get => _alias; + set => _alias = value; + } + + string IWithNameBuilder.Name + { + get => _name; + set => _name = value; + } + + int? IWithSortOrderBuilder.SortOrder + { + get => _sortOrder; + set => _sortOrder = value; + } + + string IWithDescriptionBuilder.Description + { + get => _description; + set => _description = value; + } + } +} diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs index 158a70598c..f2b17cc23f 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs @@ -59,8 +59,8 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models .WithCreateDate(DateTime.Now) .WithUpdateDate(DateTime.Now) .AddAdditionalData() - .AddKeyValue("test1", 123) - .AddKeyValue("test2", "hello") + .WithKeyValue("test1", 123) + .WithKeyValue("test2", "hello") .Done() .Build(); } diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs new file mode 100644 index 0000000000..cda7641e8e --- /dev/null +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Diagnostics; +using System.Linq; +using Newtonsoft.Json; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Common.Builders.Extensions; + +namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models +{ + [TestFixture] + public class MemberTests + { + private readonly MemberBuilder _builder = new MemberBuilder(); + + [Test] + public void Can_Deep_Clone() + { + // Arrange + var member = BuildMember(); + + // Act + var clone = (Member)member.DeepClone(); + + // Assert + Assert.AreNotSame(clone, member); + Assert.AreEqual(clone, member); + Assert.AreEqual(clone.Id, member.Id); + Assert.AreEqual(clone.VersionId, member.VersionId); + Assert.AreEqual(clone.AdditionalData, member.AdditionalData); + Assert.AreEqual(clone.ContentType, member.ContentType); + Assert.AreEqual(clone.ContentTypeId, member.ContentTypeId); + Assert.AreEqual(clone.CreateDate, member.CreateDate); + Assert.AreEqual(clone.CreatorId, member.CreatorId); + Assert.AreEqual(clone.Comments, member.Comments); + Assert.AreEqual(clone.Key, member.Key); + Assert.AreEqual(clone.FailedPasswordAttempts, member.FailedPasswordAttempts); + Assert.AreEqual(clone.Level, member.Level); + Assert.AreEqual(clone.Path, member.Path); + Assert.AreEqual(clone.Groups, member.Groups); + Assert.AreEqual(clone.Groups.Count(), member.Groups.Count()); + Assert.AreEqual(clone.IsApproved, member.IsApproved); + Assert.AreEqual(clone.IsLockedOut, member.IsLockedOut); + Assert.AreEqual(clone.SortOrder, member.SortOrder); + Assert.AreEqual(clone.LastLockoutDate, member.LastLockoutDate); + Assert.AreNotSame(clone.LastLoginDate, member.LastLoginDate); + Assert.AreEqual(clone.LastPasswordChangeDate, member.LastPasswordChangeDate); + Assert.AreEqual(clone.Trashed, member.Trashed); + Assert.AreEqual(clone.UpdateDate, member.UpdateDate); + Assert.AreEqual(clone.VersionId, member.VersionId); + Assert.AreEqual(clone.RawPasswordValue, member.RawPasswordValue); + Assert.AreNotSame(clone.Properties, member.Properties); + Assert.AreEqual(clone.Properties.Count(), member.Properties.Count()); + for (var index = 0; index < member.Properties.Count; index++) + { + Assert.AreNotSame(clone.Properties[index], member.Properties[index]); + Assert.AreEqual(clone.Properties[index], member.Properties[index]); + } + + // this can be the same, it is immutable + Assert.AreSame(clone.ContentType, member.ContentType); + + //This double verifies by reflection + var allProps = clone.GetType().GetProperties(); + foreach (var propertyInfo in allProps) + Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(member, null)); + } + + [Test] + public void Can_Serialize_Without_Error() + { + var member = BuildMember(); + + var json = JsonConvert.SerializeObject(member); + Debug.Print(json); + } + + private Member BuildMember() + { + return _builder + .AddMemberType() + .WithId(99) + .WithAlias("memberType") + .WithName("Member Type") + .WithMembershipPropertyGroup() + .AddPropertyGroup() + .WithName("Content") + .WithSortOrder(1) + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) + .WithValueStorageType(ValueStorageType.Nvarchar) + .WithAlias("title") + .WithName("Title") + .WithSortOrder(1) + .WithDataTypeId(-88) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) + .WithValueStorageType(ValueStorageType.Ntext) + .WithAlias("bodyText") + .WithName("Body Text") + .WithSortOrder(2) + .WithDataTypeId(-87) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) + .WithValueStorageType(ValueStorageType.Nvarchar) + .WithAlias("author") + .WithName("Author") + .WithDescription("Name of the author") + .WithSortOrder(3) + .WithDataTypeId(-88) + .Done() + .Done() + .Done() + .WithId(10) + .WithKey(Guid.NewGuid()) + .WithName("Name") + .WithUserName("user") + .WithRawPasswordValue("raw pass") + .WithEmail("email@email.com") + .WithCreatorId(22) + .WithCreateDate(DateTime.Now) + .WithUpdateDate(DateTime.Now) + .WithFailedPasswordAttempts(22) + .WithLevel(3) + .WithPath("-1, 4, 10") + .WithIsApproved(true) + .WithIsLockedOut(true) + .WithLastLockoutDate(DateTime.Now) + .WithLastLoginDate(DateTime.Now) + .WithLastPasswordChangeDate(DateTime.Now) + .WithSortOrder(5) + .WithTrashed(false) + .AddMemberGroups() + .WithValue("group1") + .WithValue("group2") + .Done() + .AddAdditionalData() + .WithKeyValue("test1", 123) + .WithKeyValue("test2", "hello") + .Done() + .WithPropertyIdsIncrementingFrom(200) + .AddPropertyData() + .WithKeyValue("title", "Name member") + .WithKeyValue("bodyText", "This is a subpage") + .WithKeyValue("author", "John Doe") + .Done() + .Build(); + } + } +} diff --git a/src/Umbraco.Tests/Models/MemberTests.cs b/src/Umbraco.Tests/Models/MemberTests.cs deleted file mode 100644 index e0cd826536..0000000000 --- a/src/Umbraco.Tests/Models/MemberTests.cs +++ /dev/null @@ -1,134 +0,0 @@ -using System; -using System.Diagnostics; -using System.Linq; -using Newtonsoft.Json; -using NUnit.Framework; -using Umbraco.Core.Composing; -using Umbraco.Core.Models; -using Umbraco.Tests.TestHelpers; -using Umbraco.Tests.TestHelpers.Entities; - -namespace Umbraco.Tests.Models -{ - [TestFixture] - public class MemberTests - { - [Test] - public void Can_Deep_Clone() - { - // Arrange - var memberType = MockedContentTypes.CreateSimpleMemberType("memberType", "Member Type"); - memberType.Id = 99; - var member = MockedMember.CreateSimpleMember(memberType, "Name", "email@email.com", "pass", "user", Guid.NewGuid()); - var i = 200; - foreach (var property in member.Properties) - { - property.Id = ++i; - } - member.Id = 10; - member.CreateDate = DateTime.Now; - member.CreatorId = 22; - member.Comments = "comments"; - member.Key = Guid.NewGuid(); - member.FailedPasswordAttempts = 22; - member.Level = 3; - member.Path = "-1,4,10"; - member.Groups = new[] {"group1", "group2"}; - member.IsApproved = true; - member.IsLockedOut = true; - member.LastLockoutDate = DateTime.Now; - member.LastLoginDate = DateTime.Now; - member.LastPasswordChangeDate = DateTime.Now; - member.RawPasswordValue = "raw pass"; - member.SortOrder = 5; - member.Trashed = false; - member.UpdateDate = DateTime.Now; - member.AdditionalData.Add("test1", 123); - member.AdditionalData.Add("test2", "hello"); - - // Act - var clone = (Member)member.DeepClone(); - - // Assert - Assert.AreNotSame(clone, member); - Assert.AreEqual(clone, member); - Assert.AreEqual(clone.Id, member.Id); - Assert.AreEqual(clone.VersionId, member.VersionId); - Assert.AreEqual(clone.AdditionalData, member.AdditionalData); - Assert.AreEqual(clone.ContentType, member.ContentType); - Assert.AreEqual(clone.ContentTypeId, member.ContentTypeId); - Assert.AreEqual(clone.CreateDate, member.CreateDate); - Assert.AreEqual(clone.CreatorId, member.CreatorId); - Assert.AreEqual(clone.Comments, member.Comments); - Assert.AreEqual(clone.Key, member.Key); - Assert.AreEqual(clone.FailedPasswordAttempts, member.FailedPasswordAttempts); - Assert.AreEqual(clone.Level, member.Level); - Assert.AreEqual(clone.Path, member.Path); - Assert.AreEqual(clone.Groups, member.Groups); - Assert.AreEqual(clone.Groups.Count(), member.Groups.Count()); - Assert.AreEqual(clone.IsApproved, member.IsApproved); - Assert.AreEqual(clone.IsLockedOut, member.IsLockedOut); - Assert.AreEqual(clone.SortOrder, member.SortOrder); - Assert.AreEqual(clone.LastLockoutDate, member.LastLockoutDate); - Assert.AreNotSame(clone.LastLoginDate, member.LastLoginDate); - Assert.AreEqual(clone.LastPasswordChangeDate, member.LastPasswordChangeDate); - Assert.AreEqual(clone.Trashed, member.Trashed); - Assert.AreEqual(clone.UpdateDate, member.UpdateDate); - Assert.AreEqual(clone.VersionId, member.VersionId); - Assert.AreEqual(clone.RawPasswordValue, member.RawPasswordValue); - Assert.AreNotSame(clone.Properties, member.Properties); - Assert.AreEqual(clone.Properties.Count(), member.Properties.Count()); - for (var index = 0; index < member.Properties.Count; index++) - { - Assert.AreNotSame(clone.Properties[index], member.Properties[index]); - Assert.AreEqual(clone.Properties[index], member.Properties[index]); - } - - // this can be the same, it is immutable - Assert.AreSame(clone.ContentType, member.ContentType); - - //This double verifies by reflection - var allProps = clone.GetType().GetProperties(); - foreach (var propertyInfo in allProps) - { - Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(member, null)); - } - } - - [Test] - public void Can_Serialize_Without_Error() - { - var memberType = MockedContentTypes.CreateSimpleMemberType("memberType", "Member Type"); - memberType.Id = 99; - var member = MockedMember.CreateSimpleMember(memberType, "Name", "email@email.com", "pass", "user", Guid.NewGuid()); - var i = 200; - foreach (var property in member.Properties) - { - property.Id = ++i; - } - member.Id = 10; - member.CreateDate = DateTime.Now; - member.CreatorId = 22; - member.Comments = "comments"; - member.Key = Guid.NewGuid(); - member.FailedPasswordAttempts = 22; - member.Level = 3; - member.Path = "-1,4,10"; - member.Groups = new[] { "group1", "group2" }; - member.IsApproved = true; - member.IsLockedOut = true; - member.LastLockoutDate = DateTime.Now; - member.LastLoginDate = DateTime.Now; - member.LastPasswordChangeDate = DateTime.Now; - member.RawPasswordValue = "raw pass"; - member.SortOrder = 5; - member.Trashed = false; - member.UpdateDate = DateTime.Now; - member.AdditionalData.Add("test1", 123); - member.AdditionalData.Add("test2", "hello"); - - var json = JsonConvert.SerializeObject(member); - Debug.Print(json); - } - } -} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index eb500bf7e3..79bd8cfc3d 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -298,7 +298,6 @@ - From f6af286e0bc312243495636974f59788ed268d34 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Mon, 30 Mar 2020 07:42:02 +0200 Subject: [PATCH 3/4] Added tests confirming correct building of unit test objects. --- .../Models/DataTypeTests.cs | 18 ++- .../Models/MemberGroupTests.cs | 45 +++++- .../Models/MemberTests.cs | 144 +++++++++++++----- 3 files changed, 155 insertions(+), 52 deletions(-) diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs index 4be32a9b4f..53431bfdea 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs @@ -4,7 +4,6 @@ using Umbraco.Core.Models; using Umbraco.Tests.Common.Builders; using Umbraco.Tests.Common.Builders.Extensions; - namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models { [TestFixture] @@ -12,11 +11,26 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models { private readonly DataTypeBuilder _builder = new DataTypeBuilder(); + private const int _testId = 3123; + + [Test] + public void Is_Built_Correctly() + { + // Arrange + // Act + var dtd = _builder + .WithId(_testId) + .Build(); + + // Assert + Assert.AreEqual(_testId, dtd.Id); + } + [Test] public void Can_Deep_Clone() { var dtd = _builder - .WithId(3123) + .WithId(_testId) .Build(); var clone = (DataType) dtd.DeepClone(); diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs index f2b17cc23f..a6718660dd 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using Newtonsoft.Json; using NUnit.Framework; @@ -13,6 +14,34 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models { private readonly MemberGroupBuilder _builder = new MemberGroupBuilder(); + private const int _testId = 6; + private const string _testName = "Test Group"; + private const int _testCreatorId = 4; + private readonly Guid _testKey = Guid.NewGuid(); + private readonly DateTime _testCreateDate = DateTime.Now.AddHours(-1); + private readonly DateTime _testUpdateDate = DateTime.Now; + private readonly KeyValuePair _testAdditionalData1 = new KeyValuePair("test1", 123); + private readonly KeyValuePair _testAdditionalData2 = new KeyValuePair("test2", "hello"); + + [Test] + public void Is_Built_Correctly() + { + // Arrange + // Act + var group = BuildMemberGroup(); + + // Assert + Assert.AreEqual(_testId, group.Id); + Assert.AreEqual(_testKey, group.Key); + Assert.AreEqual(_testName, group.Name); + Assert.AreEqual(_testCreateDate, group.CreateDate); + Assert.AreEqual(_testUpdateDate, group.UpdateDate); + Assert.AreEqual(_testCreatorId, group.CreatorId); + Assert.AreEqual(2, group.AdditionalData.Count); + Assert.AreEqual(_testAdditionalData1.Value, group.AdditionalData[_testAdditionalData1.Key]); + Assert.AreEqual(_testAdditionalData2.Value, group.AdditionalData[_testAdditionalData2.Key]); + } + [Test] public void Can_Deep_Clone() { @@ -52,15 +81,15 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models private MemberGroup BuildMemberGroup() { return _builder - .WithId(6) - .WithKey(Guid.NewGuid()) - .WithName("asdf") - .WithCreatorId(4) - .WithCreateDate(DateTime.Now) - .WithUpdateDate(DateTime.Now) + .WithId(_testId) + .WithKey(_testKey) + .WithName(_testName) + .WithCreatorId(_testCreatorId) + .WithCreateDate(_testCreateDate) + .WithUpdateDate(_testUpdateDate) .AddAdditionalData() - .WithKeyValue("test1", 123) - .WithKeyValue("test2", "hello") + .WithKeyValue(_testAdditionalData1.Key, _testAdditionalData1.Value) + .WithKeyValue(_testAdditionalData2.Key, _testAdditionalData2.Value) .Done() .Build(); } diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs index cda7641e8e..9420d03c8f 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Newtonsoft.Json; @@ -15,6 +16,65 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models { private readonly MemberBuilder _builder = new MemberBuilder(); + private const int _testMemberTypeId = 99; + private const string _testMemberTypeAlias = "memberType"; + private const string _testMemberTypeName = "Member Type"; + private const string _testMemberTypePropertyGroupName = "Content"; + private const int _testId = 6; + private const string _testName = "Fred"; + private const string _testUsername = "fred"; + private const string _testRawPasswordValue = "raw pass"; + private const string _testEmail = "email@email.com"; + private const int _testCreatorId = 22; + private const int _testLevel = 3; + private const string _testPath = "-1, 4, 10"; + private const bool _testIsApproved = true; + private const bool _testIsLockedOut = true; + private const int _testSortOrder = 5; + private const bool _testTrashed = false; + private readonly Guid _testKey = Guid.NewGuid(); + private readonly DateTime _testCreateDate = DateTime.Now.AddHours(-1); + private readonly DateTime _testUpdateDate = DateTime.Now; + private readonly DateTime _testLastLockoutDate = DateTime.Now.AddHours(-2); + private readonly DateTime _testLastLoginDate = DateTime.Now.AddHours(-3); + private readonly DateTime _testLastPasswordChangeDate = DateTime.Now.AddHours(-4); + private readonly (string alias, string name, string description, int sortOrder, int dataTypeId) _testPropertyType1 = ("title", "Title", string.Empty, 1, -88); + private readonly (string alias, string name, string description, int sortOrder, int dataTypeId) _testPropertyType2 = ("bodyText", "Body Text", string.Empty, 2, -87); + private readonly (string alias, string name, string description, int sortOrder, int dataTypeId) _testPropertyType3 = ("author", "Author", "Writer of the article", 1, -88); + private readonly string[] _testGroups = new string[] { "group1", "group2" }; + private readonly KeyValuePair _testPropertyData1 = new KeyValuePair("title", "Name member"); + private readonly KeyValuePair _testPropertyData2 = new KeyValuePair("bodyText", "This is a subpage"); + private readonly KeyValuePair _testPropertyData3 = new KeyValuePair("author", "John Doe"); + private readonly KeyValuePair _testAdditionalData1 = new KeyValuePair("test1", 123); + private readonly KeyValuePair _testAdditionalData2 = new KeyValuePair("test2", "hello"); + + [Test] + public void Is_Built_Correctly() + { + // Arrange + // Act + var member = BuildMember(); + + // Assert + Assert.AreEqual(_testMemberTypeId, member.ContentTypeId); + Assert.AreEqual(_testMemberTypeAlias, member.ContentType.Alias); + Assert.AreEqual(_testMemberTypeName, member.ContentType.Name); + Assert.AreEqual(_testId, member.Id); + Assert.AreEqual(_testKey, member.Key); + Assert.AreEqual(_testName, member.Name); + Assert.AreEqual(_testCreateDate, member.CreateDate); + Assert.AreEqual(_testUpdateDate, member.UpdateDate); + Assert.AreEqual(_testCreatorId, member.CreatorId); + Assert.AreEqual(_testGroups, member.Groups.ToArray()); + Assert.AreEqual(10, member.Properties.Count); // 7 from membership properties group, 3 custom + Assert.AreEqual(_testPropertyData1.Value, member.GetValue(_testPropertyData1.Key)); + Assert.AreEqual(_testPropertyData2.Value, member.GetValue(_testPropertyData2.Key)); + Assert.AreEqual(_testPropertyData3.Value, member.GetValue(_testPropertyData3.Key)); + Assert.AreEqual(2, member.AdditionalData.Count); + Assert.AreEqual(_testAdditionalData1.Value, member.AdditionalData[_testAdditionalData1.Key]); + Assert.AreEqual(_testAdditionalData2.Value, member.AdditionalData[_testAdditionalData2.Key]); + } + [Test] public void Can_Deep_Clone() { @@ -81,72 +141,72 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models { return _builder .AddMemberType() - .WithId(99) - .WithAlias("memberType") - .WithName("Member Type") + .WithId(_testMemberTypeId) + .WithAlias(_testMemberTypeAlias) + .WithName(_testMemberTypeName) .WithMembershipPropertyGroup() .AddPropertyGroup() - .WithName("Content") + .WithName(_testMemberTypePropertyGroupName) .WithSortOrder(1) .AddPropertyType() .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) .WithValueStorageType(ValueStorageType.Nvarchar) - .WithAlias("title") - .WithName("Title") - .WithSortOrder(1) - .WithDataTypeId(-88) + .WithAlias(_testPropertyType1.alias) + .WithName(_testPropertyType1.name) + .WithSortOrder(_testPropertyType1.sortOrder) + .WithDataTypeId(_testPropertyType1.dataTypeId) .Done() .AddPropertyType() .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) .WithValueStorageType(ValueStorageType.Ntext) - .WithAlias("bodyText") - .WithName("Body Text") - .WithSortOrder(2) - .WithDataTypeId(-87) + .WithAlias(_testPropertyType2.alias) + .WithName(_testPropertyType2.name) + .WithSortOrder(_testPropertyType2.sortOrder) + .WithDataTypeId(_testPropertyType2.dataTypeId) .Done() .AddPropertyType() .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) .WithValueStorageType(ValueStorageType.Nvarchar) - .WithAlias("author") - .WithName("Author") - .WithDescription("Name of the author") - .WithSortOrder(3) - .WithDataTypeId(-88) + .WithAlias(_testPropertyType3.alias) + .WithName(_testPropertyType3.name) + .WithDescription(_testPropertyType3.description) + .WithSortOrder(_testPropertyType3.sortOrder) + .WithDataTypeId(_testPropertyType3.dataTypeId) .Done() .Done() .Done() - .WithId(10) - .WithKey(Guid.NewGuid()) - .WithName("Name") - .WithUserName("user") - .WithRawPasswordValue("raw pass") - .WithEmail("email@email.com") - .WithCreatorId(22) - .WithCreateDate(DateTime.Now) - .WithUpdateDate(DateTime.Now) + .WithId(_testId) + .WithKey(_testKey) + .WithName(_testName) + .WithUserName(_testUsername) + .WithRawPasswordValue(_testRawPasswordValue) + .WithEmail(_testEmail) + .WithCreatorId(_testCreatorId) + .WithCreateDate(_testCreateDate) + .WithUpdateDate(_testUpdateDate) .WithFailedPasswordAttempts(22) - .WithLevel(3) - .WithPath("-1, 4, 10") - .WithIsApproved(true) - .WithIsLockedOut(true) - .WithLastLockoutDate(DateTime.Now) - .WithLastLoginDate(DateTime.Now) - .WithLastPasswordChangeDate(DateTime.Now) - .WithSortOrder(5) - .WithTrashed(false) + .WithLevel(_testLevel) + .WithPath(_testPath) + .WithIsApproved(_testIsApproved) + .WithIsLockedOut(_testIsLockedOut) + .WithLastLockoutDate(_testLastLockoutDate) + .WithLastLoginDate(_testLastLoginDate) + .WithLastPasswordChangeDate(_testLastPasswordChangeDate) + .WithSortOrder(_testSortOrder) + .WithTrashed(_testTrashed) .AddMemberGroups() - .WithValue("group1") - .WithValue("group2") + .WithValue(_testGroups[0]) + .WithValue(_testGroups[1]) .Done() .AddAdditionalData() - .WithKeyValue("test1", 123) - .WithKeyValue("test2", "hello") + .WithKeyValue(_testAdditionalData1.Key, _testAdditionalData1.Value) + .WithKeyValue(_testAdditionalData2.Key, _testAdditionalData2.Value) .Done() .WithPropertyIdsIncrementingFrom(200) .AddPropertyData() - .WithKeyValue("title", "Name member") - .WithKeyValue("bodyText", "This is a subpage") - .WithKeyValue("author", "John Doe") + .WithKeyValue(_testPropertyData1.Key, _testPropertyData1.Value) + .WithKeyValue(_testPropertyData2.Key, _testPropertyData2.Value) + .WithKeyValue(_testPropertyData3.Key, _testPropertyData3.Value) .Done() .Build(); } From 919861bf361115ca2d473fce7ef9787ee6937615 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 31 Mar 2020 17:37:34 +0200 Subject: [PATCH 4/4] Separated member and member group builder and model unit tests, and other minor updates from PR review. --- .../Builders/PropertyGroupBuilder.cs | 2 +- .../Models/DataTypeTests.cs | 19 +-- .../Models/MemberGroupTests.cs | 45 +---- .../Models/MemberTests.cs | 141 +++++----------- .../Builders/DataTypeBuilderTests.cs | 27 +++ .../Builders/MemberBuilderTests.cs | 158 ++++++++++++++++++ .../Builders/MemberGroupBuilderTests.cs | 53 ++++++ 7 files changed, 288 insertions(+), 157 deletions(-) create mode 100644 src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/DataTypeBuilderTests.cs create mode 100644 src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/MemberBuilderTests.cs create mode 100644 src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/MemberGroupBuilderTests.cs diff --git a/src/Umbraco.Tests.Common/Builders/PropertyGroupBuilder.cs b/src/Umbraco.Tests.Common/Builders/PropertyGroupBuilder.cs index 4b349a7e8a..5f6fe12dff 100644 --- a/src/Umbraco.Tests.Common/Builders/PropertyGroupBuilder.cs +++ b/src/Umbraco.Tests.Common/Builders/PropertyGroupBuilder.cs @@ -7,7 +7,7 @@ using Umbraco.Tests.Common.Builders.Interfaces; namespace Umbraco.Tests.Common.Builders { public class PropertyGroupBuilder - : ChildBuilderBase, // TODO: likely want to genericise this, so can use for document and media types too. + : ChildBuilderBase, // TODO: likely want to generalise this, so can use for document and media types too. IWithNameBuilder, IWithSortOrderBuilder { diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs index 53431bfdea..ba1e500d5a 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/DataTypeTests.cs @@ -11,26 +11,11 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models { private readonly DataTypeBuilder _builder = new DataTypeBuilder(); - private const int _testId = 3123; - - [Test] - public void Is_Built_Correctly() - { - // Arrange - // Act - var dtd = _builder - .WithId(_testId) - .Build(); - - // Assert - Assert.AreEqual(_testId, dtd.Id); - } - [Test] public void Can_Deep_Clone() { - var dtd = _builder - .WithId(_testId) + var dtd = _builder + .WithId(3123) .Build(); var clone = (DataType) dtd.DeepClone(); diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs index a6718660dd..91ccd56b9e 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberGroupTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using Newtonsoft.Json; using NUnit.Framework; @@ -14,34 +13,6 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models { private readonly MemberGroupBuilder _builder = new MemberGroupBuilder(); - private const int _testId = 6; - private const string _testName = "Test Group"; - private const int _testCreatorId = 4; - private readonly Guid _testKey = Guid.NewGuid(); - private readonly DateTime _testCreateDate = DateTime.Now.AddHours(-1); - private readonly DateTime _testUpdateDate = DateTime.Now; - private readonly KeyValuePair _testAdditionalData1 = new KeyValuePair("test1", 123); - private readonly KeyValuePair _testAdditionalData2 = new KeyValuePair("test2", "hello"); - - [Test] - public void Is_Built_Correctly() - { - // Arrange - // Act - var group = BuildMemberGroup(); - - // Assert - Assert.AreEqual(_testId, group.Id); - Assert.AreEqual(_testKey, group.Key); - Assert.AreEqual(_testName, group.Name); - Assert.AreEqual(_testCreateDate, group.CreateDate); - Assert.AreEqual(_testUpdateDate, group.UpdateDate); - Assert.AreEqual(_testCreatorId, group.CreatorId); - Assert.AreEqual(2, group.AdditionalData.Count); - Assert.AreEqual(_testAdditionalData1.Value, group.AdditionalData[_testAdditionalData1.Key]); - Assert.AreEqual(_testAdditionalData2.Value, group.AdditionalData[_testAdditionalData2.Key]); - } - [Test] public void Can_Deep_Clone() { @@ -81,15 +52,15 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models private MemberGroup BuildMemberGroup() { return _builder - .WithId(_testId) - .WithKey(_testKey) - .WithName(_testName) - .WithCreatorId(_testCreatorId) - .WithCreateDate(_testCreateDate) - .WithUpdateDate(_testUpdateDate) + .WithId(6) + .WithKey(Guid.NewGuid()) + .WithName("Test Group") + .WithCreatorId(4) + .WithCreateDate(DateTime.Now) + .WithUpdateDate(DateTime.Now) .AddAdditionalData() - .WithKeyValue(_testAdditionalData1.Key, _testAdditionalData1.Value) - .WithKeyValue(_testAdditionalData2.Key, _testAdditionalData2.Value) + .WithKeyValue("test1", 123) + .WithKeyValue("test2", "hello") .Done() .Build(); } diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs index 9420d03c8f..1ac255db39 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Models/MemberTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Newtonsoft.Json; @@ -16,65 +15,6 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models { private readonly MemberBuilder _builder = new MemberBuilder(); - private const int _testMemberTypeId = 99; - private const string _testMemberTypeAlias = "memberType"; - private const string _testMemberTypeName = "Member Type"; - private const string _testMemberTypePropertyGroupName = "Content"; - private const int _testId = 6; - private const string _testName = "Fred"; - private const string _testUsername = "fred"; - private const string _testRawPasswordValue = "raw pass"; - private const string _testEmail = "email@email.com"; - private const int _testCreatorId = 22; - private const int _testLevel = 3; - private const string _testPath = "-1, 4, 10"; - private const bool _testIsApproved = true; - private const bool _testIsLockedOut = true; - private const int _testSortOrder = 5; - private const bool _testTrashed = false; - private readonly Guid _testKey = Guid.NewGuid(); - private readonly DateTime _testCreateDate = DateTime.Now.AddHours(-1); - private readonly DateTime _testUpdateDate = DateTime.Now; - private readonly DateTime _testLastLockoutDate = DateTime.Now.AddHours(-2); - private readonly DateTime _testLastLoginDate = DateTime.Now.AddHours(-3); - private readonly DateTime _testLastPasswordChangeDate = DateTime.Now.AddHours(-4); - private readonly (string alias, string name, string description, int sortOrder, int dataTypeId) _testPropertyType1 = ("title", "Title", string.Empty, 1, -88); - private readonly (string alias, string name, string description, int sortOrder, int dataTypeId) _testPropertyType2 = ("bodyText", "Body Text", string.Empty, 2, -87); - private readonly (string alias, string name, string description, int sortOrder, int dataTypeId) _testPropertyType3 = ("author", "Author", "Writer of the article", 1, -88); - private readonly string[] _testGroups = new string[] { "group1", "group2" }; - private readonly KeyValuePair _testPropertyData1 = new KeyValuePair("title", "Name member"); - private readonly KeyValuePair _testPropertyData2 = new KeyValuePair("bodyText", "This is a subpage"); - private readonly KeyValuePair _testPropertyData3 = new KeyValuePair("author", "John Doe"); - private readonly KeyValuePair _testAdditionalData1 = new KeyValuePair("test1", 123); - private readonly KeyValuePair _testAdditionalData2 = new KeyValuePair("test2", "hello"); - - [Test] - public void Is_Built_Correctly() - { - // Arrange - // Act - var member = BuildMember(); - - // Assert - Assert.AreEqual(_testMemberTypeId, member.ContentTypeId); - Assert.AreEqual(_testMemberTypeAlias, member.ContentType.Alias); - Assert.AreEqual(_testMemberTypeName, member.ContentType.Name); - Assert.AreEqual(_testId, member.Id); - Assert.AreEqual(_testKey, member.Key); - Assert.AreEqual(_testName, member.Name); - Assert.AreEqual(_testCreateDate, member.CreateDate); - Assert.AreEqual(_testUpdateDate, member.UpdateDate); - Assert.AreEqual(_testCreatorId, member.CreatorId); - Assert.AreEqual(_testGroups, member.Groups.ToArray()); - Assert.AreEqual(10, member.Properties.Count); // 7 from membership properties group, 3 custom - Assert.AreEqual(_testPropertyData1.Value, member.GetValue(_testPropertyData1.Key)); - Assert.AreEqual(_testPropertyData2.Value, member.GetValue(_testPropertyData2.Key)); - Assert.AreEqual(_testPropertyData3.Value, member.GetValue(_testPropertyData3.Key)); - Assert.AreEqual(2, member.AdditionalData.Count); - Assert.AreEqual(_testAdditionalData1.Value, member.AdditionalData[_testAdditionalData1.Key]); - Assert.AreEqual(_testAdditionalData2.Value, member.AdditionalData[_testAdditionalData2.Key]); - } - [Test] public void Can_Deep_Clone() { @@ -141,72 +81,69 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models { return _builder .AddMemberType() - .WithId(_testMemberTypeId) - .WithAlias(_testMemberTypeAlias) - .WithName(_testMemberTypeName) + .WithId(99) + .WithAlias("memberType") + .WithName("Member Type") .WithMembershipPropertyGroup() .AddPropertyGroup() - .WithName(_testMemberTypePropertyGroupName) + .WithName("Content") .WithSortOrder(1) .AddPropertyType() .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) .WithValueStorageType(ValueStorageType.Nvarchar) - .WithAlias(_testPropertyType1.alias) - .WithName(_testPropertyType1.name) - .WithSortOrder(_testPropertyType1.sortOrder) - .WithDataTypeId(_testPropertyType1.dataTypeId) + .WithAlias("title") + .WithName("Title") + .WithSortOrder(1) + .WithDataTypeId(-88) .Done() .AddPropertyType() .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) .WithValueStorageType(ValueStorageType.Ntext) - .WithAlias(_testPropertyType2.alias) - .WithName(_testPropertyType2.name) - .WithSortOrder(_testPropertyType2.sortOrder) - .WithDataTypeId(_testPropertyType2.dataTypeId) + .WithAlias("bodyText") + .WithName("Body text") + .WithSortOrder(2) + .WithDataTypeId(-87) .Done() .AddPropertyType() .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) .WithValueStorageType(ValueStorageType.Nvarchar) - .WithAlias(_testPropertyType3.alias) - .WithName(_testPropertyType3.name) - .WithDescription(_testPropertyType3.description) - .WithSortOrder(_testPropertyType3.sortOrder) - .WithDataTypeId(_testPropertyType3.dataTypeId) + .WithAlias("author") + .WithName("Author") + .WithDescription("Name of the author") + .WithSortOrder(3) + .WithDataTypeId(-88) .Done() .Done() .Done() - .WithId(_testId) - .WithKey(_testKey) - .WithName(_testName) - .WithUserName(_testUsername) - .WithRawPasswordValue(_testRawPasswordValue) - .WithEmail(_testEmail) - .WithCreatorId(_testCreatorId) - .WithCreateDate(_testCreateDate) - .WithUpdateDate(_testUpdateDate) + .WithId(10) + .WithKey(Guid.NewGuid()) + .WithName("Fred") + .WithUserName("fred") + .WithRawPasswordValue("raw pass") + .WithEmail("email@email.com") + .WithCreatorId(22) + .WithCreateDate(DateTime.Now) + .WithUpdateDate(DateTime.Now) .WithFailedPasswordAttempts(22) - .WithLevel(_testLevel) - .WithPath(_testPath) - .WithIsApproved(_testIsApproved) - .WithIsLockedOut(_testIsLockedOut) - .WithLastLockoutDate(_testLastLockoutDate) - .WithLastLoginDate(_testLastLoginDate) - .WithLastPasswordChangeDate(_testLastPasswordChangeDate) - .WithSortOrder(_testSortOrder) - .WithTrashed(_testTrashed) + .WithLevel(3) + .WithPath("-1, 4, 10") + .WithIsApproved(true) + .WithIsLockedOut(true) + .WithSortOrder(5) + .WithTrashed(false) .AddMemberGroups() - .WithValue(_testGroups[0]) - .WithValue(_testGroups[1]) + .WithValue("Group 1") + .WithValue("Group 2") .Done() .AddAdditionalData() - .WithKeyValue(_testAdditionalData1.Key, _testAdditionalData1.Value) - .WithKeyValue(_testAdditionalData2.Key, _testAdditionalData2.Value) + .WithKeyValue("test1", 123) + .WithKeyValue("test2", "hello") .Done() .WithPropertyIdsIncrementingFrom(200) .AddPropertyData() - .WithKeyValue(_testPropertyData1.Key, _testPropertyData1.Value) - .WithKeyValue(_testPropertyData2.Key, _testPropertyData2.Value) - .WithKeyValue(_testPropertyData3.Key, _testPropertyData3.Value) + .WithKeyValue("title", "Name member") + .WithKeyValue("bodyText", "This is a subpage") + .WithKeyValue("author", "John Doe") .Done() .Build(); } diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/DataTypeBuilderTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/DataTypeBuilderTests.cs new file mode 100644 index 0000000000..ce47c69763 --- /dev/null +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/DataTypeBuilderTests.cs @@ -0,0 +1,27 @@ +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 DataTypeBuilderTests + { + [Test] + public void Is_Built_Correctly() + { + // Arrange + const int testId = 3123; + + var builder = new DataTypeBuilder(); + + // Act + var dtd = builder + .WithId(testId) + .Build(); + + // Assert + Assert.AreEqual(testId, dtd.Id); + } + } +} diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/MemberBuilderTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/MemberBuilderTests.cs new file mode 100644 index 0000000000..e1df867642 --- /dev/null +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/MemberBuilderTests.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Common.Builders.Extensions; + +namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders +{ + [TestFixture] + public class MemberBuilderTests + { + private class PropertyTypeDetail + { + public string Alias { get; set; } + + public string Name { get; set; } + + public string Description { get; set; } = string.Empty; + + public int SortOrder { get; set; } + + public int DataTypeId { get; set; } + } + + [Test] + public void Is_Built_Correctly() + { + // Arrange + const int testMemberTypeId = 99; + const string testMemberTypeAlias = "memberType"; + const string testMemberTypeName = "Member Type"; + const string testMemberTypePropertyGroupName = "Content"; + const int testId = 10; + const string testName = "Fred"; + const string testUsername = "fred"; + const string testRawPasswordValue = "raw pass"; + const string testEmail = "email@email.com"; + const int testCreatorId = 22; + const int testLevel = 3; + const string testPath = "-1, 4, 10"; + const bool testIsApproved = true; + const bool testIsLockedOut = true; + const int testSortOrder = 5; + const bool testTrashed = false; + var testKey = Guid.NewGuid(); + var testCreateDate = DateTime.Now.AddHours(-1); + var testUpdateDate = DateTime.Now; + var testLastLockoutDate = DateTime.Now.AddHours(-2); + var testLastLoginDate = DateTime.Now.AddHours(-3); + var testLastPasswordChangeDate = DateTime.Now.AddHours(-4); + var testPropertyType1 = new PropertyTypeDetail { Alias = "title", Name = "Title", SortOrder = 1, DataTypeId = -88 }; + var testPropertyType2 = new PropertyTypeDetail { Alias = "bodyText", Name = "Body Text", SortOrder = 2, DataTypeId = -87 }; + var testPropertyType3 = new PropertyTypeDetail { Alias = "author", Name = "Author", Description = "Writer of the article", SortOrder = 1, DataTypeId = -88 }; + var testGroups = new string[] { "group1", "group2" }; + var testPropertyData1 = new KeyValuePair("title", "Name member"); + var testPropertyData2 = new KeyValuePair("bodyText", "This is a subpage"); + var testPropertyData3 = new KeyValuePair("author", "John Doe"); + var testAdditionalData1 = new KeyValuePair("test1", 123); + var testAdditionalData2 = new KeyValuePair("test2", "hello"); + + var builder = new MemberBuilder(); + + // Act + var member = builder + .AddMemberType() + .WithId(testMemberTypeId) + .WithAlias(testMemberTypeAlias) + .WithName(testMemberTypeName) + .WithMembershipPropertyGroup() + .AddPropertyGroup() + .WithName(testMemberTypePropertyGroupName) + .WithSortOrder(1) + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) + .WithValueStorageType(ValueStorageType.Nvarchar) + .WithAlias(testPropertyType1.Alias) + .WithName(testPropertyType1.Name) + .WithSortOrder(testPropertyType1.SortOrder) + .WithDataTypeId(testPropertyType1.DataTypeId) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) + .WithValueStorageType(ValueStorageType.Ntext) + .WithAlias(testPropertyType2.Alias) + .WithName(testPropertyType2.Name) + .WithSortOrder(testPropertyType2.SortOrder) + .WithDataTypeId(testPropertyType2.DataTypeId) + .Done() + .AddPropertyType() + .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox) + .WithValueStorageType(ValueStorageType.Nvarchar) + .WithAlias(testPropertyType3.Alias) + .WithName(testPropertyType3.Name) + .WithDescription(testPropertyType3.Description) + .WithSortOrder(testPropertyType3.SortOrder) + .WithDataTypeId(testPropertyType3.DataTypeId) + .Done() + .Done() + .Done() + .WithId(testId) + .WithKey(testKey) + .WithName(testName) + .WithUserName(testUsername) + .WithRawPasswordValue(testRawPasswordValue) + .WithEmail(testEmail) + .WithCreatorId(testCreatorId) + .WithCreateDate(testCreateDate) + .WithUpdateDate(testUpdateDate) + .WithFailedPasswordAttempts(22) + .WithLevel(testLevel) + .WithPath(testPath) + .WithIsApproved(testIsApproved) + .WithIsLockedOut(testIsLockedOut) + .WithLastLockoutDate(testLastLockoutDate) + .WithLastLoginDate(testLastLoginDate) + .WithLastPasswordChangeDate(testLastPasswordChangeDate) + .WithSortOrder(testSortOrder) + .WithTrashed(testTrashed) + .AddMemberGroups() + .WithValue(testGroups[0]) + .WithValue(testGroups[1]) + .Done() + .AddAdditionalData() + .WithKeyValue(testAdditionalData1.Key, testAdditionalData1.Value) + .WithKeyValue(testAdditionalData2.Key, testAdditionalData2.Value) + .Done() + .WithPropertyIdsIncrementingFrom(200) + .AddPropertyData() + .WithKeyValue(testPropertyData1.Key, testPropertyData1.Value) + .WithKeyValue(testPropertyData2.Key, testPropertyData2.Value) + .WithKeyValue(testPropertyData3.Key, testPropertyData3.Value) + .Done() + .Build(); + + // Assert + Assert.AreEqual(testMemberTypeId, member.ContentTypeId); + Assert.AreEqual(testMemberTypeAlias, member.ContentType.Alias); + Assert.AreEqual(testMemberTypeName, member.ContentType.Name); + Assert.AreEqual(testId, member.Id); + Assert.AreEqual(testKey, member.Key); + Assert.AreEqual(testName, member.Name); + Assert.AreEqual(testCreateDate, member.CreateDate); + Assert.AreEqual(testUpdateDate, member.UpdateDate); + Assert.AreEqual(testCreatorId, member.CreatorId); + Assert.AreEqual(testGroups, member.Groups.ToArray()); + Assert.AreEqual(10, member.Properties.Count); // 7 from membership properties group, 3 custom + Assert.AreEqual(testPropertyData1.Value, member.GetValue(testPropertyData1.Key)); + Assert.AreEqual(testPropertyData2.Value, member.GetValue(testPropertyData2.Key)); + Assert.AreEqual(testPropertyData3.Value, member.GetValue(testPropertyData3.Key)); + Assert.AreEqual(2, member.AdditionalData.Count); + Assert.AreEqual(testAdditionalData1.Value, member.AdditionalData[testAdditionalData1.Key]); + Assert.AreEqual(testAdditionalData2.Value, member.AdditionalData[testAdditionalData2.Key]); + } + } +} diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/MemberGroupBuilderTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/MemberGroupBuilderTests.cs new file mode 100644 index 0000000000..4cb47de052 --- /dev/null +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/MemberGroupBuilderTests.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +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 MemberGroupBuilderTests + { + [Test] + public void Is_Built_Correctly() + { + // Arrange + const int testId = 6; + const string testName = "Test Group"; + const int testCreatorId = 4; + var testKey = Guid.NewGuid(); + var testCreateDate = DateTime.Now.AddHours(-1); + var testUpdateDate = DateTime.Now; + var testAdditionalData1 = new KeyValuePair("test1", 123); + var testAdditionalData2 = new KeyValuePair("test2", "hello"); + + var builder = new MemberGroupBuilder(); + + // Act + var group = builder + .WithId(testId) + .WithKey(testKey) + .WithName(testName) + .WithCreatorId(testCreatorId) + .WithCreateDate(testCreateDate) + .WithUpdateDate(testUpdateDate) + .AddAdditionalData() + .WithKeyValue(testAdditionalData1.Key, testAdditionalData1.Value) + .WithKeyValue(testAdditionalData2.Key, testAdditionalData2.Value) + .Done() + .Build(); + + // Assert + Assert.AreEqual(testId, group.Id); + Assert.AreEqual(testKey, group.Key); + Assert.AreEqual(testName, group.Name); + Assert.AreEqual(testCreateDate, group.CreateDate); + Assert.AreEqual(testUpdateDate, group.UpdateDate); + Assert.AreEqual(testCreatorId, group.CreatorId); + Assert.AreEqual(3, group.AdditionalData.Count); // previousName is added as part of the MemberGroup construction, plus the 2 we've added. + Assert.AreEqual(testAdditionalData1.Value, group.AdditionalData[testAdditionalData1.Key]); + Assert.AreEqual(testAdditionalData2.Value, group.AdditionalData[testAdditionalData2.Key]); + } + } +}