Skip to content

Commit 8341e12

Browse files
committed
CodeRabbit required changes
1 parent ea60936 commit 8341e12

File tree

10 files changed

+170
-155
lines changed

10 files changed

+170
-155
lines changed

Parse.Tests/LiveQueryMessageBuilderTests.cs

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ public LiveQueryMessageBuilderTests()
3737
Client.Publicize();
3838
}
3939

40-
[TestMethod]
41-
public void TestConstructor()
42-
{
43-
ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder();
44-
45-
Assert.IsNotNull(builder, "Builder should not be null after construction.");
46-
}
47-
4840
[TestMethod]
4941
public async Task TestBuildConnectMessage()
5042
{
@@ -80,24 +72,13 @@ public void TestBuildUnsubscribeMessage()
8072
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => builder.BuildUnsubscribeMessage(0));
8173
}
8274

83-
[TestMethod]
84-
public async Task TestBuildSubscribeMessage()
75+
private void ValidateSubscriptionMessage(IDictionary<string, object> message, string expectedOp, int requestId)
8576
{
86-
int requestId = 2;
87-
ParseLiveQuery<ParseObject> liveQuery = new ParseLiveQuery<ParseObject>(
88-
Client.Services,
89-
"DummyClass",
90-
new Dictionary<string, object> { { "foo", "bar" } },
91-
["foo"],
92-
["foo"]);
93-
ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder();
94-
IDictionary<string, object> message = await builder.BuildSubscribeMessage<ParseObject>(requestId, liveQuery);
95-
9677
Assert.IsNotNull(message);
9778
Assert.HasCount(4, message);
9879

9980
Assert.IsTrue(message.ContainsKey("op"));
100-
Assert.AreEqual("subscribe", message["op"]);
81+
Assert.AreEqual(expectedOp, message["op"]);
10182

10283
Assert.IsTrue(message.ContainsKey("requestId"));
10384
Assert.AreEqual(requestId, message["requestId"]);
@@ -127,6 +108,23 @@ public async Task TestBuildSubscribeMessage()
127108
Assert.HasCount(1, (string[]) query["watch"], "The 'watch' array should contain exactly one element.");
128109
Assert.AreEqual("foo", ((string[]) query["watch"])[0], "The 'watch' parameter should contain 'foo'.");
129110

111+
}
112+
113+
[TestMethod]
114+
public async Task TestBuildSubscribeMessage()
115+
{
116+
int requestId = 2;
117+
ParseLiveQuery<ParseObject> liveQuery = new ParseLiveQuery<ParseObject>(
118+
Client.Services,
119+
"DummyClass",
120+
new Dictionary<string, object> { { "foo", "bar" } },
121+
["foo"],
122+
["foo"]);
123+
ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder();
124+
IDictionary<string, object> message = await builder.BuildSubscribeMessage<ParseObject>(requestId, liveQuery);
125+
126+
ValidateSubscriptionMessage(message, "subscribe", requestId);
127+
130128
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () => await builder.BuildSubscribeMessage<ParseObject>(0, liveQuery));
131129
await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await builder.BuildSubscribeMessage<ParseObject>(requestId, null));
132130
}
@@ -144,39 +142,7 @@ public async Task TestBuildUpdateSubscriptionMessage()
144142
ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder();
145143
IDictionary<string, object> message = await builder.BuildUpdateSubscriptionMessage<ParseObject>(requestId, liveQuery);
146144

