forked from samus/mongodb-csharp
/
SerializationFactoryTests.cs
249 lines (202 loc) · 8.71 KB
/
SerializationFactoryTests.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
using System;
using System.Collections.Generic;
using MongoDB.Configuration;
using MongoDB.Serialization;
using NUnit.Framework;
namespace MongoDB.UnitTests.Serialization
{
[TestFixture]
public class SerializationFactoryTests : SerializationTestBase
{
[Test]
public void GetBsonReaderSettingsDefaults()
{
var factory = new SerializationFactory();
var readerSettings = factory.GetBsonReaderSettings(typeof(int));
Assert.AreEqual(readerSettings.ReadLocalTime,true);
Assert.IsNotNull(readerSettings.Builder);
}
[Test]
public void ReadLocalTimeCanBeChangedByConfig()
{
var factory = new SerializationFactory(new MongoConfiguration {ReadLocalTime = false});
var readerSettings = factory.GetBsonReaderSettings(typeof(int));
Assert.AreEqual(readerSettings.ReadLocalTime, false);
}
public class ProtectedConstructor
{
protected ProtectedConstructor(){}
}
[Test]
public void CanCreateObjectFromProtectedConstructor()
{
var obj = Deserialize<ProtectedConstructor>(EmptyDocumentBson);
Assert.IsNotNull(obj);
}
public class PrivateConstructor
{
private PrivateConstructor() { }
}
[Test]
[ExpectedException(typeof(MissingMethodException))]
public void CanNotCreateObjectFromPrivateConstructor()
{
var obj = Deserialize<PrivateConstructor>(EmptyDocumentBson);
Assert.IsNotNull(obj);
}
public class SetProtectedPropertys
{
protected double Property { get; set; }
public double GetProperty() {return Property; }
}
[Test]
public void CanSetProtectedProperty()
{
var bson = Serialize(new Document("Property", 4));
var prop = Deserialize<SetProtectedPropertys>(bson);
Assert.IsNotNull(prop);
Assert.AreEqual(4, prop.GetProperty());
}
public class SetPrivatePropertys
{
private double Property { get; set; }
public double GetProperty() { return Property; }
}
[Test]
public void CanNotSetPrivatePropertys()
{
var bson = Serialize(new Document("Property", 4));
var prop = Deserialize<SetPrivatePropertys>(bson);
Assert.IsNotNull(prop);
Assert.AreEqual(0, prop.GetProperty());
}
public class NullableProperty
{
public double? Value { get; set; }
}
[Test]
public void CanSetNullOnNullablPropertys()
{
var bson = Serialize(new Document("Value", null));
var obj = Deserialize<NullableProperty>(bson);
Assert.IsNotNull(obj);
Assert.IsNull(obj.Value);
}
[Test]
public void CanSetValueOnNullablPropertys()
{
var bson = Serialize(new Document("Value", 10));
var obj = Deserialize<NullableProperty>(bson);
Assert.IsNotNull(obj);
Assert.AreEqual(10,obj.Value);
}
public class GenericDictionary
{
public Dictionary<string, int> Property { get; set; }
}
[Test]
public void CanSerializeGenericDictionary()
{
var expectedBson = Serialize<Document>(new Document("Property", new Document() { { "key1", 10 }, { "key2", 20 } }));
var obj = new GenericDictionary { Property = new Dictionary<string, int> { { "key1", 10 }, { "key2", 20 } } };
var bson = Serialize<GenericDictionary>(obj);
Assert.AreEqual(expectedBson, bson);
}
[Test]
public void CanDeserializeGenericDictionary()
{
var bson = Serialize<Document>(new Document("Property", new Document() { { "key1", 10 }, { "key2", 20 } }));
var prop = Deserialize<GenericDictionary>(bson);
Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Property);
Assert.AreEqual(2,prop.Property.Count);
Assert.Contains(new KeyValuePair<string, int>("key1", 10), prop.Property);
Assert.Contains(new KeyValuePair<string, int>("key2", 20), prop.Property);
}
public class GenericStringDictionaryWithComplexType
{
public Dictionary<string, GenericDictionaryComplexType> Dict { get; set; }
}
public class GenericDictionaryComplexType
{
public string Name { get; set; }
}
[Test]
public void CanSerializeStringGenericDictionaryWithComplexType()
{
var expectedBson = Serialize<Document>(new Document("Dict", new Document { { "key1", new Document("Name", "a") }, { "key2", new Document("Name", "b") } }));
var obj = new GenericStringDictionaryWithComplexType { Dict = new Dictionary<string, GenericDictionaryComplexType> { { "key1", new GenericDictionaryComplexType { Name = "a" } }, { "key2", new GenericDictionaryComplexType { Name = "b" } } } };
var bson = Serialize<GenericStringDictionaryWithComplexType>(obj);
Assert.AreEqual(expectedBson, bson);
}
[Test]
public void CanDeserializeStringGenericDictionaryWithComplexType()
{
var bson = Serialize<Document>(new Document("Dict", new Document { { "key1", new Document("Name", "a") }, { "key2", new Document("Name", "b") } }));
var prop = Deserialize<GenericStringDictionaryWithComplexType>(bson);
Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Dict);
Assert.AreEqual(2, prop.Dict.Count);
Assert.IsTrue(prop.Dict["key1"].Name == "a");
Assert.IsTrue(prop.Dict["key2"].Name == "b");
}
public class GenericIntDictionaryWithComplexType
{
public Dictionary<int, GenericDictionaryComplexType> Dict { get; set; }
}
[Test]
public void CanSerializeIntGenericDictionaryWithComplexType()
{
var expectedBson = Serialize<Document>(new Document("Dict", new Document { { "1", new Document("Name", "a") }, { "2", new Document("Name", "b") } }));
var obj = new GenericIntDictionaryWithComplexType { Dict = new Dictionary<int, GenericDictionaryComplexType> { { 1, new GenericDictionaryComplexType { Name = "a" } }, { 2, new GenericDictionaryComplexType { Name = "b" } } } };
var bson = Serialize<GenericIntDictionaryWithComplexType>(obj);
Assert.AreEqual(expectedBson, bson);
}
[Test]
public void CanDeserializeIntGenericDictionaryWithComplexType()
{
var bson = Serialize<Document>(new Document("Dict", new Document { { "1", new Document("Name", "a") }, { "2", new Document("Name", "b") } }));
var prop = Deserialize<GenericIntDictionaryWithComplexType>(bson);
Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Dict);
Assert.AreEqual(2, prop.Dict.Count);
Assert.IsTrue(prop.Dict[1].Name == "a");
Assert.IsTrue(prop.Dict[2].Name == "b");
}
public class HashSetHelper
{
public HashSet<string> Property { get; set; }
}
[Test]
public void CanSerializeAndDeserializeHashSet()
{
var obj = new HashSetHelper {Property = new HashSet<string> {"test1", "test2"}};
var bson = Serialize<HashSetHelper>(obj);
var prop = Deserialize<HashSetHelper>(bson);
Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Property);
Assert.AreEqual(2, prop.Property.Count);
Assert.IsTrue(prop.Property.Contains("test1"));
Assert.IsTrue(prop.Property.Contains("test2"));
}
public class EnumHelper
{
public enum Test
{
A=1,
B=2
}
public List<Test> Tests { get; set; }
}
[Test]
public void CanSerializerAndDesializeEnumLists()
{
var helper = new EnumHelper {Tests = new List<EnumHelper.Test> {EnumHelper.Test.A}};
var bson = Serialize<EnumHelper>(helper);
var deserialize = Deserialize<EnumHelper>(bson);
Assert.IsNotNull(deserialize);
Assert.IsNotNull(deserialize.Tests);
Assert.Contains(EnumHelper.Test.A, deserialize.Tests);
}
}
}