/
UpdateTests.cs
84 lines (72 loc) · 2.36 KB
/
UpdateTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.Reflection;
using Elasticsearch.Net;
using NUnit.Framework;
using Nest.Tests.MockData.Domain;
using Newtonsoft.Json;
namespace Nest.Tests.Unit.Core.Update
{
[TestFixture]
public class UpdateTests : BaseJsonTests
{
public class PartialElasticsearchProject
{
public string Name { get; set; }
public string Country { get; set; }
}
/// <summary>
/// This POCO models an ElasticsearchProject that allows country to serialize to null explicitly
/// So that we can use it to clear contents in the Update API
/// </summary>
public class PartialElasticsearchProjectWithNull
{
[JsonProperty(NullValueHandling = NullValueHandling.Include)]
public string Country { get; set; }
}
public class UpsertCount
{
public int Count { get; set; }
}
[Test]
public void UpsertUsingScript()
{
var s = new UpdateDescriptor<UpsertCount, UpsertCount>()
.Script("ctx._source.counter += count")
.Params(p => p
.Add("count", 4)
)
.Upsert(new UpsertCount { Count = 1 });
this.JsonEquals(s, MethodBase.GetCurrentMethod());
}
[Test]
public void UpsertUsingScriptAndPartialObject()
{
var s = new UpdateDescriptor<object, object>()
.Script("ctx._source.counter += count")
.Params(p => p
.Add("count", 4)
)
.Upsert(new { count = 4});
this.JsonEquals(s, MethodBase.GetCurrentMethod());
}
[Test]
public void UpdateUsingPartial()
{
var originalProject = new ElasticsearchProject { Id = 1, Name = "NeST", Country = "UK" };
var partialUpdate = new PartialElasticsearchProject { Name = "NEST", Country = "Netherlands" };
var s = new UpdateDescriptor<ElasticsearchProject, PartialElasticsearchProject>()
.IdFrom(originalProject) //only used to infer the id
.Doc(partialUpdate); //the actual partial update statement;
this.JsonEquals(s, MethodBase.GetCurrentMethod());
}
[Test]
public void UpdateUsingPartialWithNull()
{
var originalProject = new ElasticsearchProject { Id = 1, Name = "NEST", Country = "UK" };
var partialUpdate = new PartialElasticsearchProjectWithNull { Country = null };
var s = new UpdateDescriptor<ElasticsearchProject, PartialElasticsearchProjectWithNull>()
.IdFrom(originalProject) //only used to infer the id
.Doc(partialUpdate); //the actual partial update statement;
this.JsonEquals(s, MethodBase.GetCurrentMethod());
}
}
}