forked from samus/mongodb-csharp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
25 changed files
with
714 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
*.pdb | ||
*.xml | ||
obj/* | ||
PartCover/* |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.