|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | +using Parse.Abstractions.Platform.Objects; |
| 6 | +using Parse.Infrastructure; |
| 7 | +using Parse.Platform.LiveQueries; |
| 8 | +using Parse.Platform.Objects; |
| 9 | + |
| 10 | +namespace Parse.Tests; |
| 11 | + |
| 12 | +[TestClass] |
| 13 | +public class LiveQuerySubscriptionTests |
| 14 | +{ |
| 15 | + private ParseClient Client { get; } = new ParseClient(new ServerConnectionData { Test = true }, new LiveQueryServerConnectionData { Test = true }); |
| 16 | + |
| 17 | + public LiveQuerySubscriptionTests() => Client.Publicize(); |
| 18 | + |
| 19 | + [TestInitialize] |
| 20 | + public void SetUp() |
| 21 | + { |
| 22 | + Client.Publicize(); |
| 23 | + } |
| 24 | + |
| 25 | + [TestCleanup] |
| 26 | + public void TearDown() => (Client.Services as ServiceHub).Reset(); |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + public void TestConstructor() => Assert.IsNotNull( |
| 30 | + new ParseLiveQuerySubscription<ParseObject>(Client.Services, "Foo", 1), |
| 31 | + "The subscription instance should not be null."); |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public void TestConstructorExceptionServiceHub() => |
| 35 | + Assert.ThrowsExactly<ArgumentNullException>(() => new ParseLiveQuerySubscription<ParseObject>(null, "Foo", 1)); |
| 36 | + |
| 37 | + [TestMethod] |
| 38 | + public void TestConstructorExceptionClassName() => |
| 39 | + Assert.ThrowsExactly<ArgumentException>(() => new ParseLiveQuerySubscription<ParseObject>(Client.Services, String.Empty, 1)); |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void TestCreate() |
| 43 | + { |
| 44 | + MutableObjectState state = new() |
| 45 | + { |
| 46 | + ObjectId = "waGiManPutr4Pet1r", |
| 47 | + ClassName = "Corgi", |
| 48 | + CreatedAt = new DateTime { }, |
| 49 | + ServerData = new Dictionary<string, object> |
| 50 | + { |
| 51 | + ["key"] = "value" |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + ParseLiveQuerySubscription<ParseObject> subscription = new(Client.Services, "Corgi", 1); |
| 56 | + |
| 57 | + subscription.Create += (_, args) => |
| 58 | + { |
| 59 | + Assert.IsNotNull(args, "The event args should not be null."); |
| 60 | + Assert.AreEqual(args.Object.ObjectId, state.ObjectId); |
| 61 | + Assert.AreEqual(args.Object.ClassName, state.ClassName); |
| 62 | + Assert.AreEqual(args.Object["key"], state["key"]); |
| 63 | + }; |
| 64 | + |
| 65 | + subscription.OnCreate(state); |
| 66 | + } |
| 67 | + |
| 68 | + [TestMethod] |
| 69 | + public void TestUpdate() |
| 70 | + { |
| 71 | + MutableObjectState originalState = new() |
| 72 | + { |
| 73 | + ObjectId = "waGiManPutr4Pet1r", |
| 74 | + ClassName = "Corgi", |
| 75 | + CreatedAt = new DateTime { }, |
| 76 | + ServerData = new Dictionary<string, object> |
| 77 | + { |
| 78 | + ["key"] = "before" |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + MutableObjectState state = originalState; |
| 83 | + state["key"] = "after"; |
| 84 | + |
| 85 | + ParseLiveQuerySubscription<ParseObject> subscription = new(Client.Services, "Corgi", 1); |
| 86 | + |
| 87 | + subscription.Update += (_, args) => |
| 88 | + { |
| 89 | + Assert.IsNotNull(args, "The event args should not be null."); |
| 90 | + |
| 91 | + Assert.AreEqual(args.Object.ObjectId, state.ObjectId); |
| 92 | + Assert.AreEqual(args.Object.ClassName, state.ClassName); |
| 93 | + Assert.AreEqual(args.Object["key"], state["key"]); |
| 94 | + |
| 95 | + Assert.AreEqual(args.Original.ObjectId, originalState.ObjectId); |
| 96 | + Assert.AreEqual(args.Original.ClassName, originalState.ClassName); |
| 97 | + Assert.AreEqual(args.Original["key"], originalState["key"]); |
| 98 | + }; |
| 99 | + |
| 100 | + subscription.OnUpdate(state, originalState); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + [TestMethod] |
| 105 | + public void TestEnter() |
| 106 | + { |
| 107 | + MutableObjectState originalState = new() |
| 108 | + { |
| 109 | + ObjectId = "waGiManPutr4Pet1r", |
| 110 | + ClassName = "Corgi", |
| 111 | + CreatedAt = new DateTime { }, |
| 112 | + ServerData = new Dictionary<string, object> |
| 113 | + { |
| 114 | + ["key"] = "before" |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + MutableObjectState state = originalState; |
| 119 | + state["key"] = "after"; |
| 120 | + |
| 121 | + ParseLiveQuerySubscription<ParseObject> subscription = new(Client.Services, "Corgi", 1); |
| 122 | + |
| 123 | + subscription.Enter += (_, args) => |
| 124 | + { |
| 125 | + Assert.IsNotNull(args, "The event args should not be null."); |
| 126 | + |
| 127 | + Assert.AreEqual(args.Object.ObjectId, state.ObjectId); |
| 128 | + Assert.AreEqual(args.Object.ClassName, state.ClassName); |
| 129 | + Assert.AreEqual(args.Object["key"], state["key"]); |
| 130 | + |
| 131 | + Assert.AreEqual(args.Original.ObjectId, originalState.ObjectId); |
| 132 | + Assert.AreEqual(args.Original.ClassName, originalState.ClassName); |
| 133 | + Assert.AreEqual(args.Original["key"], originalState["key"]); |
| 134 | + }; |
| 135 | + |
| 136 | + subscription.OnEnter(state, originalState); |
| 137 | + } |
| 138 | + |
| 139 | + [TestMethod] |
| 140 | + public void TestLeave() |
| 141 | + { |
| 142 | + MutableObjectState originalState = new() |
| 143 | + { |
| 144 | + ObjectId = "waGiManPutr4Pet1r", |
| 145 | + ClassName = "Corgi", |
| 146 | + CreatedAt = new DateTime { }, |
| 147 | + ServerData = new Dictionary<string, object> |
| 148 | + { |
| 149 | + ["key"] = "before" |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + MutableObjectState state = originalState; |
| 154 | + state["key"] = "after"; |
| 155 | + |
| 156 | + ParseLiveQuerySubscription<ParseObject> subscription = new(Client.Services, "Corgi", 1); |
| 157 | + |
| 158 | + subscription.Leave += (_, args) => |
| 159 | + { |
| 160 | + Assert.IsNotNull(args, "The event args should not be null."); |
| 161 | + |
| 162 | + Assert.AreEqual(args.Object.ObjectId, state.ObjectId); |
| 163 | + Assert.AreEqual(args.Object.ClassName, state.ClassName); |
| 164 | + Assert.AreEqual(args.Object["key"], state["key"]); |
| 165 | + |
| 166 | + Assert.AreEqual(args.Original.ObjectId, originalState.ObjectId); |
| 167 | + Assert.AreEqual(args.Original.ClassName, originalState.ClassName); |
| 168 | + Assert.AreEqual(args.Original["key"], originalState["key"]); |
| 169 | + }; |
| 170 | + |
| 171 | + subscription.OnLeave(state, originalState); |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + [TestMethod] |
| 176 | + public void TestDelete() |
| 177 | + { |
| 178 | + MutableObjectState state = new() |
| 179 | + { |
| 180 | + ObjectId = "waGiManPutr4Pet1r", |
| 181 | + ClassName = "Corgi", |
| 182 | + CreatedAt = new DateTime { }, |
| 183 | + ServerData = new Dictionary<string, object> |
| 184 | + { |
| 185 | + ["key"] = "value" |
| 186 | + } |
| 187 | + }; |
| 188 | + |
| 189 | + ParseLiveQuerySubscription<ParseObject> subscription = new(Client.Services, "Corgi", 1); |
| 190 | + |
| 191 | + subscription.Delete += (_, args) => |
| 192 | + { |
| 193 | + Assert.IsNotNull(args, "The event args should not be null."); |
| 194 | + Assert.AreEqual(args.Object.ObjectId, state.ObjectId); |
| 195 | + Assert.AreEqual(args.Object.ClassName, state.ClassName); |
| 196 | + Assert.AreEqual(args.Object["key"], state["key"]); |
| 197 | + }; |
| 198 | + |
| 199 | + subscription.OnDelete(state); |
| 200 | + } |
| 201 | +} |
0 commit comments