Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a67c615

Browse filesBrowse files
committed
Added Json tests
Added special json formatting for guid/uids.
1 parent f3ff1fd commit a67c615
Copy full SHA for a67c615

File tree

6 files changed

+71
-26
lines changed
Filter options

6 files changed

+71
-26
lines changed

‎MongoDB.Net-Tests/MongoDB.Driver.Tests.csproj

Copy file name to clipboardExpand all lines: MongoDB.Net-Tests/MongoDB.Driver.Tests.csproj
+1-1
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@
8787
<Compile Include="TestCollectionSafeMode.cs" />
8888
<Compile Include="MongoTestBase.cs" />
8989
<Compile Include="TestCollectionMetaData.cs" />
90-
<Compile Include="Util\TestJsonEscaper.cs" />
9190
<Compile Include="TestBinary.cs" />
91+
<Compile Include="Util\TestJsonUtils.cs" />
9292
</ItemGroup>
9393
<ItemGroup>
9494
<ProjectReference Include="..\MongoDBDriver\MongoDB.Driver.csproj">

‎MongoDB.Net-Tests/TestBinary.cs

Copy file name to clipboardExpand all lines: MongoDB.Net-Tests/TestBinary.cs
+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class TestBinary
1010

1111
[Test]
1212
public void TestToString(){
13-
//TODO: Write the test.
13+
Binary b = new Binary();
1414
}
1515
}
1616
}

‎MongoDB.Net-Tests/Util/TestJsonEscaper.cs renamed to ‎MongoDB.Net-Tests/Util/TestJsonUtils.cs

Copy file name to clipboardExpand all lines: MongoDB.Net-Tests/Util/TestJsonUtils.cs
+40
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,45 @@ public void TestSerializeDocWithArrayOfDocs() {
120120
Assert.AreEqual(@"{ ""foo"": [ { ""a"": 1 }, { ""b"": 2 }, { ""c"": 3 } ] }", JsonUtils.Serialize(doc));
121121
}
122122

123+
[Test]
124+
public void TestSerializeDocWithBinary(){
125+
var doc = new Document(){{"b", new Binary(){Bytes = new byte[]{0,1,2,3,4},
126+
Subtype = Binary.TypeCode.General}}};
127+
Assert.AreEqual(@"{ ""b"": { ""$binary"": ""AAECAwQ="", ""$type"" : 2 } }",
128+
JsonUtils.Serialize(doc));
129+
}
130+
131+
[Test]
132+
public void TestSerializeDocWithGUID(){
133+
var doc = new Document(){{"guid", new Guid("936da01f-9abd-4d9d-80c7-02af85c822a8")}};
134+
Assert.AreEqual(@"{ ""guid"": { ""$uid"": ""936da01f-9abd-4d9d-80c7-02af85c822a8"" } }",
135+
JsonUtils.Serialize(doc));
136+
}
137+
138+
[Test]
139+
public void TestSerializeDocWithDBRef(){
140+
var ostr = "000102030405060708090001";
141+
var doc = new Document(){{"d", new DBRef("smallreads", new Oid(ostr))}};
142+
Assert.AreEqual(String.Format(@"{{ ""d"": {{ ""$ref"": ""smallreads"", ""$id"": ""{0}"" }} }}", ostr),
143+
JsonUtils.Serialize(doc));
144+
}
145+
146+
[Test]
147+
public void TestSerializeDocWithMinMax(){
148+
var doc = new Document(){{"mn", MongoMinKey.Value}, {"mx", MongoMaxKey.Value}};
149+
Assert.AreEqual(@"{ ""mn"": { ""$minkey"": 1 }, ""mx"": { ""$maxkey"": 1 } }",
150+
JsonUtils.Serialize(doc));
151+
}
152+
153+
[Test]
154+
public void TestSerializeDocWithCode(){
155+
var c = new Code("function add(x, y){\n" +
156+
" return x + y;\n" +
157+
"}\n");
158+
var doc = new Document(){{"c", c}};
159+
Assert.AreEqual(@"{ ""c"": { $code : ""function add(x, y){\n return x + y;\n}\n"" } }",
160+
JsonUtils.Serialize(doc));
161+
}
162+
123163
}
124164
}