147-
Assert.IsNotNull(message);
148-
Assert.HasCount(4, message);
149-
150-
Assert.IsTrue(message.ContainsKey("op"));
151-
Assert.AreEqual("update", message["op"]);
152-
153-
Assert.IsTrue(message.ContainsKey("requestId"));
154-
Assert.AreEqual(requestId, message["requestId"]);
155-
156-
Assert.IsTrue(message.ContainsKey("query"));
157-
Assert.IsInstanceOfType<IDictionary<string, object>>(message["query"], "The 'query' value should be a Dictionary<string, object>.");
158-
Assert.HasCount(4, (IDictionary<string, object>) message["query"]);
159-
IDictionary<string, object> query = message["query"] as IDictionary<string, object>;
160-
161-
Assert.IsTrue(query.ContainsKey("className"), "The 'query' dictionary should contain the 'className' key.");
162-
Assert.AreEqual("DummyClass", query["className"], "The 'className' property should be 'DummyClass'.");
163-
164-
Assert.IsTrue(query.ContainsKey("where"), "The 'query' dictionary should contain the 'where' key.");
165-
Assert.IsInstanceOfType<IDictionary<string, object>>(query["where"], "The 'where' property should be a Dictionary<string, object>.");
166-
IDictionary<string, object> where = (IDictionary<string, object>) query["where"];
167-
Assert.HasCount(1, where, "The 'where' dictionary should contain exactly one key-value pair.");
168-
Assert.IsTrue(where.ContainsKey("foo"), "The 'where' dictionary should contain the 'foo' key.");
169-
Assert.AreEqual("bar", where["foo"], "The 'foo' property in 'where' should be 'bar'.");
170-
171-
Assert.IsTrue(query.ContainsKey("keys"), "The 'query' dictionary should contain the 'keys' key.");
172-
Assert.IsInstanceOfType<string[]>(query["keys"], "The 'keys' property should be a string array.");
173-
Assert.HasCount(1, (string[]) query["keys"], "The 'keys' array should contain exactly one element.");
174-
Assert.AreEqual("foo", ((string[]) query["keys"])[0], "The 'keys' parameter should contain 'foo'.");
175-
176-
Assert.IsTrue(query.ContainsKey("watch"), "The 'query' dictionary should contain the 'watch' key.");
177-
Assert.IsInstanceOfType<string[]>(query["watch"], "The 'watch' property should be a string array.");
178-
Assert.HasCount(1, (string[]) query["watch"], "The 'watch' array should contain exactly one element.");
179-
Assert.AreEqual("foo", ((string[]) query["watch"])[0], "The 'watch' parameter should contain 'foo'.");
145+
ValidateSubscriptionMessage(message, "update", requestId);
180146

181147
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () => await builder.BuildUpdateSubscriptionMessage<ParseObject>(0, liveQuery));
182148
await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await builder.BuildUpdateSubscriptionMessage<ParseObject>(requestId, null));

Parse.Tests/LiveQueryMessageParserTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Parse.Abstractions.Platform.LiveQueries;
45
using Parse.Abstractions.Platform.Objects;
56
using Parse.Infrastructure;
67
using Parse.Platform.LiveQueries;
@@ -67,8 +68,8 @@ public void TestGetObjectState()
6768
{
6869
{ "objectId", "obj123" },
6970
{ "className", "TestClass" },
70-
{ "createdAt", "2023-10-01T12:00.000Z" },
71-
{ "updatedAt", "2023-10-01T12:00.000Z" },
71+
{ "createdAt", "2023-10-01T12:00.00.000Z" },
72+
{ "updatedAt", "2023-10-01T12:00.00.000Z" },
7273
{
7374
"ACL", new Dictionary<string, object>
7475
{
@@ -135,11 +136,11 @@ public void TestGetError()
135136
{ "reconnect", reconnect }
136137
};
137138

138-
(int code, string error, bool shouldReconnect) = parser.GetError(message);
139+
IParseLiveQueryMessageParser.LiveQueryError error = parser.GetError(message);
139140
Assert.HasCount(3, message);
140-
Assert.AreEqual(errorCode, code);
141-
Assert.AreEqual(errorMessage, error);
142-
Assert.AreEqual(reconnect, shouldReconnect);
141+
Assert.AreEqual(errorCode, error.Code);
142+
Assert.AreEqual(errorMessage, error.Message);
143+
Assert.AreEqual(reconnect, error.Reconnect);
143144

144145
Assert.ThrowsExactly<ArgumentNullException>(() => parser.GetError(null));
145146
Assert.ThrowsExactly<ArgumentException>(() => parser.GetError(new Dictionary<string, object> { { "wrongField", 123 } }));

Parse.Tests/LiveQuerySubscriptionTests.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ public class LiveQuerySubscriptionTests
1616

1717
public LiveQuerySubscriptionTests() => Client.Publicize();
1818

19-
[TestInitialize]
20-
public void SetUp()
21-
{
22-
Client.Publicize();
23-
}
24-
25-
[TestCleanup]
26-
public void TearDown() => (Client.Services as ServiceHub).Reset();
27-
2819
[TestMethod]
2920
public void TestConstructor() => Assert.IsNotNull(
3021
new ParseLiveQuerySubscription<ParseObject>(Client.Services, "Foo", 1),
@@ -54,15 +45,18 @@ public void TestCreate()
5445

5546
ParseLiveQuerySubscription<ParseObject> subscription = new(Client.Services, "Corgi", 1);
5647

48+
bool createInvoked = false;
5749
subscription.Create += (_, args) =>
5850
{
51+
createInvoked = true;
5952
Assert.IsNotNull(args, "The event args should not be null.");
6053
Assert.AreEqual(args.Object.ObjectId, state.ObjectId);
6154
Assert.AreEqual(args.Object.ClassName, state.ClassName);
6255
Assert.AreEqual(args.Object["key"], state["key"]);
6356
};
6457

6558
subscription.OnCreate(state);
59+
Assert.IsTrue(createInvoked, "Create event should have been invoked.");
6660
}
6761

6862
[TestMethod]
@@ -79,8 +73,16 @@ public void TestUpdate()
7973
}
8074
};
8175

