forked from mongodb/mongo-csharp-driver
/
NullableTypeSerializerTests.cs
209 lines (182 loc) · 7.24 KB
/
NullableTypeSerializerTests.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
/* Copyright 2010-2012 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.BsonUnitTests.Serialization
{
[TestFixture]
public class NullableTypeSerializerTests
{
private class C
{
public bool? Boolean { get; set; }
public DateTime? DateTime { get; set; }
[BsonDateTimeOptions(DateOnly = true)]
public DateTime? DateOnly { get; set; }
public double? Double { get; set; }
public Guid? Guid { get; set; }
public int? Int32 { get; set; }
public long? Int64 { get; set; }
public ObjectId? ObjectId { get; set; }
[BsonRepresentation(BsonType.String)]
public ConsoleColor? Enum { get; set; }
// public Struct? Struct { get; set; }
}
//private struct Struct {
// public string StructP { get; set; }
//}
private const string _template =
"{ " +
"'Boolean' : null, " +
"'DateTime' : null, " +
"'DateOnly' : null, " +
"'Double' : null, " +
"'Guid' : null, " +
"'Int32' : null, " +
"'Int64' : null, " +
"'ObjectId' : null, " +
"'Enum' : null" +
// "'Struct' : null" +
" }";
[Test]
public void TestAllNulls()
{
C c = new C();
var json = c.ToJson();
var expected = _template.Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
[Test]
public void TestBoolean()
{
C c = new C { Boolean = true };
var json = c.ToJson();
var expected = _template.Replace("'Boolean' : null", "'Boolean' : true").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
[Test]
public void TestDateTime()
{
C c = new C { DateTime = BsonConstants.UnixEpoch };
var json = c.ToJson();
var expected = _template.Replace("'DateTime' : null", "'DateTime' : ISODate('1970-01-01T00:00:00Z')").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
[Test]
public void TestDateOnly()
{
C c = new C { DateOnly = BsonConstants.UnixEpoch };
var json = c.ToJson();
var expected = _template.Replace("'DateOnly' : null", "'DateOnly' : ISODate('1970-01-01T00:00:00Z')").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
[Test]
public void TestDouble()
{
C c = new C { Double = 1.5 };
var json = c.ToJson();
var expected = _template.Replace("'Double' : null", "'Double' : 1.5").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
[Test]
public void TestEnum()
{
var c = new C { Enum = ConsoleColor.Red };
var json = c.ToJson();
var expected = _template.Replace("'Enum' : null", "'Enum' : 'Red'").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
[Test]
public void TestGuid()
{
C c = new C { Guid = Guid.Empty };
var json = c.ToJson();
var expected = _template.Replace("'Guid' : null", "'Guid' : CSUUID('00000000-0000-0000-0000-000000000000')").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
[Test]
public void TestInt32()
{
C c = new C { Int32 = 1 };
var json = c.ToJson();
var expected = _template.Replace("'Int32' : null", "'Int32' : 1").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
[Test]
public void TestInt64()
{
C c = new C { Int64 = 2 };
var json = c.ToJson();
var expected = _template.Replace("'Int64' : null", "'Int64' : NumberLong(2)").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
[Test]
public void TestObjectId()
{
C c = new C { ObjectId = ObjectId.Empty };
var json = c.ToJson();
var expected = _template.Replace("'ObjectId' : null", "'ObjectId' : ObjectId('000000000000000000000000')").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = c.ToBson();
var rehydrated = BsonSerializer.Deserialize<C>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
//[Test]
//public void TestStruct() {
// C c = new C { Struct = new Struct { StructP = "x" } };
// var json = c.ToJson();
// var expected = template.Replace("'Struct' : null", "'Struct' : { 'StructP' : 'x' }").Replace("'", "\"");
// Assert.AreEqual(expected, json);
// var bson = c.ToBson();
// var rehydrated = BsonSerializer.Deserialize<C>(bson);
// Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
//}
}
}