Hopefull fixing tests

This commit is contained in:
Stephan
2016-07-05 15:11:10 +02:00
parent ac44079044
commit 1f40043740
3 changed files with 34 additions and 14 deletions

View File

@@ -160,28 +160,42 @@ namespace Umbraco.Core.Models
// "garbage-in", accept what we can & convert
// throw only if conversion is not possible
var s = value.ToString();
switch (_propertyType.DataTypeDatabaseType)
{
case DataTypeDatabaseType.Nvarchar:
case DataTypeDatabaseType.Ntext:
value = value.ToString();
value = s;
break;
case DataTypeDatabaseType.Integer:
var convInt = value.TryConvertTo<int>();
if (convInt == false) ThrowTypeException(value, typeof(int), _propertyType.Alias);
value = convInt.Result;
if (s.IsNullOrWhiteSpace()) value = null; // assume empty means null
else
{
var convInt = value.TryConvertTo<int>();
if (convInt == false) ThrowTypeException(value, typeof(int), _propertyType.Alias);
value = convInt.Result;
}
break;
case DataTypeDatabaseType.Decimal:
var convDecimal = value.TryConvertTo<decimal>();
if (convDecimal == false) ThrowTypeException(value, typeof(decimal), _propertyType.Alias);
// need to normalize the value (change the scaling factor and remove trailing zeroes)
// because the underlying database is going to mess with the scaling factor anyways.
value = convDecimal.Result.Normalize();
if (s.IsNullOrWhiteSpace()) value = null; // assume empty means null
else
{
var convDecimal = value.TryConvertTo<decimal>();
if (convDecimal == false) ThrowTypeException(value, typeof (decimal), _propertyType.Alias);
// need to normalize the value (change the scaling factor and remove trailing zeroes)
// because the underlying database is going to mess with the scaling factor anyways.
value = convDecimal.Result.Normalize();
}
break;
case DataTypeDatabaseType.Date:
var convDateTime = value.TryConvertTo<DateTime>();
if (convDateTime == false) ThrowTypeException(value, typeof(DateTime), _propertyType.Alias);
value = convDateTime.Result;
if (s.IsNullOrWhiteSpace()) value = null; // assume empty means null
else
{
var convDateTime = value.TryConvertTo<DateTime>();
if (convDateTime == false) ThrowTypeException(value, typeof (DateTime), _propertyType.Alias);
value = convDateTime.Result;
}
break;
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -212,7 +213,12 @@ namespace Umbraco.Tests.Models.Mapping
Assert.AreEqual(p.Alias, pDto.Alias);
Assert.AreEqual(p.Id, pDto.Id);
Assert.IsTrue(p.Value == null ? pDto.Value == string.Empty : pDto.Value == p.Value);
if (p.Value == null)
Assert.AreEqual(pDto.Value, string.Empty);
else if (p.Value is decimal)
Assert.AreEqual(pDto.Value, ((decimal) p.Value).ToString(NumberFormatInfo.InvariantInfo));
else
Assert.AreEqual(pDto.Value, p.Value.ToString());
}
private void AssertProperty<TPersisted>(ContentItemBasic<ContentPropertyDto, TPersisted> result, Property p)

View File

@@ -412,7 +412,7 @@ namespace Umbraco.Tests.TestHelpers.Entities
contentCollection.Add(new PropertyType(Constants.PropertyEditors.NoEditAlias, DataTypeDatabaseType.Integer) { Alias = Constants.Conventions.Media.Width, Name = "Width", Description = "", Mandatory = false, SortOrder = 2, DataTypeDefinitionId = -90 });
contentCollection.Add(new PropertyType(Constants.PropertyEditors.NoEditAlias, DataTypeDatabaseType.Integer) { Alias = Constants.Conventions.Media.Height, Name = "Height", Description = "", Mandatory = false, SortOrder = 2, DataTypeDefinitionId = -90 });
contentCollection.Add(new PropertyType(Constants.PropertyEditors.NoEditAlias, DataTypeDatabaseType.Integer) { Alias = Constants.Conventions.Media.Bytes, Name = "Bytes", Description = "", Mandatory = false, SortOrder = 2, DataTypeDefinitionId = -90 });
contentCollection.Add(new PropertyType(Constants.PropertyEditors.NoEditAlias, DataTypeDatabaseType.Integer) { Alias = Constants.Conventions.Media.Extension, Name = "File Extension", Description = "", Mandatory = false, SortOrder = 2, DataTypeDefinitionId = -90 });
contentCollection.Add(new PropertyType(Constants.PropertyEditors.NoEditAlias, DataTypeDatabaseType.Nvarchar) { Alias = Constants.Conventions.Media.Extension, Name = "File Extension", Description = "", Mandatory = false, SortOrder = 2, DataTypeDefinitionId = -90 });
mediaType.PropertyGroups.Add(new PropertyGroup(contentCollection) { Name = "Media", SortOrder = 1 });