From ce12bfa80e41ee1c81facddf98431cd0e642fb24 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Sun, 25 Nov 2012 19:19:10 -0100 Subject: [PATCH] Adding numeric attribute for testing. Adding type test for composition implementation. --- .../CodeFirst/Attributes/AliasAttribute.cs | 2 +- .../CodeFirst/Attributes/Numeric.cs | 40 +++++++++++++ .../Attributes/PropertyTypeAttribute.cs | 2 +- .../ContentTypeDefinitionFactory.cs | 6 +- .../CodeFirst/Definitions/Conventions.cs | 60 ++++++++++++++++--- .../CodeFirst/TestModels/Composition/Base.cs | 7 ++- .../CodeFirst/TestModels/Composition/Meta.cs | 5 +- .../CodeFirst/TestModels/Composition/Seo.cs | 7 ++- .../CodeFirst/TestModels/NumericModel.cs | 10 ++++ .../CodeFirst/TypeInheritanceTest.cs | 24 ++++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 3 + 11 files changed, 148 insertions(+), 18 deletions(-) create mode 100644 src/Umbraco.Tests/CodeFirst/Attributes/Numeric.cs create mode 100644 src/Umbraco.Tests/CodeFirst/TestModels/NumericModel.cs create mode 100644 src/Umbraco.Tests/CodeFirst/TypeInheritanceTest.cs diff --git a/src/Umbraco.Tests/CodeFirst/Attributes/AliasAttribute.cs b/src/Umbraco.Tests/CodeFirst/Attributes/AliasAttribute.cs index 91f4e62e5f..df6e333c67 100644 --- a/src/Umbraco.Tests/CodeFirst/Attributes/AliasAttribute.cs +++ b/src/Umbraco.Tests/CodeFirst/Attributes/AliasAttribute.cs @@ -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) diff --git a/src/Umbraco.Tests/CodeFirst/Attributes/Numeric.cs b/src/Umbraco.Tests/CodeFirst/Attributes/Numeric.cs new file mode 100644 index 0000000000..b46eedb78e --- /dev/null +++ b/src/Umbraco.Tests/CodeFirst/Attributes/Numeric.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/CodeFirst/Attributes/PropertyTypeAttribute.cs b/src/Umbraco.Tests/CodeFirst/Attributes/PropertyTypeAttribute.cs index 2e13cfcfb6..8304e03fd4 100644 --- a/src/Umbraco.Tests/CodeFirst/Attributes/PropertyTypeAttribute.cs +++ b/src/Umbraco.Tests/CodeFirst/Attributes/PropertyTypeAttribute.cs @@ -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; } diff --git a/src/Umbraco.Tests/CodeFirst/Definitions/ContentTypeDefinitionFactory.cs b/src/Umbraco.Tests/CodeFirst/Definitions/ContentTypeDefinitionFactory.cs index a88f27b1c4..743fca8002 100644 --- a/src/Umbraco.Tests/CodeFirst/Definitions/ContentTypeDefinitionFactory.cs +++ b/src/Umbraco.Tests/CodeFirst/Definitions/ContentTypeDefinitionFactory.cs @@ -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(); - 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 diff --git a/src/Umbraco.Tests/CodeFirst/Definitions/Conventions.cs b/src/Umbraco.Tests/CodeFirst/Definitions/Conventions.cs index 91385bd22b..6173c30213 100644 --- a/src/Umbraco.Tests/CodeFirst/Definitions/Conventions.cs +++ b/src/Umbraco.Tests/CodeFirst/Definitions/Conventions.cs @@ -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 /// /// /// - 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); } /// @@ -44,7 +46,7 @@ namespace Umbraco.Tests.CodeFirst.Definitions /// /// /// - 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(); } + /// + /// Creates a new DataTypeDefinition based on the Type in the PropertyTypeAttribute + /// + /// + /// + /// + 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; + } + + /// + /// Creates a PreValue for a + /// + /// + /// + /// + /// + 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); + } + /// /// Convention to get the Alias of the PropertyType from the AliasAttribute or the property itself /// /// /// /// - 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 /// /// /// - public static string PropertyTypeNameConvention(AliasAttribute attribute, string propertyName) + public static string GetPropertyTypeName(AliasAttribute attribute, string propertyName) { if (attribute == null) return propertyName.SplitPascalCasing(); diff --git a/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Base.cs b/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Base.cs index 61aab4c2cf..c45ef1f299 100644 --- a/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Base.cs +++ b/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Base.cs @@ -1,7 +1,12 @@ namespace Umbraco.Tests.CodeFirst.TestModels.Composition { - public class Base + public class Base : IBase { } + + public interface IBase + { + + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Meta.cs b/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Meta.cs index 94ed429e50..5d0d0f298f 100644 --- a/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Meta.cs +++ b/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Meta.cs @@ -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 {} } \ No newline at end of file diff --git a/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Seo.cs b/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Seo.cs index cd94ad61df..c51756fc77 100644 --- a/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Seo.cs +++ b/src/Umbraco.Tests/CodeFirst/TestModels/Composition/Seo.cs @@ -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 {} } \ No newline at end of file diff --git a/src/Umbraco.Tests/CodeFirst/TestModels/NumericModel.cs b/src/Umbraco.Tests/CodeFirst/TestModels/NumericModel.cs new file mode 100644 index 0000000000..743cbf64d6 --- /dev/null +++ b/src/Umbraco.Tests/CodeFirst/TestModels/NumericModel.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/CodeFirst/TypeInheritanceTest.cs b/src/Umbraco.Tests/CodeFirst/TypeInheritanceTest.cs new file mode 100644 index 0000000000..56ca19d999 --- /dev/null +++ b/src/Umbraco.Tests/CodeFirst/TypeInheritanceTest.cs @@ -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)); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 160b8baf43..c151464568 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -92,6 +92,7 @@ + @@ -111,10 +112,12 @@ + +