Adding numeric attribute for testing.
Adding type test for composition implementation.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Umbraco.Tests.CodeFirst.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Interface, AllowMultiple = false, Inherited = true)]
|
||||
public class AliasAttribute : Attribute
|
||||
{
|
||||
public AliasAttribute(string @alias)
|
||||
|
||||
40
src/Umbraco.Tests/CodeFirst/Attributes/Numeric.cs
Normal file
40
src/Umbraco.Tests/CodeFirst/Attributes/Numeric.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Tests.CodeFirst.Definitions;
|
||||
using umbraco.editorControls.numberfield;
|
||||
|
||||
namespace Umbraco.Tests.CodeFirst.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
|
||||
public class Numeric : PropertyTypeAttribute
|
||||
{
|
||||
public Numeric(string dataTypeName)
|
||||
: base(typeof(IDataTypenteger))
|
||||
{
|
||||
DataTypeName = dataTypeName;
|
||||
DatabaseType = DataTypeDatabaseType.Integer;
|
||||
}
|
||||
|
||||
public string DataTypeName { get; set; }
|
||||
|
||||
public string PreValue { get; set; }
|
||||
|
||||
public override PropertyDefinition GetPropertyConvention()
|
||||
{
|
||||
var definition = new PropertyDefinition();
|
||||
|
||||
definition.Mandatory = Mandatory;
|
||||
definition.ValidationRegExp = string.IsNullOrEmpty(ValidationRegExp) ? ValidationRegExp : string.Empty;
|
||||
definition.PropertyGroup = string.IsNullOrEmpty(PropertyGroup) ? "Generic Properties" : PropertyGroup;
|
||||
definition.DataTypeDefinition = Conventions.CreateDataTypeDefinitionFromAttribute(this, DataTypeName);
|
||||
|
||||
if(string.IsNullOrEmpty(PreValue) == false)
|
||||
{
|
||||
Conventions.CreatePrevalueForDataTypeDefinition(definition.DataTypeDefinition.Id, PreValue, 0,
|
||||
string.Empty);
|
||||
}
|
||||
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ namespace Umbraco.Tests.CodeFirst.Attributes
|
||||
definition.Mandatory = Mandatory;
|
||||
definition.ValidationRegExp = string.IsNullOrEmpty(ValidationRegExp) ? ValidationRegExp : string.Empty;
|
||||
definition.PropertyGroup = string.IsNullOrEmpty(PropertyGroup) ? "Generic Properties" : PropertyGroup;
|
||||
definition.DataTypeDefinition = Conventions.DataTypeConvention(this, Type);
|
||||
definition.DataTypeDefinition = Conventions.GetDataTypeDefinitionByAttributeOrType(this, Type);
|
||||
|
||||
return definition;
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace Umbraco.Tests.CodeFirst.Definitions
|
||||
//DataTypeDefinition fallback
|
||||
if(definition.DataTypeDefinition == null)
|
||||
{
|
||||
definition.DataTypeDefinition = Conventions.DataTypeConvention(null, propertyInfo.PropertyType);
|
||||
definition.DataTypeDefinition = Conventions.GetDataTypeDefinitionByAttributeOrType(null, propertyInfo.PropertyType);
|
||||
}
|
||||
|
||||
if(string.IsNullOrEmpty(definition.PropertyGroup))
|
||||
@@ -62,8 +62,8 @@ namespace Umbraco.Tests.CodeFirst.Definitions
|
||||
if (string.IsNullOrEmpty(definition.Alias))
|
||||
{
|
||||
var aliasAttribute = propertyInfo.FirstAttribute<AliasAttribute>();
|
||||
definition.Alias = Conventions.PropertyTypeAliasConvention(aliasAttribute, propertyInfo.Name);
|
||||
definition.Name = Conventions.PropertyTypeNameConvention(aliasAttribute, propertyInfo.Name);
|
||||
definition.Alias = Conventions.GetPropertyTypeAlias(aliasAttribute, propertyInfo.Name);
|
||||
definition.Name = Conventions.GetPropertyTypeName(aliasAttribute, propertyInfo.Name);
|
||||
}
|
||||
|
||||
//Description fallback
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
using System.Linq;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Tests.CodeFirst.Attributes;
|
||||
using umbraco.interfaces;
|
||||
@@ -16,7 +18,7 @@ namespace Umbraco.Tests.CodeFirst.Definitions
|
||||
/// <param name="attribute"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static IDataTypeDefinition DataTypeConvention(PropertyTypeAttribute attribute, Type type)
|
||||
public static IDataTypeDefinition GetDataTypeDefinitionByAttributeOrType(PropertyTypeAttribute attribute, Type type)
|
||||
{
|
||||
if (attribute != null)
|
||||
{
|
||||
@@ -27,16 +29,16 @@ namespace Umbraco.Tests.CodeFirst.Definitions
|
||||
if (definition == null)
|
||||
{
|
||||
definition = new DataTypeDefinition(-1, dataType.Id)
|
||||
{
|
||||
DatabaseType = attribute.DatabaseType,
|
||||
Name = dataType.DataTypeName
|
||||
};
|
||||
{
|
||||
DatabaseType = attribute.DatabaseType,
|
||||
Name = dataType.DataTypeName
|
||||
};
|
||||
ServiceFactory.DataTypeService.Save(definition, 0);
|
||||
}
|
||||
return definition;
|
||||
}
|
||||
|
||||
return TypeToPredefinedDataTypeConvention(type);
|
||||
return GetPredefinedDataTypeDefinitionByType(type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -44,7 +46,7 @@ namespace Umbraco.Tests.CodeFirst.Definitions
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static IDataTypeDefinition TypeToPredefinedDataTypeConvention(Type type)
|
||||
public static IDataTypeDefinition GetPredefinedDataTypeDefinitionByType(Type type)
|
||||
{
|
||||
if (type == typeof(bool))
|
||||
{
|
||||
@@ -73,13 +75,53 @@ namespace Umbraco.Tests.CodeFirst.Definitions
|
||||
return definitions.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new DataTypeDefinition based on the Type in the PropertyTypeAttribute
|
||||
/// </summary>
|
||||
/// <param name="attribute"></param>
|
||||
/// <param name="dataTypeDefinitionName"></param>
|
||||
/// <returns></returns>
|
||||
public static IDataTypeDefinition CreateDataTypeDefinitionFromAttribute(PropertyTypeAttribute attribute, string dataTypeDefinitionName)
|
||||
{
|
||||
var instance = Activator.CreateInstance(attribute.Type);
|
||||
var dataType = instance as IDataType;
|
||||
|
||||
var definition = new DataTypeDefinition(-1, dataType.Id)
|
||||
{
|
||||
DatabaseType = attribute.DatabaseType,
|
||||
Name = dataTypeDefinitionName
|
||||
};
|
||||
ServiceFactory.DataTypeService.Save(definition, 0);
|
||||
return definition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a PreValue for a <see cref="IDataTypeDefinition"/>
|
||||
/// </summary>
|
||||
/// <param name="dataTypeDefinitionId"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="sortOrder"></param>
|
||||
/// <param name="alias"></param>
|
||||
public static void CreatePrevalueForDataTypeDefinition(int dataTypeDefinitionId, string value, int sortOrder, string alias)
|
||||
{
|
||||
var poco = new DataTypePreValueDto
|
||||
{
|
||||
Alias = alias,
|
||||
DataTypeNodeId = dataTypeDefinitionId,
|
||||
SortOrder = sortOrder,
|
||||
Value = value
|
||||
};
|
||||
|
||||
DatabaseFactory.Current.Database.Insert(poco);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convention to get the Alias of the PropertyType from the AliasAttribute or the property itself
|
||||
/// </summary>
|
||||
/// <param name="attribute"></param>
|
||||
/// <param name="propertyName"></param>
|
||||
/// <returns></returns>
|
||||
public static string PropertyTypeAliasConvention(AliasAttribute attribute, string propertyName)
|
||||
public static string GetPropertyTypeAlias(AliasAttribute attribute, string propertyName)
|
||||
{
|
||||
return attribute == null ? propertyName.ToUmbracoAlias() : attribute.Alias;
|
||||
}
|
||||
@@ -90,7 +132,7 @@ namespace Umbraco.Tests.CodeFirst.Definitions
|
||||
/// <param name="attribute"></param>
|
||||
/// <param name="propertyName"></param>
|
||||
/// <returns></returns>
|
||||
public static string PropertyTypeNameConvention(AliasAttribute attribute, string propertyName)
|
||||
public static string GetPropertyTypeName(AliasAttribute attribute, string propertyName)
|
||||
{
|
||||
if (attribute == null)
|
||||
return propertyName.SplitPascalCasing();
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
namespace Umbraco.Tests.CodeFirst.TestModels.Composition
|
||||
{
|
||||
public class Base
|
||||
public class Base : IBase
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface IBase
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
namespace Umbraco.Tests.CodeFirst.TestModels.Composition
|
||||
using Umbraco.Tests.CodeFirst.Attributes;
|
||||
|
||||
namespace Umbraco.Tests.CodeFirst.TestModels.Composition
|
||||
{
|
||||
public class Meta : IMeta
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[Alias("meta", Name = "Meta")]
|
||||
public interface IMeta
|
||||
{}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
namespace Umbraco.Tests.CodeFirst.TestModels.Composition
|
||||
using Umbraco.Tests.CodeFirst.Attributes;
|
||||
|
||||
namespace Umbraco.Tests.CodeFirst.TestModels.Composition
|
||||
{
|
||||
public class Seo : ISeo
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface ISeo
|
||||
[Alias("seo", Name = "Seo")]
|
||||
public interface ISeo : IBase
|
||||
{}
|
||||
}
|
||||
10
src/Umbraco.Tests/CodeFirst/TestModels/NumericModel.cs
Normal file
10
src/Umbraco.Tests/CodeFirst/TestModels/NumericModel.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Umbraco.Tests.CodeFirst.Attributes;
|
||||
|
||||
namespace Umbraco.Tests.CodeFirst.TestModels
|
||||
{
|
||||
public class NumericModel
|
||||
{
|
||||
[Numeric("Number DataType", PreValue = "5", PropertyGroup = "Content")]
|
||||
public int Number { get; set; }
|
||||
}
|
||||
}
|
||||
24
src/Umbraco.Tests/CodeFirst/TypeInheritanceTest.cs
Normal file
24
src/Umbraco.Tests/CodeFirst/TypeInheritanceTest.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Tests.CodeFirst.TestModels.Composition;
|
||||
|
||||
namespace Umbraco.Tests.CodeFirst
|
||||
{
|
||||
[TestFixture]
|
||||
public class TypeInheritanceTest
|
||||
{
|
||||
[Test]
|
||||
public void Can_Get_Interfaces_From_Type()
|
||||
{
|
||||
var type = typeof (News);
|
||||
var interfaces = type.GetInterfaces().ToList();
|
||||
|
||||
bool hasSeo = interfaces.Any(x => x.Name == typeof(ISeo).Name);
|
||||
bool hasMeta = interfaces.Any(x => x.Name == typeof(IMeta).Name);
|
||||
|
||||
Assert.That(hasSeo, Is.True);
|
||||
Assert.That(hasMeta, Is.True);
|
||||
Assert.That(interfaces.Count, Is.EqualTo(3));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,6 +92,7 @@
|
||||
<Compile Include="CodeFirst\Attributes\AliasAttribute.cs" />
|
||||
<Compile Include="CodeFirst\Attributes\ContentTypeAttribute.cs" />
|
||||
<Compile Include="CodeFirst\Attributes\DescriptionAttribute.cs" />
|
||||
<Compile Include="CodeFirst\Attributes\Numeric.cs" />
|
||||
<Compile Include="CodeFirst\Attributes\PropertyTypeAttribute.cs" />
|
||||
<Compile Include="CodeFirst\Attributes\PropertyTypeConventionAttribute.cs" />
|
||||
<Compile Include="CodeFirst\Attributes\RichtextAttribute.cs" />
|
||||
@@ -111,10 +112,12 @@
|
||||
<Compile Include="CodeFirst\TestModels\DecoratedModelPage.cs" />
|
||||
<Compile Include="CodeFirst\TestModels\Home.cs" />
|
||||
<Compile Include="CodeFirst\TestModels\ModelWithNewDataType.cs" />
|
||||
<Compile Include="CodeFirst\TestModels\NumericModel.cs" />
|
||||
<Compile Include="CodeFirst\TestModels\PlainPocoType.cs" />
|
||||
<Compile Include="CodeFirst\TestModels\SimpleContentPage.cs" />
|
||||
<Compile Include="CodeFirst\TestModels\TextPage.cs" />
|
||||
<Compile Include="CodeFirst\TopologicalSorter.cs" />
|
||||
<Compile Include="CodeFirst\TypeInheritanceTest.cs" />
|
||||
<Compile Include="Configurations\FileSystemProviderTests.cs" />
|
||||
<Compile Include="Configurations\RepositorySettingsTests.cs" />
|
||||
<Compile Include="ContentStores\PublishMediaStoreTests.cs" />
|
||||
|
||||
Reference in New Issue
Block a user