Skip to content

Commit 91627e9

Browse files
committed
Add more, and DRYout, repo tests
1 parent dd8478b commit 91627e9

File tree

4 files changed

+117
-28
lines changed

4 files changed

+117
-28
lines changed

IntegrationEngine.Core.Tests/IntegrationEngine.Core.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="StubDeleteRequest.cs" />
6767
<Compile Include="StubPingResponse.cs" />
6868
<Compile Include="StubElasticsearchResponse.cs" />
69+
<Compile Include="StubExistsResponse.cs" />
6970
</ItemGroup>
7071
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
7172
<ItemGroup>

IntegrationEngine.Core.Tests/Storage/ElasticsearchRepositoryTest.cs

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,20 @@ public void ShouldReturnNullIfDocumentIsNotFoundById()
6262
ElasticClient.Verify(x => x.Get(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()), Times.Once);
6363
}
6464

65-
[Test]
66-
public void ShouldReturnSingleDocumentGivenAnId()
65+
void SetupForGetDocument(string id)
6766
{
6867
var getResponse = new Mock<StubGetResponse<CronTrigger>>();
69-
var expectedId = "1";
70-
getResponse.SetupGet(x => x.Id).Returns(() => expectedId);
68+
getResponse.SetupGet(x => x.Id).Returns(() => id);
7169
getResponse.SetupGet(x => x.Source).Returns(() => new CronTrigger());
7270
ElasticClient.Setup(x => x.Get(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()))
7371
.Returns(getResponse.Object);
72+
}
73+
74+
[Test]
75+
public void ShouldReturnSingleDocumentGivenAnId()
76+
{
77+
var expectedId = "1";
78+
SetupForGetDocument(expectedId);
7479

7580
var actual = Subject.SelectById<CronTrigger>(expectedId);
7681

@@ -81,14 +86,9 @@ public void ShouldReturnSingleDocumentGivenAnId()
8186
[Test]
8287
public void ShouldInsertAndReturnDocument()
8388
{
84-
var expected = new CronTrigger() {
85-
Id = "1",
86-
};
87-
var getResponse = new Mock<StubGetResponse<CronTrigger>>();
88-
getResponse.SetupGet(x => x.Id).Returns(() => expected.Id);
89-
getResponse.SetupGet(x => x.Source).Returns(() => new CronTrigger());
90-
ElasticClient.Setup(x => x.Get<CronTrigger>(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()))
91-
.Returns(getResponse.Object);
89+
var expectedId = "1";
90+
SetupForGetDocument(expectedId);
91+
var expected = new CronTrigger() { Id = expectedId };
9292
var indexResponse = new Mock<StubIndexResponse>();
9393
indexResponse.SetupGet(x => x.Id).Returns(() => expected.Id);
9494
ElasticClient.Setup(x => x.Index(expected, It.IsAny<Func<IndexDescriptor<CronTrigger>, IndexDescriptor<CronTrigger>>>()))
@@ -103,14 +103,9 @@ public void ShouldInsertAndReturnDocument()
103103
[Test]
104104
public void ShouldUpdateAndReturnDocument()
105105
{
106-
var expected = new CronTrigger() {
107-
Id = "1",
108-
};
109-
var getResponse = new Mock<StubGetResponse<CronTrigger>>();
110-
getResponse.SetupGet(x => x.Id).Returns(() => expected.Id);
111-
getResponse.SetupGet(x => x.Source).Returns(() => new CronTrigger());
112-
ElasticClient.Setup(x => x.Get<CronTrigger>(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()))
113-
.Returns(getResponse.Object);
106+
var expectedId = "1";
107+
SetupForGetDocument(expectedId);
108+
var expected = new CronTrigger() { Id = expectedId };
114109
var updateResponse = new Mock<StubUpdateResponse>();
115110
updateResponse.SetupGet(x => x.Id).Returns(() => expected.Id);
116111
ElasticClient.Setup(x => x.Update<CronTrigger>(It.IsAny<Func<UpdateDescriptor<CronTrigger, CronTrigger>, UpdateDescriptor<CronTrigger, CronTrigger>>>()))
@@ -135,14 +130,47 @@ public void ShouldDeleteDocument()
135130
ElasticClient.Verify(x => x.Delete(It.IsAny<Func<DeleteDescriptor<CronTrigger>, DeleteDescriptor<CronTrigger>>>()), Times.Once);
136131
}
137132

133+
void SetupForDocumentExists(bool exists)
134+
{
135+
var existsResponse = new Mock<StubExistsResponse>();
136+
existsResponse.SetupGet(x => x.Exists).Returns(exists);
137+
ElasticClient.Setup(x => x.DocumentExists(It.IsAny<Func<DocumentExistsDescriptor<CronTrigger>, DocumentExistsDescriptor<CronTrigger>>>()))
138+
.Returns(existsResponse.Object);
139+
}
140+
138141
[Test]
139-
public void ShouldShouldReturnTrueIfServerIsAvailable()
142+
public void ShouldReturnTrueIfDocumentExists()
143+
{
144+
SetupForDocumentExists(true);
145+
146+
var actual = Subject.Exists<CronTrigger>("does not exist");
147+
148+
Assert.That(actual, Is.True);
149+
}
150+
151+
[Test]
152+
public void ShouldReturnFalseIfDocumentDoesNotExist()
153+
{
154+
SetupForDocumentExists(false);
155+
156+
var actual = Subject.Exists<CronTrigger>("does not exist");
157+
158+
Assert.That(actual, Is.False);
159+
}
160+
161+
void SetupForIsServerAvailable(bool success)
140162
{
141163
var elasticsearchResponse = new Mock<StubElasticsearchResponse>();
142-
elasticsearchResponse.SetupGet(x => x.Success).Returns(() => true);
164+
elasticsearchResponse.SetupGet(x => x.Success).Returns(() => success);
143165
var pingResponse = new Mock<StubPingResponse>();
144166
pingResponse.SetupGet(x => x.ConnectionStatus).Returns(() => elasticsearchResponse.Object);
145167
ElasticClient.Setup(x => x.Ping(It.IsAny<PingRequest>())).Returns(pingResponse.Object);
168+
}
169+
170+
[Test]
171+
public void ShouldShouldReturnTrueIfServerIsAvailable()
172+
{
173+
SetupForIsServerAvailable(true);
146174

147175
var actual = Subject.IsServerAvailable();
148176

@@ -152,11 +180,7 @@ public void ShouldShouldReturnTrueIfServerIsAvailable()
152180
[Test]
153181
public void ShouldShouldReturnFalseIfServerIsUnavailable()
154182
{
155-
var elasticsearchResponse = new Mock<StubElasticsearchResponse>();
156-
elasticsearchResponse.SetupGet(x => x.Success).Returns(() => false);
157-
var pingResponse = new Mock<StubPingResponse>();
158-
pingResponse.SetupGet(x => x.ConnectionStatus).Returns(() => elasticsearchResponse.Object);
159-
ElasticClient.Setup(x => x.Ping(It.IsAny<PingRequest>())).Returns(pingResponse.Object);
183+
SetupForIsServerAvailable(false);
160184

161185
var actual = Subject.IsServerAvailable();
162186

IntegrationEngine.Core.Tests/StubElasticClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ public System.Threading.Tasks.Task<IEmptyResponse> ClearScrollAsync(IClearScroll
13771377
throw new NotImplementedException();
13781378
}
13791379

1380-
public IExistsResponse DocumentExists<T>(Func<DocumentExistsDescriptor<T>, DocumentExistsDescriptor<T>> existsSelector) where T : class
1380+
public virtual IExistsResponse DocumentExists<T>(Func<DocumentExistsDescriptor<T>, DocumentExistsDescriptor<T>> existsSelector) where T : class
13811381
{
13821382
throw new NotImplementedException();
13831383
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Nest;
2+
using System;
3+
4+
namespace IntegrationEngine.Core.Tests
5+
{
6+
public class StubExistsResponse : IExistsResponse
7+
{
8+
public StubExistsResponse()
9+
{
10+
}
11+
12+
#region IExistsResponse implementation
13+
14+
public virtual bool Exists {
15+
get {
16+
throw new NotImplementedException();
17+
}
18+
}
19+
20+
#endregion
21+
22+
#region IResponse implementation
23+
24+
public bool IsValid {
25+
get {
26+
throw new NotImplementedException();
27+
}
28+
}
29+
30+
public Elasticsearch.Net.IElasticsearchResponse ConnectionStatus {
31+
get {
32+
throw new NotImplementedException();
33+
}
34+
}
35+
36+
public ElasticInferrer Infer {
37+
get {
38+
throw new NotImplementedException();
39+
}
40+
}
41+
42+
public Elasticsearch.Net.ElasticsearchServerError ServerError {
43+
get {
44+
throw new NotImplementedException();
45+
}
46+
}
47+
48+
#endregion
49+
50+
#region IResponseWithRequestInformation implementation
51+
52+
public Elasticsearch.Net.IElasticsearchResponse RequestInformation {
53+
get {
54+
throw new NotImplementedException();
55+
}
56+
set {
57+
throw new NotImplementedException();
58+
}
59+
}
60+
61+
#endregion
62+
}
63+
}
64+

0 commit comments

Comments
 (0)