Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Apr 15, 2024
1 parent 0bab940 commit b24c576
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 6 deletions.
65 changes: 64 additions & 1 deletion src/ConfigCat.Client.Tests/ConfigCatClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public async Task GetValueAsync_EvaluateServiceThrowException_ShouldReturnDefaul
[DataRow(false)]
[DataRow(true)]
[DataTestMethod]
public async Task GetValueDetails_ShouldReturnCorrectEvaluationDetails_SettingIsNotAvailable(bool isAsync)
public async Task GetValueDetails_ShouldReturnCorrectEvaluationDetails_ConfigJsonIsNotAvailable(bool isAsync)
{
// Arrange

Expand Down Expand Up @@ -284,6 +284,62 @@ public async Task GetValueDetails_ShouldReturnCorrectEvaluationDetails_SettingIs
Assert.IsNull(actual.VariationId);
Assert.AreEqual(DateTime.MinValue, actual.FetchTime);
Assert.AreSame(user, actual.User);
Assert.AreEqual(EvaluationErrorCode.ConfigJsonNotAvailable, actual.ErrorCode);
Assert.IsNotNull(actual.ErrorMessage);
Assert.IsNull(actual.ErrorException);
Assert.IsNull(actual.MatchedTargetingRule);
Assert.IsNull(actual.MatchedPercentageOption);
}

