Skip to content

Commit

Permalink
PitSearch tests
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <tsfarr@amazon.com>
  • Loading branch information
Xtansia committed Oct 30, 2023
1 parent e576b77 commit 65e6835
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 132 deletions.
8 changes: 4 additions & 4 deletions guides/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,26 @@ var page1 = await client.SearchAsync<Movie>(s => s
.Query(_ => query)
.Sort(_ => sort)
.Size(2)
.PointInTime(p => p.PitId(pitResp.PitId).KeepAlive("1m")));
.PointInTime(p => p.Id(pitResp.PitId).KeepAlive("1m")));
var page2 = await client.SearchAsync<Movie>(s => s
.Query(_ => query)
.Sort(_ => sort)
.Size(2)
.PointInTime(p => p.PitId(pitResp.PitId).KeepAlive("1m"))
.PointInTime(p => p.Id(pitResp.PitId).KeepAlive("1m"))
.SearchAfter(page1.Hits.Last().Sorts));
var page3 = await client.SearchAsync<Movie>(s => s
.Query(_ => query)
.Sort(_ => sort)
.Size(2)
.PointInTime(p => p.PitId(pitResp.PitId).KeepAlive("1m"))
.PointInTime(p => p.Id(pitResp.PitId).KeepAlive("1m"))
.SearchAfter(page2.Hits.Last().Sorts));

foreach (var doc in page1.Documents.Concat(page2.Documents).Concat(page3.Documents))
{
Console.WriteLine(doc.Title);
}

