diff --git a/Slapper.AutoMapper.Tests/ArrayTests.cs b/Slapper.AutoMapper.Tests/ArrayTests.cs index 77d56d3..af4bb65 100644 --- a/Slapper.AutoMapper.Tests/ArrayTests.cs +++ b/Slapper.AutoMapper.Tests/ArrayTests.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using NUnit.Framework; namespace Slapper.Tests @@ -22,6 +23,13 @@ public class PersonWithProperties public string[] FavoriteFoods { get; set; } } + public class Department + { + public int Id { get; set; } + public string Name { get; set; } + public PersonWithProperties[] Staff { get; set; } + } + [Test] public void Can_Map_Null_Values_To_Null_Arrays() { @@ -77,5 +85,153 @@ public void Can_Map_Array_Values_To_Arrays() Assert.That( customer.LastName == lastName ); Assert.That( customer.FavoriteFoods == favoriteFoods ); } + + [Test] + public void Can_Map_Multiple_Dictionaries_To_Arrays() + { + // Arrange + const int id = 1; + const string name = "IT"; + const int firstEmployeeId = 1; + const string firstEmployeeLastName = "Smith"; + const int secondEmployeeId = 2; + const string secondEmployeeLastName = "Doe"; + + var firstDictionary = new Dictionary + { + {"Id", id}, + {"Name", name}, + {"Staff_Id", firstEmployeeId}, + {"Staff_LastName", firstEmployeeLastName} + }; + + var secondDictionary = new Dictionary + { + {"Id", id}, + {"Name", name}, + {"Staff_Id", secondEmployeeId}, + {"Staff_LastName", secondEmployeeLastName} + }; + + // Act + var department = + AutoMapper.Map(new List> {firstDictionary, secondDictionary}).Single(); + + // Assert + Assert.That(department.Id == id); + Assert.That(department.Name == name); + Assert.That(department.Staff.Count() == 2); + Assert.That(department.Staff[0].Id == firstEmployeeId); + Assert.That(department.Staff[0].LastName == firstEmployeeLastName); + Assert.That(department.Staff[1].Id == secondEmployeeId); + Assert.That(department.Staff[1].LastName == secondEmployeeLastName); + } + + public class Customer + { + public int CustomerId; + public string FirstName; + public string LastName; + public Order[] Orders; + } + + public class Order + { + public int OrderId; + public decimal OrderTotal; + public OrderDetail[] OrderDetails; + } + + public class OrderDetail + { + public int OrderDetailId; + public decimal OrderDetailTotal; + } + + [Test] + public void I_Can_Map_Nested_Types_With_Multiple_Identifiers_And_Resolve_Duplicate_Entries_Properly() + { + // Arrange + var dictionary = new Dictionary + { + { "CustomerId", 1 }, + { "FirstName", "Bob" }, + { "LastName", "Smith" }, + { "Orders_OrderId", 1 }, + { "Orders_OrderTotal", 50.50m }, + { "Orders_OrderDetails_OrderDetailId", 1 }, + { "Orders_OrderDetails_OrderDetailTotal", 25.00m } + }; + + var dictionary2 = new Dictionary + { + { "CustomerId", 1 }, + { "FirstName", "Bob" }, + { "LastName", "Smith" }, + { "Orders_OrderId", 1 }, + { "Orders_OrderTotal", 50.50m }, + { "Orders_OrderDetails_OrderDetailId", 2 }, + { "Orders_OrderDetails_OrderDetailTotal", 25.50m } + }; + + var dictionary3 = new Dictionary + { + { "CustomerId", 1 + 1 }, + { "FirstName", "Bob" }, + { "LastName", "Smith" }, + { "Orders_OrderId", 1 }, + { "Orders_OrderTotal", 50.50m }, + { "Orders_OrderDetails_OrderDetailId", 2 }, + { "Orders_OrderDetails_OrderDetailTotal", 25.50m } + }; + + var list = new List> { dictionary, dictionary2, dictionary3 }; + + // Act + var customers = Slapper.AutoMapper.Map(list).ToArray(); + + // Assert + + Assert.That(customers.Count() == 2); + Assert.That(customers.FirstOrDefault().Orders.Length == 1); // There should only be a single Order + Assert.That(customers.FirstOrDefault().Orders.FirstOrDefault().OrderDetails.Length == 2); // There should be two OrderDetails + } + + //[Test] + //public void Can_Detect_Duplicate_Parent_Members_And_Properly_Instantiate_The_Object_Only_Once() + //{ + // // Arrange + // const int id = 1; + // const string name = "IT"; + // const int firstEmployeeId = 1; + // const string firstEmployeeLastName = "Smith"; + + // var firstDictionary = new Dictionary + // { + // {"Id", id}, + // {"Name", name}, + // {"Staff_Id", firstEmployeeId}, + // {"Staff_LastName", firstEmployeeLastName} + // }; + + // var secondDictionary = new Dictionary + // { + // {"Id", id}, + // {"Name", name}, + // {"Staff_Id", firstEmployeeId}, + // {"Staff_LastName", firstEmployeeLastName} + // }; + + // // Act + // var department = + // AutoMapper.Map(new List> { firstDictionary, secondDictionary }).Single(); + + // // Assert + // Assert.That(department.Id == id); + // Assert.That(department.Name == name); + // Assert.That(department.Staff.Count() == 2); + // Assert.That(department.Staff[0].Id == firstEmployeeId); + // Assert.That(department.Staff[0].LastName == firstEmployeeLastName); + //} } } \ No newline at end of file diff --git a/Slapper.AutoMapper.Tests/MapDynamicTests.cs b/Slapper.AutoMapper.Tests/MapDynamicTests.cs index d3bed84..80b28c0 100644 --- a/Slapper.AutoMapper.Tests/MapDynamicTests.cs +++ b/Slapper.AutoMapper.Tests/MapDynamicTests.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Dynamic; using System.Linq; using NUnit.Framework; @@ -22,6 +23,46 @@ public class Order public decimal OrderTotal; } + [Test] + public void Returns_Empty_List_For_Null_Dictionary() + { + // Arrange + + // Act + var result = Slapper.AutoMapper.MapDynamic(null); + + // Assert + Assert.NotNull(result); + Assert.That(!result.Any()); + } + + [Test] + public void Returns_Null_For_Reference_Types() + { + // Arrange + object nullDictionary = null; + + // Act + var result = Slapper.AutoMapper.MapDynamic(nullDictionary); + + // Assert + Assert.IsNull(result); + } + + [Test] + public void Returns_Zero_For_Numeric_Value_Types() + { + // Arrange + object nullDictionary = null; + + // Act + var result = Slapper.AutoMapper.MapDynamic(nullDictionary); + + // Assert + Assert.IsNotNull(result); + Assert.That(result == 0); + } + [Test] public void Can_Handle_Mapping_A_Single_Dynamic_Object() { diff --git a/Slapper.AutoMapper.Tests/MappingToGuidTests.cs b/Slapper.AutoMapper.Tests/MappingToGuidTests.cs index 10207a9..2c6dfeb 100644 --- a/Slapper.AutoMapper.Tests/MappingToGuidTests.cs +++ b/Slapper.AutoMapper.Tests/MappingToGuidTests.cs @@ -129,6 +129,26 @@ public void Can_Map_Guid_String_Values_To_Guid_Properties() Assert.That( Equals( customer.UniqueId, uniqueId ) ); // This is what we are testing } + [Test] + public void Can_Map_Guid_Byte_Array_Values_To_Guid_Properties() + { + // Arrange + const int id = 1; + Guid uniqueId = Guid.NewGuid(); + + dynamic dynamicPerson = new ExpandoObject(); + dynamicPerson.Id = id; + dynamicPerson.UniqueId = uniqueId.ToByteArray(); // This is what we are testing + + // Act + PersonWithProperties customer = Slapper.AutoMapper.MapDynamic(dynamicPerson); + + // Assert + Assert.NotNull(customer); + Assert.That(customer.Id == id); + Assert.That(Equals(customer.UniqueId, uniqueId)); // This is what we are testing + } + [Test] public void Can_Map_Null_Values_To_Guid_Fields() { diff --git a/Slapper.AutoMapper.Tests/Slapper.Tests.csproj b/Slapper.AutoMapper.Tests/Slapper.Tests.csproj index ebe2552..6834426 100644 --- a/Slapper.AutoMapper.Tests/Slapper.Tests.csproj +++ b/Slapper.AutoMapper.Tests/Slapper.Tests.csproj @@ -69,6 +69,9 @@ Slapper + + +