This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
ReflectionBuilderTests.cs
237 lines (203 loc) · 7.68 KB
/
ReflectionBuilderTests.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using NUnit.Framework;
namespace MongoDB.Driver.Serialization
{
[TestFixture]
public class ReflectionBuilderTests : SerializationTestBase
{
public class SimpleObject
{
public string A { get; set; }
public string B { get; set; }
public SimpleObjectC C { get; set; }
}
public class SimpleObjectC
{
public string D { get; set; }
}
[Test]
public void CanDeserializeASimpleObject()
{
const string bson = "KAAAAAJBAAIAAABhAAJCAAIAAABiAANDAA4AAAACRAACAAAAZAAAAA==";
var simpleObject = Deserialize<SimpleObject>(bson);
Assert.IsNotNull(simpleObject);
Assert.AreEqual("a", simpleObject.A);
Assert.AreEqual("b", simpleObject.B);
Assert.IsNotNull(simpleObject.C);
Assert.AreEqual("d", simpleObject.C.D);
}
public class SimpleArrayAsList
{
public List<object> A { get; set; }
}
[Test]
public void CanDeserializeAnSimpleArrayAsList()
{
const string bson = "GwAAAARBABMAAAAQMAABAAAAEDEAAgAAAAAA";
var simpleArray = Deserialize<SimpleArrayAsList>(bson);
Assert.IsNotNull(simpleArray);
Assert.IsNotNull(simpleArray.A);
Assert.AreEqual(2, simpleArray.A.Count);
Assert.Contains(1, simpleArray.A);
Assert.Contains(2, simpleArray.A);
}
public class SimpleArrayAsListOfInt
{
public List<int> A { get; set; }
}
[Test]
public void CanDeserializeAnSimpleArrayAsListOfInt()
{
const string bson = "GwAAAARBABMAAAAQMAABAAAAEDEAAgAAAAAA";
var simpleArray = Deserialize<SimpleArrayAsListOfInt>(bson);
Assert.IsNotNull(simpleArray);
Assert.IsNotNull(simpleArray.A);
Assert.AreEqual(2, simpleArray.A.Count);
Assert.Contains(1, simpleArray.A);
Assert.Contains(2, simpleArray.A);
}
public class SimpleArrayAsArrayList
{
public ArrayList A { get; set; }
}
[Test]
public void CanDeserializeAnSimpleArrayAsArrayList()
{
const string bson = "GwAAAARBABMAAAAQMAABAAAAEDEAAgAAAAAA";
var simpleArray = Deserialize<SimpleArrayAsArrayList>(bson);
Assert.IsNotNull(simpleArray);
Assert.IsNotNull(simpleArray.A);
Assert.AreEqual(2, simpleArray.A.Count);
Assert.Contains(1, simpleArray.A);
Assert.Contains(2, simpleArray.A);
}
public class SimpleArrayAsIEnumerable
{
public IEnumerable A { get; set; }
}
[Test]
public void CanDeserializeAnSimpleArrayAsIEnumerable()
{
const string bson = "GwAAAARBABMAAAAQMAABAAAAEDEAAgAAAAAA";
var simpleArray = Deserialize<SimpleArrayAsIEnumerable>(bson);
Assert.IsNotNull(simpleArray);
Assert.IsNotNull(simpleArray.A);
var list = new List<object>();
foreach(var value in simpleArray.A)
list.Add(value);
Assert.AreEqual(2, list.Count);
Assert.Contains(1, list);
Assert.Contains(2, list);
}
public class SimpleArrayAsIEnumerableOfInt
{
public IEnumerable<int> A { get; set; }
}
[Test]
public void CanDeserializeAnSimpleArrayAsIEnumerableOfInt()
{
const string bson = "GwAAAARBABMAAAAQMAABAAAAEDEAAgAAAAAA";
var simpleArray = Deserialize<SimpleArrayAsIEnumerableOfInt>(bson);
Assert.IsNotNull(simpleArray);
Assert.IsNotNull(simpleArray.A);
var list = new List<int>(simpleArray.A);
Assert.AreEqual(2, list.Count);
Assert.Contains(1, list);
Assert.Contains(2, list);
}
public class ObjectArray
{
public IList<ObjectArrayA> A { get; set; }
}
public class ObjectArrayA
{
public string B { get; set; }
}
[Test]
public void CanDeserializeAnObjectArray()
{
const string bson = "HgAAAARBABYAAAADMAAOAAAAAkIAAgAAAGIAAAAA";
var objectArray = Deserialize<ObjectArray>(bson);
Assert.IsNotNull(objectArray);
Assert.IsNotNull(objectArray.A);
Assert.AreEqual(1, objectArray.A.Count);
Assert.IsNotNull(objectArray.A[0].B);
Assert.AreEqual("b", objectArray.A[0].B);
}
public class DocumentProperty
{
public Document A { get; set; }
}
[Test]
public void CanDeserializeAnDocumentPreperty()
{
const string bson = "FgAAAANBAA4AAAACQgACAAAAYgAAAA==";
var documentProperty = Deserialize<DocumentProperty>(bson);
Assert.IsNotNull(documentProperty);
Assert.IsNotNull(documentProperty.A);
Assert.AreEqual("b", documentProperty.A["B"]);
}
[Test]
public void CanSerializeAnDocument()
{
const string bson = "DgAAAAJBAAIAAABhAAA=";
var document = Deserialize<Document>(bson);
Assert.IsNotNull(document);
Assert.AreEqual(1, document.Count);
Assert.AreEqual("a", document["A"]);
}
public class DefaultId
{
public int Id { get; set; }
}
[Test]
public void CanDeserializeToDefaultIdProperty(){
const string bson = "DgAAABBfaWQAAQAAAAA=";
var obj = Deserialize<DefaultId>(bson);
Assert.IsNotNull(obj);
Assert.AreEqual(1,obj.Id);
}
public class ConvertPropertyValues
{
public bool Ok{ get; set;}
}
[Test]
public void CanConvertPropertyValues(){
var bson = Serialize(new Document{{"Ok", 1.0}});
var obj = Deserialize<ConvertPropertyValues>(bson);
Assert.IsNotNull(obj);
Assert.AreEqual(true, obj.Ok);
}
public class ConvertPropertyValuesToNullable
{
public int? Value { get; set; }
}
[Test]
public void CanConvertPropertyValuesToNullable(){
var bson = Serialize(new Document("Value", 2));
var obj = Deserialize<ConvertPropertyValuesToNullable>(bson);
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.Value);
Assert.AreEqual(2, obj.Value);
}
[Test]
public void CanConvertPropertyValuesToNull()
{
var bson = Serialize(new Document("Value", null));
var obj = Deserialize<ConvertPropertyValuesToNullable>(bson);
Assert.IsNotNull(obj);
Assert.IsNull(obj.Value);
}
[Test]
public void Test(){
var europeStd = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
var referenceTime = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2010, 1, 1, 10, 0, 0, DateTimeKind.Utc), europeStd);
var bson = Serialize(new Document("Value", referenceTime));
var document = Deserialize<Document>(bson);
Assert.AreEqual(referenceTime, document["Value"]);
}
}
}