Skip to content

Commit

Permalink
Merge pull request #23 from DFE-Digital/CL/LogicalOperatorFactory-tests
Browse files Browse the repository at this point in the history
Modify tests to test the actual factory rather than a mock of the factory
  • Loading branch information
CathLass authored Nov 18, 2024
2 parents fc4e784 + cc2e2db commit e03bc9c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@
/// </summary>
public interface ILogicalOperatorFactory
{
/// <summary>
/// Allows creation of an <see cref="ILogicalOperator"/> instance based on the type requested.
/// </summary>
/// <param name="logicalOperatorType">
/// The concrete implementation type of <see cref="ILogicalOperator"/> requested.
/// </param>
/// <returns>
/// The configured instance of the <see cref="ILogicalOperator"/> type.
/// </returns>
ILogicalOperator CreateLogicalOperator(Type logicalOperatorType);

/// <summary>
/// Allows creation of an <see cref="ILogicalOperator"/> instance based on the type name requested.
/// </summary>
Expand All @@ -28,15 +17,4 @@ public interface ILogicalOperatorFactory
/// The configured instance of the <see cref="ILogicalOperator"/> type.
/// </returns>
ILogicalOperator CreateLogicalOperator(string logicalOperatorName);

/// <summary>
/// Allows creation of an <see cref="ILogicalOperator"/> instance based on the generic type specified.
/// </summary>
/// <typeparam name="TLogicalOperator">
/// The concrete type of <see cref="ILogicalOperator"/> requested.
/// </typeparam>
/// <returns>
/// The configured instance of the <see cref="ILogicalOperator"/> type.
/// </returns>
ILogicalOperator CreateLogicalOperator<TLogicalOperator>() where TLogicalOperator : ILogicalOperator;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,6 @@ public LogicalOperatorFactory(Dictionary<string, Func<ILogicalOperator>> logical
_logicalOperatorFactory = logicalOperatorFactory;
}

/// <summary>
/// Allows creation of an <see cref="ILogicalOperator"/> instance based on the generic type specified.
/// </summary>
/// <typeparam name="TLogicalOperator">
/// The concrete type of <see cref="ILogicalOperator"/> requested.
/// </typeparam>
/// <returns>
/// The configured instance of the <see cref="ILogicalOperator"/> type.
/// </returns>
public ILogicalOperator CreateLogicalOperator<TLogicalOperator>()
where TLogicalOperator : ILogicalOperator => CreateLogicalOperator(typeof(TLogicalOperator));

/// <summary>
/// Allows creation of an <see cref="ILogicalOperator"/> instance based on the type requested.
/// </summary>
/// <param name="logicalOperatorType">
/// The concrete implementation type of <see cref="ILogicalOperator"/> requested.
/// </param>
/// <returns>
/// The configured instance of the <see cref="ILogicalOperator"/> type.
/// </returns>
public ILogicalOperator CreateLogicalOperator(Type logicalOperatorType) => CreateLogicalOperator(logicalOperatorName: logicalOperatorType.Name);


/// <summary>
/// Allows creation of an <see cref="ILogicalOperator"/> instance based on the type name requested.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public async Task AddAzureSearchServices_RegistersAllDependencies()

// act
var response =
await searchByKeywordService
await searchByKeywordService!
.SearchAsync<ExpandoObject>(
searchKeyword: "Test",
searchIndex: "Index",
searchOptions: new Azure.Search.Documents.SearchOptions()
);
)!;

response.Should().NotBeNull();
}
Expand All @@ -78,7 +78,7 @@ public async Task AddAzureGeoLocationSearchServices_RegistersAllDependencies()

IGeoLocationClientProvider mockGeoLocationClientProvider =
GeoLocationClientProviderTestDouble
.CreateWithHttpStatusAndResponse(HttpStatusCode.OK, "{}");
.CreateWithHttpStatusAndResponse(HttpStatusCode.OK, "{ }");

IServiceProvider? serviceProvider =
_compositionRootServiceProvider?
Expand All @@ -98,7 +98,7 @@ public async Task AddAzureGeoLocationSearchServices_RegistersAllDependencies()
}

