|
| 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 | +} |
0 commit comments