82-
MutableObjectState state = originalState;
83-
state["key"] = "after";
76+
MutableObjectState state = new()
77+
{
78+
ObjectId = originalState.ObjectId,
79+
ClassName = originalState.ClassName,
80+
CreatedAt = originalState.CreatedAt,
81+
ServerData = new Dictionary<string, object>
82+
{
83+
["key"] = "after"
84+
}
85+
};
8486

8587
ParseLiveQuerySubscription<ParseObject> subscription = new(Client.Services, "Corgi", 1);
8688

Parse.Tests/LiveQueryTests.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ public void TestConstructor()
3232
Assert.AreEqual("DummyClass", buildParameters["className"], "The ClassName property of liveQuery should be 'DummyClass'.");
3333
Assert.IsTrue(buildParameters.ContainsKey("where"), "The 'where' key should be present in the build parameters.");
3434
Assert.IsTrue(buildParameters.ContainsKey("keys"), "The 'keys' key should be present in the build parameters.");
35-
Assert.IsInstanceOfType<IDictionary<string, object>>(buildParameters["where"], "The 'where' parameter should be a Dictionary<string, object>.");
36-
Assert.IsInstanceOfType<string[]>(buildParameters["keys"], "The 'keys' parameter should be a string array.");
35+
Assert.IsInstanceOfType(buildParameters["where"], typeof(IDictionary<string, object>), "The 'where' parameter should be a Dictionary<string, object>.");
36+
Assert.IsInstanceOfType(buildParameters["keys"], typeof(string[]), "The 'keys' parameter should be a string array.");
3737
Assert.AreEqual("bar", ((IDictionary<string, object>) buildParameters["where"])["foo"], "The 'where' clause should match the query condition.");
3838
Assert.AreEqual("foo", ((string[]) buildParameters["keys"]).First(), "The 'keys' parameter should contain 'foo'.");
3939
Assert.IsFalse(buildParameters.ContainsKey("watch"), "The 'watch' parameter should not be present.");
40+
4041
}
4142

