Skip to content

Commit

Permalink
uncommitted changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anisimovyuriy committed Nov 5, 2015
1 parent a9e6073 commit 82f049e
Show file tree
Hide file tree
Showing 2 changed files with 333 additions and 0 deletions.
190 changes: 190 additions & 0 deletions ExpressMapper.Tests.Projections/Tests/CustomMemberMappingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
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
{
[TestFixture]
public class CustomMemberMappingTest : 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 + " " + sizeCode,
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 + " " + productId,
Dimensions = prodDimensions,
Variant = productVariantVm
};


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 + " " + prodId1,
Dimensions = prodDimensions1,
Variant = productVariantVm1
};

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 + " " + prodId2,
Dimensions = prodDimensions2
};

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.Id);
Mapper.Register<ProductVariant, ProductVariantViewModel>()
.Member(t => t.Size, t => t.Size);
Mapper.Register<Size, SizeViewModel>()
.Member(r => r.Name, t => t.Name + " " + t.Code);
}

#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
@@ -0,0 +1,143 @@
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 RecursionExpressionHandlingTest : BaseTest
{
private List<FullCatalogueGroupViewModel> _result;
private List<FullCatalogueGroupViewModel> _planResult;

#region Initialize data

private void InitializeData()
{
var catalId = Guid.NewGuid();
var catalName = "Catalogue # 1";
var catalogue = new Catalogue
{
Id = catalId,
Name = catalName
};

var cvm = new FullCatalogueViewModel
{
Id = catalId,
Name = catalName
};

var catalId1 = Guid.NewGuid();
var catalName1 = "Catalogue # 2";
var catalogue1 = new Catalogue
{
Id = catalId1,
Name = catalName1
};

var cvm1 = new FullCatalogueViewModel
{
Id = catalId1,
Name = catalName1
};

Context.Set<Catalogue>().Add(catalogue);
Context.Set<Catalogue>().Add(catalogue1);

var catalGrId = Guid.NewGuid();
var catalGrName = "CatalogueGroup #1";
var catalogueGroup = new CatalogueGroup
{
Id = catalGrId,
Name = catalGrName
};
catalogueGroup.Catalogues.Add(catalogue);

var cvtgr = new FullCatalogueGroupViewModel
{
Id = catalGrId,
Name = catalGrName,
Catalogues = new List<FullCatalogueViewModel> { cvm }
};

var catalGrId1 = Guid.NewGuid();
var catalGrName1 = "CatalogueGroup #2";
var catalogueGroup1 = new CatalogueGroup
{
Id = catalGrId1,
Name = catalGrName1
};
catalogueGroup1.Catalogues.AddRange(new[] { catalogue, catalogue1 });

var cvtgr1 = new FullCatalogueGroupViewModel
{
Id = catalGrId1,
Name = catalGrName1,
Catalogues = new List<FullCatalogueViewModel> { cvm, cvm1 }
};

Context.Set<CatalogueGroup>().Add(catalogueGroup);
Context.Set<CatalogueGroup>().Add(catalogueGroup1);

Context.SaveChanges();
_planResult = new List<FullCatalogueGroupViewModel>
{
cvtgr, cvtgr1
};
}

#endregion

#region Register mappings

private static void RegisterMappings()
{
Mapper.Register<Catalogue, FullCatalogueViewModel>();
Mapper.Register<CatalogueGroup, FullCatalogueGroupViewModel>();
}

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

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

[Test]
public void Test()
{
_result = SortCollections(_result);
_planResult = SortCollections(_planResult);

Assert.AreEqual(_result.Count, 2);
for (var i = 0; i < _result.Count; i++)
{
Assert.AreEqual(_result[i].Id, _planResult[i].Id);
Assert.AreEqual(_result[i].Name, _planResult[i].Name);
for (var j = 0; j < _result[i].Catalogues.Count(); j++)
{
Assert.AreEqual(_result[i].Catalogues.ElementAt(j).Id, _planResult[i].Catalogues.ElementAt(j).Id);
Assert.AreEqual(_result[i].Catalogues.ElementAt(j).Name, _planResult[i].Catalogues.ElementAt(j).Name);
}
}
}

private List<FullCatalogueGroupViewModel> SortCollections(List<FullCatalogueGroupViewModel> list)
{
list.ForEach(r =>
{
r.Catalogues = r.Catalogues.OrderBy(s => s.Id);
});
return list.OrderBy(r => r.Id).ToList();
}
}
}

0 comments on commit 82f049e

Please sign in to comment.