-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathInstallationTests.cs
251 lines (195 loc) · 9.85 KB
/
InstallationTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Parse.Abstractions.Infrastructure;
using Parse.Abstractions.Platform.Installations;
using Parse.Abstractions.Platform.Objects;
using Parse.Abstractions.Platform.Queries;
using Parse.Infrastructure;
using Parse.Platform.Objects;
namespace Parse.Tests;
[TestClass]
public class InstallationTests
{
private ParseClient Client { get; set; }
private Mock<IServiceHub> ServiceHubMock { get; set; }
private Mock<IParseObjectClassController> ClassControllerMock { get; set; }
private Mock<IParseCurrentInstallationController> CurrentInstallationControllerMock { get; set; }
[TestInitialize]
public void SetUp()
{
// Initialize mocks
ServiceHubMock = new Mock<IServiceHub>(MockBehavior.Strict);
ClassControllerMock = new Mock<IParseObjectClassController>(MockBehavior.Strict);
CurrentInstallationControllerMock = new Mock<IParseCurrentInstallationController>(MockBehavior.Strict);
// Mock ClassController behavior
ClassControllerMock.Setup(controller => controller.Instantiate(It.IsAny<string>(), It.IsAny<IServiceHub>()))
.Returns<string, IServiceHub>((className, hub) => new ParseInstallation().Bind(hub) as ParseObject);
ClassControllerMock.Setup(controller => controller.GetClassMatch("_Installation", typeof(ParseInstallation)))
.Returns(true);
ClassControllerMock.Setup(controller => controller.GetPropertyMappings("_Installation"))
.Returns(new Dictionary<string, string>
{
{ nameof(ParseInstallation.InstallationId), "installationId" },
{ nameof(ParseInstallation.DeviceType), "deviceType" },
{ nameof(ParseInstallation.AppName), "appName" },
{ nameof(ParseInstallation.AppVersion), "appVersion" },
{ nameof(ParseInstallation.AppIdentifier), "appIdentifier" },
{ nameof(ParseInstallation.TimeZone), "timeZone" },
{ nameof(ParseInstallation.LocaleIdentifier), "localeIdentifier" },
{ nameof(ParseInstallation.Channels), "channels" }
});
// Mock GetClassName
ClassControllerMock.Setup(controller => controller.GetClassName(typeof(ParseInstallation)))
.Returns("_Installation");
ClassControllerMock.Setup(controller => controller.AddValid(It.IsAny<Type>()))
.Verifiable();
ServiceHubMock.Setup(hub => hub.ClassController).Returns(ClassControllerMock.Object);
ServiceHubMock.Setup(hub => hub.CurrentInstallationController).Returns(CurrentInstallationControllerMock.Object);
// Create ParseClient with mocked ServiceHub
Client = new ParseClient(new ServerConnectionData { Test = true })
{
Services = ServiceHubMock.Object
};
// Publicize the client to set ParseClient.Instance
Client.Publicize();
// Add valid classes to the client
Client.AddValidClass<ParseInstallation>();
}
[TestCleanup]
public void TearDown()
{
(Client.Services as ServiceHub)?.Reset();
}
[TestMethod]
public void TestInstallationPropertyMappings()
{
var mappings = Client.Services.ClassController.GetPropertyMappings("_Installation");
Assert.IsNotNull(mappings);
Assert.AreEqual("installationId", mappings[nameof(ParseInstallation.InstallationId)]);
Assert.AreEqual("appName", mappings[nameof(ParseInstallation.AppName)]);
Assert.AreEqual("appIdentifier", mappings[nameof(ParseInstallation.AppIdentifier)]);
Assert.AreEqual("channels", mappings[nameof(ParseInstallation.Channels)]);
}
[TestMethod]
public void TestGetInstallationQuery()
{
// Act: Get the query
var query = Client.GetInstallationQuery();
// Assert: Verify the query type and class name
Assert.IsInstanceOfType(query, typeof(ParseQuery<ParseInstallation>));
Assert.AreEqual("_Installation", query.ClassName);
// Verify that GetClassName was called to resolve the class name
ClassControllerMock.Verify(controller => controller.GetClassName(typeof(ParseInstallation)), Times.Once);
// Verify AddValid was called for ParseInstallation
ClassControllerMock.Verify(controller => controller.AddValid(typeof(ParseInstallation)), Times.AtLeastOnce);
}
[TestMethod]
public void TestInstallationIdGetterSetter()
{
Guid guid = Guid.NewGuid();
ParseInstallation installation = Client.GenerateObjectFromState<ParseInstallation>(
new MutableObjectState { ServerData = new Dictionary<string, object> { ["installationId"] = guid.ToString() } },
"_Installation");
Assert.IsNotNull(installation);
Assert.AreEqual(guid, installation.InstallationId);
Guid newGuid = Guid.NewGuid();
Assert.ThrowsException<InvalidOperationException>(() => installation["installationId"] = newGuid);
installation.SetIfDifferent("installationId", newGuid.ToString());
Assert.AreEqual(newGuid, installation.InstallationId);
}
[TestMethod]
public void TestDeviceTypeGetterSetter()
{
ParseInstallation installation = Client.GenerateObjectFromState<ParseInstallation>(
new MutableObjectState { ServerData = new Dictionary<string, object> { ["deviceType"] = "parseOS" } },
"_Installation");
Assert.IsNotNull(installation);
Assert.AreEqual("parseOS", installation.DeviceType);
Assert.ThrowsException<InvalidOperationException>(() => installation["deviceType"] = "gogoOS");
installation.SetIfDifferent("deviceType", "gogoOS");
Assert.AreEqual("gogoOS", installation.DeviceType);
}
[TestMethod]
public void TestAppNameGetterSetter()
{
ParseInstallation installation = Client.GenerateObjectFromState<ParseInstallation>(
new MutableObjectState { ServerData = new Dictionary<string, object> { ["appName"] = "parseApp" } },
"_Installation");
Assert.IsNotNull(installation);
Assert.AreEqual("parseApp", installation.AppName);
Assert.ThrowsException<InvalidOperationException>(() => installation["appName"] = "gogoApp");
installation.SetIfDifferent("appName", "gogoApp");
Assert.AreEqual("gogoApp", installation.AppName);
}
[TestMethod]
public void TestAppVersionGetterSetter()
{
ParseInstallation installation = Client.GenerateObjectFromState<ParseInstallation>(
new MutableObjectState { ServerData = new Dictionary<string, object> { ["appVersion"] = "1.2.3" } },
"_Installation");
Assert.IsNotNull(installation);
Assert.AreEqual("1.2.3", installation.AppVersion);
Assert.ThrowsException<InvalidOperationException>(() => installation["appVersion"] = "1.2.4");
installation.SetIfDifferent("appVersion", "1.2.4");
Assert.AreEqual("1.2.4", installation.AppVersion);
}
[TestMethod]
public void TestAppIdentifierGetterSetter()
{
ParseInstallation installation = Client.GenerateObjectFromState<ParseInstallation>(
new MutableObjectState { ServerData = new Dictionary<string, object> { ["appIdentifier"] = "com.parse.app" } },
"_Installation");
Assert.IsNotNull(installation);
Assert.AreEqual("com.parse.app", installation.AppIdentifier);
Assert.ThrowsException<InvalidOperationException>(() => installation["appIdentifier"] = "com.parse.newapp");
installation.SetIfDifferent("appIdentifier", "com.parse.newapp");
Assert.AreEqual("com.parse.newapp", installation.AppIdentifier);
}
[TestMethod]
public void TestTimeZoneGetter()
{
ParseInstallation installation = Client.GenerateObjectFromState<ParseInstallation>(
new MutableObjectState { ServerData = new Dictionary<string, object> { ["timeZone"] = "America/Los_Angeles" } },
"_Installation");
Assert.IsNotNull(installation);
Assert.AreEqual("America/Los_Angeles", installation.TimeZone);
}
[TestMethod]
public void TestLocaleIdentifierGetter()
{
ParseInstallation installation = Client.GenerateObjectFromState<ParseInstallation>(
new MutableObjectState { ServerData = new Dictionary<string, object> { ["localeIdentifier"] = "en-US" } },
"_Installation");
Assert.IsNotNull(installation);
Assert.AreEqual("en-US", installation.LocaleIdentifier);
}
[TestMethod]
public void TestChannelGetterSetter()
{
ParseInstallation installation = Client.GenerateObjectFromState<ParseInstallation>(
new MutableObjectState { ServerData = new Dictionary<string, object> { ["channels"] = new List<string> { "the", "richard" } } },
"_Installation");
Assert.IsNotNull(installation);
Assert.AreEqual("the", installation.Channels[0]);
Assert.AreEqual("richard", installation.Channels[1]);
installation.Channels = new List<string> { "mr", "kevin" };
Assert.AreEqual("mr", installation.Channels[0]);
Assert.AreEqual("kevin", installation.Channels[1]);
}
[TestMethod]
public async Task TestGetCurrentInstallation()
{
var guid = Guid.NewGuid();
var expectedInstallation = new ParseInstallation();
expectedInstallation.SetIfDifferent("installationId", guid.ToString());
CurrentInstallationControllerMock.Setup(controller => controller.GetAsync(It.IsAny<IServiceHub>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(expectedInstallation));
ParseInstallation currentInstallation = await Client.GetCurrentInstallation();
Assert.IsNotNull(currentInstallation);
Assert.AreEqual(guid, currentInstallation.InstallationId);
}
}