Skip to content

Commit 943a0ef

Browse files
committed
Bug Fix & Enhancements
Did some general enhancements on the code structure it's more consistent (Working on `object` types are cool but a pain to debug since you never know the real type until you hit an exception.) I tried to make it so the code is more readable as well. Closes #397
1 parent 400f4fe commit 943a0ef

11 files changed

+708
-190
lines changed

Parse.Tests/ConfigTests.cs

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
56
using Moq;
67
using Newtonsoft.Json;
78
using Parse.Abstractions.Infrastructure;
9+
using Parse.Abstractions.Infrastructure.Data;
10+
using Parse.Abstractions.Infrastructure.Execution;
811
using Parse.Abstractions.Platform.Configuration;
912
using Parse.Abstractions.Platform.Users;
1013
using Parse.Infrastructure;
14+
using Parse.Infrastructure.Execution;
1115
using Parse.Platform.Configuration;
1216

1317
namespace Parse.Tests
@@ -61,7 +65,8 @@ public void SetUp() =>
6165
public void TearDown() => ((Client.Services as OrchestrationServiceHub).Default as ServiceHub).Reset();
6266

6367
[TestMethod]
64-
public async void TestCurrentConfig()
68+
[Description("Tests TestCurrentConfig Returns the right config")]
69+
public async Task TestCurrentConfig()// Mock difficulty: 1
6570
{
6671
var config = await Client.GetCurrentConfiguration();
6772

@@ -70,7 +75,8 @@ public async void TestCurrentConfig()
7075
}
7176

7277
[TestMethod]
73-
public async void TestToJSON()
78+
[Description("Tests the conversion of properties to json objects")]
79+
public async Task TestToJSON() // Mock difficulty: 1
7480
{
7581
var expectedJson = new Dictionary<string, object>
7682
{
@@ -81,8 +87,10 @@ public async void TestToJSON()
8187
Assert.AreEqual(JsonConvert.SerializeObject(expectedJson), JsonConvert.SerializeObject(actualJson));
8288
}
8389

90+
8491
[TestMethod]
85-
public async Task TestGetConfigAsync()
92+
[Description("Tests the fetching of a new config with an IServiceHub instance.")]
93+
public async Task TestGetConfigAsync()// Mock difficulty: 1
8694
{
8795
var config = await Client.GetConfigurationAsync();
8896

@@ -91,7 +99,8 @@ public async Task TestGetConfigAsync()
9199
}
92100

93101
[TestMethod]
94-
public async Task TestGetConfigCancelAsync()
102+
[Description("Tests fetching of config is cancelled when requested via a cancellation token.")]
103+
public async Task TestGetConfigCancelAsync() // Mock difficulty: 1
95104
{
96105
var tokenSource = new CancellationTokenSource();
97106
tokenSource.Cancel();
@@ -102,4 +111,37 @@ await Assert.ThrowsExceptionAsync<TaskCanceledException>(async () =>
102111
});
103112
}
104113
}
105-
}
114+
[TestClass]
115+
public class ParseConfigurationTests
116+
{
117+
118+
[TestMethod]
119+
[Description("Tests that Get method throws an exception if key is not found")]
120+
public void Get_ThrowsExceptionNotFound() // Mock difficulty: 1
121+
{
122+
var services = new Mock<IServiceHub>().Object;
123+
ParseConfiguration configuration = new(services);
124+
Assert.ThrowsException<KeyNotFoundException>(() => configuration.Get<string>("doesNotExist"));
125+
}
126+
127+
128+
[TestMethod]
129+
[Description("Tests that create function creates correct configuration object")]
130+
public void Create_BuildsConfigurationFromDictionary() // Mock difficulty: 3
131+
{
132+
var mockDecoder = new Mock<IParseDataDecoder>();
133+
var mockServices = new Mock<IServiceHub>();
134+
var dict = new Dictionary<string, object>
135+
{
136+
["params"] = new Dictionary<string, object> { { "test", 1 } },
137+
};
138+
mockDecoder.Setup(d => d.Decode(It.IsAny<object>(), It.IsAny<IServiceHub>())).Returns(new Dictionary<string, object> { { "test", 1 } });
139+
140+
var config = ParseConfiguration.Create(dict, mockDecoder.Object, mockServices.Object);
141+
Assert.AreEqual(1, config["test"]);
142+
Assert.IsInstanceOfType(config, typeof(ParseConfiguration));
143+
}
144+
}
145+
146+
147+
}

Parse.Tests/InstallationTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public void TestChannelGetterSetter()
234234
}
235235

