-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix DiffBuilder not handling added key in Dictionary #35
- Loading branch information
Showing
4 changed files
with
288 additions
and
6 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
src/2.1-JsonMergePatch.Tests/Builder/Diff/Dictionary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System.Collections.Generic; | ||
using Morcatko.AspNetCore.JsonMergePatch.Builders; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace Morcatko.AspNetCore.JsonMergePatch.Tests.Builders.Diff | ||
{ | ||
public class Dictionary | ||
{ | ||
class SimpleModelWithDictionary | ||
{ | ||
public Dictionary<string, string> Dic { get; set; } = new Dictionary<string, string>(); | ||
} | ||
|
||
[Fact] | ||
public void NoChange() | ||
{ | ||
var source = new SimpleModelWithDictionary(); | ||
var patched = new SimpleModelWithDictionary(); | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.Null(diff); | ||
} | ||
|
||
[Fact] | ||
public void EmptyToNull() | ||
{ | ||
var source = new SimpleModelWithDictionary(); | ||
var patched = new SimpleModelWithDictionary() { Dic = null }; | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:null}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void NullToEmpty() | ||
{ | ||
var source = new SimpleModelWithDictionary() { Dic = null }; | ||
var patched = new SimpleModelWithDictionary(); | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{}}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void KeyAdded() | ||
{ | ||
var source = new SimpleModelWithDictionary(); | ||
var patched = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val1" } | ||
} | ||
}; | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{\"key1\": \"val1\"}}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void KeyMissing() | ||
{ | ||
var source = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val1" } | ||
} | ||
}; | ||
var patched = new SimpleModelWithDictionary(); | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{\"key1\": null}}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void ValueSetToNull() | ||
{ | ||
var source = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val1" } | ||
} | ||
}; | ||
var patched = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", null } | ||
} | ||
}; | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{\"key1\": null}}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void ValueChanged() | ||
{ | ||
var source = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val1" } | ||
} | ||
}; | ||
var patched = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val_changed" } | ||
} | ||
}; | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{\"key1\": \"val_changed\"}}"), | ||
diff)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Builders/Diff/Dictionary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System.Collections.Generic; | ||
using Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson.Builders; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace Morcatko.AspNetCore.JsonMergePatch.Tests.NewtonsoftJson.Builders.Diff | ||
{ | ||
public class Dictionary | ||
{ | ||
class SimpleModelWithDictionary | ||
{ | ||
public Dictionary<string, string> Dic { get; set; } = new Dictionary<string, string>(); | ||
} | ||
|
||
[Fact] | ||
public void NoChange() | ||
{ | ||
var source = new SimpleModelWithDictionary(); | ||
var patched = new SimpleModelWithDictionary(); | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.Null(diff); | ||
} | ||
|
||
[Fact] | ||
public void EmptyToNull() | ||
{ | ||
var source = new SimpleModelWithDictionary(); | ||
var patched = new SimpleModelWithDictionary() { Dic = null }; | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:null}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void NullToEmpty() | ||
{ | ||
var source = new SimpleModelWithDictionary() { Dic = null }; | ||
var patched = new SimpleModelWithDictionary(); | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{}}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void KeyAdded() | ||
{ | ||
var source = new SimpleModelWithDictionary(); | ||
var patched = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val1" } | ||
} | ||
}; | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{\"key1\": \"val1\"}}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void KeyMissing() | ||
{ | ||
var source = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val1" } | ||
} | ||
}; | ||
var patched = new SimpleModelWithDictionary(); | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{\"key1\": null}}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void ValueSetToNull() | ||
{ | ||
var source = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val1" } | ||
} | ||
}; | ||
var patched = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", null } | ||
} | ||
}; | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{\"key1\": null}}"), | ||
diff)); | ||
} | ||
|
||
[Fact] | ||
public void ValueChanged() | ||
{ | ||
var source = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val1" } | ||
} | ||
}; | ||
var patched = new SimpleModelWithDictionary() | ||
{ | ||
Dic = new Dictionary<string, string>() | ||
{ | ||
{ "key1", "val_changed" } | ||
} | ||
}; | ||
|
||
var diff = DiffBuilder.Build(source, patched); | ||
|
||
Assert.True(JObject.DeepEquals( | ||
JObject.Parse("{Dic:{\"key1\": \"val_changed\"}}"), | ||
diff)); | ||
} | ||
} | ||
} |