From f56f41f65bc846547ca678d2f6a43df00fce86df Mon Sep 17 00:00:00 2001 From: Anthony Sneed Date: Wed, 15 Mar 2017 12:37:06 +0100 Subject: [PATCH] Added failing test: MergeChanges_Should_Merge_Updates_For_Unchanged_Order_With_DB_Added_OrderDetails --- .gitignore | 1 + .../ChangeTrackingExtensionsTests.cs | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/.gitignore b/.gitignore index 953c60dd..a3ed4d6a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ obj /Build/Output /Build/Logs /Source/.vs/config/applicationhost.config +/Samples/VS2013/WebApiSample/.vs/config/applicationhost.config diff --git a/Source/Tests/TrackableEntities.Client.Tests.Extensions/ChangeTrackingExtensionsTests.cs b/Source/Tests/TrackableEntities.Client.Tests.Extensions/ChangeTrackingExtensionsTests.cs index f24cb3e1..76528d1c 100644 --- a/Source/Tests/TrackableEntities.Client.Tests.Extensions/ChangeTrackingExtensionsTests.cs +++ b/Source/Tests/TrackableEntities.Client.Tests.Extensions/ChangeTrackingExtensionsTests.cs @@ -235,6 +235,34 @@ public void MergeChanges_Should_Merge_Updates_For_Unchanged_Order_With_Changed_O Assert.Empty(cachedDeletes); // Cached deletes have been removed } + [Fact] + public void MergeChanges_Should_Merge_Updates_For_Unchanged_Order_With_DB_Added_OrderDetails() + { + // Arrange + var database = new MockNorthwind(); + var origOrder = database.Orders[0]; + var changeTracker = new ChangeTrackingCollection(origOrder); + origOrder.OrderDetails[0].Quantity++; + var product = database.Products.Single(p => p.ProductId == 15); + var dbAddedDetail = new OrderDetail + { + ProductId = 15, + OrderId = 10249, + Quantity = 10, + UnitPrice = 19.6000M, + Product = product + }; + + var changes = changeTracker.GetChanges(); + var updatedOrder = UpdateOrdersWithDetails(database, changes, dbAddedDetail)[0]; + + // Act + changeTracker.MergeChanges(updatedOrder); + + // Assert + Assert.Same(dbAddedDetail, origOrder.OrderDetails.Single(d => d.ProductId == 15)); // Ref equality + } + #endregion #region MergeChanges: Many-to-One @@ -1220,6 +1248,24 @@ private List UpdateOrdersWithDetails(MockNorthwind database, IEnumerable< return updatedOrders; } + private List UpdateOrdersWithDetails(MockNorthwind database, IEnumerable changes, OrderDetail dbAddedDetail = null) + { + var updatedOrders = new List(); + foreach (var origOrder in changes) + { + // Simulate serialization + var updatedOrder = origOrder.Clone(); + + // Simulate db-generated values + updatedOrder.OrderDetails[0].UnitPrice++; + if (dbAddedDetail != null) + updatedOrder.OrderDetails.Add(dbAddedDetail); + updatedOrder.AcceptChanges(); + updatedOrders.Add(updatedOrder); + } + return updatedOrders; + } + private List UpdateEmployeesWithTerritories(IEnumerable changes) { var updatedEmployees = new List();