236236
[TestMethod]
237-
public async void TestGetCurrentInstallation()
237+
public async Task TestGetCurrentInstallation()
238238
{
239239
var guid = Guid.NewGuid();
240240
var expectedInstallation = new ParseInstallation();

Parse.Tests/ObjectCoderTests.cs

+74-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Moq;
23
using Parse;
4+
using Parse.Abstractions.Infrastructure.Control;
5+
using Parse.Abstractions.Infrastructure.Data;
6+
using Parse.Abstractions.Infrastructure.Execution;
7+
using Parse.Abstractions.Infrastructure;
8+
using Parse.Abstractions.Platform.Objects;
39
using Parse.Infrastructure;
410
using Parse.Infrastructure.Data;
11+
using Parse.Infrastructure.Execution;
512
using Parse.Platform.Objects;
13+
using System;
614
using System.Collections.Generic;
715
using System.Diagnostics;
16+
using System.Net.Http;
17+
using System.Net;
18+
using System.Threading.Tasks;
19+
using System.Threading;
820

921
[TestClass]
1022
public class ObjectCoderTests
@@ -14,7 +26,7 @@ public void TestACLCoding()
1426
{
1527
// Prepare the mock service hub
1628
var serviceHub = new ServiceHub(); // Mock or actual implementation depending on your setup
17-
29+
1830
// Decode the ACL from a dictionary
1931
MutableObjectState state = (MutableObjectState) ParseObjectCoder.Instance.Decode(new Dictionary<string, object>
2032
{
@@ -46,4 +58,65 @@ public void TestACLCoding()
4658
Assert.IsFalse(resultACL.GetWriteAccess("*"));
4759
Assert.IsTrue(resultACL.GetReadAccess("*"));
4860
}
61+
62+
public async Task FetchAsync_FetchesCorrectly() // Mock difficulty: 3
63+
{
64+
//Arrange
65+
var mockCommandRunner = new Mock<IParseCommandRunner>();
66+
var mockDecoder = new Mock<IParseDataDecoder>();
67+
var mockServiceHub = new Mock<IServiceHub>();
68+
var mockState = new Mock<IObjectState>();
69+
mockState.Setup(x => x.ClassName).Returns("TestClass");
70+
mockState.Setup(x => x.ObjectId).Returns("testId");
71+
72+
mockDecoder.Setup(x => x.Decode(It.IsAny<IDictionary<string, object>>(), It.IsAny<IServiceHub>())).Returns(mockState.Object);
73+
mockCommandRunner.Setup(c => c.RunCommandAsync(It.IsAny<ParseCommand>(), null, null, It.IsAny<CancellationToken>())).ReturnsAsync(new Tuple<HttpStatusCode, IDictionary<string, object>>(System.Net.HttpStatusCode.OK, new Dictionary<string, object>()));
74+
75+
ParseObjectController controller = new ParseObjectController(mockCommandRunner.Object, mockDecoder.Object, new ServerConnectionData());
76+
//Act
77+
IObjectState response = await controller.FetchAsync(mockState.Object, "session", mockServiceHub.Object);
78+
79+
//Assert
80+
mockCommandRunner.Verify(x => x.RunCommandAsync(It.IsAny<ParseCommand>(), null, null, It.IsAny<CancellationToken>()), Times.Once);
81+
Assert.AreEqual(response, mockState.Object);
82+
}
83+
84+
[TestMethod]
85+
[Description("Tests DeleteAsync correctly deletes a ParseObject.")]
86+
public async Task DeleteAsync_DeletesCorrectly() // Mock difficulty: 3
87+
{
88+
//Arrange
89+
var mockCommandRunner = new Mock<IParseCommandRunner>();
90+
var mockDecoder = new Mock<IParseDataDecoder>();
91+
var mockServiceHub = new Mock<IServiceHub>();
92+
var mockState = new Mock<IObjectState>();
93+
mockState.Setup(x => x.ClassName).Returns("test");
94+
mockState.Setup(x => x.ObjectId).Returns("testId");
95+
96+
mockCommandRunner.Setup(c => c.RunCommandAsync(It.IsAny<ParseCommand>(), null, null, It.IsAny<CancellationToken>())).ReturnsAsync(new Tuple<HttpStatusCode, IDictionary<string, object>>(System.Net.HttpStatusCode.OK, new Dictionary<string, object>()));
97+
ParseObjectController controller = new ParseObjectController(mockCommandRunner.Object, mockDecoder.Object, new ServerConnectionData());
98+
99+
//Act
100+
await controller.DeleteAsync(mockState.Object, "session");
101+
102+
//Assert
103+
mockCommandRunner.Verify(x => x.RunCommandAsync(It.IsAny<ParseCommand>(), null, null, It.IsAny<CancellationToken>()), Times.Once);
104+
105+
}
106+
107+
[TestMethod]
108+
[Description("Tests that ExecuteBatchRequests correctly handles empty list.")]
109+
public void ExecuteBatchRequests_EmptyList()
110+
{
111+
var mockCommandRunner = new Mock<IParseCommandRunner>();
112+
var mockDecoder = new Mock<IParseDataDecoder>();
113+
var mockServiceHub = new Mock<IServiceHub>();
114+
ParseObjectController controller = new ParseObjectController(mockCommandRunner.Object, mockDecoder.Object, new ServerConnectionData());
115+
IList<ParseCommand> emptyList = new List<ParseCommand>();
116+
117+
var task = controller.ExecuteBatchRequests(emptyList, "session", CancellationToken.None);
118+
119+
Assert.AreEqual(0, task.Count);
120+
121+
}
49122
}

Parse.Tests/ObjectControllerTests.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ namespace Parse.Tests;
1717
[TestClass]
1818
public class ObjectControllerTests
1919
{
20-
private ParseClient Client { get; set; }
2120

21+
private ParseClient Client { get; set; }
2222
[TestInitialize]
23-
public void SetUp() => Client = new ParseClient(new ServerConnectionData { ApplicationID = "", Key = "", Test = true });
23+
public void SetUp()
24+
{
25+
// Initialize the client and ensure the instance is set
26+
Client = new ParseClient(new ServerConnectionData { Test = true, ApplicationID = "", Key = "" });
27+
Client.Publicize();
28+
}
29+
[TestCleanup]
30+
public void TearDown() => (Client.Services as ServiceHub).Reset();
31+
2432

2533
[TestMethod]
2634
public async Task TestFetchAsync()

0 commit comments

Comments
 (0)