forked from samus/mongodb-csharp
/
TestJsonUtils.cs
175 lines (151 loc) · 6.89 KB
/
TestJsonUtils.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
using System;
using System.Globalization;
using System.Threading;
using MongoDB.Util;
using NUnit.Framework;
namespace MongoDB.UnitTests.Util
{
[TestFixture]
public class TestJsonUtils
{
[Test]
public void TestNothingToEscape(){
string str = "There is nothing here to escape";
Assert.AreEqual(str, JsonFormatter.Escape(str));
}
[Test]
public void TestAsciiToEscape(){
string str = "\b \f \n \r \t \v \' \" \\";
string expected = @"\b \f \n \r \t \v \' \"" \\";
Assert.AreEqual(expected, JsonFormatter.Escape(str));
}
[Test]
public void TestPrintableUnicode(){
string str = "pi (π)";
Assert.AreEqual(str, JsonFormatter.Escape(str));
}
[Test]
public void TestNonPrintableUnicode(){
string str = "\u0007 bell";
string expected = @"\u0007 bell";
Assert.AreEqual(expected, JsonFormatter.Escape(str));
}
[Test]
public void TestSerializeDocWithSingleNullField() {
var doc = new Document().Add("foo", null);
Assert.AreEqual(@"{ ""foo"": null }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithSingleTrueField() {
var doc = new Document().Add("foo", true);
Assert.AreEqual(@"{ ""foo"": true }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithSingleFalseField() {
var doc = new Document().Add("foo", false);
Assert.AreEqual(@"{ ""foo"": false }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithSingleStringField() {
var doc = new Document().Add("foo", "bar");
Assert.AreEqual(@"{ ""foo"": ""bar"" }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithLowUnicodeValues(){
var doc = new Document(){{"foo", "\u0007 bell"}};
Assert.AreEqual(@"{ ""foo"": ""\u0007 bell"" }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithHighUnicodeValues(){
var doc = new Document(){{"foo", "pi (π)"}};
Assert.AreEqual(@"{ ""foo"": ""pi (π)"" }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithSingleIntField() {
var doc = new Document().Add("foo", 10);
Assert.AreEqual(@"{ ""foo"": 10 }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithSingleDoubleField() {
var doc = new Document().Add("foo", 10.1);
Assert.AreEqual(@"{ ""foo"": 10.1 }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeCultureInvariantNumbers() {
var cultureBackup = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
var doc = new Document().Add("foo", 10.1);
Assert.AreEqual(@"{ ""foo"": 10.1 }", JsonFormatter.Serialize(doc));
Thread.CurrentThread.CurrentCulture = cultureBackup;
}
[Test]
public void TestSerializeDocWithSingleDateTimeField() {
var doc = new Document().Add("foo", DateTime.Parse("2009-10-10T07:00:00.0000000Z"));
Assert.AreEqual(@"{ ""foo"": ""2009-10-10T07:00:00.0000000Z"" }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithSingleOidField() {
var doc = new Document().Add("foo", new Oid("4ac7ee91f693066f1c96b649"));
Assert.AreEqual(@"{ ""foo"": ""4ac7ee91f693066f1c96b649"" }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithMultipleFields() {
var doc = new Document().Add("foo", "bar").Add("baz", 42);
Assert.AreEqual(@"{ ""foo"": ""bar"", ""baz"": 42 }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithSubDocField() {
var doc = new Document().Add("foo", "bar").Add("baz", new Document().Add("a", 1));
Assert.AreEqual(@"{ ""foo"": ""bar"", ""baz"": { ""a"": 1 } }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithArrayOfInts() {
var doc = new Document().Add("foo", new[] { 1, 2, 3, 4 });
Assert.AreEqual(@"{ ""foo"": [ 1, 2, 3, 4 ] }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithArrayOfDocs() {
var doc = new Document().Add("foo", new[] {
new Document().Add("a", 1),
new Document().Add("b", 2),
new Document().Add("c", 3),
});
Assert.AreEqual(@"{ ""foo"": [ { ""a"": 1 }, { ""b"": 2 }, { ""c"": 3 } ] }", JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithBinary(){
var doc = new Document(){{"b", new Binary(){Bytes = new byte[]{0,1,2,3,4},
Subtype = BinarySubtype.General}}};
Assert.AreEqual(@"{ ""b"": { ""$binary"": ""AAECAwQ="", ""$type"" : 2 } }",
JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithGUID(){
var doc = new Document(){{"guid", new Guid("936da01f-9abd-4d9d-80c7-02af85c822a8")}};
Assert.AreEqual(@"{ ""guid"": { ""$uid"": ""936da01f-9abd-4d9d-80c7-02af85c822a8"" } }",
JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithDBRef(){
var ostr = "000102030405060708090001";
var doc = new Document(){{"d", new DBRef("smallreads", new Oid(ostr))}};
Assert.AreEqual(String.Format(@"{{ ""d"": {{ ""$ref"": ""smallreads"", ""$id"": ""{0}"" }} }}", ostr),
JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithMinMax(){
var doc = new Document(){{"mn", MongoMinKey.Value}, {"mx", MongoMaxKey.Value}};
Assert.AreEqual(@"{ ""mn"": { ""$minkey"": 1 }, ""mx"": { ""$maxkey"": 1 } }",
JsonFormatter.Serialize(doc));
}
[Test]
public void TestSerializeDocWithCode(){
var c = new Code("function add(x, y){\n" +
" return x + y;\n" +
"}\n");
var doc = new Document(){{"c", c}};
Assert.AreEqual(@"{ ""c"": { ""$code"": ""function add(x, y){\n return x + y;\n}\n"" } }",
JsonFormatter.Serialize(doc));
}
}
}