[Fact]
public async Task AddAzureSearchFilterServices_RegistersAllDependencies()
public void AddAzureSearchFilterServices_RegistersAllDependencies()
{
Dictionary<string, string?> config = new() {
{ "FilterKeyToFilterExpressionMapOptions:FilterChainingLogicalOperator", "AndLogicalOperator" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void AddAzureGeoLocationSearchServices_MissingGeoLocationOptions_MapsSubs
}

[Fact]
public async Task AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressionMapOptions_FilterChainingLogicalOperator_ThrowOptionsValidationException()
public void AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressionMapOptions_FilterChainingLogicalOperator_ThrowOptionsValidationException()
{
Dictionary<string, string?> config = new() {
{ "FilterKeyToFilterExpressionMapOptions:SearchFilterToExpressionMap:test-key:FilterExpressionKey", "SearchInFilterExpression" },
Expand All @@ -173,7 +173,7 @@ public async Task AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressio
}

[Fact]
public async Task AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressionMapOptions_SearchFilterToExpressionMap_ThrowOptionsValidationException()
public void AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressionMapOptions_SearchFilterToExpressionMap_ThrowOptionsValidationException()
{
Dictionary<string, string?> config = new() {
{ "FilterKeyToFilterExpressionMapOptions:FilterChainingLogicalOperator", "AndLogicalOperator" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Dfe.Data.Common.Infrastructure.CognitiveSearch.Filtering.LogicalOperators;
using Dfe.Data.Common.Infrastructure.CognitiveSearch.Filtering.LogicalOperators.Factories;
using Dfe.Data.Common.Infrastructure.CognitiveSearch.Tests.Filtering.FilterExpressions;
using Dfe.Data.Common.Infrastructure.CognitiveSearch.Tests.Filtering.TestDoubles;
using FluentAssertions;
using Xunit;

Expand All @@ -12,56 +12,37 @@ public sealed class LogicalOperatorFactoryTests
public void CreateLogicalOperator_WithValidOperatorName_ReturnsExpectedConfiguredOperatorFromfactory()
{
// arrange.
ILogicalOperatorFactory logicalOperatorFactory =
LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory();
ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create());

// act.
ILogicalOperator logicalOperator =
logicalOperatorFactory.CreateLogicalOperator("AndLogicalOperator");
logicalOperatorFactory.CreateLogicalOperator("name1");

// assert.
logicalOperator.Should().NotBeNull();
logicalOperator.Should().BeOfType<AndLogicalOperator>();
}

[Fact]
public void CreateLogicalOperator_WithValidOperatorType_ReturnsExpectedConfiguredOperatorFromfactory()
public void CreateFilter_WithUnknownFilterName_ThrowsExpectedException()
{
// arrange.
ILogicalOperatorFactory logicalOperatorFactory =
LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory();
ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create());

// act.
ILogicalOperator logicalOperator =
logicalOperatorFactory.CreateLogicalOperator(typeof(AndLogicalOperator));

// assert.
logicalOperator.Should().NotBeNull();
logicalOperator.Should().BeOfType<AndLogicalOperator>();
}

[Fact]
public void CreateLogicalOperator_WithValidOperatorGenricTemplateType_ReturnsExpectedConfiguredOperatorFromfactory()
{
// arrange.
ILogicalOperatorFactory logicalOperatorFactory =
LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactoryFor<AndLogicalOperator>();

// act.
ILogicalOperator logicalOperator =
logicalOperatorFactory.CreateLogicalOperator<AndLogicalOperator>();
Action failedCreateOperatorAction = () =>
logicalOperatorFactory.CreateLogicalOperator(logicalOperatorName: "something else");

// assert.
logicalOperator.Should().NotBeNull();
logicalOperator.Should().BeOfType<AndLogicalOperator>();
//assert
ArgumentException exception =
Assert.Throws<ArgumentOutOfRangeException>(failedCreateOperatorAction);
}

[Fact]
public void CreateFilter_WithNullFilterName_ThrowsExpectedException()
{
// arrange.
ILogicalOperatorFactory logicalOperatorFactory =
LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory();
ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create());

// act.
Action failedCreateOperatorAction = () =>
Expand All @@ -78,9 +59,7 @@ public void CreateFilter_WithNullFilterName_ThrowsExpectedException()
public void CreateFilter_WithEmptyFilterName_ThrowsExpectedException()
{
// arrange.
ILogicalOperatorFactory logicalOperatorFactory =
LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory();

ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create());
// act.
Action failedCreateOperatorAction = () =>
logicalOperatorFactory.CreateLogicalOperator(logicalOperatorName: "");
Expand All @@ -96,8 +75,7 @@ public void CreateFilter_WithEmptyFilterName_ThrowsExpectedException()
public void CreateFilter_WithWhitespaceFilterName_ThrowsExpectedException()
{
// arrange.
ILogicalOperatorFactory logicalOperatorFactory =
LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory();
ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create());

// act.
Action failedCreateOperatorAction = () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Dfe.Data.Common.Infrastructure.CognitiveSearch.Filtering.LogicalOperators;

namespace Dfe.Data.Common.Infrastructure.CognitiveSearch.Tests.Filtering.TestDoubles;

public static class ILogicalOperators
{
public static Dictionary<string, Func<ILogicalOperator>> Create()
{
return new() {
{ "name1", () => new AndLogicalOperator()}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,6 @@ public static ILogicalOperatorFactory MockLogicalOperatorFactoryFor()
.Returns((string type) =>
GetLogicalOperator(type));

logicalOperatorFactoryMock
.Setup(logicalOperatorFactory =>
logicalOperatorFactory
.CreateLogicalOperator(It.IsAny<Type>()))
.Returns((Type type) =>
GetLogicalOperator(type.Name));

return logicalOperatorFactoryMock.Object;
}

public static ILogicalOperatorFactory MockLogicalOperatorFactoryFor<TFilterType>()
{
var logicalOperatorFactoryMock = LogicalOperatorFactoryMock();

logicalOperatorFactoryMock
.Setup(logicalOperatorFactory =>
logicalOperatorFactory
.CreateLogicalOperator<ILogicalOperator>())
.Returns(() =>
GetLogicalOperator(
typeof(TFilterType).Name));

return logicalOperatorFactoryMock.Object;
}

Expand Down

0 comments on commit e03bc9c

Please sign in to comment.