Skip to content

Commit b86a45a

Browse files
committed
Fine tuning some tests
1 parent a13c7ab commit b86a45a

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

Parse.Tests/LiveQueryMessageBuilderTests.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
5-
using Parse.Abstractions.Platform.Objects;
6+
using Moq;
7+
using Parse.Abstractions.Infrastructure;
8+
using Parse.Abstractions.Platform.Users;
69
using Parse.Infrastructure;
710
using Parse.Platform.LiveQueries;
811

@@ -11,12 +14,26 @@ namespace Parse.Tests;
1114
[TestClass]
1215
public class LiveQueryMessageBuilderTests
1316
{
14-
private ParseClient Client { get; } = new ParseClient(
15-
new ServerConnectionData { Test = true },
16-
new LiveQueryServerConnectionData { ApplicationID = "TestApp", Key = "t3stK3y", Test = true });
17+
private readonly Mock<IParseCurrentUserController> _mockCurrentUserController;
18+
private readonly MutableServiceHub _hub;
19+
20+
private ParseClient Client { get; }
1721

1822
public LiveQueryMessageBuilderTests()
1923
{
24+
_mockCurrentUserController = new Mock<IParseCurrentUserController>();
25+
26+
_mockCurrentUserController
27+
.Setup(controller => controller.GetCurrentSessionTokenAsync(It.IsAny<IServiceHub>(), It.IsAny<CancellationToken>()))
28+
.ReturnsAsync("s3ss!0nT0k3n");
29+
30+
_hub = new MutableServiceHub { CurrentUserController = _mockCurrentUserController.Object };
31+
32+
Client = new ParseClient(
33+
new ServerConnectionData { Test = true },
34+
new LiveQueryServerConnectionData { ApplicationID = "TestApp", Key = "t3stK3y", Test = true },
35+
_hub);
36+
2037
Client.Publicize();
2138
}
2239

@@ -38,10 +55,12 @@ public async Task TestBuildConnectMessage()
3855
Assert.IsTrue(message.ContainsKey("op"));
3956
Assert.IsTrue(message.ContainsKey("applicationId"));
4057
Assert.IsTrue(message.ContainsKey("windowsKey"));
41-
Assert.HasCount(3, message);
58+
Assert.IsTrue(message.ContainsKey("sessionToken"));
59+
Assert.HasCount(4, message);
4260
Assert.AreEqual("connect", message["op"]);
4361
Assert.AreEqual(Client.Services.LiveQueryServerConnectionData.ApplicationID, message["applicationId"]);
4462
Assert.AreEqual(Client.Services.LiveQueryServerConnectionData.Key, message["windowsKey"]);
63+
Assert.AreEqual(await Client.Services.GetCurrentSessionToken(), message["sessionToken"]);
4564
}
4665

4766
[TestMethod]
@@ -75,7 +94,7 @@ public async Task TestBuildSubscribeMessage()
7594
IDictionary<string, object> message = await builder.BuildSubscribeMessage<ParseObject>(requestId, liveQuery);
7695

7796
Assert.IsNotNull(message);
78-
Assert.HasCount(3, message);
97+
Assert.HasCount(4, message);
7998

8099
Assert.IsTrue(message.ContainsKey("op"));
81100
Assert.AreEqual("subscribe", message["op"]);
@@ -126,7 +145,7 @@ public async Task TestBuildUpdateSubscriptionMessage()
126145
IDictionary<string, object> message = await builder.BuildUpdateSubscriptionMessage<ParseObject>(requestId, liveQuery);
127146

128147
Assert.IsNotNull(message);
129-
Assert.HasCount(3, message);
148+
Assert.HasCount(4, message);
130149

131150
Assert.IsTrue(message.ContainsKey("op"));
132151
Assert.AreEqual("update", message["op"]);

Parse.Tests/LiveQueryMessageParserTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void TestGetObjectState()
8686

8787
Assert.ThrowsExactly<ArgumentNullException>(() => parser.GetObjectState(null));
8888
Assert.ThrowsExactly<ArgumentException>(() => parser.GetObjectState(new Dictionary<string, object> { { "wrongKey", objData } }));
89+
Assert.ThrowsExactly<ArgumentException>(() => parser.GetObjectState(new Dictionary<string, object> { { "object", null } }));
8990
Assert.ThrowsExactly<ArgumentException>(() => parser.GetObjectState(new Dictionary<string, object> { { "object", "notADictionary" } }));
9091
}
9192

@@ -116,6 +117,7 @@ public void TestGetOriginalState()
116117