[DataRow(false)]
[DataRow(true)]
[DataTestMethod]
public async Task GetValueDetails_ShouldReturnCorrectEvaluationDetails_SettingIsMissing(bool isAsync)
{
// Arrange

const string key = "does-not-exist";
const bool defaultValue = false;

const string cacheKey = "123";
var configJsonFilePath = Path.Combine("data", "sample_variationid_v5.json");
var timeStamp = ProjectConfig.GenerateTimeStamp();

var client = CreateClientWithMockedFetcher(cacheKey, this.loggerMock, this.fetcherMock,
onFetch: _ => FetchResult.Success(ConfigHelper.FromFile(configJsonFilePath, httpETag: "12345", timeStamp)),
configServiceFactory: (fetcher, cacheParams, loggerWrapper) =>
{
return new ManualPollConfigService(this.fetcherMock.Object, cacheParams, loggerWrapper);
},
out var configService, out _);

if (isAsync)
{
await client.ForceRefreshAsync();
}
else
{
client.ForceRefresh();
}

var user = new User("[email protected]") { Email = "[email protected]" };

// Act

var actual = isAsync
? await client.GetValueDetailsAsync(key, defaultValue, user)
: client.GetValueDetails(key, defaultValue, user);

// Assert

Assert.IsNotNull(actual);
Assert.AreEqual(key, actual.Key);
Assert.AreEqual(defaultValue, actual.Value);
Assert.IsTrue(actual.IsDefaultValue);
Assert.IsNull(actual.VariationId);
Assert.AreEqual(timeStamp, actual.FetchTime);
Assert.AreSame(user, actual.User);
Assert.AreEqual(EvaluationErrorCode.SettingKeyMissing, actual.ErrorCode);
Assert.IsNotNull(actual.ErrorMessage);
Assert.IsNull(actual.ErrorException);
Assert.IsNull(actual.MatchedTargetingRule);
Expand Down Expand Up @@ -336,6 +392,7 @@ public async Task GetValueDetails_ShouldReturnCorrectEvaluationDetails_SettingIs
Assert.AreEqual("a0e56eda", actual.VariationId);
Assert.AreEqual(timeStamp, actual.FetchTime);
Assert.IsNull(actual.User);
Assert.AreEqual(EvaluationErrorCode.None, actual.ErrorCode);
Assert.IsNull(actual.ErrorMessage);
Assert.IsNull(actual.ErrorException);
Assert.IsNull(actual.MatchedTargetingRule);
Expand Down Expand Up @@ -390,6 +447,7 @@ public async Task GetValueDetails_ShouldReturnCorrectEvaluationDetails_SettingIs
Assert.AreEqual("67787ae4", actual.VariationId);
Assert.AreEqual(timeStamp, actual.FetchTime);
Assert.AreSame(user, actual.User);
Assert.AreEqual(EvaluationErrorCode.None, actual.ErrorCode);
Assert.IsNull(actual.ErrorMessage);
Assert.IsNull(actual.ErrorException);
Assert.IsNotNull(actual.MatchedTargetingRule);
Expand Down Expand Up @@ -444,6 +502,7 @@ public async Task GetValueDetails_ShouldReturnCorrectEvaluationDetails_SettingIs
Assert.AreEqual("67787ae4", actual.VariationId);
Assert.AreEqual(timeStamp, actual.FetchTime);
Assert.AreSame(user, actual.User);
Assert.AreEqual(EvaluationErrorCode.None, actual.ErrorCode);
Assert.IsNull(actual.ErrorMessage);
Assert.IsNull(actual.ErrorException);
Assert.IsNull(actual.MatchedTargetingRule);
Expand Down Expand Up @@ -487,6 +546,7 @@ public async Task GetValueDetails_ConfigServiceThrowException_ShouldReturnDefaul
Assert.IsNull(actual.VariationId);
Assert.AreEqual(DateTime.MinValue, actual.FetchTime);
Assert.IsNull(actual.User);
Assert.AreEqual(EvaluationErrorCode.UnexpectedError, actual.ErrorCode);
Assert.AreEqual(errorMessage, actual.ErrorMessage);
Assert.IsInstanceOfType(actual.ErrorException, typeof(ApplicationException));
Assert.IsNull(actual.MatchedTargetingRule);
Expand Down Expand Up @@ -550,6 +610,7 @@ public async Task GetValueDetails_EvaluateServiceThrowException_ShouldReturnDefa
Assert.IsNull(actual.VariationId);
Assert.AreEqual(timeStamp, actual.FetchTime);
Assert.AreSame(user, actual.User);
Assert.AreEqual(EvaluationErrorCode.UnexpectedError, actual.ErrorCode);
Assert.AreEqual(errorMessage, actual.ErrorMessage);
Assert.IsInstanceOfType(actual.ErrorException, typeof(ApplicationException));
Assert.IsNull(actual.MatchedTargetingRule);
Expand Down Expand Up @@ -619,6 +680,7 @@ public async Task GetAllValueDetails_ShouldReturnCorrectEvaluationDetails(bool i
Assert.AreEqual(expectedItem.VariationId, actualDetails.VariationId);
Assert.AreEqual(timeStamp, actualDetails.FetchTime);
Assert.AreSame(user, actualDetails.User);
Assert.AreEqual(EvaluationErrorCode.None, actualDetails.ErrorCode);
Assert.IsNull(actualDetails.ErrorMessage);
Assert.IsNull(actualDetails.ErrorException);
Assert.IsNotNull(actualDetails.MatchedTargetingRule);
Expand Down Expand Up @@ -755,6 +817,7 @@ public async Task GetAllValueDetails_EvaluateServiceThrowException_ShouldReturnD
Assert.IsNull(actualDetails.VariationId);
Assert.AreEqual(timeStamp, actualDetails.FetchTime);
Assert.AreSame(user, actualDetails.User);
Assert.AreEqual(EvaluationErrorCode.UnexpectedError, actualDetails.ErrorCode);
Assert.AreEqual(errorMessage, actualDetails.ErrorMessage);
Assert.IsInstanceOfType(actualDetails.ErrorException, typeof(ApplicationException));
Assert.IsNull(actualDetails.MatchedTargetingRule);
Expand Down
60 changes: 55 additions & 5 deletions src/ConfigCat.Client.Tests/OverrideTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ConfigCat.Client.Evaluation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

#if USE_NEWTONSOFT_JSON
using JsonValue = Newtonsoft.Json.Linq.JValue;
Expand Down Expand Up @@ -576,14 +576,37 @@ public void OverrideValueTypeMismatchShouldBeHandledCorrectly_Dictionary(object
options.FlagOverrides = FlagOverrides.LocalDictionary(dictionary, OverrideBehaviour.LocalOnly);
});

var method = typeof(IConfigCatClient).GetMethod(nameof(IConfigCatClient.GetValue))!
var method = typeof(IConfigCatClient).GetMethod(nameof(IConfigCatClient.GetValueDetails))!
.GetGenericMethodDefinition()
.MakeGenericMethod(defaultValue.GetType());

var actualEvaluatedValue = method.Invoke(client, new[] { key, defaultValue, null });
var actualEvaluatedValueDetails = (EvaluationDetails)method.Invoke(client, new[] { key, defaultValue, null })!;
var actualEvaluatedValue = actualEvaluatedValueDetails.Value;
var actualEvaluatedValues = client.GetAllValues(user: null);

Assert.AreEqual(expectedEvaluatedValue, actualEvaluatedValue);
if (!defaultValue.Equals(expectedEvaluatedValue))
{
Assert.IsFalse(actualEvaluatedValueDetails.IsDefaultValue);
Assert.AreEqual(EvaluationErrorCode.None, actualEvaluatedValueDetails.ErrorCode);
Assert.IsNull(actualEvaluatedValueDetails.ErrorMessage);
Assert.IsNull(actualEvaluatedValueDetails.ErrorException);
}
else
{
Assert.IsTrue(actualEvaluatedValueDetails.IsDefaultValue);
Assert.IsNotNull(actualEvaluatedValueDetails.ErrorMessage);
if (overrideValue.ToSettingValue(out _).HasUnsupportedValue)
{
Assert.AreEqual(EvaluationErrorCode.InvalidConfigModel, actualEvaluatedValueDetails.ErrorCode);
Assert.IsInstanceOfType(actualEvaluatedValueDetails.ErrorException, typeof(InvalidConfigModelException));
}
else
{
Assert.AreEqual(EvaluationErrorCode.SettingValueTypeMismatch, actualEvaluatedValueDetails.ErrorCode);
Assert.IsInstanceOfType(actualEvaluatedValueDetails.ErrorException, typeof(EvaluationErrorException));
}
}

overrideValue.ToSettingValue(out var overrideValueSettingType);
var expectedEvaluatedValues = new KeyValuePair<string, object?>[]
Expand Down Expand Up @@ -638,14 +661,41 @@ public void OverrideValueTypeMismatchShouldBeHandledCorrectly_SimplifiedConfig(s
options.FlagOverrides = FlagOverrides.LocalFile(filePath, autoReload: false, OverrideBehaviour.LocalOnly);
});

