Permalink
Browse files

Added Database.FollowReference to dereference a DBRef.cs

Changed DBRef to use an object for the value to account for non OId keys.
Added some validation logic to the Oid class to avoid invalid values.
Created a database metadata class to do administration on database objects.
Added the deprecated BsonNull and BsonUndefined classes because asking for a list of collections as the B
Added reconnect try on communication exception.
Added communication exceptions
Removed some unnecessary files
Cleaned up some tests.
  • Loading branch information...
1 parent 06b01ba commit cdef7f0cec4fd459222d9f547a0840a544928c35 @samus committed Sep 8, 2009
@@ -2,3 +2,4 @@
*.pdb
*.xml
obj/*
+PartCover/*
View
No changes.
View
@@ -32,7 +32,30 @@ public void TestAllBytesWritten()
Assert.IsTrue(output.Length > 0);
Assert.AreEqual("3A-00-00-00-00-00-00-00-00-00-00-00-D4-07-00-00-00-00-00-00-54-65-73-74-44-42-2E-54-65-73-74-43-6F-6C-00-00-00-00-00-00-00-00-00-0F-00-00-00-10-63-6F-6C-31-00-01-00-00-00-00", hexdump);
-
+ }
+
+ [Test]
+ public void TestWriteMessageTwice(){
+ string expectedHex = "3A-00-00-00-00-00-00-00-00-00-00-00-D4-07-00-00-00-00-00-00-54-65-73-74-44-42-2E-54-65-73-74-43-6F-6C-00-00-00-00-00-00-00-00-00-0F-00-00-00-10-63-6F-6C-31-00-01-00-00-00-00";
+ BsonDocument query = new BsonDocument();
+ query.Add("col1", BsonConvert.From(1));
+
+ QueryMessage msg = new QueryMessage(query,"TestDB.TestCol");
+ MemoryStream buffer = new MemoryStream();
+ msg.Write(buffer);
+
+ Byte[] output = buffer.ToArray();
+ String hexdump = BitConverter.ToString(output);
+
+ MemoryStream buffer2 = new MemoryStream();
+ msg.Write(buffer2);
+
+ Byte[] output2 = buffer.ToArray();
+ String hexdump2 = BitConverter.ToString(output2);
+
+ Assert.AreEqual(expectedHex,hexdump);
+ Assert.AreEqual(hexdump,hexdump2);
+
}
}
}
@@ -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">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -30,10 +30,10 @@
<Reference Include="nunit.core">
<Package>nunit-2.4</Package>
</Reference>
- <Reference Include="System" />
- <Reference Include="nunit.framework, Version=2.4.7.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
+ <Reference Include="nunit.framework">
<Package>nunit-2.4</Package>
</Reference>
+ <Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bson\TestBsonArray.cs" />
@@ -48,6 +48,9 @@
<Compile Include="TestCollection.cs" />
<Compile Include="TestCollectionMetaData.cs" />
<Compile Include="TestCursor.cs" />
+ <Compile Include="TestDatabase.cs" />
+ <Compile Include="TestDatabaseMetaData.cs" />
+ <Compile Include="TestDBRef.cs" />
<Compile Include="TestDocument.cs" />
<Compile Include="TestMongo.cs" />
<Compile Include="TestConnection.cs" />
@@ -56,6 +59,8 @@
<None Include="test-data\tests.reads.txt" />
<None Include="test-data\tests.smallreads.txt" />
<Compile Include="Bson\TestBsonRegex.cs" />
+ <Compile Include="TestMongoExceptions.cs" />
+ <Compile Include="TestOid.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MongoDBDriver\MongoDB.Driver.csproj">
@@ -65,9 +70,4 @@
<Folder Include="IO\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
- <ProjectExtensions>
- <MonoDevelop>
- <Properties InternalTargetFrameworkVersion="3.5" />
- </MonoDevelop>
- </ProjectExtensions>
</Project>
View
@@ -1,49 +1,72 @@
using System;
+using System.IO;
+using System.Net;
+using System.Net.Sockets;
+
using NUnit.Framework;
-using MongoDB.Driver;
using MongoDB.Driver.IO;
using MongoDB.Driver.Bson;
namespace MongoDB.Driver
{
-
-
[TestFixture()]
public class TestConnection
- {
-
-// [Test()]
-// public void TestSendMsgMessage(){
-// Connection conn = new Connection("10.141.153.2");
-// //Connection conn = new Connection();
-// conn.Open();
-// conn.SendMsgMessage("Hello MongoDB!");
-// conn.SendMsgMessage("Hello MongoDB2!");
-// conn.SendMsgMessage("Hello MongoDB3!");
-// conn.SendMsgMessage("Hello MongoDB4!");
-// conn.SendMsgMessage("Hello MongoDB5!");
-// conn.SendMsgMessage("Hello MongoDB6!");
-// conn.SendMsgMessage("Hello MongoDB7!");
-// conn.SendMsgMessage("Hello MongoDB8!");
-// conn.Close();
-// }
-
+ {
[Test]
public void TestSendQueryMessage(){
//Connection conn = new Connection("10.141.153.2");
Connection conn = new Connection();
conn.Open();
+ QueryMessage qmsg = generateQueryMessage();
+ conn.SendTwoWayMessage(qmsg);
+
+ conn.Close();
+ }
+
+ [Test]
+ public void TestReconnectOnce(){
+ Connection conn = new Connection();
+ conn.Open();
+
+ WriteBadMessage(conn);
+ try{
+ QueryMessage qmsg = generateQueryMessage();
+ ReplyMessage rmsg = conn.SendTwoWayMessage(qmsg);
+
+ }catch(IOException){
+ //Should be able to resend.
+ Assert.IsTrue(conn.State == ConnectionState.Opened);
+ QueryMessage qmsg = generateQueryMessage();
+ ReplyMessage rmsg = conn.SendTwoWayMessage(qmsg);
+ Assert.IsNotNull(rmsg);
+
+ }
+ }
+
+ protected void WriteBadMessage(Connection conn){
+ //Write a bad message to the socket to force mongo to shut down our connection.
+ BinaryWriter writer = new BinaryWriter(conn.Tcpclnt.GetStream());
+ System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
+ Byte[] msg = encoding.GetBytes("Goodbye MongoDB!");
+ writer.Write(16 + msg.Length + 1);
+ writer.Write(1);
+ writer.Write(1);
+ writer.Write(1001);
+ writer.Write(msg);
+ writer.Write((byte)0);
+ }
+
+ protected QueryMessage generateQueryMessage(){
BsonDocument qdoc = new BsonDocument();
qdoc.Add("listDatabases", new BsonNumber(1.0));
//QueryMessage qmsg = new QueryMessage(qdoc,"system.namespaces");
QueryMessage qmsg = new QueryMessage(qdoc,"admin.$cmd");
qmsg.NumberToReturn = -1;
- conn.SendTwoWayMessage(qmsg);
- conn.Close();
+ return qmsg;
}
}
}
@@ -0,0 +1,69 @@
+
+
+using System;
+using NUnit.Framework;
+
+namespace MongoDB.Driver
+{
+ [TestFixture]
+ public class TestDBRef
+ {
+ [Test]
+ public void TestEqualsAreSameObject()
+ {
+ DBRef r = new DBRef("tests","2312314");
+ Assert.AreEqual(r,r);
+ }
+
+ [Test]
+ public void TestEqualsUsingSameValues(){
+ String colname = "tests";
+ String id = "32312312";
+ DBRef r = new DBRef(colname,id);
+ DBRef r2 = new DBRef(colname, id);
+
+ Assert.AreEqual(r,r2);
+ }
+
+ [Test]
+ public void TestFromDocument(){
+ String colname = "tests";
+ String id = "32312312";
+ Document doc = new Document().Append(DBRef.RefName,colname).Append(DBRef.IdName,id);
+
+ DBRef expected = new DBRef(colname, id);
+ Assert.AreEqual(expected, DBRef.FromDocument(doc));
+ }
+
+ [Test]
+ public void TestFromIncompleteDocumentThrowsArguementException(){
+ bool thrown = false;
+ try {
+ DBRef bad = DBRef.FromDocument(new Document().Append(DBRef.RefName,"tests"));
+ } catch (ArgumentException ae) {
+ thrown = true;
+ }
+ Assert.IsTrue(thrown,"ArgumentException should have been thrown when trying to create convert from incomplete document");
+
+ }
+
+ [Test]
+ public void TestIsDocumentDBRef(){
+ Document doc = new Document();
+
+ Assert.IsFalse(DBRef.IsDocumentDBRef(null));
+ Assert.IsFalse(DBRef.IsDocumentDBRef(doc));
+
+ doc[DBRef.RefName] = "tests";
+ Assert.IsFalse(DBRef.IsDocumentDBRef(doc));
+
+ doc.Remove(DBRef.RefName);
+ doc[DBRef.IdName] = "12312131";
+ Assert.IsFalse(DBRef.IsDocumentDBRef(doc));
+
+ doc[DBRef.RefName] = "tests";
+ Assert.IsTrue(DBRef.IsDocumentDBRef(doc));
+
+ }
+ }
+}
@@ -0,0 +1,77 @@
+using System;
+using System.Collections.Generic;
+using NUnit.Framework;
+
+namespace MongoDB.Driver
+{
+ [TestFixture]
+ public class TestDatabase
+ {
+ Mongo mongo = new Mongo();
+
+ [Test]
+ public void TestFollowReference(){
+ Database tests = mongo["tests"];
+ Oid id = new Oid("4a7067c30a57000000008ecb");
+ DBRef rf = new DBRef("reads", id);
+
+ Document target = tests.FollowReference(rf);
+ Assert.IsNotNull(target, "FollowReference returned null");
+ Assert.IsTrue(target.Contains("j"));
+ Assert.AreEqual((double)9980, (double)target["j"]);
+ }
+
+ [Test]
+ public void TestFollowNonReference(){
+ Database tests = mongo["tests"];
+ Oid id = new Oid("BAD067c30a57000000008ecb");
+ DBRef rf = new DBRef("reads", id);
+
+ Document target = tests.FollowReference(rf);
+ Assert.IsNull(target, "FollowReference returned wasn't null");
+ }
+
+ [Test]
+ public void TestReferenceNonOid(){
+ Database tests = mongo["tests"];
+ Collection refs = tests["refs"];
+
+ Document doc = new Document().Append("_id",123).Append("msg", "this has a non oid key");
+ refs.Insert(doc);
+
+ DBRef rf = new DBRef("refs",123);
+
+ Document recv = tests.FollowReference(rf);
+
+ Assert.IsNotNull(recv);
+ Assert.IsTrue(recv.Contains("msg"));
+ Assert.AreEqual(recv["_id"], (long)123);
+ }
+
+ [Test]
+ public void TestGetCollectionNames(){
+ List<String> names = mongo["tests"].GetCollectionNames();
+ Assert.IsNotNull(names,"No collection names returned");
+ Assert.IsTrue(names.Count > 0);
+ Assert.IsTrue(names.Contains("tests.reads"));
+
+ }
+
+
+ [TestFixtureSetUp]
+ public void Init(){
+ mongo.Connect();
+ cleanDB();
+ }
+
+ [TestFixtureTearDown]
+ public void Dispose(){
+ mongo.Disconnect();
+ }
+
+ protected void cleanDB(){
+ mongo["tests"]["$cmd"].FindOne(new Document().Append("drop","refs"));
+
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit cdef7f0

Please sign in to comment.