Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…of a Document.
  • Loading branch information...
commit 6c4dbe3a65ea65753b66ba586a5642ecc39790fa 1 parent c400ff9
Sam Corder authored
94  MongoDB.Net-Tests/Bson/TestRoundTrips.cs
... ...
@@ -1,4 +1,4 @@
1  
-using System;
  1
+using System;
2 2
 using System.IO;
3 3
 
4 4
 using MongoDB.Driver.Bson;
@@ -13,18 +13,10 @@ public class TestRoundTrips
13 13
 
14 14
         [Test]
15 15
         public void TestDBRef(){
16  
-            MemoryStream ms = new MemoryStream();
17  
-            BsonWriter writer = new BsonWriter(ms);
18  
-
19 16
             Document source = new Document();
20 17
             source.Append("x",1).Append("ref",new DBRef("refs","ref1"));
21 18
 
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();
  19
+            Document copy = WriteAndRead(source);
28 20
 
29 21
             Assert.IsTrue(copy.Contains("ref"));
30 22
             Assert.IsTrue(copy["ref"].GetType() == typeof(DBRef));
@@ -39,18 +31,11 @@ public class TestRoundTrips
39 31
         [Test]
40 32
         public void TestDateLocal(){
41 33
             DateTime now = DateTime.Now;
42  
-            MemoryStream ms = new MemoryStream();
43  
-            BsonWriter writer = new BsonWriter(ms);
44 34
 
45 35
             Document source = new Document();
46 36
             source.Append("d",now);
47 37
 
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();
  38
+            Document copy = WriteAndRead(source);
54 39
             
55 40
             DateTime then = (DateTime)copy["d"];           
56 41
             then = then.ToLocalTime();
@@ -62,19 +47,11 @@ public class TestRoundTrips
62 47
         [Test]
63 48
         public void TestDateUTC(){
64 49
             DateTime now = DateTime.UtcNow;
65  
-            MemoryStream ms = new MemoryStream();
66  
-            BsonWriter writer = new BsonWriter(ms);
67  
-
  50
+            
68 51
             Document source = new Document();
69 52
             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 53
             
  54
+            Document copy = WriteAndRead(source);           
78 55
             DateTime then = (DateTime)copy["d"];           
79 56
             
80 57
             Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right.");
@@ -82,30 +59,59 @@ public class TestRoundTrips
82 59
         }
83 60
 
84 61
         [Test]
85  
-        public void TestGUID()
86  
-        {
87  
-            MemoryStream ms = new MemoryStream();
88  
-            BsonWriter writer = new BsonWriter(ms);
89  
-
90  
-            Guid guid = Guid.NewGuid();
  62
+        public void TestGUID(){
  63
+            Guid expected = Guid.NewGuid();
91 64
 
92 65
             Document source = new Document();
93  
-            source.Append("uuid", guid);
  66
+            source.Append("uuid", expected);
  67
+
  68
+            Guid read = (Guid)(WriteAndRead(source)["uuid"]);
94 69
 
95  
-            /*Binary b = new Binary(guid.ToByteArray());
96  
-            b.Subtype = Binary.TypeCode.Uuid;
97  
-            source.Append("uuid", b);*/
  70
+            Assert.AreEqual(expected, read, "UUID did not round trip right.");
  71
+        }
  72
+        
  73
+        [Test]
  74
+        public void TestMultiDimensionalArray(){
  75
+            int[][] arr = new int[3][];
  76
+            for(int a = 0; a < arr.Length; a++){
  77
+                int x = a + 1;
  78
+                arr[a] = new int[]{x * 1, x * 2, x * 3};
  79
+            }
  80
+            
  81
+            Document expected = new Document(){{"arr", arr}};
  82
+            Document read = WriteAndRead(expected);
  83
+               
  84
+            Assert.AreEqual(expected.ToString(), read.ToString());
  85
+        }
  86
+        
  87
+        [Test]
  88
+        public void TestMixedArrayContents(){
  89
+            Object[] arr = new Object[]{new string[]{"one", "two"},
  90
+                                        new string[]{"three", "four"}, 
  91
+                                        new Document(){{"id", "six"}}};
  92
+            Document expected = new Document(){{"arr", arr}};
  93
+            Document read = WriteAndRead(expected);
  94
+            
  95
+            string json = @"{ ""arr"": [ [ ""one"", ""two"" ], [ ""three"", ""four"" ], { ""id"": ""six"" } ] }";
  96
+            Assert.AreEqual(json, expected.ToString());
  97
+            
  98
+            Assert.IsTrue(read["arr"] is Object[], "Mixed array wasn't returned as Object[]");
  99
+            
  100
+            Assert.AreEqual(json, read.ToString());
  101
+            
  102
+            
  103
+        }
  104
+        
  105
+        protected Document WriteAndRead(Document source){
  106
+            MemoryStream ms = new MemoryStream();
  107
+            BsonWriter writer = new BsonWriter(ms);
98 108
 
99 109
             writer.Write(source);
100 110
             writer.Flush();
101 111
             ms.Seek(0, SeekOrigin.Begin);
102 112
 
103 113
             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  
-        }
  114
+            return reader.Read();
  115
+        }   
110 116
     }
111 117
 }
12  MongoDBDriver/Bson/BsonReader.cs
@@ -108,11 +108,7 @@ public Document Read ()
108 108
 
109 109
             case BsonDataType.Array:{
110 110
                 Document doc = this.ReadDocument();
111  
-                if (ElementsSameType (doc)) {
112  
-                    return ConvertToArray (doc);
113  
-                } else {
114  
-                    return doc;
115  
-                }
  111
+                return ConvertToArray (doc);
116 112
             }
117 113
             case BsonDataType.Regex:{
118 114
                 MongoRegex r = new MongoRegex ();
@@ -274,7 +270,11 @@ public Document Read ()
274 270
             foreach (String key in doc.Keys) {
275 271
                 if (ret == null) {
276 272
                     int length = doc.Keys.Count;
277  
-                    arrayType = doc[key].GetType ();
  273
+                    if(ElementsSameType(doc)){
  274
+                        arrayType = doc[key].GetType();
  275
+                    }else{
  276
+                        arrayType = typeof(Object);
  277
+                    }
278 278
                     ret = Array.CreateInstance (arrayType, length);
279 279
                 }
280 280
                 ret.SetValue (doc[key], idx);

0 notes on commit 6c4dbe3

Please sign in to comment.
Something went wrong with that request. Please try again.