From 82f049e3900cde3b2e060fbd5af5918d7efec35b Mon Sep 17 00:00:00 2001 From: Yuriy Anisimov Date: Thu, 5 Nov 2015 01:17:04 -0800 Subject: [PATCH] uncommitted changes --- .../Tests/CustomMemberMappingTest.cs | 190 ++++++++++++++++++ .../Tests/RecursionExpressionHandlingTest.cs | 143 +++++++++++++ 2 files changed, 333 insertions(+) create mode 100644 ExpressMapper.Tests.Projections/Tests/CustomMemberMappingTest.cs create mode 100644 ExpressMapper.Tests.Projections/Tests/RecursionExpressionHandlingTest.cs diff --git a/ExpressMapper.Tests.Projections/Tests/CustomMemberMappingTest.cs b/ExpressMapper.Tests.Projections/Tests/CustomMemberMappingTest.cs new file mode 100644 index 0000000..99ff91e --- /dev/null +++ b/ExpressMapper.Tests.Projections/Tests/CustomMemberMappingTest.cs @@ -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 _result; + private List _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().Add(size); + Context.Set().Add(productVariant); + Context.Set().Add(product); + + Context.Set().Add(productVariant1); + Context.Set().Add(product1); + + Context.Set().Add(product2); + + Context.SaveChanges(); + + _planResult = new List + { + productViewModel, productVm1, prodVm2 + }; + } + + #endregion + + #region Register mappings + + private static void RegisterMappings() + { + Mapper.Register() + .Member(t => t.Name, t => t.Name + " " + t.Id); + Mapper.Register() + .Member(t => t.Size, t => t.Size); + Mapper.Register() + .Member(r => r.Name, t => t.Name + " " + t.Code); + } + + #endregion + + protected override void Setup() + { + InitializeData(); + RegisterMappings(); + } + + protected override void Execute() + { + _result = Context.Set().Project().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]); + } + } + } +} \ No newline at end of file diff --git a/ExpressMapper.Tests.Projections/Tests/RecursionExpressionHandlingTest.cs b/ExpressMapper.Tests.Projections/Tests/RecursionExpressionHandlingTest.cs new file mode 100644 index 0000000..e761de6 --- /dev/null +++ b/ExpressMapper.Tests.Projections/Tests/RecursionExpressionHandlingTest.cs @@ -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 _result; + private List _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().Add(catalogue); + Context.Set().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 { 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 { cvm, cvm1 } + }; + + Context.Set().Add(catalogueGroup); + Context.Set().Add(catalogueGroup1); + + Context.SaveChanges(); + _planResult = new List + { + cvtgr, cvtgr1 + }; + } + + #endregion + + #region Register mappings + + private static void RegisterMappings() + { + Mapper.Register(); + Mapper.Register(); + } + + #endregion + protected override void Setup() + { + InitializeData(); + RegisterMappings(); + } + + protected override void Execute() + { + _result = Context.Set().Project().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 SortCollections(List list) + { + list.ForEach(r => + { + r.Catalogues = r.Catalogues.OrderBy(s => s.Id); + }); + return list.OrderBy(r => r.Id).ToList(); + } + } +}