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 f3ff1fd

Browse filesBrowse files
committed
Merge branch 'master' into json
Conflicts: MongoDBDriver/Binary.cs
2 parents f2d1fa7 + 5848bd7 commit f3ff1fd
Copy full SHA for f3ff1fd

File tree

6 files changed

+854
-707
lines changed
Filter options

6 files changed

+854
-707
lines changed
+111-84
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,111 @@
1-
using System;
2-
using System.IO;
3-
4-
using MongoDB.Driver.Bson;
5-
6-
using NUnit.Framework;
7-
8-
namespace MongoDB.Driver.Bson
9-
{
10-
[TestFixture]
11-
public class TestRoundTrips
12-
{
13-
14-
[Test]
15-
public void TestDBRef(){
16-
MemoryStream ms = new MemoryStream();
17-
BsonWriter writer = new BsonWriter(ms);
18-
19-
Document source = new Document();
20-
source.Append("x",1).Append("ref",new DBRef("refs","ref1"));
21-
22-
writer.Write(source);
23-
writer.Flush();
24-
ms.Seek(0,SeekOrigin.Begin);
25-
26-
BsonReader reader = new BsonReader(ms);
27-
Document copy = reader.Read();
28-
29-
Assert.IsTrue(copy.Contains("ref"));
30-
Assert.IsTrue(copy["ref"].GetType() == typeof(DBRef));
31-
32-
DBRef sref = (DBRef)source["ref"];
33-
DBRef cref = (DBRef)copy["ref"];
34-
35-
Assert.AreEqual(sref.Id, cref.Id);
36-
37-
}
38-
39-
[Test]
40-
public void TestDateLocal(){
41-
DateTime now = DateTime.Now;
42-
MemoryStream ms = new MemoryStream();
43-
BsonWriter writer = new BsonWriter(ms);
44-
45-
Document source = new Document();
46-
source.Append("d",now);
47-
48-
writer.Write(source);
49-
writer.Flush();
50-
ms.Seek(0,SeekOrigin.Begin);
51-
52-
BsonReader reader = new BsonReader(ms);
53-
Document copy = reader.Read();
54-
55-
DateTime then = (DateTime)copy["d"];
56-
then = then.ToLocalTime();
57-
58-
Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right.");
59-
60-
}
61-
62-
[Test]
63-
public void TestDateUTC(){
64-
DateTime now = DateTime.UtcNow;
65-
MemoryStream ms = new MemoryStream();
66-
BsonWriter writer = new BsonWriter(ms);
67-
68-
Document source = new Document();
69-
source.Append("d",now);
70-
71-
writer.Write(source);
72-
writer.Flush();
73-
ms.Seek(0,SeekOrigin.Begin);
74-
75-
BsonReader reader = new BsonReader(ms);
76-
Document copy = reader.Read();
77-
78-
DateTime then = (DateTime)copy["d"];
79-
80-
Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right.");
81-
82-
}
83-
}
84-
}
1+
using System;
2+
using System.IO;
3+
4+
using MongoDB.Driver.Bson;
5+
6+
using NUnit.Framework;
7+
8+
namespace MongoDB.Driver.Bson
9+
{
10+
[TestFixture]
11+
public class TestRoundTrips
12+
{
13+
14+
[Test]
15+
public void TestDBRef(){
16+
MemoryStream ms = new MemoryStream();
17+
BsonWriter writer = new BsonWriter(ms);
18+
19+
Document source = new Document();
20+
source.Append("x",1).Append("ref",new DBRef("refs","ref1"));
21+
22+
writer.Write(source);
23+
writer.Flush();
24+
ms.Seek(0,SeekOrigin.Begin);
25+
26+
BsonReader reader = new BsonReader(ms);
27+
Document copy = reader.Read();
28+
29+
Assert.IsTrue(copy.Contains("ref"));
30+
Assert.IsTrue(copy["ref"].GetType() == typeof(DBRef));
31+
32+
DBRef sref = (DBRef)source["ref"];
33+
DBRef cref = (DBRef)copy["ref"];
34+
35+
Assert.AreEqual(sref.Id, cref.Id);
36+
37+
}
38+
39+
[Test]
40+
public void TestDateLocal(){
41+
DateTime now = DateTime.Now;
42+
MemoryStream ms = new MemoryStream();
43+
BsonWriter writer = new BsonWriter(ms);
44+
45+
Document source = new Document();
46+
source.Append("d",now);
47+
48+
writer.Write(source);
49+
writer.Flush();
50+
ms.Seek(0,SeekOrigin.Begin);
51+
52+
BsonReader reader = new BsonReader(ms);
53+
Document copy = reader.Read();
54+
55+
DateTime then = (DateTime)copy["d"];
56+
then = then.ToLocalTime();
57+
58+
Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right.");
59+
60+
}
61+
62+
[Test]
63+
public void TestDateUTC(){
64+
DateTime now = DateTime.UtcNow;
65+
MemoryStream ms = new MemoryStream();
66+
BsonWriter writer = new BsonWriter(ms);
67+
68+
Document source = new Document();
69+
source.Append("d",now);
70+
71+
writer.Write(source);
72+
writer.Flush();
73+
ms.Seek(0,SeekOrigin.Begin);
74+
75+
BsonReader reader = new BsonReader(ms);
76+
Document copy = reader.Read();
77+
78+
DateTime then = (DateTime)copy["d"];
79+
80+
Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right.");
81+
82+
}
83+
84+
[Test]
85+
public void TestGUID()
86+
{
87+
MemoryStream ms = new MemoryStream();
88+
BsonWriter writer = new BsonWriter(ms);
89+
90+
Guid guid = Guid.NewGuid();
91+
92+
Document source = new Document();
93+
source.Append("uuid", guid);
94+
95+
/*Binary b = new Binary(guid.ToByteArray());
96+
b.Subtype = Binary.TypeCode.Uuid;
97+
source.Append("uuid", b);*/
98+
99+
writer.Write(source);
100+
writer.Flush();
101+
ms.Seek(0, SeekOrigin.Begin);
102+
103+
BsonReader reader = new BsonReader(ms);
104+
Document copy = reader.Read();
105+
106+
Guid read = (Guid)copy["uuid"];
107+
108+
Assert.AreEqual(guid, read, "UUID did not round trip right.");
109+
}
110+
}
111+
}