4243
[TestMethod]
@@ -55,11 +56,11 @@ public void TestConstructorWithWatchedKeys()
5556
Assert.AreEqual("DummyClass", buildParameters["className"], "The ClassName property of liveQuery should be 'DummyClass'.");
5657
Assert.IsTrue(buildParameters.ContainsKey("where"), "The 'where' key should be present in the build parameters.");
5758
Assert.IsTrue(buildParameters.ContainsKey("keys"), "The 'keys' key should be present in the build parameters.");
58-
Assert.IsInstanceOfType<Dictionary<string, object>>(buildParameters["where"], "The 'where' parameter should be a Dictionary<string, object>.");
59-
Assert.IsInstanceOfType<string[]>(buildParameters["keys"], "The 'keys' parameter should be a string array.");
59+
Assert.IsInstanceOfType(buildParameters["where"], typeof(IDictionary<string, object>), "The 'where' parameter should be a Dictionary<string, object>.");
60+
Assert.IsInstanceOfType(buildParameters["keys"], typeof(string[]), "The 'keys' parameter should be a string array.");
6061
Assert.AreEqual("bar", ((IDictionary<string, object>) buildParameters["where"])["foo"], "The 'where' clause should match the query condition.");
6162
Assert.AreEqual("foo", ((string[]) buildParameters["keys"]).First(), "The 'keys' parameter should contain 'foo'.");
62-
Assert.IsInstanceOfType<string[]>(buildParameters["watch"], "The 'watch' parameter should be a string array.");
63+
Assert.IsInstanceOfType(buildParameters["watch"], typeof(string[]), "The 'watch' parameter should be a string array.");
6364
Assert.AreEqual("foo", ((string[]) buildParameters["watch"]).First(), "The 'watch' parameter should contain 'foo'.");
6465
}
6566

@@ -80,8 +81,8 @@ public void TestGetLive()
8081
Assert.AreEqual("DummyClass", buildParameters["className"], "The ClassName property of liveQuery should be 'DummyClass'.");
8182
Assert.IsTrue(buildParameters.ContainsKey("where"), "The 'where' key should be present in the build parameters.");
8283
Assert.IsTrue(buildParameters.ContainsKey("keys"), "The 'keys' key should be present in the build parameters.");
83-
Assert.IsInstanceOfType<IDictionary<string, object>>(buildParameters["where"], "The 'where' parameter should be a Dictionary<string, object>.");
84-
Assert.IsInstanceOfType<string[]>(buildParameters["keys"], "The 'keys' parameter should be a string array.");
84+
Assert.IsInstanceOfType(buildParameters["where"], typeof(IDictionary<string, object>), "The 'where' parameter should be a Dictionary<string, object>.");
85+
Assert.IsInstanceOfType(buildParameters["keys"], typeof(string[]), "The 'keys' parameter should be a string array.");
8586
Assert.AreEqual("bar", ((Dictionary<string, object>) buildParameters["where"])["foo"], "The 'where' clause should match the query condition.");
8687
Assert.AreEqual("foo", ((string[]) buildParameters["keys"]).First(), "The 'keys' parameter should contain 'foo'.");
8788
}
@@ -105,14 +106,14 @@ public void TestWatch()
105106

106107
// Assert
107108
buildParameters = liveQuery.BuildParameters();
108-
Assert.IsInstanceOfType<string[]>(buildParameters["watch"], "The 'watch' parameter should be a string array.");
109+
Assert.IsInstanceOfType(buildParameters["watch"], typeof(string[]), "The 'watch' parameter should be a string array.");
109110
Assert.AreEqual("foo", ((string[]) buildParameters["watch"]).First(), "The 'watch' parameter should contain 'foo'.");
110111

111112
// Act
112113
liveQuery = liveQuery.Watch("bar");
113114

114115
// Assert
115116
buildParameters = liveQuery.BuildParameters();
116-
Assert.Contains("bar", (string[]) buildParameters["watch"], "The 'watch' parameter should contain 'bar'.");
117+
CollectionAssert.Contains((string[]) buildParameters["watch"], "bar", "The 'watch' parameter should contain 'bar'.");
117118
}
118119
}

Parse.Tests/TextWebSocketClientTests.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,37 @@ public void TestConstructor()
1515
{
1616
TextWebSocketClient client = new TextWebSocketClient(4096);
1717
Assert.IsNotNull(client);
18-
19-
client.Dispose();
2018
}
2119

