Migrated DocumentEntitySlim tests into new project and builder pattern.
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
using System;
|
||||
using Umbraco.Core.Models.Entities;
|
||||
using Umbraco.Tests.Common.Builders.Interfaces;
|
||||
|
||||
namespace Umbraco.Tests.Common.Builders
|
||||
{
|
||||
public class DocumentEntitySlimBuilder
|
||||
: BuilderBase<DocumentEntitySlim>,
|
||||
IWithIdBuilder,
|
||||
IWithKeyBuilder,
|
||||
IWithCreatorIdBuilder,
|
||||
IWithCreateDateBuilder,
|
||||
IWithUpdateDateBuilder,
|
||||
IWithNameBuilder,
|
||||
IWithLevelBuilder,
|
||||
IWithPathBuilder,
|
||||
IWithSortOrderBuilder,
|
||||
IWithParentIdBuilder
|
||||
{
|
||||
private GenericDictionaryBuilder<DocumentEntitySlimBuilder, string, object> _additionalDataBuilder;
|
||||
|
||||
private int? _id;
|
||||
private Guid? _key;
|
||||
private DateTime? _createDate;
|
||||
private DateTime? _updateDate;
|
||||
private string _name;
|
||||
private int? _creatorId;
|
||||
private int? _level;
|
||||
private string _path;
|
||||
private int? _sortOrder;
|
||||
private int? _parentId;
|
||||
private string _contentTypeIcon;
|
||||
private string _contentTypeThumbnail;
|
||||
private string _contentTypeAlias;
|
||||
private bool? _hasChildren;
|
||||
private bool? _published;
|
||||
|
||||
public DocumentEntitySlimBuilder WithHasChildren(bool hasChildren)
|
||||
{
|
||||
_hasChildren = hasChildren;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DocumentEntitySlimBuilder WithPublished(bool published)
|
||||
{
|
||||
_published = published;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DocumentEntitySlimBuilder WithContentTypeAlias(string contentTypeAlias)
|
||||
{
|
||||
_contentTypeAlias = contentTypeAlias;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DocumentEntitySlimBuilder WithContentTypeIcon(string contentTypeIcon)
|
||||
{
|
||||
_contentTypeIcon = contentTypeIcon;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DocumentEntitySlimBuilder WithContentTypeThumbnail(string contentTypeThumbnail)
|
||||
{
|
||||
_contentTypeThumbnail = contentTypeThumbnail;
|
||||
return this;
|
||||
}
|
||||
|
||||
public GenericDictionaryBuilder<DocumentEntitySlimBuilder, string, object> AddAdditionalData()
|
||||
{
|
||||
var builder = new GenericDictionaryBuilder<DocumentEntitySlimBuilder, string, object>(this);
|
||||
_additionalDataBuilder = builder;
|
||||
return builder;
|
||||
}
|
||||
|
||||
public override DocumentEntitySlim 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 level = _level ?? 1;
|
||||
var path = _path ?? "-1";
|
||||
var sortOrder = _sortOrder ?? 0;
|
||||
var parentId = _parentId ?? -1;
|
||||
var icon = _contentTypeIcon ?? string.Empty;
|
||||
var thumbnail = _contentTypeThumbnail ?? string.Empty;
|
||||
var contentTypeAlias = _contentTypeAlias ?? string.Empty;
|
||||
var hasChildren = _hasChildren ?? false;
|
||||
var published = _published ?? false;
|
||||
|
||||
var documentEntitySlim = new DocumentEntitySlim
|
||||
{
|
||||
Id = id,
|
||||
Key = key,
|
||||
CreateDate = createDate,
|
||||
UpdateDate = updateDate,
|
||||
Name = name,
|
||||
CreatorId = creatorId,
|
||||
Level = level,
|
||||
Path = path,
|
||||
SortOrder = sortOrder,
|
||||
ParentId = parentId,
|
||||
ContentTypeIcon = icon,
|
||||
ContentTypeThumbnail = thumbnail,
|
||||
ContentTypeAlias = contentTypeAlias,
|
||||
HasChildren = hasChildren,
|
||||
Published = published,
|
||||
};
|
||||
|
||||
if (_additionalDataBuilder != null)
|
||||
{
|
||||
var additionalData = _additionalDataBuilder.Build();
|
||||
foreach (var kvp in additionalData)
|
||||
{
|
||||
documentEntitySlim.AdditionalData.Add(kvp.Key, kvp.Value);
|
||||
}
|
||||
}
|
||||
|
||||
return documentEntitySlim;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int? IWithLevelBuilder.Level
|
||||
{
|
||||
get => _level;
|
||||
set => _level = value;
|
||||
}
|
||||
|
||||
string IWithPathBuilder.Path
|
||||
{
|
||||
get => _path;
|
||||
set => _path = value;
|
||||
}
|
||||
|
||||
int? IWithSortOrderBuilder.SortOrder
|
||||
{
|
||||
get => _sortOrder;
|
||||
set => _sortOrder = value;
|
||||
}
|
||||
|
||||
int? IWithParentIdBuilder.ParentId
|
||||
{
|
||||
get => _parentId;
|
||||
set => _parentId = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Diagnostics;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Tests.Common.Builders;
|
||||
using Umbraco.Tests.Common.Builders.Extensions;
|
||||
|
||||
namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
|
||||
{
|
||||
[TestFixture]
|
||||
public class DocumentEntityTests
|
||||
{
|
||||
private DocumentEntitySlimBuilder _builder;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_builder = new DocumentEntitySlimBuilder();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Serialize_Without_Error()
|
||||
{
|
||||
var item = _builder
|
||||
.WithId(3)
|
||||
.WithCreatorId(4)
|
||||
.WithName("Test")
|
||||
.WithParentId(5)
|
||||
.WithSortOrder(6)
|
||||
.WithPath("-1,23")
|
||||
.WithLevel(7)
|
||||
.WithContentTypeAlias("test1")
|
||||
.WithContentTypeIcon("icon")
|
||||
.WithContentTypeThumbnail("thumb")
|
||||
.WithHasChildren(true)
|
||||
.WithPublished(true)
|
||||
.AddAdditionalData()
|
||||
.WithKeyValue("test1", 3)
|
||||
.WithKeyValue("test2", "value")
|
||||
.Done()
|
||||
.Build();
|
||||
|
||||
var json = JsonConvert.SerializeObject(item);
|
||||
Debug.Print(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
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 DocumentEntitySlimBuilderTests
|
||||
{
|
||||
[Test]
|
||||
public void Is_Built_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
const int testId = 11;
|
||||
const string testName = "Test";
|
||||
const int testCreatorId = 22;
|
||||
const int testLevel = 3;
|
||||
const string testPath = "-1,23";
|
||||
const int testParentId = 5;
|
||||
const int testSortOrder = 6;
|
||||
const bool testHasChildren = true;
|
||||
const bool testPublished = true;
|
||||
const string testContentTypeAlias = "test1";
|
||||
const string testContentTypeIcon = "icon";
|
||||
const string testContentTypeThumbnail = "thumb";
|
||||
var testKey = Guid.NewGuid();
|
||||
var testCreateDate = DateTime.Now.AddHours(-1);
|
||||
var testUpdateDate = DateTime.Now;
|
||||
var testAdditionalData1 = new KeyValuePair<string, object>("test1", 123);
|
||||
var testAdditionalData2 = new KeyValuePair<string, object>("test2", "hello");
|
||||
|
||||
var builder = new DocumentEntitySlimBuilder();
|
||||
|
||||
// Act
|
||||
var item = builder
|
||||
.WithId(testId)
|
||||
.WithKey(testKey)
|
||||
.WithCreatorId(testCreatorId)
|
||||
.WithCreateDate(testCreateDate)
|
||||
.WithUpdateDate(testUpdateDate)
|
||||
.WithName(testName)
|
||||
.WithParentId(testParentId)
|
||||
.WithSortOrder(testSortOrder)
|
||||
.WithLevel(testLevel)
|
||||
.WithPath(testPath)
|
||||
.WithContentTypeAlias(testContentTypeAlias)
|
||||
.WithContentTypeIcon(testContentTypeIcon)
|
||||
.WithContentTypeThumbnail(testContentTypeThumbnail)
|
||||
.WithHasChildren(testHasChildren)
|
||||
.WithPublished(testPublished)
|
||||
.AddAdditionalData()
|
||||
.WithKeyValue(testAdditionalData1.Key, testAdditionalData1.Value)
|
||||
.WithKeyValue(testAdditionalData2.Key, testAdditionalData2.Value)
|
||||
.Done()
|
||||
.Build();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(testId, item.Id);
|
||||
Assert.AreEqual(testKey, item.Key);
|
||||
Assert.AreEqual(testName, item.Name);
|
||||
Assert.AreEqual(testCreateDate, item.CreateDate);
|
||||
Assert.AreEqual(testUpdateDate, item.UpdateDate);
|
||||
Assert.AreEqual(testCreatorId, item.CreatorId);
|
||||
Assert.AreEqual(testParentId, item.ParentId);
|
||||
Assert.AreEqual(testSortOrder, item.SortOrder);
|
||||
Assert.AreEqual(testPath, item.Path);
|
||||
Assert.AreEqual(testLevel, item.Level);
|
||||
Assert.AreEqual(testContentTypeAlias, item.ContentTypeAlias);
|
||||
Assert.AreEqual(testContentTypeIcon, item.ContentTypeIcon);
|
||||
Assert.AreEqual(testContentTypeThumbnail, item.ContentTypeThumbnail);
|
||||
Assert.AreEqual(testHasChildren, item.HasChildren);
|
||||
Assert.AreEqual(testPublished, item.Published);
|
||||
Assert.AreEqual(2, item.AdditionalData.Count);
|
||||
Assert.AreEqual(testAdditionalData1.Value, item.AdditionalData[testAdditionalData1.Key]);
|
||||
Assert.AreEqual(testAdditionalData2.Value, item.AdditionalData[testAdditionalData2.Key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.Entities;
|
||||
using Umbraco.Core.Serialization;
|
||||
|
||||
namespace Umbraco.Tests.Models
|
||||
{
|
||||
[TestFixture]
|
||||
public class LightEntityTest
|
||||
{
|
||||
[Test]
|
||||
public void Can_Serialize_Without_Error()
|
||||
{
|
||||
var item = new DocumentEntitySlim()
|
||||
{
|
||||
Id = 3,
|
||||
ContentTypeAlias = "test1",
|
||||
CreatorId = 4,
|
||||
Key = Guid.NewGuid(),
|
||||
UpdateDate = DateTime.Now,
|
||||
CreateDate = DateTime.Now,
|
||||
Name = "Test",
|
||||
ParentId = 5,
|
||||
SortOrder = 6,
|
||||
Path = "-1,23",
|
||||
Level = 7,
|
||||
ContentTypeIcon = "icon",
|
||||
ContentTypeThumbnail = "thumb",
|
||||
HasChildren = true,
|
||||
Edited = true,
|
||||
Published = true,
|
||||
NodeObjectType = Guid.NewGuid()
|
||||
};
|
||||
item.AdditionalData.Add("test1", 3);
|
||||
item.AdditionalData.Add("test2", "valuie");
|
||||
|
||||
var json = JsonConvert.SerializeObject(item);
|
||||
Debug.Print(json); // FIXME: compare with v7
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -294,7 +294,6 @@
|
||||
<Compile Include="Models\ContentTypeTests.cs" />
|
||||
<Compile Include="Models\DeepCloneHelperTests.cs" />
|
||||
<Compile Include="Models\DictionaryTranslationTests.cs" />
|
||||
<Compile Include="Models\LightEntityTest.cs" />
|
||||
<Compile Include="Web\Mvc\RenderModelBinderTests.cs" />
|
||||
<Compile Include="Web\Mvc\SurfaceControllerTests.cs" />
|
||||
<Compile Include="Web\Mvc\UmbracoViewPageTests.cs" />
|
||||
|
||||
Reference in New Issue
Block a user