var method = typeof(IConfigCatClient).GetMethod(nameof(IConfigCatClient.GetValue))!
var method = typeof(IConfigCatClient).GetMethod(nameof(IConfigCatClient.GetValueDetails))!
.GetGenericMethodDefinition()
.MakeGenericMethod(defaultValue.GetType());

var actualEvaluatedValue = method.Invoke(client, new[] { key, defaultValue, null });
var actualEvaluatedValueDetails = (EvaluationDetails)method.Invoke(client, new[] { key, defaultValue, null })!;
var actualEvaluatedValue = actualEvaluatedValueDetails.Value;
var actualEvaluatedValues = client.GetAllValues(user: null);

Assert.AreEqual(expectedEvaluatedValue, actualEvaluatedValue);
if (!defaultValue.Equals(expectedEvaluatedValue))
{
Assert.IsFalse(actualEvaluatedValueDetails.IsDefaultValue);
Assert.AreEqual(EvaluationErrorCode.None, actualEvaluatedValueDetails.ErrorCode);
Assert.IsNull(actualEvaluatedValueDetails.ErrorMessage);
Assert.IsNull(actualEvaluatedValueDetails.ErrorException);
}
else
{
Assert.IsTrue(actualEvaluatedValueDetails.IsDefaultValue);
Assert.IsNotNull(actualEvaluatedValueDetails.ErrorMessage);
#if USE_NEWTONSOFT_JSON
if (overrideValue is not JsonValue overrideJsonValue || overrideJsonValue.ToSettingValue(out _).HasUnsupportedValue)
#else
if (overrideValue.ToSettingValue(out _).HasUnsupportedValue)
#endif
{
Assert.AreEqual(EvaluationErrorCode.InvalidConfigModel, actualEvaluatedValueDetails.ErrorCode);
Assert.IsInstanceOfType(actualEvaluatedValueDetails.ErrorException, typeof(InvalidConfigModelException));
}
else
{
Assert.AreEqual(EvaluationErrorCode.SettingValueTypeMismatch, actualEvaluatedValueDetails.ErrorCode);
Assert.IsInstanceOfType(actualEvaluatedValueDetails.ErrorException, typeof(EvaluationErrorException));
}
}

var unwrappedOverrideValue = overrideValue is JsonValue jsonValue
? jsonValue.ToSettingValue(out var overrideValueSettingType)
Expand Down

0 comments on commit b24c576

Please sign in to comment.