Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions Slapper.AutoMapper.Tests/ArrayTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace Slapper.Tests
Expand All @@ -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()
{
Expand Down Expand Up @@ -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<string, object>
{
{"Id", id},
{"Name", name},
{"Staff_Id", firstEmployeeId},
{"Staff_LastName", firstEmployeeLastName}
};

var secondDictionary = new Dictionary<string, object>
{
{"Id", id},
{"Name", name},
{"Staff_Id", secondEmployeeId},
{"Staff_LastName", secondEmployeeLastName}
};

// Act
var department =
AutoMapper.Map<Department>(new List<IDictionary<string, object>> {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<string, object>
{
{ "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<string, object>
{
{ "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<string, object>
{
{ "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<IDictionary<string, object>> { dictionary, dictionary2, dictionary3 };

// Act
var customers = Slapper.AutoMapper.Map<Customer>(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<string, object>
// {
// {"Id", id},
// {"Name", name},
// {"Staff_Id", firstEmployeeId},
// {"Staff_LastName", firstEmployeeLastName}
// };

// var secondDictionary = new Dictionary<string, object>
// {
// {"Id", id},
// {"Name", name},
// {"Staff_Id", firstEmployeeId},
// {"Staff_LastName", firstEmployeeLastName}
// };

// // Act
// var department =
// AutoMapper.Map<Department>(new List<IDictionary<string, object>> { 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);
//}
}
}
43 changes: 42 additions & 1 deletion Slapper.AutoMapper.Tests/MapDynamicTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using NUnit.Framework;
Expand All @@ -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<Customer>(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<Customer>(nullDictionary);

// Assert
Assert.IsNull(result);
}

[Test]
public void Returns_Zero_For_Numeric_Value_Types()
{
// Arrange
object nullDictionary = null;

// Act
var result = Slapper.AutoMapper.MapDynamic<int>(nullDictionary);

// Assert
Assert.IsNotNull(result);
Assert.That(result == 0);
}

[Test]
public void Can_Handle_Mapping_A_Single_Dynamic_Object()
{
Expand Down
20 changes: 20 additions & 0 deletions Slapper.AutoMapper.Tests/MappingToGuidTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PersonWithProperties>(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()
{
Expand Down
3 changes: 3 additions & 0 deletions Slapper.AutoMapper.Tests/Slapper.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
<Name>Slapper</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading