using extension methods to enforce consistency
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using Moq;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.PropertyEditors;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Core.Strings;
|
||||
|
||||
namespace Umbraco.Tests.Shared.Builders
|
||||
{
|
||||
public class DataEditorBuilder<TParent> : ChildBuilderBase<TParent, IDataEditor>
|
||||
{
|
||||
public DataEditorBuilder(TParent parentBuilder) : base(parentBuilder)
|
||||
{
|
||||
}
|
||||
|
||||
public override IDataEditor Build()
|
||||
{
|
||||
var result = new DataEditor(
|
||||
Mock.Of<ILogger>(),
|
||||
Mock.Of<IDataTypeService>(),
|
||||
Mock.Of<ILocalizationService>(),
|
||||
Mock.Of<ILocalizedTextService>(),
|
||||
Mock.Of<IShortStringHelper>()
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
namespace Umbraco.Tests.Shared.Builders
|
||||
{
|
||||
public class DataTypeBuilder : BuilderBase<DataType>, IWithIdBuilder
|
||||
{
|
||||
private readonly DataEditorBuilder<DataTypeBuilder> _dataEditorBuilder;
|
||||
private int? _id;
|
||||
|
||||
public DataTypeBuilder()
|
||||
{
|
||||
_dataEditorBuilder = new DataEditorBuilder<DataTypeBuilder>(this);
|
||||
}
|
||||
|
||||
public override DataType Build()
|
||||
{
|
||||
var editor = _dataEditorBuilder.Build();
|
||||
var id = _id ?? 1;
|
||||
var result = new DataType(editor)
|
||||
{
|
||||
Id = id
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int? IWithIdBuilder.Id
|
||||
{
|
||||
get => _id;
|
||||
set => _id = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
namespace Umbraco.Tests.Shared.Builders
|
||||
{
|
||||
public class DictionaryItemBuilder
|
||||
public class DictionaryItemBuilder : IWithIdBuilder, IWithCreateDateBuilder, IWithUpdateDateBuilder
|
||||
{
|
||||
private string _itemkey = null;
|
||||
private readonly List<DictionaryTranslationBuilder> _translationBuilders = new List<DictionaryTranslationBuilder>();
|
||||
@@ -14,7 +13,6 @@ namespace Umbraco.Tests.Shared.Builders
|
||||
private DateTime? _updateDate;
|
||||
private int? _id = null;
|
||||
|
||||
|
||||
public DictionaryItem Build()
|
||||
{
|
||||
var createDate = _createDate ?? DateTime.Now;
|
||||
@@ -38,24 +36,6 @@ namespace Umbraco.Tests.Shared.Builders
|
||||
return builder;
|
||||
}
|
||||
|
||||
public DictionaryItemBuilder WithCreateData(DateTime createDate)
|
||||
{
|
||||
_createDate = createDate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DictionaryItemBuilder WithUpdateData(DateTime updateDate)
|
||||
{
|
||||
_updateDate = updateDate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DictionaryItemBuilder WithId(int id)
|
||||
{
|
||||
_id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DictionaryItemBuilder WithRandomTranslations(int count)
|
||||
{
|
||||
for (var i = 0; i < count; i++)
|
||||
@@ -64,5 +44,23 @@ namespace Umbraco.Tests.Shared.Builders
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
int? IWithIdBuilder.Id
|
||||
{
|
||||
get => _id;
|
||||
set => _id = value;
|
||||
}
|
||||
|
||||
DateTime? IWithCreateDateBuilder.CreateDate
|
||||
{
|
||||
get => _createDate;
|
||||
set => _createDate = value;
|
||||
}
|
||||
|
||||
DateTime? IWithUpdateDateBuilder.UpdateDate
|
||||
{
|
||||
get => _updateDate;
|
||||
set => _updateDate = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
namespace Umbraco.Tests.Shared.Builders
|
||||
{
|
||||
public class DictionaryTranslationBuilder : ChildBuilderBase<DictionaryItemBuilder,IDictionaryTranslation>
|
||||
public class DictionaryTranslationBuilder : ChildBuilderBase<DictionaryItemBuilder,IDictionaryTranslation>, IWithIdBuilder, IWithCreateDateBuilder, IWithUpdateDateBuilder
|
||||
{
|
||||
private string _value = null;
|
||||
private Guid? _uniqueId = null;
|
||||
@@ -50,22 +49,22 @@ namespace Umbraco.Tests.Shared.Builders
|
||||
return this;
|
||||
}
|
||||
|
||||
public DictionaryTranslationBuilder WithCreateData(DateTime createDate)
|
||||
int? IWithIdBuilder.Id
|
||||
{
|
||||
_createDate = createDate;
|
||||
return this;
|
||||
get => _id;
|
||||
set => _id = value;
|
||||
}
|
||||
|
||||
public DictionaryTranslationBuilder WithUpdateData(DateTime updateDate)
|
||||
DateTime? IWithCreateDateBuilder.CreateDate
|
||||
{
|
||||
_updateDate = updateDate;
|
||||
return this;
|
||||
get => _createDate;
|
||||
set => _createDate = value;
|
||||
}
|
||||
|
||||
public DictionaryTranslationBuilder WithId(int id)
|
||||
DateTime? IWithUpdateDateBuilder.UpdateDate
|
||||
{
|
||||
_id = id;
|
||||
return this;
|
||||
get => _updateDate;
|
||||
set => _updateDate = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace Umbraco.Tests.Shared.Builders.Extensions
|
||||
{
|
||||
public static class BuilderExtensions
|
||||
{
|
||||
public static T WithId<T>(this T builder, int id)
|
||||
where T : IWithIdBuilder
|
||||
{
|
||||
builder.Id = id;
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static T WithCreateDate<T>(this T builder, DateTime createDate)
|
||||
where T : IWithCreateDateBuilder
|
||||
{
|
||||
builder.CreateDate = createDate;
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static T WithUpdateDate<T>(this T builder, DateTime updateDate)
|
||||
where T : IWithUpdateDateBuilder
|
||||
{
|
||||
builder.UpdateDate = updateDate;
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,11 @@ namespace Umbraco.Tests.Shared.Builders
|
||||
}
|
||||
|
||||
|
||||
public class LanguageBuilder<TParent> : ChildBuilderBase<TParent, ILanguage>
|
||||
public class LanguageBuilder<TParent> : ChildBuilderBase<TParent, ILanguage>, IWithIdBuilder
|
||||
{
|
||||
private int? _id = null;
|
||||
|
||||
private string _isoCode = null;
|
||||
private int? _id;
|
||||
|
||||
public LanguageBuilder(TParent parentBuilder) : base(parentBuilder)
|
||||
{
|
||||
@@ -34,10 +35,10 @@ namespace Umbraco.Tests.Shared.Builders
|
||||
};
|
||||
}
|
||||
|
||||
public LanguageBuilder<TParent> WithId(int id)
|
||||
int? IWithIdBuilder.Id
|
||||
{
|
||||
_id = id;
|
||||
return this;
|
||||
get => _id;
|
||||
set => _id = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace Umbraco.Tests.Shared.Builders
|
||||
{
|
||||
public interface IWithCreateDateBuilder
|
||||
{
|
||||
DateTime? CreateDate { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Umbraco.Tests.Shared.Builders
|
||||
{
|
||||
public interface IWithIdBuilder
|
||||
{
|
||||
int? Id { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace Umbraco.Tests.Shared.Builders
|
||||
{
|
||||
public interface IWithUpdateDateBuilder
|
||||
{
|
||||
DateTime? UpdateDate { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Tests.Shared.Builders;
|
||||
using Umbraco.Tests.Shared.Builders.Extensions;
|
||||
|
||||
namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
|
||||
{
|
||||
[TestFixture]
|
||||
public class DataTypeTests
|
||||
{
|
||||
|
||||
private readonly DataTypeBuilder _builder = new DataTypeBuilder();
|
||||
[Test]
|
||||
public void Can_Deep_Clone()
|
||||
{
|
||||
var dtd = _builder
|
||||
.WithId(3123)
|
||||
.Build();
|
||||
|
||||
var clone = (DataType) dtd.DeepClone();
|
||||
|
||||
Assert.AreNotSame(clone, dtd);
|
||||
Assert.AreEqual(clone, dtd);
|
||||
Assert.AreEqual(clone.CreateDate, dtd.CreateDate);
|
||||
Assert.AreEqual(clone.CreatorId, dtd.CreatorId);
|
||||
Assert.AreEqual(clone.DatabaseType, dtd.DatabaseType);
|
||||
Assert.AreEqual(clone.Id, dtd.Id);
|
||||
Assert.AreEqual(clone.Key, dtd.Key);
|
||||
Assert.AreEqual(clone.Level, dtd.Level);
|
||||
Assert.AreEqual(clone.Name, dtd.Name);
|
||||
Assert.AreEqual(clone.ParentId, dtd.ParentId);
|
||||
Assert.AreEqual(clone.Path, dtd.Path);
|
||||
Assert.AreEqual(clone.SortOrder, dtd.SortOrder);
|
||||
Assert.AreEqual(clone.Trashed, dtd.Trashed);
|
||||
Assert.AreEqual(clone.UpdateDate, dtd.UpdateDate);
|
||||
|
||||
//This double verifies by reflection
|
||||
var allProps = clone.GetType().GetProperties();
|
||||
foreach (var propertyInfo in allProps)
|
||||
{
|
||||
Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(dtd, null));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Serialize_Without_Error()
|
||||
{
|
||||
var item = _builder
|
||||
.Build();
|
||||
|
||||
Assert.DoesNotThrow(() => JsonConvert.SerializeObject(item));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user