Permalink
Browse files

Better array handling

Binary data type support
More fault tolerant open method for PairedConnection contributed by Andrew Kempe
  • Loading branch information...
Samuel Corder Samuel Corder
Samuel Corder authored and Samuel Corder committed Dec 9, 2009
1 parent 759a7d5 commit 337cda355dea1aaafa0bd84b52ae140cfae9eb40
View
@@ -14,6 +14,7 @@
*.usertasks
test-results/*.xml
+MongoDB.Net-Tests/bin/*
MongoDB.Net-Tests/test-results/MongoDB.Driver.Tests.csproj.test-cache
MongoDB.Linq.Tests/test-results/MongoDB.Linq.Tests.csproj.test-cache
View
@@ -69,6 +69,32 @@ public class TestBsonArray
ikey++;
}
}
-
+ [Test]
+ public void TestToNativeProducesArrayWithAllValuesTheSameType(){
+ Document[] songs = new[] {
+ new Document().Append("title", "Let The Music Set You Free").Append("length", "5:15"),
+ new Document().Append("title", "Sally Likes to Run").Append("length", "4:06"),
+ new Document().Append("title", "Deliveries After Dark").Append("length", "4:17"),
+ new Document().Append("title", "Theme From The Godfather").Append("length", "3:06"),
+ new Document().Append("title", "Grown Man Crying Blues").Append("length", "8:09"),
+ };
+ BsonArray ba = new BsonArray();
+ ba.Add(1,"A");
+ ba.Add(2,"B");
+ ba.Add(3,"C");
+ Object obj = ba.ToNative();
+ Assert.AreEqual(typeof(string[]),obj.GetType());
+ }
+
+ [Test]
+ public void TestElementsSameType(){
+ BsonArray ba = new BsonArray();
+ ba.Add(1,"A");
+ ba.Add(2,"B");
+ ba.Add(3,"C");
+
+ Assert.IsTrue(ba.ElementsSameType());
+
+ }
}
}
View
@@ -1,31 +1,121 @@
-using System;
+using System;
using System.IO;
using NUnit.Framework;
+using MongoDB.Driver;
+
namespace MongoDB.Driver.Bson
{
- [TestFixture]
+ [TestFixture]
public class TestBsonBinary
{
- [Test]
- public void TestBinary()
- {
-
- byte[] data = File.ReadAllBytes(@"test-data\tests.binary.txt");
- BsonBinary binaryIn = new BsonBinary(new Binary(data));
- MemoryStream stream = new MemoryStream();
- BsonWriter bsonWriter = new BsonWriter(stream);
- binaryIn.Write(bsonWriter);
+ [Test]
+ public void TestRoundTrip(){
+ Document idoc = new Document();
+ idoc.Add("b",new Binary(new byte[]{(byte)1,(byte)2}));
+ BsonDocument bidoc = BsonConvert.From(idoc);
+
+ MemoryStream stream = new MemoryStream();
+ BsonWriter writer = new BsonWriter(stream);
+ bidoc.Write(writer);
+
+ stream.Seek(0,SeekOrigin.Begin);
+ BsonReader reader = new BsonReader(stream);
+ BsonDocument bodoc = new BsonDocument();
+ bodoc.Read(reader);
+ Document odoc = (Document) bodoc.ToNative();
+
+ Assert.AreEqual(idoc.ToString(), odoc.ToString());
+ }
+
+ [Test]
+ public void TestBinaryRead ()
+ {
+ string hex = "28000000075f6964004b1971811d8b0f00c0000000056461746100070000000203000000e188b400";
+
+ byte[] data = DecodeHex (hex);
+ MemoryStream inmem = new MemoryStream (data);
+ BsonReader inreader = new BsonReader (inmem);
+ BsonDocument indoc = new BsonDocument ();
+ indoc.Read (inreader);
+
+ MemoryStream outmem = new MemoryStream ();
+ BsonWriter outwriter = new BsonWriter (outmem);
+ indoc.Write (outwriter);
+ byte[] outdata = outmem.ToArray ();
+ String outhex = BitConverter.ToString (outdata);
+ outhex = outhex.Replace ("-", "");
+
+ Assert.AreEqual (hex, outhex.ToLower());
+
+ }
+
+ [Test]
+ public void TestSimpleBinaryStorage(){
+ Binary b = new Binary();
+ byte[] data = new byte[2];
+ data[0] = (byte)1;
+ data[1] = (byte)2;
+ b.Subtype = Binary.TypeCode.General;
+
+ BsonBinary binaryIn = new BsonBinary (new Binary (data));
+ MemoryStream stream = new MemoryStream ();
+ BsonWriter bsonWriter = new BsonWriter (stream);
+ binaryIn.Write (bsonWriter);
+
+ string hex = BitConverter.ToString (stream.ToArray());
+ hex = hex.Replace ("-", "");
+ Assert.AreEqual("0600000002020000000102",hex);
+
+ }
+
+ [Test]
+ public void TestSimpleBinaryRead(){
+ string hex = "0600000002020000000102";
+ byte[] data = DecodeHex (hex);
+ MemoryStream stream = new MemoryStream (data);
+
+ BsonReader reader = new BsonReader(stream);
+ BsonBinary binaryOut = new BsonBinary ();
+ int read = binaryOut.Read(reader);
+ Assert.AreEqual(hex.Length/2,read);
+ Assert.AreEqual((byte)2,binaryOut.Subtype);
+ Assert.AreEqual(2,binaryOut.Val.Length);
+ Assert.AreEqual(hex.Length/2, binaryOut.Size);
+
+ }
+
+ [Test]
+ public void TestSimpleUnknownRead(){
+ string hex = "0600000001020000000102";
+ byte[] data = DecodeHex (hex);
+ MemoryStream stream = new MemoryStream (data);
- stream.Position = 0;
- BsonReader reader = new BsonReader(stream);
- BsonBinary binaryOut = new BsonBinary();
- int size = reader.ReadInt32();
- binaryOut.Subtype = reader.ReadByte();
- binaryOut.Val = reader.ReadBytes(size);
- Assert.AreEqual(binaryIn.Val, binaryOut.Val);
- Assert.AreEqual(binaryIn.Subtype, binaryOut.Subtype);
- Assert.AreEqual(data.Length, binaryOut.Size);
- }
+ BsonReader reader = new BsonReader(stream);
+ BsonBinary binaryOut = new BsonBinary ();
+ int read = binaryOut.Read(reader);
+ Assert.AreEqual(hex.Length/2,read);
+ Assert.AreEqual((byte)1,binaryOut.Subtype);
+ Assert.AreEqual(6,binaryOut.Val.Length);
+ Assert.AreEqual(hex.Length/2, binaryOut.Size);
+
+ }
+
+ protected static byte[] DecodeHex (string val)
+ {
+ int numberChars = val.Length;
+
+ byte[] bytes = new byte[numberChars / 2];
+ for (int i = 0; i < numberChars; i += 2) {
+ try {
+ bytes[i / 2] = Convert.ToByte (val.Substring (i, 2), 16);
+ } catch {
+ //failed to convert these 2 chars, they may contain illegal charracters
+ bytes[i / 2] = 0;
+ }
+ }
+ return bytes;
+ }
+
}
}
@@ -75,6 +75,16 @@ public class TestBsonConvert
BsonType t = BsonConvert.From(letters);
Assert.AreEqual(typeof(BsonArray),t.GetType());
+ }
+ [Test]
+ public void TestFromDocumentWithDocumentArray(){
+ var doc = new Document().Append("foo", new[] {
+ new Document().Append("a", 1),
+ new Document().Append("b", 2),
+ new Document().Append("c", 3),
+ });
+ BsonType t = BsonConvert.From(doc);
+ Assert.AreEqual(t.Size,60);
}
}
}
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -29,6 +29,7 @@
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
+ <RootNamespace>MongoDB.Driver.Tests</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -47,21 +48,13 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
- <Reference Include="MongoDB.Driver, Version=1.0.3560.34266, Culture=neutral, processorArchitecture=MSIL">
- <SpecificVersion>False</SpecificVersion>
- <HintPath>..\MongoDBDriver\bin\Debug\MongoDB.Driver.dll</HintPath>
- </Reference>
- <Reference Include="nunit.core, Version=2.5.0.9122, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
- <SpecificVersion>False</SpecificVersion>
- <HintPath>..\..\..\..\..\..\API\NUnit\NUnit-2.5.0.9122\bin\net-2.0\lib\nunit.core.dll</HintPath>
- </Reference>
- <Reference Include="nunit.framework, Version=2.5.0.9122, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
- <SpecificVersion>False</SpecificVersion>
- <HintPath>..\..\..\..\..\..\API\NUnit\NUnit-2.5.0.9122\bin\net-2.0\nunit.framework.dll</HintPath>
- </Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
+ <Reference Include="nunit.framework, Version=2.4.3.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\redist\nunit.framework.dll</HintPath>
+ </Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Bson\TestBsonBinary.cs" />
@@ -93,6 +86,7 @@
<Compile Include="TestOidGenerator.cs" />
<Compile Include="TestPairedConnection.cs" />
<Compile Include="Bson\TestBsonCode.cs" />
+ <Compile Include="Bson\TestBsonArray.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MongoDBDriver\MongoDB.Driver.csproj">
@@ -115,10 +109,10 @@
<Visible>False</Visible>
</BootstrapperPackage>
</ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
- <Content Include="test-data\tests.binary.txt">
+ <EmbeddedResource Include="test-data\tests.binary.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
- </Content>
+ </EmbeddedResource>
</ItemGroup>
- <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
View
@@ -1,4 +1,4 @@
-using System;
+using System;
using NUnit.Framework;
using MongoDB.Driver.Bson;
@@ -84,27 +84,6 @@ public class TestCollection
}
}
- [Test]
- public void TestManualWhere(){
- Document query = new Document().Append("$where", new Code("this.j % 2 == 0"));
- ICursor c = db["tests"]["reads"].Find(query);
- foreach(Document result in c.Documents){
- Assert.IsNotNull(result);
- Object j = result["j"];
- Assert.IsTrue((double)j % 2 == 0);
- }
- }
-
- [Test]
- public void TestWhere(){
- ICursor c = db["tests"]["reads"].Find("this.j % 2 == 0");
- foreach(Document result in c.Documents){
- Assert.IsNotNull(result);
- Object j = result["j"];
- Assert.IsTrue((double)j % 2 == 0);
- }
- }
-
[Test]
public void TestFindOneObjectContainingUKPound(){
Document query = new Document();
@@ -177,6 +156,29 @@ public class TestCollection
Assert.AreEqual(2008,result["year"]);
}
+ [Test]
+ public void TestInsertOfArray(){
+ OidGenerator ogen = new OidGenerator();
+ IMongoCollection inserts = db["tests"]["inserts"];
+ Document album = new Document();
+ album["_id"] = ogen.Generate();
+ album["artist"] = "Popa Chubby";
+ album["title"] = "Deliveries After Dark";
+ album["songs"] = new[] {
+ new Document().Append("title", "Let The Music Set You Free").Append("length", "5:15").Append("_id", ogen.Generate()),
+ new Document().Append("title", "Sally Likes to Run").Append("length", "4:06").Append("_id", ogen.Generate()),
+ new Document().Append("title", "Deliveries After Dark").Append("length", "4:17").Append("_id", ogen.Generate()),
+ new Document().Append("title", "Theme From The Godfather").Append("length", "3:06").Append("_id", ogen.Generate()),
+ new Document().Append("title", "Grown Man Crying Blues").Append("length", "8:09").Append("_id", ogen.Generate()),
+ };
+ inserts.Insert(album);
+
+ Document result = inserts.FindOne(new Document().Append("songs.title","Deliveries After Dark"));
+ Assert.IsNotNull(result);
+
+ Assert.AreEqual(album.ToString(), result.ToString());
+ }
+
[Test]
public void TestDelete(){
IMongoCollection deletes = db["tests"]["deletes"];
@@ -1,4 +1,4 @@
-
+
using System;
@@ -58,7 +58,7 @@ public class TestOid
string hex = "4a7067c30a57000000008ecb";
Oid oid = new Oid(hex);
- Assert.AreEqual(hex,"ObjectId(\"" + oid.ToString() + "\")");
+ Assert.AreEqual("ObjectId(\"" + hex + "\")", oid.ToString());
}
[Test]
View
@@ -1,20 +1,27 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Text;
namespace MongoDB.Driver
{
- public class Binary
- {
+ public class Binary{
+ public enum TypeCode:byte{
+ Unknown = 0,
+ General = 2,
+ Uuid = 3,
+ Md5 = 5,
+ UserDefined = 80
+ }
+
private byte[] bytes;
public byte[] Bytes{
get { return this.bytes; }
set { this.bytes = value; }
}
- private byte subtype;
- public byte Subtype{
+ private Binary.TypeCode subtype;
+ public Binary.TypeCode Subtype{
get { return this.subtype; }
set { this.subtype = value; }
}
@@ -23,7 +30,7 @@ public class Binary
public Binary(byte[] value){
this.Bytes = value;
- this.Subtype = (byte)2;
+ this.Subtype = TypeCode.General;
}
Oops, something went wrong.

0 comments on commit 337cda3

Please sign in to comment.