2220
[TestMethod]
21+
[TestCategory("Integration")]
22+
[Timeout(10000)]
2323
public async Task TestSendAndReceive()
2424
{
2525
TextWebSocketClient client = new TextWebSocketClient(32);
26-
await client.OpenAsync("wss://echo.websocket.org", CancellationToken.None);
26+
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
27+
TaskCompletionSource receiveTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
2728

28-
TaskCompletionSource ConnectionSignal = new TaskCompletionSource();
29-
CancellationTokenSource cts = new CancellationTokenSource();
29+
await client.OpenAsync("wss://echo.websocket.org", cts.Token);
3030

31-
client.MessageReceived += (_, e) =>
31+
string receivedMessage = null;
32+
EventHandler<MessageReceivedEventArgs> handler = (_, e) =>
3233
{
3334
if (e.Message.StartsWith("Request served by"))
3435
{
3536
return;
3637
}
37-
Assert.AreEqual("Hello world, WebSocket listening!", e.Message);
38-
ConnectionSignal?.TrySetResult();
38+
receivedMessage = e.Message;
39+
receiveTcs?.TrySetResult();
3940
};
41+
client.MessageReceived += handler;
4042

4143
await client.SendAsync("Hello world, WebSocket listening!", cts.Token);
4244
cts.CancelAfter(5000);
43-
await ConnectionSignal.Task.WaitAsync(cts.Token);
44-
ConnectionSignal = null;
45-
await client.CloseAsync();
46-
client.Dispose();
47-
}
45+
await receiveTcs.Task.WaitAsync(cts.Token);
46+
Assert.AreEqual("Hello world, WebSocket listening!", receivedMessage);
4847

49-
48+
client.MessageReceived -= handler;
49+
await client.CloseAsync(cts.Token);
50+
}
5051
}

Parse/Abstractions/Platform/LiveQueries/IParseLiveQueryMessageParser.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,48 @@ namespace Parse.Abstractions.Platform.LiveQueries;
88
/// </summary>
99
public interface IParseLiveQueryMessageParser
1010
{
11+
12+
struct LiveQueryError
13+
{
14+
public int Code { get; }
15+
public string Message { get; }
16+
public bool Reconnect { get; }
17+
18+
public LiveQueryError(int code, string message, bool reconnect)
19+
{
20+
Code = code;
21+
Message = message;
22+
Reconnect = reconnect;
23+
}
24+
}
25+
1126
/// <summary>
1227
/// Gets the client identifier from the specified message.
1328
/// </summary>
1429
/// <param name="message">The message containing the client identifier.</param>
1530
/// <returns>The client identifier as a string.</returns>
16-
public string GetClientId(IDictionary<string, object> message);
31+
string GetClientId(IDictionary<string, object> message);
1732

1833
/// <summary>
1934
/// Gets the request identifier from the specified message.
2035
/// </summary>
2136
/// <param name="message">The message containing the request identifier.</param>
2237
/// <returns>The request identifier as an integer.</returns>
23-
public int GetRequestId(IDictionary<string, object> message);
38+
int GetRequestId(IDictionary<string, object> message);
2439

2540
/// <summary>
2641
/// Gets the object state from the specified message.
2742
/// </summary>
2843
/// <param name="message">The message containing the object state data.</param>
2944
/// <returns>The object state as an <see cref="IObjectState"/>.</returns>
30-
public IObjectState GetObjectState(IDictionary<string, object> message);
45+
IObjectState GetObjectState(IDictionary<string, object> message);
3146

3247
/// <summary>
3348
/// Gets the original object state from the specified message.
3449
/// </summary>
3550
/// <param name="message">The message containing the original object state data.</param>
3651
/// <returns>The original object state as an <see cref="IObjectState"/>.</returns>
37-
public IObjectState GetOriginalState(IDictionary<string, object> message);
52+
IObjectState GetOriginalState(IDictionary<string, object> message);
3853

3954
/// <summary>
4055
/// Gets the error information from the specified message.
@@ -43,5 +58,5 @@ public interface IParseLiveQueryMessageParser
4358
/// <returns>
4459
/// A tuple containing the error code, error message, and a boolean indicating whether to reconnect.
4560
/// </returns>
46-
public (int code, string error, bool reconnect) GetError(IDictionary<string, object> message);
61+
LiveQueryError GetError(IDictionary<string, object> message);
4762
}

0 commit comments

Comments
 (0)