Skip to content

Commit

Permalink
Add tests for comparison attribute and comparison value trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Jan 30, 2024
1 parent 37c7743 commit 9f21d86
Show file tree
Hide file tree
Showing 3 changed files with 1,882 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/ConfigCat.Client.Tests/ConfigV2EvaluationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,124 @@ public void UserObjectAttributeValueConversion_NonTextComparisons_Test(string sd

Assert.AreEqual(expectedReturnValue, evaluationDetails.Value);
}

[DataTestMethod]
[DataRow("isoneof", "no trim")]
[DataRow("isnotoneof", "no trim")]
[DataRow("isoneofhashed", "no trim")]
[DataRow("isnotoneofhashed", "no trim")]
[DataRow("equalshashed", "no trim")]
[DataRow("notequalshashed", "no trim")]
[DataRow("arraycontainsanyofhashed", "no trim")]
[DataRow("arraynotcontainsanyofhashed", "no trim")]
[DataRow("equals", "no trim")]
[DataRow("notequals", "no trim")]
[DataRow("startwithanyof", "no trim")]
[DataRow("notstartwithanyof", "no trim")]
[DataRow("endswithanyof", "no trim")]
[DataRow("notendswithanyof", "no trim")]
[DataRow("arraycontainsanyof", "no trim")]
[DataRow("arraynotcontainsanyof", "no trim")]
[DataRow("startwithanyofhashed", "no trim")]
[DataRow("notstartwithanyofhashed", "no trim")]
[DataRow("endswithanyofhashed", "no trim")]
[DataRow("notendswithanyofhashed", "no trim")]
//semver comparators user values trimmed because of backward compatibility
[DataRow("semverisoneof", "4 trim")]
[DataRow("semverisnotoneof", "5 trim")]
[DataRow("semverless", "6 trim")]
[DataRow("semverlessequals", "7 trim")]
[DataRow("semvergreater", "8 trim")]
[DataRow("semvergreaterequals", "9 trim")]
//number and date comparators user values trimmed because of backward compatibility
[DataRow("numberequals", "10 trim")]
[DataRow("numbernotequals", "11 trim")]
[DataRow("numberless", "12 trim")]
[DataRow("numberlessequals", "13 trim")]
[DataRow("numbergreater", "14 trim")]
[DataRow("numbergreaterequals", "15 trim")]
[DataRow("datebefore", "18 trim")]
[DataRow("dateafter", "19 trim")]
//"contains any of" and "not contains any of" is a special case, the not trimmed user attribute checked against not trimmed comparator values.
[DataRow("containsanyof", "no trim")]
[DataRow("notcontainsanyof", "no trim")]
public void ComparisonAttributeTrimming_Test(string key, string expectedReturnValue)
{
var config = new ConfigLocation.LocalFile("data", "comparison_attribute_trimming.json").FetchConfig();

var logger = new Mock<IConfigCatLogger>().Object.AsWrapper();
var evaluator = new RolloutEvaluator(logger);

var user = new User(" 12345 ")
{
Country = "[\" USA \"]",
Custom =
{
["Version"] = " 1.0.0 ",
["Number"] = " 3 ",
["Date"] = " 1705253400 "
}
};

const string defaultValue = "default";
var actualReturnValue = evaluator.Evaluate(config!.Settings, key, defaultValue, user, remoteConfig: null, logger).Value;

Assert.AreEqual(expectedReturnValue, actualReturnValue);
}

[DataTestMethod]
[DataRow("isoneof", "no trim")]
[DataRow("isnotoneof", "no trim")]
[DataRow("containsanyof", "no trim")]
[DataRow("notcontainsanyof", "no trim")]
[DataRow("isoneofhashed", "no trim")]
[DataRow("isnotoneofhashed", "no trim")]
[DataRow("equalshashed", "no trim")]
[DataRow("notequalshashed", "no trim")]
[DataRow("arraycontainsanyofhashed", "no trim")]
[DataRow("arraynotcontainsanyofhashed", "no trim")]
[DataRow("equals", "no trim")]
[DataRow("notequals", "no trim")]
[DataRow("startwithanyof", "no trim")]
[DataRow("notstartwithanyof", "no trim")]
[DataRow("endswithanyof", "no trim")]
[DataRow("notendswithanyof", "no trim")]
[DataRow("arraycontainsanyof", "no trim")]
[DataRow("arraynotcontainsanyof", "no trim")]
[DataRow("startwithanyofhashed", "default")]
[DataRow("notstartwithanyofhashed", "default")]
[DataRow("endswithanyofhashed", "default")]
[DataRow("notendswithanyofhashed", "default")]
//semver comparator values trimmed because of backward compatibility
[DataRow("semverisoneof", "4 trim")]
[DataRow("semverisnotoneof", "5 trim")]
[DataRow("semverless", "6 trim")]
[DataRow("semverlessequals", "7 trim")]
[DataRow("semvergreater", "8 trim")]
[DataRow("semvergreaterequals", "9 trim")]
public void ComparisonValueTrimming_Test(string key, string expectedReturnValue)
{
var config = new ConfigLocation.LocalFile("data", "comparison_value_trimming.json").FetchConfig();

var logger = new Mock<IConfigCatLogger>().Object.AsWrapper();
var evaluator = new RolloutEvaluator(logger);

var user = new User("12345")
{
Country = "[\"USA\"]",
Custom =
{
["Version"] = "1.0.0",
["Number"] = "3",
["Date"] = "1705253400"
}
};

const string defaultValue = "default";
string actualReturnValue;
try { actualReturnValue = evaluator.Evaluate(config!.Settings, key, defaultValue, user, remoteConfig: null, logger).Value; }
catch (InvalidOperationException) { actualReturnValue = defaultValue; }

Assert.AreEqual(expectedReturnValue, actualReturnValue);
}
}
Loading

0 comments on commit 9f21d86

Please sign in to comment.