Skip to content

Commit

Permalink
Changed mixed array types in the database to return Object[] instead …
Browse files Browse the repository at this point in the history
…of a Document.
  • Loading branch information
samus committed Mar 2, 2010
1 parent c400ff9 commit 6c4dbe3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
94 changes: 50 additions & 44 deletions MongoDB.Net-Tests/Bson/TestRoundTrips.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;

using MongoDB.Driver.Bson;
Expand All @@ -13,18 +13,10 @@ public class TestRoundTrips

[Test]
public void TestDBRef(){
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);

Document source = new Document();
source.Append("x",1).Append("ref",new DBRef("refs","ref1"));

writer.Write(source);
writer.Flush();
ms.Seek(0,SeekOrigin.Begin);

BsonReader reader = new BsonReader(ms);
Document copy = reader.Read();
Document copy = WriteAndRead(source);

Assert.IsTrue(copy.Contains("ref"));
Assert.IsTrue(copy["ref"].GetType() == typeof(DBRef));
Expand All @@ -39,18 +31,11 @@ public void TestDBRef(){
[Test]
public void TestDateLocal(){
DateTime now = DateTime.Now;
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);

Document source = new Document();
source.Append("d",now);

writer.Write(source);
writer.Flush();
ms.Seek(0,SeekOrigin.Begin);

BsonReader reader = new BsonReader(ms);
Document copy = reader.Read();
Document copy = WriteAndRead(source);

DateTime then = (DateTime)copy["d"];
then = then.ToLocalTime();
Expand All @@ -62,50 +47,71 @@ public void TestDateLocal(){
[Test]
public void TestDateUTC(){
DateTime now = DateTime.UtcNow;
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);


Document source = new Document();
source.Append("d",now);

writer.Write(source);
writer.Flush();
ms.Seek(0,SeekOrigin.Begin);

BsonReader reader = new BsonReader(ms);
Document copy = reader.Read();

Document copy = WriteAndRead(source);
DateTime then = (DateTime)copy["d"];

Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right.");

}

[Test]
public void TestGUID()
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);

Guid guid = Guid.NewGuid();
public void TestGUID(){
Guid expected = Guid.NewGuid();

Document source = new Document();
source.Append("uuid", guid);
source.Append("uuid", expected);

Guid read = (Guid)(WriteAndRead(source)["uuid"]);

/*Binary b = new Binary(guid.ToByteArray());
b.Subtype = Binary.TypeCode.Uuid;
source.Append("uuid", b);*/
Assert.AreEqual(expected, read, "UUID did not round trip right.");
}

[Test]
public void TestMultiDimensionalArray(){
int[][] arr = new int[3][];
for(int a = 0; a < arr.Length; a++){
int x = a + 1;
arr[a] = new int[]{x * 1, x * 2, x * 3};
}

Document expected = new Document(){{"arr", arr}};
Document read = WriteAndRead(expected);

Assert.AreEqual(expected.ToString(), read.ToString());
}

[Test]
public void TestMixedArrayContents(){
Object[] arr = new Object[]{new string[]{"one", "two"},
new string[]{"three", "four"},
new Document(){{"id", "six"}}};
Document expected = new Document(){{"arr", arr}};
Document read = WriteAndRead(expected);

string json = @"{ ""arr"": [ [ ""one"", ""two"" ], [ ""three"", ""four"" ], { ""id"": ""six"" } ] }";
Assert.AreEqual(json, expected.ToString());

Assert.IsTrue(read["arr"] is Object[], "Mixed array wasn't returned as Object[]");

Assert.AreEqual(json, read.ToString());


}

protected Document WriteAndRead(Document source){
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);

writer.Write(source);
writer.Flush();
ms.Seek(0, SeekOrigin.Begin);

BsonReader reader = new BsonReader(ms);
Document copy = reader.Read();

Guid read = (Guid)copy["uuid"];

Assert.AreEqual(guid, read, "UUID did not round trip right.");
}
return reader.Read();
}
}
}
12 changes: 6 additions & 6 deletions MongoDBDriver/Bson/BsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ public Object ReadElementType (sbyte typeNum){

case BsonDataType.Array:{
Document doc = this.ReadDocument();
if (ElementsSameType (doc)) {
return ConvertToArray (doc);
} else {
return doc;
}
return ConvertToArray (doc);
}
case BsonDataType.Regex:{
MongoRegex r = new MongoRegex ();
Expand Down Expand Up @@ -274,7 +270,11 @@ private Object ConvertToArray (Document doc){
foreach (String key in doc.Keys) {
if (ret == null) {
int length = doc.Keys.Count;
arrayType = doc[key].GetType ();
if(ElementsSameType(doc)){
arrayType = doc[key].GetType();
}else{
arrayType = typeof(Object);
}
ret = Array.CreateInstance (arrayType, length);
}
ret.SetValue (doc[key], idx);
Expand Down

0 comments on commit 6c4dbe3

Please sign in to comment.