Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cl/infrastructure composition root tests #51

Merged
merged 15 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ ISearchFilterExpressionsBuilder filterExpressionsBuilder

public CognitiveSearchServiceAdapterTests()
{
_mockSearchService = new SearchServiceMockBuilder().MockSearchService("SearchKeyword", _options.SearchIndex);
_mockSearchService = new SearchServiceMockBuilder()
.WithSearchKeywordAndCollection("SearchKeyword", _options.SearchIndex)
.WithSearchResults(new SearchResultFakeBuilder().WithSearchResults().Create())
.Create();
}

[Fact]
Expand Down Expand Up @@ -100,8 +103,6 @@ public void Search_WithFilters_CallsFilterBuilder_WithComposedFilterRequests()
})
.Returns("some filter string");

var mockService = new SearchServiceMockBuilder().MockSearchService("SearchKeyword", _options.SearchIndex);

var searchServiceAdapterRequest = SearchServiceAdapterRequestTestDouble.WithFilters(serviceAdapterInputFilterRequest);

var adapter = new CognitiveSearchServiceAdapter<DataTransferObjects.Establishment>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Dfe.Data.SearchPrototype.Infrastructure.Tests.Integration.TestHarness;
using Dfe.Data.SearchPrototype.Infrastructure.Tests.TestDoubles;
using Dfe.Data.SearchPrototype.SearchForEstablishments.ByKeyword.ServiceAdapters;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Dfe.Data.SearchPrototype.Infrastructure.Tests.Integration;

public class CognitiveSearchServiceAdaptorIntegrationTests :IClassFixture<ConfigBuilder>, IClassFixture<MyServiceProvider>
{
private IConfiguration _configuration;
private IServiceProvider _serviceProvider;

private static Dictionary<string, string?> InMemoryConfig => new() {
{"AzureSearchOptions:SearchIndex", "establishments" }
};

public CognitiveSearchServiceAdaptorIntegrationTests(ConfigBuilder configBuilder, MyServiceProvider serviceProvider)
{
_configuration = configBuilder.SetupConfiguration(InMemoryConfig);
_serviceProvider = serviceProvider.SetUpServiceProvider(_configuration);
}

[Fact]
public async Task AddCognitiveSearchAdaptorServices_RegistersEverythingNeeded()
{
var adapter = _serviceProvider.GetRequiredService<ISearchServiceAdapter>();

var searchServiceAdapterRequest = SearchServiceAdapterRequestTestDouble.Create();

// act
var response = await adapter.SearchAsync(searchServiceAdapterRequest);
response.Should().NotBeNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Configuration;

namespace Dfe.Data.SearchPrototype.Infrastructure.Tests.Integration.TestHarness;

public class ConfigBuilder
{
public IConfiguration SetupConfiguration(Dictionary<string, string?> options)
{
var configBuilder = new ConfigurationBuilder();

configBuilder.AddInMemoryCollection(options);
return configBuilder.Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Dfe.Data.Common.Infrastructure.CognitiveSearch.Filtering;
using Dfe.Data.Common.Infrastructure.CognitiveSearch.SearchByKeyword;
using Dfe.Data.SearchPrototype.Infrastructure.Tests.TestDoubles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Dfe.Data.SearchPrototype.Infrastructure.Tests.Integration.TestHarness;

public class MyServiceProvider
{
public IServiceProvider SetUpServiceProvider(IConfiguration config)
spanersoraferty marked this conversation as resolved.
Show resolved Hide resolved
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton<IConfiguration>(config);

// this is the extension method to add all the dependencies
services.AddCognitiveSearchAdaptorServices(config);

// Replace Common.Infrastructure services with mocks
services.RemoveAll<ISearchByKeywordService>();
var mockSearchService = new SearchServiceMockBuilder()
.WithSearchKeywordAndCollection("searchKeyword", "establishments")
.WithSearchResults(new SearchResultFakeBuilder()
.WithSearchResults()
.Create())
.Create();
services.AddScoped<ISearchByKeywordService>(provider => mockSearchService);
services.RemoveAll<ISearchFilterExpressionsBuilder>();
var mockFilterExpressionBuilder = new FilterExpressionBuilderTestDouble().Create();
services.AddScoped<ISearchFilterExpressionsBuilder>(provider => mockFilterExpressionBuilder);

return services.BuildServiceProvider();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public static Expression<Func<ISearchByKeywordService, Task<Response<SearchResul

private string _keyword = string.Empty;
private string _collection = string.Empty;
private long _count = 100;
private IEnumerable<SearchResult<Establishment>>? _searchResults;
private Dictionary<string, IList<FacetResult>>? _facets;

Expand Down Expand Up @@ -58,18 +57,4 @@ public ISearchByKeywordService Create()
.Create();
return MockFor(response, _keyword, _collection);
}

public ISearchByKeywordService MockSearchService(string keyword, string collection)
{
_keyword = keyword;
_collection = collection;

var responseMock = new Mock<Response>();
var response =
Response.FromValue(
SearchModelFactory.SearchResults(
new SearchResultFakeBuilder().WithSearchResults().Create(), _count, null, null, responseMock.Object), responseMock.Object);

return MockFor(response, _keyword, _collection);
}
}