-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathObjectCoderTests.cs
More file actions
122 lines (105 loc) · 5.2 KB
/
ObjectCoderTests.cs
File metadata and controls
122 lines (105 loc) · 5.2 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Parse;
using Parse.Abstractions.Infrastructure.Control;
using Parse.Abstractions.Infrastructure.Data;
using Parse.Abstractions.Infrastructure.Execution;
using Parse.Abstractions.Infrastructure;
using Parse.Abstractions.Platform.Objects;
using Parse.Infrastructure;
using Parse.Infrastructure.Data;
using Parse.Infrastructure.Execution;
using Parse.Platform.Objects;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
[TestClass]
public class ObjectCoderTests
{
[TestMethod]
public void TestACLCoding()
{
// Prepare the mock service hub
var serviceHub = new ServiceHub(); // Mock or actual implementation depending on your setup
// Decode the ACL from a dictionary
MutableObjectState state = (MutableObjectState) ParseObjectCoder.Instance.Decode(new Dictionary<string, object>
{
["ACL"] = new Dictionary<string, object>
{
["ACL"] = new Dictionary<string, object>
{
["3KmCvT7Zsb"] = new Dictionary<string, object>
{
["read"] = true,
["write"] = true
},
["*"] = new Dictionary<string, object> { ["read"] = true }
}
}
}, default, serviceHub);
// Check that the ACL was properly decoded
ParseACL resultACL = state.ServerData["ACL"] as ParseACL;
Debug.WriteLine(resultACL is null);
// Assertions
Assert.IsTrue(state.ContainsKey("ACL"));
Assert.IsNotNull(resultACL);
Assert.IsTrue(resultACL.PublicReadAccess);
Assert.IsFalse(resultACL.PublicWriteAccess);
Assert.IsTrue(resultACL.GetWriteAccess("3KmCvT7Zsb"));
Assert.IsTrue(resultACL.GetReadAccess("3KmCvT7Zsb"));
Assert.IsFalse(resultACL.GetWriteAccess("*"));
Assert.IsTrue(resultACL.GetReadAccess("*"));
}
public async Task FetchAsync_FetchesCorrectly() // Mock difficulty: 3
{
//Arrange
var mockCommandRunner = new Mock<IParseCommandRunner>();
var mockDecoder = new Mock<IParseDataDecoder>();
var mockServiceHub = new Mock<IServiceHub>();
var mockState = new Mock<IObjectState>();
mockState.Setup(x => x.ClassName).Returns("TestClass");
mockState.Setup(x => x.ObjectId).Returns("testId");
mockDecoder.Setup(x => x.Decode(It.IsAny<IDictionary<string, object>>(), It.IsAny<IServiceHub>())).Returns(mockState.Object);
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>()));
ParseObjectController controller = new ParseObjectController(mockCommandRunner.Object, mockDecoder.Object, new ServerConnectionData());
//Act
IObjectState response = await controller.FetchAsync(mockState.Object, "session", mockServiceHub.Object);
//Assert
mockCommandRunner.Verify(x => x.RunCommandAsync(It.IsAny<ParseCommand>(), null, null, It.IsAny<CancellationToken>()), Times.Once);
Assert.AreEqual(response, mockState.Object);
}
[TestMethod]
[Description("Tests DeleteAsync correctly deletes a ParseObject.")]
public async Task DeleteAsync_DeletesCorrectly() // Mock difficulty: 3
{
//Arrange
var mockCommandRunner = new Mock<IParseCommandRunner>();
var mockDecoder = new Mock<IParseDataDecoder>();
var mockServiceHub = new Mock<IServiceHub>();
var mockState = new Mock<IObjectState>();
mockState.Setup(x => x.ClassName).Returns("test");
mockState.Setup(x => x.ObjectId).Returns("testId");
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>()));
ParseObjectController controller = new ParseObjectController(mockCommandRunner.Object, mockDecoder.Object, new ServerConnectionData());
//Act
await controller.DeleteAsync(mockState.Object, "session");
//Assert
mockCommandRunner.Verify(x => x.RunCommandAsync(It.IsAny<ParseCommand>(), null, null, It.IsAny<CancellationToken>()), Times.Once);
}
[TestMethod]
[Description("Tests that ExecuteBatchRequests correctly handles empty list.")]
public void ExecuteBatchRequests_EmptyList()
{
var mockCommandRunner = new Mock<IParseCommandRunner>();
var mockDecoder = new Mock<IParseDataDecoder>();
var mockServiceHub = new Mock<IServiceHub>();
ParseObjectController controller = new ParseObjectController(mockCommandRunner.Object, mockDecoder.Object, new ServerConnectionData());
IList<ParseCommand> emptyList = new List<ParseCommand>();
var task = controller.ExecuteBatchRequests(emptyList, "session", CancellationToken.None);
Assert.AreEqual(0, task.Count);
}
}