Skip to content

Commit 912ab1f

Browse files
committed
added an example to illustrate #1240
1 parent 05ac23d commit 912ab1f

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/Tests/Nest.Tests.Unit/Core/Update/UpdateTests.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
using System.Reflection;
22
using NUnit.Framework;
33
using Nest.Tests.MockData.Domain;
4+
using Newtonsoft.Json;
45

56
namespace Nest.Tests.Unit.Core.Update
67
{
78
[TestFixture]
89
public class UpdateTests : BaseJsonTests
910
{
10-
public class PartialElasticSearchProject
11+
public class PartialElasticsearchProject
1112
{
1213
public string Name { get; set; }
1314
public string Country { get; set; }
1415
}
16+
17+
/// <summary>
18+
/// This POCO models an ElasticsearchProject that allows country to serialize to null explicitly
19+
/// So that we can use it to clear contents in the Update API
20+
/// </summary>
21+
public class PartialElasticsearchProjectWithNull
22+
{
23+
[JsonProperty(NullValueHandling = NullValueHandling.Include)]
24+
public string Country { get; set; }
25+
}
26+
1527
public class UpsertCount
1628
{
1729
public int Count { get; set; }
@@ -26,8 +38,9 @@ public void UpsertUsingScript()
2638
.Add("count", 4)
2739
)
2840
.Upsert(new UpsertCount { Count = 1 });
29-
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
41+
this.JsonEquals(s, MethodBase.GetCurrentMethod());
3042
}
43+
3144
[Test]
3245
public void UpsertUsingScriptAndPartialObject()
3346
{
@@ -38,20 +51,33 @@ public void UpsertUsingScriptAndPartialObject()
3851
)
3952
.Upsert(new { count = 4});
4053

41-
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
54+
this.JsonEquals(s, MethodBase.GetCurrentMethod());
4255
}
4356

4457
[Test]
4558
public void UpdateUsingPartial()
4659
{
4760
var originalProject = new ElasticsearchProject { Id = 1, Name = "NeST", Country = "UK" };
48-
var partialUpdate = new PartialElasticSearchProject { Name = "NEST", Country = "Netherlands" };
61+
var partialUpdate = new PartialElasticsearchProject { Name = "NEST", Country = "Netherlands" };
62+
63+
var s = new UpdateDescriptor<ElasticsearchProject, PartialElasticsearchProject>()
64+
.IdFrom(originalProject) //only used to infer the id
65+
.Doc(partialUpdate); //the actual partial update statement;
66+
67+
this.JsonEquals(s, MethodBase.GetCurrentMethod());
68+
}
69+
70+
[Test]
71+
public void UpdateUsingPartialWithNull()
72+
{
73+
var originalProject = new ElasticsearchProject { Id = 1, Name = "NEST", Country = "UK" };
74+
var partialUpdate = new PartialElasticsearchProjectWithNull { Country = null };
4975

50-
var s = new UpdateDescriptor<ElasticsearchProject, PartialElasticSearchProject>()
76+
var s = new UpdateDescriptor<ElasticsearchProject, PartialElasticsearchProjectWithNull>()
5177
.IdFrom(originalProject) //only used to infer the id
5278
.Doc(partialUpdate); //the actual partial update statement;
5379

54-
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
80+
this.JsonEquals(s, MethodBase.GetCurrentMethod());
5581
}
5682

5783
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"doc": {
3+
"country": null
4+
}
5+
}

src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,9 @@
674674
<None Include="Core\UpdateSettings\CanSpecifyNewAnalysisSettings.json">
675675
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
676676
</None>
677+
<None Include="Core\Update\UpdateUsingPartialWithNull.json">
678+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
679+
</None>
677680
<None Include="Core\Update\UpsertUsingScriptAndPartialObject.json">
678681
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
679682
</None>

0 commit comments

Comments
 (0)