117118
Assert.ThrowsExactly<ArgumentNullException>(() => parser.GetOriginalState(null));
118119
Assert.ThrowsExactly<ArgumentException>(() => parser.GetOriginalState(new Dictionary<string, object> { { "wrongKey", objData } }));
120+
Assert.ThrowsExactly<ArgumentException>(() => parser.GetOriginalState(new Dictionary<string, object> { { "original", null } }));
119121
Assert.ThrowsExactly<ArgumentException>(() => parser.GetOriginalState(new Dictionary<string, object> { { "original", "notADictionary" } }));
120122
}
121123

@@ -134,14 +136,18 @@ public void TestGetError()
134136
};
135137

136138
(int code, string error, bool shouldReconnect) = parser.GetError(message);
139+
Assert.HasCount(3, message);
137140
Assert.AreEqual(errorCode, code);
138141
Assert.AreEqual(errorMessage, error);
139142
Assert.AreEqual(reconnect, shouldReconnect);
140143

141144
Assert.ThrowsExactly<ArgumentNullException>(() => parser.GetError(null));
142145
Assert.ThrowsExactly<ArgumentException>(() => parser.GetError(new Dictionary<string, object> { { "wrongField", 123 } }));
146+
Assert.ThrowsExactly<ArgumentException>(() => parser.GetError(new Dictionary<string, object> { { "error", errorMessage }, { "reconnect", reconnect } }));
143147
Assert.ThrowsExactly<ArgumentException>(() => parser.GetError(new Dictionary<string, object> { { "code", "notALong" }, { "error", errorMessage }, { "reconnect", reconnect } }));
148+
Assert.ThrowsExactly<ArgumentException>(() => parser.GetError(new Dictionary<string, object> { { "code", (long) errorCode }, { "reconnect", reconnect } }));
144149
Assert.ThrowsExactly<ArgumentException>(() => parser.GetError(new Dictionary<string, object> { { "code", (long) errorCode }, { "error", 12345 }, { "reconnect", reconnect } }));
150+
Assert.ThrowsExactly<ArgumentException>(() => parser.GetError(new Dictionary<string, object> { { "code", (long) errorCode }, { "error", errorMessage } }));
145151
Assert.ThrowsExactly<ArgumentException>(() => parser.GetError(new Dictionary<string, object> { { "code", (long) errorCode }, { "error", errorMessage }, { "reconnect", "notABoolean" } }));
146152
}
147153
}

Parse/Platform/LiveQueries/ParseLiveQueryMessageParser.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ public int GetRequestId(IDictionary<string, object> message)
4040

4141
private IDictionary<string, object> GetDictionary(IDictionary<string, object> message, string key)
4242
{
43-
if (message is null)
44-
throw new ArgumentNullException(nameof(message));
45-
4643
if (!(message.TryGetValue(key, out object obj) && obj is IDictionary<string, object> dict))
4744
throw new ArgumentException(@"Message does not contain a valid %{key}.", nameof(message));
4845

@@ -54,9 +51,8 @@ public IObjectState GetObjectState(IDictionary<string, object> message)
5451
if (message is null)
5552
throw new ArgumentNullException(nameof(message));
5653

57-
IDictionary<string, object> current = GetDictionary(message, "object");
58-
if (current is null)
59-
throw new ArgumentException("Message does not contain a valid object state.", nameof(message));
54+
IDictionary<string, object> current = GetDictionary(message, "object")
55+
?? throw new ArgumentException("Message does not contain a valid object state.", nameof(message));
6056

6157
return ParseObjectCoder.Instance.Decode(current, Decoder, ParseClient.Instance.Services);
6258
}
@@ -66,9 +62,8 @@ public IObjectState GetOriginalState(IDictionary<string, object> message)
6662
if (message is null)
6763
throw new ArgumentNullException(nameof(message));
6864

69-
IDictionary<string, object> original = GetDictionary(message, "original");
70-
if (original is null)
71-
throw new ArgumentException("Message does not contain a valid original object state.", nameof(message));
65+
IDictionary<string, object> original = GetDictionary(message, "original")
66+
?? throw new ArgumentException("Message does not contain a valid original object state.", nameof(message));
7267

7368
return ParseObjectCoder.Instance.Decode(original, Decoder, ParseClient.Instance.Services);
7469
}

Parse/Platform/LiveQueries/ParseLiveQuerySubscription.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ParseLiveQuerySubscription<T> : IParseLiveQuerySubscription where T
1616
string ClassName { get; }
1717
IServiceHub Services { get; }
1818

19-
private int RequestId { get; set; }
19+
private readonly int RequestId;
2020

2121
/// <summary>
2222
/// Represents the Create event for a live query subscription.

0 commit comments

Comments
 (0)