-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a way to mock ConfigCatClientSnapshot
- Loading branch information
Showing
4 changed files
with
197 additions
and
68 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/ConfigCat.Client.Tests/ConfigCatClientSnapshotTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using ConfigCat.Client.Cache; | ||
using ConfigCat.Client.ConfigService; | ||
using ConfigCat.Client.Configuration; | ||
using ConfigCat.Client.Evaluation; | ||
using ConfigCat.Client.Override; | ||
using ConfigCat.Client.Tests.Fakes; | ||
using ConfigCat.Client.Tests.Helpers; | ||
using ConfigCat.Client.Utils; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
|
||
namespace ConfigCat.Client.Tests; | ||
|
||
[TestClass] | ||
public class ConfigCatClientSnapshotTests | ||
{ | ||
[TestMethod] | ||
public void DefaultInstanceDoesNotThrow() | ||
{ | ||
const string key = "key"; | ||
const string defaultValue = ""; | ||
|
||
var snapshot = default(ConfigCatClientSnapshot); | ||
|
||
Assert.AreEqual(ClientCacheState.NoFlagData, snapshot.CacheState); | ||
Assert.IsNull(snapshot.FetchedConfig); | ||
CollectionAssert.AreEqual(ArrayUtils.EmptyArray<string>(), snapshot.GetAllKeys().ToArray()); | ||
Assert.AreEqual("", snapshot.GetValue(key, defaultValue)); | ||
var evaluationDetails = snapshot.GetValueDetails(key, defaultValue); | ||
Assert.IsNotNull(evaluationDetails); | ||
Assert.AreEqual(key, evaluationDetails.Key); | ||
Assert.AreEqual(defaultValue, evaluationDetails.Value); | ||
Assert.IsTrue(evaluationDetails.IsDefaultValue); | ||
Assert.IsNotNull(evaluationDetails.ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void CanMockSnapshot() | ||
{ | ||
const ClientCacheState cacheState = ClientCacheState.HasUpToDateFlagData; | ||
var fetchedConfig = new Config(); | ||
var keys = new[] { "key1", "key2" }; | ||
var evaluationDetails = new EvaluationDetails<string>("key1", "value"); | ||
|
||
var mock = new Mock<IConfigCatClientSnapshot>(); | ||
mock.SetupGet(m => m.CacheState).Returns(cacheState); | ||
mock.SetupGet(m => m.FetchedConfig).Returns(fetchedConfig); | ||
mock.Setup(m => m.GetAllKeys()).Returns(keys); | ||
mock.Setup(m => m.GetValue(evaluationDetails.Key, It.IsAny<string>(), It.IsAny<User?>())).Returns(evaluationDetails.Value); | ||
mock.Setup(m => m.GetValueDetails(evaluationDetails.Key, It.IsAny<string>(), It.IsAny<User?>())).Returns(evaluationDetails); | ||
|
||
var snapshot = new ConfigCatClientSnapshot(mock.Object); | ||
|
||
Assert.AreEqual(cacheState, snapshot.CacheState); | ||
Assert.AreEqual(fetchedConfig, snapshot.FetchedConfig); | ||
CollectionAssert.AreEqual(keys, snapshot.GetAllKeys().ToArray()); | ||
Assert.AreEqual(evaluationDetails.Value, snapshot.GetValue(evaluationDetails.Key, "")); | ||
Assert.AreSame(evaluationDetails, snapshot.GetValueDetails(evaluationDetails.Key, "")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace ConfigCat.Client; | ||
|
||
/// <summary> | ||
/// Defines the public interface of the <see cref="ConfigCatClientSnapshot"/> struct. | ||
/// </summary> | ||
public interface IConfigCatClientSnapshot | ||
{ | ||
/// <summary> | ||
/// The state of the local cache at the time the snapshot was created. | ||
/// </summary> | ||
ClientCacheState CacheState { get; } | ||
|
||
/// <summary> | ||
/// The latest config which has been fetched from the remote server. | ||
/// </summary> | ||
IConfig? FetchedConfig { get; } | ||
|
||
/// <summary> | ||
/// Returns the available setting keys. | ||
/// </summary> | ||
/// <remarks> | ||
/// In case the client is configured to use flag override, this will also include the keys provided by the flag override. | ||
/// </remarks> | ||
/// <returns>The collection of keys.</returns> | ||
IReadOnlyCollection<string> GetAllKeys(); | ||
|
||
/// <summary> | ||
/// Returns the value of a feature flag or setting identified by <paramref name="key"/> synchronously, based on the snapshot. | ||
/// </summary> | ||
/// <remarks> | ||
/// It is important to provide an argument for the <paramref name="defaultValue"/> parameter, specifically for the <typeparamref name="T"/> generic type parameter, | ||
/// that matches the type of the feature flag or setting you are evaluating.<br/> | ||
/// Please refer to <see href="https://configcat.com/docs/sdk-reference/dotnet/#setting-type-mapping">this table</see> for the corresponding types. | ||
/// </remarks> | ||
/// <typeparam name="T"> | ||
/// The type of the value. Only the following types are allowed: | ||
/// <see cref="string"/>, <see cref="bool"/>, <see cref="int"/>, <see cref="long"/>, <see cref="double"/> and <see cref="object"/> (both nullable and non-nullable).<br/> | ||
/// The type must correspond to the setting type, otherwise <paramref name="defaultValue"/> will be returned. | ||
/// </typeparam> | ||
/// <param name="key">Key of the feature flag or setting.</param> | ||
/// <param name="defaultValue">In case of failure, this value will be returned.</param> | ||
/// <param name="user">The User Object to use for evaluating targeting rules and percentage options.</param> | ||
/// <returns>The value of the feature flag or setting.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> | ||
/// <exception cref="ArgumentException"><paramref name="key"/> is an empty string.</exception> | ||
/// <exception cref="ArgumentException"><typeparamref name="T"/> is not an allowed type.</exception> | ||
T GetValue<T>(string key, T defaultValue, User? user = null); | ||
|
||
/// <summary> | ||
/// Returns the value along with evaluation details of a feature flag or setting identified by <paramref name="key"/> synchronously, based on the snapshot. | ||
/// </summary> | ||
/// <remarks> | ||
/// It is important to provide an argument for the <paramref name="defaultValue"/> parameter, specifically for the <typeparamref name="T"/> generic type parameter, | ||
/// that matches the type of the feature flag or setting you are evaluating.<br/> | ||
/// Please refer to <see href="https://configcat.com/docs/sdk-reference/dotnet/#setting-type-mapping">this table</see> for the corresponding types. | ||
/// </remarks> | ||
/// <typeparam name="T"> | ||
/// The type of the value. Only the following types are allowed: | ||
/// <see cref="string"/>, <see cref="bool"/>, <see cref="int"/>, <see cref="long"/>, <see cref="double"/> and <see cref="object"/> (both nullable and non-nullable).<br/> | ||
/// The type must correspond to the setting type, otherwise <paramref name="defaultValue"/> will be returned. | ||
/// </typeparam> | ||
/// <param name="key">Key of the feature flag or setting.</param> | ||
/// <param name="defaultValue">In case of failure, this value will be returned.</param> | ||
/// <param name="user">The User Object to use for evaluating targeting rules and percentage options.</param> | ||
/// <returns>The value along with the details of evaluation of the feature flag or setting.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> | ||
/// <exception cref="ArgumentException"><paramref name="key"/> is an empty string.</exception> | ||
/// <exception cref="ArgumentException"><typeparamref name="T"/> is not an allowed type.</exception> | ||
EvaluationDetails<T> GetValueDetails<T>(string key, T defaultValue, User? user = null); | ||
} |