Skip to content

Commit

Permalink
Adding unit tests for projections
Browse files Browse the repository at this point in the history
  • Loading branch information
anisimovyuriy committed Nov 6, 2015
1 parent b126ac0 commit 5be1a9e
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ExpressMapper NET40/MappingServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected virtual bool ComplexMapCondition(Type src, Type dst)

public Expression GetMemberMappingExpression(Expression left, Expression right)
{
var nullCheckNestedMemberVisitor = new NullCheckNestedMemberVisitor();
var nullCheckNestedMemberVisitor = new NullCheckNestedMemberVisitor(false);
nullCheckNestedMemberVisitor.Visit(right);

var destNullableType = Nullable.GetUnderlyingType(left.Type);
Expand Down
25 changes: 23 additions & 2 deletions ExpressMapper NET40/NullCheckNestedMemberVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,27 @@

namespace ExpressMapper
{
/// <summary>
/// NullCheckNestedMemberVisitor
/// </summary>
public class NullCheckNestedMemberVisitor : ExpressionVisitor
{
private readonly bool _isNull;

/// <summary>
/// NullCheckNestedMemberVisitor constructor
/// </summary>
/// <param name="isNull"></param>
public NullCheckNestedMemberVisitor(bool isNull)
{
_isNull = isNull;
}

private readonly List<string> _uniquefList = new List<string>();

/// <summary>
/// CheckNullExpression
/// </summary>
public Expression CheckNullExpression { get; set; }

protected override Expression VisitMember(MemberExpression node)
Expand All @@ -18,14 +36,17 @@ protected override Expression VisitMember(MemberExpression node)
if (!_uniquefList.Contains(memberExpression.ToString()))
{
_uniquefList.Add(memberExpression.ToString());
var expression = _isNull
? Expression.Constant(null, memberExpression.Type)
: (Expression)Expression.Default(memberExpression.Type);
if (CheckNullExpression == null)
{
CheckNullExpression = Expression.Equal(memberExpression, Expression.Default(memberExpression.Type));
CheckNullExpression = Expression.Equal(memberExpression, expression);
}
else
{
CheckNullExpression =
Expression.OrElse(Expression.Equal(memberExpression, Expression.Default(memberExpression.Type)),
Expression.OrElse(Expression.Equal(memberExpression, expression),
CheckNullExpression);
}
}
Expand Down
8 changes: 8 additions & 0 deletions ExpressMapper NET40/SourceTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ where p.ParameterType.GetGenericArguments().Count() == 2
Expression.Condition(
Expression.Equal(sourceExp, Expression.Constant(null, sourceExp.Type)),
Expression.Constant(null, destProp.PropertyType), clearanceExp);

//var nullCheckNestedMemberVisitor = new NullCheckNestedMemberVisitor(true);
//nullCheckNestedMemberVisitor.Visit(sourceExp);

//expression = nullCheckNestedMemberVisitor.CheckNullExpression != null
// ? Expression.Condition(nullCheckNestedMemberVisitor.CheckNullExpression,
// Expression.Constant(null, destProp.PropertyType), expression)
// : expression;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tests\CustomMemberMappingTest.cs" />
<Compile Include="Tests\NestedAssociationNullChecktest.cs" />
<Compile Include="Tests\RecursionExpressionHandlingTest.cs" />
<Compile Include="ViewModel\CatalogueGroupViewModel.cs" />
<Compile Include="ViewModel\CatalogueViewModel.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ExpressMapper.Extensions;
using ExpressMapper.Tests.Projections.Entities;
using ExpressMapper.Tests.Projections.ViewModel;
using NUnit.Framework;

namespace ExpressMapper.Tests.Projections.Tests
{
public class NestedAssociationNullChecktest : BaseTest
{
private List<ProductViewModel> _result;
private List<ProductViewModel> _planResult;

#region Initialize data

private void InitializeData()
{
var sizeId = Guid.NewGuid();
var sizeName = "Medium";
var sizeCode = "M";

var size = new Size
{
Id = sizeId,
Name = sizeName,
Code = sizeCode
};

var sizeVm = new SizeViewModel
{
Id = sizeId,
Name = sizeName,
Code = sizeCode
};

var productVarId = Guid.NewGuid();
var productVarName = "Orange";
var productVarColor = "Orange";
var productVariant = new ProductVariant
{
Id = productVarId,
Name = productVarName,
Color = productVarColor,
Size = size,
SizeId = size.Id
};

var productVariantVm = new ProductVariantViewModel
{
Id = productVarId,
Name = productVarName,
Color = productVarColor,
Size = sizeVm
};

var productId = Guid.NewGuid();
var prodName = "Blue Jeans";
var prodDimensions = "23x56x21";
var product = new Product
{
Id = productId,
Name = prodName,
Dimensions = prodDimensions,
Variant = productVariant,
VariantId = productVariant.Id
};

var productViewModel = new ProductViewModel
{
Id = productId,
Name = prodName + " : " + sizeVm.Code,
Dimensions = prodDimensions,
Variant = productVariantVm,
SizeName = sizeName
};


var prodVarId1 = Guid.NewGuid();
var prodVarName1 = "Yellow";
var prodVarColor1 = "Yellow";
var productVariant1 = new ProductVariant
{
Id = prodVarId1,
Name = prodVarName1,
Color = prodVarColor1
};

var productVariantVm1 = new ProductVariantViewModel
{
Id = prodVarId1,
Name = prodVarName1,
Color = prodVarColor1
};

var prodId1 = Guid.NewGuid();
var prodName1 = "Blue Jeans";
var prodDimensions1 = "53x51x99";
var product1 = new Product
{
Id = prodId1,
Name = prodName1,
Dimensions = prodDimensions1,
Variant = productVariant1,
VariantId = productVariant1.Id
};

var productVm1 = new ProductViewModel
{
Id = prodId1,
Name = prodName1 + " : ",
Dimensions = prodDimensions1,
Variant = productVariantVm1,
SizeName = null
};

var prodId2 = Guid.NewGuid();
var prodName2 = "Precious";
var prodDimensions2 = "13x36x61";
var product2 = new Product
{
Id = prodId2,
Name = prodName2,
Dimensions = prodDimensions2
};

var prodVm2 = new ProductViewModel
{
Id = prodId2,
Name = prodName2 + " : ",
Dimensions = prodDimensions2,
SizeName = null
};

Context.Set<Size>().Add(size);
Context.Set<ProductVariant>().Add(productVariant);
Context.Set<Product>().Add(product);

Context.Set<ProductVariant>().Add(productVariant1);
Context.Set<Product>().Add(product1);

Context.Set<Product>().Add(product2);

Context.SaveChanges();

_planResult = new List<ProductViewModel>
{
productViewModel, productVm1, prodVm2
};
}

#endregion

#region Register mappings

private static void RegisterMappings()
{
Mapper.Register<Product, ProductViewModel>()
.Member(t => t.Name, t => t.Name + " : " + t.Variant.Size.Code)
.Member(t => t.SizeName, t => t.Variant.Size.Name);
Mapper.Register<ProductVariant, ProductVariantViewModel>();
Mapper.Register<Size, SizeViewModel>();
}

#endregion

protected override void Setup()
{
InitializeData();
RegisterMappings();
}

protected override void Execute()
{
_result = Context.Set<Product>().Project<Product, ProductViewModel>().ToList();
}

[Test]
public void Test()
{
_result = _result.OrderBy(p => p.Id).ToList();
_planResult = _planResult.OrderBy(p => p.Id).ToList();
Assert.AreEqual(_result.Count(), 3);
for (var i = 0; i < _result.Count; i++)
{
Assert.AreEqual(_result[i], _planResult[i]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ public class ProductViewModel : IEquatable<ProductViewModel>
public Guid Id { get; set; }
public string Name { get; set; }
public string Dimensions { get; set; }
public string SizeName { get; set; }
public ProductVariantViewModel Variant { get; set; }

public bool Equals(ProductViewModel other)
{
return Id == other.Id && Name == other.Name && Dimensions == other.Dimensions && ((Variant == null && other.Variant == null) || Variant.Equals(other.Variant));
return Id == other.Id && Name == other.Name && SizeName == other.SizeName && Dimensions == other.Dimensions && ((Variant == null && other.Variant == null) || Variant.Equals(other.Variant));
}
}
}

0 comments on commit 5be1a9e

Please sign in to comment.