Skip to content

Commit

Permalink
fix: Always create the index if it doesn't already exist (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
killij authored Dec 8, 2023
1 parent 7263aa6 commit c6cda71
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public async Task DeleteIndexAsync_Skips_Deletion_If_Index_Does_Not_Exist()
// arrange
var response = Substitute.For<Response<SearchIndex>>();
response.HasValue.Returns(false);
_client.GetIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(response));
_client.GetIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(response));

// act
await _sut.DeleteIndexAsync("foo");
Expand All @@ -46,8 +45,7 @@ public async Task DeleteIndexAsync_Deletes_The_Index()
// arrange
var response = Substitute.For<Response<SearchIndex>>();
response.HasValue.Returns(true);
_client.GetIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(response));
_client.GetIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(response));

// act
await _sut.DeleteIndexAsync("foo");
Expand All @@ -65,10 +63,8 @@ public async Task DeleteIndexAsync_Logs_Failure_To_Delete_Index()

var deleteIndexResult = Substitute.For<Response>();
deleteIndexResult.IsError.Returns(true);
_client.GetIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(getIndexResult));
_client.DeleteIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(deleteIndexResult));
_client.GetIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(getIndexResult));
_client.DeleteIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(deleteIndexResult));

// act
await _sut.DeleteIndexAsync("foo");
Expand All @@ -81,6 +77,10 @@ public async Task DeleteIndexAsync_Logs_Failure_To_Delete_Index()
public async Task CreateIndexAsync_Creates_The_Index()
{
// arrange
var getIndexResult = Substitute.For<Response<SearchIndex>>();
getIndexResult.HasValue.Returns(false);
_client.GetIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(getIndexResult));

SearchIndex? searchIndex = null;
await _client.CreateIndexAsync(Arg.Do<SearchIndex>(x => searchIndex = x), Arg.Any<CancellationToken>());

Expand All @@ -92,7 +92,23 @@ public async Task CreateIndexAsync_Creates_The_Index()
searchIndex.Should().NotBeNull();
searchIndex!.Name.Should().Be("foo");
}


[Test]
public async Task CreateIndexAsync_Skips_Index_Creation()
{
// arrange
var getIndexResult = Substitute.For<Response<SearchIndex>>();
getIndexResult.HasValue.Returns(true);
_client.GetIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(getIndexResult));
await _client.CreateIndexAsync(Arg.Any<SearchIndex>(), Arg.Any<CancellationToken>());

// act
await _sut.CreateIndexAsync("foo");

// assert
await _client.Received(0).CreateIndexAsync(Arg.Any<SearchIndex>(), Arg.Any<CancellationToken>());
}

[Test]
public async Task PopulateIndexAsync_Uploads_Documents()
{
Expand Down
12 changes: 7 additions & 5 deletions src/Childrens-Social-Care-CPD-Indexer/Core/ResourcesIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ internal class ResourcesIndexer(SearchIndexClient searchIndexClient, IDocumentFe
{
public async Task CreateIndexAsync(string indexName, CancellationToken cancellationToken = default)
{
var index = await searchIndexClient.GetIndexAsync(indexName, cancellationToken);
if (index.HasValue)
{
logger.LogInformation("Index already exists, skipping creation.");
return;
}

logger.LogInformation("Creating index...");
var fieldBuilder = new FieldBuilder();
var searchFields = fieldBuilder.Build(typeof(CpdDocument));
Expand All @@ -18,15 +25,10 @@ public async Task CreateIndexAsync(string indexName, CancellationToken cancellat
public async Task DeleteIndexAsync(string indexName, CancellationToken cancellationToken = default)
{
logger.LogInformation("Deleting index...");

var index = await searchIndexClient.GetIndexAsync(indexName, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();

if (index.HasValue)
{
var deleteResponse = await searchIndexClient.DeleteIndexAsync(indexName, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();

if (deleteResponse.IsError)
{
logger.LogError("Failed to delete the index");
Expand Down
2 changes: 1 addition & 1 deletion src/Childrens-Social-Care-CPD-Indexer/IndexingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public async Task StartAsync(CancellationToken cancellationToken)
if (config.RecreateIndex)
{
await resourcesIndexer.DeleteIndexAsync(config.IndexName, cancellationToken);
await resourcesIndexer.CreateIndexAsync(config.IndexName, cancellationToken);
}

await resourcesIndexer.CreateIndexAsync(config.IndexName, cancellationToken);
await resourcesIndexer.PopulateIndexAsync(config.IndexName, config.BatchSize, cancellationToken);

}
Expand Down

0 comments on commit c6cda71

Please sign in to comment.