Skip to content

Commit c59316b

Browse files
committed
Add live query and live query event arg tests
1 parent fbe273a commit c59316b

File tree

6 files changed

+215
-1
lines changed

6 files changed

+215
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Parse.Abstractions.Platform.Objects;
5+
using Parse.Infrastructure;
6+
using Parse.Platform.LiveQueries;
7+
using Parse.Platform.Objects;
8+
9+
namespace Parse.Tests;
10+
11+
[TestClass]
12+
public class LiveQueryDualEventArgsTests
13+
{
14+
private ParseClient Client { get; set; }
15+
16+
[TestInitialize]
17+
public void SetUp()
18+
{
19+
// Initialize the client and ensure the instance is set
20+
Client = new ParseClient(new ServerConnectionData { Test = true });
21+
Client.Publicize();
22+
}
23+
24+
[TestCleanup]
25+
public void TearDown() => (Client.Services as ServiceHub).Reset();
26+
27+
[TestMethod]
28+
public void TestParseLiveQueryErrorEventArgsConstructor()
29+
{
30+
IObjectState state = new MutableObjectState
31+
{
32+
ObjectId = "waGiManPutr4Pet1r",
33+
ClassName = "Pagi",
34+
CreatedAt = new DateTime { },
35+
ServerData = new Dictionary<string, object>
36+
{
37+
["username"] = "kevin",
38+
["sessionToken"] = "se551onT0k3n"
39+
}
40+
};
41+
42+
ParseObject obj = Client.GenerateObjectFromState<ParseObject>(state, "Corgi");
43+
obj.Set("test", "after");
44+
ParseObject objOrig = Client.GenerateObjectFromState<ParseObject>(state, "Corgi");
45+
objOrig.Set("test", "before");
46+
ParseLiveQueryDualEventArgs args = new ParseLiveQueryDualEventArgs(obj, objOrig);
47+
48+
Assert.AreEqual(obj, args.Object);
49+
Assert.AreEqual(objOrig, args.Original);
50+
}
51+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Parse.Platform.LiveQueries;
4+
5+
namespace Parse.Tests;
6+
7+
[TestClass]
8+
public class LiveQueryErrorEventArgsTests
9+
{
10+
[TestMethod]
11+
public void TestParseLiveQueryErrorEventArgsConstructor()
12+
{
13+
InvalidOperationException exception = new InvalidOperationException("Test exception");
14+
ParseLiveQueryErrorEventArgs args = new ParseLiveQueryErrorEventArgs(42, "Test error", false, exception);
15+
16+
// Assert
17+
Assert.AreEqual(42, args.Code);
18+
Assert.AreEqual("Test error", args.Error);
19+
Assert.AreEqual(false, args.Reconnect);
20+
Assert.AreEqual(exception, args.LocalException);
21+
}
22+
23+
[TestMethod]
24+
public void TestParseLiveQueryErrorEventArgsConstructorWithoutException()
25+
{
26+
ParseLiveQueryErrorEventArgs args = new ParseLiveQueryErrorEventArgs(42, "Test error", true);
27+
28+
// Assert
29+
Assert.AreEqual(42, args.Code);
30+
Assert.AreEqual("Test error", args.Error);
31+
Assert.AreEqual(true, args.Reconnect);
32+
Assert.AreEqual(null, args.LocalException);
33+
}
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Parse.Abstractions.Platform.Objects;
5+
using Parse.Infrastructure;
6+
using Parse.Platform.LiveQueries;
7+
using Parse.Platform.Objects;
8+
9+
namespace Parse.Tests;
10+
11+
[TestClass]
12+
public class LiveQueryEventArgsTests
13+
{
14+
private ParseClient Client { get; set; }
15+
16+
[TestInitialize]
17+
public void SetUp()
18+
{
19+
// Initialize the client and ensure the instance is set
20+
Client = new ParseClient(new ServerConnectionData { Test = true });
21+
Client.Publicize();
22+
}
23+
24+
[TestCleanup]
25+
public void TearDown() => (Client.Services as ServiceHub).Reset();
26+
27+
[TestMethod]
28+
public void TestParseLiveQueryErrorEventArgsConstructor()
29+
{
30+
IObjectState state = new MutableObjectState
31+
{
32+
ObjectId = "waGiManPutr4Pet1r",
33+
ClassName = "Pagi",
34+
CreatedAt = new DateTime { },
35+
ServerData = new Dictionary<string, object>
36+
{
37+
["username"] = "kevin",
38+
["sessionToken"] = "se551onT0k3n"
39+
}
40+
};
41+
42+
ParseObject obj = Client.GenerateObjectFromState<ParseObject>(state, "Corgi");
43+
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs(obj);
44+
45+
// Assert
46+
Assert.AreEqual(obj, args.Object);
47+
}
48+
}

Parse.Tests/LiveQueryTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Parse.Infrastructure;
5+
6+
namespace Parse.Tests;
7+
8+
[TestClass]
9+
public class LiveQueryTests
10+
{
11+
public class DummyParseObject : ParseObject { }
12+
13+
private ParseClient Client { get; set; }
14+
15+
[TestInitialize]
16+
public void SetUp()
17+
{
18+
// Initialize the client and ensure the instance is set
19+
Client = new ParseClient(new ServerConnectionData { Test = true }, new LiveQueryServerConnectionData { Test = true });
20+
Client.Publicize();
21+
}
22+
23+
[TestCleanup]
24+
public void Clean() => (Client.Services as ServiceHub).Reset();
25+
26+
[TestMethod]
27+
public void TestConstructor()
28+
{
29+
ParseLiveQuery<ParseObject> liveQuery = new ParseLiveQuery<ParseObject>(
30+
Client.Services,
31+
"DummyClass",
32+
new Dictionary<string, object> { { "foo", "bar" } },
33+
["foo"]);
34+
35+
// Assert
36+
Assert.AreEqual("DummyClass", liveQuery.ClassName, "The ClassName property of liveQuery should be 'DummyClass'.");
37+
IDictionary<string, object> buildParameters = liveQuery.BuildParameters();
38+
Assert.AreEqual("DummyClass", buildParameters["className"], "The ClassName property of liveQuery should be 'DummyClass'.");
39+
Assert.IsTrue(buildParameters.ContainsKey("where"), "The 'where' key should be present in the build parameters.");
40+
Assert.IsTrue(buildParameters.ContainsKey("keys"), "The 'keys' key should be present in the build parameters.");
41+
Assert.IsInstanceOfType<Dictionary<string, object>>(buildParameters["where"], "The 'where' parameter should be a Dictionary<string, object>.");
42+
Assert.IsInstanceOfType<string[]>(buildParameters["keys"], "The 'keys' parameter should be a string array.");
43+
Assert.AreEqual("bar", ((Dictionary<string, object>)buildParameters["where"])["foo"], "The 'where' clause should match the query condition.");
44+
Assert.AreEqual("foo", ((string[])buildParameters["keys"]).First(), "The 'keys' parameter should contain 'foo'.");
45+
}
46+
47+
[TestMethod]
48+
public void TestGetLive()
49+
{
50+
// Arrange
51+
ParseQuery<ParseObject> query = Client.GetQuery("DummyClass")
52+
.WhereEqualTo("foo", "bar")
53+
.Select("foo");
54+
55+
// Act
56+
ParseLiveQuery<ParseObject> liveQuery = query.GetLive()
57+
.Watch("foo");
58+
59+
// Assert
60+
Assert.AreEqual("DummyClass", liveQuery.ClassName, "The ClassName property of liveQuery should be 'DummyClass'.");
61+
IDictionary<string, object> buildParameters = liveQuery.BuildParameters();
62+
Assert.AreEqual("DummyClass", buildParameters["className"], "The ClassName property of liveQuery should be 'DummyClass'.");
63+
Assert.IsTrue(buildParameters.ContainsKey("where"), "The 'where' key should be present in the build parameters.");
64+
Assert.IsTrue(buildParameters.ContainsKey("keys"), "The 'keys' key should be present in the build parameters.");
65+
Assert.IsTrue(buildParameters.ContainsKey("watch"), "The 'watch' key should be present in the build parameters.");
66+
Assert.IsInstanceOfType<Dictionary<string, object>>(buildParameters["where"], "The 'where' parameter should be a Dictionary<string, object>.");
67+
Assert.IsInstanceOfType<string[]>(buildParameters["keys"], "The 'keys' parameter should be a string array.");
68+
Assert.IsInstanceOfType<string[]>(buildParameters["watch"], "The 'watch' parameter should be a string array.");
69+
Assert.AreEqual("bar", ((Dictionary<string, object>)buildParameters["where"])["foo"], "The 'where' clause should match the query condition.");
70+
Assert.AreEqual("foo", ((string[])buildParameters["keys"]).First(), "The 'keys' parameter should contain 'foo'.");
71+
Assert.AreEqual("foo", ((string[])buildParameters["watch"]).First(), "The 'watch' parameter should contain 'foo'.");
72+
}
73+
}

Parse/Platform/LiveQueries/ParseLiveQueryErrorEventArgs.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public class ParseLiveQueryErrorEventArgs : EventArgs
3939
/// </remarks>
4040
public bool Reconnect { get; }
4141

42+
/// <summary>
43+
/// Gets the local exception that occurred during a live query operation, if any.
44+
/// </summary>
45+
/// <remarks>
46+
/// The <see cref="LocalException"/> property contains the exception instance that was thrown locally,
47+
/// providing additional context or details about the error that occurred during the live query operation.
48+
/// This property may be <c>null</c> if no local exception was thrown.
49+
/// </remarks>
4250
public Exception LocalException { get; }
4351

4452
/// <summary>

Parse/Platform/Queries/ParseQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ public override int GetHashCode()
920920
public ParseLiveQuery<T> GetLive()
921921
{
922922
ArgumentNullException.ThrowIfNull(Filters);
923-
IDictionary<string, object> filters = BuildParameters().TryGetValue("where", out object where) ? where as IDictionary<string, object> : null;
923+
IDictionary<string, object> filters = PointerOrLocalIdEncoder.Instance.Encode(Filters, Services) as IDictionary<string, object>;
924924
return new ParseLiveQuery<T>(Services, ClassName, filters, KeySelections);
925925
}
926926
}

0 commit comments

Comments
 (0)