-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathObjectStateTests.cs
163 lines (144 loc) · 4.73 KB
/
ObjectStateTests.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
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Parse.Abstractions.Infrastructure.Control;
using Parse.Abstractions.Platform.Objects;
using Parse.Infrastructure.Control;
using Parse.Platform.Objects;
namespace Parse.Tests;
[TestClass]
public class ObjectStateTests
{
[TestMethod]
public void TestDefault()
{
IObjectState state = new MutableObjectState();
Assert.IsNull(state.ClassName);
Assert.IsNull(state.ObjectId);
Assert.IsNull(state.CreatedAt);
Assert.IsNull(state.UpdatedAt);
foreach (KeyValuePair<string, object> pair in state)
{
Assert.IsNotNull(pair);
}
}
[TestMethod]
public void TestProperties()
{
DateTime now = new DateTime();
IObjectState state = new MutableObjectState
{
ClassName = "Corgi",
UpdatedAt = now,
CreatedAt = now,
ServerData = new Dictionary<string, object>() {
{ "1", "Choucho" },
{ "2", "Miku" },
{ "3", "Halyosy" }
}
};
Assert.AreEqual("Corgi", state.ClassName);
Assert.AreEqual(now, state.UpdatedAt);
Assert.AreEqual(now, state.CreatedAt);
Assert.AreEqual(3, state.Count());
Assert.AreEqual("Choucho", state["1"]);
Assert.AreEqual("Miku", state["2"]);
Assert.AreEqual("Halyosy", state["3"]);
}
[TestMethod]
public void TestContainsKey()
{
IObjectState state = new MutableObjectState
{
ServerData = new Dictionary<string, object>() {
{ "Len", "Kagamine" },
{ "Rin", "Kagamine" },
{ "3", "Halyosy" }
}
};
Assert.IsTrue(state.ContainsKey("Len"));
Assert.IsTrue(state.ContainsKey("Rin"));
Assert.IsTrue(state.ContainsKey("3"));
Assert.IsFalse(state.ContainsKey("Halyosy"));
Assert.IsFalse(state.ContainsKey("Kagamine"));
}
[TestMethod]
public void TestApplyOperation()
{
IParseFieldOperation op1 = new ParseIncrementOperation(7);
IParseFieldOperation op2 = new ParseSetOperation("legendia");
IParseFieldOperation op3 = new ParseSetOperation("vesperia");
Dictionary<string, IParseFieldOperation> operations = new Dictionary<string, IParseFieldOperation>() {
{ "exist", op1 },
{ "missing", op2 },
{ "change", op3 }
};
IObjectState state = new MutableObjectState
{
ServerData = new Dictionary<string, object>() {
{ "exist", 2 },
{ "change", "teletubies" }
}
};
Assert.AreEqual(2, state["exist"]);
Assert.AreEqual("teletubies", state["change"]);
state = state.MutatedClone(mutableClone => mutableClone.Apply(operations));
Assert.AreEqual(3, state.Count());
Assert.AreEqual(9, state["exist"]);
Assert.AreEqual("legendia", state["missing"]);
Assert.AreEqual("vesperia", state["change"]);
}
[TestMethod]
public void TestApplyState()
{
DateTime now = new DateTime();
IObjectState state = new MutableObjectState
{
ClassName = "Corgi",
ObjectId = "abcd",
ServerData = new Dictionary<string, object>() {
{ "exist", 2 },
{ "change", "teletubies" }
}
};
IObjectState appliedState = new MutableObjectState
{
ClassName = "AnotherCorgi",
ObjectId = "1234",
CreatedAt = now,
ServerData = new Dictionary<string, object>() {
{ "exist", 9 },
{ "missing", "marasy" }
}
};
state = state.MutatedClone(mutableClone => mutableClone.Apply(appliedState));
Assert.AreEqual("Corgi", state.ClassName);
Assert.AreEqual("1234", state.ObjectId);
Assert.IsNotNull(state.CreatedAt);
Assert.IsNull(state.UpdatedAt);
Assert.AreEqual(3, state.Count());
Assert.AreEqual(9, state["exist"]);
Assert.AreEqual("teletubies", state["change"]);
Assert.AreEqual("marasy", state["missing"]);
}
[TestMethod]
public void TestMutatedClone()
{
IObjectState state = new MutableObjectState
{
ClassName = "Corgi"
};
Assert.AreEqual("Corgi", state.ClassName);
IObjectState newState = state.MutatedClone((mutableClone) =>
{
mutableClone.ClassName = "AnotherCorgi";
mutableClone.CreatedAt = new DateTime();
});
Assert.AreEqual("Corgi", state.ClassName);
Assert.IsNull(state.CreatedAt);
Assert.AreEqual("AnotherCorgi", newState.ClassName);
Assert.IsNotNull(newState.CreatedAt);
Assert.AreNotSame(state, newState);
}
}