Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/samus/mongodb-csharp
Browse files Browse the repository at this point in the history
Conflicts:
	MongoDB.Net-Tests/Bson/TestBsonDocument.cs
	MongoDB.Net-Tests/MongoDB.Driver.Tests.csproj
	MongoDB.Net-Tests/PartCover/Coverage.Xml
	MongoDB.Net-Tests/TestCollection.cs
	MongoDB.Net-Tests/TestCursor.cs
	MongoDBDriver/Bson/BsonConvert.cs
	MongoDBDriver/Bson/BsonDocument.cs
	MongoDBDriver/Bson/BsonLong.cs
	MongoDBDriver/Bson/Types.cs
	MongoDBDriver/Collection.cs
	MongoDBDriver/Cursor.cs
	MongoDBDriver/MongoDB.Driver.csproj
  • Loading branch information
Sedward committed Aug 24, 2009
2 parents 3f34a5e + 2968949 commit 929c2ab
Show file tree
Hide file tree
Showing 14 changed files with 10,560 additions and 103 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -11,3 +11,7 @@

test-results/*.xml
MongoDB.Net-Tests/test-results/MongoDB.Driver.Tests.csproj.test-cache

*.suo
/_UpgradeReport_Files/*
obj/*
1 change: 1 addition & 0 deletions MongoDB.Net-Tests/.gitignore
@@ -1,3 +1,4 @@
*.dll
*.pdb
*.xml
obj/*
74 changes: 74 additions & 0 deletions MongoDB.Net-Tests/Bson/TestBsonArray.cs
@@ -0,0 +1,74 @@
/*
* User: scorder
*/

using System;
using System.IO;

using NUnit.Framework;

namespace MongoDB.Driver.Bson
{
[TestFixture]
public class TestBsonArray
{
[Test]
public void TestType(){
Assert.AreEqual(BsonDataType.Array, (BsonDataType)new BsonArray().TypeNum);
}

[Test]
public void TestOnlyNumericKeys(){
bool thrown = false;
BsonArray ba = new BsonArray();
ba.Add("1","A");
Assert.IsNotNull(ba["1"]);
try {
ba.Add("A","A");
} catch (ArgumentOutOfRangeException) {
thrown = true;
}
if(thrown != true) Assert.Fail("Exception should have been thrown");

thrown = false;
try {
ba["A"] = new BsonElement("A", new BsonString("A"));
} catch (ArgumentOutOfRangeException) {
thrown = true;
}
if(thrown != true) Assert.Fail("Exception should have been thrown");
}

[Test]
public void TestKeysCanBeInts(){
BsonArray ba = new BsonArray();
ba.Add(1,"A");
Assert.AreEqual("A", ba[1].Val.ToNative());
}

[Test]
public void TestKeyHoles(){
BsonArray ba = new BsonArray();
ba.Add(1,"A");
ba.Add(3,"C");
Assert.IsNull(ba[2]);
}

[Test]
public void TestKeyOrdering(){
BsonArray ba = new BsonArray();
ba.Add(1,"A");
ba.Add(2,"B");
ba.Add(5,"E");
ba.Add(3,"C");
ba.Add(4,"D");

int ikey = 1;
foreach(string key in ba.Keys){
Assert.AreEqual(ikey, int.Parse(key));
ikey++;
}
}

}
}
1 change: 0 additions & 1 deletion MongoDB.Net-Tests/MongoDB.Driver.Tests.csproj
Expand Up @@ -113,4 +113,3 @@
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
3 changes: 1 addition & 2 deletions MongoDB.Net-Tests/PartCover/Coverage.Xml
Expand Up @@ -46213,5 +46213,4 @@
<pt visit="1" pos="0" len="9" />
</code>
</method>
</type>
</PartCoverReport>
</type>
219 changes: 219 additions & 0 deletions MongoDB.Net-Tests/TestCollection.cs
@@ -1,3 +1,4 @@
<<<<<<< HEAD
/*
* User: scorder
*/
Expand Down Expand Up @@ -205,3 +206,221 @@ public class TestCollection
}
}
}
=======
using System;
using NUnit.Framework;
using MongoDB.Driver.Bson;

