Permalink
Browse files

Changed mixed array types in the database to return Object[] instead …

…of a Document.
  • Loading branch information...
1 parent c400ff9 commit 6c4dbe3a65ea65753b66ba586a5642ecc39790fa @samus committed Mar 2, 2010
Showing with 56 additions and 50 deletions.
  1. +50 −44 MongoDB.Net-Tests/Bson/TestRoundTrips.cs
  2. +6 −6 MongoDBDriver/Bson/BsonReader.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.IO;
using MongoDB.Driver.Bson;
@@ -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));
@@ -39,18 +31,11 @@ public class TestRoundTrips
[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();
@@ -62,50 +47,71 @@ public class TestRoundTrips
[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();
+ }
}
}
@@ -108,11 +108,7 @@ public Document Read ()
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 ();
@@ -274,7 +270,11 @@ public Document Read ()
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);

0 comments on commit 6c4dbe3

Please sign in to comment.