Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Added Database.FollowReference to dereference a DBRef.cs
Browse files Browse the repository at this point in the history
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
samus committed Sep 8, 2009
1 parent 06b01ba commit cdef7f0
Show file tree
Hide file tree
Showing 25 changed files with 714 additions and 127 deletions.
1 change: 1 addition & 0 deletions MongoDB.Net-Tests/.gitignore
Expand Up @@ -2,3 +2,4 @@
*.pdb *.pdb
*.xml *.xml
obj/* obj/*
PartCover/*
Empty file modified MongoDB.Net-Tests/Bson/TestBsonConvert.cs 100755 → 100644
Empty file.
25 changes: 24 additions & 1 deletion MongoDB.Net-Tests/IO/TestQueryMessage.cs 100644 → 100755
Expand Up @@ -32,7 +32,30 @@ public void TestAllBytesWritten()
Assert.IsTrue(output.Length > 0); 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); 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);

} }
} }
} }
16 changes: 8 additions & 8 deletions MongoDB.Net-Tests/MongoDB.Driver.Tests.csproj
@@ -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"> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -30,10 +30,10 @@
<Reference Include="nunit.core"> <Reference Include="nunit.core">
<Package>nunit-2.4</Package> <Package>nunit-2.4</Package>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="nunit.framework">
<Reference Include="nunit.framework, Version=2.4.7.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
<Package>nunit-2.4</Package> <Package>nunit-2.4</Package>
</Reference> </Reference>
<Reference Include="System" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Bson\TestBsonArray.cs" /> <Compile Include="Bson\TestBsonArray.cs" />
Expand All @@ -48,6 +48,9 @@
<Compile Include="TestCollection.cs" /> <Compile Include="TestCollection.cs" />
<Compile Include="TestCollectionMetaData.cs" /> <Compile Include="TestCollectionMetaData.cs" />
<Compile Include="TestCursor.cs" /> <Compile Include="TestCursor.cs" />
<Compile Include="TestDatabase.cs" />
<Compile Include="TestDatabaseMetaData.cs" />
<Compile Include="TestDBRef.cs" />
<Compile Include="TestDocument.cs" /> <Compile Include="TestDocument.cs" />
<Compile Include="TestMongo.cs" /> <Compile Include="TestMongo.cs" />
<Compile Include="TestConnection.cs" /> <Compile Include="TestConnection.cs" />
Expand All @@ -56,6 +59,8 @@
<None Include="test-data\tests.reads.txt" /> <None Include="test-data\tests.reads.txt" />
<None Include="test-data\tests.smallreads.txt" /> <None Include="test-data\tests.smallreads.txt" />
<Compile Include="Bson\TestBsonRegex.cs" /> <Compile Include="Bson\TestBsonRegex.cs" />
<Compile Include="TestMongoExceptions.cs" />
<Compile Include="TestOid.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MongoDBDriver\MongoDB.Driver.csproj"> <ProjectReference Include="..\MongoDBDriver\MongoDB.Driver.csproj">
Expand All @@ -65,9 +70,4 @@
<Folder Include="IO\" /> <Folder Include="IO\" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<MonoDevelop>
<Properties InternalTargetFrameworkVersion="3.5" />
</MonoDevelop>
</ProjectExtensions>
</Project> </Project>
69 changes: 46 additions & 23 deletions MongoDB.Net-Tests/TestConnection.cs 100644 → 100755
@@ -1,49 +1,72 @@


using System; using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

using NUnit.Framework; using NUnit.Framework;


using MongoDB.Driver;
using MongoDB.Driver.IO; using MongoDB.Driver.IO;
using MongoDB.Driver.Bson; using MongoDB.Driver.Bson;


namespace MongoDB.Driver namespace MongoDB.Driver
{ {


[TestFixture()] [TestFixture()]
public class TestConnection 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] [Test]
public void TestSendQueryMessage(){ public void TestSendQueryMessage(){
//Connection conn = new Connection("10.141.153.2"); //Connection conn = new Connection("10.141.153.2");
Connection conn = new Connection(); Connection conn = new Connection();
conn.Open(); 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(); BsonDocument qdoc = new BsonDocument();
qdoc.Add("listDatabases", new BsonNumber(1.0)); qdoc.Add("listDatabases", new BsonNumber(1.0));
//QueryMessage qmsg = new QueryMessage(qdoc,"system.namespaces"); //QueryMessage qmsg = new QueryMessage(qdoc,"system.namespaces");
QueryMessage qmsg = new QueryMessage(qdoc,"admin.$cmd"); QueryMessage qmsg = new QueryMessage(qdoc,"admin.$cmd");
qmsg.NumberToReturn = -1; qmsg.NumberToReturn = -1;
conn.SendTwoWayMessage(qmsg);


conn.Close(); return qmsg;
} }
} }
} }
69 changes: 69 additions & 0 deletions MongoDB.Net-Tests/TestDBRef.cs
@@ -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));

}
}
}
77 changes: 77 additions & 0 deletions MongoDB.Net-Tests/TestDatabase.cs
@@ -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"));

}
}
}

0 comments on commit cdef7f0

Please sign in to comment.