‎MongoDBDriver/Binary.cs

Copy file name to clipboardExpand all lines: MongoDBDriver/Binary.cs
+14-21
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,19 @@
44

55
namespace MongoDB.Driver
66
{
7-
public class Binary{
8-
public enum TypeCode:byte{
9-
Unknown = 0,
10-
General = 2,
11-
// Uuid is now replaced by Guid
7+
public class Binary{
8+
public enum TypeCode:byte{
9+
Unknown = 0,
10+
General = 2,
11+
// Uuid is now replaced by Guid
1212
//Uuid = 3,
13-
Md5 = 5,
14-
UserDefined = 80
15-
}
16-
17-
private byte[] bytes;
18-
public byte[] Bytes{
19-
get { return this.bytes; }
20-
set { this.bytes = value; }
21-
13+
Md5 = 5,
14+
UserDefined = 80
2215
}
16+
17+
public byte[] Bytes{get;set;}
2318

24-
private Binary.TypeCode subtype;
25-
public Binary.TypeCode Subtype{
26-
get { return this.subtype; }
27-
set { this.subtype = value; }
28-
}
19+
public Binary.TypeCode Subtype{get;set;}
2920

3021
public Binary() { }
3122

@@ -34,7 +25,9 @@ public Binary(byte[] value){
3425
this.Subtype = TypeCode.General;
3526
}
3627

37-
38-
28+
public override string ToString (){
29+
return String.Format(@"{{ ""$binary"": ""{0}"", ""$type"" : {1} }}",
30+
Convert.ToBase64String(Bytes), (int)Subtype);
31+
}
3932
}
4033
}

‎MongoDBDriver/MongoMinMaxKey.cs

Copy file name to clipboardExpand all lines: MongoDBDriver/MongoMinMaxKey.cs
+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static MongoMinKey Value {
1414
protected MongoMinKey (){}
1515

1616
public override string ToString (){
17-
return "{ \"$minkey\" : 1 }";
17+
return "{ \"$minkey\": 1 }";
1818
}
1919

2020
}
@@ -33,7 +33,7 @@ public static MongoMaxKey Value {
3333
protected MongoMaxKey (){}
3434

3535
public override string ToString (){
36-
return "{ \"$maxkey\" : 1 }";
36+
return "{ \"$maxkey\": 1 }";
3737
}
3838
}
3939
}

‎MongoDBDriver/Util/JsonUtils.cs

Copy file name to clipboardExpand all lines: MongoDBDriver/Util/JsonUtils.cs
+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Text;
45

6+
57
namespace MongoDB.Driver
68
{
79
/// <summary>
@@ -48,6 +50,12 @@ private static void SerializeType(object value, StringBuilder json) {
4850
json.Append(" ]");
4951
} else if (value is Document ||
5052
value is Oid ||
53+
value is Binary ||
54+
value is DBRef ||
55+
value is MongoMinKey ||
56+
value is MongoMaxKey ||
57+
value is Code ||
58+
value is CodeWScope ||
5159
value is int ||
5260
value is Int32 ||
5361
value is long ||
@@ -56,7 +64,11 @@ value is float ||
5664
json.Append(value);
5765
} else if (value is DateTime) {
5866
json.AppendFormat(@"""{0}""", ((DateTime)value).ToUniversalTime().ToString("o"));
59-
} else {
67+
} else if (value is Guid) {
68+
json.Append(String.Format(@"{{ ""$uid"": ""{0}"" }}",value.ToString()));
69+
} else if (value is IFormattable) {
70+
json.Append(((IFormattable)value).ToString("G", CultureInfo.InvariantCulture));
71+
} else {
6072
json.AppendFormat(@"""{0}""", Escape(value.ToString()));
6173
}
6274
return;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.