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

chore: additional unit tests for flagd provider #203

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -183,7 +183,7 @@ private ResolutionDetails<T> ResolveValue<T>(string flagKey, T defaultValue, Eva
// convert the EvaluationContext object into something the JsonLogic evaluator can work with
dynamic contextObj = (object)ConvertToDynamicObject(targetingContext);

// convery whatever is returned to a sring to try to use is an a index to Variants
// convert whatever is returned to a string to try to use it as an index to Variants
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope I didn't write this but I fear I did 😅

var ruleResult = _evaluator.Apply(rule, contextObj);
if (ruleResult is bool)
{
Expand Down Expand Up @@ -217,7 +217,7 @@ private ResolutionDetails<T> ResolveValue<T>(string flagKey, T defaultValue, Eva
}
else if (flagConfiguration.Variants.TryGetValue(variant, out var foundVariantValue))
{
// if variant can be found, return it - this could be TARGETING_MATCH or STAIC.
// if variant can be found, return it - this could be TARGETING_MATCH or STATIC.
var value = ExtractFoundVariant<T>(foundVariantValue, flagKey);
return new ResolutionDetails<T>(
flagKey: flagKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,70 @@ public void TestJsonEvaluatorDynamicBoolEvaluationUsingFlagdPropertyTimestamp()
Assert.Equal(Reason.TargetingMatch, result.Reason);
}

[Fact]
public void TestJsonEvaluatorDynamicBoolEvaluationSharedEvaluator()
{
var fixture = new Fixture();

var jsonEvaluator = new JsonEvaluator(fixture.Create<string>());

jsonEvaluator.Sync(FlagConfigurationUpdateType.ALL, Utils.flags);

var builder = EvaluationContext.Builder().Set("email", "[email protected]");

var result = jsonEvaluator.ResolveBooleanValue("targetingBoolFlagUsingSharedEvaluator", false, builder.Build());

Assert.True(result.Value);
Assert.Equal("bool1", result.Variant);
Assert.Equal(Reason.TargetingMatch, result.Reason);
}

[Fact]
public void TestJsonEvaluatorDynamicBoolEvaluationSharedEvaluatorReturningBoolType()
{
var fixture = new Fixture();

var jsonEvaluator = new JsonEvaluator(fixture.Create<string>());

jsonEvaluator.Sync(FlagConfigurationUpdateType.ALL, Utils.flags);

var builder = EvaluationContext.Builder().Set("email", "[email protected]");

var result = jsonEvaluator.ResolveBooleanValue("targetingBoolFlagUsingSharedEvaluatorReturningBoolType", false, builder.Build());

Assert.True(result.Value);
Assert.Equal("true", result.Variant);
Assert.Equal(Reason.TargetingMatch, result.Reason);
}

[Fact]
public void TestJsonEvaluatorDynamicBoolEvaluationWithMissingDefaultVariant()
{
var fixture = new Fixture();

var jsonEvaluator = new JsonEvaluator(fixture.Create<string>());

jsonEvaluator.Sync(FlagConfigurationUpdateType.ALL, Utils.flags);

var builder = EvaluationContext.Builder();

Assert.Throws<FeatureProviderException>(() => jsonEvaluator.ResolveBooleanValue("targetingBoolFlagWithMissingDefaultVariant", false, builder.Build()));
}

[Fact]
public void TestJsonEvaluatorDynamicBoolEvaluationWithUnexpectedVariantType()
{
var fixture = new Fixture();

var jsonEvaluator = new JsonEvaluator(fixture.Create<string>());

jsonEvaluator.Sync(FlagConfigurationUpdateType.ALL, Utils.flags);

var builder = EvaluationContext.Builder();

Assert.Throws<FeatureProviderException>(() => jsonEvaluator.ResolveBooleanValue("targetingBoolFlagWithUnexpectedVariantType", false, builder.Build()));
}

[Fact]
public void TestJsonEvaluatorDynamicStringEvaluation()
{
Expand Down
51 changes: 50 additions & 1 deletion test/OpenFeature.Contrib.Providers.Flagd.Test/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class Utils
}";

public static string flags = @"{
""$evaluators"":{
""emailWithFaas"": {
""ends_with"": [{""var"":""email""}, ""@faas.com""]
}
},
""flags"": {
""staticBoolFlag"": {
""state"": ""ENABLED"",
Expand Down Expand Up @@ -149,7 +154,51 @@ public class Utils
]
}
},
""targetingStringFlag"": {
""targetingBoolFlagUsingSharedEvaluator"": {
""state"": ""ENABLED"",
""variants"": {
""bool1"": true,
""bool2"": false
},
""defaultVariant"": ""bool2"",
""targeting"": {
""if"": [{ $ref: ""emailWithFaas"" }, ""bool1""]
}
},
""targetingBoolFlagUsingSharedEvaluatorReturningBoolType"": {
""state"": ""ENABLED"",
""variants"": {
""true"": true,
""false"": false
},
""defaultVariant"": ""true"",
""targeting"": {
""if"": [{ $ref: ""emailWithFaas"" }, true]
}
},
""targetingBoolFlagWithMissingDefaultVariant"": {
""state"": ""ENABLED"",
""variants"": {
""bool1"": true,
""bool2"": false
},
""defaultVariant"": ""true"",
""targeting"": {
""if"": [{ $ref: ""emailWithFaas"" }, ""bool1""]
}
},
""targetingBoolFlagWithUnexpectedVariantType"": {
""state"": ""ENABLED"",
""variants"": {
""bool1"": 20,
""bool2"": 30
},
""defaultVariant"": ""true"",
""targeting"": {
""if"": [{ $ref: ""emailWithFaas"" }, ""bool1""]
}
},
""targetingStringFlag"": {
""state"": ""ENABLED"",
""variants"": {
""str1"": ""my-string"",
Expand Down
Loading