‎MongoDBDriver/Binary.cs

Copy file name to clipboard
+40-34
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
5-
namespace MongoDB.Driver
6-
{
7-
public class Binary{
8-
public enum TypeCode:byte{
9-
Unknown = 0,
10-
General = 2,
11-
Uuid = 3,
12-
Md5 = 5,
13-
UserDefined = 80
14-
}
15-
16-
public byte[] Bytes{get;set;}
17-
18-
public Binary.TypeCode Subtype{get;set;}
19-
20-
public Binary() { }
21-
22-
public Binary(byte[] value){
23-
this.Bytes = value;
24-
this.Subtype = TypeCode.General;
25-
}
26-
27-
public override string ToString (){
28-
return String.Format(@"{{ ""$binary"" : ""{0}"", ""$type"" : ""{1}"" }}",
29-
Convert.ToBase64String(Bytes), Subtype);
30-
}
31-
32-
33-
}
34-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MongoDB.Driver
6+
{
7+
public class Binary{
8+
public enum TypeCode:byte{
9+
Unknown = 0,
10+
General = 2,
11+
// Uuid is now replaced by Guid
12+
//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+
22+
}
23+
24+
private Binary.TypeCode subtype;
25+
public Binary.TypeCode Subtype{
26+
get { return this.subtype; }
27+
set { this.subtype = value; }
28+
}
29+
30+
public Binary() { }
31+
32+
public Binary(byte[] value){
33+
this.Bytes = value;
34+
this.Subtype = TypeCode.General;
35+
}
36+
37+
38+
39+
}
40+
}

0 commit comments

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