From 4090457756790fa9bcbfe6e2827de9669fd95abc Mon Sep 17 00:00:00 2001 From: mirsking Date: Thu, 1 Sep 2016 16:43:40 +0800 Subject: [PATCH] fix issue 505 in EntityFramework Provider. --- .../Submit/ChangeSetInitializer.cs | 2 +- .../TrippinE2ETestCases.cs | 43 ++++++++++++++++--- .../Models/OrderDetail.cs | 2 + .../Models/TrippinModel.cs | 6 ++- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.Restier.Providers.EntityFramework/Submit/ChangeSetInitializer.cs b/src/Microsoft.Restier.Providers.EntityFramework/Submit/ChangeSetInitializer.cs index ad8f2985..cf8eab86 100644 --- a/src/Microsoft.Restier.Providers.EntityFramework/Submit/ChangeSetInitializer.cs +++ b/src/Microsoft.Restier.Providers.EntityFramework/Submit/ChangeSetInitializer.cs @@ -220,7 +220,7 @@ private void SetValues(DbEntityEntry dbEntry, DataModificationItem item, Type re propertyPair.Key)); } - value = Activator.CreateInstance(type); + value = propertyEntry.CurrentValue; SetValues(value, type, dic); } diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Tests/TrippinE2ETestCases.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Tests/TrippinE2ETestCases.cs index 3f96a6cc..2b617389 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Tests/TrippinE2ETestCases.cs +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Tests/TrippinE2ETestCases.cs @@ -5,9 +5,11 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net; using Microsoft.OData.Client; using Microsoft.OData.Core; using Microsoft.OData.Service.Sample.Trippin.Models; +using Newtonsoft.Json; using Xunit; namespace Microsoft.OData.Service.Sample.Tests @@ -295,11 +297,11 @@ public void CURDComputedImmutableProperty() // Query the updated entity this.TestClientContext.Detach(employee); employee = this.TestClientContext.Orders.Where(e => e.PersonId == personId && e.OrderId == orderId).First(); - + // both computed property and immutable property should not have new value Assert.Equal(400, employee.Price); Assert.NotEqual("ShouldBeIgnored2", employee.ComputedProperty); - + // Immutable property has value set during insert. Assert.NotEqual("ShouldBeIgnored2", employee.ImmutableProperty); Assert.Equal("ShouldNotBeIgnored", employee.ImmutableProperty); @@ -409,9 +411,9 @@ public void UQProperty() Assert.Equal("Cooper", lastName); // Update a property - Dictionary headers = new Dictionary() + Dictionary headers = new Dictionary() { - { "Content-Type", "application/json" } + { "Content-Type", "application/json" } }; HttpWebRequestMessage request = new HttpWebRequestMessage( @@ -566,12 +568,12 @@ public void QueryOptions() // skip people2 = this.TestClientContext.People.Skip((int)(personId - 1)).ToList(); Assert.Equal(personId, people2.First().PersonId); - + // count var countQuery = this.TestClientContext.People.IncludeTotalCount().Skip(1).Take(2) as DataServiceQuery; var response = countQuery.Execute() as QueryOperationResponse; Assert.Equal(response.TotalCount, 14); - + // count with expand countQuery = this.TestClientContext.People.IncludeTotalCount().Expand("Friends").Skip(1).Take(2) as DataServiceQuery; response = countQuery.Execute() as QueryOperationResponse; @@ -638,7 +640,7 @@ public void FilterBuiltInDateFunctions() Assert.True(flight1.All(f => f.StartsAt.Second == startDate.Second)); // Following built-in functions are not supported now. - // fractionalseconds + // fractionalseconds // date // time // totaloffsetminutes @@ -1119,5 +1121,32 @@ public void ConventionBasedChangeSetAuthorizerTest() "The current user does not have permission to delete entities from the EntitySet 'Trips'.", clientException.Message); } + + [Fact] + public void TestPatchSuccessfully() + { + // Get origin content. + var uriStringAfterServiceRoot = "Orders(PersonId=1, OrderId=1)"; + var originContent = default(string); + Action getContent = p => originContent = p; + TestGetPayload(uriStringAfterServiceRoot, getContent); + + // Patch it. + var changedDescription = "TestDescription"; + var changedNormalProperty = "TestNormalProperty"; + string patchContent = + string.Format( + "{{\n \"Description\": \"{0}\",\n \"NormalOrderDetail\": {{\n \"NormalProperty\": \"{1}\"\n }}\n}}", + changedDescription, + changedNormalProperty); + TestPatchStatusCodeIs(uriStringAfterServiceRoot, patchContent, HttpStatusCode.NoContent); + + // Test patch results. + dynamic content = JsonConvert.DeserializeObject(originContent); + content.Description = changedDescription; + content.NormalOrderDetail.NormalProperty = changedNormalProperty; + string changedContent = JsonConvert.SerializeObject(content); + TestGetPayloadContains(uriStringAfterServiceRoot, changedContent); + } } } diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Trippin/Models/OrderDetail.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Trippin/Models/OrderDetail.cs index cbd6b680..507d40cd 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Trippin/Models/OrderDetail.cs +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Trippin/Models/OrderDetail.cs @@ -16,6 +16,8 @@ public OrderDetail() public string NormalProperty { get; set; } + public string AnotherNormalProperty { get; set; } + public string ComputedProperty { get; set; } public string ImmutableProperty { get; set; } diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Trippin/Models/TrippinModel.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Trippin/Models/TrippinModel.cs index 3128f7c7..0b563aaf 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Trippin/Models/TrippinModel.cs +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Trippin/Models/TrippinModel.cs @@ -563,7 +563,11 @@ public static void ResetDataSource() OrderId = 1, Description = "Person 1 Order 1", Price = 200, - NormalOrderDetail = new OrderDetail(), + NormalOrderDetail = new OrderDetail() + { + NormalProperty = "NormalProperty", + AnotherNormalProperty = "AnotherNormalProperty" + }, ComputedOrderDetail = new OrderDetail(), ImmutableOrderDetail = new OrderDetail() },