Skip to content

Commit

Permalink
FliptProvider: Update ReadMe, test fliptClientWrapper calls
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei de la Cruz <[email protected]>
  • Loading branch information
jeandreidc committed Oct 3, 2024
1 parent 2417a9a commit fd7210a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/OpenFeature.Contrib.Providers.Flipt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ nswag openapi2csclient /className:FliptRestClient /namespace:Flipt.Rest /input:"

## Know issues and limitations

-
In https://github.com/open-feature/dotnet-sdk-contrib/blob/204144f6df0dacf46e6d52d34dd6b5a223a853f4/src/OpenFeature.Contrib.Providers.Flipt/ClientWrapper/FliptClientWrapper.cs#L41-L47
-In `BuildClient()` method
from https://github.com/open-feature/dotnet-sdk-contrib/blob/204144f6df0dacf46e6d52d34dd6b5a223a853f4/src/OpenFeature.Contrib.Providers.Flipt/ClientWrapper/FliptClientWrapper.cs#L41-L47
a new `HttpClient` is created. In the future it would be better to allow passing of `HttpConnectionFactory` to avoid
problems regarding socket starvation

82 changes: 80 additions & 2 deletions test/OpenFeature.Contrib.Providers.Flipt.Test/FliptProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task
.ReturnsAsync(new VariantEvaluationResponse
{
FlagKey = flagKey,
VariantKey = "iamastring",
VariantKey = "variant-key",
RequestId = Guid.NewGuid()
.ToString(),
SegmentKeys = ["segment1"],
Expand All @@ -65,5 +65,83 @@ public async Task
valueResolution.ErrorType.Should().Be(ErrorType.TypeMismatch);
}

// Todo Andrei: Add tests to make sure that the wrapper was called
[Fact]
public async Task ResolveStringValueAsync_WhenCalled_ShouldCallCorrectMethodFromFliptClientWrapper()
{
const string flagKey = "feature-flag-key";
var (provider, mockFliptClientWrapper) = GenerateFliptProviderWithMockedDependencies(flagKey);
await provider.ResolveStringValueAsync(flagKey, "");
mockFliptClientWrapper.Verify(
fcw => fcw.EvaluateVariantAsync(It.Is<EvaluationRequest>(er => er.FlagKey == flagKey)), Times.Once);
}

[Fact]
public async Task ResolveDoubleValueAsync_WhenCalled_ShouldCallCorrectMethodFromFliptClientWrapper()
{
const string flagKey = "feature-flag-key";
var (provider, mockFliptClientWrapper) = GenerateFliptProviderWithMockedDependencies(flagKey);
await provider.ResolveDoubleValueAsync(flagKey, 0.0);
mockFliptClientWrapper.Verify(
fcw => fcw.EvaluateVariantAsync(It.Is<EvaluationRequest>(er => er.FlagKey == flagKey)), Times.Once);
}

[Fact]
public async Task ResolveIntegerValueAsync_WhenCalled_ShouldCallCorrectMethodFromFliptClientWrapper()
{
const string flagKey = "feature-flag-key";
var (provider, mockFliptClientWrapper) = GenerateFliptProviderWithMockedDependencies(flagKey);
await provider.ResolveIntegerValueAsync(flagKey, 0);
mockFliptClientWrapper.Verify(
fcw => fcw.EvaluateVariantAsync(It.Is<EvaluationRequest>(er => er.FlagKey == flagKey)), Times.Once);
}

[Fact]
public async Task ResolveStructureValueAsync_WhenCalled_ShouldCallCorrectMethodFromFliptClientWrapper()
{
const string flagKey = "feature-flag-key";
var (provider, mockFliptClientWrapper) = GenerateFliptProviderWithMockedDependencies(flagKey);
await provider.ResolveStructureValueAsync(flagKey, new Value());
mockFliptClientWrapper.Verify(
fcw => fcw.EvaluateVariantAsync(It.Is<EvaluationRequest>(er => er.FlagKey == flagKey)), Times.Once);
}

[Fact]
public async Task ResolveBooleanValueAsync_WhenCalled_ShouldCallCorrectMethodFromFliptClientWrapper()
{
const string flagKey = "feature-flag-key";
var (provider, mockFliptClientWrapper) = GenerateFliptProviderWithMockedDependencies(flagKey);
await provider.ResolveBooleanValueAsync(flagKey, false);
mockFliptClientWrapper.Verify(
fcw => fcw.EvaluateBooleanAsync(It.Is<EvaluationRequest>(er => er.FlagKey == flagKey)), Times.Once);
}

private (FliptProvider, Mock<IFliptClientWrapper>) GenerateFliptProviderWithMockedDependencies(string flagKey)
{
var mockFliptClientWrapper = new Mock<IFliptClientWrapper>();
mockFliptClientWrapper.Setup(fcw => fcw.EvaluateVariantAsync(It.IsAny<EvaluationRequest>()))
.ReturnsAsync(new VariantEvaluationResponse
{
FlagKey = flagKey,
VariantKey = "variant-key",
RequestId = Guid.NewGuid()
.ToString(),
SegmentKeys = ["segment1"],
VariantAttachment = "",
Match = true,
Reason = EvaluationReason.MATCH_EVALUATION_REASON
});

mockFliptClientWrapper.Setup(fcw => fcw.EvaluateBooleanAsync(It.IsAny<EvaluationRequest>()))
.ReturnsAsync(new BooleanEvaluationResponse
{
FlagKey = flagKey,
RequestId = Guid.NewGuid()
.ToString(),
Enabled = true,
Reason = EvaluationReason.MATCH_EVALUATION_REASON
});

return (new FliptProvider(new FliptToOpenFeatureConverter(mockFliptClientWrapper.Object)),
mockFliptClientWrapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,4 @@ public async Task
resolution.Value.Should().BeEquivalentTo(fallbackValue);
resolution.ErrorType.Should().Be(ErrorType.FlagNotFound);
}

/* Todo Andrei: Mga kulang pa na unit test
- Successful na flag
- Boolean
- Variant and other types
- Wrong flag name
- Boolean
- Variant
- Type mismatch call
Strategy:
1. I-mock lang return type ni Flipt
*/
}

0 comments on commit fd7210a

Please sign in to comment.