public void Should_Map_Different_Sets_Correctly() { // Arrange var partialInfo = new Dictionary <string, object> { { "CustomerId", 1 }, { "FirstName", "Bob" }, { "LastName", "Smith" }, { "Orders_OrderId", 1 } }; var fullInfo = new Dictionary <string, object> { { "CustomerId", 1 }, { "FirstName", "Bob" }, { "LastName", "Smith" }, { "Orders_OrderId", 1 }, { "Orders_OrderTotal", 50.50m }, { "Orders_OrderDate", DateTime.Now } }; var mappy = new Mappy(); // Act var customerPartial = mappy.Map <Customer>(partialInfo); var customerFull = mappy.Map <Customer>(fullInfo); Assert.NotNull(customerPartial); Assert.Equal(partialInfo["CustomerId"], customerPartial.CustomerId); Assert.Equal(partialInfo["FirstName"], customerPartial.FirstName); Assert.Equal(partialInfo["LastName"], customerPartial.LastName); Assert.Single(customerPartial.Orders); var order = customerPartial.Orders.Single(); Assert.Equal(partialInfo["Orders_OrderId"], order.OrderId); Assert.Equal(default(decimal), order.OrderTotal); Assert.Null(order.OrderDate); Assert.Equal(fullInfo["CustomerId"], customerFull.CustomerId); Assert.Equal(fullInfo["FirstName"], customerFull.FirstName); Assert.Equal(fullInfo["LastName"], customerFull.LastName); Assert.Single(customerFull.Orders); order = customerFull.Orders.Single(); Assert.Equal(fullInfo["Orders_OrderId"], order.OrderId); Assert.Equal(fullInfo["Orders_OrderTotal"], order.OrderTotal); Assert.Equal(fullInfo["Orders_OrderDate"], order.OrderDate); }
public void Avoids_Hash_Collisions() { // Arrange var id2 = typeof(Employee).GetHashCode() - typeof(Contract).GetHashCode(); var source = new List <object>(); dynamic obj1 = new ExpandoObject(); obj1.Id = 1; obj1.Contracts_Id = id2; source.Add(obj1); dynamic obj2 = new ExpandoObject(); obj2.Id = 1; obj2.Contracts_Id = id2 + 1; source.Add(obj2); var mappy = new Mappy(); // Act/Assert var result = mappy.Map <Employee>(source).First(); }
public void Can_Map_Matching_Property_Names() { // Arrange const int id = 1; const string firstName = "Bob"; const string lastName = "Smith"; var dictionary = new Dictionary <string, object> { { "Id", id }, { "FirstName", firstName }, { "LastName", lastName } }; var mappy = new Mappy(); // Act var customer = mappy.Map <PersonWithProperties>(dictionary); // Assert Assert.NotNull(customer); Assert.Equal(customer.Id, id); Assert.Equal(customer.FirstName, firstName); Assert.Equal(customer.LastName, lastName); }
public void Can_Map_DateTime_Values_To_Nullable_DateTime_Properties() { // Arrange const int id = 1; const string firstName = "Bob"; const string lastName = "Smith"; var startDate = DateTime.Now.AddDays(-2); var endDate = DateTime.Now; // This is what we are testing dynamic dynamicPerson = new ExpandoObject(); dynamicPerson.Id = id; dynamicPerson.FirstName = firstName; dynamicPerson.LastName = lastName; dynamicPerson.StartDate = startDate; dynamicPerson.EndDate = endDate; var mappy = new Mappy(); // Act var customer = mappy.Map <PersonWithProperties>(dynamicPerson); // Assert Assert.NotNull(customer); Assert.Equal(customer.Id, id); Assert.Equal(customer.FirstName, firstName); Assert.Equal(customer.LastName, lastName); Assert.Equal(customer.StartDate, startDate); Assert.Equal(customer.EndDate, endDate); // This is what we are testing }
public void Can_Map_Null_Values() { // Arrange const int id = 1; const string firstName = null; const string lastName = "Smith"; var dictionary = new Dictionary <string, object> { { "Id", id }, { "FirstName", null }, { "LastName", lastName } }; var mappy = new Mappy(); // Act var customer = mappy.Map <PersonWithFields>(dictionary); // Assert Assert.NotNull(customer); Assert.Equal(customer.Id, id); Assert.Equal(customer.FirstName, firstName); Assert.Equal(customer.LastName, lastName); }
public void Can_Map_To_Array() { // Arrange var dictionary = new Dictionary <string, object> { { "CustomerId", 1 }, { "FirstName", "Bob" }, { "LastName", "Smith" }, { "Orders_OrderId", 1 }, { "Orders_OrderTotal", 50.50m } }; var mappy = new Mappy(); // Act var customer = mappy.Map <CustomerArray>(dictionary); // Assert Assert.Equal(dictionary["CustomerId"], customer.CustomerId); Assert.Equal(dictionary["FirstName"], customer.FirstName); Assert.Equal(dictionary["LastName"], customer.LastName); Assert.Single(customer.Orders); Assert.Equal(dictionary["Orders_OrderId"], customer.Orders.Single().OrderId); Assert.Equal(dictionary["Orders_OrderTotal"], customer.Orders.Single().OrderTotal); }
public void Can_Map_DifferentsRows_to_Same_object() { dynamic dynamicCustomer = new ExpandoObject(); dynamicCustomer.Id = 1; dynamicCustomer.Name = "Clark"; dynamicCustomer.Phones_Id = 1; dynamicCustomer.Phones_Name = "88888"; dynamicCustomer.Emails_Id = "1"; dynamicCustomer.Emails_Name = "*****@*****.**"; dynamic dynamicCustomer2 = new ExpandoObject(); dynamicCustomer2.Id = 1; dynamicCustomer2.Name = "Clark"; dynamicCustomer2.Phones_Id = 2; dynamicCustomer2.Phones_Name = "99999"; dynamicCustomer2.Emails_Id = "2"; dynamicCustomer2.Emails_Name = "*****@*****.**"; var list = new List <dynamic> { dynamicCustomer, dynamicCustomer2 }; var mappy = new Mappy(); var customer = mappy.Map <NameValue>(list) .FirstOrDefault(); Assert.NotNull(customer); Assert.NotEqual( customer.Emails.FirstOrDefault(e => e.Id == 1).Name, customer.Phones.FirstOrDefault(p => p.Id == 1).Name); }
public void Can_Map_Matching_Property_Names_With_Different_Types() { // Arrange const int id = 1; const string firstName = "Bob"; const string lastName = "Smith"; var dictionary = new Dictionary <string, object> { { "Id", Double.Parse("1.245698", CultureInfo.InvariantCulture) }, { "FirstName", firstName }, { "LastName", lastName } }; var mappy = new Mappy(); // Act var customer = mappy.Map <PersonWithProperties>(dictionary); // Assert Assert.NotNull(customer); Assert.Equal(customer.Id, id); Assert.Equal(customer.FirstName, firstName); Assert.Equal(customer.LastName, lastName); }
public void Can_Map_And_Ignore_Keys_Case() { // Arrange var dictionary = new Dictionary <string, object> { { "CUSTOMERID", 1 }, { "FIRSTNAME", "Bob" }, { "LASTNAME", "Smith" }, { "ORDERS_ORDERID", 1 }, { "ORDERS_ORDERTOTAL", 50.50m } }; var options = new MappyOptions( stringComparison: StringComparison.OrdinalIgnoreCase, useDefaultDictionaryComparer: false); var mappy = new Mappy(options); // Act var customer = mappy.Map <Customer>(dictionary); // Assert Assert.Equal(dictionary["CUSTOMERID"], customer.CustomerId); Assert.Equal(dictionary["FIRSTNAME"], customer.FirstName); Assert.Equal(dictionary["LASTNAME"], customer.LastName); Assert.Single(customer.Orders); Assert.Equal(dictionary["ORDERS_ORDERID"], customer.Orders.Single().OrderId); Assert.Equal(dictionary["ORDERS_ORDERTOTAL"], customer.Orders.Single().OrderTotal); }
public void Can_Map_Guid_String_Values_To_Guid_Fields() { // Arrange const int id = 1; const string firstName = "Bob"; const string lastName = "Smith"; var uniqueId = Guid.NewGuid(); dynamic dynamicPerson = new ExpandoObject(); dynamicPerson.Id = id; dynamicPerson.FirstName = firstName; dynamicPerson.LastName = lastName; dynamicPerson.UniqueId = uniqueId.ToString(); // This is what we are testing var mappy = new Mappy(); // Act var customer = mappy.Map <PersonWithFields>(dynamicPerson); // Assert Assert.NotNull(customer); Assert.Equal(id, customer.Id); Assert.Equal(firstName, customer.FirstName); Assert.Equal(lastName, customer.LastName); Assert.Equal(uniqueId, customer.UniqueId); // This is what we are testing }
public void Can_Map_DateTime_String_Values_To_Nullable_DateTime_Fields() { // Arrange const int id = 1; const string firstName = "Bob"; const string lastName = "Smith"; DateTime startDate = DateTime.Now.AddDays(-2); DateTime? endDate = DateTime.Now; dynamic dynamicPerson = new ExpandoObject(); dynamicPerson.Id = id; dynamicPerson.FirstName = firstName; dynamicPerson.LastName = lastName; dynamicPerson.StartDate = startDate.ToString(); dynamicPerson.EndDate = endDate.ToString(); var mappy = new Mappy(); // Act var customer = mappy.Map <PersonWithFields>(dynamicPerson); // Assert Assert.NotNull(customer); Assert.Equal(customer.Id, id); Assert.Equal(customer.FirstName, firstName); Assert.Equal(customer.LastName, lastName); Assert.Equal(customer.StartDate.ToString(), startDate.ToString()); Assert.Equal(customer.EndDate.ToString(), endDate.ToString()); }
public void Should_Map_Simple_Abbreviations_Correct() { var dictionary = new Dictionary <string, object> { { "Id", 1 }, { "FullName", "Bob Smith" }, { "Ph_Id", 1 }, { "Ph_PhoneNumber", "1234" } }; var data = new List <Dictionary <string, object> > { dictionary }; var mappy = new Mappy(); var customers = mappy.Map <Customer>(data, new Dictionary <string, string> { { "Phone_", "Ph_" } }); Assert.Single(customers); var customer = customers.Single(); Assert.Equal(dictionary["Id"], customer.Id); Assert.Equal(dictionary["FullName"], customer.FullName); Assert.NotNull(customer.Phone); Assert.Equal(dictionary["Ph_Id"], customer.Phone.Id); Assert.Equal(dictionary["Ph_PhoneNumber"], customer.Phone.PhoneNumber); }
public void Can_Map_Enum_Values_To_Enum_Properties() { // Arrange const int id = 1; const string firstName = "Jimbo"; const string lastName = "Smith"; const Gender gender = Gender.Male; var dictionary = new Dictionary <string, object> { { "Id", id }, { "FirstName", firstName }, { "LastName", lastName }, { "Gender", gender } }; var mappy = new Mappy(); // Act var customer = mappy.Map <PersonWithProperties>(dictionary); // Assert Assert.NotNull(customer); Assert.Equal(id, customer.Id); Assert.Equal(firstName, customer.FirstName); Assert.Equal(lastName, customer.LastName); Assert.Equal(gender, customer.Gender); }
public void Can_Map_To_Multiple_Objects() { // Arrange var dictionary = new Dictionary <string, object> { { "Id", 1 }, { "FirstName", "Bob" }, { "LastName", "Smith" }, { "Orders_Id", 1 }, { "Orders_OrderTotal", 50.50m } }; var dictionary2 = new Dictionary <string, object> { { "Id", 2 }, { "FirstName", "Jane" }, { "LastName", "Doe" }, { "Orders_Id", 2 }, { "Orders_OrderTotal", 23.40m } }; var listOfDictionaries = new List <Dictionary <string, object> > { dictionary, dictionary2 }; var mappy = new Mappy(); // Act var customers = mappy.Map <MapTestModels.CustomerWithAnIEnumerableOrdersCollection>(listOfDictionaries); // Assert Assert.Equal(2, customers.Count()); Assert.Equal("Bob", customers.ToList()[0].FirstName); Assert.Equal("Jane", customers.ToList()[1].FirstName); }
public void I_Can_Map_Value_SpecialStringTyped_Collection() { // Arrange var dictionary = new Dictionary <string, object> { { "CustomerNamesId", 1 }, { "Names_$", "Name 1" }, }; var dictionary2 = new Dictionary <string, object> { { "CustomerNamesId", 1 }, { "Names_$", "Name 2" } }; var list = new List <IDictionary <string, object> > { dictionary, dictionary2 }; var mappy = new Mappy(); // Act var customers = mappy.Map <CustomerNames>(list).ToList(); // Assert // There should only be a single customer Assert.Single(customers); // There should be two values in OrdersIds, with the correct values Assert.Equal(2, customers.Single().Names.Count); Assert.Equal("Name 1", customers.Single().Names[0]); Assert.Equal("Name 2", customers.Single().Names[1]); }
public void Can_Map_Null_Values_To_Nullable_Enum_Properties() { // Arrange const int id = 1; const string firstName = "Jimbo"; const string lastName = "Smith"; const Gender gender = Gender.Male; MaritalStatus?maritalStatus = null; dynamic person = new ExpandoObject(); person.Id = id; person.FirstName = firstName; person.LastName = lastName; person.Gender = gender; person.MaritalStatus = maritalStatus; var mappy = new Mappy(); // Act var customer = mappy.Map <PersonWithProperties>(person); // Assert Assert.NotNull(customer); Assert.Equal(id, customer.Id); Assert.Equal(firstName, customer.FirstName); Assert.Equal(lastName, customer.LastName); Assert.Equal(gender, customer.Gender); Assert.Equal(maritalStatus, customer.MaritalStatus); }
public void I_Can_Map_Value_PrimitiveTyped_Collection() { // Arrange var dictionary = new Dictionary <string, object> { { "CustomerId", 1 }, { "OrdersIds_$", 3 }, }; var dictionary2 = new Dictionary <string, object> { { "CustomerId", 1 }, { "OrdersIds_$", 5 } }; var list = new List <IDictionary <string, object> > { dictionary, dictionary2 }; var mappy = new Mappy(); // Act var customers = mappy.Map <Customer>(list) .ToList(); // Assert // There should only be a single customer Assert.Single(customers); // There should be two values in OrdersIds, with the correct values Assert.Equal(2, customers.Single().OrdersIds.Count); Assert.Equal(3, customers.Single().OrdersIds[0]); Assert.Equal(5, customers.Single().OrdersIds[1]); }
public void Can_Map_Array_Values_To_Arrays() { // Arrange const int id = 1; const string firstName = null; const string lastName = "Smith"; string[] favoriteFoods = new[] { "Ice Cream", "Jello" }; var dictionary = new Dictionary <string, object> { { "Id", id }, { "FirstName", null }, { "LastName", lastName }, { "FavoriteFoods", favoriteFoods } }; var mappy = new Mappy(); // Act var customer = mappy.Map <PersonWithFields>(dictionary); // Assert Assert.NotNull(customer); Assert.Equal(customer.Id, id); Assert.Equal(customer.FirstName, firstName); Assert.Equal(customer.LastName, lastName); Assert.Equal(customer.FavoriteFoods, favoriteFoods); }
public void Should_Map_Not_Exists_Primitive_Array_Values_To_Null_Array() { // Arrange var dictionary = new Dictionary <string, object> { { "Id", 1 }, { "FirstName", "Bob" }, { "LastName", "Smith" } }; var dictionary2 = new Dictionary <string, object> { { "Id", 1 }, { "FirstName", "Bob" }, { "LastName", "Smith" } }; var mappy = new Mappy(); var users = mappy.Map <UserWithPhones>( new List <Dictionary <string, object> > { dictionary, dictionary2 }) .ToList(); Assert.Single(users); var user = users.Single(); Assert.Equal(dictionary["Id"], user.Id); Assert.Equal(dictionary["FirstName"], user.FirstName); Assert.Equal(dictionary["LastName"], user.LastName); Assert.Null(user.Phones); }
public void Can_Map_Complex_Nested_Members() { // Arrange const int id = 1; const string firstName = "Bob"; const string lastName = "Smith"; const int orderId = 1; const decimal orderTotal = 50.50m; var dictionary = new Dictionary <string, object> { { "Id", id }, { "FirstName", firstName }, { "LastName", lastName }, { "Orders_Id", orderId }, { "Orders_OrderTotal", orderTotal } }; var mappy = new Mappy(); // Act var customer = mappy.Map <MapTestModels.CustomerWithOrdersList>(dictionary); // Assert Assert.NotNull(customer); Assert.Equal(customer.Id, id); Assert.Equal(customer.FirstName, firstName); Assert.Equal(customer.LastName, lastName); Assert.NotNull(customer.Orders); Assert.Single(customer.Orders); Assert.Equal(customer.Orders[0].Id, orderId); Assert.Equal(customer.Orders[0].OrderTotal, orderTotal); }
public void Should_Map_Null_Identifier_Correctly() { var dictionary = new Dictionary <string, object> { { "CustomerId", 1 }, { "FirstName", "Bob" }, { "LastName", "Smith" }, { "Orders_OrderId", new int?() }, { "Orders_OrderTotal", 50.50m }, { "Orders_OrderDate", DateTime.Now } }; var mappy = new Mappy(); var customer = mappy.Map <Customer>(dictionary); Assert.Equal(dictionary["CustomerId"], customer.CustomerId); Assert.Equal(dictionary["FirstName"], customer.FirstName); Assert.Single(customer.Orders); var order = customer.Orders.Single(); Assert.Equal(0, order.OrderId); Assert.Equal(dictionary["Orders_OrderTotal"], order.OrderTotal); Assert.Equal(dictionary["Orders_OrderDate"], order.OrderDate); }
public void Can_Handle_Mapping_Deeply_Nested_Members() { // Arrange var dictionary = new Dictionary <string, object> { { "Id", 1 }, { "FirstName", "Bob" }, { "LastName", "Smith" }, { "Orders_Id", 1 }, { "Orders_OrderTotal", 50.50m }, { "Orders_OrderDetails_Id", 1 }, { "Orders_OrderDetails_OrderDetailTotal", 50.50m }, { "Orders_OrderDetails_Product_Id", 546 }, { "Orders_OrderDetails_Product_ProductName", "Black Bookshelf" } }; var mappy = new Mappy(); // Act var customer = mappy.Map <MapTestModels.CustomerWithAnIEnumerableOrdersCollection>(dictionary); // Assert Assert.Single(customer.Orders); Assert.Single(customer.Orders.First().OrderDetails); Assert.Equal("Black Bookshelf", customer.Orders.First().OrderDetails[0].Product.ProductName); }
public void Can_Handle_Mapping_Nested_Members_Using_Dynamic() { // Arrange var dynamicCustomers = new List <object>(); for (var i = 0; i < 5; i++) { dynamic customer = new ExpandoObject(); customer.Id = i; customer.FirstName = "FirstName" + i; customer.LastName = "LastName" + i; customer.Orders_Id = i; customer.Orders_OrderTotal = i + 0m; dynamicCustomers.Add(customer); } var mappy = new Mappy(); // Act var customers = mappy.Map <Customer>(dynamicCustomers) .ToList(); // Assert Assert.Equal(5, customers.Count()); Assert.Equal(1, customers.First().Orders.Count); }
public void Will_Throw_An_Exception_If_The_Type_Is_Not_Dynamic() { // Arrange var someObject = new object(); var mappy = new Mappy(); // Assert Assert.Throws <ArgumentException>(() => mappy.Map <Person>(someObject as IDictionary <string, object>)); }
public void Will_Return_An_Empty_List_Of_The_Requested_Type_When_Passed_An_Empty_List() { var mappy = new Mappy(); // Act var list = mappy.Map <Person>(new List <object>()); // Assert Assert.NotNull(list); Assert.Empty(list); }
public void Will_Not_Throw_An_Exception_If_The_List_Items_Are_Not_Dynamic() { // Arrange var someObjectList = new List <object> { null }; var mappy = new Mappy(); // Act mappy.Map <Person>(someObjectList); }
public void Can_Handle_Mapping_Objects_With_Multiple_Identifiers() { // Arrange const int customerId = 1; const string customerType = "Commercial"; const string firstName = "Bob"; const string lastName = "Smith"; const int orderId = 1; const decimal orderTotal = 50.50m; var dictionary = new Dictionary <string, object> { { "Customer_Id", customerId }, { "Customer_Type", customerType }, { "FirstName", firstName }, { "LastName", lastName }, { "Orders_Id", orderId }, { "Orders_OrderTotal", orderTotal } }; var dictionary2 = new Dictionary <string, object> { { "Customer_Id", customerId }, { "Customer_Type", customerType }, { "FirstName", firstName }, { "LastName", lastName }, { "Orders_Id", orderId + 1 }, { "Orders_OrderTotal", orderTotal + 1 } }; var dictionary3 = new Dictionary <string, object> { { "Customer_Id", customerId + 1 }, { "Customer_Type", customerType }, { "FirstName", firstName }, { "LastName", lastName }, { "Orders_Id", orderId + 1 }, { "Orders_OrderTotal", orderTotal + 1 } }; var listOfDictionaries = new List <Dictionary <string, object> > { dictionary, dictionary2, dictionary3 }; var mappy = new Mappy(); // Act var customers = mappy.Map <MapTestModels.CustomerWithMultipleIdAttributes>(listOfDictionaries); // Assert Assert.Equal(2, customers.Count()); Assert.Equal(2, customers.First().Orders.Count); Assert.Equal(customers.ToList()[1].Orders[0].Id, orderId + 1); }
public void Should_not_rewrite_default_values_for_non_set_values() { var dict = new Dictionary <string, object> { { "Id", 1 } }; var mappy = new Mappy(); var result = mappy.Map <DefaultValueTest>(dict); Assert.Equal(dict["Id"], result.Id); Assert.Equal(new DefaultValueTest().DefaultValue, result.DefaultValue); }
public void Can_Detect_Duplicate_Parent_Members_And_Properly_Instantiate_The_Object_Only_Once() { // Arrange const int id = 1; const string firstName = "Bob"; const string lastName = "Smith"; const int orderId = 1; const decimal orderTotal = 50.50m; var dictionary = new Dictionary <string, object> { { "Id", id }, { "FirstName", firstName }, { "LastName", lastName }, { "Orders_Id", orderId }, { "Orders_OrderTotal", orderTotal } }; var dictionary2 = new Dictionary <string, object> { { "Id", id }, { "FirstName", firstName }, { "LastName", lastName }, { "Orders_Id", orderId + 1 }, { "Orders_OrderTotal", orderTotal + 1 } }; var listOfDictionaries = new List <Dictionary <string, object> > { dictionary, dictionary2 }; var mappy = new Mappy(); // Act var customers = mappy.Map <MapTestModels.CustomerWithOrdersList>(listOfDictionaries); var customer = customers.FirstOrDefault(); // Assert Assert.Single(customers); Assert.NotNull(customer); Assert.Equal(customer.Id, id); Assert.Equal(customer.FirstName, firstName); Assert.Equal(customer.LastName, lastName); Assert.NotNull(customer.Orders); Assert.Equal(2, customer.Orders.Count); Assert.Equal(customer.Orders[0].Id, orderId); Assert.Equal(customer.Orders[0].OrderTotal, orderTotal); Assert.Equal(customer.Orders[1].Id, orderId + 1); Assert.Equal(customer.Orders[1].OrderTotal, orderTotal + 1); }
public void Set_Property_Should_Be_Ignored() { var dictionary = new Dictionary <string, object> { { "FirstName", "Bob" }, { "LastName", "Smith" } }; var mappy = new Mappy(); // Act var user = mappy.Map <User>(dictionary); Assert.Equal(dictionary["FirstName"], user.FirstName); Assert.Equal(dictionary["LastName"], user.LastName); }