namespace MongoDB.Driver
{
[TestFixture]
public class TestCollection
{
Mongo db = new Mongo();

[Test]
public void TestFindOne(){
Document query = new Document();
query["j"] = 10;
Document result = db["tests"]["reads"].FindOne(query);
Assert.IsNotNull(result);
Assert.AreEqual(4, result["x"]);
Assert.AreEqual(10, result["j"]);

}

[Test]
public void TestFindOneNotThere(){
Document query = new Document();
query["not_there"] = 10;
Document result = db["tests"]["reads"].FindOne(query);
Assert.IsNull(result);
}

[Test]
public void TestFindAttributeLimit(){
Document query = new Document();
query["j"] = 10;
Document fields = new Document();
fields["x"] = 1;

Cursor c = db["tests"]["reads"].Find(query,-1,0,fields);
foreach(Document result in c.Documents){
Assert.IsNotNull(result);
Assert.AreEqual(4, result["x"]);
Assert.IsNull(result["j"]);
}
}

[Test]
public void TestFindGTRange(){
Document query = new Document();
query["j"] = new Document().Append("$gt",20);

Cursor c = db["tests"]["reads"].Find(query);
foreach(Document result in c.Documents){
Assert.IsNotNull(result);
Object j = result["j"];
Assert.IsTrue((double)j > 20);
}
}

[Test]
public void TestSimpleInsert(){
Collection inserts = db["tests"]["inserts"];
Document indoc = new Document();
indoc["song"] = "Palmdale";
indoc["artist"] = "Afroman";
indoc["year"] = 1999;

inserts.Insert(indoc);

Document result = inserts.FindOne(new Document().Append("song","Palmdale"));
Assert.IsNotNull(result);
Assert.AreEqual(1999,result["year"]);
}

[Test]
public void TestReallySimpleInsert(){
Collection inserts = db["tests"]["inserts"];
Document indoc = new Document();
indoc["y"] = 1;
indoc["x"] = 2;
inserts.Insert(indoc);

Document result = inserts.FindOne(new Document().Append("x",2));
Assert.IsNotNull(result);
Assert.AreEqual(1,result["y"]);
}

[Test]
public void TestArrayInsert(){
Collection inserts = db["tests"]["inserts"];
Document indoc1 = new Document();
indoc1["song"] = "The Axe";
indoc1["artist"] = "Tinsley Ellis";
indoc1["year"] = 2006;

Document indoc2 = new Document();
indoc2["song"] = "The Axe2";
indoc2["artist"] = "Tinsley Ellis2";
indoc2["year"] = 2008;

inserts.Insert(new Document[]{indoc1,indoc2});

Document result = inserts.FindOne(new Document().Append("song","The Axe"));
Assert.IsNotNull(result);
Assert.AreEqual(2006,result["year"]);

result = inserts.FindOne(new Document().Append("song","The Axe2"));
Assert.IsNotNull(result);
Assert.AreEqual(2008,result["year"]);
}

[Test]
public void TestDelete(){
Collection deletes = db["tests"]["deletes"];
Document doc = new Document();
doc["y"] = 1;
doc["x"] = 2;
deletes.Insert(doc);

Document selector = new Document().Append("x",2);

Document result = deletes.FindOne(selector);
Assert.IsNotNull(result);
Assert.AreEqual(1,result["y"]);

deletes.Delete(selector);
result = deletes.FindOne(selector);
Assert.IsNull(result,"Shouldn't have been able to find a document that was deleted");

}

[Test]
public void TestUpdateUpsertNotExisting(){
Collection updates = db["tests"]["updates"];
Document doc = new Document();
doc["First"] = "Sam";
doc["Last"] = "Corder";

updates.Update(doc);
Document selector = new Document().Append("Last", "Corder");
Document result = updates.FindOne(selector);
Assert.IsNotNull(result);
Assert.AreEqual("Sam", result["First"]);
}

[Test]
public void TestUpdateUpsertExisting(){
Collection updates = db["tests"]["updates"];
Document doc = new Document();
doc["First"] = "Mtt";
doc["Last"] = "Brewer";

updates.Insert(doc);
Document selector = new Document().Append("Last", "Brewer");
doc = updates.FindOne(selector);
Assert.IsNotNull(doc);
Assert.AreEqual("Mtt", doc["First"]);
Assert.IsNotNull(doc["_id"]);

doc["First"] = "Matt";
updates.Update(doc);

Document result = updates.FindOne(selector);
Assert.IsNotNull(result);
Assert.AreEqual("Matt", result["First"]);

}

[Test]
public void TestUpdateMany(){
Collection updates = db["tests"]["updates"];

updates.Insert(new Document().Append("Last", "Cordr").Append("First","Sam"));
updates.Insert(new Document().Append("Last", "Cordr").Append("First","Sam2"));

Document selector = new Document().Append("Last", "Cordr");
Cursor results = updates.Find(selector);
bool found = false;
foreach(Document doc in results.Documents){
Assert.AreEqual("Cordr", doc["Last"]);
found = true;
}
Assert.IsTrue(found,"Should have found docs inserted for TestMany");

Document updateData = new Document().Append("Last", "Corder2");
updates.UpdateAll(updateData, selector);

selector["Last"] = "Corder2";
results = updates.Find(selector);
found = false;
foreach(Document doc in results.Documents){
Assert.AreEqual("Corder2", doc["Last"]);
Assert.IsNotNull(doc["First"],"First name should not disappear");
found = true;
}
Assert.IsTrue(found,"Should have found docs updated for TestMany");
}

[TestFixtureSetUp]
public void Init(){
db.Connect();
cleanDB();
}

[TestFixtureTearDown]
public void Dispose(){
//cleanDB();
db.Disconnect();
}

protected void cleanDB(){
db["tests"]["$cmd"].FindOne(new Document().Append("drop","inserts"));

db["tests"]["$cmd"].FindOne(new Document().Append("drop","updates"));
}
}
}
>>>>>>> 296894954186bcd92ae9075c9a89be6f5f2c9f57

0 comments on commit 929c2ab

Please sign in to comment.