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

Modify tests to test the actual factory rather than a mock of the factory #23

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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