forked from samus/mongodb-csharp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add failing test for reusable cursors.
- Loading branch information
Showing
1 changed file
with
103 additions
and
93 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 |
---|---|---|
@@ -1,140 +1,150 @@ | ||
using System; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
|
||
namespace MongoDB.IntegrationTests | ||
{ | ||
[TestFixture] | ||
public class TestCursor : MongoTestBase | ||
{ | ||
public override string TestCollections { | ||
get { | ||
return "sorts,hintindex,smallreads,reads"; | ||
} | ||
public override string TestCollections | ||
{ | ||
get { return "sorts,hintindex,smallreads,reads"; } | ||
} | ||
|
||
public override void OnInit (){ | ||
|
||
public override void OnInit() | ||
{ | ||
//smallreads | ||
IMongoCollection smallreads = DB["smallreads"]; | ||
for(int j = 1; j < 5; j++){ | ||
smallreads.Insert(new Document(){{"x", 4},{"j", j}}); | ||
} | ||
smallreads.Insert(new Document(){{"x", 4}, {"j", 5}, {"n", 1}}); | ||
var smallreads = DB["smallreads"]; | ||
for(var j = 1; j < 5; j++) | ||
smallreads.Insert(new Document {{"x", 4}, {"j", j}}); | ||
smallreads.Insert(new Document {{"x", 4}, {"j", 5}, {"n", 1}}); | ||
|
||
IMongoCollection reads = DB["reads"]; | ||
for(int j = 1; j < 10000; j++){ | ||
reads.Insert(new Document(){{"x", 4},{"h", "hi"},{"j", j}}); | ||
} | ||
var reads = DB["reads"]; | ||
for(var j = 1; j < 10000; j++) | ||
reads.Insert(new Document {{"x", 4}, {"h", "hi"}, {"j", j}}); | ||
} | ||
|
||
|
||
[Test] | ||
public void TestCanReadSmall() | ||
public void TestCanLimit() | ||
{ | ||
ICursor c = DB["smallreads"].FindAll(); | ||
|
||
Assert.IsNotNull(c,"Cursor shouldn't be null"); | ||
int reads = 0; | ||
foreach(Document doc in c.Documents){ | ||
reads++; | ||
} | ||
var c = DB["reads"].FindAll().Limit(5); | ||
|
||
Assert.IsNotNull(c, "Cursor shouldn't be null"); | ||
var reads = c.Documents.Count(); | ||
Assert.IsTrue(reads > 0, "No documents were returned."); | ||
Assert.AreEqual(5, reads, "More than 5 documents in the small reads dataset"); | ||
Assert.AreEqual(5, reads); | ||
} | ||
|
||
[Test] | ||
public void TestCanReadAndKillCursor() | ||
{ | ||
var c = (Cursor)DB["reads"].FindAll(); | ||
|
||
Assert.IsNotNull(c, "Cursor shouldn't be null"); | ||
c.Documents.Any(); | ||
c.Dispose(); | ||
Assert.AreEqual(0, c.Id); | ||
} | ||
|
||
[Test] | ||
public void TestCanReadMore(){ | ||
Cursor c = (Cursor)DB["reads"].FindAll(); | ||
|
||
Assert.IsNotNull(c,"Cursor shouldn't be null"); | ||
int reads = 0; | ||
int idchanges = 0; | ||
public void TestCanReadMore() | ||
{ | ||
var c = (Cursor)DB["reads"].FindAll(); | ||
|
||
Assert.IsNotNull(c, "Cursor shouldn't be null"); | ||
var reads = 0; | ||
var idchanges = 0; | ||
long id = 0; | ||
foreach(Document doc in c.Documents){ | ||
foreach(var doc in c.Documents) | ||
{ | ||
reads++; | ||
if(c.Id != id){ | ||
if(c.Id != id) | ||
{ | ||
idchanges++; | ||
id = c.Id; | ||
} | ||
} | ||
Assert.IsTrue(reads > 0, "No documents were returned."); | ||
Assert.IsTrue(idchanges > 0,String.Format("ReadMore message never sent. {0} changes seen", idchanges)); | ||
Assert.AreEqual(9999,reads, "Not all documents returned."); | ||
System.Console.Out.Write(String.Format("{0} records read", reads)); | ||
|
||
|
||
Assert.IsTrue(idchanges > 0, String.Format("ReadMore message never sent. {0} changes seen", idchanges)); | ||
Assert.AreEqual(9999, reads, "Not all documents returned."); | ||
Console.Out.Write(String.Format("{0} records read", reads)); | ||
} | ||
|
||
[Test] | ||
public void TestCanReadAndKillCursor() | ||
public void TestCanReuseCursor() | ||
{ | ||
var c = (Cursor)DB["reads"].FindAll(); | ||
Assert.IsNotNull(c,"Cursor shouldn't be null"); | ||
foreach(Document doc in c.Documents){ | ||
break; | ||
} | ||
c.Dispose(); | ||
Assert.AreEqual(0,c.Id); | ||
|
||
Assert.IsNotNull(c, "Cursor shouldn't be null"); | ||
|
||
var firstCount = c.Documents.Count(); | ||
var secondCount = c.Documents.Count(); | ||
|
||
Assert.AreEqual(firstCount,secondCount); | ||
} | ||
|
||
[Test] | ||
public void TestCanLimit(){ | ||
ICursor c = DB["reads"].FindAll().Limit(5); | ||
|
||
Assert.IsNotNull(c,"Cursor shouldn't be null"); | ||
int reads = 0; | ||
foreach(Document doc in c.Documents){ | ||
reads++; | ||
} | ||
public void TestCanReadSmall() | ||
{ | ||
var c = DB["smallreads"].FindAll(); | ||
|
||
Assert.IsNotNull(c, "Cursor shouldn't be null"); | ||
var reads = c.Documents.Count(); | ||
Assert.IsTrue(reads > 0, "No documents were returned."); | ||
Assert.AreEqual(5, reads); | ||
Assert.AreEqual(5, reads, "More than 5 documents in the small reads dataset"); | ||
} | ||
|
||
[Test] | ||
public void TestExplain() | ||
{ | ||
var exp = DB["reads"].FindAll().Limit(5).Skip(5).Sort("x").Explain(); | ||
Assert.IsTrue(exp.Contains("cursor")); | ||
Assert.IsTrue(exp.Contains("n")); | ||
Assert.IsTrue(exp.Contains("nscanned")); | ||
} | ||
|
||
[Test] | ||
public void TestHint() | ||
{ | ||
var reads = DB["reads"]; | ||
var hint = new Document().Add("x", IndexOrder.Ascending); | ||
|
||
var exp = reads.FindAll().Hint(hint).Explain(); | ||
Assert.IsTrue(exp.Contains("$err"), "No error found"); | ||
|
||
reads.MetaData.CreateIndex("hintindex", hint, false); | ||
exp = reads.FindAll().Hint(hint).Explain(); | ||
|
||
Assert.IsTrue(exp.Contains("cursor")); | ||
Assert.IsTrue(exp.Contains("n")); | ||
Assert.IsTrue(exp.Contains("nscanned")); | ||
} | ||
|
||
[Test] | ||
public void TestSort(){ | ||
IMongoCollection sorts = DB["sorts"]; | ||
int[] randoms = new int[]{4,6,8,9,1,3,2,5,7,0}; | ||
foreach(int x in randoms){ | ||
public void TestSort() | ||
{ | ||
var sorts = DB["sorts"]; | ||
var randoms = new[] {4, 6, 8, 9, 1, 3, 2, 5, 7, 0}; | ||
foreach(var x in randoms) | ||
sorts.Insert(new Document().Add("x", randoms[x])); | ||
} | ||
Assert.AreEqual(randoms.Length, sorts.Count()); | ||
|
||
int exp = 0; | ||
foreach(Document doc in sorts.FindAll().Sort("x", IndexOrder.Ascending).Documents){ | ||
|
||
var exp = 0; | ||
foreach(var doc in sorts.FindAll().Sort("x", IndexOrder.Ascending).Documents) | ||
{ | ||
Assert.AreEqual(exp, Convert.ToInt32(doc["x"])); | ||
exp++; | ||
} | ||
Assert.AreEqual(randoms.Length, exp); | ||
|
||
exp = 9; | ||
foreach(Document doc in sorts.FindAll().Sort("x", IndexOrder.Descending).Documents){ | ||
foreach(var doc in sorts.FindAll().Sort("x", IndexOrder.Descending).Documents) | ||
{ | ||
Assert.AreEqual(exp, Convert.ToInt32(doc["x"])); | ||
exp--; | ||
} | ||
Assert.AreEqual(-1, exp); | ||
} | ||
|
||
[Test] | ||
public void TestExplain(){ | ||
Document exp = DB["reads"].FindAll().Limit(5).Skip(5).Sort("x").Explain(); | ||
Assert.IsTrue(exp.Contains("cursor")); | ||
Assert.IsTrue(exp.Contains("n")); | ||
Assert.IsTrue(exp.Contains("nscanned")); | ||
} | ||
|
||
[Test] | ||
public void TestHint(){ | ||
IMongoCollection reads = DB["reads"]; | ||
Document hint = new Document().Add("x",IndexOrder.Ascending); | ||
|
||
Document exp = reads.FindAll().Hint(hint).Explain(); | ||
Assert.IsTrue(exp.Contains("$err"), "No error found"); | ||
|
||
reads.MetaData.CreateIndex("hintindex",hint,false); | ||
exp = reads.FindAll().Hint(hint).Explain(); | ||
|
||
Assert.IsTrue(exp.Contains("cursor")); | ||
Assert.IsTrue(exp.Contains("n")); | ||
Assert.IsTrue(exp.Contains("nscanned")); | ||
} | ||
} | ||
} | ||
} |