Skip to content

Commit

Permalink
add unit test for datagovernance
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-cat committed Oct 10, 2020
1 parent 2630454 commit d75e88a
Show file tree
Hide file tree
Showing 14 changed files with 509 additions and 101 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ notifications:
to:
- [email protected]
on_build_success: false
on_build_failure: false
on_build_failure: true
1 change: 1 addition & 0 deletions src/ConfigCat.Client.Tests/BaseUrlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace ConfigCat.Client.Tests
{
[TestCategory(TestCategories.Integration)]
[TestClass]
public class BaseUrlTests
{
Expand Down
1 change: 1 addition & 0 deletions src/ConfigCat.Client.Tests/ConfigCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace ConfigCat.Client.Tests
{
[TestCategory(TestCategories.Integration)]
[TestClass]
public class ConfigCacheTests
{
Expand Down
117 changes: 70 additions & 47 deletions src/ConfigCat.Client.Tests/ConfigCatClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using ConfigCat.Client.Cache;

namespace ConfigCat.Client.Tests
{

[TestClass]
public class ConfigCatClientTests
{
Mock<IConfigService> configService = new Mock<IConfigService>();
Mock<IConfigService> configServiceMock = new Mock<IConfigService>();
Mock<ILogger> loggerMock = new Mock<ILogger>();
Mock<IRolloutEvaluator> evaluateMock = new Mock<IRolloutEvaluator>();
Mock<IConfigDeserializer> deserializerMock = new Mock<IConfigDeserializer>();
Mock<IRolloutEvaluator> evaluatorMock = new Mock<IRolloutEvaluator>();
Mock<IConfigDeserializer> configDeserializerMock = new Mock<IConfigDeserializer>();

[TestInitialize]
public void TestInitialize()
{
configService.Reset();
configServiceMock.Reset();
loggerMock.Reset();
evaluateMock.Reset();
deserializerMock.Reset();
evaluatorMock.Reset();
configDeserializerMock.Reset();
}

[ExpectedException(typeof(ArgumentException))]
Expand Down Expand Up @@ -170,11 +171,11 @@ public void GetValue_ConfigServiceThrowException_ShouldReturnDefaultValue()

const string defaultValue = "Victory for the Firstborn!";

configService
configServiceMock
.Setup(m => m.GetConfigAsync())
.Throws<Exception>();

var client = new ConfigCatClient(configService.Object, loggerMock.Object, evaluateMock.Object, deserializerMock.Object);
var client = new ConfigCatClient(configServiceMock.Object, loggerMock.Object, evaluatorMock.Object, configDeserializerMock.Object);

// Act

Expand All @@ -192,11 +193,11 @@ public async Task GetValueAsync_ConfigServiceThrowException_ShouldReturnDefaultV

const string defaultValue = "Victory for the Firstborn!";

configService
configServiceMock
.Setup(m => m.GetConfigAsync())
.Throws<Exception>();

var client = new ConfigCatClient(configService.Object, loggerMock.Object, evaluateMock.Object, deserializerMock.Object);
var client = new ConfigCatClient(configServiceMock.Object, loggerMock.Object, evaluatorMock.Object, configDeserializerMock.Object);

// Act

Expand All @@ -214,11 +215,11 @@ public void GetValue_EvaluateServiceThrowException_ShouldReturnDefaultValue()

const string defaultValue = "Victory for the Firstborn!";

evaluateMock
evaluatorMock
.Setup(m => m.Evaluate(It.IsAny<ProjectConfig>(), It.IsAny<string>(), defaultValue, null))
.Throws<Exception>();

var client = new ConfigCatClient(configService.Object, loggerMock.Object, evaluateMock.Object, deserializerMock.Object);
var client = new ConfigCatClient(configServiceMock.Object, loggerMock.Object, evaluatorMock.Object, configDeserializerMock.Object);

// Act

Expand All @@ -236,11 +237,11 @@ public async Task GetValueAsync_EvaluateServiceThrowException_ShouldReturnDefaul

const string defaultValue = "Victory for the Firstborn!";

evaluateMock
evaluatorMock
.Setup(m => m.Evaluate(It.IsAny<ProjectConfig>(), It.IsAny<string>(), defaultValue, null))
.Throws<Exception>();

var client = new ConfigCatClient(configService.Object, loggerMock.Object, evaluateMock.Object, deserializerMock.Object);
var client = new ConfigCatClient(configServiceMock.Object, loggerMock.Object, evaluatorMock.Object, configDeserializerMock.Object);

// Act

Expand All @@ -256,11 +257,6 @@ public void GetAllKeys_ConfigServiceThrowException_ShouldReturnsWithEmptyArray()
{
// Arrange

var configServiceMock = new Mock<IConfigService>();
var loggerMock = new Mock<ILogger>();
var evaluatorMock = new Mock<IRolloutEvaluator>();
var configDeserializerMock = new Mock<IConfigDeserializer>();

configServiceMock.Setup(m => m.GetConfigAsync()).Throws<Exception>();

IConfigCatClient instance = new ConfigCatClient(
Expand All @@ -285,11 +281,6 @@ public void GetAllKeys_DeserializerThrowException_ShouldReturnsWithEmptyArray()
{
// Arrange

var configServiceMock = new Mock<IConfigService>();
var loggerMock = new Mock<ILogger>();
var evaluatorMock = new Mock<IRolloutEvaluator>();
var configDeserializerMock = new Mock<IConfigDeserializer>();

configServiceMock.Setup(m => m.GetConfigAsync()).ReturnsAsync(ProjectConfig.Empty);
IDictionary<string, Setting> o = new Dictionary<string, Setting>();
configDeserializerMock
Expand Down Expand Up @@ -318,11 +309,6 @@ public void GetAllKeys_DeserializeFailed_ShouldReturnsWithEmptyArray()
{
// Arrange

var configServiceMock = new Mock<IConfigService>();
var loggerMock = new Mock<ILogger>();
var evaluatorMock = new Mock<IRolloutEvaluator>();
var configDeserializerMock = new Mock<IConfigDeserializer>();

configServiceMock.Setup(m => m.GetConfigAsync()).ReturnsAsync(ProjectConfig.Empty);
IDictionary<string, Setting> o = new Dictionary<string, Setting>();
configDeserializerMock
Expand Down Expand Up @@ -353,11 +339,11 @@ public void GetVariationId_EvaluateServiceThrowException_ShouldReturnDefaultValu

const string defaultValue = "Victory for the Firstborn!";

evaluateMock
evaluatorMock
.Setup(m => m.EvaluateVariationId(It.IsAny<ProjectConfig>(), It.IsAny<string>(), defaultValue, null))
.Throws<Exception>();

var client = new ConfigCatClient(configService.Object, loggerMock.Object, evaluateMock.Object, deserializerMock.Object);
var client = new ConfigCatClient(configServiceMock.Object, loggerMock.Object, evaluatorMock.Object, configDeserializerMock.Object);

// Act

Expand All @@ -375,11 +361,11 @@ public async Task GetVariationIdAsync_EvaluateServiceThrowException_ShouldReturn

const string defaultValue = "Victory for the Firstborn!";

evaluateMock
evaluatorMock
.Setup(m => m.EvaluateVariationId(It.IsAny<ProjectConfig>(), It.IsAny<string>(), defaultValue, null))
.Throws<Exception>();

var client = new ConfigCatClient(configService.Object, loggerMock.Object, evaluateMock.Object, deserializerMock.Object);
var client = new ConfigCatClient(configServiceMock.Object, loggerMock.Object, evaluatorMock.Object, configDeserializerMock.Object);

// Act

Expand All @@ -395,11 +381,6 @@ public void GetVariationId_DeserializeFailed_ShouldReturnsWithEmptyArray()
{
// Arrange

var configServiceMock = new Mock<IConfigService>();
var loggerMock = new Mock<ILogger>();
var evaluatorMock = new Mock<IRolloutEvaluator>();
var configDeserializerMock = new Mock<IConfigDeserializer>();

configServiceMock.Setup(m => m.GetConfigAsync()).ReturnsAsync(ProjectConfig.Empty);
IDictionary<string, Setting> o = new Dictionary<string, Setting>();
configDeserializerMock
Expand Down Expand Up @@ -428,11 +409,6 @@ public async Task GetVariationIdAsync_DeserializeFailed_ShouldReturnsWithEmptyAr
{
// Arrange

var configServiceMock = new Mock<IConfigService>();
var loggerMock = new Mock<ILogger>();
var evaluatorMock = new Mock<IRolloutEvaluator>();
var configDeserializerMock = new Mock<IConfigDeserializer>();

configServiceMock.Setup(m => m.GetConfigAsync()).ReturnsAsync(ProjectConfig.Empty);
IDictionary<string, Setting> o = new Dictionary<string, Setting>();
configDeserializerMock
Expand Down Expand Up @@ -461,11 +437,11 @@ public void GetAllVariationId_ConfigServiceThrowException_ShouldReturnEmptyEnume
{
// Arrange

configService
configServiceMock
.Setup(m => m.GetConfigAsync())
.Throws<Exception>();

var client = new ConfigCatClient(configService.Object, loggerMock.Object, evaluateMock.Object, deserializerMock.Object);
var client = new ConfigCatClient(configServiceMock.Object, loggerMock.Object, evaluatorMock.Object, configDeserializerMock.Object);

// Act

Expand All @@ -481,11 +457,11 @@ public async Task GetAllVariationIdAsync_ConfigServiceThrowException_ShouldRetur
{
// Arrange

configService
configServiceMock
.Setup(m => m.GetConfigAsync())
.Throws<Exception>();

var client = new ConfigCatClient(configService.Object, loggerMock.Object, evaluateMock.Object, deserializerMock.Object);
var client = new ConfigCatClient(configServiceMock.Object, loggerMock.Object, evaluatorMock.Object, configDeserializerMock.Object);

// Act

Expand All @@ -495,5 +471,52 @@ public async Task GetAllVariationIdAsync_ConfigServiceThrowException_ShouldRetur

Assert.AreEqual(Enumerable.Empty<string>(), actual);
}

[TestMethod]
public void Dispose_ConfigServiceIsDisposable_ShouldInvokeDispose()
{
// Arrange

var myMock = new FakeConfigService(Mock.Of<IConfigFetcher>(), new CacheParameters(), Mock.Of<ILogger>());

IConfigCatClient instance = new ConfigCatClient(
myMock,
loggerMock.Object,
evaluatorMock.Object,
configDeserializerMock.Object);

// Act

instance.Dispose();

// Assert

Assert.AreEqual(1, myMock.DisposeCount);
}

internal class FakeConfigService : ConfigServiceBase, IConfigService
{
public byte DisposeCount { get; private set; }

public FakeConfigService(IConfigFetcher configFetcher, CacheParameters cacheParameters, ILogger log) : base(configFetcher, cacheParameters, log)
{
}

protected override void Dispose(bool disposing)
{
DisposeCount++;
base.Dispose(disposing);
}

public Task<ProjectConfig> GetConfigAsync()
{
return Task.FromResult(ProjectConfig.Empty);
}

public Task RefreshConfigAsync()
{
return Task.CompletedTask;
}
}
}
}
10 changes: 4 additions & 6 deletions src/ConfigCat.Client.Tests/ConfigServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ConfigServiceTests

ProjectConfig cachedPc = new ProjectConfig("CACHED", DateTime.UtcNow.Add(-defaultExpire), "67890");
ProjectConfig fetchedPc = new ProjectConfig("FETCHED", DateTime.UtcNow, "12345");

[TestInitialize]
public void TestInitialize()
{
Expand Down Expand Up @@ -290,7 +290,7 @@ public async Task AutoPollConfigService_RefreshConfigAsync_ConfigChanged_ShouldR

Assert.AreEqual(1, eventChanged);
}

[TestMethod]
public void AutoPollConfigService_Dispose_ShouldStopTimer()
{
Expand All @@ -310,7 +310,7 @@ public void AutoPollConfigService_Dispose_ShouldStopTimer()

var service = new AutoPollConfigService(
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object, CacheKey = ""},
new CacheParameters { ConfigCache = cacheMock.Object, CacheKey = "" },
TimeSpan.FromSeconds(0.2d),
TimeSpan.Zero,
loggerMock.Object,
Expand Down Expand Up @@ -489,6 +489,4 @@ public void ConfigService_WithNonDisposableConfigFetcher_DisposeShouldWork()
configService.Dispose();
}
}
}


}
1 change: 1 addition & 0 deletions src/ConfigCat.Client.Tests/CustomHttpClientHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ConfigCat.Client.Tests
{
[TestCategory(TestCategories.Integration)]
[TestClass]
public class CustomHttpClientHandlerTests
{
Expand Down
Loading

0 comments on commit d75e88a

Please sign in to comment.