await client.DeletePitAsync(p => p.PitIds(pitResp.PitId));
await client.DeletePitAsync(p => p.PitId(pitResp.PitId));
```

Note that a point-in-time is associated with an index or a set of index. So, when performing a search with a point-in-time, you DO NOT specify the index in the search.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class PointInTimeDescriptor : DescriptorBase<PointInTimeDescriptor, IPoin
string IPointInTime.Id { get; set; }
Time IPointInTime.KeepAlive { get; set; }

public PointInTimeDescriptor PitId(string id) => Assign(id, (a, v) => a.Id = v);
public PointInTimeDescriptor Id(string id) => Assign(id, (a, v) => a.Id = v);

public PointInTimeDescriptor KeepAlive(Time keepAlive) => Assign(keepAlive, (a, v) => a.KeepAlive = v);
}
9 changes: 2 additions & 7 deletions tests/Tests/Search/PointInTime/CreatePitApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected override LazyResponses ClientUsage() => Calls(
(c, r) => c.CreatePitAsync(r)
);

protected override CreatePitDescriptor NewDescriptor() => new(CallIsolatedValue);
protected override CreatePitDescriptor NewDescriptor() => new(OpenSearch.Client.Indices.Index<Project>());

protected override void ExpectResponse(CreatePitResponse response)
{
Expand All @@ -65,10 +65,5 @@ protected override void ExpectResponse(CreatePitResponse response)
response.Shards.Should().NotBeNull();
}

protected override void OnAfterCall(IOpenSearchClient client)
{
if (string.IsNullOrEmpty(_pitId)) return;
client.DeletePit(d => d.PitId(_pitId));
_pitId = null;
}
protected override void OnAfterCall(IOpenSearchClient client) => client.DeletePit(d => d.PitId(_pitId));
}
6 changes: 2 additions & 4 deletions tests/Tests/Search/PointInTime/DeleteAllPitsApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ namespace Tests.Search.PointInTime;

[SkipVersion("<2.4.0", "Point-In-Time search support was added in version 2.4.0")]
public class DeleteAllPitsApiTests
: ApiIntegrationTestBase<ReadOnlyCluster, DeleteAllPitsResponse, IDeleteAllPitsRequest, DeleteAllPitsDescriptor, DeleteAllPitsRequest>
: ApiIntegrationTestBase<WritableCluster, DeleteAllPitsResponse, IDeleteAllPitsRequest, DeleteAllPitsDescriptor, DeleteAllPitsRequest>
{
private readonly List<string> _pitIds = new();

public DeleteAllPitsApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
public DeleteAllPitsApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override bool ExpectIsValid => true;

Expand Down Expand Up @@ -65,8 +65,6 @@ protected override void ExpectResponse(DeleteAllPitsResponse response)

protected override void OnBeforeCall(IOpenSearchClient client)
{
_pitIds.Clear();

for (var i = 0; i < 5; i++)
{
var pit = Client.CreatePit(OpenSearch.Client.Indices.Index<Project>(), c => c.KeepAlive("1h"));
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests/Search/PointInTime/DeletePitApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ protected override void ExpectResponse(DeletePitResponse response)

protected override void OnBeforeCall(IOpenSearchClient client)
{
var pit = Client.CreatePit(OpenSearch.Client.Indices.Index<Project>(), c => c.KeepAlive("1h"));
var pit = client.CreatePit(OpenSearch.Client.Indices.Index<Project>(), c => c.KeepAlive("1h"));
if (!pit.IsValid)
throw new Exception("Setup: Initial PIT failed.");

_pitId = pit.PitId ?? _pitId;
_pitId = pit.PitId;
}
}
15 changes: 7 additions & 8 deletions tests/Tests/Search/PointInTime/GetAllPitsApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Tests.Search.PointInTime;
public class GetAllPitsApiTests
: ApiIntegrationTestBase<ReadOnlyCluster, GetAllPitsResponse, IGetAllPitsRequest, GetAllPitsDescriptor, GetAllPitsRequest>
{
private readonly List<CreatePitResponse> _pits = new();
private List<(string id, long creationTime)> _pits = new();

public GetAllPitsApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

Expand Down Expand Up @@ -52,32 +52,31 @@ protected override LazyResponses ClientUsage() => Calls(

protected override void ExpectResponse(GetAllPitsResponse response)
{
_pits.Should().HaveCount(5);
response.ShouldBeValid();
response.Pits.Should()
.NotBeNull()
.And.HaveCount(5)
.And.BeEquivalentTo(_pits.Select(p => new PitDetail
{
PitId = p.PitId,
CreationTime = p.CreationTime,
PitId = p.id,
CreationTime = p.creationTime,
KeepAlive = 60 * 60 * 1000
}));
}

protected override void OnBeforeCall(IOpenSearchClient client)
{
_pits.Clear();

for (var i = 0; i < 5; i++)
{
var pit = Client.CreatePit(OpenSearch.Client.Indices.Index<Project>(), c => c.KeepAlive("1h"));
var pit = client.CreatePit(OpenSearch.Client.Indices.Index<Project>(), c => c.KeepAlive("1h"));
if (!pit.IsValid)
throw new Exception("Setup: Initial PIT failed.");

_pits.Add(pit);
_pits.Add((pit.PitId, pit.CreationTime));
}
}

protected override void OnAfterCall(IOpenSearchClient client) =>
client.DeletePit(d => d.PitId(_pits.Select(p => p.PitId)));
client.DeletePit(d => d.PitId(_pits.Select(p => p.id)));
}
119 changes: 119 additions & 0 deletions tests/Tests/Search/PointInTime/PitSearchApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using System;
using System.Linq;
using FluentAssertions;
using OpenSearch.Client;
using OpenSearch.Net;
using OpenSearch.OpenSearch.Xunit.XunitPlumbing;
using Tests.Core.Extensions;
using Tests.Core.ManagedOpenSearch.Clusters;
using Tests.Framework.EndpointTests;
using Tests.Framework.EndpointTests.TestState;

namespace Tests.Search.PointInTime;

[SkipVersion("<2.4.0", "Point-In-Time search support was added in version 2.4.0")]
public sealed class PitSearchApiTests :
ApiIntegrationTestBase<
WritableCluster,
ISearchResponse<PitSearchApiTests.Doc>,
ISearchRequest,
SearchDescriptor<PitSearchApiTests.Doc>,
SearchRequest<PitSearchApiTests.Doc>
>
{
public PitSearchApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private string _pitId = "default-for-unit-tests";

protected override object ExpectJson => new
{
query = new
{
match_all = new { }
},
pit = new
{
id = _pitId,
keep_alive = "1h"
},
track_total_hits = true
};

protected override HttpMethod HttpMethod => HttpMethod.POST;
protected override string UrlPath => "/_search";

protected override Func<SearchDescriptor<Doc>, ISearchRequest> Fluent => s => s
.Query(q => q.MatchAll())
.PointInTime(p => p
.Id(_pitId)
.KeepAlive("1h"))
.TrackTotalHits();

protected override bool ExpectIsValid => true;
protected override int ExpectStatusCode => 200;

protected override SearchRequest<Doc> Initializer => new(null)
{
Query = new QueryContainer(new MatchAllQuery()),
PointInTime = new OpenSearch.Client.PointInTime
{
Id = _pitId,
KeepAlive = "1h"
},
TrackTotalHits = true
};

protected override SearchDescriptor<Doc> NewDescriptor() => new(null);

protected override void ExpectResponse(ISearchResponse<Doc> response)
{
response.ShouldBeValid();
response.Total.Should().Be(10);
response
.Documents
.Should()
.NotBeNull()
.And.HaveCount(10)
.And.BeEquivalentTo(Enumerable.Range(0, 10).Select(i => new Doc { Id = i }));
}

protected override void OnBeforeCall(IOpenSearchClient client)
{
var bulkResp = client.Bulk(b => b
.Index(CallIsolatedValue)
.IndexMany(Enumerable.Range(0, 10).Select(i => new Doc { Id = i }))
.Refresh(Refresh.WaitFor));
bulkResp.ShouldBeValid();

var pitResp = client.CreatePit(CallIsolatedValue, p => p.KeepAlive("1h"));
pitResp.ShouldBeValid();
_pitId = pitResp.PitId;

bulkResp = client.Bulk(b => b
.Index(CallIsolatedValue)
.IndexMany(Enumerable.Range(10, 10).Select(i => new Doc { Id = i }))
.Refresh(Refresh.WaitFor));
bulkResp.ShouldBeValid();
}

protected override void OnAfterCall(IOpenSearchClient client) => client.DeletePit(d => d.PitId(_pitId));

protected override LazyResponses ClientUsage() => Calls(
(c, f) => c.Search(f),
(c, f) => c.SearchAsync(f),
(c, r) => c.Search<Doc>(r),
(c, r) => c.SearchAsync<Doc>(r)
);

public class Doc
{
public long Id { get; set; }
}
}
106 changes: 0 additions & 106 deletions tests/Tests/Search/PointInTime/PointInTimeIntegrationTests.cs

This file was deleted.

0 comments on commit 65e6835

Please sign in to comment.