-
-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathConfigTests.cs
More file actions
85 lines (68 loc) · 3.79 KB
/
Copy pathConfigTests.cs
File metadata and controls
85 lines (68 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json;
using Parse.Abstractions.Infrastructure;
using Parse.Abstractions.Platform.Configuration;
using Parse.Abstractions.Platform.Users;
using Parse.Infrastructure;
using Parse.Platform.Configuration;
namespace Parse.Tests
{
[TestClass]
public class ConfigTests
{
ParseClient Client { get; } = new ParseClient(new ServerConnectionData { Test = true }, new MutableServiceHub { });
IParseConfigurationController MockedConfigController
{
get
{
Mock<IParseConfigurationController> mockedConfigController = new Mock<IParseConfigurationController>();
Mock<IParseCurrentConfigurationController> mockedCurrentConfigController = new Mock<IParseCurrentConfigurationController>();
ParseConfiguration theConfig = Client.BuildConfiguration(new Dictionary<string, object> { ["params"] = new Dictionary<string, object> { ["testKey"] = "testValue" } });
mockedCurrentConfigController.Setup(obj => obj.GetCurrentConfigAsync(Client)).Returns(Task.FromResult(theConfig));
mockedConfigController.Setup(obj => obj.CurrentConfigurationController).Returns(mockedCurrentConfigController.Object);
TaskCompletionSource<ParseConfiguration> tcs = new TaskCompletionSource<ParseConfiguration>();
tcs.TrySetCanceled();
mockedConfigController.Setup(obj => obj.FetchConfigAsync(It.IsAny<string>(), It.IsAny<IServiceHub>(), It.Is<CancellationToken>(ct => ct.IsCancellationRequested))).Returns(tcs.Task);
mockedConfigController.Setup(obj => obj.FetchConfigAsync(It.IsAny<string>(), It.IsAny<IServiceHub>(), It.Is<CancellationToken>(ct => !ct.IsCancellationRequested))).Returns(Task.FromResult(theConfig));
return mockedConfigController.Object;
}
}
[TestInitialize]
public void SetUp() => (Client.Services as OrchestrationServiceHub).Custom = new MutableServiceHub { ConfigurationController = MockedConfigController, CurrentUserController = new Mock<IParseCurrentUserController>().Object };
[TestCleanup]
public void TearDown() => ((Client.Services as OrchestrationServiceHub).Default as ServiceHub).Reset();
[TestMethod]
public void TestCurrentConfig()
{
ParseConfiguration config = Client.GetCurrentConfiguration();
Assert.AreEqual("testValue", config["testKey"]);
Assert.AreEqual("testValue", config.Get<string>("testKey"));
}
[TestMethod]
public void TestToJSON()
{
IDictionary<string, object> expectedJson = new Dictionary<string, object> { { "params", new Dictionary<string, object> { { "testKey", "testValue" } } } };
Assert.AreEqual(JsonConvert.SerializeObject((Client.GetCurrentConfiguration() as IJsonConvertible).ConvertToJson()), JsonConvert.SerializeObject(expectedJson));
}
[TestMethod]
[AsyncStateMachine(typeof(ConfigTests))]
public Task TestGetConfig() => Client.GetConfigurationAsync().ContinueWith(task =>
{
Assert.AreEqual("testValue", task.Result["testKey"]);
Assert.AreEqual("testValue", task.Result.Get<string>("testKey"));
});
[TestMethod]
[AsyncStateMachine(typeof(ConfigTests))]
public Task TestGetConfigCancel()
{
CancellationTokenSource tokenSource = new CancellationTokenSource { };
tokenSource.Cancel();
return Client.GetConfigurationAsync(tokenSource.Token).ContinueWith(task => Assert.IsTrue(task.IsCanceled));
}
}
}