public void GenerateAndApplyReversePatch() { var initial = new PhoneBook() { Entries = new Dictionary <string, TestPerson>() { ["0800JOHNDOE"] = new TestPerson() { FirstName = "John", LastName = "Doe", IsAdult = true, }, ["0800JANEDOE"] = new TestPerson() { FirstName = "Jane", LastName = "Doe", IsAdult = true, }, }, }; using DraftScope scope = (DraftScope)DraftExtensions.CreateDraft(initial, out PhoneBook draft); var patchGenerator = new DictionaryPatchGenerator(); var patches = new JsonPatchDocument(); var inversePatches = new JsonPatchDocument(); draft.Entries.Remove("0800JANEDOE"); draft.Entries.Add("0800BABYDOE", new TestPerson() { FirstName = "Baby", LastName = "Doe", }); // trick the scope into thinking that is finishing and should not create proxies anymore. scope.IsFinishing = true; patchGenerator.Generate((IDraft)draft.Entries, "/Entries", patches, inversePatches); // inverse order of inverse patches. inversePatches.Operations.Reverse(); var final = scope.FinishDraft <PhoneBook, IPhoneBook>(draft); var result = IPhoneBook.Produce(initial, p => { patches.ApplyTo(p); }); result = result.Produce(p => { inversePatches.ApplyTo(p); }); Assert.Equal(2, result.Entries.Count); Assert.Same(initial.Entries["0800JOHNDOE"], result.Entries["0800JOHNDOE"]); Assert.Same(initial.Entries["0800JANEDOE"], result.Entries["0800JANEDOE"]); }
public void ApplyInverseComplexCollectionPatch() { var initial = new TestPerson() { FirstName = "John", LastName = "Doe", IsAdult = true, Cars = new List <Car>() { new Car { Make = "Ferrari", Model = "250 LM", }, new Car { Make = "Shelby", Model = "Daytona Cobra Coupe", }, new Car() { Make = "Rolls Royce", Model = "10 HP", }, new Car() { Make = "Mercedes-Benz", Model = "38/250 SSK", }, }, }; var patches = new JsonPatchDocument(); var inversePatches = new JsonPatchDocument(); ITestPerson testPerson; using (DraftScope scope = (DraftScope)DraftExtensions.CreateDraft(initial, out TestPerson draft)) { var patchGenerator = new CollectionPatchGenerator(new DynamicLargestCommonSubsequence()); draft.Cars.RemoveAt(3); draft.Cars.RemoveAt(0); draft.Cars.Add(new Car() { Make = "Bugatti", Model = "Type 57 SC Atalante", }); // trick the scope into thinking that is finishing and should not create proxies anymore. scope.IsFinishing = true; patchGenerator.Generate((IDraft)draft.Cars, "/Cars", patches, inversePatches); // inverse order of inverse patches. inversePatches.Operations.Reverse(); testPerson = scope.FinishDraft <ITestPerson, TestPerson>(draft); } var result = ITestPerson.Produce(initial, p => { patches.ApplyTo(p); }); result = result.Produce(p => { inversePatches.ApplyTo(p); }); Assert.Equal(4, result.Cars.Count); Assert.Equal("Ferrari", result.Cars[0].Make); Assert.Equal("250 LM", result.Cars[0].Model); Assert.Equal("Shelby", result.Cars[1].Make); Assert.Equal("Daytona Cobra Coupe", result.Cars[1].Model); Assert.Equal("Rolls Royce", result.Cars[2].Make); Assert.Equal("10 HP", result.Cars[2].Model); Assert.Equal("Mercedes-Benz", result.Cars[3].Make); Assert.Equal("38/250 SSK", result.Cars[3].Model); }